7

I am working on embedded Linux environment. I have a set of shared libraries in binary format (I don't have the source code and Makefile) and I want to check whether they have been compiled with -fPIC option or not. Is there any tool or an approach to test if shared library binary was -fPIC compiled?

Regards.

Aymen
  • 261
  • 2
  • 10
  • see http://stackoverflow.com/questions/1340402/how-can-i-tell-with-something-like-objdump-if-an-object-file-has-been-built-wi – Icarus3 Dec 25 '12 at 16:25
  • 1
    Sort of "objdump -d file > file.out" and examining file.out for direct memory references of some sort, don't think such a thing exists. – Mats Petersson Dec 25 '12 at 16:36
  • @Mats: after making the objdump -d, on which parameters or signs in file.out may I be based to determine -fPIC was used or not? – Aymen Dec 25 '12 at 16:54
  • 1
    objdump -d means "disassemble the code". And to check, you basically have to scan the code for relocatable address references in the code. It's not a method I'd like to rely upon for even a single library. In other words, you can't do what you want to do in any simple way. You could POSSIBLY write a piece of code that scans the executable (.so or whatever) for "suspect relacation entries", but it's far from guaranteed to be easy - and certainly, if you write something for x86, it may need to change for x86-64, and almost certainly needs to change for ARM or MIPS architecture. – Mats Petersson Dec 25 '12 at 17:24
  • @Mats Petersson The object is complied with `fPIC` indeed, but `objdump -d datetime.o | grep -i relacation` outputs nothing. – John Aug 24 '21 at 01:36
  • @John because it should be "relocation", not "relacation". – yugr Dec 28 '21 at 10:24

1 Answers1

0

You'll not be able to build shared library without -fPIC. It'll report something like

/usr/bin/ld: /tmp/ccbCwoJo.o: relocation R_X86_64_PC32 against symbol `_Z1ff' can not be used when making a shared object; recompile with -fPIC

But if you really want to check something, grep for PLT calls:

$ objdump -d a.out | g @plt
 628:   e8 23 00 00 00          callq  650 <__gmon_start__@plt>
0000000000000650 <__gmon_start__@plt>:
0000000000000660 <__cxa_finalize@plt>:
0000000000000670 <_Z1ff@plt>:
0000000000000680 <cosf@plt>:
0000000000000690 <sinf@plt>:
 72e:   e8 2d ff ff ff          callq  660 <__cxa_finalize@plt>
 7df:   e8 8c fe ff ff          callq  670 <_Z1ff@plt>
 819:   e8 62 fe ff ff          callq  680 <cosf@plt>
 82e:   e8 5d fe ff ff          callq  690 <sinf@plt>
yugr
  • 19,769
  • 3
  • 51
  • 96