VisibilityMacros.rst 7.9 KB

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