ILD

ELF重定向分析(2):依赖共享库的可执行文件
作者:Herbert Yuan 邮箱:yuanjp@hust.edu.cn
发布时间:2019-8-17 站点:Inside Linux Development

本系列第二篇,分析依赖共享库时可执行文件的重定向方式。


4 依赖共享库的可执行文件

源代码如下:访问全局变量,全局函数,静态变量,静态函数,共享库中的变量,共享库中的函数。为了简化,一个函数访问一种变量。


exe_lib.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
extern int a;
extern int b();
 
int c = 1;
int d() { return 1; }
 
static int e = 1;
static int f() { return f(); }
 
/* global var and function in shared library */
int access_lib_var()      { return a; }
int access_lib_var_addr()     { return (int)&a; }
int access_lib_fun()      { return b(); }
int access_lib_fun_ptr()  { return (int)b; }
 
/* global var and function */
int access_global_var()       { return c; }
int access_global_var_addr()  { return (int)&c; }
int access_global_fun()       { return d(); }
int access_global_fun_ptr()   { return (int)d; }
 
/* static var and function in this file */
int access_static_var()       { return e; }
int access_static_var_addr()  { return (int)&e; }
int access_static_fun()       { return f(); }
int access_static_fun_ptr()   { return (int)f; }
 
int _start()
{
    return 0;
}


4.1 位置相关代码

重定向条目:

1
2
3
4
5
6
7
8
9
10
11
12
13
Relocation section '.rel.text' at offset 0x5c0 contains 11 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
0000001b  00000d01 R_386_32          00000000   a
00000025  00000d01 R_386_32          00000000   a
00000032  00001002 R_386_PC32        00000000   b
0000003c  00001001 R_386_32          00000000   b
00000046  00000a01 R_386_32          00000000   c
00000050  00000a01 R_386_32          00000000   c
0000005a  00000b02 R_386_PC32        00000000   d
00000064  00000b01 R_386_32          00000000   d
0000006e  00000301 R_386_32          00000000   .data
00000078  00000301 R_386_32          00000000   .data
0000008f  00000201 R_386_32          00000000   .text

可以看到访问共享库的变量、函数与访问本地全局变量、函数没有任何区别。访问变量和变量的地址直接重定向为变量的绝对地址,访问函数通过PC相对位置,访问函数的地址直接重定向函数的绝对地址。


4.2 位置无关代码



Copyright © linuxdev.cc 2017-2024. Some Rights Reserved.