通过soinfo,查找函数表项,无法打印所有函数表项
<testso.c>#include <stdio.h>
void func1( void )
{
printf( "---------------------------function1\n");
}
void func2( void )
{
printf( "***************************function2\n");
}
这里定义了两个函数。在主函数里func=dlsym(handle,"func1"); func();是可以运行这两个函数的。
接下来查找func1表项。下面的函数里handle是打开testso.so的句柄,name=“func1”,addr是所求的func1的地址
void getaddr(void *handle,const char *name, void* addr)
{
if(!handle)
return;
Soinfo *si = (Soinfo*)handle;
printf("so name:%s\n",si->name);
printf("so base:%0x\n",si->base);
printf("so size:%0x\n",si->size);
Elf32_Sym *symtab = si->symtab;
const char *strtab = si->strtab;
Elf32_Rel *rel = si->plt_rel;
unsigned count = si->plt_rel_count;
unsigned idx;
printf("plt_rel:\n");
for(idx=0; idx<count; idx++)
{ //遍历输出所有表项
unsigned type = ELF32_R_TYPE(rel->r_info);
unsigned sym = ELF32_R_SYM(rel->r_info);
unsigned reloc = (unsigned)(rel->r_offset + si->base);
char *sym_name = (char *)(strtab + symtab[sym].st_name);
printf("idx:%d sym_name:%s\n",idx,sym_name);
if(type==R_ARM_GLOB_DAT && strcmp(sym_name, name)==0)
{
printf("find addr success\n");
addr=*((unsigned*)reloc);
printf("addr:%0x\n");
}
rel++;
}
}
打印结果为:
so name:testso.so
so base:80000000
so size:5000
plt_rel:
idx:0 sym_name:__cxa_begin_cleanup
idx:1 sym_name:memcpy
idx:2 sym_name:puts
idx:3 sym_name:__cxa_finalize
idx:4 sym_name:abort
idx:5 sym_name:__cxa_type_match
idx:6 sym_name:__cxa_atexit
idx:7 sym_name:__gnu_Unwind_Find_exidx
在这里没有sym_name为func1的项。
现在的问题是:rel-plt里存放的是所有函数的表项吗,如果是,那么为什么这里打印输出没有?如果不是,那么函数表项在哪里,如何获取? --------------------编程问答-------------------- 你打印的是重定位表项。和你需要的函数表项没有任何关系。
想获得所有函数,可以打印符号表项中类型为FUNC的表项即可。
--------------------编程问答-------------------- 补充说明:plt是调用的外部函数的入口,而rel.plt是这些plt函数的重定位表项。
你得找个elf的资料仔细研读,否则没法明白。 --------------------编程问答-------------------- 举个例子:下表是某个动态库x.so的符号表项列表,可以通过readelf -s x.so获得,其中类型为FUNC的的符号项目就是函数。
Symbol table '.dynsym' contains 81 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 00000000 0 NOTYPE LOCAL DEFAULT UND
1: 00000f30 0 SECTION LOCAL DEFAULT 7
2: 00009000 0 SECTION LOCAL DEFAULT 15
3: 00002360 36 FUNC GLOBAL DEFAULT 7 ___Unwind_ForcedUnwind
4: 00001fd0 164 FUNC GLOBAL DEFAULT 7 __gnu_Unwind_RaiseExcepti
5: 00009024 4 OBJECT GLOBAL DEFAULT 16 starzhu_e
6: 0000221c 0 FUNC GLOBAL DEFAULT 7 __gnu_Unwind_Save_VFP
7: 0000233c 36 FUNC GLOBAL DEFAULT 7 _Unwind_Resume_or_Rethrow
8: 00001c68 8 FUNC GLOBAL DEFAULT 7 __aeabi_unwind_cpp_pr0
9: 000027d4 44 FUNC GLOBAL DEFAULT 7 _Unwind_GetRegionStart
10: 0000233c 36 FUNC GLOBAL DEFAULT 7 ___Unwind_Resume_or_Rethr
11: 00009038 0 NOTYPE GLOBAL DEFAULT ABS _bss_end__
12: 00000f58 104 FUNC GLOBAL DEFAULT 7 Java_topstar_test_Topstar
13: 00002384 36 FUNC GLOBAL DEFAULT 7 _Unwind_Backtrace
14: 00000000 0 NOTYPE WEAK DEFAULT UND __cxa_begin_cleanup
15: 0000901c 4 OBJECT GLOBAL DEFAULT 15 starzhu_d
16: 00009018 4 OBJECT GLOBAL DEFAULT 15 starzhu_c
17: 00002200 20 FUNC GLOBAL DEFAULT 7 __restore_core_regs
18: 0000223c 0 FUNC GLOBAL DEFAULT 7 __gnu_Unwind_Save_VFP_D_1
19: 00000000 0 NOTYPE WEAK DEFAULT UND __cxa_call_unexpected
20: 00001fc8 8 FUNC GLOBAL DEFAULT 7 _Unwind_GetCFA
21: 00000000 0 FUNC GLOBAL DEFAULT UND memcpy
22: 00001854 68 FUNC GLOBAL DEFAULT 7 _Unwind_VRS_Set
23: 00000000 0 FUNC GLOBAL DEFAULT UND __cxa_finalize
24: 000011b8 96 FUNC GLOBAL DEFAULT 7 mymul
25: 00008e98 0 NOTYPE GLOBAL DEFAULT 12 __FINI_ARRAY__
26: 00009024 0 NOTYPE GLOBAL DEFAULT ABS __bss_start__
27: 00002140 192 FUNC GLOBAL DEFAULT 7 __gnu_Unwind_Backtrace
28: 0000222c 0 FUNC GLOBAL DEFAULT 7 __gnu_Unwind_Save_VFP_D
29: 00001c70 856 FUNC GLOBAL DEFAULT 7 _Unwind_VRS_Pop
30: 00001c58 8 FUNC WEAK DEFAULT 7 __aeabi_unwind_cpp_pr2
31: 00002384 36 FUNC GLOBAL DEFAULT 7 ___Unwind_Backtrace
32: 000022f4 36 FUNC GLOBAL DEFAULT 7 ___Unwind_RaiseException
33: 00000000 0 FUNC GLOBAL DEFAULT UND abort
34: 00002318 36 FUNC GLOBAL DEFAULT 7 ___Unwind_Resume
35: 00002244 0 FUNC GLOBAL DEFAULT 7 __gnu_Unwind_Restore_WMMX
36: 00001218 256 FUNC GLOBAL DEFAULT 7 mydiv
37: 00000fc0 120 FUNC GLOBAL DEFAULT 7 Java_topstar_test_Topstar
38: 00002214 0 FUNC GLOBAL DEFAULT 7 __gnu_Unwind_Restore_VFP
39: 00001318 0 FUNC GLOBAL DEFAULT 7 __aeabi_idiv
40: 00002318 36 FUNC GLOBAL DEFAULT 7 _Unwind_Resume
41: 00002120 32 FUNC GLOBAL DEFAULT 7 _Unwind_DeleteException
42: 0000211c 4 FUNC GLOBAL DEFAULT 7 _Unwind_Complete
43: 00009038 0 NOTYPE GLOBAL DEFAULT ABS __bss_end__
44: 00009028 15 OBJECT GLOBAL DEFAULT 16 starzhu_g
45: 00008e90 0 NOTYPE GLOBAL DEFAULT 11 __INIT_ARRAY__
46: 00002400 916 FUNC GLOBAL DEFAULT 7 __gnu_unwind_execute
47: 00002224 0 FUNC GLOBAL DEFAULT 7 __gnu_Unwind_Restore_VFP_
48: 00000000 0 NOTYPE WEAK DEFAULT UND __cxa_type_match
49: 00002090 108 FUNC GLOBAL DEFAULT 7 __gnu_Unwind_Resume
50: 00000000 0 FUNC GLOBAL DEFAULT UND __cxa_atexit
51: 00002234 0 FUNC GLOBAL DEFAULT 7 __gnu_Unwind_Restore_VFP_
52: 000022f4 36 FUNC GLOBAL DEFAULT 7 _Unwind_RaiseException
53: 00001414 16 FUNC WEAK DEFAULT 7 __aeabi_ldiv0
54: 00009024 0 NOTYPE GLOBAL DEFAULT ABS __bss_start
55: 00009038 0 NOTYPE GLOBAL DEFAULT ABS __end__
56: 00001318 220 FUNC GLOBAL DEFAULT 7 __divsi3
57: 00002074 28 FUNC GLOBAL DEFAULT 7 __gnu_Unwind_ForcedUnwind
58: 000020fc 32 FUNC GLOBAL DEFAULT 7 __gnu_Unwind_Resume_or_Re
59: 00000000 0 FUNC WEAK DEFAULT UND __gnu_Unwind_Find_exidx
60: 00002200 20 FUNC GLOBAL DEFAULT 7 restore_core_regs
61: 000022e0 0 FUNC GLOBAL DEFAULT 7 __gnu_Unwind_Save_WMMXC
62: 00009014 4 OBJECT GLOBAL DEFAULT 15 starzhu_b
63: 00002840 8 FUNC GLOBAL DEFAULT 7 _Unwind_GetTextRelBase
64: 00001038 336 FUNC GLOBAL DEFAULT 7 Java_topstar_test_Topstar
65: 00002800 56 FUNC GLOBAL DEFAULT 7 _Unwind_GetLanguageSpecif
66: 000017e8 68 FUNC GLOBAL DEFAULT 7 _Unwind_VRS_Get
67: 000022cc 0 FUNC GLOBAL DEFAULT 7 __gnu_Unwind_Restore_WMMX
68: 00002794 64 FUNC GLOBAL DEFAULT 7 __gnu_unwind_frame
69: 00002360 36 FUNC GLOBAL DEFAULT 7 _Unwind_ForcedUnwind
70: 00009024 0 NOTYPE GLOBAL DEFAULT ABS _edata
71: 00009038 0 NOTYPE GLOBAL DEFAULT ABS _end
72: 00001188 48 FUNC GLOBAL DEFAULT 7 myadd
73: 00002288 0 FUNC GLOBAL DEFAULT 7 __gnu_Unwind_Save_WMMXD
74: 00001414 16 FUNC WEAK DEFAULT 7 __aeabi_idiv0
75: 00001c60 8 FUNC WEAK DEFAULT 7 __aeabi_unwind_cpp_pr1
76: 00009020 4 OBJECT GLOBAL DEFAULT 15 starzhu_f
77: 00009010 4 OBJECT GLOBAL DEFAULT 15 starzhu_a
78: 00002838 8 FUNC GLOBAL DEFAULT 7 _Unwind_GetDataRelBase
79: 000013f4 32 FUNC GLOBAL DEFAULT 7 __aeabi_idivmod
80: 00000000 0 FUNC GLOBAL DEFAULT UND raise
补充:移动开发 , Android