1. 程式人生 > >elf 檔案初識-變數儲存

elf 檔案初識-變數儲存

收藏可參考的文章:https://www.cnblogs.com/LiuYanYGZ/p/5574602.html

 

 .data 初始化過的全域性變數 (含初始化靜態變數)
.bss 未初始化的全域性變數(包括 函式內外的未初始化的靜態變數)
.rodata 只讀的變數(const)

1)關於常量 和 普通變數
int*  const globle_A = 10; // .rodata
const int* globle_B = 10; // .data
2)關於初始化和非初始化

#include <stdio.h>

int global_dat1[30000]; //儲存在bss段,由系統初始化為全 0,不需要儲存初始化值
int global_dat1[30000]={0,}; //儲存在bss段,由系統初始化為全 0,不需要儲存初始化值  //是否是有優化的成分待繼續追一下


int global_dat1[30000]={1,2,3,4,}; //儲存在data 段,初始化的大陣列要被儲存,所以編譯出來的檔案會大很多!

int main(void)
{
    return 0;
}

3)具體例子

編譯 gcc para.c -g
然後讀出 readelf -a a.out (readelf -s)
 

#include <stdio.h>

const int globle_A = 10; // .rodata  read only data 4006b8(start addr:4006b0 len:3a)

int globle_a = 20; // .data globle data 601040 (start addr:r601030 len:24)
int globle_b = 21; // .data 601044
static int static_c = 30; // .data 601048
static int static_d = 31; // .data 60104c

int c;  // .bss  uninit data area 60105c(start:601054)


