由于 PIC,无法从汇编中的 x86-64 对象链接共享库 [英] Can't link a shared library from an x86-64 object from assembly because of PIC
问题描述
我正在将共享库从 32 位移植到 64 位.它由一些程序集(为 NASM 编写)组成,该程序集导出多个程序和一些更高级别的 C 粘合代码.我正在使用 NASM 2.10.01 和 GNU ld 2.22 的 64 位 Debian 机器上构建.
I'm porting a shared library from 32-bit to 64-bit. It's composed of some assembly (written for NASM) that exports several procedures and a little bit of higher-level C glue code. I'm building on a 64-bit Debian machine with NASM 2.10.01 and GNU ld 2.22.
解决了所有推送/弹出问题(推送寄存器的 32 位部分显然在 64 位模式下不起作用)后,我有要组装的对象,但现在我被链接阶段暂停了.这是我的命令行 - 程序集:
Having fixed all the push/pop issues (pushing 32-bit parts of registers obviously won't work in 64-bit mode), I've got the object to assemble, but now I'm halted by the linking stage. Here are my command lines - assembly:
nasm -Ox -dPTC_ARCH=X64 -f elf64 particl.asm -o particlasm.o
链接:
ld -shared -lc -S -melf_x86_64 particlasm.o ptc_highlevel.o -o libparticlasm.so
(-lc 开关强制链接标准 C 库 - 我需要在汇编代码中使用它的一些函数)
ld -shared -lc -S -melf_x86_64 particlasm.o ptc_highlevel.o -o libparticlasm.so
(the -lc switch enforces linking the standard C library in - I need some of its functions in the assembly code)
但是,链接器失败并显示以下消息:
However, the linker fails with the following message:
ld: particlasm.o: relocation R_X86_64_32 against `.text' can not be used when making a shared object; recompile with -fPIC
particlasm.o: could not read symbols: Bad value`
我知道为什么 64 位系统需要 PIC;事情是,我确实在使用 PIC,如 NASM 手册的第 9.2 节所述.但是,在我看来,NASM 以某种方式没有在 ELF 符号表中将我的代码标记为 PIC,这导致链接器提出抗议,并且我在手动修复此问题.
I'm aware of why PIC is required on 64-bit systems; thing is, I am indeed using PIC as described in section 9.2 of the NASM manual. However, it appears to me that NASM is somehow not marking my code as PIC in the ELF symbol table, which causes the linker to protest, and I cannot find any related command-line switches or directives in the manual to fix this.
有什么想法吗?
推荐答案
我已经解决了!就我而言,搬迁的原因是:
I've solved it! In my case the relocations resulted from:
1) libc
调用,我通过将 wrt ..plt
附加到 libc
调用来解决,例如调用rand wrt ..plt
,
1) libc
calls, which I solved by appending wrt ..plt
to libc
calls, e.g. call rand wrt ..plt
,
2) 对代码中常量的引用,需要从例如push MASK_RGB
到 push ptr [rel MASK_RGB]
(其中 ptr
是我自己的类型,它是给定平台的本地字类型的别名).
2) references to constants placed within the code, which needed to change from e.g. push MASK_RGB
to push ptr [rel MASK_RGB]
(where ptr
is my own type that aliases to the native word type for the given platform).
这篇关于由于 PIC,无法从汇编中的 x86-64 对象链接共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!