Load the address of a symbol (nasm, 64-bit OS X)

寵の児 提交于 2019-12-12 17:25:21

问题


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

However, with x86-64, this causes a link warning:

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

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

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.

EDIT: For reference, this is the assembly code generated by GAS:

leaq    some_symbol_name(%rip), %rax

回答1:


You want to force NASM to use RIP relative addressing. Do one of:

lea rax, [rel some_symbol_name]

or:

default rel
lea rax, [some_symbol_name]

If this doesn't work, post the machine code generated by both NASM and GAS.



来源:https://stackoverflow.com/questions/8722159/load-the-address-of-a-symbol-nasm-64-bit-os-x

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!