1. 程式人生 > >C++——glibc全域性構造與析構

C++——glibc全域性構造與析構

_start -> __libc_start_main -> __libc_csu_init -> _init:
Disassembly of section .init:
80480f4 <_init>:
80480f4: 55 push %ebp
80480f5: 89 e5 mov %esp,%ebp
80480f7: 53 push %ebx
80480f8: 83 ec 04 sub $0x4,%esp
80480fb: e8 00 00 00 00 call 8048100 <_init+0xc>
8048100: 5b pop %ebx
8048101: 81 c3 9c 39 07 00 add $0x7399c,%ebx
8048107: 8b 93 fc ff ff ff mov -0x4(%ebx),%edx
804810d: 85 d2 test %edx,%edx
804810f: 74 05 je 8048116 <_init+0x22>
8048111: e8 ea 7e fb f7 call 0 <_nl_current_LC_CTYPE>
8048116: e8 95 00 00 00 call 80481b0 <frame_dummy>
804811b: e8 b0 6e 05 00 call 809efd0 <__do_global_ctors_aux>
8048120: 58 pop %eax
8048121: 5b pop %ebx
8048122: c9 leave
8048123: c3 ret
可以看到_init呼叫了一個叫做__do_global_ctors_aux的函式,如果你在glibc原始碼裡面查詢這個函式,是不可能找到它的。因為它並不屬於glibc,而是來自於GCC提供的一個目標檔案crtbegin.o。我們在上一節中也介紹過,連結器在進行最終連結時,有一部分目標檔案是來自於GCC,它們是那些與語言密切相關的支援函式。很明顯,C++的全域性物件構造是與語言密切相關的,相應負責構造的函式來自於GCC也非常容易理解。