Linux Application Address layout

Source: Internet
Author: User

The layout of the Linux application in memory, from high address to low address, is: Stack, heap, BSS segment, data segment, code snippet. The starting address of the code snippet is fixed to 0x8048000, regardless of which application its code snippet start address must be 0x8048000, where the address virtual address is mapped to a different physical address.
View the address of each segment of the program
PS aux
This command is used to view the ID of the process, such as I run an executable program addr

    

    

You can see that the process ID for addr is 24048. Then use the Cat command to view the internal layout of the process
cat/proc/Process Id/maps, here we are cat/proc/24048/maps

    

From top to bottom are code snippets, data segments, heaps, stacks. Includes information such as their starting address and ending address. Then execute the sample program.

    

1#include <stdio.h>2 3 intGlobal_init_a =1;//globally initialized variables4 intGlobal_uinit_a;//variables that are not initialized globally5 Static intStatic_global_init_a =1;//global static initialization variables6 Static intStatic_global_uinit_a;//global static uninitialized variable7 Const intConst_global_a =1;//Global Constants8 9 intGlobal_init_b =1;//globally initialized variablesTen intGlobal_uinit_b;//variables that are not initialized globally One Static intStatic_global_init_b =1;//global static initialization variables A Static intStatic_global_uinit_b;//global static uninitialized variable - Const intConst_global_b =1;//Global Constants -  the  - voidMain () - { -     intLocal_init_a =1;//Local initialization Variables +     intLocal_uinit_a;//Local uninitialized variable -     Static intStatic_local_init_a =1;//local static initialization variables +     Static intStatic_local_uinit_a;//local static uninitialized variable A     Const intConst_local_a =1;//Local Constants at  -     intLocal_init_b =1;//Local initialization Variables -     intLocal_uinit_b;//Local uninitialized variable -     Static intStatic_local_init_b =1;//local static initialization variables -     Static intStatic_local_uinit_b;//local static uninitialized variable -     Const intConst_local_b =1;//Local Constants in      -     int*malloc_p_a; toMalloc_p_a = malloc (sizeof(int));//partial distribution by malloc +  -printf"&global_init_a=%p,global_init_a=%d\n",&global_init_a,global_init_a); theprintf"&global_uinit_a=%p,global_uinit_a=%d\n",&global_uinit_a,global_uinit_a); *printf"&static_global_init_a=%p,static_global_init_a=%d\n",&static_global_init_a,static_global_init_a); $printf"&static_global_uinit_a%p,static_global_uinit_a=%d\n",&static_global_uinit_a,static_global_uinit_a);Panax Notoginsengprintf"&const_global_a=%p,const_global_a=%d\n",&const_global_a,const_global_a); -  theprintf"&global_init_b=%p,global_init_b=%d\n",&global_init_b,global_init_b); +printf"&global_uinit_b=%p,global_uinit_b=%d\n",&global_uinit_b,global_uinit_b); Aprintf"&static_global_init_b=%p,static_global_init_b=%d\n",&static_global_init_b,static_global_init_b); theprintf"&static_global_uinit_b%p,static_global_uinit_b=%d\n",&static_global_uinit_b,static_global_uinit_b); +printf"&const_global_b=%p,const_global_b=%d\n",&const_global_b,const_global_b); -  $printf"&local_init_a=%p,local_init_a=%d\n",&local_init_a,local_init_a); $printf"&local_uinit_a=%p,local_uinit_a=%d\n",&local_uinit_a,local_uinit_a); -printf"&static_local_init_a=%p,static_local_init_a=%d\n",&static_local_init_a,static_local_init_a); -printf"&static_local_uinit_a%p,static_local_uinit_a=%d\n",&static_local_uinit_a,static_local_uinit_a); theprintf"&const_local_a=%p,const_local_a=%d\n",&const_local_a,const_local_a); - Wuyiprintf"&local_init_b=%p,local_init_b=%d\n",&local_init_b,local_init_b); theprintf"&local_uinit_b=%p,local_uinit_b=%d\n",&local_uinit_b,local_uinit_b); -printf"&static_local_init_b=%p,static_local_init_b=%d\n",&static_local_init_b,static_local_init_b); Wuprintf"&static_local_uinit_b%p,static_local_uinit_b=%d\n",&static_local_uinit_b,static_local_uinit_b); -printf"&const_local_b=%p,const_local_b=%d\n",&const_local_b,const_local_b); About  $printf"malloc_p_a=%p,*malloc_p_a=%d\n", malloc_p_a,*malloc_p_a); -  -      while(1); -}

Run results

  

Results Analysis:

    Global initialization variable in data segment
Global uninitialized variable in data segment
Global static initialization variable in data segment
Global static uninitialized variable in data segment
Global constants in Code snippets

Local initialization variables are located on the stack
Local uninitialized variable on stack
Local static initialization variable in data segment
Local static uninitialized variable in data segment
Local constants are located on the stack

Pointers assigned through malloc are located in the heap

This makes the analysis clear. Wait, we seem to have missed the BSS section. In fact, the BSS segment is a subset of the data segment.
Analyze BSS segments by READ-S program name

    

The analysis shows that both global uninitialized variables and global static uninitialized variables belong to BSS segments in the data segment. In this way, the layout of the Linux application in memory is analyzed clearly!

Linux Application Address layout

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.