process memory segment on linux

There are several segments in linux memory, including data segment, BSS segment, Rodata segment, heap segment and stack segment.

The Data Segmentcontains global and static variables used by the program that are initialized

The Bss Segment also known as uninitialized data starts at the end of the data segment and contains all global variables and static variables that are initialized to zero or do not have explicit initialization in source code

Rodata (read-only data) segment starts at the end of data segment which contains all the constant variables and constant strings in the code

TheHeap segment begins at the end of the BSS segment (if system has Rodata segment then it should be BSS segment) and grows to larger addresses from there.

The stack is a LIFO structure, typically located in the higher parts of memory.

Test.o is a file of Linux ELF format.

Objdump –h test.o #show all the divs in the ELF file.

Objdump –t test.o #show all symbol table in the ELF file.

Objdump –s test.o #show all the contents in the ELF file in hex.

Nm –a test.o #show symbol table in the ELF file.

Gcc –o test.i –E test.c #produces preprocessed source code.

Gcc –o test.s –S test.i #produces assembler code for each source code.

Gcc –o test.o –c test.s #compile source code to produce target object but do not link

Gcc –o test test.o #link target object to produce executable file.

Ldd test #display static or dynamic linking library used by the executable file.

Dynamic linking library are compiled target objects. It stores symbol table of the target objects. In the linking stage, the executable will not link the dynamic linking library.It will only record which library routines programs need and the index. When the executable calls the dynamic linking library it will be loaded into memory and mapped into process’s virtual address space. So it has a problem that dynamic linking library has its own data segment and if several processes access and modify the same global variable in the dynamic linking library the data will be polluted. So it is better not to allocate global variable in the dynamic linking library as a shared variable.

When an executable is executed, it is loaded into memory and operating system will give a 4G virtual address space (abbreviated VAS) to the process. All the objects used by process is stored in the memory, including text, data, BSS, heap, stack segments and those of static and dynamic linking library. Operating system is responsible for mapping the memory address of these objects to the process VAS so that process can use them. Process does not know the exact memory address of the resources it uses but it knows their addresses in the process VAS. This is the responsibility of memory management in the operation system.

example1:

From the experiment result, we can see that global and static variables that are initialized manually are located in the data segment and global and static variables that are not initialized are located in the bss segment and initialized with zero value by compiler. Local variables are stored in the stack so they are showed in the above result.

gcc compiler will treat global variable that is not initialized as a common symbol and when linking the common symbols are put into bss segment.

in the source above we declare a char * p that points to a constant string "123" and in the main function we call the function printf("%d",a) that contains a constant string "%d".

From the result above we can see that constant strings are stored in the rodata segment.

we declare a char array char tp[]="123vbcxv" but from the result above we can see that it is not stored in the rodata segment but in the data segment. This is because char array is not constant string but a sequence of characters stored in the data segment.

而开始追寻他内心世界的真正财富

process memory segment on linux

相关文章:

你感兴趣的文章:

标签云: