天天看點

Linux 可執行檔案瘦身指令 strip 使用示例

作者:碼農看看

在 Linux 系統下開發軟體,輸出的可執行檔案可大可小,運作環境如果是在伺服器那麼可能資源比較充足,但如果是在嵌入式環境,那麼存儲資源是寸土必争的。是以會有對可執行檔案進行瘦身的需求,比如使用指令 strip。

關于 strip 的操作,有人戲稱是“脫衣服”,我這裡的描述不會這麼赤裸裸,改成“瘦身”。

在 man 手冊裡,strip 的說明是:

discard symbols from object files

意思就是把檔案内的符号資訊移除,這樣檔案的大小就降下來了,同時不影響可執行檔案被運作。另外,把可執行檔案内的部分資訊移除有助于防止被逆向工程破解。

strip 指令格式如下:

strip [options] filename ...

下面我通過幾個例子來說明如何使用這個指令:

巧婦難為無米之炊,是以需要先準備一個可執行檔案 test,生成可執行檔案的源檔案 test.cpp 如下:

#include<stdio.h>

class test{
};

int main(void)
{
    printf("sizeof(class test)=%d\n", (int)sizeof(test));
    return 0;
}
           

移除所有符号資訊

先來看看可執行檔案的内部符号資訊,通過指令 readelf 讀取

$ readelf -s test 
           
Symbol table '.dynsym' contains 7 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_deregisterTMCloneTab
     2: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND printf@GLIBC_2.2.5 (2)
     3: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@GLIBC_2.2.5 (2)
     4: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
     5: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_registerTMCloneTable
     6: 0000000000000000     0 FUNC    WEAK   DEFAULT  UND __cxa_finalize@GLIBC_2.2.5 (2)

