Relocation when binary get loaded.
Posted: Sat Jan 31, 2015 10:57 pm
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,
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!!
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
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!!