Talk C chestnuts together (130th back: C language instance -- C program memory layout II)

Source: Internet
Author: User

Talk C chestnuts together (130th back: C language instance -- C program memory layout II)

Hello, everyone. The last time we talked about the C program memory layout example, let's continue with this example. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

In the last session, we introduced the layout of the C program in the memory and gave a simple demonstration. The previous example is relatively simple and can only describe the general outline of the memory layout in the program. We will introduce the layout of C Programs in memory through the specific memory address today. The following is the sample program source code. For details, refer:

int ga1;int ga2 = 1;int func(){    int i = 0;    static static_la1;    static static_la2 = 3;    printf("func is running \n");    printf("Address of i: %p \n",&i);    printf("Address of static_la1 : %p \n",&static_la1);    printf("Address of static_la2 : %p \n",&static_la2);    while(i++<8)        sleep(1);    return 0;}int main(){    int la1;    int la2 = 2;    int *p;    p = (int *) malloc(3*sizeof(int));    if(NULL == p)        printf("malloc failed \n");    printf("Address of ga1: %p \n",&ga1);    printf("Address of ga2: %p \n",&ga2);    printf("Address of la1: %p \n",&la1);    printf("Address of la2: %p \n",&la2);    printf("Address of p: %p \n",p);    func();    free(p);    p = NULL;    return 0;}

In this Code, we manually output the addresses of each variable to determine the location of the variable in the memory through the address of the variable, then confirm which partition of the variable belongs to the memory layout. In which partition is these variables? Data Partition or bss partition? The following is the running result of the program. For details, refer:

Address of ga1: 0x804a040 Address of ga2: 0x804a030 Address of la1: 0xbfc7e7b4 Address of la2: 0xbfc7e7b8 Address of p: 0x8ffc008 func is running Address of i: 0xbfc7e78c Address of static_la1 : 0x804a03c Address of static_la2 : 0x804a034 

From the running results of the program, we can see the address of each variable in the memory. However, we still don't know which memory partition these variables belong. We can use the readelf tool to view the memory layout of the program. The address range of each memory partition can be seen through the memory layout, we can infer the memory partition where the variable is located based on the address of the variable. The detailed results are as follows:

Readelf-S s // use the readelf tool to view the program's memory layout There are 30 section headers, starting at offset 0x1190: Section Headers: [Nr] Name Type Addr Off Size ES Flg Lk Inf Al [0] NULL 00000000 000000 000000 00 0 0 0 [1]. interp PROGBITS 08048154 000154 000013 00 A 0 0 1 [2]. note. ABI-tag NOTE 08048168 000168 000020 00 A 0 0 4 [3]. note. gnu. build-I NOTE 08048188 000188 000024 00 A 0 0 4 [4]. gnu. hash GNU_HASH 080481ac 0001ac 000020 04 A 5 0 4 [5]. dynsym DYNSYM 080481cc 0001cc 000090 10 A 6 1 4 [6]. dynstr STRTAB 0804825c 00025c 000063 00 A 0 0 1 [7]. gnu. version VERSYM 080482c0 0002c0 000012 02 A 5 0 2 [8]. gnu. version_r VERNEED 080482d4 0002d4 000020 00 A 6 1 4 [9]. rel. dyn REL 080482f4 0002f4 000008 08 A 5 0 4 [10]. rel. plt REL 080482fc 0002fc 000038 08 A 5 12 4 [11]. init PROGBITS 08048334 000334 000023 00 AX 0 0 4 [12]. plt PROGBITS 08048360 000360 000080 04 AX 0 0 16 [13]. text PROGBITS 080483e0 0003e0 0002a2 00 AX 0 0 16 [14]. fini PROGBITS 08048684 000684 000014 00 AX 0 0 4 [15]. rodata PROGBITS 08048698 000698 running DC 00 A 0 0 4 [16]. eh_frame_hdr PROGBITS 08048774 000774 000034 00 A 0 0 4 [17]. eh_frame PROGBITS 0801_a8 0007a8 1_d0 00 A 0 0 4 [18]. init_array INIT_ARRAY 08049f08 000f08 000004 00 WA 0 0 4 [19]. fini_array FINI_ARRAY 08049f0c 000f0c 000004 00 WA 0 0 4 [20]. jcr PROGBITS 08049f10 000f10 000004 00 WA 0 0 4 [21]. dynamic DYNAMIC 08049f14 000f14 g0e8 08 WA 6 0 4 [22]. got PROGBITS 08049ffc 000ffc 000004 04 WA 0 0 4 [23]. got. plt PROGBITS 0804a000 001000 000028 04 WA 0 0 4 [24]. data PROGBITS 0804a028 001028 000010 00 WA 0 0 4 [25]. bss NOBITS 0804a038 001038 00000c 00 WA 0 0 4 [26]. comment PROGBITS 00000000 001038 rj4f 01 MS 0 0 1 [27]. shstrtab STRTAB 00000000 001087 000106 00 0 0 1 [28]. symtab SYMTAB 00000000 001640 0004c0 10 29 47 4 [29]. strtab STRTAB 00000000 001b00 0002be 00 0 1Key to Flags: W (write), A (alloc), X (execute), M (merge), S (strings) I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown) O (extra OS processing required) o (OS specific), p (processor specific)

