linux C func dlopen and dlsym

       dysym()

      The function dlsym() takes a "handle" of a dynamic library returned by dlopen() and the null-terminated symbol name, return‐

       ing the address where that symbol is loaded into memory.  If the symbol is not found, in the specified library or any of the
       libraries  that  were automatically loaded by dlopen() when that library was loaded, dlsym() returns NULL.  (The search per‐
       formed by dlsym() is breadth first through the dependency tree of these libraries.)  Since the value  of  the  symbol  could
       actually be NULL (so that a NULL return from dlsym() need not indicate an error), the correct way to test for an error is to
       call dlerror() to clear any old error conditions, then call dlsym(), and then call dlerror() again, saving its return  value
       into a variable, and check whether this saved value is not NULL.


       There  are two special pseudo-handles, RTLD_DEFAULT and RTLD_NEXT.  The former will find the first occurrence of the desired
       symbol using the default library search order.  The latter will find the next occurrence of a function in the  search  order

       after the current library.  This allows one to provide a wrapper around a function in another shared library.


      dlopen()

     The  function  dlopen()  loads  the  dynamic library file named by the null-terminated string filename and returns an opaque
       "handle" for the dynamic library.  If filename is NULL, then the returned handle is for the main program.  If filename  con‐
       tains  a  slash  ("/"), then it is interpreted as a (relative or absolute) pathname.  Otherwise, the dynamic linker searches
       for the library as follows (see ld.so(8) for further details):

       o   (ELF only) If the executable file for the calling program contains a DT_RPATH tag, and does  not  contain  a  DT_RUNPATH
           tag, then the directories listed in the DT_RPATH tag are searched.

       o   If,  at  the time that the program was started, the environment variable LD_LIBRARY_PATH was defined to contain a colon-
           separated list of directories, then these are searched.  (As a security measure this variable is ignored for set-user-ID
           and set-group-ID programs.)

       o   (ELF only) If the executable file for the calling program contains a DT_RUNPATH tag, then the directories listed in that
           tag are searched.

       o   The cache file /etc/ld.so.cache (maintained by ldconfig(8)) is checked to see whether it contains an entry for filename.

       o   The directories /lib and /usr/lib are searched (in that order).

       If the library has dependencies on other shared libraries, then these are also automatically loaded by  the  dynamic  linker
       using the same rules.  (This process may occur recursively, if those libraries in turn have dependencies, and so on.)

       One of the following two values must be included in flag:

       RTLD_LAZY
              Perform  lazy  binding.   Only  resolve symbols as the code that references them is executed.  If the symbol is never
              referenced, then it is never resolved.  (Lazy binding is only performed for function references; references to  vari‐
              ables are always immediately bound when the library is loaded.)


       RTLD_NOW
              If  this  value is specified, or the environment variable LD_BIND_NOW is set to a nonempty string, all undefined sym‐
              bols in the library are resolved before dlopen() returns.  If this cannot be done, an error is returned.


       Zero or more of the following values may also be ORed in flag:


       RTLD_GLOBAL
              The symbols defined by this library will be made available for symbol resolution of subsequently loaded libraries.


       RTLD_LOCAL
              This is the converse of RTLD_GLOBAL, and the default if neither flag is specified.  Symbols defined in  this  library
              are not made available to resolve references in subsequently loaded libraries.


       RTLD_NODELETE (since glibc 2.2)
              Do  not  unload  the library during dlclose().  Consequently, the library's static variables are not reinitialized if
              the library is reloaded with dlopen() at a later time.  This flag is not specified in POSIX.1-2001.


       RTLD_NOLOAD (since glibc 2.2)
              Don't load the library.  This can be used to test if the library is already resident (dlopen() returns NULL if it  is
              not,  or  the library's handle if it is resident).  This flag can also be used to promote the flags on a library that
              is already loaded.  For example, a  library  that  was  previously  loaded  with  RTLD_LOCAL  can  be  reopened  with
              RTLD_NOLOAD | RTLD_GLOBAL.  This flag is not specified in POSIX.1-2001.


       RTLD_DEEPBIND (since glibc 2.3.4)
              Place  the  lookup  scope of the symbols in this library ahead of the global scope.  This means that a self-contained
              library will use its own symbols in preference to global symbols with the same name contained in libraries that  have
              already been loaded.  This flag is not specified in POSIX.1-2001.


       If  filename is a NULL pointer, then the returned handle is for the main program.  When given to dlsym(), this handle causes
       a search for a symbol in the main program, followed by all shared libraries loaded at program startup, and then  all  shared
       libraries loaded by dlopen() with the flag RTLD_GLOBAL.


       External  references  in  the  library  are  resolved  using  the  libraries in that library's dependency list and any other
       libraries previously opened with the RTLD_GLOBAL flag.  If the executable was linked with the flag "-rdynamic" (or,  synony‐
       mously,  "--export-dynamic"), then the global symbols in the executable will also be used to resolve references in a dynami‐
       cally loaded library.


       If the same library is loaded again with dlopen(), the same file handle is returned.  The  dl  library  maintains  reference
       counts  for  library handles, so a dynamic library is not deallocated until dlclose() has been called on it as many times as
       dlopen() has succeeded on it.  The _init() routine, if present, is only called once.  But a subsequent  call  with  RTLD_NOW
       may force symbol resolution for a library earlier loaded with RTLD_LAZY.


       If dlopen() fails for any reason, it returns NULL.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值