在动态链接的应用程式或共享库中,ELF的程式头描述表具备一个PT_DYNAMIC类型的描述符,他指?br>隽?dynamic段的位置,dynamic段用来描述动态链接过程。当应用程式调用的共享库函数时,要通过.plt段进行跳转。plt段又称为过程连接表,他是连接器ld所生成的一组静态的trampline,是只读的可执行的段,包含在.text段一起映射到内存。plt每16个字节为一个槽位,plt的第1个槽位保留给动态解析器使用,其余的槽位表示对不同共享库函数的调用。plt依赖于全局偏移量表(.got段),GOT表是一可写的数据段,包含在.data段中一起映射到内存,用来存放共享符号的绝对地址。?br>τ贸淌降饔霉蚕砜夂褪峭ü齪lt槽位上的一条jmp指令跳转到GOT表所指的一个共享函数指针。这样,共享库的重定位就化为对GOT表项的重定位。GOT表的第1个指针指向.dynamic段,第2、3个?br>刚牒蚿lt段的第1个槽位对应,用来安装动态解析器。为了少做无用功,Linux采用了动态解析技术,就是说在加载共享库时,并不进行函数的解析,而是安装动态解析器,让共享库调用指向解析器,只有当函数调用发生时才进行解析。为此,在ld在生成可执行程式时,让其GOT共享函数指针指?br>蚋髯詐lt槽位上的两条指令,一条是pushl指令,将该函数所对应GOT重定位表的索引作为参数压入堆栈,然后通过另一条jmp指令跳转到plt槽位1,他再跳转到GOT表第3个指针所表示的动态解析器?br>肟凇U庋狈⑸⒊晒馕瞿勘旰诠蚕砜庵械牡刂肥保煤诔淌紾OT表中的指针就被实际的地址刷新。
下面是基于动态解释器ld.so-1.9.9版本的简单分析
简单的测试文档testso.c:
int x = 0;
int test()
{
return x;
}
用gcc -S -fPIC testso.c编绎成的汇编代码:
.globl x
.data
.align 4
.type x,@object
.size x,4
x:
.long 0
.text
.align 4
.globl test
.type test,@function
test:
pushl 雙
movl %esp,雙
pushl 離
call .L2
.L2:
popl 離 # 取标号.L2所在的地址
addl $_GLOBAL_OFFSET_TABLE_ [.-.L2],離 #_GLOBAL_OFFSET_TABLE_为当前地址到GOT表的偏移
movl x@GOT(離),陎 # ebx在-fPIC编绎的函数中用于指向本模块的GOT表
movl (陎),陎 # x@GOT表示符号x在GOT表中的索引
movl -4(雙),離
leave
ret
.Lfe1:
.size test,.Lfe1-test
用gcc -shared testso.s -o testso.so生成共享库的反汇编的有关输出:
Disassembly of section .plt:
00000258 <.plt>:
258: ff b3 04 00 00 pushl 0x4(離) #GOT表的第2个指针,对-fPIC编绎的函数,ebx总是指向GOT表
25d: 00
25e: ff a3 08 00 00 jmp *0x8(離) #跳转到GOT表的第3个指针,调用__dl_linux_resolover
263: 00
264: 00 00 addb %al,(陎)
266: 00 00 addb %al,(陎)
268: ff a3 0c 00 00 jmp *0xc(離) # 跳转到共享函数test()所在的GOT的指针
26d: 00
26e: 68 00 00 00 00 pushl x0 # test()所在GOT指针的初始入口
273: e9 e0 ff ff ff jmp 258 <_init 0x8> # 跳转到plt槽位1
Disassembly of section .text:
000002d8 :
2d8: 55 pushl 雙
2d9: 89 e5 movl %esp,雙
2db: 53 pushl 離
2dc: e8 00 00 00 00 call 2e1
2e1: 5b popl 離
2e2: 81 c3 ab 10 00 addl x10ab,離 # 取GOT表指针
2e7: 00
2e8: 8b 83 10 00 00 movl 0x10(離),陎 # 从GOT表中取变量x的地址
2ed: 00
2ee: 8b 00 movl (陎),陎
2f0: 8b 5d fc movl 0xfffffffc(雙),離
2f3: c9 leave
2f4: c3 ret
引用testso的应用程式文档test.c:
main()
{
printf("%d\n",test());
}
用gcc test.c testso.so -o test生成可执行文档的反汇编输出:
Disassembly of section .plt:
08048398 <.plt>:
8048398: ff 35 54 95 04 pushl 0x8049554 # GOT表的第2个指针
804839d: 08
804839e: ff 25 58 95 04 jmp *0x8049558 #GOT表的第3个指针,运行_dl_linux_resolver
80483a3: 08
80483a4: 00 00 addb %al,(陎)
80483a6: 00 00 addb %al,(陎)
80483a8: ff 25 5c 95 04 jmp *0x804955c # printf()在plt的入口
80483ad: 08
80483ae: 68 00 00 00 00 pushl x0
80483b3: e9 e0 ff ff ff jmp 8048398 <_init 0x8>
80483b8: ff 25 60 95 04 jmp *0x8049560
80483bd: 08
80483be: 68 08 00 00 00 pushl x8
80483c3: e9 d0 ff ff ff jmp 8048398 <_init 0x8>
80483c8: ff 25 64 95 04 jmp *0x8049564 # test()在plt段的调用点
80483cd: 08 # [0x8048564]初始时指向0x80483ce
80483ce: 68 10 00 00 00 pushl x10 # test()在GOT表重定位表.rel.got中的索引
80483d3: e9 c0 ff ff ff jmp 8048398 <_init 0x8> # 跳转到plt的第1槽位
80483d8: ff 25 68 95 04 jmp *0x8049568
80483dd: 08
80483de: 68 18 00 00 00 pushl x18
80483e3: e9 b0 ff ff ff jmp 8048398 <_init 0x8>
80483e8: ff 25 6c 95 04 jmp *0x804956c
80483ed: 08
80483ee: 68 20 00 00 00 pushl x20
80483f3: e9 a0 ff ff ff jmp 8048398 <_init 0x8>
80483f8: ff 25 70 95 04 jmp *0x8049570
80483fd: 08
80483fe: 68 28 00 00 00 pushl x28
8048403: e9 90 ff ff ff jmp 8048398 <_init 0x8>
Disassembly of section .text:
080484c8 :
80484c8: 55 pushl 雙
80484c9: 89 e5 movl %esp,雙
80484cb: e8 f8 fe ff ff call 80483c8 <_init 0x38> #
80484d0: 89 c0 movl 陎,陎
80484d2: 50 pushl 陎
80484d3: 68 38 85 04 08 pushl x8048538
80484d8: e8 cb fe ff ff call 80483a8 <_init 0x18>
下面是基于动态解释器ld.so-1.9.9版本的简单分析
简单的测试文档testso.c:
int x = 0;
int test()
{
return x;
}
用gcc -S -fPIC testso.c编绎成的汇编代码:
.globl x
.data
.align 4
.type x,@object
.size x,4
x:
.long 0
.text
.align 4
.globl test
.type test,@function
test:
pushl 雙
movl %esp,雙
pushl 離
call .L2
.L2:
popl 離 # 取标号.L2所在的地址
addl $_GLOBAL_OFFSET_TABLE_ [.-.L2],離 #_GLOBAL_OFFSET_TABLE_为当前地址到GOT表的偏移
movl x@GOT(離),陎 # ebx在-fPIC编绎的函数中用于指向本模块的GOT表
movl (陎),陎 # x@GOT表示符号x在GOT表中的索引
movl -4(雙),離
leave
ret
.Lfe1:
.size test,.Lfe1-test
用gcc -shared testso.s -o testso.so生成共享库的反汇编的有关输出:
Disassembly of section .plt:
00000258 <.plt>:
258: ff b3 04 00 00 pushl 0x4(離) #GOT表的第2个指针,对-fPIC编绎的函数,ebx总是指向GOT表
25d: 00
25e: ff a3 08 00 00 jmp *0x8(離) #跳转到GOT表的第3个指针,调用__dl_linux_resolover
263: 00
264: 00 00 addb %al,(陎)
266: 00 00 addb %al,(陎)
268: ff a3 0c 00 00 jmp *0xc(離) # 跳转到共享函数test()所在的GOT的指针
26d: 00
26e: 68 00 00 00 00 pushl x0 # test()所在GOT指针的初始入口
273: e9 e0 ff ff ff jmp 258 <_init 0x8> # 跳转到plt槽位1
Disassembly of section .text:
000002d8 :
2d8: 55 pushl 雙
2d9: 89 e5 movl %esp,雙
2db: 53 pushl 離
2dc: e8 00 00 00 00 call 2e1
2e1: 5b popl 離
2e2: 81 c3 ab 10 00 addl x10ab,離 # 取GOT表指针
2e7: 00
2e8: 8b 83 10 00 00 movl 0x10(離),陎 # 从GOT表中取变量x的地址
2ed: 00
2ee: 8b 00 movl (陎),陎
2f0: 8b 5d fc movl 0xfffffffc(雙),離
2f3: c9 leave
2f4: c3 ret
引用testso的应用程式文档test.c:
main()
{
printf("%d\n",test());
}
用gcc test.c testso.so -o test生成可执行文档的反汇编输出:
Disassembly of section .plt:
08048398 <.plt>:
8048398: ff 35 54 95 04 pushl 0x8049554 # GOT表的第2个指针
804839d: 08
804839e: ff 25 58 95 04 jmp *0x8049558 #GOT表的第3个指针,运行_dl_linux_resolver
80483a3: 08
80483a4: 00 00 addb %al,(陎)
80483a6: 00 00 addb %al,(陎)
80483a8: ff 25 5c 95 04 jmp *0x804955c # printf()在plt的入口
80483ad: 08
80483ae: 68 00 00 00 00 pushl x0
80483b3: e9 e0 ff ff ff jmp 8048398 <_init 0x8>
80483b8: ff 25 60 95 04 jmp *0x8049560
80483bd: 08
80483be: 68 08 00 00 00 pushl x8
80483c3: e9 d0 ff ff ff jmp 8048398 <_init 0x8>
80483c8: ff 25 64 95 04 jmp *0x8049564 # test()在plt段的调用点
80483cd: 08 # [0x8048564]初始时指向0x80483ce
80483ce: 68 10 00 00 00 pushl x10 # test()在GOT表重定位表.rel.got中的索引
80483d3: e9 c0 ff ff ff jmp 8048398 <_init 0x8> # 跳转到plt的第1槽位
80483d8: ff 25 68 95 04 jmp *0x8049568
80483dd: 08
80483de: 68 18 00 00 00 pushl x18
80483e3: e9 b0 ff ff ff jmp 8048398 <_init 0x8>
80483e8: ff 25 6c 95 04 jmp *0x804956c
80483ed: 08
80483ee: 68 20 00 00 00 pushl x20
80483f3: e9 a0 ff ff ff jmp 8048398 <_init 0x8>
80483f8: ff 25 70 95 04 jmp *0x8049570
80483fd: 08
80483fe: 68 28 00 00 00 pushl x28
8048403: e9 90 ff ff ff jmp 8048398 <_init 0x8>
Disassembly of section .text:
080484c8 :
80484c8: 55 pushl 雙
80484c9: 89 e5 movl %esp,雙
80484cb: e8 f8 fe ff ff call 80483c8 <_init 0x38> #
80484d0: 89 c0 movl 陎,陎
80484d2: 50 pushl 陎
80484d3: 68 38 85 04 08 pushl x8048538
80484d8: e8 cb fe ff ff call 80483a8 <_init 0x18>
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!



