最近看到前人写的一段代码:
typedef void (*fnFunc_t)(int);
void *gpDllHandle = NULL; /* 此变量只在以下代码用到 */
fnFunc_t fnFunc = NULL;
void func1()
{
void *pDllHandle = gpDllHandle;
pDllHandle = dlopen("...", RTLD_LAZY | RTLD_GLOBAL);
}
void func2()
{
fnFunc = dlsym(gpDllHandle, "...");
}
void func3()
{
fnFunc(10);
}
这段代码存在的问题是dlopen打开动态库得到的句柄pDllHandle并没有保存到全局变量,而dlsym时又将此变量作为入参。这样一来,就很怀疑为什么dlsym传入一个无效的句柄,也能正确找到相应函数了(fnFun非空)。
然后仔细看了看dlsym的man手册,有这么一段话:
There are two special pseudo-handles that may be specified in handle: RTLD_DEFAULT Find the first occurrence of the desired symbol using the default shared object search order. The search will include global symbols in the executable and its dependencies, as well as symbols in shared objects that were dynamically loaded with the RTLD_GLOBAL flag. RTLD_NEXT Find the next occurrence of the desired symbol in the search order after the current object. This allows one to provide a wrapper around a function in another shared object, so that, for example, the definition of a function in a preloaded shared object (see LD_PRELOAD in ld.so(8)) can find and invoke the "real" function provided in another shared object (or for that matter, the "next" definition of the function in cases where there are multiple layers of preloading).
大概意思是dlsym可以传入另外两个特殊的句柄,RTLD_DEFAULT和RTLD_NEXT,值分别是0和-1.当传入RTLD_DEFAULT时,意思是在当前进程环境中(包括已加载到全局可用的动态库)查找第一个匹配的函数或全局变量。
而我的代码中dlsym传入的第一个参数是NULL,也就是RTLD_DEFAULT,而这个动态库又是以GLOBAL加载的,所以没有问题,但是这样的代码逻辑让不清楚dlsym这个机制的人很费解,逻辑本身是存在问题的,但是因为巧合,或者说dlsym这种机制,让这段代码实际运行又恰好没有问题。