Relocation when binary get loaded.

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
HyperAssembler
Member
Member
Posts: 36
Joined: Thu Sep 04, 2014 5:24 pm
Location: !SIGSEGV

Relocation when binary get loaded.

Post by HyperAssembler »

Hi guys,
So I've been using grub to load my kernel which is compiled and linked as binary.

It suddenly came to me today that binary does not have symbol table, nor can you specify where to load the kernel in grub, how can a arbitrarily-loaded kernel still address itself correctly?

For instance,

Code: Select all

msg:
db  'abcde';
mov eax,msg ; This should behave differently depending on where the binary kernel is loaded, since the address of msg is determined after being linked to binary.
push eax
call print_str
add esp,4
My first thought was PC-relative(RIP-relative) addressing.
So basically everything is the offset relative to its current address.
Then I looked up intel manual and found out that MOV is actually absolute-addressing.

I also tried to specify different base addresses in linker script (one 0x0, the other one 0x10000)
and they exhibited the exact same behavior in bochs.

And now it's confusing the hell out of me. Could someone please clear this up for me?

Thank you for your time!!
HyperAssembler
Member
Member
Posts: 36
Joined: Thu Sep 04, 2014 5:24 pm
Location: !SIGSEGV

Re: Relocation when binary get loaded.

Post by HyperAssembler »

And I did compare the binary output for different org address.
They are DIFFERENT.
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

Re: Relocation when binary get loaded.

Post by alexfru »

In 16-bit and 32-bit modes only memory locations in direct near jumps and direct near calls are relative to (E)IP.
Everything else requires proper relocation or must be rewritten to figure out the distance between "org" and the load location and use said distance in memory accesses.
HyperAssembler
Member
Member
Posts: 36
Joined: Thu Sep 04, 2014 5:24 pm
Location: !SIGSEGV

Re: Relocation when binary get loaded.

Post by HyperAssembler »

I've figured out the problem.
The load address is specified in multiboot header, which is relocated by linker.

...I should probably slow down the steps to 64 bits.
I need to write a elf64 interpreter first.
OS dev is fun.
HyperAssembler
Member
Member
Posts: 36
Joined: Thu Sep 04, 2014 5:24 pm
Location: !SIGSEGV

Re: Relocation when binary get loaded.

Post by HyperAssembler »

Thanks for your kind replies.
Post Reply