1.
1.1 共享库
有如下代码共享库与主函数中都有相同的add函数
-
cong@msi:/work/test/ctest/5dlopen_1$ cat libtestadd/libadd.c
-
int add(int a, int b)
-
{
-
return (a+b);
-
}
编译生成共享库:
gcc -g -fPIC -O0 -c -o libadd.o libadd.c
gcc -fPIC -shared libadd.o -o libtestadd.so
1.2 主函数
-
cong@msi:/work/test/ctest/5dlopen_1$ cat main.c
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include "libadd.h"
-
int add(int a, int b)
-
{
-
printf("main.c: add\n");
-
return 0;
-
}
-
int main (int argc, char *argv[])
-
{
-
printf("add=%d\n",add(1,2));
-
return 0;
-
}
gcc -g -O0 -I./libtestadd -c -o main.o main.c
gcc main.o -o main -L./libtestadd/ -ltestadd -Wl,-rpath,libtestadd
1.3 运行结果
-
cong@msi:/work/test/ctest/5dlopen_1$ ./main
-
main.c: add -->发现调用的是主函数的add
-
add=0
用ldd及readelf查看
-
cong@msi:/work/test/ctest/5dlopen_1$ ldd main
-
linux-vdso.so.1 => (0x00007ffd975d9000) -->这儿根本就没有去链接libtestadd.so
-
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f6ca45c9000)
-
/lib64/ld-linux-x86-64.so.2 (0x00007f6ca49b5000)
-
cong@msi:/work/test/ctest/5dlopen_1$
-
cong@msi:/work/test/ctest/5dlopen_1$ readelf -d ./main
-
-
Dynamic section at offset 0xe18 contains 25 entries:
-
Tag Type Name/Value
-
0x0000000000000001 (NEEDED) Shared library: [libc.so.6] -->需要的共享库只有libc.so根本就没有用libtestadd.so
-
0x000000000000000f (RPATH) Library rpath: [libtestadd]
-
0x000000000000000c (INIT) 0x400428
将main.c中的add函数注掉之后,再次用ldd及readelf查看:
-
cong@msi:/work/test/ctest/5dlopen_1$ ldd main
-
linux-vdso.so.1 => (0x00007ffd43f39000)
-
libtestadd.so => libtestadd/libtestadd.so (0x00007fd3bebd0000) -->去掉main中的add才会用到libtestadd.so这个共享库
-
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd3be7e6000)
-
/lib64/ld-linux-x86-64.so.2 (0x00007fd3bedd4000)
-
cong@msi:/work/test/ctest/5dlopen_1$
-
cong@msi:/work/test/ctest/5dlopen_1$ readelf -d ./main
-
Dynamic section at offset 0xe08 contains 26 entries:
-
Tag Type Name/Value
-
0x0000000000000001 (NEEDED) Shared library: [libtestadd.so] -->去掉main中的add才会用到共享库中的add
-
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
-
0x000000000000000f (RPATH) Library rpath: [libtestadd]
-
0x000000000000000c (INIT) 0x400580
1.4 代码下载
5dlopen_1.rar (下载后改名为5dlopen_1.tar.gz)
2. 运行时加载库
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <dlfcn.h>
-
int add(int a, int b)
-
{
-
printf("add in main.c\n");
-
return (a+b);
-
}
-
#define TEST_LIB_PATH "./libtestadd/libtestadd.so"
-
int main (int argc, char *argv[])
-
{
-
int result;
-
void* dp = NULL;
-
int (*add_func)(int, int);
-
dp = dlopen(TEST_LIB_PATH, RTLD_LAZY );
-
if(dp==NULL)
-
{
-
printf("dlopen failed\n");
-
return 0;
-
}
-
-
add_func = dlsym(dp, "add");
-
result = add_func(3, 4);
-
printf("addr of add_func=%p\n", add_func);
-
printf("dlopen: add_func=%d\n", result);
-
dlclose(dp);
-
-
printf("add=%d\n",add(1,2));
-
return 0;
-
}
2.3 运行结果
-
cong@msi:/work/test/ctest/5dlopen$ ./main
-
addr of add_func=0x7f192e886675
-
dlopen: add_func=7 -->运行了libtestadd.so中的add
-
add in main.c
-
add=3
用dlopen之后再次用ldd及readelf查看:
-
cong@msi:/work/test/ctest/5dlopen$ ldd main
-
linux-vdso.so.1 => (0x00007ffdea9f0000)
-
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f5d20aa9000) -->需要用到libdl.so,但是自己的libtestadd.so没有用到
-
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f5d206e4000)
-
/lib64/ld-linux-x86-64.so.2 (0x00007f5d20cd4000)
-
cong@msi:/work/test/ctest/5dlopen$ readelf -d ./main
-
-
Dynamic section at offset 0xe18 contains 25 entries:
-
Tag Type Name/Value
-
0x0000000000000001 (NEEDED) Shared library: [libdl.so.2] -->需要用到libdl.so,但是自己的libtestadd.so没有用到
-
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
-
0x000000000000000c (INIT) 0x400588
2.4 代码下载
5dlopen.rar(下载后改名为5dlopen.tar.gz)
阅读(793) | 评论(0) | 转发(0) |