Search path for the Linux (GNU) environment Dynamic Link Library

Source: Internet
Author: User

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
    1. #define _gnu_source
    2. #include <dlfcn.h>
    3. 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
  1. typedef struct {
  2. Const Char *dli_fname; / * Pathname of shared object that contains address * /
  3. void *dli_fbase; / * Address at which shared object is loaded * /
  4. Const Char *dli_sname; / * Name of nearest symbol with Addresslower than addr * /
  5. void *dli_saddr; / * Exact address of symbol named in Dli_sname * /
  6. } Dl_info;

Here is the code for the program and the dynamic library that needs to be loaded: ld_main.c: [CPP]View PlainCopy
    1. int main ()
    2. {
    3. Lib_fun ();
    4. return 0;
    5. }

LD_LIB.C: [CPP]View PlainCopy
    1. #define _gnu_source
    2. #include <stdio.h>
    3. #include <dlfcn.h>
    4. int Lib_fun ()
    5. {
    6. Dl_info Dl_info;
    7. Dladdr ((void*) Lib_fun, &dl_info);
    8. fprintf (stdout, "[email protected]%s.\n", dl_info.dli_fname);
    9. return 0;
    10. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.