ELFIO正在创建全零的ELF节,无论设置数据如何。如何将数据放入.text部分?

问题描述 投票:0回答:1

我正在尝试使用ELFIO创建ELF对象。

Solution: 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;
}

当我使用readelf转储该节时,.text节的大小和标志(AX,16byte)正确,但数据全为零)>

[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 ................

hexdump是:

Hex dump of section '.text':
  0x00000000 00000000 00000000 00000000 00000000 ................

预期的时间是:

Hex dump of section '.text':
  0x00000000 30313233 34353637 38394142 43444546 0123456789ABCDEF

我正在尝试使用ELFIO创建ELF对象。解决方案:该行section_text-> set_flags(SHT_PROGBITS);应该是section_text-> set_type(SHT_PROGBITS);结果对象错误(...

linker elf readelf
1个回答
0
投票

您有错字。改为

© www.soinside.com 2019 - 2024. All rights reserved.