[binutils] strip

stripは gnu binutilsの中の一つだよぉ
http://d.hatena.ne.jp/toshi_hirasawa/20120202/1328189742



システムコールexitにてつくったhello4(1038byte)の調理つづき



readelfで解析してみると

[hirasawa@ubunt1004-32-2 gcc-Programming-Kobo]$ readelf -h hello4
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           Intel 80386
  Version:                           0x1
  Entry point address:               0x80480cc
  Start of program headers:          52 (bytes into file)
  Start of section headers:          360 (bytes into file)
  Flags:                             0x0
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         3
  Size of section headers:           40 (bytes)
  Number of section headers:         10
  Section header string table index: 7
[hirasawa@ubunt1004-32-2 gcc-Programming-Kobo]$ readelf -l hello4

Elf file type is EXEC (Executable file)
Entry point 0x80480cc
There are 3 program headers, starting at offset 52

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  LOAD           0x000000 0x08048000 0x08048000 0x000fd 0x000fd R E 0x1000
  LOAD           0x000100 0x08049100 0x08049100 0x00000 0x00000 RW  0x1000
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x4

 Section to Segment mapping:
  Segment Sections...
   00     .text .rodata 
   01     
   02     
[hirasawa@ubunt1004-32-2 gcc-Programming-Kobo]$ readelf -S hello4
There are 10 section headers, starting at offset 0x168:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .rel.plt          REL             08048094 000094 000000 08   A  0   2  4
  [ 2] .plt              PROGBITS        08048094 000094 000000 00  AX  0   0  4
  [ 3] .text             PROGBITS        08048094 000094 000062 00  AX  0   0  4
  [ 4] .rodata           PROGBITS        080480f6 0000f6 000007 00   A  0   0  1
  [ 5] .got.plt          PROGBITS        08049100 000100 000000 00  WA  0   0  4
  [ 6] .comment          PROGBITS        00000000 000100 000023 01  MS  0   0  1
  [ 7] .shstrtab         STRTAB          00000000 000123 000044 00      0   0  1
  [ 8] .symtab           SYMTAB          00000000 0002f8 0000e0 10      9   8  4
  [ 9] .strtab           STRTAB          00000000 0003d8 000036 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)
[hirasawa@ubunt1004-32-2 gcc-Programming-Kobo]$ 

セクション情報(readelf -S)に注目すると
symbol tableを削れそうなので、
strip -s (strip all symbols)をやってみます

[hirasawa@ubunt1004-32-2 gcc-Programming-Kobo]$ ls -ltr hello4
-rwxr-xr-x 1 500 500 1038 2012-02-04 18:52 hello4
[hirasawa@ubunt1004-32-2 gcc-Programming-Kobo]$ strip -s hello4
[hirasawa@ubunt1004-32-2 gcc-Programming-Kobo]$ ls -ltr hello4
-rwxr-xr-x 1 500 500 664 2012-02-04 19:26 hello4
[hirasawa@ubunt1004-32-2 gcc-Programming-Kobo]$ readelf -S hello4
There are 8 section headers, starting at offset 0x158:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .rel.plt          REL             08048094 000100 000000 08   A  0   2  4
  [ 2] .plt              PROGBITS        08048094 000100 000000 00  AX  0   0  4
  [ 3] .text             PROGBITS        08048094 000094 000062 00  AX  0   0  4
  [ 4] .rodata           PROGBITS        080480f6 0000f6 000007 00   A  0   0  1
  [ 5] .got.plt          PROGBITS        08049100 000100 000000 00  WA  0   0  4
  [ 6] .comment          PROGBITS        00000000 000100 000023 01  MS  0   0  1
  [ 7] .shstrtab         STRTAB          00000000 000123 000034 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)
[hirasawa@ubunt1004-32-2 gcc-Programming-Kobo]$ 

||>

1038byteから664byteまで小さくなりました

つぎは strip -R にて 必要ないものを削除

[hirasawa@ubunt1004-32-2 gcc-Programming-Kobo]$ strip -R .comment hello4

ってかんじでどんどんけずると
最終的には 440byte

[hirasawa@ubunt1004-32-2 gcc-Programming-Kobo]$ ls -ltr hello4
-rwxr-xr-x 1 500 500 440 2012-02-04 19:33 hello4
[hirasawa@ubunt1004-32-2 gcc-Programming-Kobo]$ readelf -S hello4
There are 4 section headers, starting at offset 0x118:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        08048094 000094 000062 00  AX  0   0  4
  [ 2] .rodata           PROGBITS        080480f6 0000f6 000007 00   A  0   0  1
  [ 3] .shstrtab         STRTAB          00000000 0000fd 000019 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)
[hirasawa@ubunt1004-32-2 gcc-Programming-Kobo]$ 

まで小さくすることができますぇ

hexdumpにつづく