控制linux动态链接库(so, standard object)导出函数

来源:互联网 发布:linux语言改为中文 编辑:程序博客网 时间:2024/04/29 18:38
http://blog.csdn.net/wangjingfei/archive/2010/06/25/5693535.aspx

参考:http://www.lampchina.net/article/htmls/201006/Mjg4MDMy.html

注意:参考文献中有一些函数是不能编译通过的。

编译环境:gcc 4.4.3

在windows中,我们可以指定__declspec(dllexport)定义来控制导出函数,在linux下,我们也有类似的控制参数。

在GCC帮助文档 -fvisibility=default|internal|hidden|protected参数下有这样一段描述:

a superior solution made possible by this option to marking things hidden when the default is public is to make the default hidden and mark things public. This is the norm with DLL's on Windows and with -fvisibility=hidden and "__attribute__ ((visibility("default")))" instead of "__declspec(dllexport)" you get almost identical semantics with identical syntax. This is a great boon to those working with cross-platform projects.

需要了解的是,在linux下,源文件中的所有函数都有一个默认的visibility属性为public,在编译命令中加入-fvisibility=hidden参数,会将所有默认的public的属性变为hidden。此时,如果对函数设置__attribute__ ((visibility("default")))参数,使特定的函数仍然按默认的public属性处理,则-fvisibility=hidden参数不会对该函数起作用。所以,设置了-fvisibility=hidden参数之后,只有设置了__attribute__ ((visibility("default")))的函数才是对外可见的,如此则效果等同于Visual Studio下的__declspec(dllexport)定义。

下面看一个例子:

[cpp] view plaincopy
  1. #include<stdio.h>  
  2. #include<string.h>  
  3. #include<stdlib.h>  
  4. extern int test (int i);  
  5. __attribute ((visibility("default"))) int test2 (int i)  
  6. {  
  7.      test(i);  
  8.      printf("this is test2/n");  
  9. }  
  10. int test3 (int i)  
  11. {  
  12.      printf("this is test 3/n");  
  13. }  

此时调用以下命令编译:

[ruby] view plaincopy
  1. gcc -shared -o test.so -fvisibility=hidden test.c  

可以获得test.so文件,用以下命令查看so文件属性:

[ruby] view plaincopy
  1. readelf -s test.so  

得到以下结果:

[c-sharp] view plaincopy
  1. Symbol table '.dynsym' contains 12 entries:  
  2.    Num:    Value  Size Type    Bind   Vis      Ndx Name  
  3.      0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND   
  4.      1: 00000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__  
  5.      2: 00000000     0 NOTYPE  WEAK   DEFAULT  UND _Jv_RegisterClasses  
  6.      3: 00000000     0 NOTYPE  GLOBAL DEFAULT  UND test  
  7.      4: 00000000     0 FUNC    GLOBAL DEFAULT  UND puts@GLIBC_2.0 (2)  
  8.      5: 00000000     0 FUNC    WEAK   DEFAULT  UND __cxa_finalize@GLIBC_2.1.3 (3)  
  9.      6: 00002014     0 NOTYPE  GLOBAL DEFAULT  ABS _end  
  10.      7: 0000200c     0 NOTYPE  GLOBAL DEFAULT  ABS _edata  
  11.      8: 0000049c    31 FUNC    GLOBAL DEFAULT   12 test2  
  12.      9: 0000200c     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_start  
  13.     10: 00000380     0 FUNC    GLOBAL DEFAULT   10 _init  
  14.     11: 00000508     0 FUNC    GLOBAL DEFAULT   13 _fini  
  15. Symbol table '.symtab' contains 56 entries:  
  16.    Num:    Value  Size Type    Bind   Vis      Ndx Name  
  17.      0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND   
  18.      1: 000000f4     0 SECTION LOCAL  DEFAULT    1   
  19.      2: 00000118     0 SECTION LOCAL  DEFAULT    2   
  20.      3: 0000015c     0 SECTION LOCAL  DEFAULT    3   
  21.      4: 00000198     0 SECTION LOCAL  DEFAULT    4   
  22.      5: 00000258     0 SECTION LOCAL  DEFAULT    5   
  23.      6: 000002e0     0 SECTION LOCAL  DEFAULT    6   
  24.      7: 000002f8     0 SECTION LOCAL  DEFAULT    7   
  25.      8: 00000328     0 SECTION LOCAL  DEFAULT    8   
  26.      9: 00000370     0 SECTION LOCAL  DEFAULT    9   
  27.     10: 00000380     0 SECTION LOCAL  DEFAULT   10   
  28.     11: 000003b0     0 SECTION LOCAL  DEFAULT   11   
  29.     12: 000003e0     0 SECTION LOCAL  DEFAULT   12   
  30.     13: 00000508     0 SECTION LOCAL  DEFAULT   13   
  31.     14: 00000524     0 SECTION LOCAL  DEFAULT   14   
  32.     15: 00000544     0 SECTION LOCAL  DEFAULT   15   
  33.     16: 00001f04     0 SECTION LOCAL  DEFAULT   16   
  34.     17: 00001f0c     0 SECTION LOCAL  DEFAULT   17   
  35.     18: 00001f14     0 SECTION LOCAL  DEFAULT   18   
  36.     19: 00001f18     0 SECTION LOCAL  DEFAULT   19   
  37.     20: 00001fe8     0 SECTION LOCAL  DEFAULT   20   
  38.     21: 00001ff4     0 SECTION LOCAL  DEFAULT   21   
  39.     22: 00002008     0 SECTION LOCAL  DEFAULT   22   
  40.     23: 0000200c     0 SECTION LOCAL  DEFAULT   23   
  41.     24: 00000000     0 SECTION LOCAL  DEFAULT   24   
  42.     25: 00000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c  
  43.     26: 00001f04     0 OBJECT  LOCAL  DEFAULT   16 __CTOR_LIST__  
  44.     27: 00001f0c     0 OBJECT  LOCAL  DEFAULT   17 __DTOR_LIST__  
  45.     28: 00001f14     0 OBJECT  LOCAL  DEFAULT   18 __JCR_LIST__  
  46.     29: 000003e0     0 FUNC    LOCAL  DEFAULT   12 __do_global_dtors_aux  
  47.     30: 0000200c     1 OBJECT  LOCAL  DEFAULT   23 completed.7021  
  48.     31: 00002010     4 OBJECT  LOCAL  DEFAULT   23 dtor_idx.7023  
  49.     32: 00000460     0 FUNC    LOCAL  DEFAULT   12 frame_dummy  
  50.     33: 00000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c  
  51.     34: 00001f08     0 OBJECT  LOCAL  DEFAULT   16 __CTOR_END__  
  52.     35: 00000544     0 OBJECT  LOCAL  DEFAULT   15 __FRAME_END__  
  53.     36: 00001f14     0 OBJECT  LOCAL  DEFAULT   18 __JCR_END__  
  54.     37: 000004d0     0 FUNC    LOCAL  DEFAULT   12 __do_global_ctors_aux  
  55.     38: 00000000     0 FILE    LOCAL  DEFAULT  ABS test.c  
  56.     39: 00001ff4     0 OBJECT  LOCAL  HIDDEN  ABS _GLOBAL_OFFSET_TABLE_  
  57.     40: 00002008     0 OBJECT  LOCAL  HIDDEN   22 __dso_handle  
  58.     41: 00001f10     0 OBJECT  LOCAL  HIDDEN   17 __DTOR_END__  
  59.     42: 000004bb    20 FUNC    LOCAL  HIDDEN   12 test3  
  60.     43: 00000497     0 FUNC    LOCAL  HIDDEN   12 __i686.get_pc_thunk.bx  
  61.     44: 00001f18     0 OBJECT  LOCAL  HIDDEN  ABS _DYNAMIC  
  62.     45: 00000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__  
  63.     46: 00000000     0 NOTYPE  WEAK   DEFAULT  UND _Jv_RegisterClasses  
  64.     47: 00000508     0 FUNC    GLOBAL DEFAULT   13 _fini  
  65.     48: 0000049c    31 FUNC    GLOBAL DEFAULT   12 test2  
  66.     49: 0000200c     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_start  
  67.     50: 00000000     0 NOTYPE  GLOBAL DEFAULT  UND test  
  68.     51: 00002014     0 NOTYPE  GLOBAL DEFAULT  ABS _end  
  69.     52: 00000000     0 FUNC    GLOBAL DEFAULT  UND puts@@GLIBC_2.0  
  70.     53: 0000200c     0 NOTYPE  GLOBAL DEFAULT  ABS _edata  
  71.     54: 00000000     0 FUNC    WEAK   DEFAULT  UND __cxa_finalize@@GLIBC_2.1  
  72.     55: 00000380     0 FUNC    GLOBAL DEFAULT   10 _init  

可以看出,so文件中包含test2()和test3()这两个函数,并且test2()的属性为default(即可见),而test3()的属性则为hidden(不可见)。

0 0
原创粉丝点击