Is there a way to determine that a .a or .so library has been compiled as position indepenent code?

早过忘川 提交于 2019-12-18 12:48:08

问题


I am getting a linking error when compiling the numpy library against lapack indicating I need to compile lapack with -fPIC. I thought I had done just that. Is there a way to determine that the produced lapack library is position independent?


回答1:


In general, you have no way of knowing:

$ cat a.c
int foo(int x) { return x+1; }
$ gcc -fno-pic a.c -c -o nopic.o
$ gcc -fPIC a.c -c -o pic.o   
$ cmp pic.o nopic.o 
$ cmp pic.o nopic.o && echo Identical
Identical



回答2:


You may have some luck with this answer, although it's platform dependent and doesn't work for all object files (but if you code manipulates pointers in any way, it should work).

This is the result of objdump -r on a file compiled with -fPIC:

test.o:     file format elf32-i386

RELOCATION RECORDS FOR [.text]:
OFFSET   TYPE              VALUE 
00000007 R_386_PC32        __i686.get_pc_thunk.cx
0000000d R_386_GOTPC       _GLOBAL_OFFSET_TABLE_

and this is for a file without PIC:

test.o:     file format elf32-i386


来源:https://stackoverflow.com/questions/3507296/is-there-a-way-to-determine-that-a-a-or-so-library-has-been-compiled-as-positi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!