使用NASM加载符号的地址吗? [英] Load the address of a symbol using NASM?
问题描述
我有一些需要在OS X(x86-64)中加载C符号的程序集.使用x86,您可以这样做:
I have some assembly that needs to load a C symbol in OS X (x86-64). With x86, the way you do this is:
mov rax, some_symbol_name
但是,对于x86-64,这会导致链接警告:
However, with x86-64, this causes a link warning:
ld:警告:禁用PIE.在代码签名的PIE中不允许绝对寻址(也许是-mdynamic-no-pic),但是在Test2.o的_main中使用.
要解决此警告,请不要使用-mdynamic-no-pic进行编译,也不要使用-Wl,-no_pie
ld: warning: PIE disabled. Absolute addressing (perhaps -mdynamic-no-pic) not allowed in code signed PIE, but used in _main from Test2.o.
To fix this warning, don't compile with -mdynamic-no-pic or link with -Wl,-no_pie
注意:我知道PIE是什么,并且我不想禁用它.这是我将符号地址加载到寄存器中的其他一些尝试:
Note: I know what PIE is, and I don't want to disable it. Here are some of my other attempts to load the symbol address into a register:
movq rax, some_symbol_name ; Link warning
lea rax, [rel some_symbol_name] ; No link warning, but doesn't always get correct address
我真的很困惑这个(貌似)简单的问题.我已经看过GAS的反汇编,它似乎正在按照上述lea
的方式进行操作,但是我无法让NASM生成正确的代码.
I'm really stumped on this (seemingly) simple problem. I've looked at the GAS disassembly, and it seems to be doing something along the lines of the lea
above, but I can't get NASM to generate the right code.
供参考,这是GAS生成的汇编代码:
For reference, this is the assembly code generated by GAS:
leaq some_symbol_name(%rip), %rax
推荐答案
您要强制NASM使用RIP相对寻址.请执行以下一项操作:
You want to force NASM to use RIP relative addressing. Do one of:
lea rax, [rel some_symbol_name]
或:
default rel
lea rax, [some_symbol_name]
如果这不起作用,请张贴由NASM和GAS生成的机器代码.
If this doesn't work, post the machine code generated by both NASM and GAS.
这篇关于使用NASM加载符号的地址吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!