CSE Roadmap

Core tier · about 4 hours · 6 parts

Part 4 of 6 · 2 min read

A real layout

In plain words: A process's address space is divided into regions with different jobs. A short C program printed the address of an item from each region:

int g_init = 42;              /* initialised data */
int g_zero;                   /* uninitialised data (bss) */
const char msg[] = "hello";   /* read-only data */
int main(void) {
    int local = 1; int *heap = malloc(100); void *big = malloc(1 << 20);
    printf("code   (main)  %p\n", (void*)main);   /* ... and the others in the same way */
}

With address randomisation switched off (setarch -R), the output was:

code   (main)        0x555555555199
rodata (msg)         0x555555556004
data   (g_init)      0x555555558010
bss    (g_zero)      0x555555558018
heap   (malloc 100)  0x5555555592a0
mmap   (malloc 1MB)  0x7ffff7ea9010
libc   (printf)      0x7ffff7c60100
stack  (local)       0x7fffffff9be4

The first rows form the program's own image: code, then read-only data, then data. The heap, which holds malloc memory, begins just above them. The shared library libc and the large block of 1 MB lie much higher, near 0x7ffff7.... The stack is at the top, near 0x7fffffff..., and grows downward. (The 1 MB block is larger than the usual 128 KiB threshold of the default allocator, so malloc puts it in its own mapped region. Level 7 returns to malloc.)

The file /proc/self/maps lists the regions of a running process. For the same program, the key lines were (shortened):

555555554000-555555555000 r--p  /tmp/m6/maps        (ELF header, read-only)
555555555000-555555556000 r-xp  /tmp/m6/maps        (code: read, execute)
555555558000-555555559000 rw-p  /tmp/m6/maps        (data: read, write)
555555559000-55555557a000 rw-p  [heap]
7ffff7c28000-7ffff7db1000 r-xp  /usr/lib/x86_64-linux-gnu/libc.so.6
7ffffffda000-7ffffffff000 rw-p  [stack]

Each line gives a start and end address, the permissions (r read, w write, x execute, p private) and the file behind the region. The code region cannot be written, and the data region cannot be executed. These permissions are checked by the hardware on every access, and page 6 explains how.

Notice what is missing: there is no physical address anywhere. The process sees only logical addresses. Where each region sits in the physical memory is known to the kernel and the MMU alone (page 3 shows how to ask the kernel for it).

Virtual address

A virtual address is an address that a process uses in its instructions and data; it is not a place in the physical memory, but a number in the process's own address space that the hardware translates on each access.

Example: the stack address 0x7fffffff9be4 printed by the program is a virtual address and has no meaning outside that process.

Check yourself: 1. Which region is at the highest addresses, and in which direction does it grow? 2. What do the letters r-xp mean for the code region? 3. Why do the printed addresses tell you nothing about where the data sits in the real memory?