void print_globle(void)
{
	printf(" %s: globle_a = %d \n", __FUNCTION__, globle_a );
}
int main(void)
{
static int static_local_e = 40; // .data 601050
static int static_local_f;  // .data 601058

int local_h = 70;

char globle_b[] = "Hello world";
int globle_a = 90;
register int c = 50;

printf(" %s: globle_a = %d \n", __FUNCTION__, globle_a );
//printf("Hello 1 %d\n", sizeof(A));
print_globle();

return 0;
}

 

ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1
  Entry point address:               0x4004a0
  Start of program headers:          64 (bytes into file)
  Start of section headers:          8408 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         9
  Size of section headers:           64 (bytes)
  Number of section headers:         36
  Section header string table index: 33

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000
       0000000000000000  0000000000000000           0     0     0
  [ 1] .interp           PROGBITS         0000000000400238  00000238
       000000000000001c  0000000000000000   A       0     0     1
  [ 2] .note.ABI-tag     NOTE             0000000000400254  00000254
       0000000000000020  0000000000000000   A       0     0     4
  [ 3] .note.gnu.build-i NOTE             0000000000400274  00000274
       0000000000000024  0000000000000000   A       0     0     4
  [ 4] .gnu.hash         GNU_HASH         0000000000400298  00000298
       000000000000001c  0000000000000000   A       5     0     8
  [ 5] .dynsym           DYNSYM           00000000004002b8  000002b8
       0000000000000078  0000000000000018   A       6     1     8
  [ 6] .dynstr           STRTAB           0000000000400330  00000330
       000000000000005a  0000000000000000   A       0     0     1
  [ 7] .gnu.version      VERSYM           000000000040038a  0000038a
       000000000000000a  0000000000000002   A       5     0     2
  [ 8] .gnu.version_r    VERNEED          0000000000400398  00000398
       0000000000000030  0000000000000000   A       6     1     8
  [ 9] .rela.dyn         RELA             00000000004003c8  000003c8
       0000000000000018  0000000000000018   A       5     0     8
  [10] .rela.plt         RELA             00000000004003e0  000003e0
       0000000000000048  0000000000000018  AI       5    24     8
  [11] .init             PROGBITS         0000000000400428  00000428
       000000000000001a  0000000000000000  AX       0     0     4
  [12] .plt              PROGBITS         0000000000400450  00000450
       0000000000000040  0000000000000010  AX       0     0     16
  [13] .plt.got          PROGBITS         0000000000400490  00000490
       0000000000000008  0000000000000000  AX       0     0     8
  [14] .text             PROGBITS         00000000004004a0  000004a0
       0000000000000202  0000000000000000  AX       0     0     16
  [15] .fini             PROGBITS         00000000004006a4  000006a4
       0000000000000009  0000000000000000  AX       0     0     4
  [16] .rodata           PROGBITS         00000000004006b0  000006b0
       000000000000003a  0000000000000000   A       0     0     8

  [17] .eh_frame_hdr     PROGBITS         00000000004006ec  000006ec
       000000000000003c  0000000000000000   A       0     0     4
  [18] .eh_frame         PROGBITS         0000000000400728  00000728
       0000000000000114  0000000000000000   A       0     0     8
  [19] .init_array       INIT_ARRAY       0000000000600e10  00000e10
       0000000000000008  0000000000000000  WA       0     0     8
  [20] .fini_array       FINI_ARRAY       0000000000600e18  00000e18
       0000000000000008  0000000000000000  WA       0     0     8
  [21] .jcr              PROGBITS         0000000000600e20  00000e20
       0000000000000008  0000000000000000  WA       0     0     8
  [22] .dynamic          DYNAMIC          0000000000600e28  00000e28
       00000000000001d0  0000000000000010  WA       6     0     8
  [23] .got              PROGBITS         0000000000600ff8  00000ff8
       0000000000000008  0000000000000008  WA       0     0     8
  [24] .got.plt          PROGBITS         0000000000601000  00001000
       0000000000000030  0000000000000008  WA       0     0     8
  [25] .data             PROGBITS         0000000000601030  00001030
       0000000000000024  0000000000000000  WA       0     0     8

  [26] .bss              NOBITS           0000000000601054  00001054
       000000000000000c  0000000000000000  WA       0     0     4

  [27] .comment          PROGBITS         0000000000000000  00001054
       0000000000000035  0000000000000001  MS       0     0     1
  [28] .debug_aranges    PROGBITS         0000000000000000  00001089
       0000000000000030  0000000000000000           0     0     1
  [29] .debug_info       PROGBITS         0000000000000000  000010b9
       00000000000001f7  0000000000000000           0     0     1
  [30] .debug_abbrev     PROGBITS         0000000000000000  000012b0
       00000000000000c4  0000000000000000           0     0     1
  [31] .debug_line       PROGBITS         0000000000000000  00001374
       0000000000000048  0000000000000000           0     0     1
  [32] .debug_str        PROGBITS         0000000000000000  000013bc
       0000000000000130  0000000000000001  MS       0     0     1
  [33] .shstrtab         STRTAB           0000000000000000  00001f87
       000000000000014c  0000000000000000           0     0     1
  [34] .symtab           SYMTAB           0000000000000000  000014f0
       00000000000007e0  0000000000000018          35    58     8
  [35] .strtab           STRTAB           0000000000000000  00001cd0
       00000000000002b7  0000000000000000           0     0     1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), l (large)
  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)