Symbol table '.symtab' contains 63 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000238     0 SECTION LOCAL  DEFAULT    1 
     2: 0000000000000254     0 SECTION LOCAL  DEFAULT    2 
     3: 0000000000000274     0 SECTION LOCAL  DEFAULT    3 
     4: 0000000000000298     0 SECTION LOCAL  DEFAULT    4 
     5: 00000000000002b8     0 SECTION LOCAL  DEFAULT    5 
     6: 0000000000000360     0 SECTION LOCAL  DEFAULT    6 
     7: 00000000000003e4     0 SECTION LOCAL  DEFAULT    7 
     8: 00000000000003f8     0 SECTION LOCAL  DEFAULT    8 
     9: 0000000000000418     0 SECTION LOCAL  DEFAULT    9 
    10: 00000000000004d8     0 SECTION LOCAL  DEFAULT   10 
    11: 00000000000004f0     0 SECTION LOCAL  DEFAULT   11 
    12: 0000000000000510     0 SECTION LOCAL  DEFAULT   12 
    13: 0000000000000530     0 SECTION LOCAL  DEFAULT   13 
    14: 0000000000000540     0 SECTION LOCAL  DEFAULT   14 
    15: 00000000000006e4     0 SECTION LOCAL  DEFAULT   15 
    16: 00000000000006f0     0 SECTION LOCAL  DEFAULT   16 
    17: 000000000000070c     0 SECTION LOCAL  DEFAULT   17 
    18: 0000000000000748     0 SECTION LOCAL  DEFAULT   18 
    19: 0000000000200db8     0 SECTION LOCAL  DEFAULT   19 
    20: 0000000000200dc0     0 SECTION LOCAL  DEFAULT   20 
    21: 0000000000200dc8     0 SECTION LOCAL  DEFAULT   21 
    22: 0000000000200fb8     0 SECTION LOCAL  DEFAULT   22 
    23: 0000000000201000     0 SECTION LOCAL  DEFAULT   23 
    24: 0000000000201010     0 SECTION LOCAL  DEFAULT   24 
    25: 0000000000000000     0 SECTION LOCAL  DEFAULT   25 
    26: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
    27: 0000000000000570     0 FUNC    LOCAL  DEFAULT   14 deregister_tm_clones
    28: 00000000000005b0     0 FUNC    LOCAL  DEFAULT   14 register_tm_clones
    29: 0000000000000600     0 FUNC    LOCAL  DEFAULT   14 __do_global_dtors_aux
    30: 0000000000201010     1 OBJECT  LOCAL  DEFAULT   24 completed.7698
    31: 0000000000200dc0     0 OBJECT  LOCAL  DEFAULT   20 __do_global_dtors_aux_fin
    32: 0000000000000640     0 FUNC    LOCAL  DEFAULT   14 frame_dummy
    33: 0000000000200db8     0 OBJECT  LOCAL  DEFAULT   19 __frame_dummy_init_array_
    34: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS test.cpp
    35: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
    36: 000000000000084c     0 OBJECT  LOCAL  DEFAULT   18 __FRAME_END__
    37: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS 
    38: 0000000000200dc0     0 NOTYPE  LOCAL  DEFAULT   19 __init_array_end
    39: 0000000000200dc8     0 OBJECT  LOCAL  DEFAULT   21 _DYNAMIC
    40: 0000000000200db8     0 NOTYPE  LOCAL  DEFAULT   19 __init_array_start
    41: 000000000000070c     0 NOTYPE  LOCAL  DEFAULT   17 __GNU_EH_FRAME_HDR
    42: 0000000000200fb8     0 OBJECT  LOCAL  DEFAULT   22 _GLOBAL_OFFSET_TABLE_
    43: 00000000000006e0     2 FUNC    GLOBAL DEFAULT   14 __libc_csu_fini
    44: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_deregisterTMCloneTab
    45: 0000000000201000     0 NOTYPE  WEAK   DEFAULT   23 data_start
    46: 0000000000201010     0 NOTYPE  GLOBAL DEFAULT   23 _edata
    47: 00000000000006e4     0 FUNC    GLOBAL DEFAULT   15 _fini
    48: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND printf@@GLIBC_2.2.5
    49: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@@GLIBC_
    50: 0000000000201000     0 NOTYPE  GLOBAL DEFAULT   23 __data_start
    51: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
    52: 0000000000201008     0 OBJECT  GLOBAL HIDDEN    23 __dso_handle
    53: 00000000000006f0     4 OBJECT  GLOBAL DEFAULT   16 _IO_stdin_used
    54: 0000000000000670   101 FUNC    GLOBAL DEFAULT   14 __libc_csu_init
    55: 0000000000201018     0 NOTYPE  GLOBAL DEFAULT   24 _end
    56: 0000000000000540    43 FUNC    GLOBAL DEFAULT   14 _start
    57: 0000000000201010     0 NOTYPE  GLOBAL DEFAULT   24 __bss_start
    58: 000000000000064a    33 FUNC    GLOBAL DEFAULT   14 main
    59: 0000000000201010     0 OBJECT  GLOBAL HIDDEN    23 __TMC_END__
    60: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_registerTMCloneTable
    61: 0000000000000000     0 FUNC    WEAK   DEFAULT  UND __cxa_finalize@@GLIBC_2.2
    62: 00000000000004f0     0 FUNC    GLOBAL DEFAULT   11 _init
           

用 strip 加上 -s 把所有符号資訊移除,然後再次讀取檔案資訊看看

$ strip -s test
$ readelf -s test
           
Symbol table '.dynsym' contains 7 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_deregisterTMCloneTab
     2: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND printf@GLIBC_2.2.5 (2)
     3: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@GLIBC_2.2.5 (2)
     4: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
     5: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_registerTMCloneTable
     6: 0000000000000000     0 FUNC    WEAK   DEFAULT  UND __cxa_finalize@GLIBC_2.2.5 (2)
           

從上面的輸出可以看到檔案内的符号資訊已被移除。但是 .dynsym 和 .dynstr 表資訊還是存在的,這兩個表資訊是用于動态連結庫連結使用,是以執行 strip -s 後,不影響動态連結庫的使用。

移除調試資訊

如果僅僅需要移除調試資訊,那麼可以使用選項 --strip-debug

strip --strip-debug test
           

然後對比一下讀出來的符号資訊,發現瘦身後檔案資訊缺失了下面的内容

...
    26: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
    ...
    34: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS test.cpp
    35: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
    ...
           

移除特定的字段資訊

