Show me the money
分类: 嵌入式
2010-07-16 18:03:25
对于使用了-fpic选项的MIPS ELF,objdump的输出相当的不友好。所有的函数调用都是这样的形式:
740:
744:
|
完全看不出谁是谁。
还好,还有其它的工具来帮助我们理解xxxx(gp)代表什么
$mips-elf-gnu-objdump -A xxx.so
Attribute Section: gnu File Attributes
Tag_GNU_MIPS_ABI_FP: Soft float Primary GOT: Canonical
gp value: 00018d20 Reserved
entries:
Address Access Initial Purpose 00010d30
-32752(gp) 00000000 Lazy resolver 00010d34
-32748(gp) 80000000 Module pointer (GNU extension) Local
entries:
Address Access Initial 00010d38
-32744(gp) 00010000 00010d ... Global
entries:
Address Access Initial Sym.Val. Type Ndx Name 00010d54
-32716(gp) 00010d58
-32712(gp) ... |
还可以通过nm来帮助我们理解地址/符号名之间的对应关系:
$mips-elf-gnu-nm -D xxx.so
00010d w
_Jv_RegisterClasses 00010d w
__cxa_finalize w
__deregister_frame_info U
__errno_location w
__register_frame_info 00010d 00010db 00010d 00010d30 D _fdata 00000690 T _ftext 00018d 00000614 T _init ... |
有了这3个输出文件,就可以理解每个 jalr t9是调用哪个函数了。但是,在3个文件之前不同的切换/查找也是一件很麻烦的时候。还好,我们可以利用shell强大的文本处理功能,给objdump的输出打点补丁,使之更为友好。
#!/bin/bash if [ ! -f $1 ]; then exit 2 fi TMP_LST_FILE=~1.lst TMP_MAP_FILE=~2.map TMP_GP_FILE=~3.gp ${CROSS}objdump -d $1 > $TMP_LST_FILE
&& ${CROSS}readelf -A $1 > $TMP_GP_FILE && ${CROSS}nm -D $1 > $TMP_MAP_FILE && gawk ' FILENAME==ARGV[1] { symbols[$1]
= $3; } FILENAME==ARGV[2]{ if($1=="Local" && $2=="entries:") { flag =
1 next } else
if($1=="Global" && $2=="entries:") { flag =
2 next } if( $1 ) { if (
flag == 1 ) { if
( $ name
= symbols[$3]; else name
= $3 gp_local[$2]
= name } else
if( flag == 2 ) { gp_global[$2]
= $7 } } } FILENAME==ARGV[3] { n =
match($0, /[ls]w t9,(.+\(gp\))/, m); if(n >
0) { gp =
substr($0, m[1,"start"], m[1,"length"]); if( gp
in gp_local ) name
= gp_local[gp]; else
if ( gp in gp_global ) name
= gp_global[gp]; t9 =
name } if( name
!= "" ) printf("%s
/*PRC:%s*/\n", $0, name) else { if( $0
~ /jalr t9/ && t9 !=
"" ) { printf("%s
<%s>\n", $0, t9) t9
= "" } else print
$0 } name =
""; } ' $TMP_MAP_FILE $TMP_GP_FILE $TMP_LST_FILE
&& rm -f $TMP_MAP_FILE $TMP_GP_FILE $TMP_LST_FILE |
$./pic-mips-elf-objdump xx.so
... 8fc: 00000000
nop ... |
OH, YEAH!