4

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
ecm
  • 2,583
  • 4
  • 21
  • 29
Matt Fichman
  • 5,458
  • 4
  • 39
  • 59
  • Possible duplicate of [NASM issue on OSX 64-bit](https://stackoverflow.com/questions/30814930/nasm-issue-on-osx-64-bit) – Peter Cordes Apr 20 '18 at 08:28

1 Answers1

7

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.

  • 1
    I have tried both of these options, and they sometimes result in an incorrect address being computed. This _should_ be the correct solution, but I think there is a bug in the OS X version of NASM for x86-64. – Matt Fichman Mar 19 '12 at 01:35
  • 2
    @MattFichman: Indeed, there was a NASM 2.11.08 bug on OS X that affected RIP-relative addressing: [NASM issue on OSX 64-bit](//stackoverflow.com/q/30814930) – Peter Cordes Apr 20 '18 at 08:22