ELF動(dòng)態(tài)解析符號(hào)過(guò)程(修訂版)
by alert7
2002-01-27
轉(zhuǎn)載自:http://elfhack.whitecell.org
★★ 前言
本篇文章以linux為平臺(tái)為例,演示ELF動(dòng)態(tài)解析符號(hào)的過(guò)程。
不正之處,還請(qǐng)斧正。
通常,ELF解析符號(hào)方式稱(chēng)為lazy MODE裝載的。這種裝載技術(shù)是ELF平臺(tái)上
默認(rèn)的方式。在不同的體系平臺(tái)在實(shí)現(xiàn)這種機(jī)制也是不同的。但是i386和SPARC
在大部分上是相同的。
動(dòng)態(tài)連接器(rtld)提供符號(hào)的動(dòng)態(tài)連接,裝載共享objects和解析標(biāo)號(hào)的引用。
通常是ld.so,它可以是一個(gè)共享object也可以是個(gè)可執(zhí)行的文件。
★★ 符號(hào)表(symbol table)
每個(gè)object要想使它對(duì)其他的ELF文件可用,就要用到符號(hào)表(symbol table)中
symbol entry.事實(shí)上,一個(gè)symbol entry 是個(gè)symbol結(jié)構(gòu),它描述了這個(gè)
symbol的名字和該symbol的value.symbol name被編碼作為dynamic string
table的索引(index). The value of a symbol是在ELF OBJECT文件內(nèi)該
symbol的地址。該地址通常需要被重新定位(加上該object裝載到內(nèi)存的基地址
(base load address)). 從而構(gòu)成該symbol在內(nèi)存中的絕對(duì)地址。
一個(gè)符號(hào)表入口有如下的格式:
typedef struct
{
Elf32_Word st_name; /* Symbol name (string tbl index) */
Elf32_Addr st_value; /* Symbol value */
Elf32_Word st_size; /* Symbol size */
unsigned char st_info; /* Symbol type and binding */
unsigned char st_other; /* No defined meaning, 0 */
Elf32_Section st_shndx; /* Section index */
} Elf32_Sym;
可執(zhí)行文件他們知道運(yùn)行時(shí)刻他們的地址,所以他們內(nèi)部的引用符號(hào)在編譯時(shí)候就已
經(jīng)被重定位了。
★★ GOT(global offset table)
GOT是一個(gè)數(shù)組,存在ELF image的數(shù)據(jù)段中,他們是一些指向objects的指針(通常
是數(shù)據(jù)objects).動(dòng)態(tài)連接器將重新修改那些編譯時(shí)還沒(méi)有確定下來(lái)地址的符號(hào)的
GOT入口。所以說(shuō)GOT在i386動(dòng)態(tài)連接中扮演著重要的角色。
★★ PLT(procedure linkage table)
PLT是一個(gè)這樣的結(jié)構(gòu),它的entries包含了一些代碼片段用來(lái)傳輸控制到外部的過(guò)程。
在i386體系下,PLT和他的代碼片段entries有如下格式:
PLT0:
push GOT[1] ; word of identifying information
jmp GOT[2] ; pointer to rtld function nop
...
PLTn:
jmp GOT[x + n] ; GOT offset of symbol address
push n ; relocation offset of symbol
jmp PLT0 ; call the rtld
PLTn + 1
jmp GOT[x +n +1]; GOT offset of symbol address
push n +1 ; relocation offset of symbol
jmp PLT0 ; call the rtld
當(dāng)傳輸控制到一個(gè)外部的函數(shù)時(shí),它傳輸執(zhí)行到PLT 中跟該symbol相關(guān)的那個(gè)entry
(是在編譯時(shí)候連接器安裝的)。在PLT entry中第一條指令將jump到一個(gè)存儲(chǔ)在GOT
中的一個(gè)指針地址;假如符號(hào)還沒(méi)有被解析,該GOT中存放著的是該P(yáng)LT entry中的
下一條指令地址。該指令push一個(gè)在重定位表中的偏移量到stack,然后下一條指令
傳輸控制到PLT[0]入口。該P(yáng)LT[0]包含了調(diào)用RTLD解析符號(hào)的函數(shù)代碼。該
解析符號(hào)函數(shù)地址由程序裝載器已經(jīng)插入到GOT[2]中了。
動(dòng)態(tài)連接器將展開(kāi)stack并且獲取需要解析符號(hào)在重定位表地址信息。重定位入口、
符號(hào)表和字符串表共同決定著PLT entry引用的那個(gè)符號(hào)和在進(jìn)程內(nèi)存中符號(hào)應(yīng)該
存放的地址。假如可能的話(huà),該符號(hào)將被解析出來(lái),它的地址將被存放在被該
PLT entry使用的GOT entry中。下一次該符號(hào)被請(qǐng)求時(shí),與之對(duì)應(yīng)的GOT已經(jīng)包
含了該符號(hào)的地址了。所以,所有后來(lái)的調(diào)用將直接通過(guò)GOT傳輸控制。動(dòng)態(tài)連接器
只解析第一次被二進(jìn)制文件所引用的符號(hào);這種引用方式就是我們上面所說(shuō)的
lazy MODE。
★★ 哈希表和鏈(hash table and chain)
除了符號(hào)表(symbol table),GOT(global offset table),PLT(procedure
linkage table),字符串表(string table),ELF objects還可以包含一個(gè)
hash table和chain(用來(lái)使動(dòng)態(tài)連接器解析符號(hào)更加容易)。hash table和chain
通常被用來(lái)迅速判定在符號(hào)表中哪個(gè)entry可能符合所請(qǐng)求的符號(hào)名。hash table(總
是伴隨著chain的)被作為整型數(shù)組存放。在hash表中,一半位置是留給那些buckets的,
另一半是留給在chain中的元素(element)的. hash table直接反映了symbol table
的元素?cái)?shù)目和他們的次序。
動(dòng)態(tài)連接器結(jié)構(gòu)提供了所有動(dòng)態(tài)連接的執(zhí)行是以透明方式訪(fǎng)問(wèn)動(dòng)態(tài)連接器.
然而,明確訪(fǎng)問(wèn)也是可用的。動(dòng)態(tài)連接(裝載共享objects和解析符號(hào)),
可以通過(guò)直接訪(fǎng)問(wèn)RTLD的那些函數(shù)來(lái)完成:dlopen() , dlsym() and
dlclose() .這些函數(shù)被包含在動(dòng)態(tài)連接器本身中。為了訪(fǎng)問(wèn)那些函數(shù),
連接時(shí)需要把動(dòng)態(tài)連接函數(shù)庫(kù)(libdl)連接進(jìn)去。該庫(kù)包含了一些stub函數(shù)
允許編譯時(shí)候連接器解析那些函數(shù)的引用;然而那些stub函數(shù)只簡(jiǎn)單的返回0。
因?yàn)槭聦?shí)上函數(shù)駐留在動(dòng)態(tài)連接器中,假如從靜態(tài)連接的ELF文件中調(diào)用
那些函數(shù),共享object的裝載將會(huì)失敗。
對(duì)于執(zhí)行動(dòng)態(tài)連接器所必須的是:hash table,hash table元素的數(shù)目,
chain,dynamic string table和dynamic symbol talbe。滿(mǎn)足了
這些條件,下面算法適用任何symbol的地址計(jì)算:
1. hn = elf_hash(sym_name) % nbuckets;
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); }
hash號(hào)是elf_hash()的返回值,在ELF規(guī)范的第4部分有定義,以hash table中元素
個(gè)數(shù)取模。該號(hào)被用來(lái)做hash table的下表索引,求得hash值,找出與之匹配的符號(hào)
名的chain的索引(line 3)。使用該索引,符號(hào)從符號(hào)表中獲得(line 3).比較獲得
的符號(hào)名和請(qǐng)求的符號(hào)名是否相同(line 5).使用這個(gè)算法,就可以簡(jiǎn)單解析任何符號(hào)了。
★★ 演示
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Hello, world\n");
return 0;
}
Relocation section '.rel.plt' at offset 0x278 contains 4 entries:
Offset Info Type Symbol's Value Symbol's Name
0804947c 00107 R_386_JUMP_SLOT 080482d8 __register_frame_info
08049480 00207 R_386_JUMP_SLOT 080482e8 __deregister_frame_info
08049484 00307 R_386_JUMP_SLOT 080482f8 __libc_start_main
08049488 00407 R_386_JUMP_SLOT 08048308 printf
只有R_386_JUMP_SLOT的才會(huì)出現(xiàn)在GOT中
Symbol table '.dynsym' contains 7 entries:
Num: Value Size Type Bind Ot Ndx Name
0: 0 0 NOTYPE LOCAL 0 UND
1: 80482d8 116 FUNC WEAK 0 UND __register_frame_info@GLIBC_2.0 (2)
2: 80482e8 162 FUNC WEAK 0 UND __deregister_frame_info@GLIBC_2.0 (
2)
3: 80482f8 261 FUNC GLOBAL 0 UND __libc_start_main@GLIBC_2.0 (2)
4: 8048308 41 FUNC GLOBAL 0 UND printf@GLIBC_2.0 (2)
5: 804843c 4 OBJECT GLOBAL 0 14 _IO_stdin_used
6: 0 0 NOTYPE WEAK 0 UND __gmon_start__
[alert7@redhat]$ gcc -o test test.c
[alert7@redhat]$ ./test
Hello, world
[alert7@redhat]$ objdump -x test
...
Dynamic Section:
NEEDED libc.so.6
INIT 0x8048298
FINI 0x804841c
HASH 0x8048128
STRTAB 0x80481c8
SYMTAB 0x8048158
STRSZ 0x70
SYMENT 0x10
DEBUG 0x0
PLTGOT 0x8049470
PLTRELSZ 0x20
PLTREL 0x11
JMPREL 0x8048278
REL 0x8048270
RELSZ 0x8
RELENT 0x8
VERNEED 0x8048250
VERNEEDNUM 0x1
VERSYM 0x8048242
...
7 .rel.got 00000008 08048270 08048270 00000270 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
8 .rel.plt 00000020 08048278 08048278 00000278 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
9 .init 0000002f 08048298 08048298 00000298 2**2
CONTENTS, ALLOC, LOAD, READONLY, CODE
10 .plt 00000050 080482c8 080482c8 000002c8 2**2
CONTENTS, ALLOC, LOAD, READONLY, CODE
11 .text 000000fc 08048320 08048320 00000320 2**4
CONTENTS, ALLOC, LOAD, READONLY, CODE
12 .fini 0000001a 0804841c 0804841c 0000041c 2**2
CONTENTS, ALLOC, LOAD, READONLY, CODE
13 .rodata 00000016 08048438 08048438 00000438 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
14 .data 0000000c 08049450 08049450 00000450 2**2
CONTENTS, ALLOC, LOAD, DATA
15 .eh_frame 00000004 0804945c 0804945c 0000045c 2**2
CONTENTS, ALLOC, LOAD, DATA
16 .ctors 00000008 08049460 08049460 00000460 2**2
CONTENTS, ALLOC, LOAD, DATA
17 .dtors 00000008 08049468 08049468 00000468 2**2
CONTENTS, ALLOC, LOAD, DATA
18 .got 00000020 08049470 08049470 00000470 2**2
CONTENTS, ALLOC, LOAD, DATA
19 .dynamic 000000a0 08049490 08049490 00000490 2**2
CONTENTS, ALLOC, LOAD, DATA
...
[alert7@redhat]$ gdb -q test
(gdb) disass main
Dump of assembler code for function main:
0x80483d0 <main>: push %ebp
0x80483d1 <main+1>: mov %esp,%ebp
0x80483d3 <main+3>: push $0x8048440
0x80483d8 <main+8>: call 0x8048308 <printf>
0x80483dd <main+13>: add $0x4,%esp
0x80483e0 <main+16>: xor %eax,%eax
0x80483e2 <main+18>: jmp 0x80483e4 <main+20>
0x80483e4 <main+20>: leave
0x80483e5 <main+21>: ret
...
0x80483ef <main+31>: nop
End of assembler dump.
(gdb) b * 0x80483d8
Breakpoint 1 at 0x80483d8
(gdb) r
Starting program: /home/alert7/test
Breakpoint 1, 0x80483d8 in main ()
(gdb) disass 0x8048308 ① ⑴
Dump of assembler code for function printf:
/****************************************/ //PLT4:
0x8048308 <printf>: jmp *0x8049488 //jmp GOT[6]
//此時(shí),GOT[6]中存在的是0x804830e
0x804830e <printf+6>: push $0x18 //$0x18為printf重定位入口在JMPREL section中的偏移量
0x8048313 <printf+11>: jmp 0x80482c8 <_init+48> //jmp PLT0
//PLT0處存放著調(diào)用RTLD函數(shù)的指令
//當(dāng)函數(shù)返回時(shí)候,把GOT[6]修改為真正的
//printf函數(shù)地址,然后直接跳到printf函數(shù)
//執(zhí)行。
該部分為PLT的一部分
/****************************************/
End of assembler dump.
(gdb) x 0x8049488
0x8049488 <_GLOBAL_OFFSET_TABLE_+24>: 0x0804830e
080482c8 <.plt>: ② //PLT0:
80482c8: ff 35 74 94 04 08 pushl 0x8049474 //pushl GOT[1]地址
//GOT[1]是一個(gè)鑒別信息,是link_map類(lèi)型的一個(gè)指針
80482ce: ff 25 78 94 04 08 jmp *0x8049478 //JMP GOT[2]
//跳到動(dòng)態(tài)連接器解析函數(shù)執(zhí)行
80482d4: 00 00 add %al,(%eax)
80482d6: 00 00 add %al,(%eax)
80482d8: ff 25 7c 94 04 08 jmp *0x804947c //PLT1:
80482de: 68 00 00 00 00 push $0x0
80482e3: e9 e0 ff ff ff jmp 80482c8 <_init+0x30>
80482e8: ff 25 80 94 04 08 jmp *0x8049480 //PLT2:
80482ee: 68 08 00 00 00 push $0x8
80482f3: e9 d0 ff ff ff jmp 80482c8 <_init+0x30>
80482f8: ff 25 84 94 04 08 jmp *0x8049484 //PLT3:
80482fe: 68 10 00 00 00 push $0x10
8048303: e9 c0 ff ff ff jmp 80482c8 <_init+0x30>
8048308: ff 25 88 94 04 08 jmp *0x8049488 //PLT4:
804830e: 68 18 00 00 00 push $0x18
8048313: e9 b0 ff ff ff jmp 80482c8 <_init+0x30>
(gdb) b * 0x80482c8
Breakpoint 2 at 0x80482c8
(gdb) c
Continuing.
Breakpoint 2, 0x80482c8 in _init ()
(gdb) x/8x 0x8049470
0x8049470 <_GLOBAL_OFFSET_TABLE_>: 0x08049490 0x40013ed0 0x4000a960 0x400fa550
0x8049480 <_GLOBAL_OFFSET_TABLE_+16>: 0x080482ee 0x400328cc 0x0804830e 0x00000000
(gdb) x/50x 0x40013ed0 ( * link_map類(lèi)型)
0x40013ed0: 0x00000000 0x40010c27 0x08049490 0x400143e0
0x40013ee0: 0x00000000 0x40014100 0x00000000 0x08049490
0x40013ef0: 0x080494e0 0x080494d8 0x080494a8 0x080494b0
0x40013f00: 0x080494b8 0x00000000 0x00000000 0x00000000
0x40013f10: 0x080494c0 0x080494c8 0x08049498 0x080494a0
0x40013f20: 0x00000000 0x00000000 0x00000000 0x080494f8
0x40013f30: 0x08049500 0x08049508 0x080494e8 0x080494d0
0x40013f40: 0x00000000 0x080494f0 0x00000000 0x00000000
0x40013f50: 0x00000000 0x00000000 0x00000000 0x00000000
0x40013f60: 0x00000000 0x00000000 0x00000000 0x00000000
(gdb) disass 0x4000a960 ③
Dump of assembler code for function _dl_runtime_resolve:
0x4000a960 <_dl_runtime_resolve>: push %eax
0x4000a961 <_dl_runtime_resolve+1>: push %ecx
0x4000a962 <_dl_runtime_resolve+2>: push %edx
0x4000a963 <_dl_runtime_resolve+3>: mov 0x10(%esp,1),%edx
0x4000a967 <_dl_runtime_resolve+7>: mov 0xc(%esp,1),%eax
0x4000a96b <_dl_runtime_resolve+11>: call 0x4000a740 <fixup>
//調(diào)用真正的解析函數(shù)fixup(),修正GOT[6],使它指向真正的printf函數(shù)地址
0x4000a970 <_dl_runtime_resolve+16>: pop %edx
0x4000a971 <_dl_runtime_resolve+17>: pop %ecx
0x4000a972 <_dl_runtime_resolve+18>: xchg %eax,(%esp,1)
0x4000a975 <_dl_runtime_resolve+21>: ret $0x8 //跳到printf函數(shù)地址執(zhí)行
0x4000a978 <_dl_runtime_resolve+24>: nop
0x4000a979 <_dl_runtime_resolve+25>: lea 0x0(%esi,1),%esi
End of assembler dump.
(gdb) b * 0x4000a972
Breakpoint 4 at 0x4000a972: file dl-runtime.c, line 182.
(gdb) c
Continuing.
Breakpoint 4, 0x4000a972 in _dl_runtime_resolve () at dl-runtime.c:182
182 in dl-runtime.c
(gdb) i reg $eax $esp
eax 0x4006804c 1074167884
esp 0xbffffb64 -1073743004
(gdb) b *0x4000a975
Breakpoint 5 at 0x4000a975: file dl-runtime.c, line 182.
(gdb) c
Continuing.
Breakpoint 5, 0x4000a975 in _dl_runtime_resolve () at dl-runtime.c:182
182 in dl-runtime.c
(gdb) si
printf (format=0x1 <Address 0x1 out of bounds>) at printf.c:26
26 printf.c: No such file or directory.
(gdb) disass ④ ⑵
Dump of assembler code for function printf:
0x4006804c <printf>: push %ebp
0x4006804d <printf+1>: mov %esp,%ebp
0x4006804f <printf+3>: push %ebx
0x40068050 <printf+4>: call 0x40068055 <printf+9>
0x40068055 <printf+9>: pop %ebx
0x40068056 <printf+10>: add $0xa2197,%ebx
0x4006805c <printf+16>: lea 0xc(%ebp),%eax
0x4006805f <printf+19>: push %eax
0x40068060 <printf+20>: pushl 0x8(%ebp)
0x40068063 <printf+23>: mov 0x81c(%ebx),%eax
0x40068069 <printf+29>: pushl (%eax)
0x4006806b <printf+31>: call 0x400325b4
0x40068070 <printf+36>: mov 0xfffffffc(%ebp),%ebx
0x40068073 <printf+39>: leave
0x40068074 <printf+40>: ret
End of assembler dump.
(gdb) x/8x 0x8049470
0x8049470 <_GLOBAL_OFFSET_TABLE_>: 0x08049490 0x40013ed0 0x4000a960 0x400fa550
0x8049480 <_GLOBAL_OFFSET_TABLE_+16>: 0x080482ee 0x400328cc 0x4006804c 0x00000000
GOT[6]已經(jīng)被修正為0x4006804c了
第一次調(diào)用printf()的時(shí)候需要經(jīng)過(guò)①->②->③->④
以后調(diào)用printf()的時(shí)候就不需要這么復(fù)雜了,只要經(jīng)過(guò)⑴->⑵就可以了
我們來(lái)看看到底是如何修正GOT[6]的,也是就說(shuō)如何找到要修正的地址的
(以前我在這點(diǎn)理解上發(fā)生了一些比較大的誤解,誤導(dǎo)各位的地方還請(qǐng)包涵:) )
1:
進(jìn)入PLT4的時(shí)候 push $0x18 ,該$0x18為printf重定位入口在JMPREL section中的偏移量
2:
printf重定位地址為JMPREL+$0x18 /* Elf32_Rel * reloc = JMPREL + reloc_offset; */
(gdb) x/8x 0x8048278+0x18
0x8048290: 0x08049488 0x00000407 0x53e58955 0x000000e8
0x80482a0 <_init+8>: 0xc3815b00 0x000011cf 0x001cbb83 0x74000000
typedef struct {
Elf32_Addr r_offset;
Elf32_Word r_info;
} Elf32_Rel;
也就是說(shuō)printf重定位printf_retloc.r_offset=0x08049488;
printf_retloc.r_info=0x00000407;
再看看0x08049488是什么地方
(gdb) x 0x08049488
0x8049488 <_GLOBAL_OFFSET_TABLE_+24>: 0x4006804c
也就是GOT[6]
3:
void *const rel_addr = (void *)(l->l_addr + reloc->r_offset);
對(duì)一個(gè)可執(zhí)行文件 或一個(gè)共享目標(biāo)而言,rel_addr就等于reloc->r_offset
所以rel_addr=0x08049488=GOT[6];
4:
*reloc_addr = value;
修正了rel_addr也就是GOT[6]
至于value是如何計(jì)算的,請(qǐng)參考下面的源代碼
同時(shí)r_info又關(guān)聯(lián)著一個(gè)符號(hào)
Elf32_Sym * sym = &SYMTAB[ ELF32_R_SYM (reloc->r_info) ];
sym=0x8048158+0x00000407;
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;
(gdb) x/10x 0x8048158+0x00000407
0x804855f: 0x00003a00 0x00008000 0x00000000 0x00006900
0x804856f: 0x00008000 0x00000000 0x00008300 0x00008000
0x804857f: 0x00000000 0x0000b700
link_map結(jié)構(gòu)說(shuō)明如下:
/* Structure describing a loaded shared object. The `l_next' and `l_prev'
members form a chain of all the shared objects loaded at startup.
These data structures exist in space used by the run-time dynamic linker;
modifying them may have disastrous results.
This data structure might change in future, if necessary. User-level
programs must avoid defining objects of this type. */
★★ glibc中動(dòng)態(tài)解析符號(hào)的源代碼(glibc 2.1.3的實(shí)現(xiàn))
.text
.globl _dl_runtime_resolve
.type _dl_runtime_resolve, @function
.align 16
_dl_runtime_resolve:
pushl %eax # Preserve registers otherwise clobbered.
pushl %ecx
pushl %edx
movl 16(%esp), %edx # Copy args pushed by PLT in register. Note
movl 12(%esp), %eax # that `fixup' takes its parameters in regs.
call fixup # Call resolver.
popl %edx # Get register content back.
popl %ecx
xchgl %eax, (%esp) # Get %eax contents end store function address.
ret $8 # Jump to function address.
static ElfW(Addr) __attribute__ ((unused))
fixup (
# ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
ELF_MACHINE_RUNTIME_FIXUP_ARGS,
# endif
struct link_map *l, ElfW(Word) reloc_offset)
{
const ElfW(Sym) *const symtab
= (const void *) l->l_info[DT_SYMTAB]->d_un.d_ptr;
const char *strtab = (const void *) l->l_info[DT_STRTAB]->d_un.d_ptr;
const PLTREL *const reloc /*計(jì)算函數(shù)重定位人口*/
= (const void *) (l->l_info[DT_JMPREL]->d_un.d_ptr + reloc_offset);
/*l->l_info[DT_JMPREL]->d_un.d_ptr 為JMPREL section的地址*/
const ElfW(Sym) *sym = &symtab[ELFW(R_SYM) (reloc->r_info)];/*計(jì)算函數(shù)symtab入口*/
void *const rel_addr = (void *)(l->l_addr + reloc->r_offset);/*重定向符號(hào)的絕對(duì)地址*/
ElfW(Addr) value;
/* The use of `alloca' here looks ridiculous but it helps. The goal is
to prevent the function from being inlined and thus optimized out.
There is no official way to do this so we use this trick. gcc never
inlines functions which use `alloca'. */
alloca (sizeof (int));
/* Sanity check that we're really looking at a PLT relocation. */
assert (ELFW(R_TYPE)(reloc->r_info) == ELF_MACHINE_JMP_SLOT);/*健壯性檢查*/
/* Look up the target symbol. */
switch (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
{
default:
{
const ElfW(Half) *vernum =
(const void *) l->l_info[VERSYMIDX (DT_VERSYM)]->d_un.d_ptr;
ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)];
const struct r_found_version *version = &l->l_versions[ndx];
if (version->hash != 0)
{
value = _dl_lookup_versioned_symbol(strtab + sym->st_name,
&sym, l->l_scope, l->l_name,
version, ELF_MACHINE_JMP_SLOT);
break;
}
}
case 0:
value = _dl_lookup_symbol (strtab + sym->st_name, &sym, l->l_scope,
l->l_name, ELF_MACHINE_JMP_SLOT);
}
/*此時(shí)value為object裝載的基地址*/
/* Currently value contains the base load address of the object
that defines sym. Now add in the symbol offset. */
value = (sym ? value + sym->st_value : 0);/*函數(shù)的絕對(duì)地址*/
/* And now perhaps the relocation addend. */
value = elf_machine_plt_value (l, reloc, value);/*可能還需要一下重定位*/
/* Finally, fix up the plt itself. */
elf_machine_fixup_plt (l, reloc, rel_addr, value);/*修正rel_addr,一般來(lái)說(shuō)是GOT[N]*/
return value;
}
static inline Elf32_Addr
elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc,
Elf32_Addr value)
{
return value + reloc->r_addend;
}
/* Fixup a PLT entry to bounce directly to the function at VALUE. */
static inline void
elf_machine_fixup_plt (struct link_map *map, const Elf32_Rel *reloc,
Elf32_Addr *reloc_addr, Elf32_Addr value)
{
*reloc_addr = value;
}
參考資料:
1.glibc 2.1.3 src
2.<<ELF文件格式>>
3.<<Cheating the ELF Subversive Dynamic Linking to Libraries>> write by the grugq
4.Linux動(dòng)態(tài)鏈接技術(shù)
http://www.linuxforum.net/forum/showflat.php?Cat=&Board=Kstudy&Number=102793&page=1&view=collapsed&sb=5&o=31&part=
5.p58-0x04 by Nergal <nergal@owl.openwall.com>
<< The advanced return-into-lib(c) exploits >>