6

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?

user2881914
  • 407
  • 1
  • 4
  • 9
  • What about using 7 unused bytes between [`e_ident[EI_ABIVERSION] and e_type`](http://en.wikipedia.org/wiki/Executable_and_Linkable_Format)? :) – Kamiccolo Oct 15 '13 at 16:40
  • I suppose you are using the GNU linker? If so, it comes with scripts that can be tweaked to your taste. I do not think that you could change the script to force a symbol at a specific location, however, you could create a special .appversion section and ensure that this new section appears first. Now don't ask me how to do all of that... I just know you can do it. – Alexis Wilke Oct 15 '13 at 17:25
  • Linker scripts seem one option although as you say it is not clear about how you would use them to put a symbol at the top of the ELF file. The mystery is why this is occurring at all. Previously the link order decided symbol placement. The inclusion of a C++ library seems to have affected that. I don't know whether C++ object code has any special requirements in terms of ELF ordering – user2881914 Oct 16 '13 at 08:12

2 Answers2

1

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.

voodooattack
  • 1,127
  • 9
  • 16
0

At the top of ELF file it should be magic signature 0x7f, 'E', 'L', 'F' according to the ELF specification. Instead putting your code version at the top of executable, I suppose you can use some unsignificant fields from Elf header, for example 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];       
};
Qwerty
  • 153
  • 1
  • 9