Theory behind relocation

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

Theory behind relocation

Post by mariuszp »

OK, so relocation is basically "moving" addresses inside an executable so that it can be loaded at a position different from the one it wants to be loaded it, right? But how exactly does relocation happen? I mean, do I have to have some really advanced loader (in my kernel) that can update whole instructions to change the addresses based on what the executable says? Or is the instruction 'broken up' so I can insert the address in?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Theory behind relocation

Post by Combuster »

Relocating is just the process of changing addresses when segments of the executable is loaded at a different location as they were originally linked to. So if you link a binary to 1M, and you load it to 3G, all absolute addresses will need to have the difference (3G-1M) added to them.

Relocation entries consist of just the section and offset of the address to be patch, the segment of the target, and the type of relocation. So absolute relocations will be updated when the offset changes, and relative relocations will be update when the distance between section starts change. In both cases you simply add the difference to the original location. And those are the only relocations you will find in a simple 32-bits executable.

The process is relatively simple: my own relocator does the job in about 50 lines of assembly.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

Re: Theory behind relocation

Post by mariuszp »

You mean, it's something like:

The relocation table contains the address of the address to relocate (target?) and I just change the 'target' to the address needed (add the difference?)
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: Theory behind relocation

Post by Owen »

For simple executables, yes. For more complex ones (e.g. if doing runtime linking against a shared object or DLL) and dealing with things like position independent code and/or IP-relative addressing, things will be more involved.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Theory behind relocation

Post by JamesM »

mariuszp wrote:You mean, it's something like:

The relocation table contains the address of the address to relocate (target?) and I just change the 'target' to the address needed (add the difference?)
Pretty much - although how you change the target may be slightly more involved. Most 32-bit x86 instructions take a 32-bit absolute/relative offset, so yes, just adding the difference will work.

Some instructions, and especially in other architectures, are formatted differently so you may need to do more involved calculations.
Post Reply