1.分析可执行文件中包含的动态信息部分(对ELF文件来说就是.dynamic section)来决定该文件运行所需的依赖库;
2.定位和装载这些依赖库,分析这些依赖库所包含的动态信息部分,来决定是否需装载要任何附加的依赖库;
3.对动态库进行必要的重定位,在进程的执行期间绑定这些对象;
4.调用这些依赖库提供的初始化函数(ELF文件来说就是.init section,而且顺序是先执行依赖库的,再执行可执行文件的);
5.把控制权转交给应用程序;
6.在应用程序执行期间,能被再调用,来执行延后的函数绑定(即动态解析);
7.在应用程序调用dlopen(3C)打开动态库和用dlsym(3C)绑定这些库的符号时,也要被调用;
#include <stdio.h>
int main(int agrc, char *argv[])
{
printf ("hello world/n");
return 0;
}
# cc test.c -o test
# file test
test: ELF 32-bit LSB executable 80386 Version 1, dynamically linked, not stripped
# mdb test
> main::dis
main: pushl %ebp
main+1: movl %esp,%ebp
main+3: subl $0x10,%esp
main+6: movl %ebx,-0x8(%ebp)
main+9: movl %esi,-0xc(%ebp)
main+0xc: movl %edi,-0x10(%ebp)
main+0xf: pushl $0x80506ec
main+0x14: call -0x148 <PLT:printf>
main+0x19: addl $0x4,%esp
main+0x1c: movl $0x0,-0x4(%ebp)
main+0x23: jmp +0x5 <main+0x28>
main+0x28: movl -0x4(%ebp),%eax
main+0x2b: movl -0x8(%ebp),%ebx
main+0x2e: movl -0xc(%ebp),%esi
main+0x31: movl -0x10(%ebp),%edi
main+0x34: leave
main+0x35: ret
> 0x80506ec/s
0x80506ec: hello world
# /usr/ccs/bin/elfdump -c -N .rodata1 test
Section Header[13]: sh_name: .rodata1
sh_addr: 0x80506ec sh_flags: [ SHF_ALLOC ]
sh_size: 0xd sh_type: [ SHT_PROGBITS ]
sh_offset: 0x6ec sh_entsize: 0
sh_link: 0 sh_info: 0
sh_addralign: 0x4
> main+0x14:b程序在调用printf之前停止,我们计算一下printf的地址:
> :r
mdb: stop at main+0x14
mdb: target stopped at:
main+0x14: call -0x148 <PLT:printf>
> main+0x14-0x148=X
8050544
在test文件的.symtab和.dynsym section都可以找到符号表中包含printf,符号表实际上是一个数组,数组元素定义如下:
# /usr/ccs/bin/elfdump -s -N .symtab test | grep printf
[38] 0x08050544 0x00000000 FUNC GLOB D 0 UNDEF printf
# /usr/ccs/bin/elfdump -s -N .dynsym test | grep printf
[1] 0x08050544 0x00000000 FUNC GLOB D 0 UNDEF printf
typedef struct {
Elf32_Word st_name;
Elf32_Addr st_value;
Elf32_Word st_size;
unsigned char st_info;
unsigned char st_other;
Elf32_Half st_shndx;
} Elf32_Sym;
# /usr/ccs/bin/nm -x test | grep printfprintf的st_shndx的值是UNDEF,说明printf未在test中定义。既然程序可以链接通过,那么printf肯定存在于它依赖的共享 库中。
[Index] Value Size Type Bind Other Shndx Name
......
[38] |0x08050544|0x00000000|FUNC |GLOB |0 |UNDEF |printf
# ldd test当一个程序有多个共享库依赖时,runtime linker是按照一定的顺序运行各个库的.init函数的,即前面提到的步骤4,查看顺序用ldd -i:
libc.so.1 => /lib/libc.so.1
libm.so.2 => /lib/libm.so.2
# ldd -i /usr/bin/cptest依赖的库只有libc(3LIB)和libm(3LIB),libm是数学库,因此printf一定在libc(3LIB)中。我们知道,在 libc(3LIB)库中,包含了System V, ANSI C, POSIX等多种标准的函数实现。
libcmdutils.so.1 => /lib/libcmdutils.so.1
libavl.so.1 => /lib/libavl.so.1
libsec.so.1 => /lib/libsec.so.1
libc.so.1 => /lib/libc.so.1
libm.so.2 => /lib/libm.so.2
init object=/lib/libc.so.1
init object=/lib/libavl.so.1
init object=/lib/libcmdutils.so.1
init object=/lib/libsec.so.1
# /usr/ccs/bin/nm -x /usr/lib/libc.so | grep "|printf___FCKpd___13quot;libc.so中printf的st_value是0x00061f39,由于libc.so是一个共享库,因此这个地址只是printf在 libc.so中的偏移量,需要和libc.so的加载地址相加才可以得出真正的虚存地址,而这个地址才是真正的printf函数的代码入口。
[Index] Value Size Type Bind Other Shndx Name
......
[7653] |0x00061f39|0x00000105|FUNC |GLOB |0 |11 |printf
# /usr/ccs/bin/elfdump -c /usr/lib/libc.so | grep 11ELF文件test中的.symtab和.dynsym都包含了printf,而且st_value都相同,但是我们看到如果strip以后,nm命令没 有输出,这是因为test文件中的.symtab section被去除的原因:
Section Header[11]: sh_name: .text
sh_size: 0x110 sh_type: [ SHT_SUNW_SIGNATURE ]
# /usr/ccs/bin/strip test
# /usr/ccs/bin/elfdump -s -N .symtab test | grep printf
# /usr/ccs/bin/nm -x test1 | grep printf
# /usr/ccs/bin/elfdump -s -N .dynsym test | grep printf
[1] 0x08050544 0x00000000 FUNC GLOB D 0 UNDEF printf
> 8050544::dis
PLT:printf: jmp *0x8060714
PLT:printf: pushl $0x18
PLT:printf: jmp -0x4b <0x8050504>
PLT:_get_exit_frame_monitor: jmp *0x8060718
PLT:_get_exit_frame_monitor: pushl $0x20
PLT:_get_exit_frame_monitor: jmp -0x5b <0x8050504>
................
# /usr/ccs/bin/elfdump -c -N .got test
Section Header[14]: sh_name: .got
sh_addr: 0x80606fc sh_flags: [ SHF_WRITE SHF_ALLOC ]
sh_size: 0x20 sh_type: [ SHT_PROGBITS ]
sh_offset: 0x6fc sh_entsize: 0x4
sh_link: 0 sh_info: 0
sh_addralign: 0x4
# /usr/ccs/bin/elfdump -G test
Global Offset Table Section: .got (8 entries)
ndx addr value reloc addend symbol
[00000] 080606fc 0806071c R_386_NONE 00000000
[00001] 08060700 00000000 R_386_NONE 00000000
[00002] 08060704 00000000 R_386_NONE 00000000
[00003] 08060708 0805051a R_386_JMP_SLOT 00000000 atexit
[00004] 0806070c 0805052a R_386_JMP_SLOT 00000000 __fpstart
[00005] 08060710 0805053a R_386_JMP_SLOT 00000000 exit
[00006] 08060714 0805054a R_386_JMP_SLOT 00000000 printf
[00007] 08060718 0805055a R_386_JMP_SLOT 00000000 _get_exit_frame_monitor
而test运行到main+0x14断点处,查看GOT:
> 0x80606fc+20=X
806071c
> 0x80606fc,9/naX
0x80606fc:
0x80606fc: 806071c
0x8060700: d17fd900
0x8060704: d17cb260
0x8060708: d1710814
0x806070c: d1701e51
0x8060710: 805053a
0x8060714: 805054a
0x8060718: 805055a
0x806071c: 1
> 0x80606fc,9/nap
0x80606fc:
0x80606fc: 0x806071c --->未改变,.dynamic section的起始地址
0x8060700: 0xd17fd900 --->改变,Rt_map首地址,也是link_map首地址
0x8060704: ld.so.1`elf_rtbndr --->改变,Runtime linker的入口
0x8060708: libc.so.1`atexit --->改变,已经被ld.so解析成绝对地址
0x806070c: libc.so.1`_fpstart --->改变,已经被ld.so解析成绝对地址
0x8060710: PLT:exit --->未改变,还未解析,指向PLT:exit的第2条指令
0x8060714: PLT:printf --->未改变,还未解析,指向PLT:printf的第2条指令
0x8060718: PLT:_get_exit_frame_monitor --->未改变,还未解析,指向PLT:_get_exit_frame_monitor的第2条指令
0x806071c: 1
> _start::dis
_start: pushl $0x0
_start+2: pushl $0x0
_start+4: movl %esp,%ebp
_start+6: pushl %edx
_start+7: movl $0x806071c,%eax
_start+0xc: testl %eax,%eax
_start+0xe: je +0x7 <_start+0x15>
_start+0x10: call -0x64 <PLT:atexit>
_start+0x15: pushl $0x80506cc
_start+0x1a: call -0x6e <PLT:atexit>
_start+0x1f: leal 0x80607f4,%eax
_start+0x25: movl (%eax),%eax
_start+0x27: testl %eax,%eax
_start+0x29: je +0x17 <_start+0x40>
_start+0x2b: leal 0x80607f8,%eax
_start+0x31: movl (%eax),%eax
_start+0x33: testl %eax,%eax
_start+0x35: je +0xb <_start+0x40>
_start+0x37: pushl %eax
_start+0x38: call -0x8c <PLT:atexit>
_start+0x3d: addl $0x4,%esp
_start+0x40: movl 0x8(%ebp),%eax
_start+0x43: movl 0x80607d4,%edx
_start+0x49: testl %edx,%edx
_start+0x4b: jne +0xc <_start+0x57>
_start+0x4d: leal 0x10(%ebp,%eax,4),%edx
_start+0x51: movl %edx,0x80607d4
_start+0x57: andl $0xfffffff0,%esp
_start+0x5a: pushl %edx
_start+0x5b: leal 0xc(%ebp),%edx
_start+0x5e: movl %edx,0x80607f0
_start+0x64: pushl %edx
_start+0x65: pushl %eax
_start+0x66: call -0xaa <PLT:__fpstart>
_start+0x6b: call +0x29 <__fsr>
_start+0x70: call +0xd8 <_init>
_start+0x75: call +0x9b <main>
_start+0x7a: addl $0xc,%esp
_start+0x7d: pushl %eax
_start+0x7e: call -0xb2 <PLT:exit>
_start+0x83: pushl $0x0
_start+0x85: movl $0x1,%eax
_start+0x8a: lcall $0x7,$0x0
_start+0x91: hlt
# /usr/ccs/bin/elfdump -c -N .plt test
Section Header[8]: sh_name: .plt
sh_addr: 0x8050504 sh_flags: [ SHF_ALLOC SHF_EXECINSTR ]
sh_size: 0x60 sh_type: [ SHT_PROGBITS ]
sh_offset: 0x504 sh_entsize: 0x10
sh_link: 0 sh_info: 0
sh_addralign: 0x4
> 0x8050504+0x60=X根据.plt的起始和结束地址可以反汇编:
8050564
> 0x8050504::dis -a -n 13
8050504 pushl 0x8060700 ---->pushl got_plus_4,指向Rt_map地址
805050a jmp *0x8060704 ---->jmp *got_plus_8,跳转到Runtime linker的入口
8050510 addb %al,(%eax)
8050512 addb %al,(%eax)
8050514 jmp *0x8060708
805051a pushl $0x0
805051f jmp -0x1b <0x8050504>
8050524 jmp *0x806070c
805052a pushl $0x8
805052f jmp -0x2b <0x8050504>
8050534 jmp *0x8060710
805053a pushl $0x10
805053f jmp -0x3b <0x8050504>
8050544 jmp *0x8060714 ---->跳转到0x805054a,即下一条指令
805054a pushl $0x18
805054f jmp -0x4b <0x8050504>
8050554 jmp *0x8060718
805055a pushl $0x20
805055f jmp -0x5b <0x8050504>
8050564 addb %al,(%eax)
> 0x8050504::dis -n 13
0x8050504: pushl 0x8060700
0x805050a: jmp *0x8060704
0x8050510: addb %al,(%eax)
0x8050512: addb %al,(%eax)
PLT=libc.so.1`atexit: jmp *0x8060708
PLT=libc.so.1`atexit: pushl $0x0
PLT=libc.so.1`atexit: jmp -0x1b <0x8050504>
PLT=libc.so.1`_fpstart: jmp *0x806070c
PLT=libc.so.1`_fpstart: pushl $0x8
PLT=libc.so.1`_fpstart: jmp -0x2b <0x8050504>
PLT:exit: jmp *0x8060710
PLT:exit: pushl $0x10
PLT:exit: jmp -0x3b <0x8050504>
PLT:printf: jmp *0x8060714
PLT:printf: pushl $0x18
PLT:printf: jmp -0x4b <0x8050504>
PLT:_get_exit_frame_monitor: jmp *0x8060718
PLT:_get_exit_frame_monitor: pushl $0x20
PLT:_get_exit_frame_monitor: jmp -0x5b <0x8050504>
0x8050564: addb %al,(%eax)
> :s查看0x8060714即printf在GOT中的内容,其实就是PLT:printf中下一条push指令:
mdb: target stopped at:
PLT:printf: jmp *0x8060714
> *0x8060714=X
805054a
> *0x8060714::dis -n 1
PLT:printf: pushl $0x18
PLT:printf: jmp -0x4b <0x8050504>
# /usr/ccs/bin/elfdump -c -N .rel.plt test
Section Header[7]: sh_name: .rel.plt
sh_addr: 0x80504dc sh_flags: [ SHF_ALLOC SHF_INFO_LINK ]
sh_size: 0x28 sh_type: [ SHT_REL ]
sh_offset: 0x4dc sh_entsize: 0x8
sh_link: 3 sh_info: 8
sh_addralign: 0x4
# /usr/ccs/bin/elfdump -d test
Dynamic Section: .dynamic
index tag value
[0] NEEDED 0x111 libc.so.1
[1] INIT 0x80506b0
[2] FINI 0x80506cc
[3] HASH 0x80500e8
[4] STRTAB 0x805036c
[5] STRSZ 0x137
[6] SYMTAB 0x80501cc
[7] SYMENT 0x10
[8] CHECKSUM 0x5a2b
[9] VERNEED 0x80504a4
[10] VERNEEDNUM 0x1
[11] PLTRELSZ 0x28
[12] PLTREL 0x11
[13] JMPREL 0x80504dc ---> 重定位表.rel.plt的基地址
[14] REL 0x80504d4
[15] RELSZ 0x30
[16] RELENT 0x8
[17] DEBUG 0
[18] FEATURE_1 0x1 [ PARINIT ]
[19] FLAGS 0 0
[20] FLAGS_1 0 0
[21] PLTGOT 0x80606fc
# /usr/ccs/bin/elfdump -r test
Relocation Section: .rel.data
type offset section with respect to
R_386_32 0x80607f8 .rel.data __1cG__CrunMdo_exit_code6F_v_
Relocation Section: .rel.plt
type offset section with respect to
R_386_JMP_SLOT 0x8060708 .rel.plt atexit
R_386_JMP_SLOT 0x806070c .rel.plt __fpstart
R_386_JMP_SLOT 0x8060710 .rel.plt exit
R_386_JMP_SLOT 0x8060714 .rel.plt printf
R_386_JMP_SLOT 0x8060718 .rel.plt _get_exit_frame_monitor
typedef struct {因此,printf在重定位表中偏移量=(4-1)*8=24,即16进制的0x18。
Elf32_Addr r_offset;
Elf32_Word r_info;
} Elf32_Rel;
> 0x80504dc,a/nap
0x80504dc:
0x80504dc: 0x8060708
0x80504e0: 0xf07
0x80504e4: 0x806070c
0x80504e8: 0x1007
0x80504ec: 0x8060710
0x80504f0: 0x1207
0x80504f4: 0x8060714
0x80504f8: 0x107
0x80504fc: 0x8060718
0x8050500: 0x1307
继续单步执行:
> :s
mdb: target stopped at:
PLT:printf: pushl $0x18
> :s地址0x8050504就是PLT0的地址:
mdb: target stopped at:
PLT:printf: jmp -0x4b <0x8050504>
0x8060700就是GOT[1],存储的就是Rt_map的首地址,相当于把Rt_map的首地址压栈:
> :s
mdb: target stopped at:
0x8050504: pushl 0x8060700
0x8060704就是GOT[2],存储着runtime linker - ld.so的入口地址:
> :s
mdb: target stopped at:
0x805050a: jmp *0x8060704
> :s可以看到,这样控制权就由PLT这样转换到runtime linker了,显然,下面将进入runtime link editor来动态绑定了,我们查看目前栈的状态:
mdb: target stopped at:
ld.so.1`elf_rtbndr: pushl %ebp
> <esp,10/nap
0x804734c:
0x804734c: 0xd17fd900 ----> Rt_map的首地址
0x8047350: 0x18 ----> printf对应项重定位表中的偏移量
0x8047354: main+0x19 ----> printf返回后应跳转的地址
0x8047358: 0x80506ec
0x804735c: 0x8047460
0x8047360: 0x8047354
0x8047364: 0xd17fb840
0x8047368: 0x8047460
0x804736c: 0x804738c
0x8047370: _start+0x7a
0x8047374: 1
0x8047378: 0x8047398
0x804737c: 0x80473a0
0x8047380: _start+0x1f
0x8047384: _fini
0x8047388: ld.so.1`atexit_fini
288 #if defined(lint)
289
290 extern unsigned long elf_bndr(Rt_map *, unsigned long, caddr_t);
291
292 void
293 elf_rtbndr(Rt_map * lmp, unsigned long reloc, caddr_t pc)
294 {
295 (void) elf_bndr(lmp, reloc, pc);
296 }
297
298 #else
299 .globl elf_bndr
300 .globl elf_rtbndr
301 .weak _elf_rtbndr
302 _elf_rtbndr = elf_rtbndr / Make dbx happy
303 .type elf_rtbndr,@function
304 .align 4
305
306 elf_rtbndr:
307 pushl %ebp
308 movl %esp, %ebp
309 pushl %eax
310 pushl %ecx
311 pushl %edx
312 pushl 12(%ebp) / push pc
313 pushl 8(%ebp) / push reloc
314 pushl 4(%ebp) / push *lmp
315 call elf_bndr@PLT / call the C binder code
316 addl $12, %esp / pop args
317 movl %eax, 8(%ebp) / store final destination
318 popl %edx
319 popl %ecx
320 popl %eax
321 movl %ebp, %esp
322 popl %ebp
323 addl $4,%esp / pop args
324 ret / invoke resolved function
325 .size elf_rtbndr, .-elf_rtbndr
326 #endif
290 extern unsigned long elf_bndr(Rt_map *, unsigned long, caddr_t);因此在elf_rtbndr的312-314这几行,实际上是为调用elf_bndr做传递参数的准备:
312 pushl 12(%ebp) / push返回地址 main+0x19根据32位x86的ABI,压栈顺序是从右到左,正好吻合elf_bndr的参数顺序和类型定义。
313 pushl 8(%ebp) / push重定位表的对应printf项的偏移量 0x18
314 pushl 4(%ebp) / push Rt_map的首地址,0xd17fd900
> ld.so.1`elf_rtbndr::dis下面检查ld.so.1`elf_bndr调用前栈的状况,可以看到,3个参数已经按顺序压入栈中:
ld.so.1`elf_rtbndr: pushl %ebp
ld.so.1`elf_rtbndr+1: movl %esp,%ebp
ld.so.1`elf_rtbndr+3: pushl %eax
ld.so.1`elf_rtbndr+4: pushl %ecx
ld.so.1`elf_rtbndr+5: pushl %edx
ld.so.1`elf_rtbndr+6: pushl 0xc(%ebp)
ld.so.1`elf_rtbndr+9: pushl 0x8(%ebp)
ld.so.1`elf_rtbndr+0xc: pushl 0x4(%ebp)
ld.so.1`elf_rtbndr+0xf: call +0x14c5d <ld.so.1`elf_bndr>
ld.so.1`elf_rtbndr+0x14: addl $0xc,%esp
ld.so.1`elf_rtbndr+0x17: movl %eax,0x8(%ebp)
ld.so.1`elf_rtbndr+0x1a: popl %edx
ld.so.1`elf_rtbndr+0x1b: popl %ecx
ld.so.1`elf_rtbndr+0x1c: popl %eax
ld.so.1`elf_rtbndr+0x1d: movl %ebp,%esp
ld.so.1`elf_rtbndr+0x1f: popl %ebp
ld.so.1`elf_rtbndr+0x20: addl $0x4,%esp
ld.so.1`elf_rtbndr+0x23: ret
> ld.so.1`elf_rtbndr+0xf:b
> :c
mdb: stop at ld.so.1`elf_rtbndr+0xf
mdb: target stopped at:
ld.so.1`elf_rtbndr+0xf: call +0x14c5d <ld.so.1`elf_bndr>
> <esp,10/napelf_rtbndr会返回我们需要的printf在libc.so中的绝对地址吗?
0x8047330:
0x8047330: 0xd17fd900
0x8047334: 0x18
0x8047338: main+0x19
0x804733c: 3
0x8047340: libc.so.1`_sse_hw
0x8047344: libc.so.1`__flt_rounds
0x8047348: 0x804736c
0x804734c: 0xd17fd900
0x8047350: 0x18
0x8047354: main+0x19
0x8047358: 0x80506ec
0x804735c: 0x8047460
0x8047360: 0x8047354
0x8047364: 0xd17fb840
0x8047368: 0x8047460
0x804736c: 0x804738c
>
> ld.so.1`elf_rtbndr+0x14:b检查一下函数返回值,它应该存在rax的寄存器中:
> :c
mdb: stop at ld.so.1`elf_rtbndr+0x14
mdb: target stopped at:
ld.so.1`elf_rtbndr+0x14:addl $0xc,%esp
> <eax=X显然,d1741f39就是printf的绝对地址,它处于libc.so中:
d1741f39
> d1741f39::dis -w
libc.so.1`printf: pushl %ebp
libc.so.1`printf+1: movl %esp,%ebp
libc.so.1`printf+3: subl $0x10,%esp
libc.so.1`printf+6: andl $0xfffffff0,%esp
libc.so.1`printf+9: pushl %ebx
libc.so.1`printf+0xa: pushl %esi
libc.so.1`printf+0xb: pushl %edi
libc.so.1`printf+0xc: call +0x5 <libc.so.1`printf+0x11>
libc.so.1`printf+0x11: popl %ebx
libc.so.1`printf+0x12: addl $0x6d0b6,%ebx
libc.so.1`printf+0x18: movl 0x244(%ebx),%esi
> 0x80606fc,9/napprintf被成功解析后,ld.so修改了GOT[7],接着就应该把控制权转到libc的printf函数了。显然,在 ld.so.1`elf_rtbndr+0x17处的指令将会把eax寄存器中的printf的绝对函数地址存入栈中:
0x80606fc:
0x80606fc: 0x806071c
0x8060700: 0xd17fd900
0x8060704: ld.so.1`elf_rtbndr
0x8060708: libc.so.1`atexit
0x806070c: libc.so.1`_fpstart
0x8060710: PLT:exit
0x8060714: libc.so.1`printf
0x8060718: PLT:_get_exit_frame_monitor
0x806071c: 1
>
> ld.so.1`elf_rtbndr+0x17:b此时栈中还没有printf的地址:
> :c
mdb: stop at ld.so.1`elf_rtbndr+0x17
mdb: target stopped at:
ld.so.1`elf_rtbndr+0x17:movl %eax,0x8(%ebp)
> <esp,10/nap单步执行后,再观察栈,会发现,printf已经存入栈:
0x80473cc:
0x80473cc: 3
0x80473d0: libc.so.1`_sse_hw
0x80473d4: libc.so.1`__flt_rounds
0x80473d8: 0x80473fc
0x80473dc: 0xd17fd900
0x80473e0: 0x18
0x80473e4: main+0x19
0x80473e8: 0x80506ec
0x80473ec: 0x80474f4
0x80473f0: 0x80473e8
0x80473f4: 0xd17fb840
0x80473f8: 0x80474f4
0x80473fc: 0x8047420
0x8047400: _start+0x7a
0x8047404: 1
0x8047408: 0x804742c
> :s
mdb: target stopped at:
ld.so.1`elf_rtbndr+0x1a:popl %edx
> <esp,10/nap
0x80473cc:
0x80473cc: 3
0x80473d0: libc.so.1`_sse_hw
0x80473d4: libc.so.1`__flt_rounds
0x80473d8: 0x80473fc
0x80473dc: 0xd17fd900
0x80473e0: libc.so.1`printf
0x80473e4: main+0x19
0x80473e8: 0x80506ec
0x80473ec: 0x80474f4
0x80473f0: 0x80473e8
0x80473f4: 0xd17fb840
0x80473f8: 0x80474f4
0x80473fc: 0x8047420
0x8047400: _start+0x7a
0x8047404: 1
0x8047408: 0x804742c
> :s这样,控制权就由ld.so到了我们要调用的函数 - printf:
mdb: target stopped at:
ld.so.1`elf_rtbndr+0x23:ret
> <esp,10/nap
0x8047350:
0x8047350: libc.so.1`printf
0x8047354: main+0x19
0x8047358: 0x80506ec
0x804735c: 0x8047460
0x8047360: 0x8047354
0x8047364: 0xd17fb840
0x8047368: 0x8047460
0x804736c: 0x804738c
0x8047370: _start+0x7a
0x8047374: 1
0x8047378: 0x8047398
0x804737c: 0x80473a0
0x8047380: _start+0x1f
0x8047384: _fini
0x8047388: ld.so.1`atexit_fini
0x804738c: 0
> :s至此,一个完整的动态绑定过程结束,此时可以再次反汇编我们的main函数:
mdb: target stopped at:
libc.so.1`printf: pushl %ebp
> main::dis可以看到,由于GOT[7]已经存储了printf的绝对地址,因此,反汇编结果发生了变化。
main: pushl %ebp
main+1: movl %esp,%ebp
main+3: subl $0x10,%esp
main+6: movl %ebx,-0x8(%ebp)
main+9: movl %esi,-0xc(%ebp)
main+0xc: movl %edi,-0x10(%ebp)
main+0xf: pushl $0x80506ec
main+0x14: call -0x148 <PLT=libc.so.1`printf>
main+0x19: addl $0x4,%esp
main+0x1c: movl $0x0,-0x4(%ebp)
main+0x23: jmp +0x5 <main+0x28>
main+0x28: movl -0x4(%ebp),%eax
main+0x2b: movl -0x8(%ebp),%ebx
main+0x2e: movl -0xc(%ebp),%esi
main+0x31: movl -0x10(%ebp),%edi
main+0x34: leave
main+0x35: ret
>
main
|
V
PLT:printf的第1条指令<---GOT[7]指向的地址
| |
V |
PLT:printf的第2条指令<---------+
|
V
PLT:printf的第3条指令
|
V
PLT0
ld.so.1`elf_rtbndr
|
V
libc.so.1`printf
main
|
V
PLT:printf的第1条指令<---GOT[7]指向的地址
| |
V |
libc.so.1`printf<---------+
231 /*
232 * Use relocation entry to get symbol table entry and symbol name.
233 */
234 addr = (ulong_t)JMPREL(lmp);
235 rptr = (Rel *)(addr + reloff);
236 rsymndx = ELF_R_SYM(rptr->r_info);
237 sym = (Sym *)((ulong_t)SYMTAB(lmp) + (rsymndx * SYMENT(lmp)));
238 name = (char *)(STRTAB(lmp) + sym->st_name);
239
# /usr/ccs/bin/elfdump -d test
Dynamic Section: .dynamic
index tag value
[0] NEEDED 0x111 libc.so.1
[1] INIT 0x80506b0
[2] FINI 0x80506cc
[3] HASH 0x80500e8
[4] STRTAB 0x805036c --->STRTAB(lmp)的值,字符串表基地址
[5] STRSZ 0x137
[6] SYMTAB 0x80501cc --->SYMTAB(lmp)的值,符号表基地址
[7] SYMENT 0x10 --->SYMENT(lmp)的值,符号表元素的长度
[8] CHECKSUM 0x5a2b
[9] VERNEED 0x80504a4
[10] VERNEEDNUM 0x1
[11] PLTRELSZ 0x28
[12] PLTREL 0x11
[13] JMPREL 0x80504dc --->JMPREL(lmp)的值,重定位表基地址
[14] REL 0x80504d4
[15] RELSZ 0x30
[16] RELENT 0x8
[17] DEBUG 0
[18] FEATURE_1 0x1 [ PARINIT ]
[19] FLAGS 0 0
[20] FLAGS_1 0 0
[21] PLTGOT 0x80606fc
# mdb test因此rptr->r_offset=0x8060714,rptr->r_info=0x107,实际上这个rptr就指向 printf在重定位表中的相应项,而rptr->r_offset就对应着printf在GOT中的的地址,即GOT[7]地址。
> 0x80504dc,a/nap
0x80504dc:
0x80504dc: 0x8060708
0x80504e0: 0xf07
0x80504e4: 0x806070c
0x80504e8: 0x1007
0x80504ec: 0x8060710
0x80504f0: 0x1207
0x80504f4: 0x8060714
0x80504f8: 0x107
0x80504fc: 0x8060718
0x8050500: 0x1307
可见,根据给定符号对应的重定位表的偏移量,就可以找到该符号的符号表的记录,进而确定其名字字符串。
# mdb test
> 80501dc,2/nap
0x80501dc:
0x80501dc: 1 ---> sym->st_name
0x80501e0: PLT:printf ---> sym->st_value
> 0x805036d/s
0x805036d: printf ---> name的值
>
244 llmp = LIST(lmp)->lm_tail;
245
246 /*
247 * Find definition for symbol.
248 */
249 sl.sl_name = name;
250 sl.sl_cmap = lmp;
251 sl.sl_imap = LIST(lmp)->lm_head;
252 sl.sl_hash = 0;
253 sl.sl_rsymndx = rsymndx;
254 sl.sl_flags = LKUP_DEFT;
255
256 if ((nsym = lookup_sym(&sl, &nlmp, &binfo)) == 0) {
257 eprintf(ERR_FATAL, MSG_INTL(MSG_REL_NOSYM), NAME(lmp),
258 demangle(name));
259 rtldexit(LIST(lmp), 1);
260 }
261
775 typedef struct {
776 const char *sl_name; /* symbol name */
777 Rt_map *sl_cmap; /* callers link-map */
778 Rt_map *sl_imap; /* initial link-map to search */
779 ulong_t sl_hash; /* symbol hash value */
780 ulong_t sl_rsymndx; /* referencing reloc symndx */
781 uint_t sl_flags; /* lookup flags */
782 } Slookup;
783
64 typedef struct rt_map Rt_map;Rt_map的起始地址处定义了一个结构Link_map,它的定义如下:
459 struct rt_map {
460 /*
461 * BEGIN: Exposed to rtld_db - don't move, don't delete
462 */
463 Link_map rt_public; /* public data */
..................................................................................
485 struct fct *rt_fct; /* file class table for this object */
486 Sym *(*rt_symintp)(); /* link map symbol interpreter */
487 void *rt_priv; /* private data, object type specific */
488 Lm_list *rt_list; /* link map list we belong to */
..................................................................................
523 };
422 typedef struct link_map Link_map;可以看到实际上多个Rt_map是可以通过双向链表链接起来。
422 typedef struct link_map Link_map;
423
424 struct link_map {
425 unsigned long l_addr; /* address at which object is mapped */
426 char *l_name; /* full name of loaded object */
427 #ifdef _LP64
428 Elf64_Dyn *l_ld; /* dynamic structure of object */
429 #else
430 Elf32_Dyn *l_ld; /* dynamic structure of object */
431 #endif
432 Link_map *l_next; /* next link object */
433 Link_map *l_prev; /* previous link object */
434 char *l_refname; /* filters reference name */
435 };
> 0xd17fd900,20/napRt_map结构的成员rt_fct是指向struct fct结构的指针,struct fct结构定义如下:
0xd17fd900:
0xd17fd900: 0x8050000
0xd17fd904: 0x8047ff5
0xd17fd908: 0x806071c
0xd17fd90c: 0xd17fdd40
0xd17fd910: 0
0xd17fd914: 0
0xd17fd918: 0xd17fdbe8
0xd17fd91c: 0x8050000
0xd17fd920: 0x10820
0xd17fd924: 0x10820
0xd17fd928: 0x20421605
0xd17fd92c: 0x602
0xd17fd930: 0
0xd17fd934: 0xd17fdb78
0xd17fd938: 0
0xd17fd93c: 0
0xd17fd940: 0
0xd17fd944: 0
0xd17fd948: 0
0xd17fd94c: 0xd16d00d8
0xd17fd950: 0
0xd17fd954: 0
0xd17fd958: 0
0xd17fd95c: 0x80506f9
0xd17fd960: ld.so.1`elf_fct
0xd17fd964: ld.so.1`elf_find_sym
0xd17fd968: 0xd17fda00
0xd17fd96c: ld.so.1`lml_main
0xd17fd970: 0xffffffff
0xd17fd974: 0
0xd17fd978: 0
0xd17fd97c: 0x1901
71 typedef struct fct {
72 int (*fct_are_u_this)(Rej_desc *); /* determine type of object */
73 ulong_t (*fct_entry_pt)(void); /* get entry point */
74 Rt_map *(*fct_map_so)(Lm_list *, Aliste, const char *, const char *,
75 int); /* map in a shared object */
76 void (*fct_unmap_so)(Rt_map *); /* unmap a shared object */
77 int (*fct_needed)(Lm_list *, Aliste, Rt_map *);
78 /* determine needed objects */
79 Sym *(*fct_lookup_sym)(Slookup *, Rt_map **, uint_t *);
80 /* initialize symbol lookup */
81 int (*fct_reloc)(Rt_map *, uint_t); /* relocate shared object */
82 Pnode *fct_dflt_dirs; /* list of default dirs to */
83 /* search */
84 Pnode *fct_secure_dirs; /* list of secure dirs to */
85 /* search (set[ug]id) */
86 Pnode *(*fct_fix_name)(const char *, Rt_map *, uint_t);
87 /* transpose name */
88 char *(*fct_get_so)(const char *, const char *);
89 /* get shared object */
90 void (*fct_dladdr)(ulong_t, Rt_map *, Dl_info *, void **, int);
91 /* get symbolic address */
92 Sym *(*fct_dlsym)(Grp_hdl *, Slookup *, Rt_map **, uint_t *);
93 /* process dlsym request */
94 int (*fct_verify_vers)(const char *, Rt_map *, Rt_map *);
95 /* verify versioning (ELF) */
96 int (*fct_set_prot)(Rt_map *, int);
97 /* set protection */
98 } Fct;
> ld.so.1`elf_fct,10/nap与rt_fct类似的是Rt_map的另一个成员,rt_symintp,它实际上指向了真正的符号解析函数elf_find_sym:
ld.so.1`elf_fct:
ld.so.1`elf_fct:
ld.so.1`elf_fct:ld.so.1`elf_are_u
ld.so.1`elf_fct+4: ld.so.1`elf_entry_pt
ld.so.1`elf_fct+8: ld.so.1`elf_map_so
ld.so.1`elf_fct+0xc: ld.so.1`elf_unmap_so
ld.so.1`elf_fct+0x10: ld.so.1`elf_needed
ld.so.1`elf_fct+0x14: ld.so.1`lookup_sym
ld.so.1`elf_fct+0x18: ld.so.1`elf_reloc
ld.so.1`elf_fct+0x1c: ld.so.1`elf_dflt_dirs
ld.so.1`elf_fct+0x20: ld.so.1`elf_secure_dirs
ld.so.1`elf_fct+0x24: ld.so.1`elf_fix_name
ld.so.1`elf_fct+0x28: ld.so.1`elf_get_so
ld.so.1`elf_fct+0x2c: ld.so.1`elf_dladdr
ld.so.1`elf_fct+0x30: ld.so.1`dlsym_handle
ld.so.1`elf_fct+0x34: ld.so.1`elf_verify_vers
ld.so.1`elf_fct+0x38: ld.so.1`elf_set_prot
ld.so.1`elf_secure_dirs: ld.so.1`__rtld_msg+0x133e
....................................正是elf_find_sym,完成了真正的符号表查找工作。
0xd17fd964: ld.so.1`elf_find_sym
....................................
> 0xd17fd900,6/nap与可执行文件不同,共享库中并没有在ELF文件的.text section头中规定共享库的加载地址,而只是给出了相对地址,待被装载后才重新确定:
0xd17fd900:
0xd17fd900: 0x8050000 --->test加载地址
0xd17fd904: 0x8047ff5 --->Rt_map对应的二进制对象名,此处是test
0xd17fd908: 0x806071c
0xd17fd90c: 0xd17fdd40 --->后向指针,指向libc.so的link map
0xd17fd910: 0 --->前向指针,此处为NULL,表明是linkmap list的头
0xd17fd914: 0
> 0x8047ff5/s
0x8047ff5: test --->名字验证
> 0xd17fdd40,6/nap
0xd17fdd40:
0xd17fdd40: 0xd16e0000 --->libc.so加载地址
0xd17fdd44: 0xd17fdcd0 --->Rt_map对应的二进制对象名,此处是/lib/libc.so.1
0xd17fdd48: 0xd17afa3c
0xd17fdd4c: 0 ---->后向指针,是NULL,表明是linkmap list的尾
0xd17fdd50: 0xd17fd900 ---->前向指针,指向test的link map
0xd17fdd54: 0
> 0xd17fdcd0/s
0xd17fdcd0: /lib/libc.so.1
# /usr/ccs/bin/elfdump -c -N .text /usr/lib/libc.so而实际上,通过遍历linkmap list,ld.so可以确定所有linkmap list中的二进制对象的实际装载地址。
Section Header[11]: sh_name: .text
sh_addr: 0x1f370 sh_flags: [ SHF_ALLOC SHF_EXECINSTR ]
sh_size: 0x89895 sh_type: [ SHT_PROGBITS ]
sh_offset: 0x1f370 sh_entsize: 0
sh_link: 0 sh_info: 0
sh_addralign: 0x10
# pmap -x 1597同样的,共享库中符号表的st_value也不是该符号的绝对地址,而是偏移量,例如,libc.so中符号表中printf的取值是:
1597: test
Address Kbytes RSS Anon Locked Mode Mapped File
08046000 8 8 8 - rwx-- [ stack ]
08050000 4 4 - - r-x-- test
08060000 4 4 4 - rwx-- test
D16C0000 24 12 12 - rwx-- [ anon ]
D16D0000 4 4 4 - rwx-- [ anon ]
D16E0000 764 764 - - r-x-- libc.so.1
D17AF000 24 24 24 - rw--- libc.so.1
D17B5000 8 8 8 - rw--- libc.so.1
D17C8000 140 140 - - r-x-- ld.so.1
D17FB000 4 4 4 - rwx-- ld.so.1
D17FC000 8 8 8 - rwx-- ld.so.1
-------- ------- ------- ------- -------
total Kb 992 980 72 -
# /usr/ccs/bin/elfdump -s -N .dynsym /usr/lib/libc.so | grep " printf___FCKpd___70quot;那么,如果lookup_sym函数得到printf在libc.so中的符号表记录的指针,那么很容易计算得出printf的绝对地址。
[2416] 0x00061f39 0x00000105 FUNC GLOB D 34 .text printf
> 0xd16e0000+0x00061f39=X如果用mdb反汇编这个地址,d1741f39就是printf在libc.so的真正入口:
d1741f39
> d1741f39::dis -w前面我们遍历link map是从0xd17fd900开始的,这个地址指向的Rt_map节点碰巧是整个linkmap list的头节点。实际上,0xd17fd900指向的Rt_map的准确含义是调用者的link map,假设符号解析的调用是从共享库发出的,那么这个地址指向的Rt_map就未必是头节点了。
libc.so.1`printf: pushl %ebp
libc.so.1`printf+1: movl %esp,%ebp
libc.so.1`printf+3: subl $0x10,%esp
libc.so.1`printf+6: andl $0xfffffff0,%esp
libc.so.1`printf+9: pushl %ebx
libc.so.1`printf+0xa: pushl %esi
libc.so.1`printf+0xb: pushl %edi
libc.so.1`printf+0xc: call +0x5 <libc.so.1`printf+0x11>
libc.so.1`printf+0x11: popl %ebx
libc.so.1`printf+0x12: addl $0x6d0b6,%ebx
libc.so.1`printf+0x18: movl 0x244(%ebx),%esi
799 extern Lm_list lml_main; /* main's link map list */Lm_list定义如下:
239 typedef struct {这样,实际上通过rt_list->lm_head即可定位到进程的linkmap list的头节点了,elf_bndr函数就是这样做的:
240 /*
241 * BEGIN: Exposed to rtld_db - don't move, don't delete
242 */
243 Rt_map *lm_head; /* linked list pointers to active */
244 Rt_map *lm_tail; /* link-map list */
.....................................................................
263 } Lm_list;
250 sl.sl_cmap = lmp; --->指向调用者的Rt_map因此,要确定给定符号存在于哪一个依赖的共享库时,需要遍历所有linkmap list中的节点时,就需要使用sl.sl_imap。
251 sl.sl_imap = LIST(lmp)->lm_head; --->取得进程的link map list的头节点
# mdb test装载ld.so模块:
> main+0x14:b
> :c
mdb: stop at main+0x14
mdb: target stopped at:
main+0x14: call -0x148 <PLT:printf>
> ::load ld.so查看目前ld.so管理的所有Rt_map:
> ::Rt_maps
Link-map lists (dynlm_list): 0x8046368
----------------------------------------------
Lm_list: 0xd17fb220 (LM_ID_BASE)
----------------------------------------------
lmco rtmap ADDR() NAME()
----------------------------------------------
[0xc] 0xd17fd900 0x08050000 test
[0xc] 0xd17fdd40 0xd16e0000 /lib/libc.so.1
----------------------------------------------
Lm_list: 0xd17fb1e0 (LM_ID_LDSO)
----------------------------------------------
[0xc] 0xd17fd590 0xd17c8000 /lib/ld.so.1
> 0xd17fd900::Rt_maps -v
----------------------------------------------
Rt_map located at: 0xd17fd900
----------------------------------------------
NAME: test
PATHNAME: /export/home/personal/blog/test
ADDR: 0x08050000 DYN: 0x0806071c
NEXT: 0xd17fdd40 PREV: 0x00000000
FCT: 0xd17fb054 TLSMODID: 0
INIT: 0x00000000 FINI: 0x00000000
GROUPS: 0x00000000 HANDLES: 0x00000000
DEPENDS: 0xd16d00d8 CALLERS: 0x00000000
DYNINFO: 0xd17fda80 REFNAME:
RLIST: 0x00000000 RPATH:
LIST: 0xd17fb220 [ld.so.1`lml_main]
FLAGS: 0x20421605
[ ISMAIN,RELOCED,ANALYZED,INITDONE,FIXED,MODESET,INITCALL,INITCLCT ]
FLAGS1: 0x00000602
[ RELATIVE,NOINITFINI,USED ]
MODE: 0x00001901
[ LAZY,GLOBAL,WORLD,NODELETE ]
----------------------------------------------
Rt_map located at: 0xd17fdd40
----------------------------------------------
NAME: /lib/libc.so.1
ADDR: 0xd16e0000 DYN: 0xd17afa3c
NEXT: 0x00000000 PREV: 0xd17fd900
FCT: 0xd17fb054 TLSMODID: 0
INIT: 0xd1788c10 FINI: 0xd1788c30
GROUPS: 0x00000000 HANDLES: 0x00000000
DEPENDS: 0xd16d02e0 CALLERS: 0xd16d0120
DYNINFO: 0xd17fdee0 REFNAME:
RLIST: 0x00000000 RPATH:
LIST: 0xd17fb220 [ld.so.1`lml_main]
FLAGS: 0x20420604
[ RELOCED,ANALYZED,INITDONE,MODESET,INITCALL,INITCLCT ]
FLAGS1: 0x00004402
[ RELATIVE,USED,SYMSFLTR ]
MODE: 0x00001901
[ LAZY,GLOBAL,WORLD,NODELETE ]
> 0xd17fb220::Lm_list不难想象,顺序遍历linkmap list,查找当前库是否包含printf符号,如果包含就返回指向符号表记录的指针,这就是lookup_sym接下来要做的工作。
Lm_list: 0xd17fb220 (LM_ID_BASE)
----------------------------------------------
lists: 0xd17fd3f0 Alist[used 1: total 8]
----------------------------------------------
head: 0xd17fd900 tail: 0xd17fdd40 ---->可以看到,这里有link map list的头尾节点指针
audit: 0x00000000 preexec: 0xd17fdd40
handle: 0x00000000 obj: 2 init: 0 lazy: 0
flags: 0x00000821
[ BASELM,ENVIRON,STARTREL ]
tflags: 0x00000000
>
262 symval = nsym->st_value;symval即printf在libc.so的符号表的st_value。nlmp则返回包含printf的libc的指向Rt_map指针的指针。
263 if (!(FLAGS(nlmp) & FLG_RT_FIXED) &&
264 (nsym->st_shndx != SHN_ABS))
265 symval += ADDR(nlmp);
281 if (!(rtld_flags & RT_FL_NOBIND)) {在3.1小节,我们已经知道rptr->r_offset就对应着printf在GOT中的的地址,即GOT[7]地址。
282 addr = rptr->r_offset;
283 if (!(FLAGS(lmp) & FLG_RT_FIXED))最终,304行的语句会将printf的绝对地址存入GOT[7]中:
284 addr += ADDR(lmp);
285 if (((LIST(lmp)->lm_tflags | FLAGS1(lmp)) &
286 (LML_TFLG_AUD_PLTENTER | LML_TFLG_AUD_PLTEXIT)) &&
287 AUDINFO(lmp)->ai_dynplts) {
..............................................................................
..............................................................................
..............................................................................
299 } else {
300 /*
301 * Write standard PLT entry to jump directly
302 * to newly bound function.
303 */
304 *(ulong_t *)addr = symval;
305 }
306 }
# /usr/ccs/bin/elfdump -h testELF文件的.hash section提供了hash表本身,以及hash表元素的数目即nbuckets,每个hash表的bucket可能对应一个chain,chain的 每一个元素是下一个符号在字符串表中的索引,这样这个chain相当于一个字符串索引值组成的list。这样,给定一个符号名,通过ELF规范定义的 hash函数,可以求得一个bucket号,再根据bucket号,遍历其对应的chain,对比字符串,来查找符号:
Hash Section: .hash
bucket symndx name
0 [1] printf
1 [2] environ
[3] _PROCEDURE_LINKAGE_TABLE_
3 [4] _DYNAMIC
5 [5] _edata
[6] ___Argv
6 [7] _etext
[8] _init
7 [9] __fsr_init_value
9 [10] main
[11] _mcount
10 [12] _environ
11 [13] _GLOBAL_OFFSET_TABLE_
15 [14] _lib_version
16 [15] atexit
[16] __fpstart
18 [17] __fsr
[18] exit
[19] _get_exit_frame_monitor
19 [20] _end
[21] _start
21 [22] _fini
24 [23] __environ_lock
27 [24] __longdouble_used
28 [25] __1cG__CrunMdo_exit_code6F_v_
12 buckets contain 0 symbols
10 buckets contain 1 symbols
6 buckets contain 2 symbols
1 buckets contain 3 symbols
29 buckets 25 symbols (globals)
1. hn = elf_hash(sym_name) % nbuckets;利用mdb,我们可以得到完整的解析printf时的代码路径:
2. for (ndx = hash[ hn ]; ndx; ndx = chain[ ndx ]) {
3. symbol = sym_tab + ndx;
4. if (strcmp(sym_name, str_tab + symbol->st_name) == 0)
5. return (load_addr + symbol->st_value); }
bash-3.00# mdb testlookup_sym函数根据给定的符号名,通过hash函数算出其在hash表中的bucket号:
> main+0x14:b
> :c
mdb: stop at main+0x14
mdb: target stopped at:
main+0x14: call -0x148 <PLT:printf>
> ld.so.1`elf_find_sym::dis !grep strcmp
ld.so.1`elf_find_sym+0xbf: call +0x14e14 <ld.so.1`strcmp>
> ld.so.1`elf_find_sym+0xbf:b
> :c
mdb: stop at ld.so.1`elf_find_sym+0xbf
mdb: target stopped at:
ld.so.1`elf_find_sym+0xbf: call +0x14e14 <ld.so.1`strcmp>
> $c
ld.so.1`elf_find_sym+0xbf(80472e8, 80473ac, 80473b0)
ld.so.1`_lookup_sym+0x6e(d17fd900, 80472e8, 80473ac, 80473b0, c)
ld.so.1`lookup_sym+0x1d7(8047358, 80473ac, 80473b0)
ld.so.1`elf_bndr+0xf8(d17fd900, 18, 8050691)
ld.so.1`elf_rtbndr+0x14(18, 8050691, 80506ec, 80474f4, 80473e8, d17fb840)
0xd17fd900(1, 804742c, 8047434)
_start+0x7a(1, 804755c, 0, 8047561, 8047583, 8047597)
>
_lookup_sym中循环遍历了linkmap list,对每个依赖库调用了SYMINTP来解析符号:
2492 if (slp->sl_hash == 0)
2493 slp->sl_hash = elf_hash(name);
2438 for (; lmp; lmp = (Rt_map *)NEXT(lmp)) {如果是ELF文件,SYMINTP对应的则是elf_find_sym函数,它在给定ELF对象的指定bucket中的chain list来查找符号。
2439 if (callable(slp->sl_cmap, lmp, 0)) {
2440 Sym *sym;
2441
2442 slp->sl_imap = lmp;
2443 if ((sym = SYMINTP(lmp)(slp, dlmp, binfo)) != 0)
2444 return (sym);
2445 }
2446 }
#!/usr/sbin/dtrace -s运行dtrace脚本来观察每次elf_find_sym调用strcmp时的入口参数:
#pragma D option quiet
BEGIN
{
printf("Target pid: %d/n", $target);
}
pid$target::main:entry
{
self->main=1;
}
pid$target::main:return
{
self->main=0;
}
pid$target::elf_find_sym:entry
/self->main==1/
{
self->trace=1;
}
pid$target::elf_find_sym:return
/self->main==1 && self->trace==1 /
{
self->trace=0;
}
pid$target::strcmp:entry
/self->main==1 && self->trace==1 /
{
printf("/n%s`%s(%s,%s)/n", probemod, probefunc,copyinstr(arg0),copyinstr(arg1));
}
# ./test.d -c ./test
hello world
Target pid: 3934
LM1`ld.so.1`strcmp(rintf,rintf)
LM1`ld.so.1`strcmp(rintf,rintf)
LM1`ld.so.1`strcmp(edata,findbuf)
LM1`ld.so.1`strcmp(__Argv,findbuf)
..............................................
1869 if ((*strtabname++ != *name) || strcmp(strtabname, &name[1])) {
1870 if ((ndx = chainptr[ndx]) != 0)
1871 continue;
1872 return ((Sym *)0);
1873 }
1874
1869行代码是一个语言或表达式,首先比较两个字符串的首字符,如果不相等,则或表达式已经为真,接下来的strcmp就不会被执行。这样做,可以减低 符号查找时带来的调用strcmp的开销。