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?
RIP
as in first case (EAX
in your example is justEIP
at the beginning of call) is popular enough to get a flag - x86-64 code is simpler (no trick) and faster so from clang POV it's clear win (I understand that it's not from yours). Note that disabling address space randomization do disable some "defence in depth" you have. – Maciej Piechotka%rip
-relative addressing? This is the kind of addressing the amd64-architecture was designed for. – fuz