有些情況下,我們需要對特定的字段頭資訊移除,那麼可以使用選項 -R

先來讀取一下未經瘦身的可執行檔案的字段頭資訊

$ readelf -S test
           
There are 29 section headers, starting at offset 0x18b8:

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         0000000000000238  00000238
       000000000000001c  0000000000000000   A       0     0     1
  [ 2] .note.ABI-tag     NOTE             0000000000000254  00000254
       0000000000000020  0000000000000000   A       0     0     4
  [ 3] .note.gnu.build-i NOTE             0000000000000274  00000274
       0000000000000024  0000000000000000   A       0     0     4
  [ 4] .gnu.hash         GNU_HASH         0000000000000298  00000298
       000000000000001c  0000000000000000   A       5     0     8
  [ 5] .dynsym           DYNSYM           00000000000002b8  000002b8
       00000000000000a8  0000000000000018   A       6     1     8
  [ 6] .dynstr           STRTAB           0000000000000360  00000360
       0000000000000084  0000000000000000   A       0     0     1
  [ 7] .gnu.version      VERSYM           00000000000003e4  000003e4
       000000000000000e  0000000000000002   A       5     0     2
  [ 8] .gnu.version_r    VERNEED          00000000000003f8  000003f8
       0000000000000020  0000000000000000   A       6     1     8
  [ 9] .rela.dyn         RELA             0000000000000418  00000418
       00000000000000c0  0000000000000018   A       5     0     8
  [10] .rela.plt         RELA             00000000000004d8  000004d8
       0000000000000018  0000000000000018  AI       5    22     8
  [11] .init             PROGBITS         00000000000004f0  000004f0
       0000000000000017  0000000000000000  AX       0     0     4
  [12] .plt              PROGBITS         0000000000000510  00000510
       0000000000000020  0000000000000010  AX       0     0     16
  [13] .plt.got          PROGBITS         0000000000000530  00000530
       0000000000000008  0000000000000008  AX       0     0     8
  [14] .text             PROGBITS         0000000000000540  00000540
       00000000000001a2  0000000000000000  AX       0     0     16
  [15] .fini             PROGBITS         00000000000006e4  000006e4
       0000000000000009  0000000000000000  AX       0     0     4
  [16] .rodata           PROGBITS         00000000000006f0  000006f0
       000000000000001b  0000000000000000   A       0     0     4
  [17] .eh_frame_hdr     PROGBITS         000000000000070c  0000070c
       000000000000003c  0000000000000000   A       0     0     4
  [18] .eh_frame         PROGBITS         0000000000000748  00000748
       0000000000000108  0000000000000000   A       0     0     8
  [19] .init_array       INIT_ARRAY       0000000000200db8  00000db8
       0000000000000008  0000000000000008  WA       0     0     8
  [20] .fini_array       FINI_ARRAY       0000000000200dc0  00000dc0
       0000000000000008  0000000000000008  WA       0     0     8
  [21] .dynamic          DYNAMIC          0000000000200dc8  00000dc8
       00000000000001f0  0000000000000010  WA       6     0     8
  [22] .got              PROGBITS         0000000000200fb8  00000fb8
       0000000000000048  0000000000000008  WA       0     0     8
  [23] .data             PROGBITS         0000000000201000  00001000
       0000000000000010  0000000000000000  WA       0     0     8
  [24] .bss              NOBITS           0000000000201010  00001010
       0000000000000008  0000000000000000  WA       0     0     1
  [25] .comment          PROGBITS         0000000000000000  00001010
       0000000000000029  0000000000000001  MS       0     0     1
  [26] .symtab           SYMTAB           0000000000000000  00001040
       0000000000000588  0000000000000018          27    39     8
  [27] .strtab           STRTAB           0000000000000000  000015c8
       00000000000001f2  0000000000000000           0     0     1
  [28] .shstrtab         STRTAB           0000000000000000  000017ba
       00000000000000fe  0000000000000000           0     0     1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  l (large), p (processor specific)
           

假如需要移除字段頭 .gnu.version 的資訊,那麼執行

$ strip -R .gnu.version test
           

再次使用 readelf -S 讀檔案資訊可以發現指定的資訊已被移除。

