indent

indent命令可辨识C的原始代码文件,并加以格式化,以方便程序员阅读、修改等操作。

语法

indent(选项)(源文件)
或
indent(选项)(源文件)(-o 目标文件)

选项

-bad:在声明区加上空白行;
-bap:添加空白行;
-bbb:在注释后面添加空白行;
-bc:在声明段中,如果出现逗号就换行;
-bl:if(或是else、for等)与后面执行区段的“{”不同行,且“}”自成一行-bli<缩排格数>设置{}缩排的格数;
-br:if(或是else、for等)与后面执行区段的“{”同行,且“}”自成一行;
-bs:在sizeof之后空一格;
-c<栏数>:将注释置于程序右侧指定的栏位;
-cd<栏数>:将注释置于声明右侧指定的栏位;
-cdb:注释符号自成一行;
-ce:将else置于“}”(if执行区段的结尾)之后;
-ci:<缩排格数>:叙述过长而换行时,指定换行后缩排的格数;
-cli<缩排格数>:使用case时,switch缩排的格数;
-cp<栏数>:将注释置于else与elseif叙述右侧指定的栏位;
-cs:在case之后空一格;
-d<缩排格数>:针对不是放在程序码右侧的注释,设置其缩排格数;
-di<栏数>:将声明区段的变量置于指定的栏位;
-fc1:针对放在每行最前端的注释,设置其格式;
-fca:设置所有注释的格式;
-gnu:使用指定的GNU格式,该参数为默认值;
-i<格数>:设置缩排的格数;
-ip<格数>:设置参数的缩排格数;
-kr:指定使用Kernighan&Ritchie的格式;
-lp:叙述过长而换行,且叙述中包含了括号时,将括号中的每行起始栏位内容垂直对其排列;
-nbad:在声明区段后不要加上空白行;
-nbap:在程序后面不添加空白行;
-nbbb:在注释段后面不添加空白行;
-nbc:在声明段中,即使出现逗号,也不换行;
-ncdb:注释符号不自成一行;
-nce:不将else置于“}”后面;
-ncs:不在case后面空一格;
-nfc1:不要格式化放在每行最前端的注释;
-nfca:不用格式化任何的注释;
-nip:参数不要缩排;
-nlp:叙述过长而换行,且叙述中包含了括号时,不用将括号中的每行起始栏位垂直对其排列;
-npcs:在调用函数名之后,不要添加空格;
-npro:不要读取indent的配置文件“.indent.pro”;
-npsl:程序类型与程序名称放在同一行;
-nsc:注释左侧不要添加星号;
-nsob:不用处理多余的空白行;
-nss:若for或while区段仅有一行时,在分号前不加空格;
-nv:不显示详细的信息;
-orig:使用berkeley格式;
-pcs:在调用函数名与“{”之间添加空格;
-psl:程序类型置于程序名称的前一行;
-sc:在每行注释左侧添加星号;
-sob:删除多余的空白行;
-ss:若for或swile区段仅有一行时,在分号前加上空格;
-st:将结果显示在标准输出设备上;
-T:数据类型名称缩排;
-ts<格数>:设置tab的长度;
-v:显示详细的执行过程;
--version:显示版本信息。

实例

使用indent命令将C语言源文件"test.c"中所有的sizeof后面添加一个空格,输入如下命令:

indent -bs /home/rootlocal/桌面/test.c

执行上面的命令后,用户可以打开指定的源文件查看在sizeof后面是否都添加了一个空格。由于该命令的参数非常多,所以用户可以根据实际需要选择适合的参数进行使用即可。

dpkg-reconfigure

dpkg-reconfigure命令是Debian Linux中重新配置已经安装过的软件包,可以将一个或者多个已安装的软件包传递给此指令,它将询问软件初次安装后的配置问题。

当用户需要再次对软件包配置的时候,可以使用dpkg-reconfigure命令来对指定的软件包进行配置。

语法

dpkg-reconfigure(选项)(参数)

选项

-a:重新配置所有的软件包;
-u或--unseen-only:仅显示未提过的问题;
--default-priority:使用默认优先级,而非“低”级;
--force:强制执行操作,需谨慎使用此选项;
--no-reload:不要轻易的重装模板(使用时请慎重考虑);
-f或--frontend:指定 debconf 前端界面;
-p或--priority:指定要显示的问题的最优先级;
--terse:开启简要模式。

参数

软件包名:需要重新配置的已安装的软件包。

实例

用于配置语言:

sudo dpkg-reconfigure locales

reject

reject命令属于CUPS套件,用于指示打印系统拒绝发往指定目标打印机的打印任务。

语法

reject(选项)(参数)

选项

-E:当连接到服务器时强制使用加密;
-U:指定连接服务器时使用的用户名;
-h:指定连接服务器名和端口号;
-r:指定拒绝打印任务的原因。

参数

目标:指定目标打印机。

ngrep

ngrep命令grep命令的网络版,他力求更多的grep特征,用于搜寻指定的数据包。正由于安装ngrep需用到libpcap库, 所以支持大量的操作系统和网络协议。能识别TCP、UDP和ICMP包,理解bpf的过滤机制。

安装

ngrep命令的下载地址:http://ngrep.sourceforge.net/,libpcap下载地址:http://www.tcpdump.org/。先用yum install libpcap完全安装libpcap,注意有时候用libpcap安装包安装的不完整会影响ngrep的使用。

如果yum无法安装就用以下步骤安装libpcap:

wget http://www.tcpdump.org/release/libpcap-1.3.0.tar.gz
tar -zxf libpcap-1.3.0.tar.gz
cd libpcap-1.3.0
./configure
make && make install

ngrep的安装就是 configure/make/make install 三部曲。

注:configure时是遇到 please wipe out all unused pcap installations,添加以下选项:

./configure --with-pcap-includes=/usr/local/include/pcap

在安装后输入ngrep来验证下安装是否成功。

语法

ngrep <-LhNXViwqpevxlDtTRM> <-IO pcap_dump> <-n num> <-d dev> <-A num>
<-s snaplen> <-S limitlen> <-w normal|byline|single|none> <-c cols>
<-P char> <-F file> <match expression> <bpf filter>

选项

-e :显示空数据包
-i :忽略大小写
-v :反转匹配
-R :don't do privilege revocation logic
-x :以16进制格式显示
-X :以16进制格式匹配
-w :整字匹配
-p :不使用混杂模式
-l :make stdout line buffered
-D :replay pcap_dumps with their recorded time intervals
-t :在每个匹配的包之前显示时间戳
-T :显示上一个匹配的数据包之间的时间间隔
-M :仅进行单行匹配
-I :从文件中读取数据进行匹配
-O :将匹配的数据保存到文件
-n :仅捕获指定数目的数据包进行查看
-A :匹配到数据包后dump随后的指定数目的数据包
-s :set the bpf caplen
-S :set the limitlen on matched packets
-W :设置显示格式byline将解析包中的换行符
-c :强制显示列的宽度
-P :set the non-printable display char to what is specified
-F :使用文件中定义的bpf(Berkeley Packet Filter)
-N :显示由IANA定义的子协议号
-d :使用哪个网卡,可以用-L选项查询
-L :查询网卡接口

实例

捕捉cloudian:18080端口的request和response,-W byline用来解析包中的换行符,否则包里的所有数据都是连续的,可读性差。-d lo是监听本地网卡:

ngrep -W byline -d lo port 18080

捕捉amazon:80端口的request和response。-d eth0是用来监听对外的网卡:

ngrep -W byline -d eth0 port 80

可以用-d any来捕捉所有的包:

ngrep '[a-zA-Z]' -t -W byline -d any tcp port 18080

捕获字符串.flv,比如要查看在Web Flash 视频中的.flv文件的下载地址:

ngrep -d3 -N -q \.flv
interface: \Device\TNT_40_1_{670F6B50-0A13-4BAB-9D9E-994A833F5BA9} (10.132.0.0/2
55.255.192.0)
match: \.flv

打开一个视频页面:

T(6) 10.132.34.23:24860 -> 61.142.208.154:80 [AP]
GET /f59.c31.56.com/flvdownload/12/19/ggyg7741@56.com_56flv_zhajm_119556973
97.flv HTTP/1.1..accept: */*..Referer: http://www.56.com/flashApp/v_player_
site.swf..x-flash-version: 9,0,45,0..UA-CPU: x86..Accept-Encoding: gzip, de
flate..User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET
CLR 2.0.50727; .NET CLR 3.0.04506.30)..host: f59.r.56.com..Connection: Keep
-Alive..Cookie: whistoryview=23423759-23635627-23423344-23171935-23058374-2
3081156-23207350-22395727-; geoip=............; wl_all_s=y....

OK。地址已经找到了,就是http://f59.c31.56.com/flvdownload/12/19/ggyg7741@56.com_56flv_zhajm_11955697397.flv

加个-W byline参数后,将解析包中的换行符:

T(6) 2007/11/25 15:56:12.192619 10.132.34.23:26365 -> 59.151.21.101:80 [AP]
GET /aa.flv HTTP/1.1.
Accept: */*.
Accept-Language: zh-cn.
UA-CPU: x86.
Accept-Encoding: gzip, deflate.
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.5072
7; .NET CLR 3.0.04506.30).
Host: www.google.cn.
Connection: Keep-Alive.
Cookie: PREF=id=a0b2932c336477e9:TB=4:NW=1:TM=1187877372:LM=1187956074:S=Y1Fzndp
rT3vFo7ac; SID=DQAAAHcAAABJCEXeOVLHu2rIfb5BfKP3GG9PbhJDEkXsLTV8y0f_lvSd2Y46Q0FPt
83CnEs9rxA1xBDM9mLR8-ckWeScyOQA8PyYnX5u5OjFvjfRbDg_FDZfwxhRzqS9KPZv26pjnsUxs0FDM
1xpJ5AgDn38pXtlCdkksJ0-cbiIWoA61oHWMg; NID=7=AvJxn5B6YOLLxoYz4LLzhIbNsQUQiulRS6U
JGxdBniQBmXm99y7L-NBNORN82N3unmZSGHFPfePVHnLK2MjYjglyXZhU9x7ETXNBnY3NurNijHDhJ7K
yi7E53UBOcv4V.

