// first.cpp
#include <stdio.h>
void print_message(){
printf("the first function\n");
}
// second.cpp
#include <stdio.h>
void print_message(){
printf("the second function\n");
}
#include <stdio.h>
#include <dlfcn.h>
#include <errno.h>
void(*f)();
void load_func() __attribute__((constructor));
void load_func(){
f = (void(*)())dlsym(RTLD_NEXT,"print_message");
char *error_str;
error_str = dlerror();
if (error_str != NULL) {
printf("%s\n", error_str);
}
printf("load func first f=%p\n",f);
}
void print_message(){
printf("the main function\n");
f();
}
int main(){
f();
return 0;
}
首先把前两个 程序编译成共享库
g++ -fpic --shared first.cpp -o libfirst.so
g++ -fpic --shared second.cpp -o libsecond.so
然后链接,这两个库编译main文件
g++ -g -o test main.cpp -lfirst -lsecond -ldl -L./
然后运行这个test程序,就会发现段错误
./test: undefined symbol: print_message
load func first f=(nil)
段错误
显然是由于print_message没有找到,那就看看libfirst.so和libsecond.so的符号表
// nm libfirst.so
0000000000004028 b completed.8061
w __cxa_finalize@@GLIBC_2.2.5
0000000000001060 t deregister_tm_clones
00000000000010d0 t __do_global_dtors_aux
0000000000003e18 d __do_global_dtors_aux_fini_array_entry
0000000000004020 d __dso_handle
0000000000003e20 d _DYNAMIC
0000000000001130 t _fini
0000000000001110 t frame_dummy
0000000000003e10 d __frame_dummy_init_array_entry
00000000000020d0 r __FRAME_END__
0000000000004000 d _GLOBAL_OFFSET_TABLE_
w __gmon_start__
0000000000002014 r __GNU_EH_FRAME_HDR
0000000000001000 t _init
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
U puts@@GLIBC_2.2.5
0000000000001090 t register_tm_clones
0000000000004028 d __TMC_END__
0000000000001119 T _Z13print_messagev
// nm libsecond.so
0000000000004028 b completed.8061
w __cxa_finalize@@GLIBC_2.2.5
0000000000001060 t deregister_tm_clones
00000000000010d0 t __do_global_dtors_aux
0000000000003e18 d __do_global_dtors_aux_fini_array_entry
0000000000004020 d __dso_handle
0000000000003e20 d _DYNAMIC
0000000000001130 t _fini
0000000000001110 t frame_dummy
0000000000003e10 d __frame_dummy_init_array_entry
00000000000020d0 r __FRAME_END__
0000000000004000 d _GLOBAL_OFFSET_TABLE_
w __gmon_start__
0000000000002014 r __GNU_EH_FRAME_HDR
0000000000001000 t _init
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
U puts@@GLIBC_2.2.5
0000000000001090 t register_tm_clones
0000000000004028 d __TMC_END__
0000000000001119 T _Z13print_messagev
可以看到_Z13print_messagev不就是printf_message的符号吗,突然想到,由于c++支持重载,所以函数符号命名和c不一样,难道需要符号完全匹配?试试, 将main程序改为
#include <stdio.h>
#include <dlfcn.h>
#include <errno.h>
void(*f)();
void load_func() __attribute__((constructor));
void load_func(){
f = (void(*)())dlsym(RTLD_NEXT,"_Z13print_messagev");
char *error_str;
error_str = dlerror();
if (error_str != NULL) {
printf("%s\n", error_str);
}
printf("load func first f=%p\n",f);
}
void print_message(){
printf("the main function\n");
f();
}
int main(){
f();
return 0;
}
再次尝试,就会发现依然不能运行成功,依然是
./test: undefined symbol: _Z13print_messagev
load func first f=(nil)
段错误
这个时候,就检查ldd查看test是如何链接的
// ldd test
linux-vdso.so.1 (0x00007ffd67d5c000)
/usr/local/lib/libprocesshider.so (0x00007fd826ada000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fd826ab7000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd8268c5000)
/lib64/ld-linux-x86-64.so.2 (0x00007fd826ce4000)
从中可以看出,根本没有链接本的共享库,这其实也很合理,因为main的编译链接根本没有符号需要链接本地共享库的东西。所以需要一个能够链接到共享库的东西,可以加东西
// first.cpp
#include <stdio.h>
void first(){
printf("first\n");
}
void print_message(){
printf("the first function\n");
}
// second.cpp
#include <stdio.h>
void second(){
printf("second\n");
}
void print_message(){
printf("the second function\n");
}
#include <stdio.h>
#include <dlfcn.h>
#include <errno.h>
void first();
void second();
void(*f)();
void load_func() __attribute__((constructor));
void load_func(){
f = (void(*)())dlsym(RTLD_NEXT,"_Z13print_messagev");
char *error_str;
error_str = dlerror();
if (error_str != NULL) {
printf("%s\n", error_str);
}
printf("load func first f=%p\n",f);
}
void print_message(){
printf("the main function\n");
f();
}
int main(){
first();
second();
f();
return 0;
}
重新编译运行就行了。
说了这么多,整了一堆傻布拉吉的例子,实际中谁会写这么明显的错误的代码,但其实是想说如何解决undefined symbol错误以及dlsym在c++中错误的示例。
总结
对于运行期间undefined symbol错误,说明运行过程中没有找到符号,可能有
- 可执行文件中rpath中根本没有需要链接的动态库,可以通过ldd查看可执行文件需要链接哪些动态库;通过LD_DEBUG=libs ./文件查看rpath以及在搜索库的时候为什么没有从路径中找到,通过-Wl,-rpath或者export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:解决;
- 由于C和C++对于符号命名规则的不同,导致链接不到符号,通过nm命令查看共享库中的符号表对比可重定位文件的符号。
甚至可能出现多个同名符号链接导致程序奇怪的结果,尤其是强符号和弱符号等,通过LD_DEBUG=libs可以查看链接库的时候查找顺序。
对于dlsym问题,可以通过extern"C"命令兼容C。另外对于dlsym详见计算机系统篇之链接(16):真正理解 RTLD_NEXT 的作用,这里最后我实验的结果是,next会掠过本文件的符号搜索,而是直接链接下一个出现的符号的共享库的符号。
例如
#include <stdio.h>
#include <dlfcn.h>
#include <errno.h>
void first();
void second();
void(*f)();
void load_func() __attribute__((constructor));
void load_func(){
f = (void(*)())dlsym(RTLD_DEFAULT,"_Z13print_messagev");
char *error_str;
error_str = dlerror();
if (error_str != NULL) {
printf("%s\n", error_str);
}
printf("load func first f=%p\n",f);
}
void printmessage(){
printf("the main function\n");
f();
}
int main(){
first();
second();
f();
return 0;
}
// 无论是default和是next,结果都是 first function