Main Memory: Allocation, Linking & Segmentation

relocation

Picture moving an entire bookshop to a new building. Every internal sign that said 'fiction is 10 metres to your right' still works, because it was written relative to wherever you stand. But any sign giving an absolute room number would now be wrong and must be corrected. Relocation is the act of adjusting a program so that its references still point to the right places after it is placed at a new starting address in memory.

Concretely, a freshly compiled program does not yet know where it will live, so it contains many addresses that assume a starting point of zero (or are flagged as 'this needs fixing'). When the program is actually placed at, say, physical 90000, all those internal addresses are off by 90000. Static relocation fixes this once, at load time: the loader walks a relocation table and adds 90000 to every flagged address, baking the final numbers into the code. Dynamic relocation does not rewrite anything; instead it keeps the program in logical addresses and lets the hardware add the base (here 90000) on every memory access as the program runs — so the same code works at any base, and can even be moved later.

Why it matters: relocation is what makes a program position-independent enough to load anywhere RAM is free, which is essential when many programs share memory and when a process must be moved (for example after compaction or swapping). Dynamic relocation, done by a base register or the MMU, is the form that gives the OS the freedom to shuffle processes around at will.

A relocatable program contains 'jump to address 250'. Loaded at base 90000, static relocation rewrites that to 'jump to 90250'. With dynamic relocation the instruction still says 250, and the hardware adds the base 90000 each time it runs.

Static relocation edits the code once; dynamic relocation adjusts on every access.

Relocation and address binding are closely linked: load-time binding uses static relocation, execution-time binding uses dynamic relocation. Static relocation cannot move a program once it has been loaded.

Also called
address relocation重定址