pstack

pstack命令可显示每个进程的栈跟踪。pstack 命令必须由相应进程的属主或 root 运行。可以使用 pstack 来确定进程挂起的位置。此命令允许使用的唯一选项是要检查的进程的 PID

命令软件包下载地址:https://packages.debian.org/sid/pstack

实例

pstree以树结构显示进程

pstree -p work | grep ad
sshd(22669)---bash(22670)---ad_preprocess(4551)-+-{ad_preprocess}(4552)
                                                |-{ad_preprocess}(4553)
                                                |-{ad_preprocess}(4554)
                                                |-{ad_preprocess}(4555)
                                                |-{ad_preprocess}(4556)
                                                `-{ad_preprocess}(4557)

work为工作用户,-p为显示进程识别码,ad_preprocess共启动了6个子线程,加上主线程共7个线程。

ps -Lf 4551
UID        PID  PPID   LWP  C NLWP STIME TTY      stat   time CMD
work      4551 22670  4551  2    7 16:30 pts/2    Sl+    0:02 ./ad_preprocess
work      4551 22670  4552  0    7 16:30 pts/2    Sl+    0:00 ./ad_preprocess
work      4551 22670  4553  0    7 16:30 pts/2    Sl+    0:00 ./ad_preprocess
work      4551 22670  4554  0    7 16:30 pts/2    Sl+    0:00 ./ad_preprocess
work      4551 22670  4555  0    7 16:30 pts/2    Sl+    0:00 ./ad_preprocess
work      4551 22670  4556  0    7 16:30 pts/2    Sl+    0:00 ./ad_preprocess
work      4551 22670  4557  0    7 16:30 pts/2    Sl+    0:00 ./ad_preprocess

进程共启动了7个线程

pstack显示每个进程的栈跟踪:

pstack 4551
Thread 7 (Thread 1084229984 (LWP 4552)):
#0  0x000000302afc63dc in epoll_wait () from /lib64/tls/libc.so.6
#1  0x00000000006f0730 in ub::EPollEx::poll ()
#2  0x00000000006f172a in ub::NetReactor::callback ()
#3  0x00000000006fbbbb in ub::UBTask::CALLBACK ()
#4  0x000000302b80610a in start_thread () from /lib64/tls/libpthread.so.0
#5  0x000000302afc6003 in clone () from /lib64/tls/libc.so.6
#6  0x0000000000000000 in ?? ()
Thread 6 (Thread 1094719840 (LWP 4553)):
#0  0x000000302afc63dc in epoll_wait () from /lib64/tls/libc.so.6
#1  0x00000000006f0730 in ub::EPollEx::poll ()
#2  0x00000000006f172a in ub::NetReactor::callback ()
#3  0x00000000006fbbbb in ub::UBTask::CALLBACK ()
#4  0x000000302b80610a in start_thread () from /lib64/tls/libpthread.so.0
#5  0x000000302afc6003 in clone () from /lib64/tls/libc.so.6
#6  0x0000000000000000 in ?? ()
Thread 5 (Thread 1105209696 (LWP 4554)):
#0  0x000000302b80baa5 in __nanosleep_nocancel ()
#1  0x000000000079e758 in comcm::ms_sleep ()
#2  0x00000000006c8581 in ub::UbClientManager::healthyCheck ()
#3  0x00000000006c8471 in ub::UbClientManager::start_healthy_check ()
#4  0x000000302b80610a in start_thread () from /lib64/tls/libpthread.so.0
#5  0x000000302afc6003 in clone () from /lib64/tls/libc.so.6
#6  0x0000000000000000 in ?? ()
Thread 4 (Thread 1115699552 (LWP 4555)):
#0  0x000000302b80baa5 in __nanosleep_nocancel ()
#1  0x0000000000482b0e in armor::armor_check_thread ()
#2  0x000000302b80610a in start_thread () from /lib64/tls/libpthread.so.0
#3  0x000000302afc6003 in clone () from /lib64/tls/libc.so.6
#4  0x0000000000000000 in ?? ()
Thread 3 (Thread 1126189408 (LWP 4556)):
#0  0x000000302af8f1a5 in __nanosleep_nocancel () from /lib64/tls/libc.so.6
#1  0x000000302af8f010 in sleep () from /lib64/tls/libc.so.6
#2  0x000000000044c972 in Business_config_manager::run ()
#3  0x0000000000457b83 in Thread::run_thread ()
#4  0x000000302b80610a in start_thread () from /lib64/tls/libpthread.so.0
#5  0x000000302afc6003 in clone () from /lib64/tls/libc.so.6
#6  0x0000000000000000 in ?? ()
Thread 2 (Thread 1136679264 (LWP 4557)):
#0  0x000000302af8f1a5 in __nanosleep_nocancel () from /lib64/tls/libc.so.6
#1  0x000000302af8f010 in sleep () from /lib64/tls/libc.so.6
#2  0x00000000004524bb in Process_thread::sleep_period ()
#3  0x0000000000452641 in Process_thread::run ()
#4  0x0000000000457b83 in Thread::run_thread ()
#5  0x000000302b80610a in start_thread () from /lib64/tls/libpthread.so.0
#6  0x000000302afc6003 in clone () from /lib64/tls/libc.so.6
#7  0x0000000000000000 in ?? ()
Thread 1 (Thread 182894129792 (LWP 4551)):
#0  0x000000302af8f1a5 in __nanosleep_nocancel () from /lib64/tls/libc.so.6
#1  0x000000302af8f010 in sleep () from /lib64/tls/libc.so.6
#2  0x0000000000420d79 in Ad_preprocess::run ()
#3  0x0000000000450ad0 in main ()

objdump

objdump命令是用查看目标文件或者可执行的目标文件的构成的gcc工具。

选项

--archive-headers 
-a 
显示档案库的成员信息,类似ls -l将lib*.a的信息列出。 

-b bfdname 
--target=bfdname 
指定目标码格式。这不是必须的,objdump能自动识别许多格式,比如: 

objdump -b oasys -m vax -h fu.o 
显示fu.o的头部摘要信息,明确指出该文件是Vax系统下用Oasys编译器生成的目标文件。objdump -i将给出这里可以指定的目标码格式列表。 

-C 
--demangle 
将底层的符号名解码成用户级名字,除了去掉所开头的下划线之外,还使得C++函数名以可理解的方式显示出来。 

--debugging 
-g 
显示调试信息。企图解析保存在文件中的调试信息并以C语言的语法显示出来。仅仅支持某些类型的调试信息。有些其他的格式被readelf -w支持。 

-e 
--debugging-tags 
类似-g选项,但是生成的信息是和ctags工具相兼容的格式。 

--disassemble 
-d 
从objfile中反汇编那些特定指令机器码的section。 

-D 
--disassemble-all 
与 -d 类似,但反汇编所有section. 

--prefix-addresses 
反汇编的时候,显示每一行的完整地址。这是一种比较老的反汇编格式。 

-EB 
-EL 
--endian={big|little} 
指定目标文件的小端。这个项将影响反汇编出来的指令。在反汇编的文件没描述小端信息的时候用。例如S-records. 

-f 
--file-headers 
显示objfile中每个文件的整体头部摘要信息。 

-h 
--section-headers 
--headers 
显示目标文件各个section的头部摘要信息。 

-H 
--help 
简短的帮助信息。 

-i 
--info 
显示对于 -b 或者 -m 选项可用的架构和目标格式列表。 

-j name
--section=name 
仅仅显示指定名称为name的section的信息 

-l
--line-numbers 
用文件名和行号标注相应的目标代码,仅仅和-d、-D或者-r一起使用使用-ld和使用-d的区别不是很大,在源码级调试的时候有用,要求编译时使用了-g之类的调试编译选项。 

-m machine 
--architecture=machine 
指定反汇编目标文件时使用的架构,当待反汇编文件本身没描述架构信息的时候(比如S-records),这个选项很有用。可以用-i选项列出这里能够指定的架构. 

--reloc 
-r 
显示文件的重定位入口。如果和-d或者-D一起使用,重定位部分以反汇编后的格式显示出来。 

--dynamic-reloc 
-R 
显示文件的动态重定位入口,仅仅对于动态目标文件意义,比如某些共享库。 

-s 
--full-contents 
显示指定section的完整内容。默认所有的非空section都会被显示。 

-S 
--source 
尽可能反汇编出源代码,尤其当编译的时候指定了-g这种调试参数时,效果比较明显。隐含了-d参数。 

--show-raw-insn 
反汇编的时候,显示每条汇编指令对应的机器码,如不指定--prefix-addresses,这将是缺省选项。 

--no-show-raw-insn 
反汇编时,不显示汇编指令的机器码,如不指定--prefix-addresses,这将是缺省选项。 

--start-address=address 
从指定地址开始显示数据,该选项影响-d、-r和-s选项的输出。 

--stop-address=address 
显示数据直到指定地址为止,该项影响-d、-r和-s选项的输出。 

-t 
--syms 
显示文件的符号表入口。类似于nm -s提供的信息 

-T 
--dynamic-syms 
显示文件的动态符号表入口,仅仅对动态目标文件意义,比如某些共享库。它显示的信息类似于 nm -D|--dynamic 显示的信息。 

-V 
--version 
版本信息 

--all-headers 
-x 
显示所可用的头信息,包括符号表、重定位入口。-x 等价于-a -f -h -r -t 同时指定。 

-z 
--disassemble-zeroes 
一般反汇编输出将省略大块的零,该选项使得这些零块也被反汇编。 

@file 可以将选项集中到一个文件中,然后使用这个@file选项载入。

实例

首先,在给出后面大部分测试所基于的源代码以及编译指令。 源代码如下: 

[root@localhost test]# nl mytest.cpp 
void printTest() {
    char a;
    a = 'a';
}

void printTest2() {
int a = 2;
a+=2;
} 

对以上源代码进行编译,如下: 

[root@localhost test]# g++ -c -g mytest.cpp 

这里,生成的文件是mytest.o,为了方便测试包含了调试的信息,对可执行文件的测试,显示的结果类似。 

查看当前使用的objdump的版本号: 

[root@localhost test]# objdump -V 
GNU objdump 2.17.50.0.6-14.el5 20061020 
Copyright 2005 free Software Foundation, Inc. 
This program is free software; you may redistribute it under the terms of 
the GNU General Public License.  This program has absolutely no warranty. 

查看档案库文件中的信息: 

[root@localhost test]# objdump -a libmy2.a 
In archive libmy2.a: 
myfile.o:     file format elf32-i386 
rwxrwxrwx 0/0   2724 Nov 16 16:06 2009 myfile.o 
mytest.o:     file format elf32-i386 
rw-r--r-- 0/0    727 Jul 13 15:32 2011 mytest.o 

这里,libmy2.a是一个使用ar命令将多个*.o目标文件打包而生成的静态库。命令的输出类似ar -tv,相比较ar -tv输出如下: 

[root@localhost test]# ar -tv libmy2.a 
rwxrwxrwx 0/0   2724 Nov 16 16:06 2009 myfile.o 
rw-r--r-- 0/0    727 Jul 13 15:32 2011 mytest.o 

显示可用的架构和目标结构列表: 

[root@localhost test]# objdump -i 
BFD header file version 2.17.50.0.6-14.el5 20061020 
elf32-i386 
(header little endian, data little endian) 
  i386 
a.out-i386-linux 
(header little endian, data little endian) 
  i386 
efi-app-ia32 
(header little endian, data little endian) 
  i386 
elf64-x86-64 
(header little endian, data little endian) 
  i386 
elf64-little 
(header little endian, data little endian) 
  i386 
elf64-big 
(header big endian, data big endian) 
  i386 
elf32-little 
(header little endian, data little endian) 
  i386 
elf32-big 
(header big endian, data big endian) 
  i386 
srec 
(header endianness unknown, data endianness unknown) 
  i386 
symbolsrec 
(header endianness unknown, data endianness unknown) 
  i386 
tekhex 
(header endianness unknown, data endianness unknown) 
  i386 
binary 
(header endianness unknown, data endianness unknown) 
  i386 
ihex 
(header endianness unknown, data endianness unknown) 
  i386 
trad-core 
(header endianness unknown, data endianness unknown) 

              elf32-i386 a.out-i386-linux efi-app-ia32 elf64-x86-64 
          i386 elf32-i386 a.out-i386-linux efi-app-ia32 elf64-x86-64 

              elf64-little elf64-big elf32-little elf32-big srec symbolsrec 
          i386 elf64-little elf64-big elf32-little elf32-big srec symbolsrec 

              tekhex binary ihex trad-core 
          i386 tekhex binary ihex --------- 

这里,显示的信息是相对于 -b 或者 -m 选项可用的架构和目标格式列表。 

显示mytest.o文件中的text段的内容: 

[root@localhost test]# objdump --section=.text -s mytest.o 
mytest.o:     file format elf32-i386 
Contents of section .text: 
0000 5589e583 ec10c645 ff61c9c3 5589e583  U......E.a..U... 
0010 ec10c745 fc020000 008345fc 02c9c3    ...E......E.... 

这里注意,不能单独使用-j或者–section,例如objdump --section=.text mytest.o是不会运行成功的。 

反汇编mytest.o中的text段内容,并尽可能用源代码形式表示: 

[root@localhost test]# objdump -j .text -S mytest.o 
mytest.o:     file format elf32-i386 
Disassembly of section .text: 
00000000 <_Z9printTestv>: 
void printTest() 
   0:   55                      push   %ebp 
   1:   89 e5                   mov    %esp,%ebp 
   3:   83 ec 10                sub    $0x10,%esp 
{ 
        char a; 
        a = 'a'; 
   6:   c6 45 ff 61             movb   $0x61,0xffffffff(%ebp) 
} 
   a:   c9                      leave  
   b:   c3                      ret    

000000c <_Z10printTest2v>: 
void printTest2() 
   c:   55                      push   %ebp 
   d:   89 e5                   mov    %esp,%ebp 
   f:   83 ec 10                sub    $0x10,%esp 
{ 
        int a = 2; 
  12:   c7 45 fc 02 00 00 00    movl   $0x2,0xfffffffc(%ebp) 
        a+=2; 
  19:   83 45 fc 02             addl   $0x2,0xfffffffc(%ebp) 
} 
  1d:   c9                      leave  
  1e:   c3                      ret    

这里注意,不能单独使用-j或者–section,例如objdump -j .text mytest.o是不会运行成功的。另外-S命令对于包含调试信息的目标文件,显示的效果比较好,如果编译时没有指定g++的-g选项,那么目标文件就不包含调试信息,那么显示效果就差多了。 

反汇编出mytest.o的源代码: 

[root@localhost test]# objdump -S mytest.o 
mytest.o:     file format elf32-i386 

Disassembly of section .text: 

00000000 <_Z9printTestv>: 
void printTest() 
   0:   55                      push   %ebp 
   1:   89 e5                   mov    %esp,%ebp 
   3:   83 ec 10                sub    $0x10,%esp 
{ 
        char a; 
        a = 'a'; 
   6:   c6 45 ff 61             movb   $0x61,0xffffffff(%ebp) 
} 
   a:   c9                      leave  
   b:   c3                      ret    

0000000c <_Z10printTest2v>: 
void printTest2() 
   c:   55                      push   %ebp 
   d:   89 e5                   mov    %esp,%ebp 
   f:   83 ec 10                sub    $0x10,%esp 
{ 
       int a = 2; 
  12:   c7 45 fc 02 00 00 00    movl   $0x2,0xfffffffc(%ebp) 
        a+=2; 
  19:   83 45 fc 02             addl   $0x2,0xfffffffc(%ebp) 
} 
  1d:   c9                      leave  
  1e:   c3                      ret    

这里,尤其当编译的时候指定了-g这种调试参数时,反汇编的效果比较明显。隐含了-d参数。 

显示文件的符号表入口: 

[root@localhost test]# objdump -t mytest.o 
mytest.o:     file format elf32-i386 

SYMBOL TABLE: 
00000000 l    df *ABS*  00000000 mytest.cpp 
00000000 l    d  .text  00000000 .text 
00000000 l    d  .data  00000000 .data 
00000000 l    d  .bss   00000000 .bss 
00000000 l    d  .debug_abbrev  00000000 .debug_abbrev 
00000000 l    d  .debug_info    00000000 .debug_info 
00000000 l    d  .debug_line    00000000 .debug_line 
00000000 l    d  .debug_frame   00000000 .debug_frame 
00000000 l    d  .debug_loc     00000000 .debug_loc 
00000000 l    d  .debug_pubnames        00000000 .debug_pubnames 
00000000 l    d  .debug_aranges 00000000 .debug_aranges 
00000000 l    d  .note.GNU-stack        00000000 .note.GNU-stack 
00000000 l    d  .comment       00000000 .comment 
00000000 g     F .text  0000000c _Z9printTestv 
00000000         *UND*  00000000 __gxx_personality_v0 
0000000c g     F .text  00000013 _Z10printTest2v 

这里,输出的信息类似nm -s命令的输出,相比较之下,nm命令的输出如下: 

[root@localhost test]# nm -s mytest.o 
0000000c T _Z10printTest2v 
00000000 T _Z9printTestv 
         U __gxx_personality_v0 

显示文件的符号表入口,将底层符号解码并表示成用户级别: 

[root@localhost test]# objdump -t -C mytest.o 
mytest.o:     file format elf32-i386 
SYMBOL TABLE: 
00000000 l    df *ABS*  00000000 mytest.cpp 
00000000 l    d  .text  00000000 .text 
00000000 l    d  .data  00000000 .data 
00000000 l    d  .bss   00000000 .bss 
00000000 l    d  .debug_abbrev  00000000 .debug_abbrev 
00000000 l    d  .debug_info    00000000 .debug_info 
00000000 l    d  .debug_line    00000000 .debug_line 
00000000 l    d  .debug_frame   00000000 .debug_frame 
00000000 l    d  .debug_loc     00000000 .debug_loc 
00000000 l    d  .debug_pubnames        00000000 .debug_pubnames 
00000000 l    d  .debug_aranges 00000000 .debug_aranges 
00000000 l    d  .note.GNU-stack        00000000 .note.GNU-stack 
00000000 l    d  .comment       00000000 .comment 
00000000 g     F .text  0000000c printTest() 
00000000         *UND*  00000000 __gxx_personality_v0 
0000000c g     F .text  00000013 printTest2() 

这里,和没-C相比,printTest2函数可读性增加了。 

反汇编目标文件的特定机器码段: 

[root@localhost test]# objdump -d mytest.o 
mytest.o:     file format elf32-i386 
Disassembly of section .text: 

00000000 <_Z9printTestv>: 
   0:   55                      push   %ebp 
   1:   89 e5                   mov    %esp,%ebp 
   3:   83 ec 10                sub    $0x10,%esp 
   6:   c6 45 ff 61             movb   $0x61,0xffffffff(%ebp) 
   a:   c9                      leave  
  b:   c3                      ret    

0000000c <_Z10printTest2v>: 
   c:   55                      push   %ebp 
   d:   89 e5                   mov    %esp,%ebp 
   f:   83 ec 10                sub    $0x10,%esp 
  12:   c7 45 fc 02 00 00 00    movl   $0x2,0xfffffffc(%ebp) 
  19:   83 45 fc 02             addl   $0x2,0xfffffffc(%ebp) 
  1d:   c9                      leave  
  1e:   c3                      ret    

这里,对text段的内容进行了反汇编。 

反汇编特定段,并将汇编代码对应的文件名称和行号对应上: 

[root@localhost test]# objdump -d -l mytest.o
mytest.o:     file format elf32-i386 
Disassembly of section .text: 

00000000 <_Z9printTestv>: 
_Z9printTestv(): 
/root/test/04_libraryTest/mytest.cpp:1 
   0:   55                      push   %ebp 
   1:   89 e5                   mov    %esp,%ebp 
   3:   83 ec 10                sub    $0x10,%esp 
/root/test/04_libraryTest/mytest.cpp:4 
   6:   c6 45 ff 61             movb   $0x61,0xffffffff(%ebp) 
/root/test/04_libraryTest/mytest.cpp:5 
   a:   c9                      leave  
   b:   c3                      ret    

0000000c <_Z10printTest2v>: 
_Z10printTest2v(): 
/root/test/04_libraryTest/mytest.cpp:6 
   c:   55                      push   %ebp 
   d:   89 e5                   mov    %esp,%ebp 
   f:   83 ec 10                sub    $0x10,%esp 
/root/test/04_libraryTest/mytest.cpp:8 
  12:   c7 45 fc 02 00 00 00    movl   $0x2,0xfffffffc(%ebp) 
/root/test/04_libraryTest/mytest.cpp:9 
  19:   83 45 fc 02             addl   $0x2,0xfffffffc(%ebp) 
/root/test/04_libraryTest/mytest.cpp:10 
  1d:   c9                      leave  
  1e:   c3                      ret    

这里,项"-d"从objfile中反汇编那些特定指令机器码的section,而使用"-l"指定用文件名和行号标注相应的目标代码,仅仅和-d、-D或者-r一起使用,使用-ld和使用-d的区别不是很大,在源码级调试的时候有用,要求编译时使用了-g之类的调试编译选项。 

显示目标文件各个段的头部摘要信息: 

[root@localhost test]# objdump -h mytest.o 
mytest.o:     file format elf32-i386 

Sections: 
Idx Name          Size      VMA       LMA       File off  Algn 
  0 .text         0000001f  00000000  00000000  00000034  2**2 
                  CONTENTS, ALLOC, LOAD, readonly, CODE 
  1 .data         00000000  00000000  00000000  00000054  2**2 
                  CONTENTS, ALLOC, LOAD, DATA 
  2 .bss          00000000  00000000  00000000  00000054  2**2 
                  ALLOC 
  3 .debug_abbrev 00000046  00000000  00000000  00000054  2**0 
                  CONTENTS, READONLY, DEBUGGING 
  4 .debug_info   000000ed  00000000  00000000  0000009a  2**0 
                  CONTENTS, RELOC, READONLY, DEBUGGING 
  5 .debug_line   0000003e  00000000  00000000  00000187  2**0 
                  CONTENTS, RELOC, READONLY, DEBUGGING 
  6 .debug_frame  00000044  00000000  00000000  000001c8  2**2 
                  CONTENTS, RELOC, READONLY, DEBUGGING 
  7 .debug_loc    00000058  00000000  00000000  0000020c  2**0 
                  CONTENTS, READONLY, DEBUGGING 
  8 .debug_pubnames 0000002f  00000000  00000000  00000264  2**0 
                  CONTENTS, RELOC, READONLY, DEBUGGING 
  9 .debug_aranges 00000020  00000000  00000000  00000293  2**0 
                  CONTENTS, RELOC, READONLY, DEBUGGING 
10 .comment      0000002e  00000000  00000000  000002b3  2**0 
                  CONTENTS, READONLY 
11 .note.GNU-stack 00000000  00000000  00000000  000002e1  2**0 
                  CONTENTS, READONLY 

这里,更多的内容参见man objdump中的这个选项。

 

readelf

readelf命令用来显示一个或者多个elf格式的目标文件的信息,可以通过它的选项来控制显示哪些信息。这里的elf-file(s)就表示那些被检查的文件。可以支持32位,64位的elf格式文件,也支持包含elf文件的文档(这里一般指的是使用ar命令将一些elf文件打包之后生成的例如lib*.a之类的“静态库”文件)。 

这个程序和objdump提供的功能类似,但是它显示的信息更为具体,并且它不依赖BFD库(BFD库是一个GNU项目,它的目标就是希望通过一种统一的接口来处理不同的目标文件),所以即使BFD库有什么bug存在的话也不会影响到readelf程序。 

运行readelf的时候,除了-v和-H之外,其它的选项必须有一个被指定。 

ELF文件类型

种类型的ELF文件:

  1. 可重定位文件:用户和其他目标文件一起创建可执行文件或者共享目标文件,例如lib*.a文件。 
  2. 可执行文件:用于生成进程映像,载入内存执行,例如编译好的可执行文件a.out。 
  3. 共享目标文件:用于和其他共享目标文件或者可重定位文件一起生成elf目标文件或者和执行文件一起创建进程映像,例如lib*.so文件。 

ELF文件作用:

ELF文件参与程序的连接(建立一个程序)和程序的执行(运行一个程序),所以可以从不同的角度来看待elf格式的文件: 

  1. 如果用于编译和链接(可重定位文件),则编译器和链接器将把elf文件看作是节头表描述的节的集合,程序头表可选。 
  2. 如果用于加载执行(可执行文件),则加载器则将把elf文件看作是程序头表描述的段的集合,一个段可能包含多个节,节头表可选。 
  3. 如果是共享文件,则两者都含有。 

ELF文件总体组成: 

elf文件头描述elf文件的总体信息。包括:系统相关,类型相关,加载相关,链接相关。 

  • 系统相关表示:elf文件标识的魔术数,以及硬件和平台等相关信息,增加了elf文件的移植性,使交叉编译成为可能。 
  • 类型相关就是前面说的那个类型。 
  • 加载相关:包括程序头表相关信息。 
  • 链接相关:节头表相关信息。 

选项

-a 
--all 显示全部信息,等价于 -h -l -S -s -r -d -V -A -I. 

-h 
--file-header 显示elf文件开始的文件头信息. 

-l 
--program-headers  
--segments 显示程序头(段头)信息(如果有的话)。 

-S 
--section-headers  
--sections 显示节头信息(如果有的话)。 

-g 
--section-groups 显示节组信息(如果有的话)。 

-t 
--section-details 显示节的详细信息(-S的)。 

-s 
--syms        
--symbols 显示符号表段中的项(如果有的话)。 

-e 
--headers 显示全部头信息,等价于: -h -l -S 

-n 
--notes 显示note段(内核注释)的信息。 

-r 
--relocs 显示可重定位段的信息。 

-u 
--unwind 显示unwind段信息。当前只支持IA64 ELF的unwind段信息。 

-d 
--dynamic 显示动态段的信息。 

-V 
--version-info 显示版本段的信息。 

-A 
--arch-specific 显示CPU构架信息。 

-D 
--use-dynamic 使用动态段中的符号表显示符号,而不是使用符号段。 

-x <number or name> 
--hex-dump=<number or name> 以16进制方式显示指定段内内容。number指定段表中段的索引,或字符串指定文件中的段名。 

-w[liaprmfFsoR] or 
--debug-dump[=line,=info,=abbrev,=pubnames,=aranges,=macro,=frames,=frames-interp,=str,=loc,=Ranges] 显示调试段中指定的内容。 

-I 
--histogram 显示符号的时候,显示bucket list长度的柱状图。 

-v 
--version 显示readelf的版本信息。 

-H 
--help 显示readelf所支持的命令行选项。 

-W 
--wide 宽行输出。 

@file 可以将选项集中到一个文件中,然后使用这个@file选项载入。 

实例

先给出如下例子:

1.对于可执行文件形式的elf格式文件:

1)查看可执行程序的源代码如下: 

[root@localhost test]$ cat main.cpp 
#include <iostream> 
using std::cout; 
using std::endl; 
void my_print(); 

int main(int argc, char *argv[]) 
{ 
        my_print(); 
        cout<<"hello!"<<endl; 
        return 0; 
} 

void  my_print() 
{ 
        cout<<"print!"<<endl; 
} 

2)编译如下: 

[root@localhost test]$ g++ main.cpp -o main 
[root@localhost test]$ g++ -g main.cpp -o main.debug 

3)编译之后,查看生成的文件: 

[root@localhost test]$ ls -l 
总计 64 
-rwxr-xr-x 1 quietheart quietheart  6700 07-07 18:04 main 
-rw-r--r-- 1 quietheart quietheart   201 07-07 18:02 main.cpp 
-rwxr-xr-x 1 quietheart quietheart 38932 07-07 18:04 main.debug 

这里,main.debug是带有调试信息的可执行文件,main是一般的可执行文件。 

2.对于库文件形式的elf格式文件:

1)查看库的源代码如下: 

//myfile.h 
#ifndef __MYFILE_H 
#define __MYFILE_H 
void printInfo(); 
#endif 

//myfile.cpp 
#include "myfile.h" 
#include <iostream> 
using std::cout; 
using std::endl; 
void printInfo() 
{ 
    cout<<"hello"<<endl; 
} 

2)编译如下: 

[root@localhost test]$ g++ -c myfile.cpp 
[root@localhost test]$ g++ -shared -fPCI -o libmy.so myfile.o 
[root@localhost test]$ ar -r libmy.a myfile.o 
ar: creating libmy.a 

3)编译之后,查看生成的文件: 

[root@localhost test]$ ls -l 

总计 44 

-rw-r--r-- 1 quietheart quietheart 2154 07-08 16:14 libmy.a 
-rwxr-xr-x 1 quietheart quietheart 5707 07-08 16:08 libmy.so 
-rwxr-xr-x 1 quietheart quietheart  117 07-08 16:06 myfile.cpp 
-rwxr-xr-x 1 quietheart quietheart   63 07-08 16:08 myfile.h 
-rw-r--r-- 1 quietheart quietheart 2004 07-08 16:08 myfile.o 
libmy.a  libmy.so  myfile.cpp  myfile.h  myfile.o 

这里,分别生成目标文件myfile.o,共享库文件libmy.so,和静态库文件libmy.a。 

基于以上可执行文件和库,这里给出一些常用的命令。 

读取可执行文件形式的elf文件头信息:

[root@localhost test]$ readelf -h main 
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:               0x8048580 
  Start of program headers:          52 (bytes into file) 
  Start of section headers:          3232 (bytes into file) 
  Flags:                             0x0 
  Size of this header:               52 (bytes) 
  Size of program headers:           32 (bytes) 
  Number of program headers:         8 
  Size of section headers:           40 (bytes) 
  Number of section headers:         29 
  Section header string table index: 26 

这里,可见可执行文件的elf文件,其类型为EXEC(可执行文件)。另外,含调试信息的"main.debug"和不含调试信息的"main"除了一些大小信息之外,其内容是一样的。并且由此可见文件的体系结构为Intel 80386。 

读取目标文件形式的elf文件头信息:

[root@localhost test]$ readelf -h myfile.o 
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:                              REL (Relocatable file) 
  Machine:                           Intel 80386 
  Version:                           0x1 
  Entry point address:               0x0 
  Start of program headers:          0 (bytes into file) 
  Start of section headers:          516 (bytes into file) 
  Flags:                             0x0 
  Size of this header:               52 (bytes) 
  Size of program headers:           0 (bytes) 
  Number of program headers:         0 
  Size of section headers:           40 (bytes) 
  Number of section headers:         15 
  Section header string table index: 12 

这里,可见目标文件的elf文件,其类型为REL(可重定位文件)。 

读取静态库文件形式的elf文件头信息:

[root@localhost test]$ readelf -h libmy.a 
File: libmy.a(myfile.o) 
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:                              REL (Relocatable file) 
  Machine:                           Intel 80386 
  Version:                           0x1 
  Entry point address:               0x0 
  Start of program headers:          0 (bytes into file) 
  Start of section headers:          516 (bytes into file) 
  Flags:                             0x0 
  Size of this header:               52 (bytes) 
  Size of program headers:           0 (bytes) 
  Number of program headers:         0 
  Size of section headers:           40 (bytes) 
  Number of section headers:         15 
  Section header string table index: 12 

这里,可见静态库文件的elf文件,其类型为REL(可重定位文件)。 

读取动态库文件形式的elf文件头信息:

[root@localhost test]$ readelf -h libmy.so 
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:                              DYN (Shared object file) 
  Machine:                           Intel 80386 
  Version:                           0x1 
  Entry point address:               0x550 
  Start of program headers:          52 (bytes into file) 
  Start of section headers:          2768 (bytes into file) 
  Flags:                             0x0 
  Size of this header:               52 (bytes) 
  Size of program headers:           32 (bytes) 
  Number of program headers:         5 
  Size of section headers:           40 (bytes) 
  Number of section headers:         27 
  Section header string table index: 24 

这里,可见动态库文件的elf文件,其类型为DYN(共享目标文件)。 

查看可执行的elf文件程序头表信息:

[root@localhost test]$ readelf -l main 
Elf file type is EXEC (Executable file) 
Entry point 0x8048580 
There are 8 program headers, starting at offset 52 

Program Headers: 
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align 
  PHDR           0x000034 0x08048034 0x08048034 0x00100 0x00100 R E 0x4 
  INTERP         0x000134 0x08048134 0x08048134 0x00013 0x00013 R   0x1 
      [Requesting program interpreter: /lib/ld-linux.so.2] 
  LOAD           0x000000 0x08048000 0x08048000 0x00970 0x00970 R E 0x1000 
  LOAD           0x000970 0x08049970 0x08049970 0x00130 0x001c8 RW  0x1000 
  DYNAMIC        0x000988 0x08049988 0x08049988 0x000e0 0x000e0 RW  0x4 
  NOTE           0x000148 0x08048148 0x08048148 0x00020 0x00020 R   0x4 
  GNU_EH_FRAME   0x000820 0x08048820 0x08048820 0x00044 0x00044 R   0x4 
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x4 

Section to Segment mapping: 
  Segment Sections... 
   00     
   01     .interp 
   02     .interp .note.ABI-tag .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rel.dyn .rel.plt .init .plt .text .fini .rodata .eh_frame_hdr .eh_frame 
   03     .ctors .dtors .jcr .dynamic .got .got.plt .data .bss 
   04     .dynamic 
   05     .note.ABI-tag 
   06     .eh_frame_hdr 
   07     

这里,含调试信息的"main.debug"和不含调试信息的"main"其内容是一样的。 

查看目标文件的elf文件程序头表信息: 

[root@localhost test]$ readelf -l myfile.o 
There are no program headers in this file. 

这里可知,可重定位的目标文件,它没程序头表。 

查看静态库文件的elf文件程序头表信息:

[root@localhost test]$ readelf -l libmy.a 
File: libmy.a(myfile.o) 
There are no program headers in this file. 

这里可知,可重定位的静态库文件,它没程序头表。 

查看动态库文件的elf文件程序头表信息:

[root@localhost test]$ readelf -l libmy.so 
Elf file type is DYN (Shared object file) 
Entry point 0x550 
There are 5 program headers, starting at offset 52 

Program Headers: 
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align 
  LOAD           0x000000 0x00000000 0x00000000 0x007f4 0x007f4 R E 0x1000 
  LOAD           0x0007f4 0x000017f4 0x000017f4 0x0011c 0x00128 RW  0x1000 
  DYNAMIC        0x000810 0x00001810 0x00001810 0x000e0 0x000e0 RW  0x4 
  GNU_EH_FRAME   0x000738 0x00000738 0x00000738 0x0002c 0x0002c R   0x4 
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x4 

Section to Segment mapping: 
  Segment Sections... 
   00     .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rel.dyn .rel.plt .init .plt .text .fini .rodata .eh_frame_hdr .eh_frame 
   01     .ctors .dtors .jcr .data.rel.ro .dynamic .got .got.plt .bss 
   02     .dynamic 
   03     .eh_frame_hdr 
   04     

这里可知,做为共享目标文件的动态库,它程序头表。 

查看一个可执行的elf文件的节信息:

[root@localhost test]$ readelf -S main 
There are 29 section headers, starting at offset 0xca0: 
Section Headers: 
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al 
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0 
  [ 1] .interp           PROGBITS        08048134 000134 000013 00   A  0   0  1 
  [ 2] .note.ABI-tag     NOTE            08048148 000148 000020 00   A  0   0  4 
  [ 3] .gnu.hash         GNU_HASH        08048168 000168 000030 04   A  4   0  4 
  [ 4] .dynsym           DYNSYM          08048198 000198 0000d0 10   A  5   1  4 
  [ 5] .dynstr           STRTAB          08048268 000268 000183 00   A  0   0  1 
  [ 6] .gnu.version      VERSYM          080483ec 0003ec 00001a 02   A  4   0  2 
  [ 7] .gnu.version_r    VERNEED         08048408 000408 000060 00   A  5   2  4 
  [ 8] .rel.dyn          REL             08048468 000468 000010 08   A  4   0  4 
  [ 9] .rel.plt          REL             08048478 000478 000048 08   A  4  11  4 
  [10] .init             PROGBITS        080484c0 0004c0 000017 00  AX  0   0  4 
  [11] .plt              PROGBITS        080484d8 0004d8 0000a0 04  AX  0   0  4 
  [12] .text             PROGBITS        08048580 000580 000268 00  AX  0   0 16 
  [13] .fini             PROGBITS        080487e8 0007e8 00001c 00  AX  0   0  4 
  [14] .rodata           PROGBITS        08048804 000804 00001a 00   A  0   0  4 
  [15] .eh_frame_hdr     PROGBITS        08048820 000820 000044 00   A  0   0  4 
  [16] .eh_frame         PROGBITS        08048864 000864 00010c 00   A  0   0  4 
  [17] .ctors            PROGBITS        08049970 000970 00000c 00  WA  0   0  4 
  [18] .dtors            PROGBITS        0804997c 00097c 000008 00  WA  0   0  4 
  [19] .jcr              PROGBITS        08049984 000984 000004 00  WA  0   0  4 
  [20] .dynamic          DYNAMIC         08049988 000988 0000e0 08  WA  5   0  4 
  [21] .got              PROGBITS        08049a68 000a68 000004 04  WA  0   0  4 
  [22] .got.plt          PROGBITS        08049a6c 000a6c 000030 04  WA  0   0  4 
  [23] .data             PROGBITS        08049a9c 000a9c 000004 00  WA  0   0  4 
  [24] .bss              NOBITS          08049aa0 000aa0 000098 00  WA  0   0  8 
  [25] .comment          PROGBITS        00000000 000aa0 000114 00      0   0  1 
  [26] .shstrtab         STRTAB          00000000 000bb4 0000e9 00      0   0  1 
  [27] .symtab           SYMTAB          00000000 001128 000510 10     28  53  4 
  [28] .strtab           STRTAB          00000000 001638 0003f4 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) 

这里,main是可执行文件,不含调试信息。 

查看一个包含调试信息的可执行的elf文件的节信息:

[root@localhost test]$ readelf -S main.debug 
There are 37 section headers, starting at offset 0x88c8: 

Section Headers: 
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al 
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0 
  [ 1] .interp           PROGBITS        08048134 000134 000013 00   A  0   0  1 
  [ 2] .note.ABI-tag     NOTE            08048148 000148 000020 00   A  0   0  4 
  [ 3] .gnu.hash         GNU_HASH        08048168 000168 000030 04   A  4   0  4 
  [ 4] .dynsym           DYNSYM          08048198 000198 0000d0 10   A  5   1  4 
  [ 5] .dynstr           STRTAB          08048268 000268 000183 00   A  0   0  1 
  [ 6] .gnu.version      VERSYM          080483ec 0003ec 00001a 02   A  4   0  2 
  [ 7] .gnu.version_r    VERNEED         08048408 000408 000060 00   A  5   2  4 
  [ 8] .rel.dyn          REL             08048468 000468 000010 08   A  4   0  4 
  [ 9] .rel.plt          REL             08048478 000478 000048 08   A  4  11  4 
  [10] .init             PROGBITS        080484c0 0004c0 000017 00  AX  0   0  4 
  [11] .plt              PROGBITS        080484d8 0004d8 0000a0 04  AX  0   0  4 
  [12] .text             PROGBITS        08048580 000580 000268 00  AX  0   0 16 
  [13] .fini             PROGBITS        080487e8 0007e8 00001c 00  AX  0   0  4 
  [14] .rodata           PROGBITS        08048804 000804 00001a 00   A  0   0  4 
  [15] .eh_frame_hdr     PROGBITS        08048820 000820 000044 00   A  0   0  4 
  [16] .eh_frame         PROGBITS        08048864 000864 00010c 00   A  0   0  4 
  [17] .ctors            PROGBITS        08049970 000970 00000c 00  WA  0   0  4 
  [18] .dtors            PROGBITS        0804997c 00097c 000008 00  WA  0   0  4 
  [19] .jcr              PROGBITS        08049984 000984 000004 00  WA  0   0  4 
  [20] .dynamic          DYNAMIC         08049988 000988 0000e0 08  WA  5   0  4 
  [21] .got              PROGBITS        08049a68 000a68 000004 04  WA  0   0  4 
  [22] .got.plt          PROGBITS        08049a6c 000a6c 000030 04  WA  0   0  4 
  [23] .data             PROGBITS        08049a9c 000a9c 000004 00  WA  0   0  4 
  [24] .bss              NOBITS          08049aa0 000aa0 000098 00  WA  0   0  8 
  [25] .comment          PROGBITS        00000000 000aa0 000114 00      0   0  1 
  [26] .debug_aranges    PROGBITS        00000000 000bb4 000020 00      0   0  1 
  [27] .debug_pubnames   PROGBITS        00000000 000bd4 000028 00      0   0  1 
  [28] .debug_info       PROGBITS        00000000 000bfc 0067aa 00      0   0  1 
  [29] .debug_abbrev     PROGBITS        00000000 0073a6 000726 00      0   0  1 
  [30] .debug_line       PROGBITS        00000000 007acc 0003e1 00      0   0  1 
  [31] .debug_frame      PROGBITS        00000000 007eb0 00009c 00      0   0  4 
  [32] .debug_str        PROGBITS        00000000 007f4c 000735 00      0   0  1 
  [33] .debug_loc        PROGBITS        00000000 008681 0000f3 00      0   0  1 
  [34] .shstrtab         STRTAB          00000000 008774 000151 00      0   0  1 
  [35] .symtab           SYMTAB          00000000 008e90 000590 10     36  61  4 
  [36] .strtab           STRTAB          00000000 009420 0003f4 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) 

可见,相对非调试版本的可执行文件,多了".debug*"段的信息。 

查看一个目标文件的elf文件的节信息:

[root@localhost test]$ readelf -S myfile.o 
There are 15 section headers, starting at offset 0x204: 

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        00000000 000034 00009e 00  AX  0   0  4 
  [ 2] .rel.text         REL             00000000 000744 000060 08     13   1  4 
  [ 3] .data             PROGBITS        00000000 0000d4 000000 00  WA  0   0  4 
  [ 4] .bss              NOBITS          00000000 0000d4 000001 00  WA  0   0  4 
  [ 5] .ctors            PROGBITS        00000000 0000d4 000004 00  WA  0   0  4 
  [ 6] .rel.ctors        REL             00000000 0007a4 000008 08     13   5  4 
  [ 7] .rodata           PROGBITS        00000000 0000d8 000006 00   A  0   0  1 
  [ 8] .eh_frame         PROGBITS        00000000 0000e0 00008c 00   A  0   0  4 
  [ 9] .rel.eh_frame     REL             00000000 0007ac 000028 08     13   8  4 
  [10] .comment          PROGBITS        00000000 00016c 00002e 00      0   0  1 
  [11] .note.GNU-stack   PROGBITS        00000000 00019a 000000 00      0   0  1 
  [12] .shstrtab         STRTAB          00000000 00019a 00006a 00      0   0  1 
  [13] .symtab           SYMTAB          00000000 00045c 000180 10     14  14  4 
  [14] .strtab           STRTAB          00000000 0005dc 000166 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) 

查看一个静态库文件的elf文件的节信息:

[root@localhost test]$ readelf -S libmy.a 
File: libmy.a(myfile.o) 
There are 15 section headers, starting at offset 0x204: 

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        00000000 000034 00009e 00  AX  0   0  4 
  [ 2] .rel.text         REL             00000000 000744 000060 08     13   1  4 
  [ 3] .data             PROGBITS        00000000 0000d4 000000 00  WA  0   0  4 
  [ 4] .bss              NOBITS          00000000 0000d4 000001 00  WA  0   0  4 
  [ 5] .ctors            PROGBITS        00000000 0000d4 000004 00  WA  0   0  4 
  [ 6] .rel.ctors        REL             00000000 0007a4 000008 08     13   5  4 
  [ 7] .rodata           PROGBITS        00000000 0000d8 000006 00   A  0   0  1 
  [ 8] .eh_frame         PROGBITS        00000000 0000e0 00008c 00   A  0   0  4 
  [ 9] .rel.eh_frame     REL             00000000 0007ac 000028 08     13   8  4 
  [10] .comment          PROGBITS        00000000 00016c 00002e 00      0   0  1 
  [11] .note.GNU-stack   PROGBITS        00000000 00019a 000000 00      0   0  1 
  [12] .shstrtab         STRTAB          00000000 00019a 00006a 00      0   0  1 
  [13] .symtab           SYMTAB          00000000 00045c 000180 10     14  14  4 
  [14] .strtab           STRTAB          00000000 0005dc 000166 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) 

查看一个动态库文件的elf文件的节信息:

[root@localhost test]$ readelf -S libmy.so 
There are 27 section headers, starting at offset 0xad0: 

Section Headers: 
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al 
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0 
  [ 1] .gnu.hash         GNU_HASH        000000d4 0000d4 00003c 04   A  2   0  4 
  [ 2] .dynsym           DYNSYM          00000110 000110 000120 10   A  3   1  4 
  [ 3] .dynstr           STRTAB          00000230 000230 000199 00   A  0   0  1 
  [ 4] .gnu.version      VERSYM          000003ca 0003ca 000024 02   A  2   0  2 
  [ 5] .gnu.version_r    VERNEED         000003f0 0003f0 000050 00   A  3   2  4 
  [ 6] .rel.dyn          REL             00000440 000440 0000b0 08   A  2   0  4 
  [ 7] .rel.plt          REL             000004f0 0004f0 000010 08   A  2   9  4 
  [ 8] .init             PROGBITS        00000500 000500 000017 00  AX  0   0  4 
  [ 9] .plt              PROGBITS        00000518 000518 000030 04  AX  0   0  4 
  [10] .text             PROGBITS        00000550 000550 0001c4 00  AX  0   0 16 
  [11] .fini             PROGBITS        00000714 000714 00001c 00  AX  0   0  4 
  [12] .rodata           PROGBITS        00000730 000730 000006 00   A  0   0  1 
  [13] .eh_frame_hdr     PROGBITS        00000738 000738 00002c 00   A  0   0  4 
  [14] .eh_frame         PROGBITS        00000764 000764 000090 00   A  0   0  4 
  [15] .ctors            PROGBITS        000017f4 0007f4 00000c 00  WA  0   0  4 
  [16] .dtors            PROGBITS        00001800 000800 000008 00  WA  0   0  4 
  [17] .jcr              PROGBITS        00001808 000808 000004 00  WA  0   0  4 
  [18] .data.rel.ro      PROGBITS        0000180c 00080c 000004 00  WA  0   0  4 
  [19] .dynamic          DYNAMIC         00001810 000810 0000e0 08  WA  3   0  4 
  [20] .got              PROGBITS        000018f0 0008f0 00000c 04  WA  0   0  4 
  [21] .got.plt          PROGBITS        000018fc 0008fc 000014 04  WA  0   0  4 
  [22] .bss              NOBITS          00001910 000910 00000c 00  WA  0   0  4 
  [23] .comment          PROGBITS        00000000 000910 0000e6 00      0   0  1 
  [24] .shstrtab         STRTAB          00000000 0009f6 0000da 00      0   0  1 
  [25] .symtab           SYMTAB          00000000 000f08 000410 10     26  48  4 
  [26] .strtab           STRTAB          00000000 001318 000333 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) 

vdfuse

vdfuse命令是VirtualBox软件挂载VDI分区文件的一个工具,VirtualBox是一款能创建虚拟机的开源软件,vdi是它的默认磁盘格式。

什么是VirtualBox

VirtualBox是一款功能强大的x86虚拟机软件,它不仅具有丰富的特色,而且性能也很优异。更可喜的是,VirtualBox于数日前走向开源,成为了一个发布在GPL许可之下的自由软件。VirtualBox可以在Linux和Windows主机中运行,并支持在其中安装Windows (NT 4.0、2000、XP、Server 2003、Vista)、DOS/Windows 3.x、Linux (2.4 和 2.6)、OpenBSD等系列的客户操作系统。

在Ubuntu中安装vdfuse,打开终端,输入:

sudo apt-get install virtualbox-fuse

语法

vdfuse [options] -f image-file mountpoint

选项

-h 帮助
-r 只读
-t 类型 (VDI, VMDK, VHD, or raw; default: auto)
-f 镜像文件
-a 允许所有用户读取
-w 允许所有用户都写
-g 前台运行
-v 输出反馈
-d debug模式

注意:必须编辑一下/etc/fuse.confand,去掉 "user_allow_other" 前面的注释符号(#),否则不能正确运行。

实例

使用如下如下语句挂载.vdi文件:

sudo vdfuse -f /path/to/file.vdi /path/to/mountpoint

/path/to/mountpoint应该包含如下文件EntireDisk、Partition1等,如果只有一个文件,你可能需要这样挂载:

mount /path/to/mountpoint/Partition1 /path/to/someother/mountpoint

文件系统就挂载到/path/to/someother/mountpoint了。

rsync

rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。 rsync是一个功能非常强大的工具,其命令也有很多功能特色选项,我们下面就对它的选项一一进行分析说明。

语法

rsync [OPTION]... SRC DEST
rsync [OPTION]... SRC [USER@]host:DEST
rsync [OPTION]... [USER@]HOST:SRC DEST
rsync [OPTION]... [USER@]HOST::SRC DEST
rsync [OPTION]... SRC [USER@]HOST::DEST
rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]

对应于以上六种命令格式,rsync有六种不同的工作模式:

  1. 拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式。如:rsync -a /data /backup
  2. 使用一个远程shell程序(如rshssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包含单个冒号":"分隔符时启动该模式。如:rsync -avz *.c foo:src
  3. 使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径包含单个冒号":"分隔符时启动该模式。如:rsync -avz foo:src/bar /data
  4. 从远程rsync服务器中拷贝文件到本地机。当SRC路径信息包含"::"分隔符时启动该模式。如:rsync -av root@192.168.78.192::www /databack
  5. 从本地机器拷贝文件到远程rsync服务器中。当DST路径信息包含"::"分隔符时启动该模式。如:rsync -av /databack root@192.168.78.192::www
  6. 列远程机的文件列表。这类似于rsync传输,不过只要在命令中省略掉本地机信息即可。如:rsync -v rsync://192.168.78.192/www

选项

-v, --verbose 详细模式输出。
-q, --quiet 精简输出模式。
-c, --checksum 打开校验开关,强制对文件传输进行校验。
-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD。
-r, --recursive 对子目录以递归模式处理。
-R, --relative 使用相对路径信息。
-b, --backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用--suffix选项来指定不同的备份文件前缀。
--backup-dir 将备份文件(如~filename)存放在在目录下。
-suffix=SUFFIX 定义备份文件前缀。
-u, --update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件,不覆盖更新的文件。
-l, --links 保留软链结。
-L, --copy-links 想对待常规文件一样处理软链结。
--copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链结。
--safe-links 忽略指向SRC路径目录树以外的链结。
-H, --hard-links 保留硬链结。
-p, --perms 保持文件权限。
-o, --owner 保持文件属主信息。
-g, --group 保持文件属组信息。
-D, --devices 保持设备文件信息。
-t, --times 保持文件时间信息。
-S, --sparse 对稀疏文件进行特殊处理以节省DST的空间。
-n, --dry-run现实哪些文件将被传输。
-w, --whole-file 拷贝文件,不进行增量检测。
-x, --one-file-system 不要跨越文件系统边界。
-B, --block-size=SIZE 检验算法使用的块尺寸,默认是700字节。
-e, --rsh=command 指定使用rsh、ssh方式进行数据同步。
--rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息。
-C, --cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件。
--existing 仅仅更新那些已经存在于DST的文件,而不备份那些新创建的文件。
--delete 删除那些DST中SRC没有的文件。
--delete-excluded 同样删除接收端那些被该选项指定排除的文件。
--delete-after 传输结束以后再删除。
--ignore-errors 及时出现IO错误也进行删除。
--max-delete=NUM 最多删除NUM个文件。
--partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输。
--force 强制删除目录,即使不为空。
--numeric-ids 不将数字的用户和组id匹配为用户名和组名。
--timeout=time ip超时时间,单位为秒。
-I, --ignore-times 不跳过那些有同样的时间和长度的文件。
--size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间。
--modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0。
-T --temp-dir=DIR 在DIR中创建临时文件。
--compare-dest=DIR 同样比较DIR中的文件来决定是否需要备份。
-P 等同于 --partial。
--progress 显示备份过程。
-z, --compress 对备份的文件在传输时进行压缩处理。
--exclude=PATTERN 指定排除不需要传输的文件模式。
--include=PATTERN 指定不排除而需要传输的文件模式。
--exclude-from=FILE 排除FILE中指定模式的文件。
--include-from=FILE 不排除FILE指定模式匹配的文件。
--version 打印版本信息。
--address 绑定到特定的地址。
--config=FILE 指定其他的配置文件,不使用默认的rsyncd.conf文件。
--port=PORT 指定其他的rsync服务端口。
--blocking-io 对远程shell使用阻塞IO。
-stats 给出某些文件的传输状态。
--progress 在传输时现实传输过程。
--log-format=formAT 指定日志文件格式。
--password-file=FILE 从FILE中得到密码。
--bwlimit=KBPS 限制I/O带宽,KBytes per second。
-h, --help 显示帮助信息。

实例

SSH方式

首先在服务端启动ssh服务:

service sshd start
启动 sshd: [确定]

使用rsync进行同步

接下来就可以在客户端使用rsync命令来备份服务端上的数据了,SSH方式是通过系统用户来进行备份的,如下:

rsync -vzrtopg --progress -e ssh --delete work@172.16.78.192:/www/* /databack/experiment/rsync
work@172.16.78.192's password:
receiving file list ...
5 files to consider
test/
a
0 100% 0.00kB/s 527:35:41 (1, 20.0% of 5)
b
67 100% 65.43kB/s 0:00:00 (2, 40.0% of 5)
c
0 100% 0.00kB/s 527:35:41 (3, 60.0% of 5)
dd
100663296 100% 42.22MB/s 0:00:02 (4, 80.0% of 5)
sent 96 bytes received 98190 bytes 11563.06 bytes/sec
total size is 100663363 speedup is 1024.19

上面的信息描述了整个的备份过程,以及总共备份数据的大小。

后台服务方式

启动rsync服务,编辑/etc/xinetd.d/rsync文件,将其中的disable=yes改为disable=no,并重启xinetd服务,如下:

vi /etc/xinetd.d/rsync

#default: off
# description: The rsync server is a good addition to an ftp server, as it \
# allows crc checksumming etc.
service rsync {
disable = no
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
/etc/init.d/xinetd restart
停止 xinetd: [确定]
启动 xinetd: [确定]

创建配置文件,默认安装好rsync程序后,并不会自动创建rsync的主配置文件,需要手工来创建,其主配置文件为“/etc/rsyncd.conf”,创建该文件并插入如下内容:

vi /etc/rsyncd.conf

uid=root
gid=root
max connections=4
log file=/var/log/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
secrets file=/etc/rsyncd.passwd
hosts deny=172.16.78.0/22

[www]
comment= backup web
path=/www
read only = no
exclude=test
auth users=work

创建密码文件,采用这种方式不能使用系统用户对客户端进行认证,所以需要创建一个密码文件,其格式为“username:password”,用户名可以和密码可以随便定义,最好不要和系统帐户一致,同时要把创建的密码文件权限设置为600,这在前面的模块参数做了详细介绍。

echo "work:abc123" > /etc/rsyncd.passwd
chmod 600 /etc/rsyncd.passwd

备份,完成以上工作,现在就可以对数据进行备份了,如下:

rsync -avz --progress --delete work@172.16.78.192::www /databack/experiment/rsync

Password:
receiving file list ...
6 files to consider
./ files...
a
0 100% 0.00kB/s 528:20:41 (1, 50.0% of 6)
b
67 100% 65.43kB/s 0:00:00 (2, 66.7% of 6)
c
0 100% 0.00kB/s 528:20:41 (3, 83.3% of 6)
dd
100663296 100% 37.49MB/s 0:00:02 (4, 100.0% of 6)
sent 172 bytes received 98276 bytes 17899.64 bytes/sec
total size is 150995011 speedup is 1533.75

恢复,当服务器的数据出现问题时,那么这时就需要通过客户端的数据对服务端进行恢复,但前提是服务端允许客户端有写入权限,否则也不能在客户端直接对服务端进行恢复,使用rsync对数据进行恢复的方法如下:

rsync -avz --progress /databack/experiment/rsync/ work@172.16.78.192::www

Password:
building file list ...
6 files to consider
./
a
b
67 100% 0.00kB/s 0:00:00 (2, 66.7% of 6)
c
sent 258 bytes received 76 bytes 95.43 bytes/sec
total size is 150995011 speedup is 452080.87

ldconfig

ldconfig命令的用途主要是在默认搜寻目录/lib/usr/lib以及动态库配置文件/etc/ld.so.conf内所列的目录下,搜索出可共享的动态链接库(格式如lib*.so*),进而创建出动态装入程序(ld.so)所需的连接和缓存文件。缓存文件默认为/etc/ld.so.cache,此文件保存已排好序的动态链接库名字列表,为了让动态链接库为系统所共享,需运行动态链接库的管理命令ldconfig,此执行程序存放在/sbin目录下。

ldconfig通常在系统启动时运行,而当用户安装了一个新的动态链接库时,就需要手工运行这个命令。

语法

ldconfig [-v|--verbose] [-n] [-N] [-X] [-f CONF] [-C CACHE] [-r ROOT] [-l] [-p|--print-cache] [-c FORMAT] [--format=FORMAT] [-V] [-?|--help|--usage] path... 

选项

-v或--verbose:用此选项时,ldconfig将显示正在扫描的目录及搜索到的动态链接库,还有它所创建的连接的名字。
-n:用此选项时,ldconfig仅扫描命令行指定的目录,不扫描默认目录(/lib、/usr/lib),也不扫描配置文件/etc/ld.so.conf所列的目录。
-N:此选项指示ldconfig不重建缓存文件(/etc/ld.so.cache),若未用-X选项,ldconfig照常更新文件的连接。
-X:此选项指示ldconfig不更新文件的连接,若未用-N选项,则缓存文件正常更新。
-f CONF:此选项指定动态链接库的配置文件为CONF,系统默认为/etc/ld.so.conf。
-C CACHE:此选项指定生成的缓存文件为CACHE,系统默认的是/etc/ld.so.cache,此文件存放已排好序的可共享的动态链接库的列表。
-r ROOT:此选项改变应用程序的根目录为ROOT(是调用chroot函数实现的)。选择此项时,系统默认的配置文件/etc/ld.so.conf,实际对应的为ROOT/etc/ld.so.conf。如用-r /usr/zzz时,打开配置文件/etc/ld.so.conf时,实际打开的是/usr/zzz/etc/ld.so.conf文件。用此选项,可以大大增加动态链接库管理的灵活性。
-l:通常情况下,ldconfig搜索动态链接库时将自动建立动态链接库的连接,选择此项时,将进入专家模式,需要手工设置连接,一般用户不用此项。
-p或--print-cache:此选项指示ldconfig打印出当前缓存文件所保存的所有共享库的名字。
-c FORMAT 或 --format=FORMAT:此选项用于指定缓存文件所使用的格式,共有三种:old(老格式),new(新格式)和compat(兼容格式,此为默认格式)。
-V:此选项打印出ldconfig的版本信息,而后退出。
-? 或 --help 或 --usage:这三个选项作用相同,都是让ldconfig打印出其帮助信息,而后退出。

ldconfig几个需要注意的地方:

  1. /lib/usr/lib里面加东西,是不用修改/etc/ld.so.conf的,但是完了之后要调一下ldconfig,不然这个library会找不到。
  2. 想往上面两个目录以外加东西的时候,一定要修改/etc/ld.so.conf,然后再调用ldconfig,不然也会找不到。
  3. 比如安装了一个mysql/usr/local/mysql,mysql有一大堆library在/usr/local/mysql/lib下面,这时就需要在/etc/ld.so.conf下面加一行/usr/local/mysql/lib,保存过后ldconfig一下,新的library才能在程序运行时被找到。
  4. 如果想在这两个目录以外放lib,但是又不想在/etc/ld.so.conf中加东西(或者是没有权限加东西)。那也可以,就是export一个全局变量LD_LIBRARY_PATH,然后运行程序的时候就会去这个目录中找library。一般来讲这只是一种临时的解决方案,在没有权限或临时需要的时候使用。
  5. ldconfig做的这些东西都与运行程序时有关,跟编译时一点关系都没有。编译的时候还是该加-L就得加,不要混淆了。
  6. 总之,就是不管做了什么关于library的变动后,最好都ldconfig一下,不然会出现一些意想不到的结果。不会花太多的时间,但是会省很多的事。
  7. 再有,诸如libdb-4.3.so文件头中是会含有库名相关的信息的(即含“libdb-4.3.so”,可用strings命令察看),因此仅通过修改文件名以冒充某已被识别的库(如libdb-4.8.so)是行不通的。为此可在编译库的Makefile中直接修改配置信息,指定特别的库名。