萬事皆有緣由,還是先從我遇到的這個問題說起~~~<br />問:有一個主執行程式main,其中實現了函數foo(),同時調用動態庫liba.so中的函數bar(),而動態庫liba.so中也實現了foo()函數,那麼在執行的時候如果在bar()中調用foo()會調用到哪一個?在main()中調用呢?<br />直接給答案:如果是在Linux上,liba.so中的foo()函數是一個匯出的(extern)”可見”函數,那麼調用會落入主程式裡,這對於liba.so的作者來說實在是個災難,自己辛辛苦苦的工作竟然被自己人視而不見,偏偏誤入歧途,偏偏那個歧途“看起來”(declaration)和自己完全一樣,但表裡(definition)不一的後果就是程式出錯或者直接crash~~~<br />到這裡故事講完了,只想知道結論的可以離開了,覺得不爽的別忙著扔臭雞蛋,下面待我從頭慢慢敘來。<br />先貼代碼:<br />main.cpp<br />#include <stdio.h><br />#include <stdlib.h><br />#include "mylib.h" </p><p>extern "C" void foo() {<br /> printf("foo in main/n");<br />} </p><p>int main() {<br /> bar();<br /> return 0;<br />}mylib.cpp<br />#include "mylib.h"<br />#include <stdio.h><br />#include <stdlib.h><br />#include "mylib_i.h" </p><p>void bar() {<br /> foo();<br />}mylib_i.cpp<br />#include "mylib_i.h"<br />#include <stdio.h><br />#include <stdlib.h> </p><p>extern "C" void foo() {<br /> printf("foo in lib/n");<br />}Makefile<br />all:<br /> g++ -c -o mylib_i.o mylib_i.cpp<br /> g++ -c -o mylib.o mylib.cpp<br /> g++ -shared -o libmylib.so mylib.o mylib_i.o<br /> g++ -c -o main.o main.cpp<br /> g++ -o main main.o -L. -lmylib代碼很簡單(沒有貼header檔案),結果也很簡單,正如前面所說,會輸出foo in main。<br />那麼,為什麼會這樣呢?<br />看一下libmylib.so裡的東西:<br />[root@zouf testlib]# objdump -t libmylib.so |grep "foo|bar"<br />[root@zouf testlib]# objdump -t libmylib.so |egrep "foo|bar"<br />00000614 g F .text 00000034 foo<br />000005f4 g F .text 0000001e bar<br />[root@zouf testlib]# nm libmylib.so |egrep "foo|bar"<br />000005f4 T bar<br />00000614 T foo我們看到libmylib.so中匯出了兩個全域符號(global)foo和bar,而Linux中動態運行庫的符號是在運行時進行重定位的,我們可以用objdump -R看到libmylib.so的重定位表,中間有foo符號,為什麼沒有bar符號呢?<br />這裡有點複雜了,先扯開談一下另一個問題,即地址重定位發生的時機,而在這之前先看無處不在的符號的問題,事實上我們的C/C++程式從可讀的代碼變成電腦可執行檔進程,中間經過了很多步驟:編譯、連結、載入、最後才是真正運行我們的代碼。C/C++代碼的變數函數等符號最後怎麼變成正確的記憶體中地址,這個和符號相關的問題牽涉到很多:這裡和我們相關的主要是兩個話題:符號的尋找(resolve,或者叫決議、綁定、解析)和符號的重定位(relocation)<br />符號的尋找/綁定在每一個步驟中都可能會發生(嚴格說編譯時間並不是符號的綁定,它只會牽涉到局部符號(static)的綁定,它是基於token的文法和語義層面的“綁定”)。<br />連結時的符號綁定做了很多工作,這也是我們經常看到ld會返回"undefined reference to …"或"unresolved symbol …",這就是ld找不到符號,完成不了綁定工作,只能向我們抱怨老,但並不是所有的連結時都會把符號綁定進行到底的,只要在產生最終的執行程式之前把所有的符號全部綁定完成就可以了。這樣我們可以理解為什麼連結動態庫或靜態庫是即使有符號找不到也不會報錯(但如果是局部符號找不到那時一定會報錯的,因為連結器知道已經沒有機會再找到了)。當然靜態連結(ar)和動態連結(ld)在這方面有著眾多生理以及心理、外在以及本源的差別,比如後面系列會提到的弱符號解析、地址無關代碼等等,這裡按下不表。<br />由於編譯和連結都不能保證所有符號都已經解析,因此我們通過nm查看.o或者.a或者.so檔案時會看到U符號,即undefined符號,那都是待字閨中的代表(無視剩女的後果是當你產生最終的執行程式時報告unresolved symbol…)<br />載入時的綁定,其實載入時對於符號的綁定和連結對應的執行程式做的事情基本類似,需要重複勞動的原因是OS無法保證載入程式時當初的原配是否還在,通過綁定來找到符號在那個模組的那個地址。<br />既然載入時都已經綁定了,那為什麼運行時還要?唉,懶惰的OS總是抱著僥倖的心理想可能有些符號不需要綁定,載入時就不理它們了,直到運行時不能不用時火燒眉頭再來綁定,此所謂延遲綁定,在Linux裡通過PLT實現。<br />另一個符號的概念是重定位(relocation),似乎這個概念和符號的綁定是很相關、甚至有點重疊的。我的理解,符號的綁定一般包含了重定位這樣一個操作(但也不絕對,比如局部符號就沒有重定位的發生),而要完成重定位則需要符號的尋找先。一般而言,我們更多地提重定位是在載入和啟動並執行時候。嚴格的區分,嗯,我也說不清,哪位大大解釋一下?<br />所以類似地,重定位也可以分為<br />連結時重定位<br />載入時重定位<br />運行時重定位<br />所有重定位都要依賴與重定位表(relocation table),可以通過objdump –r/-R看到,就是前文中提到objdump –R libmylib.so會看到foo這個符號需要重定位,為什麼呢?因為連結器發現libmylib.so中的bar()函數調用了foo()函數,而foo()也可能會被外面的函數調用到(extern函數),所以連結器得讓它在載入或運行時再次重定位以完成綁定。那為什麼沒有bar()函數在重定位表中呢?ld在連結libmylib.so時還沒看到任何它需要載入/運行時重定位的跡象,那就放過吧,但是,main…我要用啊,試試objdum –R main?OK,果然ld在這時候把它加上了。<br />所以,foo/bar這兩個函數都需要在載入/運行時進行重定位。那究竟是載入還是運行時呢?前面已經提過,就是PLT的差別,具體到編譯選項,是-fPIC,也就是地址無關代碼,如果編譯.o檔案時有-fPIC,那就會是PLT代碼,即運行時重定位,如果只在連結時指定-shared,那就是載入時運行咯。搞這麼複雜的目的有兩個:一是杜絕浪費,二是為了COW。Windows上採用的就是載入時重定位,並且引入了所謂基地址重設(rebasing)來解決共用問題。糟糕,劇透了,打住~~~<br />這篇我們編譯的時候沒有帶-fPIC參數,所以重定位會發生在程式載入的時候。<br />OK,說了這麼多,其實只是想說明:對於用到的動態庫中的extern符號,在載入/運行時會發生重定位(如果我們注意前面的結果其實會看到中間還有printf,也就是用到的libc.so中的符號,同樣它也會在載入/運行時被重定位)。<br />可是,這個和最初提到的問題有啥關係?然,正是重定位導致了問題的出現!<br />這個就源於載入器對於重定位的實現邏輯了(重定位這個操作是由載入器完成,也就是我們在ldd main是會看到的/lib/ld-linux.so.2,它裡面實現了一個重要的函數_dl_runtime_resolve,會真正完成重定位的邏輯)<br />載入器在進行符號解析和重定位時,會把找到的符號及對應的模組和地址放入一個全域符號表(Global Symbol Table),但GST中是不能放多個同名的符號的,否則使用的人就要抓狂了,所以載入器在往全域符號表中加入item時會解決同名符號衝突的問題,即引入符號優先順序,原則其實很簡單:當一個符號需要被加入全域符號表時,如果相同的符號名已經存在,則後加入的符號被忽略。所以在Linux中,發生全域符號衝突真是太正常了~~~,主執行程式中的符號可能會覆蓋動態庫中的符號(因為主程式肯定是第一個載入的),甚至動態庫a中符號也會覆蓋動態庫b中的符號。這又被稱為共用對象全域符號介入(Global Symbol Interpose)。<br />當然,對於動態庫和動態庫中的符號衝突,又牽涉到另一個問題,即動態庫的載入順序問題,這可是生死攸關的大問題,畢竟誰在前面誰就說話算數啊。載入器的原則是廣度優先(BFS),即首先載入主程式,然後載入所有主程式顯式依賴所有動態庫,然後載入動態庫們依賴的動態庫,知道全部載入完畢。<br />當然,這邊還有個動態載入動態庫(dlopen)的問題,原則也是一樣,真正的邏輯同樣發生在_dl_runtime_resolve中。<br />好,到這裡這個問題的來龍去脈已經搞清楚了。應該可以清晰地回答同名函數的執行究竟是怎樣這樣一個問題了。至於http://wf.xplore.cn/post/test_lib.php中提到的動態庫和靜態庫同名衝突的問題,可以看成是這裡的一個特例,原則並不是靜態庫優先,而是主程式優先,因為靜態庫被靜態連結進了主程式,所以“看起來”被優先了。<br />那麼,這個行為是好是壞呢?在我看來很痛苦~~~因為這個衝突gdb半天可不是鬧著玩的~~~C/C++中的全域命名空間汙染問題由來已久,也糾結很多很多人,《C++大規模程式開發》中也給出很多實務的方法解決這個問題。那這個行為就一點好處沒有?也未必,比如可以很方便地hook,通過控制動態庫的載入循序可以用自己的代碼替換掉其他人庫裡的代碼,應該可以實現很酷的功能(比如LD_PRELOAD),不清楚valgrind是不是就是通過這種方法實現?hook住malloc/free/new/delete應該可以~~~<br />但總的看起來這個行為還是很危險的,特別是如果你鴕鳥了,那它就會在最不經意間刺出一刀,然後讓你話費巨大的精力尋找、修改(對於大型項目尋找很難,修改也很難)<br />那難道就沒法避免了?畢竟在大規模團隊開發中,每個人在寫自己的代碼,怎樣保證自己的就一定和別人的不衝突呢?<br />C++的namespace(C怎麼辦?),合理的命名規範(所有人都能很好地遵守嗎?),儘可能地避免全域變數和函數(能夠static的盡量static)(static只能在同一個c/cpp中使用,不同c/cpp檔案中的變數或函數怎麼辦?),這些都是很好的解決方案,但都是著眼在避免同名衝突,但如果真的無法避免同名衝突,有什麼方面能夠解決嗎?比如動態庫中就必須有個函數也叫foo(),而且動態庫中必須調用這樣一個foo(),而不是可被別人覆蓋的foo()(static或許可以解決問題,呵呵)<br />這裡再多介紹一個新學習到的方法:GCC的visibility屬性http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html#Function-Attributes:<br />visibility ("visibility_type")<br />This attribute affects the linkage of the declaration to which it is attached. There are four supported visibility_type values: default, hidden, protected or internal visibility.<br /> void __attribute__ ((visibility ("protected")))<br /> f () { /* Do something. */; }<br /> int i __attribute__ ((visibility ("hidden")));</p><p>The possible values of visibility_type correspond to the visibility settings in the ELF gABI.<br />default<br />Default visibility is the normal case for the object file format. This value is available for the visibility attribute to override other options that may change the assumed visibility of entities.<br />On ELF, default visibility means that the declaration is visible to other modules and, in shared libraries, means that the declared entity may be overridden.<br />On Darwin, default visibility means that the declaration is visible to other modules.<br />Default visibility corresponds to “external linkage” in the language.<br />hidden<br />Hidden visibility indicates that the entity declared will have a new form of linkage, which we'll call “hidden linkage”. Two declarations of an object with hidden linkage refer to the same object if they are in the same shared object.<br />internal<br />Internal visibility is like hidden visibility, but with additional processor specific semantics. Unless otherwise specified by the psABI, GCC defines internal visibility to mean that a function is never called from another module. Compare this with hidden functions which, while they cannot be referenced directly by other modules, can be referenced indirectly via function pointers. By indicating that a function cannot be called from outside the module, GCC may for instance omit the load of a PIC register since it is known that the calling function loaded the correct value.<br />protected<br />Protected visibility is like default visibility except that it indicates that references within the defining module will bind to the definition in that module. That is, the declared entity cannot be overridden by another module.<br />All visibilities are supported on many, but not all, ELF targets (supported when the assembler supports the `.visibility' pseudo-op). Default visibility is supported everywhere. Hidden visibility is supported on Darwin targets.<br />The visibility attribute should be applied only to declarations which would otherwise have external linkage. The attribute should be applied consistently, so that the same entity should not be declared with different settings of the attribute.<br />In C++, the visibility attribute applies to types as well as functions and objects, because in C++ types have linkage. A class must not have greater visibility than its non-static data member types and bases, and class members default to the visibility of their class. Also, a declaration without explicit visibility is limited to the visibility of its type.<br />In C++, you can mark member functions and static member variables of a class with the visibility attribute. This is useful if you know a particular method or static member variable should only be used from one shared object; then you can mark it hidden while the rest of the class has default visibility. Care must be taken to avoid breaking the One Definition Rule; for example, it is usually not useful to mark an inline method as hidden without marking the whole class as hidden.<br />A C++ namespace declaration can also have the visibility attribute. This attribute applies only to the particular namespace body, not to other definitions of the same namespace; it is equivalent to using `#pragma GCC visibility' before and after the namespace definition (see Visibility Pragmas).<br />In C++, if a template argument has limited visibility, this restriction is implicitly propagated to the template instantiation. Otherwise, template instantiations and specializations default to the visibility of their template.<br />If both the template and enclosing class have explicit visibility, the visibility from the template is used.<br />正如這段描述,通過visibility屬性可以讓連結器知道,這個符號是否是extern的,如果把一個變數或函數的visibility設為hidden或internal(我不清楚這兩者有什麼差別?)那其他模組是調用不到這個變數或函數的,即只能內部使用。既然連結器知道它們只會被內部使用,那應該就不需要重定位了吧?下面我們通過例子來看一下。<br />有兩種使用方法指定visibility屬性<br />編譯時間指定,指定編譯選項-fvisibility=hidden,則該編譯出的.o檔案中的所有符號均為外部不可訪問<br />代碼中指定,即上面例子中的代碼,void __attribute__((visibility (“hidden”))) foo(); 則foo()就只能在模組內部使用了。<br />我們先來簡單地改一下Makefile:<br />all:<br /> g++ -c -fvisibility=hidden -o mylib_i.o mylib_i.cpp<br /> g++ -c -o mylib.o mylib.cpp<br /> g++ -shared -o libmylib.so mylib.o mylib_i.o<br /> g++ -c -o main.o main.cpp<br /> g++ -o main main.o -L. -lmylib再運行,發現結果變成了"foo in lib”。<br />深入一下:<br />[root@zouf testlib]# objdump -t libmylib.so |egrep "foo|bar"<br />000005ec l F .text 00000022 .hidden foo<br />000005dc g F .text 0000000d bar<br />[root@zouf testlib]# nm libmylib.so |egrep "foo|bar"<br />000005dc T bar<br />000005ec t foo我們看到nm輸出中foo()函數變成了t,nm的man手冊中說:<br />The symbol type. At least the following types are used; others are, as well, depending on the object file format. If lowercase, the symbol is local; if uppercase, the symbol is global (external).<br />很清楚了,我們已經成功地把這個函數限定在動態庫內部。<br />再深入一點,看一下沒加visibility=hidden和加了visibility=hidden的bar()函數調用foo()函數的彙編碼有什麼不一樣:<br />沒加visibility=hidden<br />[root@zouf testlib]# objdump -d libmylib.so<br />000005fc :<br /> 5fc: 55 push %ebp<br /> 5fd: 89 e5 mov %esp,%ebp<br /> 5ff: 83 ec 08 sub $0x8,%esp<br /> 602: e8 fc ff ff ff call 603 <bar +0X7><br /> 607: c9 leave<br /> 608: c3 ret<br /> 609: 90 nop<br /> 60a: 90 nop<br /> 60b: 90 nop </p><p>0000060c :<br />加了visibility=hidden<br />[root@zouf testlib]# objdump -d libmylib.so<br />...<br />000005dc :<br /> 5dc: 55 push %ebp<br /> 5dd: 89 e5 mov %esp,%ebp<br /> 5df: 83 ec 08 sub $0x8,%esp<br /> 5e2: e8 05 00 00 00 call 5ec <foo><br /> 5e7: c9 leave<br /> 5e8: c3 ret<br /> 5e9: 90 nop<br /> 5ea: 90 nop<br /> 5eb: 90 nop </p><p>000005ec <foo>:<br />...<br />可以清楚地看到,沒加visibility=hidden的bar()函數,在調用foo()函數的地方,填入的是0xfffffffc,也就是call 603,那603是什嗎?<br />[root@zouf testlib]# objdump -R libmylib.so |grep -r foo<br />00000603 R_386_PC32 foo哈哈,原來603就是要重定位的foo啊。<br />而我們看加了visibility=hidden的bar()函數,其中調用foo()函數的地方被正確地填入相對位移地址0x00000005,也就是foo()函數所在地,所以它已經不再需要依賴重定位來找到正確的地址啦。<br />再提一遍,沒加visibility=hidden的那個也沒有加-fPIC,否則看到的就不是這個結果了,預知詳情,下回分解~~~<br />這樣看起來多好啊,動態庫內部的變數或函數就內部使用,只有需要匯出的符號才參與全域匯入。事實上這樣的行為也正是Windows上DLL的行為,在DLL中定義的變數或函數預設是不會被匯出的,只有在.DEF檔案加上EXPORTS,或者__declspec__(dllexport)才會匯出,別的模組才能使用,而對於未匯出的符號,內部使用時是不經過全域匯入的,從而保證了全域空間的“相對”清潔。<br />唉,Linux/GCC為什麼不這樣設計呢?是不是有什麼不可告人的秘密?哪位高人指點一下?莫非又是曆史遺留問題?為了相容性而妥協?<br />不過GCC在wiki上對於visilibity的建議似乎也很清楚,就是盡量在Makefile中開啟visilibity屬性,同時在明確需要匯出的變數/函數/類加上修飾符http://gcc.gnu.org/wiki/Visibility<br />甚至GCC都給出了很清楚的代碼來展示:<br />// Generic helper definitions for shared library support<br />#if defined _WIN32 || defined __CYGWIN__<br /> #define FOX_HELPER_DLL_IMPORT __declspec(dllimport)<br /> #define FOX_HELPER_DLL_EXPORT __declspec(dllexport)<br /> #define FOX_HELPER_DLL_LOCAL<br />#else<br /> #if __GNUC__ >= 4<br /> #define FOX_HELPER_DLL_IMPORT __attribute__ ((visibility("default")))<br /> #define FOX_HELPER_DLL_EXPORT __attribute__ ((visibility("default")))<br /> #define FOX_HELPER_DLL_LOCAL __attribute__ ((visibility("hidden")))<br /> #else<br /> #define FOX_HELPER_DLL_IMPORT<br /> #define FOX_HELPER_DLL_EXPORT<br /> #define FOX_HELPER_DLL_LOCAL<br /> #endif<br />#endif </p><p>// Now we use the generic helper definitions above to define FOX_API and FOX_LOCAL.<br />// FOX_API is used for the public API symbols. It either DLL imports or DLL exports (or does nothing for static build)<br />// FOX_LOCAL is used for non-api symbols. </p><p>#ifdef FOX_DLL // defined if FOX is compiled as a DLL<br /> #ifdef FOX_DLL_EXPORTS // defined if we are building the FOX DLL (instead of using it)<br /> #define FOX_API FOX_HELPER_DLL_EXPORT<br /> #else<br /> #define FOX_API FOX_HELPER_DLL_IMPORT<br /> #endif // FOX_DLL_EXPORTS<br /> #define FOX_LOCAL FOX_HELPER_DLL_LOCAL<br />#else // FOX_DLL is not defined: this means FOX is a static lib.<br /> #define FX_API<br /> #define FOX_LOCAL<br />#endif // FOX_DLL<br />就我理解,所有Linux上的.so都該這樣設計,除非…嗯,我沒想到什麼除非,哪位高人指點?<br />#TO BE CONTINUED#<br />寫在最後的話:這個topic實在有點大,很多概念似乎瞭解但深入想下去又似乎不盡然,中間很多理解都有自己的加工,很可能紕漏百出,希望大家細心指正,共同進步,把這些模糊的概念和過程具體化,致謝!<br />Relate Posts:<br />Linux動態庫符號的問題<br />Tags: Linux Library Symbol Conflict<br />Posted in Technology |<br />Trackback:http://zoufeiblog.appspot.com/2010/09/24/LinuxSymbolConflict.html?code=agp6b3VmZWlibG9ncg0LEgVFbnRyeRiZ4AcM