165042b18Sopenharmony_ci# ELFIO 265042b18Sopenharmony_ci 365042b18Sopenharmony_ci[](https://travis-ci.com/serge1/ELFIO) 465042b18Sopenharmony_ci 565042b18Sopenharmony_ci 665042b18Sopenharmony_ci[](http://elfio.sourceforge.net/elfio.pdf) 765042b18Sopenharmony_ci[](https://github.com/serge1/ELFIO/blob/master/COPYING) 865042b18Sopenharmony_ci 965042b18Sopenharmony_ciELFIO是一个只有头文件的C++库,用于读取和生成ELF二进制格式文件。它被用作一个独立的库,它不依赖于任何其他产品或项目。 1065042b18Sopenharmony_ci它遵循ISO C++,可以在各种架构和编译器上编译。 1165042b18Sopenharmony_ci 1265042b18Sopenharmony_ci虽然是用C++编写的,但是该库也提供了一个C语言接口封装器。 1365042b18Sopenharmony_ci 1465042b18Sopenharmony_ci完整的库文档可以在【ELFIO-教程和用户手册】(http://elfio.sourceforge.net/elfio.pdf)中找到。 1565042b18Sopenharmony_ci 1665042b18Sopenharmony_ci 1765042b18Sopenharmony_ci# 目录结构 1865042b18Sopenharmony_ci. 1965042b18Sopenharmony_ci├── elfio_array.hpp # array section访问类,提供对array section的处理 2065042b18Sopenharmony_ci├── elfio_demo.cpp # 空源文件,目的是为了编译出动态库 2165042b18Sopenharmony_ci├── elfio_dump.hpp # elf dump类,dump出elf文件的各类信息,例如:elf header、section headers、segment headers等 2265042b18Sopenharmony_ci├── elfio_dynamic.hpp # dynamic section访问类,提供对dynamic section的处理 2365042b18Sopenharmony_ci├── elfio_header.hpp # elf header类,解析elf header的section 2465042b18Sopenharmony_ci├── elfio.hpp # elfio库的主要类,可以创建、加载、保存elf文件 2565042b18Sopenharmony_ci├── elfio_modinfo.hpp # modinfo section访问类,提供对modinfo section的处理 2665042b18Sopenharmony_ci├── elfio_note.hpp # note section访问类,提供对note section的处理 2765042b18Sopenharmony_ci├── elfio_relocation.hpp # relocation section访问类,提供对relocation section的处理 2865042b18Sopenharmony_ci├── elfio_section.hpp # section类 2965042b18Sopenharmony_ci├── elfio_segment.hpp # segment类 3065042b18Sopenharmony_ci├── elfio_strings.hpp # string section访问类,提供对string section的处理 3165042b18Sopenharmony_ci├── elfio_symbols.hpp # symbol section访问类,提供对symbol section的处理 3265042b18Sopenharmony_ci├── elfio_utils.hpp # elf工具类 3365042b18Sopenharmony_ci├── elfio_version.hpp # 定义elfio库版本号 3465042b18Sopenharmony_ci└── elf_types.hpp # elf类型类,定义了elf文件类型、文件版本等 3565042b18Sopenharmony_ci 3665042b18Sopenharmony_ci 3765042b18Sopenharmony_ci# 使用说明 3865042b18Sopenharmony_ci 3965042b18Sopenharmony_ci(一)加载elf文件 4065042b18Sopenharmony_ci 4165042b18Sopenharmony_ci1. 调用elfio类的load\(\)接口加载elf文件。 4265042b18Sopenharmony_ci 4365042b18Sopenharmony_ci ELFIO::elfio elfIo; 4465042b18Sopenharmony_ci elfIo.load(fileName); 4565042b18Sopenharmony_ci 4665042b18Sopenharmony_ci 4765042b18Sopenharmony_ci(二)获取section名称 4865042b18Sopenharmony_ci 4965042b18Sopenharmony_ci1. 遍历elf文件的sections。 5065042b18Sopenharmony_ci2. 调用section_impl的get_name\(\)接口获取section名称。 5165042b18Sopenharmony_ci 5265042b18Sopenharmony_ci ELFIO::elfio elfIo; 5365042b18Sopenharmony_ci for (const auto §ion : elfIo.sections) { 5465042b18Sopenharmony_ci if (section->get_name() == "xxx") 5565042b18Sopenharmony_ci } 5665042b18Sopenharmony_ci 5765042b18Sopenharmony_ci 5865042b18Sopenharmony_ci(三)获取section数据 5965042b18Sopenharmony_ci 6065042b18Sopenharmony_ci1. 遍历elf文件的sections。 6165042b18Sopenharmony_ci2. 调用section_impl的get_data\(\)接口获取section数据。 6265042b18Sopenharmony_ci 6365042b18Sopenharmony_ci ELFIO::elfio elfIo; 6465042b18Sopenharmony_ci for (const auto §ion : elfIo_.sections) { 6565042b18Sopenharmony_ci if (section->get_name() == "xxx") { 6665042b18Sopenharmony_ci section->get_data(); 6765042b18Sopenharmony_ci } 6865042b18Sopenharmony_ci } 6965042b18Sopenharmony_ci 7065042b18Sopenharmony_ci 7165042b18Sopenharmony_ci(四)获取symbol section 7265042b18Sopenharmony_ci 7365042b18Sopenharmony_ci1. 遍历elf文件的symbol sections。 7465042b18Sopenharmony_ci2. 调用symbol_section_accessor的get_symbol\(\)接口获取symbol section。 7565042b18Sopenharmony_ci 7665042b18Sopenharmony_ci const symbol_section_accessor symbols(reader, psec); 7765042b18Sopenharmony_ci for (unsigned int i = 0; i < symbols.get_symbols_num(); ++i) { 7865042b18Sopenharmony_ci std::string name; 7965042b18Sopenharmony_ci Elf64_Addr value; 8065042b18Sopenharmony_ci Elf_Xword size; 8165042b18Sopenharmony_ci unsigned char bind; 8265042b18Sopenharmony_ci unsigned char type; 8365042b18Sopenharmony_ci Elf_Half section_index; 8465042b18Sopenharmony_ci unsigned char other; 8565042b18Sopenharmony_ci symbols.get_symbol(i, name, value, size, bind, type, section_index, other); 8665042b18Sopenharmony_ci } 8765042b18Sopenharmony_ci 8865042b18Sopenharmony_ci 8965042b18Sopenharmony_ci(五)获取relocation section 9065042b18Sopenharmony_ci 9165042b18Sopenharmony_ci1. 调用relocation_section_accessor的get_entry\(\)接口获取relocation section。 9265042b18Sopenharmony_ci 9365042b18Sopenharmony_ci Elf64_Addr offset; 9465042b18Sopenharmony_ci Elf64_Addr symbolValue; 9565042b18Sopenharmony_ci string symbolName; 9665042b18Sopenharmony_ci Elf_Word type; 9765042b18Sopenharmony_ci Elf_Sxword addend; 9865042b18Sopenharmony_ci Elf_Sxword calcValue; 9965042b18Sopenharmony_ci relocation_section_accessor reloc(elfIo_, sec); 10065042b18Sopenharmony_ci for (uint32_t i = 0; i < sec->get_size() / sec->get_entry_size(); i++) { 10165042b18Sopenharmony_ci reloc.get_entry(i, offset, symbolValue, symbolName, type, addend, calcValue); 10265042b18Sopenharmony_ci } 103