This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

writing a program to extract binary data from elf

Hi,

I build elf file using TI compiler for Tiva C target.

I have to write a program to manage elf file, one of the operations is to extract binary data, as the command "tiobj2bin file.elf file.bin armofd armhex mkhex4bin" do.

I use libelf to access the elf file.

Elf file I have to handle are such that the output of the command readelf is:

There are 28 section headers, starting at offset 0x225e24:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .bootVecs         NOBITS          00000000 00f400 000008 00      0   0  4
  [ 2] .vecs             NOBITS          20000000 00f0c0 000360 00  WA  0   0  4
  [ 3] .resetVecs        PROGBITS        00002000 000038 00003c 00   A  0   0  4
  [ 4] xdc.meta          PROGBITS        00000000 00f400 000119 00      0   0  1
  [ 5] .intvecs          NOBITS          00000000 000000 000000 00      0   0  1
  [ 6] .text             PROGBITS        0000203c 000074 00c6f4 00  AX  0   0  4
  [ 7] .const            PROGBITS        0000e730 00c768 002582 00   A  0   0  4
  [ 8] .cinit            PROGBITS        00010cb8 00ecf0 0003d0 00   A  0   0  8
  [ 9] .pinit            NOBITS          00000000 000000 000000 00      0   0  1
  [10] .init_array       NOBITS          00000000 000000 000000 00   W  0   0  1
  [11] .vtable           NOBITS          00000000 000000 000000 00      0   0  1
  [12] .data             NOBITS          2000694c 01594c 0004ec 00  WA  0   0  4
  [13] .bss              NOBITS          20000400 00f400 00554b 00  WA  0   0 1024
  [14] .sysmem           NOBITS          00000000 000000 000000 00      0   0  1
  [15] .stack            NOBITS          2000594c 01494c 001000 00  WA  0   0  4
  [16] .debug_info       PROGBITS        00000000 00f519 0b3d72 00      0   0  1
  [17] .debug_line       PROGBITS        00000000 0c328b 01ced6 00      0   0  1
  [18] .debug_aranges    PROGBITS        00000000 0e0161 002828 00      0   0  1
  [19] .debug_frame      PROGBITS        00000000 0e2989 009b8d 00      0   0  1
  [20] .debug_abbrev     PROGBITS        00000000 0ec516 016b59 00      0   0  1
  [21] .debug_str        PROGBITS        00000000 10306f 07fc55 00      0   0  1
  [22] .debug_pubtypes   PROGBITS        00000000 182cc4 06d45b 00      0   0  1
  [23] .ARM.attributes   ARM_ATTRIBUTES  00000000 1f011f 00005b 00      0   0  0
  [24] .symtab           SYMTAB          00000000 1f017c 0253a0 10     26 8687  0
  [25] .TI.section.flags PROGBITS        00000000 21551c 00001a 00      0   0  0
  [26] .strtab           STRTAB          00000000 215536 01077b 01   S  0   0  0
  [27] .shstrtab         STRTAB          00000000 225cb1 000110 01   S  0   0  0
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)

Elf file type is EXEC (Executable file)
Entry point 0xc695
There are 3 program headers, starting at offset 2252228

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  LOAD           0x000038 0x00002000 0x00002000 0x0f088 0x0f088 R E 0x8
  LOAD           0x00f0c0 0x20000000 0x20000000 0x00000 0x00360 RW  0x4
  LOAD           0x00f400 0x20000400 0x20000400 0x00000 0x06a38 RW  0x400

 Section to Segment mapping:
  Segment Sections...
   00     .bootVecs .resetVecs .intvecs .text .const .cinit .pinit .init_array .vtable .sysmem 
   01     .bootVecs .vecs .intvecs .pinit .init_array .vtable .sysmem 
   02     .bootVecs .intvecs .pinit .init_array .vtable .data .bss .sysmem .stack 

How do I choose section to exctract? Do I need to take into accouny section type and flags? And what about segments?

best regards

 

  • mastupristi said:
    I have to write a program to manage elf file, one of the operations is to extract binary data, as the command "tiobj2bin file.elf file.bin armofd armhex mkhex4bin" do.

    What problem do you need solved that tiobj2bin does not handle?  Until I understand that, I don't know how to help you.

    It might help to know that tiobj2bin comes from the cg_xml package.  It includes the source code for tiobj2bin.bat (a Windows batch file) and mkhex4bin.pl (a Perl script).  Note armofd and armhex are executables that are part of the ARM compiler tools; source code is not included for those.

    Some background information about ELF can be found in this wiki article.

    Thanks and regards,

    -George

  • I try to explain:

    • I have 3 elf file
      • one resides in the first slice of flash (0x0000~0x2000)
      • the second resides in 0x2000~0x40000 in the flash
      • the third run in sram.
    • I must extract informations (data) from some custom sections of all elf file.
      • These informations are collected in structure, one structure per file.
    • I must extract binary data from all elf file
    • I must combine binary data and information structure from all elf file in a single file

    I hope I have clarified

     

    best regards

  • Please see if this wiki article on combining executables is useful.  If it isn't, I have some thoughts on how you can adapt the bootimage utility from the cg_xml package.

    Thanks and regards,

    -George