next up previous contents
Next: strip 工具 Up: Strip 程式執行檔瘦身 Previous: 執行檔製作   Contents

執行檔瘦身

  1. 讀取 ELF(Executable and Linkable Format) 格式的執行檔 stripex1,顯示此執行檔包含原始的 75 個符號項目。
    [dywang@dyH gcc]$ readelf -s stripex1
    

    # readelf -s stripex1
    
    Symbol table '.dynsym' contains 4 entries:
       Num:    Value          Size Type    Bind   Vis      Ndx Name
         0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
         1: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND printf@GLIBC_2.2.5 (2)
         2: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
         3: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@GLIBC_2.2.5 (2)
    
    Symbol table '.symtab' contains 75 entries:
       Num:    Value          Size Type    Bind   Vis      Ndx Name
         0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
         1: 0000000000400200     0 SECTION LOCAL  DEFAULT    1 
         2: 000000000040021c     0 SECTION LOCAL  DEFAULT    2 
         3: 000000000040023c     0 SECTION LOCAL  DEFAULT    3 
         4: 0000000000400260     0 SECTION LOCAL  DEFAULT    4 
         5: 0000000000400280     0 SECTION LOCAL  DEFAULT    5 
         6: 00000000004002e0     0 SECTION LOCAL  DEFAULT    6 
         7: 0000000000400320     0 SECTION LOCAL  DEFAULT    7 
         8: 0000000000400328     0 SECTION LOCAL  DEFAULT    8 
         9: 0000000000400348     0 SECTION LOCAL  DEFAULT    9 
        10: 0000000000400360     0 SECTION LOCAL  DEFAULT   10 
        11: 0000000000400390     0 SECTION LOCAL  DEFAULT   11 
        12: 00000000004003a8     0 SECTION LOCAL  DEFAULT   12 
        13: 00000000004003e0     0 SECTION LOCAL  DEFAULT   13 
        14: 0000000000400608     0 SECTION LOCAL  DEFAULT   14 
        15: 0000000000400618     0 SECTION LOCAL  DEFAULT   15 
        16: 0000000000400634     0 SECTION LOCAL  DEFAULT   16 
        17: 0000000000400660     0 SECTION LOCAL  DEFAULT   17 
        18: 0000000000600700     0 SECTION LOCAL  DEFAULT   18 
        19: 0000000000600710     0 SECTION LOCAL  DEFAULT   19 
        20: 0000000000600720     0 SECTION LOCAL  DEFAULT   20 
        21: 0000000000600728     0 SECTION LOCAL  DEFAULT   21 
        22: 00000000006008b8     0 SECTION LOCAL  DEFAULT   22 
        23: 00000000006008c0     0 SECTION LOCAL  DEFAULT   23 
        24: 00000000006008e8     0 SECTION LOCAL  DEFAULT   24 
        25: 00000000006008f8     0 SECTION LOCAL  DEFAULT   25 
        26: 0000000000000000     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 SECTION LOCAL  DEFAULT   33 
        34: 000000000040040c     0 FUNC    LOCAL  DEFAULT   13 call_gmon_start
        35: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
        36: 0000000000600700     0 OBJECT  LOCAL  DEFAULT   18 __CTOR_LIST__
        37: 0000000000600710     0 OBJECT  LOCAL  DEFAULT   19 __DTOR_LIST__
        38: 0000000000600720     0 OBJECT  LOCAL  DEFAULT   20 __JCR_LIST__
        39: 0000000000400430     0 FUNC    LOCAL  DEFAULT   13 __do_global_dtors_aux
        40: 00000000006008f8     1 OBJECT  LOCAL  DEFAULT   25 completed.6349
        41: 0000000000600900     8 OBJECT  LOCAL  DEFAULT   25 dtor_idx.6351
        42: 00000000004004a0     0 FUNC    LOCAL  DEFAULT   13 frame_dummy
        43: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
        44: 0000000000600708     0 OBJECT  LOCAL  DEFAULT   18 __CTOR_END__
        45: 00000000004006f8     0 OBJECT  LOCAL  DEFAULT   17 __FRAME_END__
        46: 0000000000600720     0 OBJECT  LOCAL  DEFAULT   20 __JCR_END__
        47: 00000000004005d0     0 FUNC    LOCAL  DEFAULT   13 __do_global_ctors_aux
        48: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS stripex1.c
        49: 00000000006008ec     4 OBJECT  LOCAL  DEFAULT   24 i
        50: 0000000000600908     4 OBJECT  LOCAL  DEFAULT   25 local.2052
        51: 00000000006008c0     0 OBJECT  LOCAL  DEFAULT   23 _GLOBAL_OFFSET_TABLE_
        52: 00000000006006fc     0 NOTYPE  LOCAL  DEFAULT   18 __init_array_end
        53: 00000000006006fc     0 NOTYPE  LOCAL  DEFAULT   18 __init_array_start
        54: 0000000000600728     0 OBJECT  LOCAL  DEFAULT   21 _DYNAMIC
        55: 00000000006008e8     0 NOTYPE  WEAK   DEFAULT   24 data_start
        56: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND printf@@GLIBC_2.2.5
        57: 0000000000400530     2 FUNC    GLOBAL DEFAULT   13 __libc_csu_fini
        58: 00000000004003e0     0 FUNC    GLOBAL DEFAULT   13 _start
        59: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
        60: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _Jv_RegisterClasses
        61: 0000000000400608     0 FUNC    GLOBAL DEFAULT   14 _fini
        62: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@@GLIBC_
        63: 00000000006008f0     4 OBJECT  GLOBAL DEFAULT   24 global
        64: 0000000000400618     4 OBJECT  GLOBAL DEFAULT   15 _IO_stdin_used
        65: 00000000006008e8     0 NOTYPE  GLOBAL DEFAULT   24 __data_start
        66: 0000000000400620     0 OBJECT  GLOBAL HIDDEN    15 __dso_handle
        67: 0000000000600718     0 OBJECT  GLOBAL HIDDEN    19 __DTOR_END__
        68: 0000000000400540   137 FUNC    GLOBAL DEFAULT   13 __libc_csu_init
        69: 00000000006008f4     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_start
        70: 0000000000600910     0 NOTYPE  GLOBAL DEFAULT  ABS _end
        71: 00000000006008f4     0 NOTYPE  GLOBAL DEFAULT  ABS _edata
        72: 00000000004004c4    27 FUNC    GLOBAL DEFAULT   13 inc_func
        73: 00000000004004df    67 FUNC    GLOBAL DEFAULT   13 main
        74: 0000000000400390     0 FUNC    GLOBAL DEFAULT   11 _init
    
  2. 以 strip 工具修整執行檔
    ng@dyH gcc]$ strip -s stripex1
    
  3. 修整後的執行檔,原始的符號項目已刪除。
    [dywang@dyH gcc]$ readelf -s stripex1
    
    Symbol table '.dynsym' contains 4 entries:
       Num:    Value          Size Type    Bind   Vis      Ndx Name
         0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
         1: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND printf@GLIBC_2.2.5 (2)
         2: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
         3: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@GLIBC_2.2.5 (2)
    
  4. 再看檔案大小,已從 8195 bytes 降至 4368 bytes。
    [dywang@dyH gcc]$ ll stripex1*
    -rwxrwxr-x. 1 dywang dywang 4368 Sep  1 06:41 stripex1
    -rw-rw-r--. 1 dywang dywang  323 Aug 31 22:19 stripex1.c
    



2015-01-16