In our last lesson, we examined the static structure of a Linux program by dissecting the ELF file format. You learned how an executable is organized into sections for the linker and segments for the operating system loader. Today, we'll see what happens when that static file is brought to life. We will explore how the operating system uses the information from the ELF segments to construct a process's virtual address space in memory.
This lesson will detail the anatomy of a running process's memory, covering the distinct regions that serve specific purposes: the code and data segments, the heap for dynamic allocation, the stack for function calls, and the mapped regions for shared libraries. Understanding this layout is not just an academic exercise; it is the map of the territory where every memory corruption vulnerability—and its exploit—plays out.
From Blueprint to Building: The Virtual Address Space
When you execute a program, the Linux kernel's loader doesn't just copy the binary into physical RAM. Instead, it creates a virtual address space for the new process. This is a fundamental concept in modern operating systems. It provides each process with its own private, isolated view of memory, starting from address zero up to a very large address. The kernel and the CPU's Memory Management Unit (MMU) handle the translation from these virtual addresses to actual physical RAM addresses behind the scenes.
This isolation is a key security feature: one process cannot, by default, read or write to another process's memory. For an attacker, our goal is often to subvert the logic within a single process's address space to gain control.
The ELF file's program headers, which we discussed previously, act as the blueprint. The loader reads the PT_LOAD segments and maps them into this new virtual address space, setting the memory permissions (read, write, execute) as specified in the segment flags. This forms the initial core of the process memory, but several other regions are also created.
The diagram below shows a canonical layout of a process's virtual address space on Linux.