移除重定位時不需要的符号資訊

如果需要移除重定位時不需要的符号資訊,那麼可以使用選項 --strip-unneeded

$ strip --strip-unneeded test
           

比如需要對靜态連結庫檔案瘦身時,這個選項不影響靜态庫的連結使用。

瘦身時保留部分符号資訊

某些情況下,對可執行檔案瘦身不是一刀切的,還可能需要對部分符号資訊網開一面,可以使用選項 -K

$ strip -s -Kframe_dummy test
           
$ readelf -s test 
Symbol table '.dynsym' contains 7 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_deregisterTMCloneTab
     2: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND printf@GLIBC_2.2.5 (2)
     3: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@GLIBC_2.2.5 (2)
     4: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
     5: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_registerTMCloneTable
     6: 0000000000000000     0 FUNC    WEAK   DEFAULT  UND __cxa_finalize@GLIBC_2.2.5 (2)

Symbol table '.symtab' contains 27 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
    ...
     1: 0000000000000640     0 FUNC    LOCAL  DEFAULT   14 frame_dummy
    ...
           

可見跟在 -K 選項後指定的符号被保留了下來,沒有被一起移除。

另外,在同一個 strip 指令中 -K 選項可以同時多次使用。

移除特定的符号資訊

如果僅僅需要對特定的符号資訊進行移除,其它符号資訊保留,那麼可以使用選項 -N

假設可執行檔案未經任何瘦身操作,然後執行下面的指令

$ strip -Nframe_dummy test

執行上面的指令後,再使用 readelf -s 讀取符号資訊,可以發現跟在 -N 選項後的符号已被移除。

另存檔案

在移除相關資訊後,希望保留原有可執行檔案的内部資訊不變,并且将瘦身後的檔案另存為新檔案,那麼可以使用選項 -o

$ strip -s -otest_striped test
           

輸出瘦身檔案後,使用 readelf -s 分别讀取檔案 test_striped 和 test,對比可見檔案 test 的符号資訊沒有變化,而檔案 test_striped 已移除符号資訊。

保留原有通路和修改時間

當有需要保留原有的通路和修改時間資訊時,可以使用選項 -p

假設可執行檔案未曾被瘦身過,用 stat 看看檔案的詳細狀态

$ stat test
           
File: test
  Size: 8304      	Blocks: 24         IO Block: 4096   regular file
Device: 801h/2049d	Inode: 803646      Links: 1
Access: (0775/-rwxrwxr-x)  Uid: ( 1000/      if)   Gid: ( 1000/      if)
Access: 2023-06-26 21:02:49.274028914 +0800
Modify: 2023-06-26 21:02:49.274028914 +0800
Change: 2023-06-26 21:02:49.274028914 +0800
 Birth: -
           

然後執行 strip 指令,并且嘗試保留原有的通路和修改時間資訊

$ strip -s -p test
           

再看看檔案的詳細狀态

$ stat test
  File: test
  Size: 6120      	Blocks: 16         IO Block: 4096   regular file
Device: 801h/2049d	Inode: 803647      Links: 1
Access: (0775/-rwxrwxr-x)  Uid: ( 1000/      if)   Gid: ( 1000/      if)
Access: 2023-06-26 21:02:49.000000000 +0800
Modify: 2023-06-26 21:02:49.000000000 +0800
Change: 2023-06-26 21:10:53.991892402 +0800
 Birth: -
           

可見,原有的通路和修改時間資訊得以保留,保留到秒機關。

從檔案讀取選項内容

在運維過程中,為了友善不同配置的靈活切換,往往會把參數配置等存儲在檔案中,不同的需求對求不同配置檔案。

strip 指令同樣支援從檔案中讀取配置選項,格式如

$ strip @filename
           

檔案 filename 中儲存有文本形式的參數選項等。比如

$ echo "-s test" > options.txt
$ strip @options.txt
           

效果等同于

$ strip -s test
           

輸出詳細過程

在執行 strip 指令的過程中可以同時輸出詳細資訊,使用選線 -v

$ strip -v -s test
copy from `test' [elf64-x86-64] to `stPp23jj' [elf64-x86-64]           

繼續閱讀