How to know whether a shared library binary is built with -fPIC option
Asked Answered
C

1

8

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.

Cazzie answered 25/12, 2012 at 16:17 Comment(6)
see #1340902Embrocation
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.Gaggle
@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?Cazzie
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.Gaggle
@Mats Petersson The object is complied with fPIC indeed, but objdump -d datetime.o | grep -i relacation outputs nothing.Brie
@Brie because it should be "relocation", not "relacation".Kitchenette
K
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>
Kitchenette answered 28/12, 2021 at 10:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.