Win32下可以通過DllMain來初始化和~初始化動態庫,而Linux下則沒有與之完全對應的函數,但可以通過一些方法類比它的部分功能。有人會說,很簡單,實現_init/_fini兩個函數就行了。好,我們來看看事實是不是這樣的。
很多資料上都說可以利用_init/_fini來實現,而我從來沒有測試成功過,原因是這兩個函數都已經被gcc佔用了。比如:
test.c
#include <stdio.h>
void _init(void)
{
printf("%s", __func__);
}
void _fini(void)
{
printf("%s", __func__);
}
編譯結果:
[root@localhost soinit]# gcc -g test.c -shared -o libtest.so
/tmp/cc4DUw68.o(.text+0x0): In function `_init':
/work/test/soinit/test.c:5: multiple definition of `_init'
/usr/lib/gcc/i386-redhat-linux/4.0.0/../../../crti.o(.init+0x0): first defined here
/tmp/cc4DUw68.o(.text+0x1d): In function `_fini':
/work/test/soinit/test.c:10: multiple definition of `_fini'
/usr/lib/gcc/i386-redhat-linux/4.0.0/../../../crti.o(.fini+0x0): first defined here
collect2: ld returned 1 exit status
由此可見,這兩個符號已經被編譯器的腳手架代碼佔用了,我們不能再使用。編譯器用這兩個函數做什嗎?我們能不能搶佔這兩個函數,不用編譯器提供的,而用我們自己的呢?先看看這兩個的實現:
00000594 _fini>:
594: 55 push %ebp
595: 89 e5 mov %esp,%ebp
597: 53 push %ebx
598: 50 push %eax
599: e8 00 00 00 00 call 59e _fini+0xa>
59e: 5b pop %ebx
59f: 81 c3 02 11 00 00 add $0x1102,%ebx
5a5: e8 de fe ff ff call 488 __do_global_dtors_aux>
5aa: 58 pop %eax
5ab: 5b pop %ebx
5ac: c9 leave
5ad: c3 ret
0000041c _init>:
41c: 55 push %ebp
41d: 89 e5 mov %esp,%ebp
41f: 83 ec 08 sub $0x8,%esp
422: e8 3d 00 00 00 call 464
427: e8 b8 00 00 00 call 4e4
42c: e8 2b 01 00 00 call 55c __do_global_ctors_aux>
431: c9 leave
432: c3 ret
從以上代碼中可以看出,這兩個函數是用來初始化/~初始化全域變數/對象的,搶佔這兩個函數可能導致初始化/~初始化全域變數/對象出錯。所以不能再打_init/_fini的主意,那怎麼辦呢?
使用全域對象
test.cpp
#include <stdio.h>
class InitFini
{
public:
InitFini()
{
printf("%s/n", __func__);
}
~InitFini()
{
printf("%s/n", __func__);
}
};
static InitFini aInitFini;
extern "C" int test(int n)
{
return n;
}
main.c
int test(int n);
int main(int argc, char* argv[])
{
test(1);
return 0;
}
測試結果:
[root@localhost soinit]# ./t.exe
InitFini
~InitFini
那麼這兩個函數是怎麼被調用的呢?我們在gdb裡看看:
Breakpoint 3, InitFini (this=0xa507bc) at test.cpp:7
7 printf("%s/n", __func__);
Current language: auto; currently c++
(gdb) bt
#0 InitFini (this=0xa507bc) at test.cpp:7
#1 0x00a4f5e0 in __static_initialization_and_destruction_0 (__initialize_p=1, __priority=65535) at test.cpp:15
#2 0x00a4f611 in global constructors keyed to test () at test.cpp:21
#3 0x00a4f66a in __do_global_ctors_aux () from ./libtest.so
#4 0x00a4f4a9 in _init () from ./libtest.so
#5 0x002c8b4b in call_init () from /lib/ld-linux.so.2
#6 0x002c8c4a in _dl_init_internal () from /lib/ld-linux.so.2
#7 0x002bb83f in _dl_start_user () from /lib/ld-linux.so.2
Breakpoint 4, ~InitFini (this=0x0) at test.cpp:9
9 ~InitFini()
(gdb) bt
#0 ~InitFini (this=0x0) at test.cpp:9
#1 0x00a4f5b3 in __tcf_0 () at test.cpp:15
#2 0x00303e6f in __cxa_finalize () from /lib/libc.so.6
#3 0x00a4f532 in __do_global_dtors_aux () from ./libtest.so
#4 0x00a4f692 in _fini () from ./libtest.so
#5 0x002c9058 in _dl_fini () from /lib/ld-linux.so.2
#6 0x00303c69 in exit () from /lib/libc.so.6
#7 0x002eddee in __libc_start_main () from /lib/libc.so.6
#8 0x080483b5 in _start ()
從以上資訊可以看出,正是從_init/_fini兩個函數調用過來的。
使用gcc擴充
毫無疑問,以上方法可行,但是在C++中才行。而C語言中,根本沒有構造和解構函式。怎麼辦呢?這時我們可以使用gcc的擴充:
#include <stdio.h>
__attribute ((constructor)) void test_init(void)
{
printf("%s/n", __func__);
}
__attribute ((destructor)) void test_fini(void)
{
printf("%s/n", __func__);
}
int test(int n)
{
return n;
}
測試結果:
[root@localhost soinit]# ./t.exe
test_init
test_fini
我們不防也看看這兩個函數是怎麼被調到的:
Breakpoint 3, test_init () at test.c:6
6 printf("%s/n", __func__);
(gdb) bt
#0 test_init () at test.c:6
#1 0x00860586 in __do_global_ctors_aux () from ./libtest.so
#2 0x00860439 in _init () from ./libtest.so
#3 0x002c8b4b in call_init () from /lib/ld-linux.so.2
#4 0x002c8c4a in _dl_init_internal () from /lib/ld-linux.so.2
#5 0x002bb83f in _dl_start_user () from /lib/ld-linux.so.2
(gdb) c
Breakpoint 4, test_fini () at test.c:11
11 printf("%s/n", __func__);
(gdb) bt
#0 test_fini () at test.c:11
#1 0x008604d3 in __do_global_dtors_aux () from ./libtest.so
#2 0x008605ae in _fini () from ./libtest.so
#3 0x002c9058 in _dl_fini () from /lib/ld-linux.so.2
#4 0x00303c69 in exit () from /lib/libc.so.6
#5 0x002eddee in __libc_start_main () from /lib/libc.so.6
#6 0x080483b5 in _start ()
從以上資訊可以看出,也是從_init/_fini兩個函數調用過來的。
總結:正如一些資料上所說的,在linux下,_init/_fini是共用庫的初始化和~初始化函數。但這兩個函數是給gcc用的,我們不能直接使用它們,但可以用本文中提到另外兩種方法來實現。