Memory Layout of C Programs 这篇文章主要讲C程序中的内存布局。

main


A typical memory representation of a C program consists of the following sections.

  1. Text segment (i.e. instructions)
    frequently executed programs

    read-only
  2. Initialized data segment
    the global variables and static variables that are initialized by the programmer

    This segment can be further classified into the initialized read-only area and the initialized read-write area.
  3. Uninitialized data segment (bss)
    global variables and static variables that are initialized to zero or do not have explicit initialization in source code

    Data in this segment is initialized by the kernel to arithmetic 0 before the program starts executing uninitialized data.
  4. Heap
    The Heap area is managed by malloc, realloc, and free
  5. Stack
    Stack, where automatic variables are stored, along with information that is saved each time a function is called. Each time a function is called, the address of where to return to and certain information about the caller’s environment, such as some of the machine registers, are saved on the stack. The newly called function then allocates room on the stack for its automatic and temporary variables.

    局部变量、函数调用信息、中断现场保留信息

test

#include <stdio.h>
 
int global; /* Uninitialized variable stored in bss*/
 
int main(void)
{
    //static int i;   /* Uninitialized static variable stored in bss */
    static int i = 0; /* Uninitialized static variable stored in bss */
    //static int j = 10; /* Initialized static variable stored in Initialized data segmemt */
    return 0;
}
# gaoliang @ gaoliang-pc in ~/work/leetcode/test on git:master x [10:50:29]
$ size memory_test
   text       data        bss        dec        hex    filename
   1418        544         16       1978        7ba    memory_test
objdump -s -d memory_layout > memory_layout_obj.txt
memory_layout:     file format elf64-x86-64

Contents of section .interp:
 0318 2f6c6962 36342f6c 642d6c69 6e75782d  /lib64/ld-linux-
 0328 7838362d 36342e73 6f2e3200           x86-64.so.2.    
Contents of section .note.gnu.property:
 0338 04000000 10000000 05000000 474e5500  ............GNU.
 0348 020000c0 04000000 03000000 00000000  ................
Contents of section .note.gnu.build-id:
 0358 04000000 14000000 03000000 474e5500  ............GNU.
 0368 bdd76df2 2293d5b4 75a2dacb a2c2891a  ..m."...u.......
 0378 a049839d                             .I..            
Contents of section .note.ABI-tag:
 037c 04000000 10000000 01000000 474e5500  ............GNU.
 038c 00000000 03000000 02000000 00000000  ................
Contents of section .gnu.hash:
 03a0 02000000 05000000 01000000 06000000  ................
 03b0 00008100 00000000 05000000 00000000  ................
 03c0 d165ce6d                             .e.m            
Contents of section .dynsym:
 03c8 00000000 00000000 00000000 00000000  ................
 03d8 00000000 00000000 38000000 20000000  ........8... ...
 03e8 00000000 00000000 00000000 00000000  ................
 03f8 1a000000 12000000 00000000 00000000  ................
 0408 00000000 00000000 54000000 20000000  ........T... ...
 0418 00000000 00000000 00000000 00000000  ................
 0428 63000000 20000000 00000000 00000000  c... ...........
 0438 00000000 00000000 0b000000 22000000  ............"...
 0448 00000000 00000000 00000000 00000000  ................
Contents of section .dynstr:
 0458 006c6962 632e736f 2e36005f 5f637861  .libc.so.6.__cxa
 0468 5f66696e 616c697a 65005f5f 6c696263  _finalize.__libc
 0478 5f737461 72745f6d 61696e00 474c4942  _start_main.GLIB
 0488 435f322e 322e3500 5f49544d 5f646572  C_2.2.5._ITM_der
 0498 65676973 74657254 4d436c6f 6e655461  egisterTMCloneTa
 04a8 626c6500 5f5f676d 6f6e5f73 74617274  ble.__gmon_start
 04b8 5f5f005f 49544d5f 72656769 73746572  __._ITM_register
 04c8 544d436c 6f6e6554 61626c65 00        TMCloneTable.   
Contents of section .gnu.version:
 04d6 00000000 02000000 00000200           ............    
Contents of section .gnu.version_r:
 04e8 01000100 01000000 10000000 00000000  ................
 04f8 751a6909 00000200 2c000000 00000000  u.i.....,.......
