yield: Implement for OS/2 kLIBC.
[gnulib.git] / doc / lib-symbol-visibility.texi
blob2bffbb384405b42d27d270d06f128db3030b5014
1 @node Exported Symbols of Shared Libraries
2 @section Controlling the Exported Symbols of Shared Libraries
4 @c Documentation of gnulib module 'lib-symbol-visibility'.
6 @c Copyright (C) 2005--2006, 2009--2021 Free Software Foundation, Inc.
8 @c Permission is granted to copy, distribute and/or modify this document
9 @c under the terms of the GNU Free Documentation License, Version 1.3 or
10 @c any later version published by the Free Software Foundation; with no
11 @c Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.  A
12 @c copy of the license is at <https://www.gnu.org/licenses/fdl-1.3.en.html>.
14 The @code{lib-symbol-visibility} module allows precise control of the
15 symbols exported by a shared library.  This is useful because
17 @itemize @bullet
18 @item
19 It prevents abuse of undocumented APIs of your library.  Symbols that
20 are not exported from the library cannot be used.  This eliminates the
21 problem that when the maintainer of the library changes internals of the
22 library, maintainers of other projects cry ``breakage''.  Instead, these
23 maintainers are forced to negotiate the desired API from the maintainer
24 of the library.
26 @item
27 It reduces the risk of symbol collision between your library and other
28 libraries.  For example, the symbol @samp{readline} is defined in several
29 libraries, most of which don't have the same semantics and the same calling
30 convention as the GNU readline library.
32 @item
33 It reduces the startup time of programs linked to the library.  This is
34 because the dynamic loader has less symbols to process.
36 @item
37 It allows the compiler to generate better code.  Within a shared library,
38 a call to a function that is a global symbol costs a ``call'' instruction
39 to a code location in the so-called PLT (procedure linkage table) which
40 contains a ``jump'' instruction to the actual function's code.  (This is
41 needed so that the function can be overridden, for example by a function
42 with the same name in the executable or in a shared library interposed
43 with @code{LD_PRELOAD}.) Whereas a call to a function for which the compiler
44 can assume that it is in the same shared library is just a direct ``call''
45 instructions.  Similarly for variables: A reference to a global variable
46 fetches a pointer in the so-called GOT (global offset table); this is a
47 pointer to the variable's memory.  So the code to access it is two memory
48 load instructions.  Whereas for a variable which is known to reside in the
49 same shared library, it is just a direct memory access: one memory load
50 instruction.
51 @end itemize
53 There are traditionally three ways to specify the exported symbols of a
54 shared library.
56 @itemize @bullet
57 @item
58 The programmer specifies the list of symbols to be exported when the
59 shared library is created.  Usually a command-line option is passed
60 to the linker, with the name of a file containing the symbols.
62 The upside of this approach is flexibility: it allows the same code to
63 be used in different libraries with different export lists.  The downsides
64 are: 1. it's a lot of maintenance overhead when the symbol list is platform
65 dependent, 2. it doesn't work well with C++, due to name mangling.
67 @item
68 The programmer specifies a ``hidden'' attribute for every variable and
69 function that shall not be exported.
71 The drawbacks of this approach are: Symbols are still exported from
72 the library by default.  It's a lot of maintenance work to mark every non-
73 exported variable and function.  But usually the exported API is quite small,
74 compared to the internal API of the library.  And it's the wrong paradigm:
75 It doesn't force thinking when introducing new exported API.
77 @item
78 The programmer specifies a ``hidden'' attribute for all files that make up
79 the shared library, and an ``exported'' attribute for those symbols in these
80 files that shall be exported.
82 This is perfect: It burdens the maintainer only for exported API, not
83 for library-internal API@.  And it keeps the annotations in the source code.
84 @end itemize
86 GNU libtool's @option{-export-symbols} option implements the first approach.
87 The script @code{declared.sh} from Gnulib can help to produce the list of
88 symbols.
90 This gnulib module implements the third approach.  For this it relies on
91 GNU GCC 4.0 or newer, namely on its @samp{-fvisibility=hidden} command-line
92 option and the ``visibility'' attribute.  (The ``visibility'' attribute
93 was already supported in GCC 3.4, but without the command line option,
94 introduced in GCC 4.0, the third approach could not be used.)
96 More explanations on this subject can be found in
97 @url{https://gcc.gnu.org/wiki/Visibility}, which contains more details
98 on the GCC features and additional advice for C++ libraries, and in
99 Ulrich Drepper's paper @url{https://www.akkadia.org/drepper/dsohowto.pdf},
100 which also explains other tricks for reducing the startup time impact
101 of shared libraries.
103 The gnulib autoconf macro @code{gl_VISIBILITY} tests for GCC 4.0 or newer.
104 It defines a Makefile variable @code{@@CFLAG_VISIBILITY@@} containing
105 @samp{-fvisibility=hidden} or nothing.  It also defines as a C macro and
106 as a substituted variable: @@HAVE_VISIBILITY@@.  Its value is 1 when symbol
107 visibility control is supported, and 0 otherwise.
109 To use this module in a library, say libfoo, you will do these steps:
111 @enumerate
112 @item
113 Add @code{@@CFLAG_VISIBILITY@@} or (in a Makefile.am)
114 @code{$(CFLAG_VISIBILITY)} to the CFLAGS for the compilation of the sources
115 that make up the library.
117 @item
118 Add a C macro definition, say @samp{-DBUILDING_LIBFOO}, to the CPPFLAGS
119 for the compilation of the sources that make up the library.
121 @item
122 Define a macro specific to your library like this.
123 @smallexample
124 #if BUILDING_LIBFOO && HAVE_VISIBILITY
125 #define LIBFOO_DLL_EXPORTED __attribute__((__visibility__("default")))
126 #else
127 #define LIBFOO_DLL_EXPORTED
128 #endif
129 @end smallexample
130 This macro should be enabled in all public header files of your library.
132 @item
133 Annotate all variable, function and class declarations in all public header
134 files of your library with @samp{LIBFOO_DLL_EXPORTED}.  This annotation
135 can occur at different locations: between the @samp{extern} and the
136 type or return type, or just before the entity being declared, or after
137 the entire declarator.  My preference is to put it right after @samp{extern},
138 so that the declarations in the header files remain halfway readable.
139 @end enumerate
141 Note that the precise control of the exported symbols will not work with
142 other compilers than GCC >= 4.0, and will not work on systems where the
143 assembler or linker lack the support of ``hidden'' visibility.  Therefore,
144 it's good if, in order to reduce the risk of collisions with symbols in
145 other libraries, you continue to use a prefix specific to your library
146 for all non-static variables and functions and for all C++ classes in
147 your library.
149 Note about other compilers: MSVC support can be added easily, by extending
150 the definition of the macro mentioned above, to something like this:
151 @smallexample
152 #if BUILDING_LIBFOO && HAVE_VISIBILITY
153 #define LIBFOO_DLL_EXPORTED __attribute__((__visibility__("default")))
154 #elif BUILDING_LIBFOO && defined _MSC_VER
155 #define LIBFOO_DLL_EXPORTED __declspec(dllexport)
156 #elif defined _MSC_VER
157 #define LIBFOO_DLL_EXPORTED __declspec(dllimport)
158 #else
159 #define LIBFOO_DLL_EXPORTED
160 #endif
161 @end smallexample