In LINUX determine if a .a library/archive 32-bit or 64-bit?

Linux32bit 64bitArchive

Linux Problem Overview


We distribute in Linux a static lib in both 64-bit and 32-bit versions. When troubleshooting a customer, I would like my diagnostic shell script to quickly eliminate the issue by checking the .a archive file to detetmine whether it is 32 or 64 bit. The methods that occur to me are less than elegant:

  1. extract a .o member and ask the "file" command (e.g., ELF 32-bit etc)

  2. start including a dummy member coded to indicate, e.g. 32bit.o/64bit.o and use "ar -t" to check

I have tried "strings xyz.a | grep 32" but this doesn't work well over versions. Not a heartbreaker problem, but if you know of an elegant solution, I would like to know.

Linux Solutions


Solution 1 - Linux

objdump seems like the best way:

objdump -f libfoo.a | grep ^architecture

Solution 2 - Linux

The simplest way is to use the file command.

$ file <.so file or .a file>

Solution 3 - Linux

Just use the file command; i.e. file library.so

Solution 4 - Linux

> oops, that missing sed means that it was displaying to many items.

Just in an answer:

count=$(nm foo.a | grep '^0' | head -1 | sed 's/ .*//' | wc -c)
((count == 17)) && echo 64bit
((count == 9)) && echo 32bit
((count == 0)) && echo '??bit'

How it's supposed to work:

  • nm - get the symbols from the library
  • grep - get lines starting with a hex string (address of symbol in file)
  • head - get the first line
  • sed - remove everything past the whitespace, including the whitespace
  • wc - count the number of characters.

In a 32 bit environment, you get addresses made up of 8 hex digits, adding the new line gives you 9, In a 64bit environment, you get addresses made up of 16 hex digits, adding the new line gives you 17.

Solution 5 - Linux

If there are functions that are specific to a particular version you could try nm then grep for the function.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncvsdaveView Question on Stackoverflow
Solution 1 - LinuxcafView Answer on Stackoverflow
Solution 2 - Linuxpankaj kapoorView Answer on Stackoverflow
Solution 3 - LinuxErikView Answer on Stackoverflow
Solution 4 - LinuxPeteshView Answer on Stackoverflow
Solution 5 - LinuxColWhiView Answer on Stackoverflow