Linux, Gnu Gcc, Ld, Version Scripts and the Elf Binary Format -- How Does It Work

Does gcc have any options to add version info in ELF binary file?

You can emit your version info into a text file, then turn that text file into an object file which you then statically link into your executable.

The first step is simple but you have to write some code: a script or something to write your version info in any format you like as a plain text file. Then write a makefile rule to produce say version.o from version.txt, using objcopy. Now you'll have an object file with two useful symbols defined in it: the beginning and end of the textual version info. Add that generated object to your executable, and you'll be able to access the version two ways: by running strings on the binary, or by writing code in the application to print the version string (you'll need to declare the start and end symbols as variables in some header file).

New versions of LD cannot take ELF files as input to link

I have just come up with a similar problem.

It seems ld version GNU ld (GNU Binutils) 2.36.1 links the file into an executable format.

To fix it, add -r in your ld command to output a relocatable format file.

From the ld manual:

-r

--relocateable

Generate relocatable output--i.e., generate an output file that can in turn serve as input to ld. This is often called partial linking. As a side effect, in environments that support standard Unix magic numbers, this option also sets the output file's magic number to OMAGIC. If this option is not specified, an absolute file is produced. When linking C++ programs, this option will not resolve references to constructors; to do that, use -Ur. This option does the same thing as -i.

Resources equvalent in linux. GNU LD

I found a solution based on other threads and some googling.

For people with similar tasks/problems go and check what objcopy does. You can generate .obj from any file and then link it with linker script whenever you want.

can a program read its own elf section?

How can I print the build version of the program (located in the .note.gnu.build-id elf section) from the program itself?

  1. You need to read the ElfW(Ehdr) (at the beginning of the file) to find program headers in your binary (.e_phoff and .e_phnum will tell you where program headers are, and how many of them to read).

  2. You then read program headers, until you find PT_NOTE segment of your program. That segment will tell you offset to the beginning of all the notes in your binary.

  3. You then need to read the ElfW(Nhdr) and skip the rest of the note (total size of the note is sizeof(Nhdr) + .n_namesz + .n_descsz, properly aligned), until you find a note with .n_type == NT_GNU_BUILD_ID.

  4. Once you find NT_GNU_BUILD_ID note, skip past its .n_namesz, and read the .n_descsz bytes to read the actual build-id.

You can verify that you are reading the right data by comparing what you read with the output of readelf -n a.out.

P.S.

If you are going to go through the trouble to decode build-id as above, and if your executable is not stripped, it may be better for you to just decode and print symbol names instead (i.e. to replicate what backtrace_symbols does) -- it's actually easier to do than decoding ELF notes, because the symbol table contains fixed-sized entries.

Can a linked ELF binary contain multiple .text.* sections (automatically)

About avoiding a custom linker script, I don't think it is possible. If you want to fiddle with sections you need a linker script.

In my machine, the command ld --verbose outputs:

  .text           :
{
*(.text.unlikely .text.*_unlikely .text.unlikely.*)
*(.text.exit .text.exit.*)
*(.text.startup .text.startup.*)
*(.text.hot .text.hot.*)
*(.text .stub .text.* .gnu.linkonce.t.*)
/* .gnu.warning sections are handled specially by elf32.em. */
*(.gnu.warning)
}

If you want to change that, you must change the script. There is no workaround!



Related Topics



Leave a reply



Submit