c++clinuxshared-objects

How to test if a shared object is 32-bit or 64-bit?


I'm developing a C++ application where I need to test whether a .SO is 32-bit or 64-bit, before loading it.

I managed to make this assessment on Windows, taking the Headers file.

Now I'm searching for a way to make that assessment in Linux.

Initially I want to do this using functions or methods in C++, ie without calling system() on an external program (file, objdump...).

If our community can help me find a solution to this, I will be very grateful.

Thanks!


Solution

  • .so files use the ELF format. The ELF header comes in two variants for 32bit and 64bit platforms. Which of the two the file contains is determined by byte 0x04 in the file. It is 1 for 32bit format and 2 for the 64bit format.

    You can simply read and test this byte.

    The actual instruction set that the machine code is compiled for can also be determined from bytes 0x12 and 0x13, e.g. 0x03 for x86 and 0x3E for x86_64. Note that the endianess for the two bytes is determined by byte 0x05, which is either 1 for little endian or 2 for big endian.

    See also the wikipedia article on the ELF format.