serge1 / ELFIO

ELFIO - ELF (Executable and Linkable Format) reader and producer implemented as a header only C++ library
http://serge1.github.io/ELFIO
MIT License
720 stars 155 forks source link

Saved .elf differs from read .elf by a lot more than the changed section #62

Closed sktpin closed 3 years ago

sktpin commented 3 years ago

Hello, I'm don't know a lot of details about the .elf format, so maybe there can be a good reason for it.

I have a bare metal project for ARM Cortex M, using the ARM GCC compiler toolchain for that. So I use ELFIO load a produced .elf file, search for one section by name, call set_data() on that section to change the content (size should remain the same), and call .save() on that elfio object. The changed section is only a few uint32's big.

Looking at the resulting file in the hex editor, I find things like big blocks (kilobytes maybe) of zeroes where the original file had data. Also, the saved file is now 6KB bigger than the original one.

Can there be legitimate reasons for these observations, or is something wrong? (I've not tried using it yet)

serge1 commented 3 years ago

Hi,

Generally speaking, it is not valid to compare files in ELF format by binary comparison. The more correct way is comparison of headers, sections and segments' contents.

Without knowing more details, the file size may increase due to increasing of section size - ELF writer recalculates layouout and moves location of the next section to a place corresponding to its align requriement.

Best regards, Serge

sktpin commented 3 years ago

Ok, thanks, good to know. I meanwhile also tested the resulting .elf with the MCU vendor-provided IDE to debug, then read out the flash with their utility & compaired against the toolchain-generated raw binary file. The flash content derived from the saved .elf matches with that IDE-generated bin file, so it looks like it works. Then there is no problem. Thanks for your very useful library!

serge1 commented 3 years ago

Per the previous comment - the issue is closed