3
votes

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
Possible duplicate of NASM issue on OSX 64-bitPeter Cordes

1 Answers

6
votes

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.