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