2

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!

jww
  • 97,681
  • 90
  • 411
  • 885
Dan Sigolo
  • 55
  • 1
  • 6

2 Answers2

6

.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.

walnut
  • 21,629
  • 4
  • 23
  • 59
0

The ELF File Format holds that information. All that's needed is to read the file into a buffer and follow the ELF format layout (in the linked wiki), reading the e_machine field.

Disclaimer: I'm a Windows guy, if there's something Linux specific that makes this not apply then please let me know with a comment.

Pickle Rick
  • 808
  • 3
  • 6
  • Ah, had he separated the word ``system`` from ``call`` I would have caught that. I was so confused as to why he would want to avoid a syscall, which is of course impossible when accessing the file system. lol – Pickle Rick Nov 14 '19 at 15:14
  • Much appreciated. :) Thankfully the confusion didn't lead to an incorrect answer this time. – Pickle Rick Nov 14 '19 at 15:16