Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing a symbol to the top of a ELF file

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

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

However for one executable this has failed to work and the only difference we can find is that the executable contains a mixture of C and C++ code, and the version symbol is being relocated somewhere else.

The question is therefore is there a way of guaranteeing the absolute location of a symbol in a ELF file such that a symbol is always located at the top of the file either through linker commands or code attribute directives?

like image 935
user2881914 Avatar asked Oct 15 '13 09:10

user2881914


1 Answers

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

For example:

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

This doesn't control where the sections will exactly appear in the linked executable, but it might influence it (it certainly does when dealing with ROM images), you'll have to experiment yourself.

like image 50
voodooattack Avatar answered Sep 30 '22 18:09

voodooattack