一、ELF标准
1、wiki官方定义
参考: Executable and Linkable Format
2、标准格式
ELF结构 | 查看命令 |
1.ELF Header | readelf -h |
2.Program header table | readelf -l |
3.Section(s) | |
4.Section header table | readelf -S |
二、解析ELF开源库
1、C++开源库:ELFIO库
- github: ELFIOhttps://github.com/serge1/ELFIO
- 内容:支持ELF的解析和读写,并提供详细的api文档(elfio.pdf)
2、java开源库:jelf库
- github: jelf https://github.com/fornwall/jelf
- 内容:只支持ELF头文件的解析,sections内容只支持部分解析,不支持写入
三、ELF文件(hello_word.out)定制,重构writerELF功能
1、整体ELF结构变化
整体结构发生变化,各offset需要最后重新计算
- 原始结构
1.ELF Header |
2.Program header table |
3.Section(s) |
4.Section header table |
readelf -h hello_word.out查看:
- 新的结构
1.ELF Header |
2.Program header table |
3.Section header table |
4.Section(s) |
readelf -h hello_word.out查看:
2、section结构变化
- 原始结构,根据Address来排序
readelf -SW hello_word.out查看:
- 新的结构,.shstrtab内容需要根据新的sections结构重新实现
1.mandatory_sections(2个) | 【0】 【1】.shstrtab(是用来记录每个Section的名称的,每个Section Header Table的sh_name字段记录的就是这个Section的名称在.shstrtab中的偏移) |
2.alloc sections(14个) 【运行是需要runtime】 | 【2】.text 【3】.eh_frame 【4】nuksymstr 【5】nuksymtab 【6】nusymstr 【7】nusymtab 【8】nuusymstr 【9】nuusymtab 【10】.rodata 【11】.rtl.data 【12】.rtl.bss 【13】.data 【14】.noinit 【15】.bss |
3.non-alloc sections(13个) 【运行时不需要,linker、debug等工具使用,可以通过strip移除】 | 【16】meminfo 【17】.debug_info 【18】.debug_abbrev 【19】.debug_aranges 【20】.debug_ranges 【21】.debug_macro 【22】.debug_line 【23】.debug_str 【24】.comment 【25】.debug_frame 【26】.debug_loc 【27】.symtab 【28】.strtab |
readelf -SW hello_word.out查看:
3、Program Header 更新
- 原始结构
readelf -l hello_word.out查看:
- 新的结构:根据新的sections重新计算得出
readelf -l hello_word.out查看:
4、.symtab section的st_shndx字段更新
readelf -sW hello_word.out查看(内容比较多,保存到文件里面),Ndx对应section id,由于sections架构重新调整,这边Ndx的id需要和新的section id一一对应
四、参考资料
1、ELF的介绍
可以参考:
ELF 详解2 – Section Header & Section
ELF 详解3 – Symbol Table & Symbol