Control Linux dynamic link library (So, standard Object) Export Function

Source: Internet
Author: User

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

Note: Some functions in the references cannot be compiled.

Compiling environment: gcc 4.4.3

In windows, we can specify _ declspec (dllexport) to control the export function. In linux, we also have similar control parameters.

The GCC help document-fvisibility = default | internal | hidden | protected has the following description:

A superior solution made possible by this option
Marking things hidden when the default is public is
Make the default hidden and mark things public. This is
The norm with DLL's on Windows and
-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.

In linux, all functions in the source file have a default visibility attribute of public, and the-fvisibility = hidden parameter is added to the compilation command, changes all default public attributes to hidden. In this case, if the _ attribute _ (visibility ("default") parameter is set for the function, the specific function is still processed according to the default public attribute, the-fvisibility = hidden parameter does not work for this function. Therefore, after the-fvisibility = hidden parameter is set, only functions with _ attribute _ (visibility ("default") are visible to the outside world, this is equivalent to the _ declspec (dllexport) Definition in Visual Studio.

The following is an example:

# Include <stdio. h> <br/> # include <string. h> <br/> # include <stdlib. h> <br/> extern int test (int I); <br/>__ attribute (visibility ("default") int Test2 (int I) <br/>{< br/> test (I); <br/> printf ("this is Test2/N "); <br/>}< br/> int test3 (int I) <br/> {<br/> printf ("this is test 3/N "); <br/>}

Call the following command to compile:

Gcc-shared-O test. So-fvisibility = hidden test. c

To obtain the test. so file, run the following command to view the properties of the so file:

Readelf-s test. So

The following result is displayed:

Symbol table '. dynsym 'ins ins 12 entries: <br/> Num: Value Size Type Bind Vis Ndx Name <br/> 0: 00000000 0 notype local default und <br/> 1: 00000000 0 notype weak default und _ gmon_start __< br/> 2: 00000000 0 notype weak default und _ Jv_RegisterClasses <br/> 3: 00000000 0 notype global default und test <br/> 4: 00000000 0 func global default und puts@GLIBC_2.0 (2) <br/> 5: 00000000 0 func weak default und _ cxa_finalize@GLIBC_2.1.3 (3) <br/> 6: 00002014 0 notype global default abs _ end <br/> 7: 2017200c 0 notype global default abs _ edata <br/> 8: 0000049c 31 func global default 12 test2 <br/> 9: 2017200c 0 notype global default abs _ bss_start <br/> 10: 00000380 0 func global default 10 _ init <br/> 11: 00000508 0 func global default 13 _ fini <br/> Symbol table '. symtab 'ins ins 56 entries: <br/> Num: Value Size Type Bind Vis Ndx Name <br/> 0: 00000000 0 notype local default und <br/> 1: 1000000f4 0 section local default 1 <br/> 2: 00000118 0 section local default 2 <br/> 3: 0000015c 0 section local default 3 <br/> 4: 00000198 0 section local default 4 <br/> 5: 00000258 0 section local default 5 <br/> 6: 000002e0 0 section local default 6 <br/> 7: 000002f8 0 section local default 7 <br/> 8: 00000328 0 section local default 8 <br/> 9: 00000370 0 section local default 9 <br/> 10: 00000380 0 section local default 10 <br/> 11: 000003b0 section local default 11 <br/> 12: 000003e0 0 section local default 12 <br/> 13: 00000508 0 section local default 13 <br/> 14: 00000524 0 section local default 14 <br/> 15: 00000544 0 section local default 15 <br/> 16: limit 1f04 0 section local default 16 <br/> 17: Limit 1f0c 0 section local default 17 <br/> 18: Limit 1f14 0 section local default 18 <br/> 19: limit 1f18 0 section local default 19 <br/> 20: Limit 1fe8 0 section local default 20 <br/> 21: Limit 1ff4 0 section local default 21 <br/> 22: 00002008 0 section local default 22 <br/> 23: Drawing 200C 0 section local default 23 <br/> 24: 00000000 0 section local default 24 <br/> 25: 00000000 0 file local default abs crtstuff. c <br/> 26: 20171f04 0 object local default 16 _ CTOR_LIST __< br/> 27: 20171f0c 0 object local default 17 _ DTOR_LIST __< br/> 28: defaults 1f14 0 object local default 18 _ JCR_LIST __< br/> 29: 000003e0 0 func local default 12 _ do_global_dtors_aux <br/> 30: 2017200c 1 object local default 23 completed.7021 <br/> 31: 00002010 4 object local default 23 dtor_idx.7023 <br/> 32: 00000460 0 func local default 12 frame_dummy <br/> 33: 00000000 0 file local default abs crtstuff. c <br/> 34: 100001f08 0 object local default 16 _ CTOR_END __< br/> 35: 00000544 0 object local default 15 _ FRAME_END __< br/> 36: defaults 1f14 0 object local default 18 _ JCR_END __< br/> 37: 000004d0 0 func local default 12 _ do_global_ctors_aux <br/> 38: 00000000 0 file local default abs test. c <br/> 39: 1271ff4 0 object local hidden abs _ GLOBAL_OFFSET_TABLE _ <br/> 40: 00002008 0 object local hidden 22 _ dso_handle <br/> 41: export 1f10 0 object local hidden 17 _ DTOR_END __< br/> 42: 000004bb 20 func local hidden 12 test3 <br/> 43: 00000497 0 func local hidden 12 _ i686.get _ pc_thunk.bx <br/> 44: Drawing 1f18 0 object local hidden abs _ DYNAMIC <br/> 45: 00000000 0 notype weak default und _ gmon_start __< br/> 46: 00000000 0 notype weak default und _ Jv_RegisterClasses <br/> 47: 00000508 0 func global default 13 _ fini <br/> 48: 0000049c 31 func global default 12 test2 <br/> 49: 2017200c 0 notype global default abs _ bss_start <br/> 50: 00000000 0 notype global default und test <br/> 51: 00002014 0 notype global default abs _ end <br/> 52: 00000000 0 func global default und puts @ GLIBC_2.0 <br/> 53: 2017200c 0 notype global default abs _ edata <br/> 54: 00000000 0 func weak default und _ cxa_finalize @ GLIBC_2.1 <br/> 55: 00000380 0 func global default 10 _ init

It can be seen that the so file contains the test2 () and test3 () functions, and the property of test2 () is default (that is, visible), while test3 () is hidden (invisible ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.