为什么我的图书馆链接了两次?

时间:2017-05-26 18:20:31

标签: c mapping ld dynamic-linking

以gdb输出为例:

(gdb) info proc mappings
process 3975
cmdline = '/mnt/hw6/rop-exploit'
cwd = '/mnt/hw6'
exe = '/mnt/hw6/rop-exploit'
Mapped address spaces:

    Start Addr   End Addr       Size     Offset objfile
     0x8048000  0x8049000     0x1000          0         /mnt/hw6/rop-exploit
     0x8049000  0x804a000     0x1000          0         /mnt/hw6/rop-exploit
     0x804a000  0x806b000    0x21000  0x804a000           [heap]
    0xb7d51000 0xb7e84000   0x133000          0      /lib/libc-2.7.so
    0xb7e84000 0xb7e85000     0x1000   0x133000      /lib/libc-2.7.so
    0xb7e85000 0xb7e87000     0x2000   0x134000      /lib/libc-2.7.so
    0xb7e87000 0xb7e8b000     0x4000 0xb7e87000        
    0xb7e8b000 0xb7fd4000   0x149000          0      /lib/tls/i686/cmov/libc-2.7.so
    0xb7fd4000 0xb7fd5000     0x1000   0x149000      /lib/tls/i686/cmov/libc-2.7.so
    0xb7fd5000 0xb7fd7000     0x2000   0x14a000      /lib/tls/i686/cmov/libc-2.7.so
    0xb7fd7000 0xb7fda000     0x3000 0xb7fd7000        
    0xb7fda000 0xb7fdc000     0x2000          0      /lib/tls/i686/cmov/libdl-2.7.so
    0xb7fdc000 0xb7fde000     0x2000     0x1000      /lib/tls/i686/cmov/libdl-2.7.so
    0xb7fe1000 0xb7fe3000     0x2000 0xb7fe1000        
    0xb7fe3000 0xb7fe4000     0x1000 0xb7fe3000           [vdso]
    0xb7fe4000 0xb7ffe000    0x1a000          0      /lib/ld-2.7.so
    0xb7ffe000 0xb8000000     0x2000    0x19000      /lib/ld-2.7.so
    0xbffeb000 0xc0000000    0x15000 0xbffeb000           [stack]
(gdb) list
13  }
14  
15  int main(int argc, char** argv) {
16    void* libc = dlopen("/lib/libc-2.7.so", RTLD_NOW);
17    void* address = dlsym( libc, "__libc_init_first");
18    printf("Address of <__libc_init_first>: %p\n", address);
19    if(argc > 1) {
20      foo(argv[1]);
21    }
22    printf("Done.\n");
(gdb) x libc
0x804a020:  0xb7d51000
(gdb) print address 
$4 = (void *) 0xb7d672a0

您可以看到三个库与二进制文件动态链接。 libc-2.7,libc2-7.so(来自不同的位置,也许这​​是自动完成的)和libdl-2.7.so

我不明白为什么每个库的这个列表中有两到三个条目,我想知道那里发生了什么,以及为什么它们是单独的条目。

我没有任何假设在那里发生了什么,我不知道如何解释这一点。

以第一个链接库为例:

我假设的第一部分是库本身,它有ELF头,所以我确信它确实是我链接的libc。

当我查看/lib/libc-2.7-的第二个条目时,我只是获得了很多NULL字节,不知道这里发生了什么。

(gdb) x/100x 0xb7e84000
0xb7e84000: 0x00000000  0x00000000  0x00000000  0x00000000
0xb7e84010: 0x00000000  0x00000000  0x00000000  0x00000000
0xb7e84020: 0x00000000  0x00000000  0x00000000  0x00000000

当我查看/lib/libc-2.6.so的第三个条目时,我得到指令的地址(可能是小工具或功能)。

(gdb) x/100x 0xb7e85000
0xb7e85000: 0x0000006f  0xb7e671dc  0xb7e671e0  0xb7e671e4
0xb7e85010: 0xb7e671e8  0xb7e671ec  0xb7e671f0  0xb7e671f4
0xb7e85020: 0xb7e671f8  0xb7e671ff  0xb7e67206  0xb7e6720e

这是我从其中一个地址获得的示例输出,似乎非常有说服力,这可能是可用的代码。

(gdb) x/10i 0xb7e671e8
0xb7e671e8: push   %edi
0xb7e671e9: add    %dl,%fs:%gs:0x75(%eax,%ebp,2)
0xb7e671ef: add    %al,0x72(%esi)
0xb7e671f2: imul   $0x746153,(%eax),%eax
0xb7e671f8: push   %ebx
0xb7e671f9: jne    0xb7e67269
0xb7e671fb: fs
0xb7e671fc: popa   
0xb7e671fd: jns    0xb7e671ff
0xb7e671ff: dec    %ebp
(gdb) 

我的问题是,这三个部分分别做了什么?他们为什么这样分开?在没有相应标签但仍然映射的区域会发生什么?

0 个答案:

没有答案