There are no section groups in this file.

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  PHDR           0x0000000000000040 0x0000000000400040 0x0000000000400040
                 0x00000000000001f8 0x00000000000001f8  R E    8
  INTERP         0x0000000000000238 0x0000000000400238 0x0000000000400238
                 0x000000000000001c 0x000000000000001c  R      1
      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
                 0x000000000000083c 0x000000000000083c  R E    200000
  LOAD           0x0000000000000e10 0x0000000000600e10 0x0000000000600e10
                 0x0000000000000244 0x0000000000000250  RW     200000
  DYNAMIC        0x0000000000000e28 0x0000000000600e28 0x0000000000600e28
                 0x00000000000001d0 0x00000000000001d0  RW     8
  NOTE           0x0000000000000254 0x0000000000400254 0x0000000000400254
                 0x0000000000000044 0x0000000000000044  R      4
  GNU_EH_FRAME   0x00000000000006ec 0x00000000004006ec 0x00000000004006ec
                 0x000000000000003c 0x000000000000003c  R      4
  GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x0000000000000000 0x0000000000000000  RW     10
  GNU_RELRO      0x0000000000000e10 0x0000000000600e10 0x0000000000600e10
                 0x00000000000001f0 0x00000000000001f0  R      1

 Section to Segment mapping:
  Segment Sections...
   00     
   01     .interp
   02     .interp .note.ABI-tag .note.gnu.build-id .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn .rela.plt .init .plt .plt.got .text .fini .rodata .eh_frame_hdr .eh_frame
   03     .init_array .fini_array .jcr .dynamic .got .got.plt .data .bss
   04     .dynamic
   05     .note.ABI-tag .note.gnu.build-id
   06     .eh_frame_hdr
   07     
   08     .init_array .fini_array .jcr .dynamic .got

Dynamic section at offset 0xe28 contains 24 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x000000000000000c (INIT)               0x400428
 0x000000000000000d (FINI)               0x4006a4
 0x0000000000000019 (INIT_ARRAY)         0x600e10
 0x000000000000001b (INIT_ARRAYSZ)       8 (bytes)
 0x000000000000001a (FINI_ARRAY)         0x600e18
 0x000000000000001c (FINI_ARRAYSZ)       8 (bytes)
 0x000000006ffffef5 (GNU_HASH)           0x400298
 0x0000000000000005 (STRTAB)             0x400330
 0x0000000000000006 (SYMTAB)             0x4002b8
 0x000000000000000a (STRSZ)              90 (bytes)
 0x000000000000000b (SYMENT)             24 (bytes)
 0x0000000000000015 (DEBUG)              0x0
 0x0000000000000003 (PLTGOT)             0x601000
 0x0000000000000002 (PLTRELSZ)           72 (bytes)
 0x0000000000000014 (PLTREL)             RELA
 0x0000000000000017 (JMPREL)             0x4003e0
 0x0000000000000007 (RELA)               0x4003c8
 0x0000000000000008 (RELASZ)             24 (bytes)
 0x0000000000000009 (RELAENT)            24 (bytes)
 0x000000006ffffffe (VERNEED)            0x400398
 0x000000006fffffff (VERNEEDNUM)         1
 0x000000006ffffff0 (VERSYM)             0x40038a
 0x0000000000000000 (NULL)               0x0

Relocation section '.rela.dyn' at offset 0x3c8 contains 1 entries:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
000000600ff8  000400000006 R_X86_64_GLOB_DAT 0000000000000000 __gmon_start__ + 0

Relocation section '.rela.plt' at offset 0x3e0 contains 3 entries:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
000000601018  000100000007 R_X86_64_JUMP_SLO 0000000000000000 [email protected]_2.4 + 0
000000601020  000200000007 R_X86_64_JUMP_SLO 0000000000000000 [email protected]_2.2.5 + 0
000000601028  000300000007 R_X86_64_JUMP_SLO 0000000000000000 [email protected]_2.2.5 + 0

The decoding of unwind sections for machine type Advanced Micro Devices X86-64 is not currently supported.

Symbol table '.dynsym' contains 5 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND [email protected]_2.4 (2)
     2: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND [email protected]_2.2.5 (3)
     3: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND [email protected]_2.2.5 (3)
     4: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__

