HOW TO generate R_X86_64_64 relocations with yasm/nasm

Programming, for all ages and all languages.
Post Reply
User avatar
daveATseclogs
Posts: 9
Joined: Sat Feb 18, 2012 4:54 am
Location: Italy
Contact:

HOW TO generate R_X86_64_64 relocations with yasm/nasm

Post by daveATseclogs »

Hi there i have a question,
i'm porting my "OS" to x86-64. How i can generate a relocation of type R_X86_64_64 for external symbols?!

for example if i have defined the gdt in another file this instruction:

Code: Select all

extern gdtr
lgdt     [gdtr]
generates a R_X86_64_32 relocation for the gdtr symbol. It's not correct since the pointers must be 64 bits and the address will be truncated.

I assemble in elf64...

Code: Select all

$> yasm -f elf64 test.asm
$> objdump --reloc test.o
000000000000002b R_X86_64_32       gdtd
If i do like this then a 64 bit reloc is generated and the gdtd address is not truncated:

Code: Select all

extern gdtr
mov    rax, gdtr
lgdt     [rax]
There is another way to tell yasm/nasm to generate a 64 bit relocation directly for the gdtr symbol?!

Thanks a lot
Dave
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: HOW TO generate R_X86_64_64 relocations with yasm/nasm

Post by jnc100 »

The problem is not with yasm, it is that the lgdt instruction specifies the location of its operand in a ModR/M byte. If you are simply using a memory address then this is equivalent to a simple displacement (without a base). The ModR/M / SIB combo only allows 32 bit displacements, thus you will only generate 32 bit relocations. The way you are doing it with MOV is the only way to encode 64 bit immediates as far as I know. It works because there is a MOV r64, imm64 instruction.

Regards,
John.
User avatar
daveATseclogs
Posts: 9
Joined: Sat Feb 18, 2012 4:54 am
Location: Italy
Contact:

Re: HOW TO generate R_X86_64_64 relocations with yasm/nasm

Post by daveATseclogs »

Thanks man
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: HOW TO generate R_X86_64_64 relocations with yasm/nasm

Post by Owen »

...Hence why everyone places their kernel binary in the -2GB region of the address space. This region is where said 32-bit 0-offset signed displacements work
User avatar
turdus
Member
Member
Posts: 496
Joined: Tue Feb 08, 2011 1:58 pm

Re: HOW TO generate R_X86_64_64 relocations with yasm/nasm

Post by turdus »

jnc100 wrote: The ModR/M / SIB combo only allows 32 bit displacements, thus you will only generate 32 bit relocations. The way you are doing it with MOV is the only way to encode 64 bit immediates as far as I know. It works because there is a MOV r64, imm64 instruction.

Regards,
John.
Right about modrm, but there’s only MOV rax, imm64. Other registers are sign extended from imm32.
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: HOW TO generate R_X86_64_64 relocations with yasm/nasm

Post by jnc100 »

turdus wrote:Right about modrm, but there’s only MOV rax, imm64. Other registers are sign extended from imm32.
My copy of Intel 2A has MOV r64, imm64 encoded as rex.w + b8 + rd, described as "move imm64 to r64".

Regards,
John.
User avatar
turdus
Member
Member
Posts: 496
Joined: Tue Feb 08, 2011 1:58 pm

Re: HOW TO generate R_X86_64_64 relocations with yasm/nasm

Post by turdus »

jnc100 wrote:My copy of Intel 2A has MOV r64, imm64 encoded as rex.w + b8 + rd, described as "move imm64 to r64".
Yep, you're right, I was misleaded by the example and the first sentence in 2.2.1.5 (same document). Gosh, if I would read more carefully, I would not have been sucking with using only the accumulator for 64 bit immediates in isrs...
Post Reply