General memory layout

Given the following general memory layout illustration from GeeksforGeeks:

Important things to note

In this image, the low address is placed at the bottom of the drawing, while the high address is at the top

  1. The stack grows towards lower memory addresses

  2. The heap grows towards higher memory addresses

Fields within the memory

  1. Stack

  • Stores local variables in a function, return addresses and function call information

  • stores variables with values that changes during runtime (but are usually of fixed size)

  • The variables are automatically cleared when function returns (stack pointer adjusted)

  1. Heap

  • Dynamically allocated memory

  • Explicitly requested and freed by the programmer

  • Size can vary during runtime

  • eg. malloc(), new int[x] , etc.

  1. Unitialized data (bss)

  • This segment stores all uninitialized global and static variables

  • These variables are automatically initialized to zero by the system at runtime

  1. Initialized data

  • This section stores all initialized global and static variables of the program

  • Variables in this segment retain their values throughout the lifetime of the program execution

  1. Text

  • The text segment (or code segment) contains the executable code of the compiled program — in the form of machine code

Example print "hello world!" binary

  • We will be using the following print "hello world!" binary as an example:

We can use the info files command which simply shows the names of targets and files being debugged:

From the output, we can see multiple sections such as: .text , .data .bss

Summary table of the memory sections

Section
Notes
Reference

.init

Small init code ran by the loader before the main() function

.plt

Procedure Linkage Table

.fini

Cleanup code that is ran after main() exists

.rodata (Read-Only Data)

Read-only constant data — non-writable

.text

Text/code segment

  • contains machine code of the compiled program

.got (Global Offset Table)

A section inside of programs that holds addresses of functions that are dynamically linked

.data

Initialized data segment

  • global and static variables that are initialized

.bss (Block started by symbol)

Unitialized data segment

  • global and static variables that do not have explicit initialization

  • all uninitialized variables are initialized to zero at load time (by the loader)

Last updated