我正在尝试使用 ELFIO 创建一个 ELF 对象。
解决方法:线路section_text->set_flags(SHT_PROGBITS);
应该是section_text->set_type(SHT_PROGBITS);
生成的对象是错误的(如下所述),因此:
如何创建一个包含实际数据的 elf 部分?
该程序如下:
#include <memory>
#include <string>
#include "elfio/elfio.hpp"
int main(){
std::shared_ptr<ELFIO::elfio>elf_object;
elf_object = std::shared_ptr<ELFIO::elfio>(new ELFIO::elfio);
elf_object->create(ELFCLASS32, ELFDATA2LSB);
elf_object->set_os_abi(ELFOSABI_NONE);
elf_object->set_type(ET_REL);
elf_object->set_machine(EM_NONE);
ELFIO::section *section_text;
section_text = elf_object->sections.add(".text"); //<-- Executable code
section_text->set_flags(SHT_PROGBITS);
section_text->set_flags(SHF_ALLOC | SHF_EXECINSTR);
section_text->set_addr_align(0x10);
section_text->set_data("0123456789ABCDEF", 16);
elf_object->save("sample.elf");
return 0;
}
当我使用 .text 部分转储该部分时,该部分readelf
的大小和标志(AX,16 字节)是正确的,但数据全为零
readelf -x .text -aW sample.elf
输出:
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: REL (Relocatable file)
Machine: None
Version: 0x1
Entry point address: 0x0
Start of program headers: 0 (bytes into file)
Start of section headers: 80 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 0
Size of section headers: 40 (bytes)
Number of section headers: 3
Section header string table index: 1
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .shstrtab STRTAB 00000000 000034 000011 00 0 0 1
[ 2] .text NULL 00000000 000050 000010 00 AX 0 0 16
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
L (link order), O (extra OS processing required), G (group), T (TLS),
C (compressed), x (unknown), o (OS specific), E (exclude),
p (processor specific)
There are no section groups in this file.
There are no program headers in this file.
There is no dynamic section in this file.
There are no relocations in this file.
The decoding of unwind sections for machine type None is not currently supported.
No version information found in this file.
Hex dump of section '.text':
0x00000000 00000000 00000000 00000000 00000000 ................
十六进制转储是:
Hex dump of section '.text':
0x00000000 00000000 00000000 00000000 00000000 ................
当预期是:
Hex dump of section '.text':
0x00000000 30313233 34353637 38394142 43444546 0123456789ABCDEF