由于PIC,无法从程序集的x86-64对象链接共享库 [英] Can't link a shared library from an x86-64 object from assembly because of PIC

查看:82
本文介绍了由于PIC,无法从程序集的x86-64对象链接共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将共享库从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.

已解决了所有push/pop问题(将寄存器的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.事实是,我的确使用了 NASM手册"9.2节中所述的PIC.但是,在我看来,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调用来解决,例如call 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_RGBpush 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