我正在尝试在ASM中创建一个共享库(*.so),我不确定我是否正确...
我的代码是:
.section .data
.globl var1
var1:
.quad 0x012345
.section .text
.globl func1
func1:
xor %rax, %rax
# mov var1, %rcx # this is commented
ret
Run Code Online (Sandbox Code Playgroud)
要编译它我运行
gcc ker.s -g -fPIC -m64 -o ker.o
gcc ker.o -shared -fPIC -m64 -o libker.so
Run Code Online (Sandbox Code Playgroud)
我可以访问变量var1并使用dlopen()和dlsym()从C中的程序调用func1.
问题在于变量var1.当我尝试从func1访问它时,即取消注释该行,编译器会生成错误:
/usr/bin/ld: ker.o: relocation R_X86_64_32S against `var1' can not be used when making a shared object; recompile with -fPIC
ker.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
我不明白.我已经用-fPIC编译了,那有什么不对?