简体   繁体   English

用气体(-fPIC)生成与位置无关的代码

[英]Generating position independent code with gas (-fPIC)

I try to create a shared library on x86-64 but fail. 我尝试在x86-64上创建共享库,但失败。 The problem boils down to the following code (please don't mind, that it does not make a lot of sense): 问题归结为以下代码(请不要介意,这没有任何意义):

.section .data
newline:
    .ascii "\n"

.section .text
.globl write_newline
    .type write_newline, @function
write_newline: 
    mov  $newline, %rax    
    ret

building as follows: 建设如下:

as minimal.s -o minimal.o
ld -shared minimal.o -o libmin.so

leads to the following error: 导致以下错误:

ld: minimal.o: relocation R_X86_64_32 against `.data' can not be used when making a shared object; recompile with -fPIC
minimal.o: error adding symbols: Bad value

But gas doesn't know option -fPIC, so I cannot recompile with it: 但是gas不知道选项-fPIC,因此我无法对其进行重新编译:

as -fPIC minimal.s -o minimal.o
as: unrecognized option '-PIC'

What can be done instead? 可以做什么呢?

As Marc B said in the comments, the error message assumes you were using GCC or so some other compiler. 正如Marc B在评论中所说,错误消息假定您使用的是GCC或其他某种编译器。 Since you've written the code in assembly its your responsibility to ensure that your code is position independent. 由于您已经用汇编语言编写了代码,因此您有责任确保代码与位置无关。

On x86-64 targets you can resolve this error using RIP relative addressing: 在x86-64目标上,可以使用RIP相对寻址解决此错误:

        .section .data
newline:
    .ascii "\n"

.section .text
.globl write_newline
    .type write_newline, @function
write_newline:
    lea  newline(%rip), %rax
    ret

Note that you don't need to use the GOT here because newline is a local symbol. 请注意,您不需要在这里使用GOT,因为newline是本地符号。 If it were global, and you wanted the reference to newline in your code to able to refer to some other definition of newline in some other runtime component, then you would need to use an @GOTPCREL relocation. 如果是全球性的,你想要的参考newline在代码能够引用的其他一些定义newline在其他一些运行时组件,那么你就需要使用@GOTPCREL搬迁。 That would allow the dynamic linker to change the entry in the GOT to point to a different definition of newline . 这将允许动态链接程序更改GOT中的条目,以指向newline的不同定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编译GAS代码未检测到-fPIC选项 - Compiling GAS code doesn't detect -fPIC option 访问位置无关代码中的 .data 部分 - Access .data section in Position Independent Code 宏汇编中与位置无关的代码 - Position independent code in macro-assembly 如何从 ARM 汇编中的位置无关代码 (PIC) 访问数据? - How to access data from Position Independent Code (PIC) in ARM Assembly? 在linux可加载内核模块中重定位与位置无关的代码 - Relocating position independent code in linux loadable kernel module 在位置无关代码中引用链接器定义的符号 - Referencing linker-defined symbols in position independent code PowerPC Assembly 中的位置相关、独立代码和全局变量 - Position Dependent, Independent Code, and Global Variables in PowerPC Assembly 16 位实模式下的位置无关代码,引导加载/软盘读取 - Position independent code in 16-bit real mode, bootloading/floppy read 如何在 Microsoft Visual Studio C/C++ 编译器中关闭与位置无关的代码编译? - How to turn off position-independent code compilation in Microsoft Visual Studio C/C++ compiler? 在Linux上创建.SO文件而不使用PIC(与位置无关的代码)(x86 32位) - Create .SO files on Linux without using PIC (position independent code) (x86 32bit)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM