VisibilityMacros.rst 11 KB

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