前言
如果普通编程不需要了解这些东西,如果想精确控制你的目标文件的格式或者你想查看一下文件里的内容以便作出某种判断,那么你可以看一下下面的工具:ar,nm,objdump,objcopy。具体用法请参考man在线手册。
ar基本用法
ar命令可以用来创建、修改库,也可以从库中提出单个模块。库是一单独的文件,里面包含了按照特定的结构组织起来的其它的一些文件(称做此库文件的member)。原始文件的内容、模式、时间戳、属主、组等属性都保留在库文件中。
下面是ar命令的格式:
ar [-]{dmpqrtx}[abcfilNoPsSuvV] [membername] [count] archive files…
例如我们可以用ar rv libtest.a hello.o hello1.o来
生成一个库,库名字是test,链接时可以用-ltest链接。该库中存放了两个模块hello.o和hello1.o。选项前可以有‘-’字符,也可以
没有。下面我们来看看命令的操作选项和任选项。现在我们把{dmpqrtx}部分称为操作选项,而[abcfilNoPsSuvV]部分称为任选项。
{dmpqrtx}中的操作选项在命令中只能并且必须使用其中一个,它们的含义如下:
下面在看看可与操作选项结合使用的任选项:
nm基本用法
nm用来列出目标文件的符号清单。下面是nm命令的格式:
nm [-a│--debug-syms] [-g│--extern-only]
[-B] [-C│--demangle[=style]] [-D│--dynamic]
[-S│--print-size] [-s│--print-armap]
[-A│-o│--print-file-name][--special-syms]
[-n│-v│--numeric-sort] [-p│--no-sort]
[-r│--reverse-sort] [--size-sort] [-u│--undefined-only]
[-t radix│--radix=radix] [-P│--portability]
[--target=bfdname] [-f format│--format=format]
[--defined-only] [-l│--line-numbers] [--no-demangle]
[-V│--version] [-X 32_64] [--help] [objfile...]
如果没有为nm命令指出目标文件,则nm假定目标文件是a.out。下面列出该命令的任选项,大部分支持”-”开头的短格式和”—”开头的长格式。
例如nm libtest.a的输出如下:
CPThread.o:
00000068 T Main__8CPThreadPv
00000038 T Start__8CPThread
00000014 T _._8CPThread
00000000 T __8CPThread
00000000 ? __FRAME_BEGIN__
…………………………………
则nm -A 的输出如下:
libtest.a:CPThread.o:00000068 T Main__8CPThreadPv
libtest.a:CPThread.o:00000038 T Start__8CPThread
libtest.a:CPThread.o:00000014 T _._8CPThread
libtest.a:CPThread.o:00000000 T __8CPThread
libtest.a:CPThread.o:00000000 ? __FRAME_BEGIN__
…………………………………………………………
对于每一个符号,nm列出其值(the symbol value),类型(the symbol type)和其名字(the symbol name)。
符号
类型
|
说明
|
A
|
该符号的值是绝对的,在以后的链接过程中,不允许进行改变。这样的符号值,常常出现在中断向量表中,例如用符号来表示各个中断向量函数在中断向量表中的位置。
|
B
|
该符号的值出现在非初始化数据段(bss)中。例如,在一个文件中定义全局static int test。则该符号test的类型为b,位于bss section中。其值表示该符号在bss段中的偏移。一般而言,bss段分配于RAM中
|
C
|
该符号为common。common symbol是未初始话数据段。该符号没有包含于一个普通section中。只有在链接过程中才进行分配。符号的值表示该符号需要的字节数。例如在一个c文件中,定义int test,并且该符号在别的地方会被引用,则该符号类型即为C。否则其类型为B。
|
D
|
该符号位于初始话数据段中。一般来说,分配到data section中。例如定义全局int baud_table[5] = {9600, 19200, 38400, 57600, 115200},则会分配于初始化数据段中。
|
G
|
该符号也位于初始化数据段中。主要用于small object提高访问small data object的一种方式。
|
I
|
该符号是对另一个符号的间接引用。
|
N
|
该符号是一个debugging符号。
|
R
|
该符号位于只读数据区。例如定义全局const int test[] = {123, 123};则test就是一个只读数据区的符号。注意在cygwin下如果使用gcc直接编译成MZ格式时,源文件中的test对应_test,并且其符号类型为D,即初始化数据段中。但是如果使用m6812-elf-gcc这样的交叉编译工具,源文件中的test对应目标文件的test,即没有添加下划线,并且其符号类型为R。一般而言,位于rodata section。值得注意的是,如果在一个函数中定义const char *test = “abc”, const char test_int = 3。使用nm都不会得到符号信息,但是字符串“abc”分配于只读存储器中,test在rodata section中,大小为4。
|
S
|
符号位于非初始化数据区,用于small object。
|
T
|
该符号位于代码区text section。
|
U
|
该符号在当前文件中是未定义的,即该符号的定义在别的文件中。例如,当前文件调用另一个文件中定义的函数,在这个被调用的函数在当前就是未定义的;但是在定义它的文件中类型是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。
|
?
|
该符号类型没有定义
|
objdump基本用法
objdump有点象那个快速查看之流的工具,就是以一种可阅读的格式让你更多地了解二进制文件可能带有的附加信息。对于一般只想让自己程序跑起来的程序
员,这个命令没有更多意义,对于想进一步了解系统的程序员,应该掌握这种工具,至少你可以自己写写shellcode了,或者看看人家给的exploit
中的shellcode是什么东西。更多关于目标文件的内容分析建议看看《深入理解计算机》这本书的第二部分第七章节。
常用法:
objdump [-a│--archive-headers]
[-b bfdname│--target=bfdname]
[-C│--demangle[=style] ]
[-d│--disassemble]
[-D│--disassemble-all]
[-z│--disassemble-zeroes]
[-EB│-EL│--endian={big │ little }]
[-f│--file-headers]
[--file-start-context]
[-g│--debugging]
[-e│--debugging-tags]
[-h│--section-headers│--headers]
[-i│--info]
[-j section│--section=section]
[-l│--line-numbers]
[-S│--source]
[-m machine│--architecture=machine]
[-M options│--disassembler-options=options]
[-p│--private-headers]
[-r│--reloc]
[-R│--dynamic-reloc]
[-s│--full-contents]
[-W│--dwarf]
[-G│--stabs]
[-t│--syms]
[-T│--dynamic-syms]
[-x│--all-headers]
[-w│--wide]
[--start-address=address]
[--stop-address=address]
[--prefix-addresses]
[--[no-]show-raw-insn]
[--adjust-vma=offset]
[--special-syms]
[-V│--version]
[-H│--help]
objfile…
选项详解:
–archive-headers
-a 显示档案库的成员信息,与 ar tv 类似
objdump -a libpcap.a
和 ar -tv libpcap.a 显示结果比较比较
显然这个选项没有什么意思。
–adjust-vma=offset
When dumping information, first add offset to all
the section addresses. This is useful if the sec-
tion addresses do not correspond to the symbol
table, which can happen when putting sections at
particular addresses when using a format which can
not represent section addresses, such as a.out.
-b bfdname
–target=bfdname
指定目标码格式。这不是必须的,objdump能自动识别许多格式,
比如:objdump -b oasys -m vax -h fu.o
显示fu.o的头部摘要信息,明确指出该文件是Vax系统下用Oasys
编译器生成的目标文件。objdump -i将给出这里可以指定的
目标码格式列表
–demangle
-C 将底层的符号名解码成用户级名字,除了去掉所有开头
的下划线之外,还使得C++函数名以可理解的方式显示出来。
–debugging
显示调试信息。企图解析保存在文件中的调试信息并以C语言
的语法显示出来。仅仅支持某些类型的调试信息。
–disassemble
-d 反汇编那些含有指令机器码的section
–disassemble-all
-D 与 -d 类似,但反汇编所有section
–prefix-addresses
反汇编的时候,显示每一行的完整地址。这是一种比较老的反汇编格式。
显示效果并不理想,但可能会用到其中的某些显示,自己可以对比。
–disassemble-zeroes
一般反汇编输出将省略大块的零,该选项使得这些零块也被反汇编。
-EB
-EL
–endian={big|little}
这个选项将影响反汇编出来的指令。
little-endian就是我们当年在dos下玩汇编的时候常说的高位在高地址,
x86都是这种。
–file-headers
-f 显示objfile中每个文件的整体头部摘要信息。
–section-headers
–headers
-h 显示目标文件各个section的头部摘要信息。
–help 简短的帮助信息。
–info
-i 显示对于 -b 或者 -m 选项可用的架构和目标格式列表。
–section=name
-j name 仅仅显示指定section的信息
–line-numbers
-l 用文件名和行号标注相应的目标代码,仅仅和-d、-D或者-r一起使用
使用-ld和使用-d的区别不是很大,在源码级调试的时候有用,要求
编译时使用了-g之类的调试编译选项。
–architecture=machine
-m machine
指定反汇编目标文件时使用的架构,当待反汇编文件本身没有描述
架构信息的时候(比如S-records),这个选项很有用。可以用-i选项
列出这里能够指定的架构
–reloc
-r 显示文件的重定位入口。如果和-d或者-D一起使用,重定位部分以反汇
编后的格式显示出来。
–dynamic-reloc
-R 显示文件的动态重定位入口,仅仅对于动态目标文件有意义,比如某些
共享库。
–full-contents
-s 显示指定section的完整内容。
objdump –section=.text -s inet.o | more
–source
-S 尽可能反汇编出源代码,尤其当编译的时候指定了-g这种调试参数时,
效果比较明显。隐含了-d参数。
–show-raw-insn
反汇编的时候,显示每条汇编指令对应的机器码,除非指定了
–prefix-addresses,这将是缺省选项。
–no-show-raw-insn
反汇编时,不显示汇编指令的机器码,这是指定 –prefix-addresses
选项时的缺省设置。
–stabs
Display the contents of the .stab, .stab.index, and
.stab.excl sections from an ELF file. This is only
useful on systems (such as Solaris 2.0) in which
.stab debugging symbol-table entries are carried in
an ELF section. In most other file formats, debug-
ging symbol-table entries are interleaved with
linkage symbols, and are visible in the –syms output.
–start-address=address
从指定地址开始显示数据,该选项影响-d、-r和-s选项的输出。
–stop-address=address
显示数据直到指定地址为止,该选项影响-d、-r和-s选项的输出。
–syms
-t 显示文件的符号表入口。类似于nm提供的信息
–dynamic-syms
-T 显示文件的动态符号表入口,仅仅对动态目标文件有意义,比如某些
共享库。它显示的信息类似于 nm -D|–dynamic 显示的信息。
–version 版本信息
objdump –version
–all-headers
-x 显示所有可用的头信息,包括符号表、重定位入口。-x 等价于
-a -f -h -r -t 同时指定。
objdump -x inet.o
readelf基本用法
readelf 负责显示ELF文件的信息
Usage: readelf <option(s)> elf-file(s)
Display information about the contents of ELF format files
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
-e –headers 全部头 Equivalent to: -h -l -S
-s –syms 符号表 Display the symbol table
–symbols An alias for –syms
-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 segment (if present)
-V –version-info 版本 Display the version sections (if present)
-A –arch-specific CPU构架 Display architecture specific information (if any).
-D –use-dynamic 动态段 Use the dynamic section info when displaying symbols
-x –hex-dump=<number> 显示 段内内容Dump the contents of section <number>
-w[liaprmfFso] or
–debug-dump[=line,=info,=abbrev,=pubnames,=ranges,=macro,=frames,=str,=loc]
显示DWARF2调试段内容 Display the contents of DWARF2 debug sections
-I –histogram Display histogram of bucket list lengths
-W –wide 宽行输出 Allow output width to exceed 80 characters
-H –help Display this information
-v –version Display the version number of readelf
Usage: readelf <option(s)> elf-file(s)
Display information about the contents of ELF format files
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
-e –headers 全部头 Equivalent to: -h -l -S
-s –syms 符号表 Display the symbol table
–symbols An alias for –syms
-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 segment (if present)
-V –version-info 版本 Display the version sections (if present)
-A –arch-specific CPU构架 Display architecture specific information (if any).
-D –use-dynamic 动态段 Use the dynamic section info when displaying symbols
-x –hex-dump=<number> 显示 段内内容Dump the contents of section <number>
-w[liaprmfFso] or
–debug-dump[=line,=info,=abbrev,=pubnames,=ranges,=macro,=frames,=str,=loc]
显示DWARF2调试段内容 Display the contents of DWARF2 debug sections
-I –histogram Display histogram of bucket list lengths
-W –wide 宽行输出 Allow output width to exceed 80 characters
-H –help Display this information
本文转自:http://www.kgdb.info/linuxdev/object_analyse_tools/