1. 程式人生 > >【link】relocation R_X86_64_32 against `text' cannot be used when making a shared object

【link】relocation R_X86_64_32 against `text' cannot be used when making a shared object

DATE: 2018.11.19

1、參考
2、問題描述
relocation R_X86_64_32 against `text' cannot be used when making a shared object; 
recomplied with -fPIC
3、解決方案

在編譯動態庫時,出現重定位的問題,主要是由於在編譯原始檔成為目標檔案時,沒有新增-fPIC選項造成的。

CFLAGS += -fPIC  //c檔案編譯新增編譯flag
ASMFLAGS += -DPIC  //彙編檔案編譯新增編譯flag

Update:
在編譯彙編檔案時,加上-DPIC ,但是在連結成動態庫時,還是會出現上面重定位的問題?
解決方案:在連結成動態庫時,指定-Wl, -Bsymbolic的連結選項。

LDFLAGS += -shared -lm -fPIC -Wl,-Bsymbolic

Bsymbolic表示強制採用本地的全域性變數定義,這樣就不會出現動態連結庫的全域性變數定義被應用程式/動態連結庫中的同名定義給覆蓋了!
-Bsymbolic
When creating a shared library, bind references to global symbols to the
definition within the shared library, if any. Normally, it is possible
for a program linked against a shared library to override the definition
within the shared library.
This option is only meaningful on ELF platforms which support shared libraries.
-Bsymbolic-functions


When creating a shared library, bind references to global function symbols
to the definition within the shared library, if any.
This option is only meaningful on ELF platforms which support shared libraries.
-fPIC
Generate position-independent code (PIC) suitable for use in a
shared library, if supported for the target machine. Such code
accesses all constant addresses through a global offset table
(GOT).

Update: 2018.11.21
IOS編譯彙編檔案時,連結成動態庫又又出現了重定位的問題?
解決方案:

LDFLAGS += -read_only_relocs suppress

In fact, the only thing the -Bsymbolic flag does when building a shared library is add a flag in the dynamic section of the binary called DT_SYMBOLIC.
This element’s presence in a shared object library alters the dynamic linker’s symbol resolution algorithm for references within the library. Instead of starting a symbol search with the executable file, the dynamic linker starts from the shared object itself. If the shared object fails to supply the referenced symbol, the dynamic linker then searches the executable file and other shared objects as usual.

THE END!