Address Translation

2026-11-18 · virtualisation

notes / Operating Systems / 01_vir

OS

Mechanism: Address Translation

Tip

Interposition is powerful, and helps you add transparency (invisibility) to your systems. Here the hardware interposes on each memory access

An Example

┌ 22: int main (int argc, char **argv, char **envp);
│ afv: vars(1:sp[0xc..0xc])
│ 0x00001119      55             push rbp
│ 0x0000111a      4889e5         mov rbp, rsp
│ 0x0000111d      c745fcb80b..   mov dword [var_4h], 0xbb8   ; 3000
│ 0x00001124      8345fc03       add dword [var_4h], 3
│ 0x00001128      b800000000     mov eax, 0
│ 0x0000112d      5d             pop rbp
└ 0x0000112e      c3             ret

This disassembler claims that the main function starts at address 0x1119 of the program.

┌ 11: int dbg.main (int argc, char **argv, char **envp);
│ 0x00001119      55             push rbp                    ; test.c:3:16int main(void) { return 42; } ; int main();
│ 0x0000111a      4889e5         mov rbp, rsp
│ 0x0000111d      b82a000000     mov eax, 0x2a               ; test.c:3:25int main(void) { return 42; } ; '*'
│ 0x00001122      5d             pop rbp                     ; test.c:3:29int main(void) { return 42; }
└ 0x00001123      c3             ret

But it claims this program starts at 0x1119 too. So if I ran them both together, wouldn't we have an issue? Like we mentioned earlier, all addresses that the program sees are virtual. Both the programs believe their main functions start at 0x1119 and as the programmer, so do you. However its the OS's job to ensure they are mapped to different memory blocks and do not interfere with each other.

Let's look at an example mapping

❯ build test.c
Compilation successful: test.out
pid : 29815
main: 0x561c168ae179
heap: 0x561c39151430
stac: 0x7ffc4c586d74
❯ cat /proc/29815/maps
561c168ad000-561c168ae000 r--p 00000000                   /home/subzcuber/test.out
561c168ae000-561c168af000 r-xp 00001000                   /home/subzcuber/test.out
561c168af000-561c168b0000 r--p 00002000                   /home/subzcuber/test.out
561c168b0000-561c168b1000 r--p 00002000                   /home/subzcuber/test.out
561c168b1000-561c168b2000 rw-p 00003000                   /home/subzcuber/test.out
561c3913f000-561c39172000 rw-p 00000000                   [heap]
...
7fb532b16000-7fb532c7f000 r-xp 00024000                   /usr/lib/libc.so.6
7fb533136000-7fb53315f000 r-xp 00001000                   /usr/lib/ld-linux-x86-64.so.2
...
7ffc4c567000-7ffc4c588000 rw-p 00000000                   [stack]

All the addresses you see to the left there are virtual addresses, these are addresses that the program is allowed to address. proc/pid/maps returns the segments of that virtual address space that have been mapped to physical memory in the main memory. Notice how we don't have any mappings before 0x561c1... whatever. Nor do we have any between 0x561... and 0x7ff.. whatever. This goes to show that all the memory used by a program does not need to be loaded in the memory, only the relevant parts.

Internal Fragmentation

By not loading our entire address space at once we are actually avoiding a problem called internal fragmentation, where unused space inside the address space is wasted. This is usually far less wasteful than external fragmentation though

Dynamic (Hardware-Based) Relocation

A basic address translation scheme could be to work with offsets, where programs may have the same offsets, but the OS will give them different bases thus keeping their memory somewhate separate.

add two registers

now when the process runs any virtual address is converted to physical as physical address = virtual address + $base and ensures it is within the bounds

This is a good example of interposition

Static Relocation

Initially before running a program, a loader would translate every address to the desired offset The issue with this is protection is breached. In general you need hardware support for true protection

Of course this still has issues we must resolve, namely those of both internal and external fragmentation, since we are still treating memory as contiguous

Hardware Support

The setting of the base and bounds registers is a privileged operation, and must be done so only in kernel mode.

OS Issues