b. Linux 中有特殊函數 _init 和 _fini, 主要是分别用來初始化動态庫和關閉的時候
做一些必要的處理, 我們可以把自己認為需要的代碼放到這兩個函數裡面, 它們分别
在動态庫被加載和釋放的時候被執行。具體說, 如果一個動态庫裡面有一個名字為
"_init" 的函數輸出, 那麼在第一次通過 dlopen() 函數打開這個動态庫, 或者隻是
簡單的作為共享動态庫被打開的時候, _init 函數被自動調用執行。與之相對應的就
是 _fini 函數, 當一個程式調用 dlclose() 去釋放對這個動态庫的引用的時候, 如
果該動态庫的被引用計數器為 0 了, 或者這個動态庫是作為一般的共享動态庫被使
用而使用它的程式正常退出的時候, _fini就會被調用執行。
C語言定義它們的原型如下:
void _init(void);
void _fini(void);
當使用你自己的 _init 和 _fini 函數時, 會出現命名沖突, 就會得到一個
"multiple-definition" 的錯誤, 編譯器提示已經存在這個名字, 可以通過幾種方式
來解決:
1). 自定義 init 函數名字, 比如 myinit 用 -Wl, 選項給 ld 傳遞此名字:
gcc ... -Wl,-init=myinit
2). 當 GCC 編譯源程式時, 可以使用選項 -nostartfiles 來使共享庫不與系統
啟動檔案一起編譯
gcc ... -nostartfiles
3). 使用上面的函數或 GCC 的 -nostartfiles 選項并不是很好的習慣, 因為這
可能會産生一些意外的結果。相反, 庫應該使用
__attribute__((constructor)) 和 __attribute__((destructor)) 函數屬
性來輸出它的構造函數和析構函數。如下所示:
void __attribute__((constructor)) x_init(void);
void __attribute__((destructor)) x_fini(void);
構造函數會在dlopen()傳回前或庫被裝載時調用;
析構函數會在這樣幾種情況下被調用: dlclose() 傳回前, 或 main() 傳回
後, 或裝載庫過程中 exit() 被調用時。
c. Linux 中的初始化和釋放函數不建議使用。
後記:
我按作者b 2)中提到的方式建立so檔案入口點,确實是做到了加載so檔案時調用入口函數。但是用objdump -S檢視so檔案,發現檔案中仍有 _init段。我的入口代碼如下:
/*
當使用你自己的_init和_fini函數時,需要注意不要與系統啟動檔案一起連結。
可以使用GCC選項 -nostartfiles 做到這一點
gcc -shared -fPIC -nostartfiles -o mylib.so mylib.c
*/
__attribute ((constructor)) void detour_init(void)
{
printf("detour_init\n");
fp = fopen(DETOURLOGPATH, "a+");
dllHnd = dlopen(LIBVIRTPATH,RTLD_LAZY|RTLD_GLOBAL);
assert(fp != NULL);
assert(dllHnd != NULL);
printf("auditInitilize\n");
auditInitilize();
auditParam.fp = fp;
printf("LogFile\n");
auditCBFunc = audit2LogFile;
return;
}
objdump -S的輸入如下:
Disassembly of section .init:
00000a90 <_init>:
a90: 53 push %ebx
a91: 83 ec 08 sub $0x8,%esp
a94: e8 00 00 00 00 call a99 <_init+0x9>
a99: 5b pop %ebx
a9a: 81 c3 5b 35 00 00 add $0x355b,%ebx
aa0: 8b 83 dc ff ff ff mov -0x24(%ebx),%eax
aa6: 85 c0 test %eax,%eax
aa8: 74 05 je aaf <_init+0x1f>
aaa: e8 c1 00 00 00 call b70 <__gmon_start__@plt>
aaf: e8 cc 01 00 00 call c80 <frame_dummy>
ab4: e8 47 17 00 00 call 2200 <__do_global_ctors_aux>
ab9: 83 c4 08 add $0x8,%esp
abc: 5b pop %ebx
abd: c3 ret
Disassembly of section .text:
...
00000cbc <detour_init>:
¿ÉÒÔʹÓÃGCCÑ¡Ïî -nostartfiles ×öµœÕâÒ»µã
gcc -shared -fPIC -nostartfiles -o mylib.so mylib.c
*/
__attribute ((constructor)) void detour_init(void)
{
cbc: 55 push %ebp
cbd: 89 e5 mov %esp,%ebp
cbf: 53 push %ebx
cc0: 83 ec 14 sub $0x14,%esp
cc3: e8 ef ff ff ff call cb7 <__i686.get_pc_thunk.bx>
cc8: 81 c3 2c 33 00 00 add $0x332c,%ebx
printf("detour_init\n");
cce: 8d 83 60 e2 ff ff lea -0x1da0(%ebx),%eax
cd4: 89 04 24 mov %eax,(%esp)
cd7: e8 84 fe ff ff call b60 <puts@plt>
fp = fopen(DETOURLOGPATH, "a+");
cdc: 8d 93 6c e2 ff ff lea -0x1d94(%ebx),%edx
ce2: 8d 83 6f e2 ff ff lea -0x1d91(%ebx),%eax
ce8: 89 54 24 04 mov %edx,0x4(%esp)
cec: 89 04 24 mov %eax,(%esp)
cef: e8 bc fe ff ff call bb0 <fopen@plt>
cf4: 8b 93 c0 ff ff ff mov -0x40(%ebx),%edx
cfa: 89 02 mov %eax,(%edx)
dllHnd = dlopen(LIBVIRTPATH,RTLD_LAZY|RTLD_GLOBAL);
cfc: c7 44 24 04 01 01 00 movl $0x101,0x4(%esp)
d03: 00
d04: 8d 83 80 e2 ff ff lea -0x1d80(%ebx),%eax
d0a: 89 04 24 mov %eax,(%esp)
d0d: e8 ce fe ff ff call be0 <dlopen@plt>
d12: 8b 93 cc ff ff ff mov -0x34(%ebx),%edx
d18: 89 02 mov %eax,(%edx)
assert(fp != NULL);
d1a: 8b 83 c0 ff ff ff mov -0x40(%ebx),%eax
d20: 8b 00 mov (%eax),%eax
d22: 85 c0 test %eax,%eax
d24: 75 2a jne d50 <detour_init+0x94>
d26: 8d 83 08 e3 ff ff lea -0x1cf8(%ebx),%eax
d2c: 89 44 24 0c mov %eax,0xc(%esp)
d30: c7 44 24 08 12 00 00 movl $0x12,0x8(%esp)
d37: 00
d38: 8d 83 96 e2 ff ff lea -0x1d6a(%ebx),%eax
d3e: 89 44 24 04 mov %eax,0x4(%esp)
d42: 8d 83 a9 e2 ff ff lea -0x1d57(%ebx),%eax
d48: 89 04 24 mov %eax,(%esp)
d4b: e8 a0 fe ff ff call bf0 <__assert_fail@plt>
assert(dllHnd != NULL);
d50: 8b 83 cc ff ff ff mov -0x34(%ebx),%eax
d56: 8b 00 mov (%eax),%eax
d58: 85 c0 test %eax,%eax
d5a: 75 2a jne d86 <detour_init+0xca>
d5c: 8d 83 08 e3 ff ff lea -0x1cf8(%ebx),%eax
d62: 89 44 24 0c mov %eax,0xc(%esp)
d66: c7 44 24 08 13 00 00 movl $0x13,0x8(%esp)
d6d: 00
d6e: 8d 83 96 e2 ff ff lea -0x1d6a(%ebx),%eax
d74: 89 44 24 04 mov %eax,0x4(%esp)
d78: 8d 83 bb e2 ff ff lea -0x1d45(%ebx),%eax
d7e: 89 04 24 mov %eax,(%esp)
d81: e8 6a fe ff ff call bf0 <__assert_fail@plt>
printf("auditInitilize\n");
d86: 8d 83 d1 e2 ff ff lea -0x1d2f(%ebx),%eax
d8c: 89 04 24 mov %eax,(%esp)
d8f: e8 cc fd ff ff call b60 <puts@plt>
auditInitilize();
d94: e8 57 fd ff ff call af0 <auditInitilize@plt>
auditParam.fp = fp;
d99: 8b 83 c0 ff ff ff mov -0x40(%ebx),%eax
d9f: 8b 10 mov (%eax),%edx
da1: 8b 83 f8 ff ff ff mov -0x8(%ebx),%eax
da7: 89 10 mov %edx,(%eax)
printf("LogFile\n");
da9: 8d 83 e0 e2 ff ff lea -0x1d20(%ebx),%eax
daf: 89 04 24 mov %eax,(%esp)
db2: e8 a9 fd ff ff call b60 <puts@plt>
auditCBFunc = audit2LogFile;
db7: 8b 83 fc ff ff ff mov -0x4(%ebx),%eax
dbd: 8b 93 d4 ff ff ff mov -0x2c(%ebx),%edx
dc3: 89 10 mov %edx,(%eax)
return;
}
dc5: 83 c4 14 add $0x14,%esp
dc8: 5b pop %ebx
dc9: 5d pop %ebp
dca: c3 ret