A quick question about elf file headers, I can't seem to find anything useful on how to add/change fields in the elf header. I'd like to be able to change the magic numbers and to add a build date to the header, and probably a few other things.
As I understand it the linker creates the header information, but I don't see anything in the LD script that refers to it (though i'm new to ld scripts).
I'm using gcc and building for ARM.
thanks!
Updates:
- ok maybe my first question should be: is it possible to create/edit the header file at link time?
This link (teensy elf binary) was someone's answer to another question, but it goes into the intricacies of an ELF header in some detail.
I'm fairly sure that a sufficiently complex ld script can do what you want. However, I have no idea how.
On the other hand, elfsh can easily do all sorts of manipulations to elf objects, so give it a whirl.
You might be able to use libmelf, a dead project on freshmeat, but available from LOPI - http://www.ipd.bth.se/ska/lopi.html
Otherwise, you can get the spec and (over)write the header yourself.
In Linux Console:
$ man ld
$ ld --verbose
HTH
I don't know of linker script commands that can do this, but you can do it post-link using the objcopy command. The --add-section option can be used to add a section containing arbitrary data to the ELF file. If the ELF header doesn't contain the fields you want, just make a new section and add them there.
You can create an object file with informative fields like a version number and link that file such that they are included in the resulting ELF binary.
Ident
For example, as part of you build process, you can generate - say -
info.c
that contains one or more#ident
directives:Compile it:
Check if the information is included:
Alternatively, you can use
objdump -s --section .comment info.o
. Note that GCC also writes its own comment, by default.Check the information after linking an ELF executable:
Comment Section
Using
#ident
in a C translation unit is basically equivalent to creating a.comment
section in an assembler file. Example:Using an uncommon section name works, as well (e.g.
.section .blahblah
). But.comment
is used and understood by other tools. GNU as also understands the.ident
directive, and this is what GCC translates#ident
to.With Symbols
For data that you also want to access from the ELF executable itself you need to create symbols.
Objcopy
Say you want to include some magic bytes stored in a data file:
Convert into a object file with GNU objcopy:
Check for the symbols:
Example usage:
Link everything together:
GNU ld
GNU ld is also able to turn data files into object files using an objcopy compatible naming scheme:
Unlike objcopy, it places the symbols into the
.data
instead of the.rodata
section, though (cf.objdump -h magic.o
).incbin
In case GNU objcopy isn't available, one can use the GNU as
.incbin
directive to create the object file (assemble withgcc -c incbin.s
):xxd
A more portable alternative that doesn't require GNU objcopy nor GNU as is to create an intermediate C file and compile and link that. For example with xxd: