Linux编程 --- 动态加载动态库 dlopen() dlsym()。。。

#include <dlfcn.h>

void *dlopen(const char *filename, int flag);

char *dlerror(void);

void *dlsym(void *handle, const char *symbol);

int dlclose(void *handle);

Link with -ldl.

Description

The four functions dlopen(), dlsym(), dlclose(), dlerror() implement the interface to the dynamic linking loader.

dlerror()

The function dlerror() returns a human readable string describing the most recent error that occurred from dlopen(), dlsym() or dlclose() since the last call to dlerror(). It returns NULL if no errors have occurred since initialization or since it was last called.

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 contains 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 variables 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 symbols 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 withRTLD_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, synonymously, "--export-dynamic"), then the global symbols in the executable will also be used to resolve references in a dynamically 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 withRTLD_NOW may force symbol resolution for a library earlier loaded with RTLD_LAZY.

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

dlsym()

The function dlsym() takes a "handle" of a dynamic library returned by dlopen() and the null-terminated symbol name, returning 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 performed 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.

dlclose()

The function dlclose() decrements the reference count on the dynamic library handle handle. If the reference count drops to zero and no other loaded libraries use symbols in it, then the dynamic library is unloaded.

The function dlclose() returns 0 on success, and nonzero on error.

The obsolete symbols _init() and _fini()

The linker recognizes special symbols _init and _fini. If a dynamic library exports a routine named _init(), then that code is executed after the loading, before dlopen() returns. If the dynamic library exports a routine named _fini(), then that routine is called just before the library is unloaded. In case you need to avoid linking against the system startup files, this can be done by using the gcc(1) -nostartfiles command-line option.

Using these routines, or the gcc -nostartfiles or -nostdlib options, is not recommended. Their use may result in undesired behavior, since the constructor/destructor routines will not be executed (unless special measures are taken).

Instead, libraries should export routines using the __attribute__((constructor)) and __attribute__((destructor)) function attributes. See the gcc info pages for information on these. Constructor routines are executed before dlopen() returns, and destructor routines are executed before dlclose() returns.

Glibc extensions: dladdr() and dlvsym()

Glibc adds two functions not described by POSIX, with prototypes

#define _GNU_SOURCE         /* See feature_test_macros(7) */
#include <dlfcn.h>

int dladdr(void *addr, Dl_info *info);

void *dlvsym(void *handle, char *symbol, char *version);

The function dladdr() takes a function pointer and tries to resolve name and file where it is located. Information is stored in the Dl_info structure:

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 address
                               lower than addr */
    void       *dli_saddr;  /* Exact address of symbol named
                               in dli_sname */
} Dl_info;

If no symbol matching addr could be found, then dli_sname and dli_saddr are set to NULL.

dladdr() returns 0 on error, and nonzero on success.

The function dlvsym(), provided by glibc since version 2.1, does the same as dlsym() but takes a version string as an additional argument.

Conforming To

POSIX.1-2001 describes dlclose(), dlerror(), dlopen(), and dlsym().

Notes

The symbols RTLD_DEFAULT and RTLD_NEXT are defined by <dlfcn.h> only when _GNU_SOURCE was defined before including it.

Since glibc 2.2.3, atexit(3) can be used to register an exit handler that is automatically called when a library is unloaded.

History

The dlopen interface standard comes from SunOS. That system also has dladdr(), but not dlvsym().

Bugs

Sometimes, the function pointers you pass to dladdr() may surprise you. On some architectures (notably i386 and x86_64), dli_fname and dli_fbase may end up pointing back at the object from which you called dladdr(), even if the function used as an argument should come from a dynamically linked library.

The problem is that the function pointer will still be resolved at compile time, but merely point to the plt (Procedure Linkage Table) section of the original object (which dispatches the call after asking the dynamic linker to resolve the symbol). To work around this, you can try to compile the code to be position-independent: then, the compiler cannot prepare the pointer at compile time anymore and today's gcc(1) will generate code that just loads the final symbol address from the got (Global Offset Table) at run time before passing it to dladdr().

Example

Load the math library, and print the cosine of 2.0:

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

int
main(int argc, char **argv)
{
    void *handle;
    double (*cosine)(double);
    char *error;

   handle = dlopen("libm.so", RTLD_LAZY);
    if (!handle) {
        fprintf(stderr, "%s\n", dlerror());
        exit(EXIT_FAILURE);
    }

   dlerror();    /* Clear any existing error */

   /* Writing: cosine = (double (*)(double)) dlsym(handle, "cos");
       would seem more natural, but the C99 standard leaves
       casting from "void *" to a function pointer undefined.
       The assignment used below is the POSIX.1-2003 (Technical
       Corrigendum 1) workaround; see the Rationale for the
       POSIX specification of dlsym(). */

   *(void **) (&cosine) = dlsym(handle, "cos");

   if ((error = dlerror()) != NULL)  {
        fprintf(stderr, "%s\n", error);
        exit(EXIT_FAILURE);
    }

   printf("%f\n", (*cosine)(2.0));
    dlclose(handle);
    exit(EXIT_SUCCESS);
}

If this program were in a file named "foo.c", you would build the program with the following command:

gcc -rdynamic -o foo foo.c -ldl

Libraries exporting _init() and _fini() will want to be compiled as follows, using bar.c as the example name:

gcc -shared -nostartfiles -o bar bar.c

http://linux.die.net/man/3/dlopen

================================================================

dlopen和dlsym是用于打开动态链接库中的函数,将动态链接库中的函数或类导入到本程序中:

dlopen函数:

功能:打开一个动态链接库
  包含头文件:
  #include <dlfcn.h>
  函数定义:
  void * dlopen( const char * pathname, int mode );
  函数描述:
  在dlopen的()函数以指定模式打开指定的动态连接库文件,并返回一个句柄给调用进程。通过这个句柄来使用库中的函数和类。使用dlclose  

   ()来卸载打开的库。
  mode:分为这两种
  RTLD_LAZY 暂缓决定,等有需要时再解出符号

resolve undefined symbols as code from the dynamic library is executed

自己理解:在之后调用动态库中的函数时才将动态库内容加载到内存中并解析出库中符号的地址;

  RTLD_NOW 立即决定,返回前解除所有未决定的符号。

resolve all undefined symbols before dlopen() returns and fail if this cannot be done'

自己理解:在调用 dlopen 函数时就将动态库内容加载到内存并解析出库中所有符号(函数,变量等)地址。
  RTLD_LOCAL
  RTLD_GLOBAL 允许导出符号
  RTLD_GROUP
  RTLD_WORLD

返回值:
  打开错误返回NULL
  成功,返回库引用
  编译时候要加入 -ldl (指定dl库)

dlsym函数:

  函数原型是

  void* dlsym(void* handle,const char* symbol)

  该函数在<dlfcn.h>文件中。

  handle是由dlopen打开动态链接库后返回的指针,symbol就是要求获取的函数的名称,函数  返回值是void*,指向函数的地址,供调用使用。

实例:

首先我们先编译一个动态库:

1、写一个add.c

#include

#include "add.h"

int add(int a, intb)

{undefined

return a+b;

}

其中add.h就是下面一句:

int add(int a, int b);

2、编译add.c:

[root@loadlibrary]# gcc -fPIC -c add.c

[root@loadlibrary]# gcc -shared -o test_add.so add.c

生成文件目录如下:

add.c

add.o

test_add.so

其中,test_add.so就是我们需要用来测试的动态库了;

接下来测试loadlibrary:

1、测试代码test.c:

#include

#include

int main()

{undefined

void* dp=NULL;

int (*add_function)(int, int);

dp = dlopen("test_add.so",RTLD_LAZY );

if(dp==NULL)

{undefined

printf("dlopen failed\n");

return 0;

}

add_function = dlsym(dp, "add");

int result = add_function(3, 4);

printf("dlopen success\nresult=%d\n");

dlclose(dp);

return 0;

}

2、编译test.c为bin文件:

[root@loadlibrary]# gcc -c test.c

[root@loadlibrary]# gcc -o test_add -ldl test.o

生成的文件列表:

add.c

add.o

test_add

test_add.so

test.c

test.o

而test_add就是我们用来测试的bin文件了

3、执行bin文件:

执行失败了,应该是没找到库的位置,临时添加一个lib path:

[root@loadlibrary]# export LD_LIBRARY_PATH=./:LD_LIBRARY_PATH

好了,接下来就执行成功了:

[root@ loadlibrary]#./test_add

dlopen success

result=4

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值