dynamic linked library on linux

来源:互联网 发布:软件流量违法么 编辑:程序博客网 时间:2024/05/23 05:09

i copy the linux man page to address how to use dynamic linked library in linux

 

the main functions are:

#include <dlfcn.h>

void *dlopen(const char *filename, intflag);

char *dlerror(void);

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

int dlclose(void *handle);

 

dlerror

The function dlerror() returns a human readable string describing the most recent error that occurred fromdlopen(), dlsym() or dlclose() since the last call todlerror(). 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 stringfilename 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 (seeld.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 the environment variable LD_LIBRARY_PATH is 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 forfilename.
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 non-empty string, all undefined symbols in the library are resolved beforedlopen() returns. If this cannot be done, an error is returned.

Zero of 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 reinitialised if the library is reloaded withdlopen() 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 re-opened 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 todlsym(), 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 bydlopen() 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 theRTLD_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 untildlclose() 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 withRTLD_LAZY.

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

dlsym

The function dlsym() takes a "handle" of a dynamic library returned bydlopen() 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 bydlopen() when that library was loaded, dlsym() returns NULL. (The search performed bydlsym() 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 fromdlsym() need not indicate an error), the correct way to test for an error is to calldlerror() to clear any old error conditions, then call dlsym(), and then calldlerror() 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 handlehandle. 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 non-zero on error.

 

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(1);    }    dlerror();    /* Clear any existing error */    cosine = dlsym(handle, "cos");    if ((error = dlerror()) != NULL)  {        fprintf (stderr, "%s/n", error);        exit(1);    }    printf ("%f/n", (*cosine)(2.0));    dlclose(handle);    return 0;
}

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

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 异界气息的装备怎么办 vivo电板没电了怎么办 门套拼接有缝隙怎么办 公司如果一直没有上税收入怎么办 赛车输了俩百万怎么办 交pk金员工不交怎么办 员工不想交pk金怎么办 心悦光环领错角色怎么办 心悦光环领错了怎么办 扑克牌1到13洗后怎么办 南通长牌没钱了怎么办 镇魔曲手游阵营人数已满怎么办 镇魔曲忘记在哪个区怎么办 登录镇魔曲卡在实名验证怎么办 电脑玩联盟花屏怎么办 优盘文件或目录损坏怎么办 苹果下吃鸡设备不兼容怎么办 龙之谷账号忘了怎么办 不花钱的排风除湿怎么办 苹果平板id密码忘了怎么办 苹果平板忘记id及密码怎么办 饥荒抓到的兔子怎么办 苹果手机下载不了王者荣耀怎么办 ipad登录显示验证失败怎么办 苹果手机系统内存太大怎么办 ipad玩游戏没声音怎么办 微信活跃度低怎么办 想开通淘宝直播粉丝不够怎么办 下巴长泡泡还痒怎么办 脚起泡泡很痒怎么办 脚痒还有小泡泡怎么办 外阴长了肉疙瘩怎么办 嘴巴里泡泡破了怎么办 脚上泡泡破了怎么办 脸被自己扣破了怎么办 6s安装不了软件怎么办 苹果6s特别卡怎么办 苹果手机4g网慢怎么办 大王卡玩王者卡怎么办 荣耀7c手机卡顿怎么办 华为6x手机卡顿怎么办