天天看點

ELF 可執行程式在運作階段尋找動态庫的路徑順序

Linux 上 ELF 格式的可執行程式,在程式啟動階段,會加載  ld.so 這個動态庫(比較奇妙的是,它也可以直接被執行,動态庫/可執行程式 二合一?),并通過它來加載其它需要的動态庫。

ld.so  manual page 中介紹了運作階段尋找動态庫的順序 :

       ld.so  loads  the shared libraries needed by a program, prepares the program to run, and then runs it.  Unless explicitly

       specified via the -static option to ld during compilation, all Linux programs are incomplete and require further  linking

       at run time.

       The necessary shared libraries needed by the program are searched for in the following order

       o      Using  the  environment  variable  LD_LIBRARY_PATH  (LD_AOUT_LIBRARY_PATH for a.out programs).  Except if the executable is a setuid/setgid binary, in which case it is ignored.

       o      From the cache file /etc/ld.so.cache which contains a compiled list of candidate libraries previously found in the

              augmented library path.

       o      In the default path /usr/lib, and then /lib.

如果在連結階段,通過 -static 選項指定了動态庫搜尋路徑的話,那麼這個路徑會被優先選擇 

繼續閱讀