Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2112794
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: LINUX

2016-08-12 17:53:56

1. 
1.1 共享库
有如下代码共享库与主函数中都有相同的add函数
  1. cong@msi:/work/test/ctest/5dlopen_1$ cat libtestadd/libadd.c
  2. int add(int a, int b)
  3. {
  4.     return (a+b);
  5. }
编译生成共享库:
gcc -g -fPIC -O0   -c -o libadd.o libadd.c
gcc -fPIC -shared libadd.o -o libtestadd.so
1.2 主函数
  1. cong@msi:/work/test/ctest/5dlopen_1$ cat main.c
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "libadd.h"
  6. int add(int a, int b)
  7. {
  8.     printf("main.c: add\n");
  9.     return 0;
  10. }
  11. int main (int argc, char *argv[])
  12. {
  13.     printf("add=%d\n",add(1,2));
  14.     return 0;
  15. }
gcc -g -O0 -I./libtestadd    -c -o main.o main.c
gcc main.o -o main -L./libtestadd/ -ltestadd  -Wl,-rpath,libtestadd
1.3 运行结果
  1. cong@msi:/work/test/ctest/5dlopen_1$ ./main
  2. main.c: add                    -->发现调用的是主函数的add
  3. add=0
用ldd及readelf查看
  1. cong@msi:/work/test/ctest/5dlopen_1$ ldd main
  2.     linux-vdso.so.1 => (0x00007ffd975d9000)           -->这儿根本就没有去链接libtestadd.so
  3.     libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f6ca45c9000)
  4.     /lib64/ld-linux-x86-64.so.2 (0x00007f6ca49b5000)
  5. cong@msi:/work/test/ctest/5dlopen_1$ 
  6. cong@msi:/work/test/ctest/5dlopen_1$ readelf -d ./main

  7. Dynamic section at offset 0xe18 contains 25 entries:
  8.   Tag Type Name/Value
  9.  0x0000000000000001 (NEEDED) Shared library: [libc.so.6]   -->需要的共享库只有libc.so根本就没有用libtestadd.so
  10.  0x000000000000000f (RPATH) Library rpath: [libtestadd]
  11.  0x000000000000000c (INIT) 0x400428
将main.c中的add函数注掉之后,再次用ldd及readelf查看:
  1. cong@msi:/work/test/ctest/5dlopen_1$ ldd main
  2.     linux-vdso.so.1 => (0x00007ffd43f39000)
  3.     libtestadd.so => libtestadd/libtestadd.so (0x00007fd3bebd0000)     -->去掉main中的add才会用到libtestadd.so这个共享库
  4.     libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd3be7e6000)
  5.     /lib64/ld-linux-x86-64.so.2 (0x00007fd3bedd4000)
  6. cong@msi:/work/test/ctest/5dlopen_1$ 
  7. cong@msi:/work/test/ctest/5dlopen_1$ readelf -d ./main
  8. Dynamic section at offset 0xe08 contains 26 entries:
  9.   Tag Type Name/Value
  10.  0x0000000000000001 (NEEDED) Shared library: [libtestadd.so]           -->去掉main中的add才会用到共享库中的add
  11.  0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
  12.  0x000000000000000f (RPATH) Library rpath: [libtestadd]
  13.  0x000000000000000c (INIT) 0x400580
1.4 代码下载
5dlopen_1.rar   (下载后改名为5dlopen_1.tar.gz)

2. 运行时加载库
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <dlfcn.h>
  5. int add(int a, int b)
  6. {
  7.     printf("add in main.c\n");
  8.     return (a+b);
  9. }
  10. #define TEST_LIB_PATH "./libtestadd/libtestadd.so"
  11. int main (int argc, char *argv[])
  12. {
  13.     int result;
  14.     void* dp = NULL;
  15.     int (*add_func)(int, int);
  16.     dp = dlopen(TEST_LIB_PATH, RTLD_LAZY );
  17.     if(dp==NULL)
  18.     {
  19.         printf("dlopen failed\n");
  20.         return 0;
  21.     }

  22.     add_func = dlsym(dp, "add");
  23.     result = add_func(3, 4);
  24.     printf("addr of add_func=%p\n", add_func);
  25.     printf("dlopen: add_func=%d\n", result);
  26.     dlclose(dp);

  27.     printf("add=%d\n",add(1,2));
  28.     return 0;
  29. }
2.3 运行结果
  1. cong@msi:/work/test/ctest/5dlopen$ ./main
  2. addr of add_func=0x7f192e886675
  3. dlopen: add_func=7    -->运行了libtestadd.so中的add
  4. add in main.c
  5. add=3
用dlopen之后再次用ldd及readelf查看:
  1. cong@msi:/work/test/ctest/5dlopen$ ldd main
  2.     linux-vdso.so.1 => (0x00007ffdea9f0000)
  3.     libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f5d20aa9000)   -->需要用到libdl.so,但是自己的libtestadd.so没有用到
  4.     libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f5d206e4000)
  5.     /lib64/ld-linux-x86-64.so.2 (0x00007f5d20cd4000)
  6. cong@msi:/work/test/ctest/5dlopen$ readelf -d ./main

  7. Dynamic section at offset 0xe18 contains 25 entries:
  8.   Tag Type Name/Value
  9.  0x0000000000000001 (NEEDED) Shared library: [libdl.so.2]    -->需要用到libdl.so,但是自己的libtestadd.so没有用到
  10.  0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
  11.  0x000000000000000c (INIT) 0x400588
2.4 代码下载
 5dlopen.rar
(下载后改名为5dlopen.tar.gz)

阅读(752) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~