Contents of section .rela.dyn:
 0508 f03d0000 00000000 08000000 00000000  .=..............
 0518 20110000 00000000 f83d0000 00000000   ........=......
 0528 08000000 00000000 e0100000 00000000  ................
 0538 08400000 00000000 08000000 00000000  .@..............
 0548 08400000 00000000 d83f0000 00000000  .@.......?......
 0558 06000000 01000000 00000000 00000000  ................
 0568 e03f0000 00000000 06000000 02000000  .?..............
 0578 00000000 00000000 e83f0000 00000000  .........?......
 0588 06000000 03000000 00000000 00000000  ................
 0598 f03f0000 00000000 06000000 04000000  .?..............
 05a8 00000000 00000000 f83f0000 00000000  .........?......
 05b8 06000000 05000000 00000000 00000000  ................
Contents of section .init:
 1000 f30f1efa 4883ec08 488b05d9 2f000048  ....H...H.../..H
 1010 85c07402 ffd04883 c408c3             ..t...H....     
Contents of section .plt:
 1020 ff35a22f 0000f2ff 25a32f00 000f1f00  .5./....%./.....
Contents of section .plt.got:
 1030 f30f1efa f2ff25bd 2f00000f 1f440000  ......%./....D..
Contents of section .text:
 1040 f30f1efa 31ed4989 d15e4889 e24883e4  ....1.I..^H..H..
 1050 f050544c 8d055601 0000488d 0ddf0000  .PTL..V...H.....
 1060 00488d3d c1000000 ff15722f 0000f490  .H.=......r/....
 1070 488d3d99 2f000048 8d05922f 00004839  H.=./..H.../..H9
 1080 f8741548 8b054e2f 00004885 c07409ff  .t.H..N/..H..t..
 1090 e00f1f80 00000000 c30f1f80 00000000  ................
 10a0 488d3d69 2f000048 8d35622f 00004829  H.=i/..H.5b/..H)
 10b0 fe4889f0 48c1ee3f 48c1f803 4801c648  .H..H..?H...H..H
 10c0 d1fe7414 488b0525 2f000048 85c07408  ..t.H..%/..H..t.
 10d0 ffe0660f 1f440000 c30f1f80 00000000  ..f..D..........
 10e0 f30f1efa 803d252f 00000075 2b554883  .....=%/...u+UH.
 10f0 3d022f00 00004889 e5740c48 8b3d062f  =./...H..t.H.=./
 1100 0000e829 ffffffe8 64ffffff c605fd2e  ...)....d.......
 1110 0000015d c30f1f00 c30f1f80 00000000  ...]............
 1120 f30f1efa e977ffff fff30f1e fa554889  .....w.......UH.
 1130 e5b80000 00005dc3 0f1f8400 00000000  ......].........
 1140 f30f1efa 41574c8d 3da32c00 00415649  ....AWL.=.,..AVI
 1150 89d64155 4989f541 544189fc 55488d2d  ..AUI..ATA..UH.-
 1160 942c0000 534c29fd 4883ec08 e88ffeff  .,..SL).H.......
 1170 ff48c1fd 03741f31 db0f1f80 00000000  .H...t.1........
 1180 4c89f24c 89ee4489 e741ff14 df4883c3  L..L..D..A...H..
 1190 014839dd 75ea4883 c4085b5d 415c415d  .H9.u.H...[]A\A]
 11a0 415e415f c366662e 0f1f8400 00000000  A^A_.ff.........
 11b0 f30f1efa c3                          .....           
Contents of section .fini:
 11b8 f30f1efa 4883ec08 4883c408 c3        ....H...H....   
Contents of section .rodata:
 2000 01000200                             ....            
Contents of section .eh_frame_hdr:
 2004 011b033b 38000000 06000000 1cf0ffff  ...;8...........
 2014 6c000000 2cf0ffff 94000000 3cf0ffff  l...,.......<...
 2024 54000000 25f1ffff ac000000 3cf1ffff  T...%.......<...
 2034 cc000000 acf1ffff 14010000           ............    
