Which parts of an .ELF file go into the .bin file?
(since I am getting "Error while parsing Rich Text Content" in the saved post all the time, even though in the EDITOR everything renders just fine, I dumped the text in there without nice formatting)
-------------------------------------------------------------
I have a bootloader and app project in one CubeIDE workspace, for a STM32F0 target.
I added a linker script section (see below, ".buildinfo") to put some info into, also including the CRC and size of the firmware app image, so that the bootloader can check it when it's in flash.
I am also trying to get this to flash all in one go (loader, app with info section) for debugging from CubeIDE, which is why I am now trying to go the .elf route and ditch my previous "tack header onto .bin file" approach.
So I would manipulate the ELF file as post build step, and re-generate the .bin, too, with a call to objcopy on the modified elf.
I am using a library (ELFIO) to parse the .elf file, and I see the sections further below.
While I looked for information about what parts of the .elf go into the .bin, I alas found no single document that describes exactly that. I found some scattered info, like that linked below, and drew some, possibly erroneous, conclusions.
Now, to determine the size of the wouldbe .bin file from only info out of the .elf file, I am summing this:
- sizes of all PROGBITS sections where address != 0x0
- sizes of PREINIT_ARRAY, INIT_ARRAY, FINI_ARRAY
- size of the ***_ARM_EXIDX=0x70000001 (below ".ARM") section
This results in the same size as the currently generated .bin has.
But: Is this correct?.
This was all a bit of guesswork, and I especially don't know why e.g. the .data section's address is given as out of the flash range (that's why I check for addr!=0 instead of "outside flash range"). Oh, and I also don't know why there are even sections with address=0.
References:
ELF sections
https://www.tortall.net/projects/yasm/manual/html/objfmt-elf-section.html
ARM specific sectrions, p. 22, s. 5.3.2
https://static.docs.arm.com/ihi0044/g/aaelf32.pdf
Section Headers:
[ Nr ] Type Addr Size ES Flg Lk Inf Al Name
[ 0] NULL 00000000 00000000 00 00 000 00
[ 1] PROGBITS 08008080 000000bc 00 A 00 000 01 .isr_vector
[ 2] PROGBITS 0800813c 00000040 00 A 00 000 04 .buildinfo
[ 3] PROGBITS 0800817c 00013268 00 AX 00 000 04 .text
[ 4] PROGBITS 0801b3e4 00004c44 00 A 00 000 04 .rodata
[ 5] PROGBITS 08020028 00000000 00 W 00 000 01 .ARM.extab
[ 6] ? (0x70000001) 08020028 00000008 00 A 03 000 04 .ARM
[ 7] PREINIT_ARRAY 08020030 00000000 04 WA 00 000 01 .preinit_array
[ 8] INIT_ARRAY 08020030 00000020 04 WA 00 000 04 .init_array
[ 9] FINI_ARRAY 08020050 0000000c 04 WA 00 000 04 .fini_array
[ 10] PROGBITS 200000c0 00000134 00 WA 00 000 04 .data
[ 11] NOBITS 200001f8 000018e0 00 WA 00 000 08 .bss
[ 12] NOBITS 20001ad8 00000600 00 WA 00 000 01 ._user_heap_stack
[ 13] ? (0x70000003) 00000000 00000028 00 00 000 01 .ARM.attributes
[ 14] PROGBITS 00000000 0006e259 00 00 000 01 .debug_info
[ 15] PROGBITS 00000000 00011f5e 00 00 000 01 .debug_abbrev
[ 16] PROGBITS 00000000 00002ab8 00 00 000 08 .debug_aranges
[ 17] PROGBITS 00000000 000027d0 00 00 000 08 .debug_ranges
[ 18] PROGBITS 00000000 00023d0c 00 00 000 01 .debug_macro
[ 19] PROGBITS 00000000 00030c90 00 00 000 01 .debug_line
[ 20] PROGBITS 00000000 00094f6e 01 00 000 01 .debug_str
[ 21] PROGBITS 00000000 00000053 01 00 000 01 .comment
[ 22] PROGBITS 00000000 0000a680 00 00 000 04 .debug_frame
[ 23] SYMTAB 00000000 0000c5d0 10 18 970 04 .symtab
[ 24] STRTAB 00000000 0000bdca 00 00 000 01 .strtab
[ 25] STRTAB 00000000 00000115 00 00 000 01 .shstrtab