00. 目錄
01. GCC的二進制工具
在GCC的開發環境中,除了基本程序生成的編譯、鏈接工具,還有其它一系列二進制的工具可以使用。
ar 歸檔工具
readelf 讀取ELF格式文件信息
strings 查看字符串
nm 顯示符號信息
strip 刪除符號
02. ar命令
ar工具用於建立、修改、提取歸檔文件(archive)。一個歸檔文件是包含多個目標文件的單個文件,它也被稱爲靜態庫。歸檔文件的結構保證了可以從中檢索並得到原始的被包含的文件,也就是這個歸檔文件的成員。被包含的原始文件的內容、模式(權限)、時間戳、所有者和組等屬性都保存在歸檔文件中,在提取後,可以恢復原始文件的相關屬性。
2.1 ar用法命令
Usage: ar [emulation options] [-]{dmpqrstx}[abcDfilMNoOPsSTuvV] [--plugin <name>] [member-name] [count]
archive-file file...
ar -M [<mri-腳本]
命令:
d - 從歸檔文件中刪除文件
m[ab] - 在歸檔文件中移動文件
p - 打印在歸檔文件中找到的文件
q[f] - 將文件快速追加到歸檔文件中
r[ab][f][u] - 替換歸檔文件中已有的文件或加入新文件
s - 作爲 ranlib 工作
t[O][v] - display contents of the archive
x[o] - 從歸檔文件中分解文件
特定命令修飾符:
[a] - 將文件置於 [成員名] 之後
[b] - 將文件置於 [成員名] 之前 (於 [i] 相同)
[D] - 將 0 用於時間戳和 uid/gid(默認)
[D] - 使用實際時間戳和 uid/gid
[N] - 使用名稱的實例 [數量]
[f] - 截去插入的文件名稱
[P] - 在匹配時使用完整的路徑名
[o] - 保留原來的日期
[O] - display offsets of files in the archive
[u] - 只替換比當前歸檔內容更新的文件
通用修飾符:
[c] - 不在必須創建庫的時候給出警告
[s] - 創建歸檔索引 (cf. ranlib)
[S] - 不要創建符號表
[T] - 產生一個簡單歸檔
[v] - 輸出較多信息
[V] - 顯示版本號
@<file> - 從 <file> 讀取選項
--target=BFDNAME - 指定目標對象格式爲 BFDNAME
--output=DIRNAME - specify the output directory for extraction operations
可選項:
--plugin <p> - 加載指定的插件程序
2.2 用法示例
在GCC工具系列中,ar工具常用於生成靜態庫(static lib)文件。
生成靜態庫
deng@itcast:~/share/3rd/1static_lib$ ar -rv libtest.a add.o sub.o mul.o div.o
ar: 正在創建 libtest.a
a - add.o
a - sub.o
a - mul.o
a - div.o
deng@itcast:~/share/3rd/1static_lib$
2.3 查看已經生成的靜態庫的內容
deng@itcast:~/share/3rd/1static_lib$ ar -t libtest.a
add.o
sub.o
mul.o
div.o
deng@itcast:~/share/3rd/1static_lib$
2.4 對於一個已經生成的靜態庫,再次調用ar可以替換靜態庫中的內容
deng@itcast:~/share/3rd/1static_lib$ ar -rv libtest.a add.o sub.o
r - add.o
r - sub.o
deng@itcast:~/share/3rd/1static_lib$
2.5 使用ar工具加-d選項,可以刪除庫中的成員
deng@itcast:~/share/3rd/1static_lib$ ar -d libtest.a add.o
deng@itcast:~/share/3rd/1static_lib$ ar -t libtest.a
sub.o
mul.o
div.o
deng@itcast:~/share/3rd/1static_lib$
2.6 追加文件到歸檔文件中
deng@itcast:~/share/3rd/1static_lib$ ar -rv libtest.a add.o
a - add.o
deng@itcast:~/share/3rd/1static_lib$
2.7 提取其中的各個目標文件
deng@itcast:~/share/3rd/1static_lib$ ar -x libtest.a
deng@itcast:~/share/3rd/1static_lib$ ls -l *.o
-rw-r--r-- 1 deng deng 1368 6月 15 22:38 add.o
-rw-r--r-- 1 deng deng 1368 6月 15 22:38 div.o
-rw-r--r-- 1 deng deng 1368 6月 15 22:38 mul.o
-rw-r--r-- 1 deng deng 1368 6月 15 22:38 sub.o
03. readelf命令
readelf用來顯示ELF格式文件的信息,可通過參數選項來控制顯示那些特定信息。readelf工具的使用方式如下所示:
deng@itcast:~$ readelf --help
用法:readelf <選項> elf-文件
顯示關於 ELF 格式文件內容的信息
Options are:
-a --all 顯示所有,等價於-h -l -S -s -r -d -V -A -I
-h --file-header 顯示ELF文件頭信息
-l --program-headers 顯示ELF程序頭
--segments --program-headers的別名
-S --section-headers Display the sections' header 顯示節頭
--sections --section-headers的別名
-g --section-groups Display the section groups
-t --section-details Display the section details
-e --headers Equivalent to: -h -l -S
-s --syms 顯示符號表
--symbols An alias for --syms
--dyn-syms Display the dynamic symbol table
-n --notes 顯示核心註釋
-r --relocs 顯示重定向信息
-u --unwind 顯示unwind信息
-d --dynamic 顯示動態段
-V --version-info 顯示版本節
-A --arch-specific 顯示體系架構特定信息
-c --archive-index Display the symbol/file index in an archive
-D --use-dynamic 顯示信息使用動態節
-x --hex-dump=<number|name>
Dump the contents of section <number|name> as bytes
-p --string-dump=<number|name>
Dump the contents of section <number|name> as strings
-R --relocated-dump=<number|name>
Dump the contents of section <number|name> as relocated bytes
-z --decompress Decompress section before dumping it
-w[lLiaprmfFsoRtUuTgAckK] or
--debug-dump[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,
=frames-interp,=str,=loc,=Ranges,=pubtypes,
=gdb_index,=trace_info,=trace_abbrev,=trace_aranges,
=addr,=cu_index,=links,=follow-links]
Display the contents of DWARF debug sections
--dwarf-depth=N Do not display DIEs at depth N or greater
--dwarf-start=N Display DIEs starting with N, at the same depth
or deeper
--ctf=<number|name> Display CTF info from section <number|name>
--ctf-parent=<number|name>
Use section <number|name> as the CTF parent
--ctf-symbols=<number|name>
Use section <number|name> as the CTF external symtab
--ctf-strings=<number|name>
Use section <number|name> as the CTF external strtab
-I --histogram 顯示柱狀圖
-W --wide 對超過80列的輸出設備指定一些行的格式
@<file> Read options from <file>
-H --help 顯示幫助信息
-v --version Display the version number of readelf
將 bug 報告到 <http://www.sourceware.org/bugzilla/>
3.1 顯示目標文件ELF文件頭信息
deng@itcast:~/share/3rd/1static_lib$ readelf -h add.o
ELF 頭:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
類別: ELF64
數據: 2 補碼,小端序 (little endian)
Version: 1 (current)
OS/ABI: UNIX - System V
ABI 版本: 0
類型: REL (可重定位文件)
系統架構: Advanced Micro Devices X86-64
版本: 0x1
入口點地址: 0x0
程序頭起點: 0 (bytes into file)
Start of section headers: 600 (bytes into file)
標誌: 0x0
Size of this header: 64 (bytes)
Size of program headers: 0 (bytes)
Number of program headers: 0
Size of section headers: 64 (bytes)
Number of section headers: 12
Section header string table index: 11
deng@itcast:~/share/3rd/1static_lib$
3.2 顯示共享庫頭信息
deng@itcast:~/share/3rd/3share_lib$ readelf -h libtest.so
ELF 頭:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
類別: ELF64
數據: 2 補碼,小端序 (little endian)
Version: 1 (current)
OS/ABI: UNIX - System V
ABI 版本: 0
類型: DYN (共享目標文件)
系統架構: Advanced Micro Devices X86-64
版本: 0x1
入口點地址: 0x1040
程序頭起點: 64 (bytes into file)
Start of section headers: 14208 (bytes into file)
標誌: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 11
Size of section headers: 64 (bytes)
Number of section headers: 25
Section header string table index: 24
deng@itcast:~/share/3rd/3share_lib$
3.3 顯示可執行文件頭信息如下
deng@itcast:~/share/3rd/4share_test$ readelf -h main
ELF 頭:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
類別: ELF64
數據: 2 補碼,小端序 (little endian)
Version: 1 (current)
OS/ABI: UNIX - System V
ABI 版本: 0
類型: DYN (共享目標文件)
系統架構: Advanced Micro Devices X86-64
版本: 0x1
入口點地址: 0x10e0
程序頭起點: 64 (bytes into file)
Start of section headers: 14816 (bytes into file)
標誌: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 13
Size of section headers: 64 (bytes)
Number of section headers: 31
Section header string table index: 30
deng@itcast:~/share/3rd/4share_test$
04. strings命令
strings是一個簡單的工具,用於查看文件中的字符串。
deng@itcast:~/share/3rd/4share_test$ strings --help
用法:strings [選項] [文件]
打印 [文件] (默認爲標準輸入) 中可打印的字符串
選項爲:
-a - --all 掃描整個文件,而不是數據段
-d --data 掃描數據段
-f --print-file-name 在字符串前面打印文件的名字
-n --bytes=[number] 定位和打印任何以NUL結束的序列,至少number個字節,默認爲4個字節
-<number> least [number] characters (default 4).
-t --radix={o,d,x} 基於八、十、十六進制打印字符串
-w --include-all-whitespace Include all whitespace as valid string characters
-o An alias for --radix=o
-T --target=<BFDNAME> 指定二進制文件的格式
-e --encoding={s,S,b,l,B,L} 選擇字符大小
s = 7-bit, S = 8-bit, {b,l} = 16-bit, {B,L} = 32-bit
-s --output-separator=<string> String used to separate strings in output.
@<file> Read options from <file>
-h --help 顯示strings的所有選項然後退出
-v -V --version 顯示strings的版本號然後退出
deng@itcast:~/share/3rd/4share_test$
4.1 查看目標文件中的字符串
deng@itcast:~/share/3rd/1static_lib$ strings add.o
GCC: (Ubuntu 9.3.0-10ubuntu2) 9.3.0
add.c
.symtab
.strtab
.shstrtab
.text
.data
.bss
.comment
.note.GNU-stack
.note.gnu.property
.rela.eh_frame
deng@itcast:~/share/3rd/1static_lib$
05. nm命令
nm工具用於顯示文件中的符號,可以用於各種ELF格式的文件。使用nm查看前面的目標文件(可重定向文件),將顯示符號及其相關內容。
deng@itcast:~/share/3rd/1static_lib$ nm --help
用法:nm [選項] [文件]
列舉 [文件] 中的符號 (默認爲 a.out)。
The options are:
-a, --debug-syms 只顯示調試符號
-A, --print-file-name 在每個符號前打印輸入文件的名稱
-B Same as --format=bsd
-C, --demangle[=STYLE] 將底層的符號解碼位用戶層的符號。
The STYLE, if specified, can be `auto' (the default),
`gnu', `lucid', `arm', `hp', `edg', `gnu-v3', `java'
or `gnat'
--no-demangle 不解析底層符號
--recurse-limit Enable a demangling recursion limit. This is the default.
--no-recurse-limit Disable a demangling recursion limit.
-D, --dynamic 顯示動態符號而不是普通符號。對動態目標文件有意義。
--defined-only 只顯示定義的符號
-e (ignored)
-f, --format=FORMAT 使用輸出的格式 FORMAT can be `bsd',
`sysv' or `posix'. The default is `bsd'
-g, --extern-only 只顯示外部符號
-l, --line-numbers 對每個符號,使用調試信息去視圖找到文件名符號。對於已定義的符號,查找符號地址的行號。
對於未定義的符號,查找指定符號重定位入口的行號。如果可以找到行號信息,
在其它符號信息之後顯示該信息。
-n, --numeric-sort 按符號對應地址的順序排序,而不是按符號名的字符順序
-o Same as -A
-p, --no-sort 不以任何順序對符號進行排序
-P, --portability Same as --format=posix
-r, --reverse-sort 反轉排序的順序(按照數字或者字母順序)顯示
--plugin NAME Load the specified plugin
-S, --print-size Print size of defined symbols
-s, --print-armap 列出歸檔文件中成員的符號時包含索引,即名字和包含該名字定義的模塊的映射
--size-sort 按照大小排列符號順序,改大小是按照一個符號的值與它下一個符號的值進行計算
--special-syms 包括輸出特殊符號
--synthetic 顯示綜合符號
-t, --radix=RADIX Use RADIX for printing symbol values
--target=BFDNAME Specify the target object format as BFDNAME
-u, --undefined-only 僅顯示沒有定義的符號(那些目標文件中的外部符號)
--with-symbol-versions Display version strings after symbol names
-X 32_64 (ignored)
@FILE Read options from FILE
-h, --help Display this information
-V, --version Display this program's version number
nm符號類型
符號類型 | 含義 |
---|---|
A | Absolute 符號的值是絕對值,並且不會被將來的連接所改變 |
B | BSS 符號位於未初始化數據段 |
C | Common 符號是公共。公共符號是未初始化的數據。在連接時,多個公共符號可能以相同的名字出現。如果符號在其它地方被定義,該符號會被當做未定義的引用來處理。 |
D | Data,符號位於已經初始化數據部分 |
N | 調試符號 |
R | ReadOnly Data 符號位於只讀數據段 |
T | Text 符號位於代碼段 |
U | Undefined 未被定義的符號 |
W | Weak 弱定義符號,也稱爲弱符號。當一個弱定義符號和一個已定義的普通符號連接時,使用該以定義的普通符號不會引起錯誤。當一個未定義的弱符號被連接且該符號位被定義時,該Weak符號變爲零,並且不會產生錯誤。 |
在nm顯示的符號中,小寫字母表示這個符號是局部符號,大寫字母表示這個符號是全局符號。
5.1 查看目標文件中的符號
deng@itcast:~/share/3rd/1static_lib$ nm add.o
0000000000000000 T add
deng@itcast:~/share/3rd/1static_lib$
5.2 查看共享庫中的符號
deng@itcast:~/share/3rd/3share_lib$ nm libtest.so
00000000000010f9 T add
0000000000004020 b completed.8059
w __cxa_finalize
0000000000001040 t deregister_tm_clones
00000000000010b0 t __do_global_dtors_aux
0000000000003e88 d __do_global_dtors_aux_fini_array_entry
0000000000004018 d __dso_handle
0000000000003e90 d _DYNAMIC
0000000000001158 t _fini
00000000000010f0 t frame_dummy
0000000000003e80 d __frame_dummy_init_array_entry
0000000000002118 r __FRAME_END__
0000000000004000 d _GLOBAL_OFFSET_TABLE_
w __gmon_start__
0000000000002000 r __GNU_EH_FRAME_HDR
0000000000001000 t _init
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
0000000000001127 T mul
000000000000113e T mydiv
0000000000001070 t register_tm_clones
0000000000001111 T sub
0000000000004020 d __TMC_END__
deng@itcast:~/share/3rd/3share_lib$
5.3 查看可執行文件中的符號
deng@itcast:~/share/3rd/2static_test$ nm main
00000000000011f2 T add
0000000000004010 B __bss_start
0000000000004010 b completed.8059
w __cxa_finalize@@GLIBC_2.2.5
0000000000004000 D __data_start
0000000000004000 W data_start
0000000000001090 t deregister_tm_clones
0000000000001100 t __do_global_dtors_aux
0000000000003dc0 d __do_global_dtors_aux_fini_array_entry
0000000000004008 D __dso_handle
0000000000003dc8 d _DYNAMIC
0000000000004010 D _edata
0000000000004018 B _end
00000000000012c8 T _fini
06. strip命令
strip工具用於刪除文件中的符號。使用strip既可以刪除目標文件中的某個符號,也可以刪除整個節。使用strip至少要有一個輸入文件,也可以輸入一個文件列表。
strip命令用法如下
deng@itcast:~/share/3rd/3share_lib$ strip --help
用法:strip <選項> 輸入文件
從文件中刪除符號和節
選項爲:
-I --input-target=<bfdname> 默認所有輸入文件爲<bfdname>格式
-O --output-target=<bfdname> 建立<bfdname>格式的輸出文件
-F --target=<bfdname> Set both input and output format to <bfdname>
-p --preserve-dates 保留時間戳信息
-D --enable-deterministic-archives
Produce deterministic output when stripping archives (default)
-U --disable-deterministic-archives
Disable -D behavior
-R --remove-section=<name> 刪除名稱爲<name>的節
--remove-relocations <name> Remove relocations from section <name>
-s --strip-all 刪除所有的節
-g -S -d --strip-debug 刪除所有符號和重定向信息
--strip-dwo Remove all DWO sections
--strip-unneeded 刪除所有重定向不需要的節
--only-keep-debug Strip everything but the debug information
-M --merge-notes Remove redundant entries in note sections (default)
--no-merge-notes Do not attempt to remove redundant notes
-N --strip-symbol=<name> 刪除名稱爲name的符號
--keep-section=<name> Do not strip section <name>
-K --keep-symbol=<name> 保留名稱爲name的符號
--keep-file-symbols Do not strip file symbol(s)
-w --wildcard Permit wildcard in symbol comparison
-x --discard-all 刪除所有非全局符號
-X --discard-locals 刪除所有編譯產生的符號
-v --verbose 顯示所有修改的文件
-V --version Display this program's version number
-h --help Display this output
--info List object formats & architectures supported
-o <file> 輸出文件爲file
6.1 使用strip刪除main文件中的符號,輸出爲main_stripped文件。
# main_stripped是被刪除了符號的可執行程序。
deng@itcast:~/projects/Project1$ strip main -o main_stripped
deng@itcast:~/projects/Project1$ ls -lh main main_stripped
-rwxrwxr-x 1 deng deng 17K 6月 18 20:11 main
-rwxrwxr-x 1 deng deng 15K 6月 18 20:11 main_stripped
deng@itcast:~/projects/Project1$
# main_stripped中已經不包含任何符號
deng@itcast:~/projects/Project1$ nm main_stripped
nm: main_stripped:無符號
deng@itcast:~/projects/Project1$
6.2 使用file命令對比main和main_stripped兩個文件
deng@itcast:~/projects/Project1$ file main
main: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked,
interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=655998d8473f3943b09638ea12e6bb148838c2e1,
for GNU/Linux 3.2.0, not stripped
deng@itcast:~/projects/Project1$ file main_stripped
main_stripped: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-
linux-x86-64.so.2, BuildID[sha1]=655998d8473f3943b09638ea12e6bb148838c2e1, for GNU/Linux 3.2.0, stripped
deng@itcast:~/projects/Project1$
友情提示:
strip工具在嵌入式這種存儲空間非常有限的系統中很有用,它可以極大程度的減少程序的體積,去掉複雜的符號表,因爲符號表主要用於調試,所以可以再PC上調試完成後,燒寫到目標板之前就直接利用strip命令。
07. 附錄
7.1【Linux】GCC程序開發工具(上)
7.2 【Linux】GCC程序開發工具(中)
7.3 【Linux】GCC程序開發工具(下)