Contents of section .eh_frame:
 2040 14000000 00000000 017a5200 01781001  .........zR..x..
 2050 1b0c0708 90010000 14000000 1c000000  ................
 2060 e0efffff 2f000000 00440710 00000000  ..../....D......
 2070 24000000 34000000 a8efffff 10000000  $...4...........
 2080 000e1046 0e184a0f 0b770880 003f1a3a  ...F..J..w...?.:
 2090 2a332422 00000000 14000000 5c000000  *3$"........\...
 20a0 90efffff 10000000 00000000 00000000  ................
 20b0 1c000000 74000000 71f0ffff 0f000000  ....t...q.......
 20c0 00450e10 8602430d 06460c07 08000000  .E....C..F......
 20d0 44000000 94000000 68f0ffff 65000000  D.......h...e...
 20e0 00460e10 8f02490e 188e0345 0e208d04  .F....I....E. ..
 20f0 450e288c 05440e30 8606480e 38830747  E.(..D.0..H.8..G
 2100 0e406e0e 38410e30 410e2842 0e20420e  .@n.8A.0A.(B. B.
 2110 18420e10 420e0800 10000000 dc000000  .B..B...........
 2120 90f0ffff 05000000 00000000 00000000  ................
Contents of section .init_array:
 3df0 20110000 00000000                     .......        
Contents of section .fini_array:
 3df8 e0100000 00000000                    ........        
Contents of section .dynamic:
 3e00 01000000 00000000 01000000 00000000  ................
 3e10 0c000000 00000000 00100000 00000000  ................
 3e20 0d000000 00000000 b8110000 00000000  ................
 3e30 19000000 00000000 f03d0000 00000000  .........=......
 3e40 1b000000 00000000 08000000 00000000  ................
 3e50 1a000000 00000000 f83d0000 00000000  .........=......
 3e60 1c000000 00000000 08000000 00000000  ................
 3e70 f5feff6f 00000000 a0030000 00000000  ...o............
 3e80 05000000 00000000 58040000 00000000  ........X.......
 3e90 06000000 00000000 c8030000 00000000  ................
 3ea0 0a000000 00000000 7d000000 00000000  ........}.......
 3eb0 0b000000 00000000 18000000 00000000  ................
 3ec0 15000000 00000000 00000000 00000000  ................
 3ed0 03000000 00000000 c03f0000 00000000  .........?......
 3ee0 07000000 00000000 08050000 00000000  ................
 3ef0 08000000 00000000 c0000000 00000000  ................
 3f00 09000000 00000000 18000000 00000000  ................
 3f10 1e000000 00000000 08000000 00000000  ................
 3f20 fbffff6f 00000000 01000008 00000000  ...o............
 3f30 feffff6f 00000000 e8040000 00000000  ...o............
 3f40 ffffff6f 00000000 01000000 00000000  ...o............
 3f50 f0ffff6f 00000000 d6040000 00000000  ...o............
 3f60 f9ffff6f 00000000 03000000 00000000  ...o............
 3f70 00000000 00000000 00000000 00000000  ................
 3f80 00000000 00000000 00000000 00000000  ................
 3f90 00000000 00000000 00000000 00000000  ................
 3fa0 00000000 00000000 00000000 00000000  ................
 3fb0 00000000 00000000 00000000 00000000  ................
Contents of section .got:
 3fc0 003e0000 00000000 00000000 00000000  .>..............
 3fd0 00000000 00000000 00000000 00000000  ................
 3fe0 00000000 00000000 00000000 00000000  ................
 3ff0 00000000 00000000 00000000 00000000  ................
Contents of section .data:
 4000 00000000 00000000 08400000 00000000  .........@......
Contents of section .comment:
 0000 4743433a 20285562 756e7475 20392e34  GCC: (Ubuntu 9.4
 0010 2e302d31 7562756e 7475317e 32302e30  .0-1ubuntu1~20.0
 0020 342e3129 20392e34 2e3000             4.1) 9.4.0.     

Disassembly of section .init:

0000000000001000 <_init>:
    1000:    f3 0f 1e fa              endbr64 
    1004:    48 83 ec 08              sub    $0x8,%rsp
    1008:    48 8b 05 d9 2f 00 00     mov    0x2fd9(%rip),%rax        # 3fe8 <__gmon_start__>
    100f:    48 85 c0                 test   %rax,%rax
    1012:    74 02                    je     1016 <_init+0x16>
    1014:    ff d0                    callq  *%rax
    1016:    48 83 c4 08              add    $0x8,%rsp
    101a:    c3                       retq   

Disassembly of section .plt:

0000000000001020 <.plt>:
    1020:    ff 35 a2 2f 00 00        pushq  0x2fa2(%rip)        # 3fc8 <_GLOBAL_OFFSET_TABLE_+0x8>
    1026:    f2 ff 25 a3 2f 00 00     bnd jmpq *0x2fa3(%rip)        # 3fd0 <_GLOBAL_OFFSET_TABLE_+0x10>
    102d:    0f 1f 00                 nopl   (%rax)

Disassembly of section .plt.got:

0000000000001030 <__cxa_finalize@plt>:
    1030:    f3 0f 1e fa              endbr64 
    1034:    f2 ff 25 bd 2f 00 00     bnd jmpq *0x2fbd(%rip)        # 3ff8 <__cxa_finalize@GLIBC_2.2.5>
    103b:    0f 1f 44 00 00           nopl   0x0(%rax,%rax,1)

Disassembly of section .text:

0000000000001040 <_start>:
    1040:    f3 0f 1e fa              endbr64 
    1044:    31 ed                    xor    %ebp,%ebp
    1046:    49 89 d1                 mov    %rdx,%r9
    1049:    5e                       pop    %rsi
    104a:    48 89 e2                 mov    %rsp,%rdx
    104d:    48 83 e4 f0              and    $0xfffffffffffffff0,%rsp
    1051:    50                       push   %rax
    1052:    54                       push   %rsp
    1053:    4c 8d 05 56 01 00 00     lea    0x156(%rip),%r8        # 11b0 <__libc_csu_fini>
    105a:    48 8d 0d df 00 00 00     lea    0xdf(%rip),%rcx        # 1140 <__libc_csu_init>
    1061:    48 8d 3d c1 00 00 00     lea    0xc1(%rip),%rdi        # 1129 <main>
    1068:    ff 15 72 2f 00 00        callq  *0x2f72(%rip)        # 3fe0 <__libc_start_main@GLIBC_2.2.5>
    106e:    f4                       hlt    
    106f:    90                       nop

0000000000001070 <deregister_tm_clones>:
    1070:    48 8d 3d 99 2f 00 00     lea    0x2f99(%rip),%rdi        # 4010 <__TMC_END__>
    1077:    48 8d 05 92 2f 00 00     lea    0x2f92(%rip),%rax        # 4010 <__TMC_END__>
    107e:    48 39 f8                 cmp    %rdi,%rax
    1081:    74 15                    je     1098 <deregister_tm_clones+0x28>
    1083:    48 8b 05 4e 2f 00 00     mov    0x2f4e(%rip),%rax        # 3fd8 <_ITM_deregisterTMCloneTable>
    108a:    48 85 c0                 test   %rax,%rax
    108d:    74 09                    je     1098 <deregister_tm_clones+0x28>
    108f:    ff e0                    jmpq   *%rax
    1091:    0f 1f 80 00 00 00 00     nopl   0x0(%rax)
    1098:    c3                       retq   
    1099:    0f 1f 80 00 00 00 00     nopl   0x0(%rax)

00000000000010a0 <register_tm_clones>:
    10a0:    48 8d 3d 69 2f 00 00     lea    0x2f69(%rip),%rdi        # 4010 <__TMC_END__>
    10a7:    48 8d 35 62 2f 00 00     lea    0x2f62(%rip),%rsi        # 4010 <__TMC_END__>
    10ae:    48 29 fe                 sub    %rdi,%rsi
    10b1:    48 89 f0                 mov    %rsi,%rax
    10b4:    48 c1 ee 3f              shr    $0x3f,%rsi
    10b8:    48 c1 f8 03              sar    $0x3,%rax
    10bc:    48 01 c6                 add    %rax,%rsi
    10bf:    48 d1 fe                 sar    %rsi
    10c2:    74 14                    je     10d8 <register_tm_clones+0x38>
    10c4:    48 8b 05 25 2f 00 00     mov    0x2f25(%rip),%rax        # 3ff0 <_ITM_registerTMCloneTable>
    10cb:    48 85 c0                 test   %rax,%rax
    10ce:    74 08                    je     10d8 <register_tm_clones+0x38>
    10d0:    ff e0                    jmpq   *%rax
    10d2:    66 0f 1f 44 00 00        nopw   0x0(%rax,%rax,1)
    10d8:    c3                       retq   
    10d9:    0f 1f 80 00 00 00 00     nopl   0x0(%rax)

00000000000010e0 <__do_global_dtors_aux>:
    10e0:    f3 0f 1e fa              endbr64 
    10e4:    80 3d 25 2f 00 00 00     cmpb   $0x0,0x2f25(%rip)        # 4010 <__TMC_END__>
    10eb:    75 2b                    jne    1118 <__do_global_dtors_aux+0x38>
    10ed:    55                       push   %rbp
    10ee:    48 83 3d 02 2f 00 00     cmpq   $0x0,0x2f02(%rip)        # 3ff8 <__cxa_finalize@GLIBC_2.2.5>
    10f5:    00 
    10f6:    48 89 e5                 mov    %rsp,%rbp
    10f9:    74 0c                    je     1107 <__do_global_dtors_aux+0x27>
    10fb:    48 8b 3d 06 2f 00 00     mov    0x2f06(%rip),%rdi        # 4008 <__dso_handle>
    1102:    e8 29 ff ff ff           callq  1030 <__cxa_finalize@plt>
    1107:    e8 64 ff ff ff           callq  1070 <deregister_tm_clones>
    110c:    c6 05 fd 2e 00 00 01     movb   $0x1,0x2efd(%rip)        # 4010 <__TMC_END__>
    1113:    5d                       pop    %rbp
    1114:    c3                       retq   
    1115:    0f 1f 00                 nopl   (%rax)
    1118:    c3                       retq   
    1119:    0f 1f 80 00 00 00 00     nopl   0x0(%rax)

0000000000001120 <frame_dummy>:
    1120:    f3 0f 1e fa              endbr64 
    1124:    e9 77 ff ff ff           jmpq   10a0 <register_tm_clones>

0000000000001129 <main>:
    1129:    f3 0f 1e fa              endbr64 
    112d:    55                       push   %rbp
    112e:    48 89 e5                 mov    %rsp,%rbp
    1131:    b8 00 00 00 00           mov    $0x0,%eax
    1136:    5d                       pop    %rbp
    1137:    c3                       retq   
    1138:    0f 1f 84 00 00 00 00     nopl   0x0(%rax,%rax,1)
    113f:    00 

0000000000001140 <__libc_csu_init>:
    1140:    f3 0f 1e fa              endbr64 
    1144:    41 57                    push   %r15
    1146:    4c 8d 3d a3 2c 00 00     lea    0x2ca3(%rip),%r15        # 3df0 <__frame_dummy_init_array_entry>
    114d:    41 56                    push   %r14
    114f:    49 89 d6                 mov    %rdx,%r14
    1152:    41 55                    push   %r13
    1154:    49 89 f5                 mov    %rsi,%r13
    1157:    41 54                    push   %r12
    1159:    41 89 fc                 mov    %edi,%r12d
    115c:    55                       push   %rbp
    115d:    48 8d 2d 94 2c 00 00     lea    0x2c94(%rip),%rbp        # 3df8 <__do_global_dtors_aux_fini_array_entry>
    1164:    53                       push   %rbx
    1165:    4c 29 fd                 sub    %r15,%rbp
    1168:    48 83 ec 08              sub    $0x8,%rsp
    116c:    e8 8f fe ff ff           callq  1000 <_init>
    1171:    48 c1 fd 03              sar    $0x3,%rbp
    1175:    74 1f                    je     1196 <__libc_csu_init+0x56>
    1177:    31 db                    xor    %ebx,%ebx
    1179:    0f 1f 80 00 00 00 00     nopl   0x0(%rax)
    1180:    4c 89 f2                 mov    %r14,%rdx
    1183:    4c 89 ee                 mov    %r13,%rsi
    1186:    44 89 e7                 mov    %r12d,%edi
    1189:    41 ff 14 df              callq  *(%r15,%rbx,8)
    118d:    48 83 c3 01              add    $0x1,%rbx
    1191:    48 39 dd                 cmp    %rbx,%rbp
    1194:    75 ea                    jne    1180 <__libc_csu_init+0x40>
    1196:    48 83 c4 08              add    $0x8,%rsp
    119a:    5b                       pop    %rbx
    119b:    5d                       pop    %rbp
    119c:    41 5c                    pop    %r12
    119e:    41 5d                    pop    %r13
    11a0:    41 5e                    pop    %r14
    11a2:    41 5f                    pop    %r15
    11a4:    c3                       retq   
    11a5:    66 66 2e 0f 1f 84 00     data16 nopw %cs:0x0(%rax,%rax,1)
    11ac:    00 00 00 00 

00000000000011b0 <__libc_csu_fini>:
    11b0:    f3 0f 1e fa              endbr64 
    11b4:    c3                       retq   

Disassembly of section .fini:

00000000000011b8 <_fini>:
    11b8:    f3 0f 1e fa              endbr64 
    11bc:    48 83 ec 08              sub    $0x8,%rsp
    11c0:    48 83 c4 08              add    $0x8,%rsp
    11c4:    c3                       retq   

标签: os

添加新评论