Position independent addressing in GNU assembler with Intel syntax

若如初见. 提交于 2019-12-04 04:18:58

问题


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:


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


来源:https://stackoverflow.com/questions/19984584/position-independent-addressing-in-gnu-assembler-with-intel-syntax

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