Excerpted from http://blog.csdn.net/saga1979/article/details/14161229
Man ld.so (8) said that if the library dependency does not include "/", then it will search sequentially according to the following rules:
- (elf format only) if the executable contains a Dt_rpath tag and does not contain a dt_runpath tag, it will be searched from the path listed Dt_rpath. (Dt_rpath has been abandoned, please use Dt_runpath)
- If Ld_library_path is defined when the program is running, it starts with the path it contains. For security reasons, this variable is ignored for Set-user-id or Set-group-id programs.
- (elf format only) if the executable file contains a Dt_runpath tag, the search will start from the path listed in this tab.
- Find files from/etc/ld.so.cache (run Ldconfig Generation)
- Search sequentially from/lib and/urs/lib. If you specify-Z nodefaultlib when linking, this step is ignored.
Seems concise enough, as a leisure, write a program to verify. But before that, let's introduce a glibc extension function (not in POSIX)
[CPP]View PlainCopy
- #define _gnu_source
- #include <dlfcn.h>
- int dladdr (void* addr, dl_info *info);
This function parses the incoming function pointer (the first argument) and fills the information into the DL_INFO structure
[CPP]View PlainCopy
- typedef struct {
- Const Char *dli_fname; / * Pathname of shared object that contains address * /
- void *dli_fbase; / * Address at which shared object is loaded * /
- Const Char *dli_sname; / * Name of nearest symbol with Addresslower than addr * /
- void *dli_saddr; / * Exact address of symbol named in Dli_sname * /
- } Dl_info;
Here is the code for the program and the dynamic library that needs to be loaded: ld_main.c:
[CPP]View PlainCopy
- int main ()
- {
- Lib_fun ();
- return 0;
- }
LD_LIB.C:
[CPP]View PlainCopy
- #define _gnu_source
- #include <stdio.h>
- #include <dlfcn.h>
- int Lib_fun ()
- {
- Dl_info Dl_info;
- Dladdr ((void*) Lib_fun, &dl_info);
- fprintf (stdout, "[email protected]%s.\n", dl_info.dli_fname);
- return 0;
- }
Compile these two files: 1, dynamic library: gcc--shared-fpic ld_lib.c-o libld_lib.so-ldl2, main program: GCC ld_main.c-o ld_main-wl,-rpath,./-ldl-lld_lib The-l./-wl,-rpath compilation option will generate Dt_rpath nodes in the program, and Readelf will see that the library rpath is set to the current directory:
Next, copy the generated libld_lib.so to the search path described earlier:
For Ld_library_path, casually set: Export ld_library_path=. /
For ld.so.conf referred to the path, under/etc/ld.so.conf.d/casually find one, or build one yourself, here with the system comes with the libc.conf
The path mentioned in:/usr/local/lib
Then run (remove the program first load the so file each time):
(The ld.so.conf path updates the file after you run the Ldconfig update cache, otherwise you will not find the file, such as).
About-Z nodefaultlib link Options:
Looks like it's working.
For Dt_runpath, you need to use the--enable-new-dtags link option:
(Linux programs do not search for. So files by default from the current path, which is not an advantage for the "programs" that are developed to be installed in the same directory as many modules. Fortunately, you can use Dt_runpath to specify the load path for. So.
Linux (GNU) environment Dynamic link Library search path