简体   繁体   中英

llvm: How to generate x64 code avoiding RIP-based addressing mode?

I am using clang assembly language output for a custom assembler and linker. I feel pretty comfortable with x86 mode, but not with x64 mode, where PC-relative addressing. Consider the following toy example.

C code:

void sum()
{
    static int a, b;
    static int c;
    c = a + b;
}

x86 output fragment:

sum:
    call    .L0$pb
.L0$pb:
    pop     EAX
.Ltmp0:
    add     EAX, _GLOBAL_OFFSET_TABLE_+(.Ltmp0-.L0$pb)
    mov     ECX, DWORD PTR [EAX + sum.a@GOTOFF]
    add     ECX, DWORD PTR [EAX + sum.b@GOTOFF]
    mov     DWORD PTR [EAX + sum.c@GOTOFF], ECX
    ret

x64 output fragment:

sum:
    mov     EAX, DWORD PTR [RIP + sum.a]
    add     EAX, DWORD PTR [RIP + sum.b]
    mov     DWORD PTR [RIP + sum.c], EAX
    ret

My question is: Is there any command line switch that would allow me to generate x64 code without using RIP-based addressing mode? If not, is it possible to modify the LLVM code to avoid RIP-based addressing mode generation for x64 mode and how?

You need to learn about x86-64 code model (see. eg X86-64 ABI doc or, eg http://eli.thegreenplace.net/2012/01/03/understanding-the-x64-code-models ). By default the code model is small - everything is expected to be within 2Gb range and therefore rip-rel addressing should be used. If you won't want to use rip-rel addressing, then you need to use eg large code mode. Pass -mcmodel=large to clang and you're done.

PS: Intel asmprinter is of listing quality, the syntax is not expressive enough for the majority of apps, so, always use at&t.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM