5. Linux Application Address layout
Program Composition:
When you learn Linux application development, you often experience the following concepts: Code snippets, data segments, BSS segments (Block Started by Symbol, AKA: Uninitialized data segments), heap (heap), and stack (stack). These parts are also an important part of the Linux application.
Memory layout:
When a Linux application runs in memory, the above components are laid out in memory:
- From low address to high address are: Code snippet, data segment, BSS segment, heap, stack.
- The heap grows to a high memory address.
- The stack grows to a low memory address.
The following program is tested below:
To view the threads of a system:
Here we look at the address assignment for this code:
Address of data storage:
- Code snippet: Code, global constant (const), string constant.
- Data segments: Global variables (initialization and uninitialized), static variables (global and local, initialized, and uninitialized).
- Heap: Dynamically allocated regions
- Stacks: Local variables (initialization and uninitialized, but excluding static variables), local read-only variables (const).
?
?
?
From the above: The starting address of the code snippet is 8048000. Data segment start Address 8049000, heap start address b77a5000, stack starting address bfb47000.
?
?
Analysis Data Storage location:
Example code:
#include <stdio.h>
#include <stdlib.h>
int global_init_a=1; Global init variable
int global_uninit_a; Global UnInit variable
static int static_global_init_a=1;
static int static_global_uninit_a;
const int const_global_a=12;
?
int global_init_b=2; Global init variable
int global_uninit_b; Global UnInit variable
static int static_global_init_b=12;
static int static_global_uninit_b;
const int const_global_b=12;
?
int main ()
{
???? int local_init_a=1;
???? int local_uninit_a;
???? static int static_local_init_a=1;
???? static int static_local_uninit_a;
???? const int const_local_a=12;
?
???? int local_init_b=1;
???? int local_uninit_b;
???? static int static_local_init_b=1;
???? static int static_local_uninit_b;
???? const int const_local_b=12;
???? int * MALLOC_P_A;
???? Malloc_p_a=malloc (sizeof (int)); //
?
?
???? printf ("&global_init_a =%p\n", &global_init_a);
???? printf ("&global_uninit_a =%p\n", &global_uninit_a);
???? printf ("&static_global_init_a =%p\n", &static_global_init_a);
???? printf ("&static_global_uninit_a =%p\n", &static_global_uninit_a);
???? printf ("&const_global_a =%p\n", &const_global_a);
?
???? printf ("&global_init_b =%p\n", &global_init_b);
???? printf ("&global_uninit_b =%p\n", &global_uninit_b);
???? printf ("&static_global_init_b =%p\n", &static_global_init_b);
???? printf ("&static_global_uninit_b =%p\n", &static_global_uninit_b);
???? printf ("&const_global_b =%p\n", &const_global_b);
?
???? printf ("&local_init_a =%p\n", &local_init_a);
???? printf ("&local_uninit_a =%p\n", &local_uninit_a);
???? printf ("&static_local_init_a =%p\n", &static_local_init_a);
???? printf ("&static_local_uninit_a =%p\n", &static_local_uninit_a);
???? printf ("&const_local_a =%p\n", &const_local_a);
?
???? printf ("&local_init_b =%p\n", &local_init_b);
???? printf ("&local_uninit_b =%p\n", &local_uninit_b);
???? printf ("&static_local_init_b =%p\n", &static_local_init_b);
???? printf ("&static_local_uninit_b =%p\n", &static_local_uninit_b);
???? printf ("&const_local_b =%p\n", &const_local_b);
?
???? printf ("&malloc_p_a =%p\n", &malloc_p_a);
?
?
???? return 0;
}
?
?
Analyze BSS Segments:
A BSS segment is a sub-segment of a data segment.
5. Linux Application Address layout