blob: 493b445750e7d9219c7c1a35d2001c7ec4fb6324 [file] [log] [blame]
Eric Fiselier1b57fa82016-09-15 22:27:07 +00001========================
2Symbol Visibility Macros
3========================
4
5.. contents::
6 :local:
7
8Overview
9========
10
11Libc++ uses various "visibility" macros in order to provide a stable ABI in
12both the library and the headers. These macros work by changing the
13visibility and inlining characteristics of the symbols they are applied to.
14
15Visibility Macros
16=================
17
18**_LIBCPP_HIDDEN**
19 Mark a symbol as hidden so it will not be exported from shared libraries.
20
21**_LIBCPP_FUNC_VIS**
22 Mark a symbol as being exported by the libc++ library. This attribute must
23 be applied to the declaration of all functions exported by the libc++ dylib.
24
Eric Fiselier99505072017-01-16 21:01:00 +000025**_LIBCPP_EXTERN_VIS**
26 Mark a symbol as being exported by the libc++ library. This attribute may
27 only be applied to objects defined in the libc++ library. On Windows this
28 macro applies `dllimport`/`dllexport` to the symbol. On all other platforms
29 this macro has no effect.
30
Shoaib Meenai2d71db42016-11-16 22:18:10 +000031**_LIBCPP_OVERRIDABLE_FUNC_VIS**
32 Mark a symbol as being exported by the libc++ library, but allow it to be
33 overridden locally. On non-Windows, this is equivalent to `_LIBCPP_FUNC_VIS`.
34 This macro is applied to all `operator new` and `operator delete` overloads.
35
36 **Windows Behavior**: Any symbol marked `dllimport` cannot be overridden
37 locally, since `dllimport` indicates the symbol should be bound to a separate
38 DLL. All `operator new` and `operator delete` overloads are required to be
39 locally overridable, and therefore must not be marked `dllimport`. On Windows,
40 this macro therefore expands to `__declspec(dllexport)` when building the
41 library and has an empty definition otherwise.
42
Louis Dionne3a197992018-07-27 12:46:03 +000043**_LIBCPP_HIDE_FROM_ABI**
Louis Dionne16fe2952018-07-11 23:14:33 +000044 Mark a function as not being part of the ABI of any final linked image that
45 uses it, and also as being internal to each TU that uses that function. In
46 other words, the address of a function marked with this attribute is not
47 guaranteed to be the same across translation units.
Eric Fiselier1b57fa82016-09-15 22:27:07 +000048
Louis Dionned797b8d2018-08-06 14:11:50 +000049**_LIBCPP_HIDE_FROM_ABI_AFTER_V1**
50 Mark a function as being hidden from the ABI (per `_LIBCPP_HIDE_FROM_ABI`)
51 when libc++ is built with an ABI version after ABI v1. This macro is used to
52 maintain ABI compatibility for symbols that have been historically exported
53 by libc++ in v1 of the ABI, but that we don't want to export in the future.
54
55 This macro works as follows. When we build libc++, we either hide the symbol
56 from the ABI (if the symbol is not part of the ABI in the version we're
57 building), or we leave it included. From user code (i.e. when we're not
58 building libc++), the macro always marks symbols as internal so that programs
59 built using new libc++ headers stop relying on symbols that are removed from
60 the ABI in a future version. Each time we release a new stable version of the
61 ABI, we should create a new _LIBCPP_HIDE_FROM_ABI_AFTER_XXX macro, and we can
62 use it to start removing symbols from the ABI after that stable version.
63
Eric Fiselier1b57fa82016-09-15 22:27:07 +000064**_LIBCPP_TYPE_VIS**
Shoaib Meenai55f3a462017-03-02 03:22:18 +000065 Mark a type's typeinfo, vtable and members as having default visibility.
66 This attribute cannot be used on class templates.
67
68**_LIBCPP_TEMPLATE_VIS**
Eric Fiselier1b57fa82016-09-15 22:27:07 +000069 Mark a type's typeinfo and vtable as having default visibility.
Shoaib Meenai55f3a462017-03-02 03:22:18 +000070 This macro has no effect on the visibility of the type's member functions.
Eric Fiselier1b57fa82016-09-15 22:27:07 +000071
72 **GCC Behavior**: GCC does not support Clang's `type_visibility(...)`
73 attribute. With GCC the `visibility(...)` attribute is used and member
74 functions are affected.
75
Eric Fiselier1b57fa82016-09-15 22:27:07 +000076 **Windows Behavior**: DLLs do not support dllimport/export on class templates.
77 The macro has an empty definition on this platform.
78
Eric Fiselier1b57fa82016-09-15 22:27:07 +000079
80**_LIBCPP_ENUM_VIS**
81 Mark the typeinfo of an enum as having default visibility. This attribute
82 should be applied to all enum declarations.
83
84 **Windows Behavior**: DLLs do not support importing or exporting enumeration
85 typeinfo. The macro has an empty definition on this platform.
86
87 **GCC Behavior**: GCC un-hides the typeinfo for enumerations by default, even
88 if `-fvisibility=hidden` is specified. Additionally applying a visibility
89 attribute to an enum class results in a warning. The macro has an empty
90 definition with GCC.
91
92**_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS**
93 Mark the member functions, typeinfo, and vtable of the type named in
94 a `_LIBCPP_EXTERN_TEMPLATE` declaration as being exported by the libc++ library.
95 This attribute must be specified on all extern class template declarations.
96
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +000097 This macro is used to override the `_LIBCPP_TEMPLATE_VIS` attribute
Eric Fiselier1b57fa82016-09-15 22:27:07 +000098 specified on the primary template and to export the member functions produced
99 by the explicit instantiation in the dylib.
100
101 **GCC Behavior**: GCC ignores visibility attributes applied the type in
102 extern template declarations and applying an attribute results in a warning.
Eric Fiselier0e7ad562017-01-05 00:04:37 +0000103 However since `_LIBCPP_TEMPLATE_VIS` is the same as
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000104 `__attribute__((visibility("default"))` the visibility is already correct.
105 The macro has an empty definition with GCC.
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000106
Shoaib Meenai5ac10672016-09-19 18:29:07 +0000107 **Windows Behavior**: `extern template` and `dllexport` are fundamentally
Shoaib Meenaic0d2abd2017-07-13 20:47:24 +0000108 incompatible *on a class template* on Windows; the former suppresses
Shoaib Meenai5ac10672016-09-19 18:29:07 +0000109 instantiation, while the latter forces it. Specifying both on the same
Shoaib Meenaic0d2abd2017-07-13 20:47:24 +0000110 declaration makes the class template be instantiated, which is not desirable
Shoaib Meenai5ac10672016-09-19 18:29:07 +0000111 inside headers. This macro therefore expands to `dllimport` outside of libc++
112 but nothing inside of it (rather than expanding to `dllexport`); instead, the
113 explicit instantiations themselves are marked as exported. Note that this
Shoaib Meenaic0d2abd2017-07-13 20:47:24 +0000114 applies *only* to extern *class* templates. Extern *function* templates obey
Shoaib Meenai5ac10672016-09-19 18:29:07 +0000115 regular import/export semantics, and applying `dllexport` directly to the
Shoaib Meenaif9f98d12017-07-13 21:35:52 +0000116 extern template declaration (i.e. using `_LIBCPP_FUNC_VIS`) is the correct
117 thing to do for them.
Shoaib Meenai5ac10672016-09-19 18:29:07 +0000118
119**_LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS**
120 Mark the member functions, typeinfo, and vtable of an explicit instantiation
121 of a class template as being exported by the libc++ library. This attribute
Shoaib Meenaic3910782017-07-13 22:08:59 +0000122 must be specified on all class template explicit instantiations.
Shoaib Meenai5ac10672016-09-19 18:29:07 +0000123
124 It is only necessary to mark the explicit instantiation itself (as opposed to
125 the extern template declaration) as exported on Windows, as discussed above.
126 On all other platforms, this macro has an empty definition.
127
Shoaib Meenai69c57412017-03-02 03:02:50 +0000128**_LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS**
129 Mark a symbol as hidden so it will not be exported from shared libraries. This
130 is intended specifically for method templates of either classes marked with
131 `_LIBCPP_TYPE_VIS` or classes with an extern template instantiation
132 declaration marked with `_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS`.
133
134 When building libc++ with hidden visibility, we want explicit template
135 instantiations to export members, which is consistent with existing Windows
136 behavior. We also want classes annotated with `_LIBCPP_TYPE_VIS` to export
137 their members, which is again consistent with existing Windows behavior.
138 Both these changes are necessary for clients to be able to link against a
139 libc++ DSO built with hidden visibility without encountering missing symbols.
140
141 An unfortunate side effect, however, is that method templates of classes
142 either marked `_LIBCPP_TYPE_VIS` or with extern template instantiation
143 declarations marked with `_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS` also get default
144 visibility when instantiated. These methods are often implicitly instantiated
145 inside other libraries which use the libc++ headers, and will therefore end up
146 being exported from those libraries, since those implicit instantiations will
147 receive default visibility. This is not acceptable for libraries that wish to
148 control their visibility, and led to PR30642.
149
150 Consequently, all such problematic method templates are explicitly marked
151 either hidden (via this macro) or inline, so that they don't leak into client
152 libraries. The problematic methods were found by running
153 `bad-visibility-finder <https://github.com/smeenai/bad-visibility-finder>`_
154 against the libc++ headers after making `_LIBCPP_TYPE_VIS` and
155 `_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS` expand to default visibility.
156
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000157**_LIBCPP_EXCEPTION_ABI**
158 Mark the member functions, typeinfo, and vtable of the type as being exported
159 by the libc++ library. This macro must be applied to all *exception types*.
Eric Fiselier0f47c382016-09-16 02:51:26 +0000160 Exception types should be defined directly in namespace `std` and not the
161 versioning namespace. This allows throwing and catching some exception types
162 between libc++ and libstdc++.
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000163
Louis Dionne3a197992018-07-27 12:46:03 +0000164**_LIBCPP_INTERNAL_LINKAGE**
165 Mark the affected entity as having internal linkage (i.e. the `static`
166 keyword in C). This is only a best effort: when the `internal_linkage`
167 attribute is not available, we fall back to forcing the function to be
168 inlined, which approximates internal linkage since an externally visible
169 symbol is never generated for that function. This is an internal macro
170 used as an implementation detail by other visibility macros. Never mark
171 a function or a class with this macro directly.
172
173**_LIBCPP_ALWAYS_INLINE**
174 Forces inlining of the function it is applied to. For visibility purposes,
175 this macro is used to make sure that an externally visible symbol is never
176 generated in an object file when the `internal_linkage` attribute is not
177 available. This is an internal macro used by other visibility macros, and
178 it should not be used directly.
179
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000180Links
181=====
182
183* `[cfe-dev] Visibility in libc++ - 1 <http://lists.llvm.org/pipermail/cfe-dev/2013-July/030610.html>`_
184* `[cfe-dev] Visibility in libc++ - 2 <http://lists.llvm.org/pipermail/cfe-dev/2013-August/031195.html>`_
185* `[libcxx] Visibility fixes for Windows <http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20130805/085461.html>`_