Symbol table '.symtab' contains 84 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000400238     0 SECTION LOCAL  DEFAULT    1
     2: 0000000000400254     0 SECTION LOCAL  DEFAULT    2
     3: 0000000000400274     0 SECTION LOCAL  DEFAULT    3
     4: 0000000000400298     0 SECTION LOCAL  DEFAULT    4
     5: 00000000004002b8     0 SECTION LOCAL  DEFAULT    5
     6: 0000000000400330     0 SECTION LOCAL  DEFAULT    6
     7: 000000000040038a     0 SECTION LOCAL  DEFAULT    7
     8: 0000000000400398     0 SECTION LOCAL  DEFAULT    8
     9: 00000000004003c8     0 SECTION LOCAL  DEFAULT    9
    10: 00000000004003e0     0 SECTION LOCAL  DEFAULT   10
    11: 0000000000400428     0 SECTION LOCAL  DEFAULT   11
    12: 0000000000400450     0 SECTION LOCAL  DEFAULT   12
    13: 0000000000400490     0 SECTION LOCAL  DEFAULT   13
    14: 00000000004004a0     0 SECTION LOCAL  DEFAULT   14
    15: 00000000004006a4     0 SECTION LOCAL  DEFAULT   15
    16: 00000000004006b0     0 SECTION LOCAL  DEFAULT   16
    17: 00000000004006ec     0 SECTION LOCAL  DEFAULT   17
    18: 0000000000400728     0 SECTION LOCAL  DEFAULT   18
    19: 0000000000600e10     0 SECTION LOCAL  DEFAULT   19
    20: 0000000000600e18     0 SECTION LOCAL  DEFAULT   20
    21: 0000000000600e20     0 SECTION LOCAL  DEFAULT   21
    22: 0000000000600e28     0 SECTION LOCAL  DEFAULT   22
    23: 0000000000600ff8     0 SECTION LOCAL  DEFAULT   23
    24: 0000000000601000     0 SECTION LOCAL  DEFAULT   24
    25: 0000000000601030     0 SECTION LOCAL  DEFAULT   25
    26: 0000000000601054     0 SECTION LOCAL  DEFAULT   26
    27: 0000000000000000     0 SECTION LOCAL  DEFAULT   27
    28: 0000000000000000     0 SECTION LOCAL  DEFAULT   28
    29: 0000000000000000     0 SECTION LOCAL  DEFAULT   29
    30: 0000000000000000     0 SECTION LOCAL  DEFAULT   30
    31: 0000000000000000     0 SECTION LOCAL  DEFAULT   31
    32: 0000000000000000     0 SECTION LOCAL  DEFAULT   32
    33: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
    34: 0000000000600e20     0 OBJECT  LOCAL  DEFAULT   21 __JCR_LIST__
    35: 00000000004004d0     0 FUNC    LOCAL  DEFAULT   14 deregister_tm_clones
    36: 0000000000400510     0 FUNC    LOCAL  DEFAULT   14 register_tm_clones
    37: 0000000000400550     0 FUNC    LOCAL  DEFAULT   14 __do_global_dtors_aux
    38: 0000000000601054     1 OBJECT  LOCAL  DEFAULT   26 completed.7594
    39: 0000000000600e18     0 OBJECT  LOCAL  DEFAULT   20 __do_global_dtors_aux_fin
    40: 0000000000400570     0 FUNC    LOCAL  DEFAULT   14 frame_dummy
    41: 0000000000600e10     0 OBJECT  LOCAL  DEFAULT   19 __frame_dummy_init_array_
    42: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS para.c
    43: 0000000000601048     4 OBJECT  LOCAL  DEFAULT   25 static_c
    44: 000000000060104c     4 OBJECT  LOCAL  DEFAULT   25 static_d

    45: 00000000004006d8    13 OBJECT  LOCAL  DEFAULT   16 __FUNCTION__.2292
    46: 00000000004006e5     5 OBJECT  LOCAL  DEFAULT   16 __FUNCTION__.2302
    47: 0000000000601058     4 OBJECT  LOCAL  DEFAULT   26 static_local_f.2297
    48: 0000000000601050     4 OBJECT  LOCAL  DEFAULT   25 static_local_e.2296

    49: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
    50: 0000000000400838     0 OBJECT  LOCAL  DEFAULT   18 __FRAME_END__
    51: 0000000000600e20     0 OBJECT  LOCAL  DEFAULT   21 __JCR_END__
    52: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS
    53: 0000000000600e18     0 NOTYPE  LOCAL  DEFAULT   19 __init_array_end
    54: 0000000000600e28     0 OBJECT  LOCAL  DEFAULT   22 _DYNAMIC
    55: 0000000000600e10     0 NOTYPE  LOCAL  DEFAULT   19 __init_array_start
    56: 00000000004006ec     0 NOTYPE  LOCAL  DEFAULT   17 __GNU_EH_FRAME_HDR
    57: 0000000000601000     0 OBJECT  LOCAL  DEFAULT   24 _GLOBAL_OFFSET_TABLE_
    58: 00000000004006a0     2 FUNC    GLOBAL DEFAULT   14 __libc_csu_fini
    59: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_deregisterTMCloneTab
    60: 0000000000601030     0 NOTYPE  WEAK   DEFAULT   25 data_start
    61: 0000000000601054     0 NOTYPE  GLOBAL DEFAULT   25 _edata
    62: 00000000004006a4     0 FUNC    GLOBAL DEFAULT   15 _fini
    63: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND [email protected]@GLIBC_2
    64: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND [email protected]@GLIBC_2.2.5
    65: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND [email protected]@GLIBC_
    66: 0000000000601030     0 NOTYPE  GLOBAL DEFAULT   25 __data_start
    67: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
    68: 0000000000601040     4 OBJECT  GLOBAL DEFAULT   25 globle_a
    69: 0000000000601038     0 OBJECT  GLOBAL HIDDEN    25 __dso_handle
    70: 00000000004006b0     4 OBJECT  GLOBAL DEFAULT   16 _IO_stdin_used
    71: 00000000004006b8     4 OBJECT  GLOBAL DEFAULT   16 globle_A
    72: 0000000000400630   101 FUNC    GLOBAL DEFAULT   14 __libc_csu_init
    73: 0000000000601060     0 NOTYPE  GLOBAL DEFAULT   26 _end
    74: 00000000004004a0    42 FUNC    GLOBAL DEFAULT   14 _start
    75: 000000000060105c     4 OBJECT  GLOBAL DEFAULT   26 c
    76: 0000000000601054     0 NOTYPE  GLOBAL DEFAULT   26 __bss_start
    77: 00000000004005b9   115 FUNC    GLOBAL DEFAULT   14 main
    78: 0000000000400596    35 FUNC    GLOBAL DEFAULT   14 print_globle
    79: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _Jv_RegisterClasses
    80: 0000000000601058     0 OBJECT  GLOBAL HIDDEN    25 __TMC_END__
    81: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_registerTMCloneTable
    82: 0000000000400428     0 FUNC    GLOBAL DEFAULT   11 _init
    83: 0000000000601044     4 OBJECT  GLOBAL DEFAULT   25 globle_b

Version symbols section '.gnu.version' contains 5 entries:
 Addr: 000000000040038a  Offset: 0x00038a  Link: 5 (.dynsym)
  000:   0 (*local*)       2 (GLIBC_2.4)     3 (GLIBC_2.2.5)   3 (GLIBC_2.2.5)
  004:   0 (*local*)    

Version needs section '.gnu.version_r' contains 1 entries:
 Addr: 0x0000000000400398  Offset: 0x000398  Link: 6 (.dynstr)
  000000: Version: 1  File: libc.so.6  Cnt: 2
  0x0010:   Name: GLIBC_2.2.5  Flags: none  Version: 3
  0x0020:   Name: GLIBC_2.4  Flags: none  Version: 2

Displaying notes found at file offset 0x00000254 with length 0x00000020:
  Owner                 Data size       Description
  GNU                  0x00000010       NT_GNU_ABI_TAG (ABI version tag)
    OS: Linux, ABI: 2.6.32

Displaying notes found at file offset 0x00000274 with length 0x00000024:
  Owner                 Data size       Description
  GNU                  0x00000014       NT_GNU_BUILD_ID (unique build ID bitstring)
    Build ID: 55ab69f6327e884925a448bcf65acbaa59cc07a9