Theory behind relocation
Theory behind relocation
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?
- Combuster
- 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
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.
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.
Re: Theory behind relocation
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?)
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?)
- Owen
- Member
- Posts: 1700
- Joined: Fri Jun 13, 2008 3:21 pm
- Location: Cambridge, United Kingdom
- Contact:
Re: Theory behind relocation
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.
Re: Theory behind relocation
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.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?)
Some instructions, and especially in other architectures, are formatted differently so you may need to do more involved calculations.