Let's explore each of these regions in detail.
The Anatomy of Process Memory
The different regions in a process's memory are not arbitrary; they are separated for security and efficiency. For a great overview of why this separation is so important, please watch this short segment.
Why Programs Use Stack, Heap, and Other Memory Segments
This video from BitLemon clearly explains the rationale behind segmenting process memory, covering security, access patterns, and bug isolation.
Watch the section that explains the reasons for memory division. Pay attention to how memory permissions prevent certain vulnerabilities.
Now, let's look at what goes into each of these specialized regions.
1. The Program Image: Code and Data
This is the portion of memory mapped directly from the executable file on disk, based on the LOAD segments in the ELF header. It's typically divided into three main parts, corresponding to the ELF sections we've already learned about.
-
Text Segment (
.text): This region contains the program's executable machine code. As you saw in the video, it is mapped with read-only and execute (r-x) permissions. This is a critical security feature called W^X (Write XOR Execute), which prevents a program from easily modifying its own instructions. An attacker who finds a way to write to this memory could directly inject malicious code. -
Data Segment (
.data): This segment stores initialized global and static variables—variables that have a predefined value in your source code (e.g.,int x = 10;). It is mapped with read-write (rw-) permissions, but not execute. -
BSS Segment (
.bss): This segment is for uninitialized global and static variables (e.g.,static char buffer[1024];). The name BSS stands for "Block Started by Symbol." Since these variables have no initial value in the source, they don't need to be stored in the executable file itself. Instead, the loader allocates a block of memory for them and fills it with zeros. Like the data segment, it has read-write (rw-) permissions.
The following video segment provides a concise explanation of these three regions.
Why Programs Use Stack, Heap, and Other Memory Segments
Continuing with the BitLemon video, we'll look at the specific segments that make up the program image.
First, watch the part covering the text segment. Then, watch the explanation of the data and BSS segments, which does an excellent job explaining why separating initialized and uninitialized data is efficient.
2. The Heap
The heap is the primary region for dynamic memory allocation. When your C program calls malloc() or your C++ program uses the new operator, the memory is provided from the heap.
- Growth: The heap is located above the BSS segment and grows upwards towards higher memory addresses.
- Management: It is managed by a library component known as the heap allocator (part of
libc), which keeps track of free and used blocks of memory. This management logic is complex and is itself a major target for advanced exploitation techniques. - Permissions: The heap is mapped with read-write (
rw-) permissions.
Why Programs Use Stack, Heap, and Other Memory Segments
The next part of the BitLemon video covers the heap.
Watch the section on the heap. It introduces the concept of a memory allocator and memory leaks, which are relevant to heap exploitation vulnerabilities.
3. The Stack
The stack is a region of memory used for managing function calls. Every time a function is called, a new "stack frame" is pushed onto the stack.
- Contents: A stack frame contains the function's local variables, arguments passed to it, and the return address (the instruction pointer to return to in the calling function). Your knowledge of x86-64 assembly, particularly the
RSP,RBP,push,pop,call, andretinstructions, is directly applicable here. - Growth: On x86-64 Linux, the stack is located at a high address and grows downwards toward lower memory addresses.
- Management: It operates on a Last-In, First-Out (LIFO) basis. Allocation and deallocation are extremely fast, simply involving decrementing or incrementing the stack pointer register (
RSP). - Permissions: The stack is mapped with read-write (
rw-) permissions. The fact that it is not executable is the basis for Non-Executable (NX) stack protection.
Why Programs Use Stack, Heap, and Other Memory Segments
The final component we'll review from this video is the stack.
Watch the explanation of the stack. This section's discussion of stack frames and the stack overflow error is a direct preview of the first major vulnerability class we will study.
4. Shared Libraries
Modern programs rarely include all the code they need. Instead, they dynamically link against shared libraries like libc.so (the C standard library). The dynamic linker (ld.so) is responsible for finding these libraries and mapping their code and data segments into the process's address space. This allows many running programs to share the same physical copy of a library's code in RAM, saving a significant amount of memory.
These library mappings typically appear in a memory region between the heap and the stack. They will have their own text (r-x) and data (rw-) segments.
Inspecting Memory in Practice: The /proc Filesystem
Theory is useful, but the ability to verify it is essential. Linux provides a special pseudo-filesystem called /proc that exposes kernel data structures as files. For any running process, you can inspect its complete memory map by reading the file /proc/<PID>/maps, where <PID> is the process ID.
This file is your ground truth for understanding a process's memory layout. The following article provides an excellent, detailed guide to interpreting its contents.
Fiddling with Linux Processes in Memory - Linux.com
This article from Linux.com provides a fantastic practical guide to exploring process memory. We will focus on its explanation of the maps file.
Please start reading from the heading "The real addresses". Read through the explanation of the maps file output, paying close attention to the table that describes each column: Address, Permissions, Offset, Device, INode, and Pathname. Continue reading as the author analyzes the memory map of a sample program, identifying the text, data, heap, and shared library regions based on their permissions and pathnames. Stop before the subsection "A simple exercise".
As you've just read, the permissions column is key.
r-xp(read, execute, private) is a strong indicator of a text segment.rw-p(read, write, private) typically indicates a data, BSS, heap, or stack segment.- The
Pathnamecolumn tells you which file is mapped (the executable itself or a shared library), or it provides a label like[heap]or[stack].
For a quick demonstration of this in action, the pwn.college series has a relevant clip.
pwn.college - Program Interaction - Linux Process Loading
This video from pwn.college's lectures on Linux process loading shows a live demonstration of inspecting the memory map.
Watch the segment from this timestamp, where the instructor uses cat /proc/self/maps to show the memory layout of the cat command itself. Notice the randomized addresses for the binary and stack, a protection we will discuss later called ASLR.
To connect all these concepts, I strongly recommend you work through the practical exercise in the Linux.com article you just read. It uses GDB to print the addresses of different types of variables (global, local, heap-allocated) and matches them to the memory regions listed in the /proc/<pid>/maps file. This is an invaluable exercise for building practical intuition.
Fiddling with Linux Processes in Memory - Linux.com
This is the final part of the same article.
Now, please read the final two sections of the article, starting from "A simple exercise". Follow along with the author's use of GDB to find variable addresses and correlate them with the maps file output. You don't need to compile and run the exact code, but make sure you understand the logic of how each variable's address places it within a specific memory segment (stack, data, or heap).
Conclusion
In this lesson, we transitioned from the static ELF file to the dynamic world of a running process. You now have a mental model of how a process's memory is laid out and the tools to inspect it.
Key Takeaways:
- Each process runs in its own virtual address space, isolated from other processes.
- The memory is divided into distinct regions:
- Text Segment: Read-only, executable code from the program binary.
- Data/BSS Segments: Read-write memory for global and static variables.
- Heap: Read-write memory for dynamic allocations (
malloc), growing upwards. - Stack: Read-write memory for function calls (local variables, return addresses), growing downwards.
- Shared Libraries: Mapped regions for code and data from libraries like
libc.so.
- This separation is enforced by memory permissions (
r,w,x), a cornerstone of modern OS security. - The
/proc/<pid>/mapsfile provides a complete, live view of a process's memory layout.
We have only scratched the surface of the /proc filesystem. In our next lesson, we will take a deeper dive, exploring it as a powerful tool not just for inspection, but for gathering the intelligence needed to craft exploits.