Forcing a character to the top of an ELF file

In our ELF files generated using the GCC linker, the top of the ELF file is always the version identifier of the executable code.

This is achieved by creating the version.c file and creating the resulting object file of the 1st linked object in the link command.

However, for one executable, this did not work, and the only difference we can find is that the executable contains a mixture of C and C ++ code, and the version symbol moves somewhere else.

Thus, the question arises: is there a way to guarantee the absolute position of the character in the ELF file so that the character is always at the top of the file either with the linker commands or with the code attribute directives?

+6
source share
2 answers

You can control the output of the linker through scripts. In your case, you can check: https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS for a possible solution.

For instance:

SECTIONS { .version 0x2020 : { version.o } .text : { *(.text) } .data : { *(.data) } .bss : { *(.bss) *(COMMON) } } 

This does not control where the partitions will be displayed exactly in the linked executable, but it can affect it (this, of course, deals with ROM images), you have to experiment yourself.

+1
source

At the top of the ELF file there should be a magic signature 0x7f, 'E', 'L', 'F' according to the ELF specification . Instead of putting your version of code at the top of the executable, I assume you can use some fuzzy fields from the Elf header, like ei_pad

 struct E_Ident { unsigned long ei_magic; unsigned char ei_class; unsigned char ei_data; unsigned char ei_version; unsigned char ei_pad[9]; }; 
0
source

Source: https://habr.com/ru/post/955969/


All Articles