You can see

The data partition starts from the address 0x0804a028, And the size is 0x000010 (16 bytes ).
The bss partition starts from 0x0804a038 and the size is 0x00000c (12 byte ).

Then we can infer the memory partition where the variable is located based on the variable address in the program:

Address of ga1: 0x804a040 // The variable Address is greater than bss (0x0804a038). The variable Address in the bss partition Address of ga2: 0x804a030 // The variable Address is greater than data (0x0804a028) and smaller than bss, the variable belongs to the data Partition Address of static_la1: 0x804a03c // The variable Address is greater than bss (0x0804a038), the variable is located in the bss partition Address of static_la2: 0x804a034 // The variable Address is greater than data ), the variable is smaller than bss and belongs to the data partition.

The address of four other variables is not in the range of the bss and data partitions. Therefore, the partition where these variables are located cannot be determined:

Address of la1: 0xbfc7e7b4 // The variable Address is greater than bss (0x0804a038), but beyond the bss partition range Address of la2: 0xbfc7e7b8 // The variable Address is greater than bss (0x0804a038 ), however, the Address of p: 0x8ffc008 variable beyond the bss partition range is greater than bss (0x0804a038), but beyond the bss partition range Address of I: 0xbfc7e78c // The variable address is greater than bss (0x0804a038), but beyond the range of bss partitions

We introduced in the previous chapter that there are four important partitions. They are not in data and bss, so they can only be in the heap or stack. Now we only have the address of the variable. It would be nice to have the address range of the heap or stack. In this way, we can infer the partitions of these variables in the memory from the variables in the bss and data partitions just now.

Next, let's look at the stack and heap partition of the program. Do you still remember the proc Virtual File System we introduced in the previous chapter? We can use it to obtain the memory space information of the stack and heap areas.

. /S & // run the compiled program [1] 2736 in the background. // The program runs normally in the background and displays the PIDAddress of ga1: 0x804a040 // The program is running, address of ga2: 0x804a030 Address of la1: 0xbfe01cf4 Address of la2: 0xbfe01cf8 Address of p: 0x964d008 func is running Address of I: 0xbfe01ccc Address of static_la1: 0x804a03c Address of static_la2: 0x804a034 cat/proc/2736/maps // view process information in proc 08048000-08049000 r-xp 00000000 22415696/home/talk8/s08049000-0804a000 r -- p 00000000 22415696/home /talk8/s0804a000-0804b000 rw-p 00001000 22415696/home/talk8/s0964d000-0966e000 rw-p 00000000 0 [heap] b75b7000-b75b8000 rw-p 00000000 0 b75b8000-b7761000 r-xp 00000000 6444/ lib/i386-linux-gnu/libc-2.19.sob7761000-b7763000 r -- p 001a9000 08:16 6444/lib/i386-linux-gnu/libc-2.19.sob7763000-b7764000 rw-p 001ab000 08:16 6444/lib/i386-linux-gnu/libc-2.19.sob7764000-b7767000 rw-p 00000000 0 b777e000-b7781000 rw-p 00000000 0 b7781000-b7782000 r-xp 00000000 0 [vdso] b7782000-b77a2000 r-xp 00000000/lib/i386-linux-gnu/ld-2.19.sob77a2000-b77a3000 r -- p 0001f000 08:16 6459/lib/i386-linux-gnu/ld-2.19.sob77a3000-b77a4000 rw-p 00020000 6459/lib/i386-linux-gnu/ld-2.19.sobfde3000-bfe04000 rw-p 00000000 0 [stack]

From the above information, we can see that the partitions marked as [heap] and [stack] Are the heap and stack areas we are looking, the content in the same row shows the memory address space of the two partitions.

Heap: 0x0964d000-0x0966e000
STACK: 0xbfde3000-0xbfe04000

Now, we can infer which four variables do not know their partitions. The following is our inference result:

Address of la1: 0xbfe01cf4 // The variable Address is located in the stack Address space. The variable Address is located in the stack Address of la2: 0xbfe01cf8 // The variable Address is located in the stack Address space, the variable is located in the stack Address of p: 0x964d008 // the Address of the variable is located in the heap Address space. The variable is located in the heap Address of I: 0xbfe01ccc // The variable address is located in the stack address space, and the variable is located in the stack Area

So far, the specific memory address clearly shows the memory layout of the C program. Finally, let's draw an intuitive graph to show you, so that you can intuitively see the memory model of the C program.
Download and use (CSDN has a problem, so you cannot upload the program for the time being. After CSDN has fixed the problem, I will upload the program in time and you can download the program from our resources ). Because each person's computer is different and the programming environment used may be different, we recommend that you download the program to your computer to run it, then observe the model of the program in the memory according to the method we introduced.

Let's talk about the example of C program memory layout. I want to know what examples will be provided later, and I will try again.

Related Article

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.