dlsym RTLD_DEFAULT和RTLD_NEXT

161 篇文章 10 订阅

https://blog.csdn.net/ustcxiangchun/article/details/6310085
https://linux.die.net/man/3/dlsym
http://man7.org/linux/man-pages/man3/dlsym.3.html

dlsym()

函数dlsym()接受dlopen()返回的动态库的“句柄” 和以空字符结尾的符号名称,返回将该符号加载到内存中的地址。如果未找到该符号,则在指定的库中,或在加载该库时由dlopen()自动加载的任何 库中,dlsym()返回NULL。(由dlsym()执行的搜索首先遍历这些库的依赖关系树。)由于该符号的值实际上可以为NULL(因此从dlsym()返回的NULL 不必表示错误),因此采用正确的方法测试错误是调用dlerror()清除任何旧的错误条件,然后调用dlsym(),然后再次调用dlerror(),将其返回值保存到变量中,并检查此保存的值是否不为NULL。
有两个特殊的伪句柄,RTLD_DEFAULT和RTLD_NEXT。前者将使用默认的库搜索顺序找到所需符号的第一个匹配项。后者将在当前库之后的搜索顺序中找到功能的下一个出现。这样一来,用户就可以在另一个共享库中为函数提供包装。

 

今天看alsa-lib的代码,发现一个场景是会调到

dlsym(RTLD_DEFAULT, name);

也就是说,handle=RTLD_DEFAULT,在网上查了下,这种情况下会发生的事情是,会在当前进程中按照 default library search order搜索name这个symbol,网上的介绍摘录如下:

 

http://www.qnx.de/developers/docs/6.4.0/neutrino/lib_ref/d/dlsym.html?lang=cn

If handle is a handle returned by dlopen(), you must not have closed that shared object by calling dlclose(). The dlsym() functions also searches for the named symbol in the objects loaded as part of the dependencies for that object.

 

If handle is RTLD_DEFAULT, dlsym() searches all objects in the current process, in load-order.

 

In the case of RTLD_DEFAULT, if the objects being searched were loaded with dlopen(), dlsym() searches the object only if the caller is part of the same dependency hierarchy, or if the object was loaded with global search access (using the RTLD_GLOBAL mode).

 

http://www.linux.com/learn/docs/man/2818-dlsym3

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.  
 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值