本系列第二篇,分析依赖共享库时可执行文件的重定向方式。
源代码如下:访问全局变量,全局函数,静态变量,静态函数,共享库中的变量,共享库中的函数。为了简化,一个函数访问一种变量。
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;} |
重定向条目:
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. Name0000001b 00000d01 R_386_32 00000000 a00000025 00000d01 R_386_32 00000000 a00000032 00001002 R_386_PC32 00000000 b0000003c 00001001 R_386_32 00000000 b00000046 00000a01 R_386_32 00000000 c00000050 00000a01 R_386_32 00000000 c0000005a 00000b02 R_386_PC32 00000000 d00000064 00000b01 R_386_32 00000000 d0000006e 00000301 R_386_32 00000000 .data00000078 00000301 R_386_32 00000000 .data0000008f 00000201 R_386_32 00000000 .text |
可以看到访问共享库的变量、函数与访问本地全局变量、函数没有任何区别。访问变量和变量的地址直接重定向为变量的绝对地址,访问函数通过PC相对位置,访问函数的地址直接重定向函数的绝对地址。