Is there a way to check if a library has been built for little endian or big endian architecture without executing it's code?

c, endianness

Solution

if you are running linux/unix, the easiest way is using file command.

$ file /lib64/libc-2.15.so 
/lib64/libc-2.15.so: ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), dynamically linked (uses shared libs), BuildID[sha1]=0x2dc710cc03932ca6fb7f223e2c0f67e21adebb4f, for GNU/Linux 2.6.32, not stripped

So the information is definitely built into the library header. You can check using readelf command.

$ readelf -h /lib64/libc-2.15.so
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 03 00 00 00 00 00 00 00 00 
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - GNU
  ABI Version:                       0
  Type:                              DYN (Shared object file)
  Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1
  Entry point address:               0x3fc4c21840
  Start of program headers:          64 (bytes into file)
  Start of section headers:          2062800 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         10
  Size of section headers:           64 (bytes)
  Number of section headers:         43
  Section header string table index: 42

In windows, I don't know how to check, but should be there in the header of the dll.

Problem

Is there a way to check if a library has been built for little endian or big endian architecture without executing it's code? Let's say, I have a library X, but, I don't know if it has been built for little endian or big endian, is there any command or is there a way to find out through the build output if the library has been built for little endian or big endian? Or is it possible to place this information in the library?

Original source