1*58b9f456SAndroid Build Coastguard Worker======================== 2*58b9f456SAndroid Build Coastguard WorkerSymbol Visibility Macros 3*58b9f456SAndroid Build Coastguard Worker======================== 4*58b9f456SAndroid Build Coastguard Worker 5*58b9f456SAndroid Build Coastguard Worker.. contents:: 6*58b9f456SAndroid Build Coastguard Worker :local: 7*58b9f456SAndroid Build Coastguard Worker 8*58b9f456SAndroid Build Coastguard WorkerOverview 9*58b9f456SAndroid Build Coastguard Worker======== 10*58b9f456SAndroid Build Coastguard Worker 11*58b9f456SAndroid Build Coastguard WorkerLibc++ uses various "visibility" macros in order to provide a stable ABI in 12*58b9f456SAndroid Build Coastguard Workerboth the library and the headers. These macros work by changing the 13*58b9f456SAndroid Build Coastguard Workervisibility and inlining characteristics of the symbols they are applied to. 14*58b9f456SAndroid Build Coastguard Worker 15*58b9f456SAndroid Build Coastguard WorkerVisibility Macros 16*58b9f456SAndroid Build Coastguard Worker================= 17*58b9f456SAndroid Build Coastguard Worker 18*58b9f456SAndroid Build Coastguard Worker**_LIBCPP_HIDDEN** 19*58b9f456SAndroid Build Coastguard Worker Mark a symbol as hidden so it will not be exported from shared libraries. 20*58b9f456SAndroid Build Coastguard Worker 21*58b9f456SAndroid Build Coastguard Worker**_LIBCPP_FUNC_VIS** 22*58b9f456SAndroid Build Coastguard Worker Mark a symbol as being exported by the libc++ library. This attribute must 23*58b9f456SAndroid Build Coastguard Worker be applied to the declaration of all functions exported by the libc++ dylib. 24*58b9f456SAndroid Build Coastguard Worker 25*58b9f456SAndroid Build Coastguard Worker**_LIBCPP_EXPORTED_FROM_ABI** 26*58b9f456SAndroid Build Coastguard Worker Mark a symbol as being exported by the libc++ library. This attribute may 27*58b9f456SAndroid Build Coastguard Worker only be applied to objects defined in the libc++ runtime library. On Windows, 28*58b9f456SAndroid Build Coastguard Worker this macro applies `dllimport`/`dllexport` to the symbol, and on other 29*58b9f456SAndroid Build Coastguard Worker platforms it gives the symbol default visibility. 30*58b9f456SAndroid Build Coastguard Worker 31*58b9f456SAndroid Build Coastguard Worker**_LIBCPP_OVERRIDABLE_FUNC_VIS** 32*58b9f456SAndroid Build Coastguard Worker Mark a symbol as being exported by the libc++ library, but allow it to be 33*58b9f456SAndroid Build Coastguard Worker overridden locally. On non-Windows, this is equivalent to `_LIBCPP_FUNC_VIS`. 34*58b9f456SAndroid Build Coastguard Worker This macro is applied to all `operator new` and `operator delete` overloads. 35*58b9f456SAndroid Build Coastguard Worker 36*58b9f456SAndroid Build Coastguard Worker **Windows Behavior**: Any symbol marked `dllimport` cannot be overridden 37*58b9f456SAndroid Build Coastguard Worker locally, since `dllimport` indicates the symbol should be bound to a separate 38*58b9f456SAndroid Build Coastguard Worker DLL. All `operator new` and `operator delete` overloads are required to be 39*58b9f456SAndroid Build Coastguard Worker locally overridable, and therefore must not be marked `dllimport`. On Windows, 40*58b9f456SAndroid Build Coastguard Worker this macro therefore expands to `__declspec(dllexport)` when building the 41*58b9f456SAndroid Build Coastguard Worker library and has an empty definition otherwise. 42*58b9f456SAndroid Build Coastguard Worker 43*58b9f456SAndroid Build Coastguard Worker**_LIBCPP_HIDE_FROM_ABI** 44*58b9f456SAndroid Build Coastguard Worker Mark a function as not being part of the ABI of any final linked image that 45*58b9f456SAndroid Build Coastguard Worker uses it. 46*58b9f456SAndroid Build Coastguard Worker 47*58b9f456SAndroid Build Coastguard Worker**_LIBCPP_HIDE_FROM_ABI_AFTER_V1** 48*58b9f456SAndroid Build Coastguard Worker Mark a function as being hidden from the ABI (per `_LIBCPP_HIDE_FROM_ABI`) 49*58b9f456SAndroid Build Coastguard Worker when libc++ is built with an ABI version after ABI v1. This macro is used to 50*58b9f456SAndroid Build Coastguard Worker maintain ABI compatibility for symbols that have been historically exported 51*58b9f456SAndroid Build Coastguard Worker by libc++ in v1 of the ABI, but that we don't want to export in the future. 52*58b9f456SAndroid Build Coastguard Worker 53*58b9f456SAndroid Build Coastguard Worker This macro works as follows. When we build libc++, we either hide the symbol 54*58b9f456SAndroid Build Coastguard Worker from the ABI (if the symbol is not part of the ABI in the version we're 55*58b9f456SAndroid Build Coastguard Worker building), or we leave it included. From user code (i.e. when we're not 56*58b9f456SAndroid Build Coastguard Worker building libc++), the macro always marks symbols as internal so that programs 57*58b9f456SAndroid Build Coastguard Worker built using new libc++ headers stop relying on symbols that are removed from 58*58b9f456SAndroid Build Coastguard Worker the ABI in a future version. Each time we release a new stable version of the 59*58b9f456SAndroid Build Coastguard Worker ABI, we should create a new _LIBCPP_HIDE_FROM_ABI_AFTER_XXX macro, and we can 60*58b9f456SAndroid Build Coastguard Worker use it to start removing symbols from the ABI after that stable version. 61*58b9f456SAndroid Build Coastguard Worker 62*58b9f456SAndroid Build Coastguard Worker**_LIBCPP_HIDE_FROM_ABI_PER_TU** 63*58b9f456SAndroid Build Coastguard Worker This macro controls whether symbols hidden from the ABI with `_LIBCPP_HIDE_FROM_ABI` 64*58b9f456SAndroid Build Coastguard Worker are local to each translation unit in addition to being local to each final 65*58b9f456SAndroid Build Coastguard Worker linked image. This macro is defined to either 0 or 1. When it is defined to 66*58b9f456SAndroid Build Coastguard Worker 1, translation units compiled with different versions of libc++ can be linked 67*58b9f456SAndroid Build Coastguard Worker together, since all non ABI-facing functions are local to each translation unit. 68*58b9f456SAndroid Build Coastguard Worker This allows static archives built with different versions of libc++ to be linked 69*58b9f456SAndroid Build Coastguard Worker together. This also means that functions marked with `_LIBCPP_HIDE_FROM_ABI` 70*58b9f456SAndroid Build Coastguard Worker are not guaranteed to have the same address across translation unit boundaries. 71*58b9f456SAndroid Build Coastguard Worker 72*58b9f456SAndroid Build Coastguard Worker When the macro is defined to 0, there is no guarantee that translation units 73*58b9f456SAndroid Build Coastguard Worker compiled with different versions of libc++ can interoperate. However, this 74*58b9f456SAndroid Build Coastguard Worker leads to code size improvements, since non ABI-facing functions can be 75*58b9f456SAndroid Build Coastguard Worker deduplicated across translation unit boundaries. 76*58b9f456SAndroid Build Coastguard Worker 77*58b9f456SAndroid Build Coastguard Worker This macro can be defined by users to control the behavior they want from 78*58b9f456SAndroid Build Coastguard Worker libc++. The default value of this macro (0 or 1) is controlled by whether 79*58b9f456SAndroid Build Coastguard Worker `_LIBCPP_HIDE_FROM_ABI_PER_TU_BY_DEFAULT` is defined, which is intended to 80*58b9f456SAndroid Build Coastguard Worker be used by vendors only (see below). 81*58b9f456SAndroid Build Coastguard Worker 82*58b9f456SAndroid Build Coastguard Worker**_LIBCPP_HIDE_FROM_ABI_PER_TU_BY_DEFAULT** 83*58b9f456SAndroid Build Coastguard Worker This macro controls the default value for `_LIBCPP_HIDE_FROM_ABI_PER_TU`. 84*58b9f456SAndroid Build Coastguard Worker When the macro is defined, per TU ABI insulation is enabled by default, and 85*58b9f456SAndroid Build Coastguard Worker `_LIBCPP_HIDE_FROM_ABI_PER_TU` is defined to 1 unless overridden by users. 86*58b9f456SAndroid Build Coastguard Worker Otherwise, per TU ABI insulation is disabled by default, and 87*58b9f456SAndroid Build Coastguard Worker `_LIBCPP_HIDE_FROM_ABI_PER_TU` is defined to 0 unless overridden by users. 88*58b9f456SAndroid Build Coastguard Worker 89*58b9f456SAndroid Build Coastguard Worker This macro is intended for vendors to control whether they want to ship 90*58b9f456SAndroid Build Coastguard Worker libc++ with per TU ABI insulation enabled by default. Users can always 91*58b9f456SAndroid Build Coastguard Worker control the behavior they want by defining `_LIBCPP_HIDE_FROM_ABI_PER_TU` 92*58b9f456SAndroid Build Coastguard Worker appropriately. 93*58b9f456SAndroid Build Coastguard Worker 94*58b9f456SAndroid Build Coastguard Worker By default, this macro is not defined, which means that per TU ABI insulation 95*58b9f456SAndroid Build Coastguard Worker is not provided unless explicitly overridden by users. 96*58b9f456SAndroid Build Coastguard Worker 97*58b9f456SAndroid Build Coastguard Worker**_LIBCPP_TYPE_VIS** 98*58b9f456SAndroid Build Coastguard Worker Mark a type's typeinfo, vtable and members as having default visibility. 99*58b9f456SAndroid Build Coastguard Worker This attribute cannot be used on class templates. 100*58b9f456SAndroid Build Coastguard Worker 101*58b9f456SAndroid Build Coastguard Worker**_LIBCPP_TEMPLATE_VIS** 102*58b9f456SAndroid Build Coastguard Worker Mark a type's typeinfo and vtable as having default visibility. 103*58b9f456SAndroid Build Coastguard Worker This macro has no effect on the visibility of the type's member functions. 104*58b9f456SAndroid Build Coastguard Worker 105*58b9f456SAndroid Build Coastguard Worker **GCC Behavior**: GCC does not support Clang's `type_visibility(...)` 106*58b9f456SAndroid Build Coastguard Worker attribute. With GCC the `visibility(...)` attribute is used and member 107*58b9f456SAndroid Build Coastguard Worker functions are affected. 108*58b9f456SAndroid Build Coastguard Worker 109*58b9f456SAndroid Build Coastguard Worker **Windows Behavior**: DLLs do not support dllimport/export on class templates. 110*58b9f456SAndroid Build Coastguard Worker The macro has an empty definition on this platform. 111*58b9f456SAndroid Build Coastguard Worker 112*58b9f456SAndroid Build Coastguard Worker 113*58b9f456SAndroid Build Coastguard Worker**_LIBCPP_ENUM_VIS** 114*58b9f456SAndroid Build Coastguard Worker Mark the typeinfo of an enum as having default visibility. This attribute 115*58b9f456SAndroid Build Coastguard Worker should be applied to all enum declarations. 116*58b9f456SAndroid Build Coastguard Worker 117*58b9f456SAndroid Build Coastguard Worker **Windows Behavior**: DLLs do not support importing or exporting enumeration 118*58b9f456SAndroid Build Coastguard Worker typeinfo. The macro has an empty definition on this platform. 119*58b9f456SAndroid Build Coastguard Worker 120*58b9f456SAndroid Build Coastguard Worker **GCC Behavior**: GCC un-hides the typeinfo for enumerations by default, even 121*58b9f456SAndroid Build Coastguard Worker if `-fvisibility=hidden` is specified. Additionally applying a visibility 122*58b9f456SAndroid Build Coastguard Worker attribute to an enum class results in a warning. The macro has an empty 123*58b9f456SAndroid Build Coastguard Worker definition with GCC. 124*58b9f456SAndroid Build Coastguard Worker 125*58b9f456SAndroid Build Coastguard Worker**_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS** 126*58b9f456SAndroid Build Coastguard Worker Mark the member functions, typeinfo, and vtable of the type named in 127*58b9f456SAndroid Build Coastguard Worker a `_LIBCPP_EXTERN_TEMPLATE` declaration as being exported by the libc++ library. 128*58b9f456SAndroid Build Coastguard Worker This attribute must be specified on all extern class template declarations. 129*58b9f456SAndroid Build Coastguard Worker 130*58b9f456SAndroid Build Coastguard Worker This macro is used to override the `_LIBCPP_TEMPLATE_VIS` attribute 131*58b9f456SAndroid Build Coastguard Worker specified on the primary template and to export the member functions produced 132*58b9f456SAndroid Build Coastguard Worker by the explicit instantiation in the dylib. 133*58b9f456SAndroid Build Coastguard Worker 134*58b9f456SAndroid Build Coastguard Worker **GCC Behavior**: GCC ignores visibility attributes applied the type in 135*58b9f456SAndroid Build Coastguard Worker extern template declarations and applying an attribute results in a warning. 136*58b9f456SAndroid Build Coastguard Worker However since `_LIBCPP_TEMPLATE_VIS` is the same as 137*58b9f456SAndroid Build Coastguard Worker `__attribute__((visibility("default"))` the visibility is already correct. 138*58b9f456SAndroid Build Coastguard Worker The macro has an empty definition with GCC. 139*58b9f456SAndroid Build Coastguard Worker 140*58b9f456SAndroid Build Coastguard Worker **Windows Behavior**: `extern template` and `dllexport` are fundamentally 141*58b9f456SAndroid Build Coastguard Worker incompatible *on a class template* on Windows; the former suppresses 142*58b9f456SAndroid Build Coastguard Worker instantiation, while the latter forces it. Specifying both on the same 143*58b9f456SAndroid Build Coastguard Worker declaration makes the class template be instantiated, which is not desirable 144*58b9f456SAndroid Build Coastguard Worker inside headers. This macro therefore expands to `dllimport` outside of libc++ 145*58b9f456SAndroid Build Coastguard Worker but nothing inside of it (rather than expanding to `dllexport`); instead, the 146*58b9f456SAndroid Build Coastguard Worker explicit instantiations themselves are marked as exported. Note that this 147*58b9f456SAndroid Build Coastguard Worker applies *only* to extern *class* templates. Extern *function* templates obey 148*58b9f456SAndroid Build Coastguard Worker regular import/export semantics, and applying `dllexport` directly to the 149*58b9f456SAndroid Build Coastguard Worker extern template declaration (i.e. using `_LIBCPP_FUNC_VIS`) is the correct 150*58b9f456SAndroid Build Coastguard Worker thing to do for them. 151*58b9f456SAndroid Build Coastguard Worker 152*58b9f456SAndroid Build Coastguard Worker**_LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS** 153*58b9f456SAndroid Build Coastguard Worker Mark the member functions, typeinfo, and vtable of an explicit instantiation 154*58b9f456SAndroid Build Coastguard Worker of a class template as being exported by the libc++ library. This attribute 155*58b9f456SAndroid Build Coastguard Worker must be specified on all class template explicit instantiations. 156*58b9f456SAndroid Build Coastguard Worker 157*58b9f456SAndroid Build Coastguard Worker It is only necessary to mark the explicit instantiation itself (as opposed to 158*58b9f456SAndroid Build Coastguard Worker the extern template declaration) as exported on Windows, as discussed above. 159*58b9f456SAndroid Build Coastguard Worker On all other platforms, this macro has an empty definition. 160*58b9f456SAndroid Build Coastguard Worker 161*58b9f456SAndroid Build Coastguard Worker**_LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS** 162*58b9f456SAndroid Build Coastguard Worker Mark a symbol as hidden so it will not be exported from shared libraries. This 163*58b9f456SAndroid Build Coastguard Worker is intended specifically for method templates of either classes marked with 164*58b9f456SAndroid Build Coastguard Worker `_LIBCPP_TYPE_VIS` or classes with an extern template instantiation 165*58b9f456SAndroid Build Coastguard Worker declaration marked with `_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS`. 166*58b9f456SAndroid Build Coastguard Worker 167*58b9f456SAndroid Build Coastguard Worker When building libc++ with hidden visibility, we want explicit template 168*58b9f456SAndroid Build Coastguard Worker instantiations to export members, which is consistent with existing Windows 169*58b9f456SAndroid Build Coastguard Worker behavior. We also want classes annotated with `_LIBCPP_TYPE_VIS` to export 170*58b9f456SAndroid Build Coastguard Worker their members, which is again consistent with existing Windows behavior. 171*58b9f456SAndroid Build Coastguard Worker Both these changes are necessary for clients to be able to link against a 172*58b9f456SAndroid Build Coastguard Worker libc++ DSO built with hidden visibility without encountering missing symbols. 173*58b9f456SAndroid Build Coastguard Worker 174*58b9f456SAndroid Build Coastguard Worker An unfortunate side effect, however, is that method templates of classes 175*58b9f456SAndroid Build Coastguard Worker either marked `_LIBCPP_TYPE_VIS` or with extern template instantiation 176*58b9f456SAndroid Build Coastguard Worker declarations marked with `_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS` also get default 177*58b9f456SAndroid Build Coastguard Worker visibility when instantiated. These methods are often implicitly instantiated 178*58b9f456SAndroid Build Coastguard Worker inside other libraries which use the libc++ headers, and will therefore end up 179*58b9f456SAndroid Build Coastguard Worker being exported from those libraries, since those implicit instantiations will 180*58b9f456SAndroid Build Coastguard Worker receive default visibility. This is not acceptable for libraries that wish to 181*58b9f456SAndroid Build Coastguard Worker control their visibility, and led to PR30642. 182*58b9f456SAndroid Build Coastguard Worker 183*58b9f456SAndroid Build Coastguard Worker Consequently, all such problematic method templates are explicitly marked 184*58b9f456SAndroid Build Coastguard Worker either hidden (via this macro) or inline, so that they don't leak into client 185*58b9f456SAndroid Build Coastguard Worker libraries. The problematic methods were found by running 186*58b9f456SAndroid Build Coastguard Worker `bad-visibility-finder <https://github.com/smeenai/bad-visibility-finder>`_ 187*58b9f456SAndroid Build Coastguard Worker against the libc++ headers after making `_LIBCPP_TYPE_VIS` and 188*58b9f456SAndroid Build Coastguard Worker `_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS` expand to default visibility. 189*58b9f456SAndroid Build Coastguard Worker 190*58b9f456SAndroid Build Coastguard Worker**_LIBCPP_EXCEPTION_ABI** 191*58b9f456SAndroid Build Coastguard Worker Mark the member functions, typeinfo, and vtable of the type as being exported 192*58b9f456SAndroid Build Coastguard Worker by the libc++ library. This macro must be applied to all *exception types*. 193*58b9f456SAndroid Build Coastguard Worker Exception types should be defined directly in namespace `std` and not the 194*58b9f456SAndroid Build Coastguard Worker versioning namespace. This allows throwing and catching some exception types 195*58b9f456SAndroid Build Coastguard Worker between libc++ and libstdc++. 196*58b9f456SAndroid Build Coastguard Worker 197*58b9f456SAndroid Build Coastguard Worker**_LIBCPP_INTERNAL_LINKAGE** 198*58b9f456SAndroid Build Coastguard Worker Mark the affected entity as having internal linkage (i.e. the `static` 199*58b9f456SAndroid Build Coastguard Worker keyword in C). This is only a best effort: when the `internal_linkage` 200*58b9f456SAndroid Build Coastguard Worker attribute is not available, we fall back to forcing the function to be 201*58b9f456SAndroid Build Coastguard Worker inlined, which approximates internal linkage since an externally visible 202*58b9f456SAndroid Build Coastguard Worker symbol is never generated for that function. This is an internal macro 203*58b9f456SAndroid Build Coastguard Worker used as an implementation detail by other visibility macros. Never mark 204*58b9f456SAndroid Build Coastguard Worker a function or a class with this macro directly. 205*58b9f456SAndroid Build Coastguard Worker 206*58b9f456SAndroid Build Coastguard Worker**_LIBCPP_ALWAYS_INLINE** 207*58b9f456SAndroid Build Coastguard Worker Forces inlining of the function it is applied to. For visibility purposes, 208*58b9f456SAndroid Build Coastguard Worker this macro is used to make sure that an externally visible symbol is never 209*58b9f456SAndroid Build Coastguard Worker generated in an object file when the `internal_linkage` attribute is not 210*58b9f456SAndroid Build Coastguard Worker available. This is an internal macro used by other visibility macros, and 211*58b9f456SAndroid Build Coastguard Worker it should not be used directly. 212*58b9f456SAndroid Build Coastguard Worker 213*58b9f456SAndroid Build Coastguard WorkerLinks 214*58b9f456SAndroid Build Coastguard Worker===== 215*58b9f456SAndroid Build Coastguard Worker 216*58b9f456SAndroid Build Coastguard Worker* `[cfe-dev] Visibility in libc++ - 1 <http://lists.llvm.org/pipermail/cfe-dev/2013-July/030610.html>`_ 217*58b9f456SAndroid Build Coastguard Worker* `[cfe-dev] Visibility in libc++ - 2 <http://lists.llvm.org/pipermail/cfe-dev/2013-August/031195.html>`_ 218*58b9f456SAndroid Build Coastguard Worker* `[libcxx] Visibility fixes for Windows <http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20130805/085461.html>`_ 219