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