Eric Fiselier | 833d644 | 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 | |
Eric Fiselier | dae3960 | 2017-01-16 21:01:00 +0000 | [diff] [blame] | 25 | **_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 Meenai | e6479bc | 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 | |
Eric Fiselier | 833d644 | 2016-09-15 22:27:07 +0000 | [diff] [blame] | 43 | **_LIBCPP_INLINE_VISIBILITY** |
| 44 | Mark a function as hidden and force inlining whenever possible. |
| 45 | |
| 46 | **_LIBCPP_ALWAYS_INLINE** |
| 47 | A synonym for `_LIBCPP_INLINE_VISIBILITY` |
| 48 | |
| 49 | **_LIBCPP_TYPE_VIS** |
Shoaib Meenai | 6b73492 | 2017-03-02 03:22:18 +0000 | [diff] [blame] | 50 | Mark a type's typeinfo, vtable and members as having default visibility. |
| 51 | This attribute cannot be used on class templates. |
| 52 | |
| 53 | **_LIBCPP_TEMPLATE_VIS** |
Eric Fiselier | 833d644 | 2016-09-15 22:27:07 +0000 | [diff] [blame] | 54 | Mark a type's typeinfo and vtable as having default visibility. |
Shoaib Meenai | 6b73492 | 2017-03-02 03:22:18 +0000 | [diff] [blame] | 55 | This macro has no effect on the visibility of the type's member functions. |
Eric Fiselier | 833d644 | 2016-09-15 22:27:07 +0000 | [diff] [blame] | 56 | |
| 57 | **GCC Behavior**: GCC does not support Clang's `type_visibility(...)` |
| 58 | attribute. With GCC the `visibility(...)` attribute is used and member |
| 59 | functions are affected. |
| 60 | |
Eric Fiselier | 833d644 | 2016-09-15 22:27:07 +0000 | [diff] [blame] | 61 | **Windows Behavior**: DLLs do not support dllimport/export on class templates. |
| 62 | The macro has an empty definition on this platform. |
| 63 | |
Eric Fiselier | 833d644 | 2016-09-15 22:27:07 +0000 | [diff] [blame] | 64 | |
| 65 | **_LIBCPP_ENUM_VIS** |
| 66 | Mark the typeinfo of an enum as having default visibility. This attribute |
| 67 | should be applied to all enum declarations. |
| 68 | |
| 69 | **Windows Behavior**: DLLs do not support importing or exporting enumeration |
| 70 | typeinfo. The macro has an empty definition on this platform. |
| 71 | |
| 72 | **GCC Behavior**: GCC un-hides the typeinfo for enumerations by default, even |
| 73 | if `-fvisibility=hidden` is specified. Additionally applying a visibility |
| 74 | attribute to an enum class results in a warning. The macro has an empty |
| 75 | definition with GCC. |
| 76 | |
| 77 | **_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS** |
| 78 | Mark the member functions, typeinfo, and vtable of the type named in |
| 79 | a `_LIBCPP_EXTERN_TEMPLATE` declaration as being exported by the libc++ library. |
| 80 | This attribute must be specified on all extern class template declarations. |
| 81 | |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 82 | This macro is used to override the `_LIBCPP_TEMPLATE_VIS` attribute |
Eric Fiselier | 833d644 | 2016-09-15 22:27:07 +0000 | [diff] [blame] | 83 | specified on the primary template and to export the member functions produced |
| 84 | by the explicit instantiation in the dylib. |
| 85 | |
| 86 | **GCC Behavior**: GCC ignores visibility attributes applied the type in |
| 87 | extern template declarations and applying an attribute results in a warning. |
Eric Fiselier | a2e3d1e | 2017-01-05 00:04:37 +0000 | [diff] [blame] | 88 | However since `_LIBCPP_TEMPLATE_VIS` is the same as |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 89 | `__attribute__((visibility("default"))` the visibility is already correct. |
| 90 | The macro has an empty definition with GCC. |
Eric Fiselier | 833d644 | 2016-09-15 22:27:07 +0000 | [diff] [blame] | 91 | |
Shoaib Meenai | e5cbce4 | 2016-09-19 18:29:07 +0000 | [diff] [blame] | 92 | **Windows Behavior**: `extern template` and `dllexport` are fundamentally |
Shoaib Meenai | a3c3d5f | 2017-07-13 20:47:24 +0000 | [diff] [blame] | 93 | incompatible *on a class template* on Windows; the former suppresses |
Shoaib Meenai | e5cbce4 | 2016-09-19 18:29:07 +0000 | [diff] [blame] | 94 | instantiation, while the latter forces it. Specifying both on the same |
Shoaib Meenai | a3c3d5f | 2017-07-13 20:47:24 +0000 | [diff] [blame] | 95 | declaration makes the class template be instantiated, which is not desirable |
Shoaib Meenai | e5cbce4 | 2016-09-19 18:29:07 +0000 | [diff] [blame] | 96 | inside headers. This macro therefore expands to `dllimport` outside of libc++ |
| 97 | but nothing inside of it (rather than expanding to `dllexport`); instead, the |
| 98 | explicit instantiations themselves are marked as exported. Note that this |
Shoaib Meenai | a3c3d5f | 2017-07-13 20:47:24 +0000 | [diff] [blame] | 99 | applies *only* to extern *class* templates. Extern *function* templates obey |
Shoaib Meenai | e5cbce4 | 2016-09-19 18:29:07 +0000 | [diff] [blame] | 100 | regular import/export semantics, and applying `dllexport` directly to the |
Shoaib Meenai | cd75b28 | 2017-07-13 21:35:52 +0000 | [diff] [blame] | 101 | extern template declaration (i.e. using `_LIBCPP_FUNC_VIS`) is the correct |
| 102 | thing to do for them. |
Shoaib Meenai | e5cbce4 | 2016-09-19 18:29:07 +0000 | [diff] [blame] | 103 | |
| 104 | **_LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS** |
| 105 | Mark the member functions, typeinfo, and vtable of an explicit instantiation |
| 106 | of a class template as being exported by the libc++ library. This attribute |
Shoaib Meenai | b58dd3d | 2017-07-13 22:08:59 +0000 | [diff] [blame] | 107 | must be specified on all class template explicit instantiations. |
Shoaib Meenai | e5cbce4 | 2016-09-19 18:29:07 +0000 | [diff] [blame] | 108 | |
| 109 | It is only necessary to mark the explicit instantiation itself (as opposed to |
| 110 | the extern template declaration) as exported on Windows, as discussed above. |
| 111 | On all other platforms, this macro has an empty definition. |
| 112 | |
Shoaib Meenai | 24e8dbd | 2017-03-02 03:02:50 +0000 | [diff] [blame] | 113 | **_LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS** |
| 114 | Mark a symbol as hidden so it will not be exported from shared libraries. This |
| 115 | is intended specifically for method templates of either classes marked with |
| 116 | `_LIBCPP_TYPE_VIS` or classes with an extern template instantiation |
| 117 | declaration marked with `_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS`. |
| 118 | |
| 119 | When building libc++ with hidden visibility, we want explicit template |
| 120 | instantiations to export members, which is consistent with existing Windows |
| 121 | behavior. We also want classes annotated with `_LIBCPP_TYPE_VIS` to export |
| 122 | their members, which is again consistent with existing Windows behavior. |
| 123 | Both these changes are necessary for clients to be able to link against a |
| 124 | libc++ DSO built with hidden visibility without encountering missing symbols. |
| 125 | |
| 126 | An unfortunate side effect, however, is that method templates of classes |
| 127 | either marked `_LIBCPP_TYPE_VIS` or with extern template instantiation |
| 128 | declarations marked with `_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS` also get default |
| 129 | visibility when instantiated. These methods are often implicitly instantiated |
| 130 | inside other libraries which use the libc++ headers, and will therefore end up |
| 131 | being exported from those libraries, since those implicit instantiations will |
| 132 | receive default visibility. This is not acceptable for libraries that wish to |
| 133 | control their visibility, and led to PR30642. |
| 134 | |
| 135 | Consequently, all such problematic method templates are explicitly marked |
| 136 | either hidden (via this macro) or inline, so that they don't leak into client |
| 137 | libraries. The problematic methods were found by running |
| 138 | `bad-visibility-finder <https://github.com/smeenai/bad-visibility-finder>`_ |
| 139 | against the libc++ headers after making `_LIBCPP_TYPE_VIS` and |
| 140 | `_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS` expand to default visibility. |
| 141 | |
Eric Fiselier | 6dbed46 | 2016-09-16 00:00:48 +0000 | [diff] [blame] | 142 | **_LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY** |
Eric Fiselier | eb6b13f | 2016-10-31 02:07:23 +0000 | [diff] [blame] | 143 | Mark a member function of a class template as visible and always inline. This |
| 144 | macro should only be applied to member functions of class templates that are |
| 145 | externally instantiated. It is important that these symbols are not marked |
| 146 | as hidden as that will prevent the dylib definition from being found. |
Eric Fiselier | 6dbed46 | 2016-09-16 00:00:48 +0000 | [diff] [blame] | 147 | |
| 148 | This macro is used to maintain ABI compatibility for symbols that have been |
Eric Fiselier | eb6b13f | 2016-10-31 02:07:23 +0000 | [diff] [blame] | 149 | historically exported by the libc++ library but are now marked inline. |
Eric Fiselier | 6dbed46 | 2016-09-16 00:00:48 +0000 | [diff] [blame] | 150 | |
Eric Fiselier | 833d644 | 2016-09-15 22:27:07 +0000 | [diff] [blame] | 151 | **_LIBCPP_EXCEPTION_ABI** |
| 152 | Mark the member functions, typeinfo, and vtable of the type as being exported |
| 153 | by the libc++ library. This macro must be applied to all *exception types*. |
Eric Fiselier | 65d504e | 2016-09-16 02:51:26 +0000 | [diff] [blame] | 154 | Exception types should be defined directly in namespace `std` and not the |
| 155 | versioning namespace. This allows throwing and catching some exception types |
| 156 | between libc++ and libstdc++. |
Eric Fiselier | 833d644 | 2016-09-15 22:27:07 +0000 | [diff] [blame] | 157 | |
Eric Fiselier | 833d644 | 2016-09-15 22:27:07 +0000 | [diff] [blame] | 158 | Links |
| 159 | ===== |
| 160 | |
| 161 | * `[cfe-dev] Visibility in libc++ - 1 <http://lists.llvm.org/pipermail/cfe-dev/2013-July/030610.html>`_ |
| 162 | * `[cfe-dev] Visibility in libc++ - 2 <http://lists.llvm.org/pipermail/cfe-dev/2013-August/031195.html>`_ |
| 163 | * `[libcxx] Visibility fixes for Windows <http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20130805/085461.html>`_ |