麗水微信網(wǎng)站建設(shè)哪家好滄州網(wǎng)絡(luò)推廣公司
嵌入式調(diào)試工具
- 1. nm
- 2. addr2line
- 3. readelf
- 3.1 ELF 文件分類
- 3.2 ELF文件組成
- 3.3使用
1. nm
nm源于name,是linux下一個文本分析工具,可以羅列指定文件中的符號(函數(shù)名、變量,以及符號類型)。
nm
命令參數(shù)如下:
用法:nm [選項] [文件]列舉 [文件] 中的符號 (默認為 a.out)。The options are:-a, --debug-syms Display debugger-only symbols-A, --print-file-name Print name of the input file before every symbol-B Same as --format=bsd-C, --demangle[=STYLE] Decode low-level symbol names into user-level namesThe STYLE, if specified, can be `auto' (the default),`gnu', `lucid', `arm', `hp', `edg', `gnu-v3', `java'or `gnat'--no-demangle Do not demangle low-level symbol names--recurse-limit Enable a demangling recursion limit. This is the default.--no-recurse-limit Disable a demangling recursion limit.-D, --dynamic Display dynamic symbols instead of normal symbols--defined-only Display only defined symbols-e (ignored)-f, --format=FORMAT Use the output format FORMAT. FORMAT can be `bsd',`sysv' or `posix'. The default is `bsd'-g, --extern-only Display only external symbols-l, --line-numbers Use debugging information to find a filename andline number for each symbol-n, --numeric-sort Sort symbols numerically by address-o Same as -A-p, --no-sort Do not sort the symbols-P, --portability Same as --format=posix-r, --reverse-sort Reverse the sense of the sort--plugin NAME Load the specified plugin-S, --print-size Print size of defined symbols-s, --print-armap Include index for symbols from archive members--size-sort Sort symbols by size--special-syms Include special symbols in the output--synthetic Display synthetic symbols as well-t, --radix=RADIX Use RADIX for printing symbol values--target=BFDNAME Specify the target object format as BFDNAME-u, --undefined-only Display only undefined symbols--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
參數(shù)說明:
- -A/-o/–print-file-name: 在輸出時加上文件名;
- -a/–debug-syms: 輸出所有符號,包含debugger-only symbols;
- -B/–format=bsd: BSD碼顯示,兼容MIPS nm;
- -C/–demangle: 將低級符號名解析為用戶級名字,可以使得C++函數(shù)名更具可讀性;
- -D/–dynamic: 顯示動態(tài)符號。該選項只對動態(tài)目標(如特定類型的共享庫)有意義;
- -f format/–format=format 使用format格式輸出。format可以選取bsd、sysv或posix,該選項在GNU的nm中有用。默認為bsd
- -g/–extern-only: 只顯示外部符號;
- -l/–line-numbers: 對于每個符號,使用debug信息找到文件名和行號;
- -n/-v/–numeric-sort: 按符號對應(yīng)地址的順序排序,而非按符號名字字符順序排序;
- -P/–portability: 按照POSIX2.0標準格式輸出,等同于使用 -f posix;
- -p/–no-sort: 按照目標文件中遇到的符號順序顯示,不排序;
- -r/–reverse-sort: 反轉(zhuǎn)排序;
- -s/–print-armap: 當列出庫成員符號時,包含索引。索引的內(nèi)容:模塊和其包含名字的映射;
- -u/–undefined-only: 只顯示未定義符號;
- –defined-only: 只顯示定義了的符號。
符號類型 :如果類型是小寫的,表示該符號是local的;如果符號是大寫的,表示該符號是global(external)的。
符號類型 | 說明 |
---|---|
A | 該符號的值是絕對的,在以后的鏈接過程中,不允許進行改變。這樣的符號值,常常出現(xiàn)在中斷向量表中,例如用符號來表示各個中斷向量函數(shù)在中斷向量表中的位置。 |
B | 該符號的值出現(xiàn)在非初始化數(shù)據(jù)段(bss)中。例如,在一個文件中定義全局static int test。則該符號test的類型為b,位于bss section中。其值表示該符號在bss段中的偏移。一般而言,bss段分配于RAM中 |
C | 該符號為common。common symbol是未初始話數(shù)據(jù)段。該符號沒有包含于一個普通section中。只有在鏈接過程中才進行分配。符號的值表示該符號需要的字節(jié)數(shù)。例如在一個c文件中,定義int test,并且該符號在別的地方會被引用,則該符號類型即為C。否則其類型為B。 |
D | 該符號位于初始話數(shù)據(jù)段中。一般來說,分配到data section中。例如定義全局int baud_table[5] = {9600, 19200, 38400, 57600, 115200},則會分配于初始化數(shù)據(jù)段中。 |
G | 該符號也位于初始化數(shù)據(jù)段中。主要用于small object提高訪問small data object的一種方式。 |
I | 該符號是對另一個符號的間接引用。 |
N | 該符號是一個debugging符號。 |
R | 該符號位于只讀數(shù)據(jù)區(qū)。例如定義全局const int test[] = {123, 123};則test就是一個只讀數(shù)據(jù)區(qū)的符號。注意在cygwin下如果使用gcc直接編譯成MZ格式時,源文件中的test對應(yīng)_test,并且其符號類型為D,即初始化數(shù)據(jù)段中。但是如果使用m6812-elf-gcc這樣的交叉編譯工具,源文件中的test對應(yīng)目標文件的test,即沒有添加下劃線,并且其符號類型為R。一般而言,位于rodata section。值得注意的是,如果在一個函數(shù)中定義const char *test = “abc”, const char test_int = 3。使用nm都不會得到符號信息,但是字符串“abc”分配于只讀存儲器中,test在rodata section中,大小為4。 |
S | 符號位于非初始化數(shù)據(jù)區(qū),用于small object。 |
T | 該符號位于代碼區(qū)text section。 |
U | 該符號在當前文件中是未定義的,即該符號的定義在別的文件中。例如,當前文件調(diào)用另一個文件中定義的函數(shù),在這個被調(diào)用的函數(shù)在當前就是未定義的;但是在定義它的文件中類型是T。但是對于全局變量來說,在定義它的文件中,其符號類型為C,在使用它的文件中,其類型為U。 |
V | 該符號是一個weak object。 |
W | The symbol is a weak symbol that has not been specifically tagged as a weak object symbol. |
- | 該符號是a.out格式文件中的stabs symbol。 |
? | 該符號類型沒有定義 |
2. addr2line
addr2line用于將程序指令地址轉(zhuǎn)換為所對應(yīng)的函數(shù)名、以及函數(shù)所在的源文件名和行號。如果無法確定文件名或函數(shù)名,addr2line將在它們的位置打印兩個問號;如果無法確定行號,addr2line將打印0或一個問號。
addr2line
參數(shù)如下:
用法:addr2line [選項] [地址]將地址轉(zhuǎn)換成文件名/行號對。如果沒有在命令行中給出地址,就從標準輸入中讀取它們The options are:@<file> Read options from <file>-a --addresses Show addresses-b --target=<bfdname> Set the binary file format-e --exe=<executable> Set the input file name (default is a.out)-i --inlines Unwind inlined functions-j --section=<name> Read section-relative offsets instead of addresses-p --pretty-print Make the output easier to read for humans-s --basenames Strip directory names-f --functions Show function names-C --demangle[=style] Demangle function names-R --recurse-limit Enable a limit on recursion whilst demangling. [Default]-r --no-recurse-limit Disable a limit on recursion whilst demangling-h --help Display this information-v --version Display the program's version
參考博客 https://blog.csdn.net/fengbingchun/article/details/119980076
3. readelf
readelf用于讀取ELF(Executable and Linkable Format)格式文件的詳細信息。
3.1 ELF 文件分類
(1)可重定位文件(Relocatable File),這類文件包含了代碼和數(shù)據(jù),用于鏈接生成可以執(zhí)行文件或共享目標文件,目標文件和靜態(tài)鏈接庫均屬于可重定位文件,例如*.o或lib*.a文件;
(2)可執(zhí)行文件(Executable File),用于生成進程映像,載入內(nèi)存執(zhí)行。Linux 環(huán)境下的 ELF 可執(zhí)行文件一般沒有擴展名,例如用戶命令 ls;
(3)共享目標文件(Shared Object File),這種文件包含了代碼和數(shù)據(jù),用于和可重定位文件或其他共享目標文件一起生成可執(zhí)行文件。例如 Linux 的動態(tài)共享對象(Dynamic Shared Object),C 語言運行時庫 glibc-2.5.so;
(4)核心轉(zhuǎn)儲文件(Core Dump File),當進程意外終止時,系統(tǒng)可以將該進程的地址空間的內(nèi)容及終止時的一些其他信息轉(zhuǎn)儲到核心轉(zhuǎn)儲文件。例如 Linux 下的 core dump。
3.2 ELF文件組成
(1)ELF文件頭表(ELF header):記錄了ELF文件的組織結(jié)構(gòu)(如程序頭表的位置和數(shù)量,節(jié)頭表的位置和數(shù)量等等)
(2)程序頭表/段表(program header table):重定位文件不一定需要,生成進程的可執(zhí)行文件必須擁有此文件,告訴程序如何創(chuàng)建進程映像
(3)節(jié)頭表(section header table):記錄ELF文件的節(jié)區(qū)信息(每一個section的入口,名字與大小),用于鏈接的目標文件必須擁有此結(jié)構(gòu)。
Filename extension | none, .axf, .bin, .elf, .o, .out, .prx, .puff, .ko, .mod, and .so |
---|
3.3使用
用法:readelf <選項> elf-文件顯示關(guān)于 ELF 格式文件內(nèi)容的信息Options are:-a --all Equivalent to: -h -l -S -s -r -d -V -A -I-h --file-header Display the ELF file header-l --program-headers Display the program headers--segments An alias for --program-headers-S --section-headers Display the sections' header--sections An alias for --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 Display the symbol table--symbols An alias for --syms--dyn-syms Display the dynamic symbol table-n --notes Display the core notes (if present)-r --relocs Display the relocations (if present)-u --unwind Display the unwind info (if present)-d --dynamic Display the dynamic section (if present)-V --version-info Display the version sections (if present)-A --arch-specific Display architecture specific information (if any)-c --archive-index Display the symbol/file index in an archive-D --use-dynamic Use the dynamic section info when displaying symbols-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 depthor 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 Display histogram of bucket list lengths-W --wide Allow output width to exceed 80 characters@<file> Read options from <file>-H --help Display this information-v --version Display the version number of readelf
參考readelf