Reputation: 412
I would like to be able to check the format of an object file from within my C code so that I can use different functions for reading the file based on whether it is COFF or ELF format.
Is this possible, and if so, how would I do it?
This is in Linux btw if that makes any difference.
Upvotes: 8
Views: 14833
Reputation: 179552
Read the first four bytes. If they are equal to \x7fELF
, it's an ELF file. Otherwise, you should parse it as COFF and see if it makes sense. (Note that COFF magic is a lot more complicated; I get no less than 42 magic entries in /usr/share/file/magic
for it).
Upvotes: 15
Reputation: 25873
Check the magic number. The ELF magic number is 0x7f454C46 (0x7f + "ELF") and COFF's is 0x14c. Take care about this anyway because there are different magic numbers for COFF.
Watch out about endianness when reading these values.
Upvotes: 5