3
votes

On x86-64, how do I load an address from the .data section in a position independent manner (PIC and PIE compatible) when using the GNU assembler with intel syntax.

For example, using AT&T syntax, you can do this:

leaq mystring(%rip), %rdi

Is there an equivalent for Intel syntax? I can't seem to find the answer using search engines...

I am actually using the noprefix version of intel syntax, in case that makes a difference.

Thanks

1

1 Answers

2
votes

An easy way to answer this is to assemble the instruction into an object file and disassemble it in Intel syntax. You might use a command something like objdump -d -Mintel test.o.

Doing it that way gives us:

Disassembly of section .text:

0000000000000000 <test>:
   0:   48 8d 3d 00 00 00 00    lea    rdi,[rip+0x0]        # 7 <test+0x7>

And that should be pretty clear. Note that mystring has turned into 0x0: those zeros are placeholder bytes which will be adjusted at link time using the relocation of mystring. In your assembly source, you would use an identifier there.

Edit: to make it more clear, here is an example source file:

    .intel_syntax noprefix
    .globl test
test:
    lea rdi, [rip+mystring]

And here is the disassembly (from objdump -rd -Mintel test.o). Note the PC-relative relocation:

0000000000000000 <test>:
   0:   48 8d 3d 00 00 00 00    lea    rdi,[rip+0x0]        # 7 <test+0x7>
                        3: R_X86_64_PC32    mystring+0xfffffffffffffffc