views:

791

answers:

8
+3  Q: 

ELF file headers

Hey!

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?
A: 

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.

Chris
A: 

I haven't done this in awhile, but can't you just append arbitrary data to an executable. If you always append fixed-size data it would be trivial to recover anything you append. Variable size wouldn't be much harder. Probably easier than messing w/ elf headers and potentially ruining you executables.

basszero
good idea, i'll probably do a checksum this way, it should be possible to add the build date etc too. However I still really want to know if you can change the elf file header know i've spent so much time looking at it!
Richard
+1  A: 

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.

Ben Combee
+2  A: 

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.

wnoise
+1  A: 

In Solaris you can use elfedit but I think you are really asking solutions for Linux. Linux Is Not UniX :P

wbkang
A: 

I didn't finish the book but iirc Linkers and Loaders by John Levine had the gory details that you would need to be able to do this.

Toybuilder
+3  A: 

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.

ConcernedOfTunbridgeWells
Great! thanks for that, helps me with another question i have also!
Richard
+1 for a really interesting link.
Chris Huang-Leaver
+1 for the awesome link
Will Bickford
A: 

In Linux Console:

$ man ld

$ ld --verbose

HTH

plan9assembler