ELFIO is creating all-zero ELF sections, regardless of set data. How do I get the data into the .text section?

0

I'm trying to create an ELF object using ELFIO.

Solution: the line section_text->set_flags(SHT_PROGBITS); should be section_text->set_type(SHT_PROGBITS);

The resulting object is wrong (described below) so:
How do I create an elf section with the actual data inside?

The program is the following:

#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;
}

when I dump the section using readelfthe .text section is correct in size and flags (AX, 16byte) but the data is all-zero

readelf -x .text -aW sample.elf output:


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

The hexdump is:

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

When the expected would be:

Hex dump of section '.text':
  0x00000000 30313233 34353637 38394142 43444546 0123456789ABCDEF
linker
elf
readelf
asked on Stack Overflow Dec 5, 2019 by NeonMan • edited Dec 8, 2019 by NeonMan

1 Answer

0

You have a typo. Instead

section_text->set_flags(SHT_PROGBITS);

write

section_text->set_type(SHT_PROGBITS);
answered on Stack Overflow Feb 12, 2020 by Serge C

User contributions licensed under CC BY-SA 3.0