xref: /openbsd-src/gnu/llvm/libcxx/docs/DesignDocs/VisibilityMacros.rst (revision 4bdff4bed0e3d54e55670334c7d0077db4170f86)
146035553Spatrick========================
246035553SpatrickSymbol Visibility Macros
346035553Spatrick========================
446035553Spatrick
546035553Spatrick.. contents::
646035553Spatrick   :local:
746035553Spatrick
876d0caaeSpatrick.. _visibility-macros:
976d0caaeSpatrick
1046035553SpatrickOverview
1146035553Spatrick========
1246035553Spatrick
1346035553SpatrickLibc++ uses various "visibility" macros in order to provide a stable ABI in
1446035553Spatrickboth the library and the headers. These macros work by changing the
1546035553Spatrickvisibility and inlining characteristics of the symbols they are applied to.
1646035553Spatrick
1746035553SpatrickVisibility Macros
1846035553Spatrick=================
1946035553Spatrick
2046035553Spatrick**_LIBCPP_HIDDEN**
2146035553Spatrick  Mark a symbol as hidden so it will not be exported from shared libraries.
2246035553Spatrick
2346035553Spatrick**_LIBCPP_FUNC_VIS**
2446035553Spatrick  Mark a symbol as being exported by the libc++ library. This attribute must
2546035553Spatrick  be applied to the declaration of all functions exported by the libc++ dylib.
2646035553Spatrick
2746035553Spatrick**_LIBCPP_EXPORTED_FROM_ABI**
2846035553Spatrick  Mark a symbol as being exported by the libc++ library. This attribute may
2946035553Spatrick  only be applied to objects defined in the libc++ runtime library. On Windows,
3046035553Spatrick  this macro applies `dllimport`/`dllexport` to the symbol, and on other
3146035553Spatrick  platforms it gives the symbol default visibility.
3246035553Spatrick
3346035553Spatrick**_LIBCPP_OVERRIDABLE_FUNC_VIS**
3446035553Spatrick  Mark a symbol as being exported by the libc++ library, but allow it to be
3546035553Spatrick  overridden locally. On non-Windows, this is equivalent to `_LIBCPP_FUNC_VIS`.
3646035553Spatrick  This macro is applied to all `operator new` and `operator delete` overloads.
3746035553Spatrick
3846035553Spatrick  **Windows Behavior**: Any symbol marked `dllimport` cannot be overridden
3946035553Spatrick  locally, since `dllimport` indicates the symbol should be bound to a separate
4046035553Spatrick  DLL. All `operator new` and `operator delete` overloads are required to be
4146035553Spatrick  locally overridable, and therefore must not be marked `dllimport`. On Windows,
4246035553Spatrick  this macro therefore expands to `__declspec(dllexport)` when building the
4346035553Spatrick  library and has an empty definition otherwise.
4446035553Spatrick
4546035553Spatrick**_LIBCPP_HIDE_FROM_ABI**
4646035553Spatrick  Mark a function as not being part of the ABI of any final linked image that
4746035553Spatrick  uses it.
4846035553Spatrick
4976d0caaeSpatrick**_LIBCPP_INLINE_VISIBILITY**
5076d0caaeSpatrick  Historical predecessor of ``_LIBCPP_HIDE_FROM_ABI`` -- please use
5176d0caaeSpatrick  ``_LIBCPP_HIDE_FROM_ABI`` instead.
5276d0caaeSpatrick
5346035553Spatrick**_LIBCPP_HIDE_FROM_ABI_AFTER_V1**
5446035553Spatrick  Mark a function as being hidden from the ABI (per `_LIBCPP_HIDE_FROM_ABI`)
5546035553Spatrick  when libc++ is built with an ABI version after ABI v1. This macro is used to
5646035553Spatrick  maintain ABI compatibility for symbols that have been historically exported
5746035553Spatrick  by libc++ in v1 of the ABI, but that we don't want to export in the future.
5846035553Spatrick
5946035553Spatrick  This macro works as follows. When we build libc++, we either hide the symbol
6046035553Spatrick  from the ABI (if the symbol is not part of the ABI in the version we're
6146035553Spatrick  building), or we leave it included. From user code (i.e. when we're not
6246035553Spatrick  building libc++), the macro always marks symbols as internal so that programs
6346035553Spatrick  built using new libc++ headers stop relying on symbols that are removed from
6446035553Spatrick  the ABI in a future version. Each time we release a new stable version of the
6546035553Spatrick  ABI, we should create a new _LIBCPP_HIDE_FROM_ABI_AFTER_XXX macro, and we can
6646035553Spatrick  use it to start removing symbols from the ABI after that stable version.
6746035553Spatrick
6846035553Spatrick**_LIBCPP_TYPE_VIS**
6946035553Spatrick  Mark a type's typeinfo, vtable and members as having default visibility.
7046035553Spatrick  This attribute cannot be used on class templates.
7146035553Spatrick
7246035553Spatrick**_LIBCPP_TEMPLATE_VIS**
7346035553Spatrick  Mark a type's typeinfo and vtable as having default visibility.
7446035553Spatrick  This macro has no effect on the visibility of the type's member functions.
7546035553Spatrick
7646035553Spatrick  **GCC Behavior**: GCC does not support Clang's `type_visibility(...)`
7746035553Spatrick  attribute. With GCC the `visibility(...)` attribute is used and member
7846035553Spatrick  functions are affected.
7946035553Spatrick
8046035553Spatrick  **Windows Behavior**: DLLs do not support dllimport/export on class templates.
8146035553Spatrick  The macro has an empty definition on this platform.
8246035553Spatrick
8346035553Spatrick
8446035553Spatrick**_LIBCPP_ENUM_VIS**
8546035553Spatrick  Mark the typeinfo of an enum as having default visibility. This attribute
8646035553Spatrick  should be applied to all enum declarations.
8746035553Spatrick
8846035553Spatrick  **Windows Behavior**: DLLs do not support importing or exporting enumeration
8946035553Spatrick  typeinfo. The macro has an empty definition on this platform.
9046035553Spatrick
9146035553Spatrick  **GCC Behavior**: GCC un-hides the typeinfo for enumerations by default, even
9246035553Spatrick  if `-fvisibility=hidden` is specified. Additionally applying a visibility
9346035553Spatrick  attribute to an enum class results in a warning. The macro has an empty
9446035553Spatrick  definition with GCC.
9546035553Spatrick
9646035553Spatrick**_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS**
9746035553Spatrick  Mark the member functions, typeinfo, and vtable of the type named in
98*4bdff4beSrobert  an extern template declaration as being exported by the libc++ library.
9946035553Spatrick  This attribute must be specified on all extern class template declarations.
10046035553Spatrick
10146035553Spatrick  This macro is used to override the `_LIBCPP_TEMPLATE_VIS` attribute
10246035553Spatrick  specified on the primary template and to export the member functions produced
10346035553Spatrick  by the explicit instantiation in the dylib.
10446035553Spatrick
10546035553Spatrick  **Windows Behavior**: `extern template` and `dllexport` are fundamentally
10646035553Spatrick  incompatible *on a class template* on Windows; the former suppresses
10746035553Spatrick  instantiation, while the latter forces it. Specifying both on the same
10846035553Spatrick  declaration makes the class template be instantiated, which is not desirable
10946035553Spatrick  inside headers. This macro therefore expands to `dllimport` outside of libc++
11046035553Spatrick  but nothing inside of it (rather than expanding to `dllexport`); instead, the
11146035553Spatrick  explicit instantiations themselves are marked as exported. Note that this
11246035553Spatrick  applies *only* to extern *class* templates. Extern *function* templates obey
11346035553Spatrick  regular import/export semantics, and applying `dllexport` directly to the
11446035553Spatrick  extern template declaration (i.e. using `_LIBCPP_FUNC_VIS`) is the correct
11546035553Spatrick  thing to do for them.
11646035553Spatrick
11746035553Spatrick**_LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS**
11846035553Spatrick  Mark the member functions, typeinfo, and vtable of an explicit instantiation
11946035553Spatrick  of a class template as being exported by the libc++ library. This attribute
12046035553Spatrick  must be specified on all class template explicit instantiations.
12146035553Spatrick
12246035553Spatrick  It is only necessary to mark the explicit instantiation itself (as opposed to
12346035553Spatrick  the extern template declaration) as exported on Windows, as discussed above.
12446035553Spatrick  On all other platforms, this macro has an empty definition.
12546035553Spatrick
12646035553Spatrick**_LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS**
12746035553Spatrick  Mark a symbol as hidden so it will not be exported from shared libraries. This
12846035553Spatrick  is intended specifically for method templates of either classes marked with
12946035553Spatrick  `_LIBCPP_TYPE_VIS` or classes with an extern template instantiation
13046035553Spatrick  declaration marked with `_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS`.
13146035553Spatrick
13246035553Spatrick  When building libc++ with hidden visibility, we want explicit template
13346035553Spatrick  instantiations to export members, which is consistent with existing Windows
13446035553Spatrick  behavior. We also want classes annotated with `_LIBCPP_TYPE_VIS` to export
13546035553Spatrick  their members, which is again consistent with existing Windows behavior.
13646035553Spatrick  Both these changes are necessary for clients to be able to link against a
13746035553Spatrick  libc++ DSO built with hidden visibility without encountering missing symbols.
13846035553Spatrick
13946035553Spatrick  An unfortunate side effect, however, is that method templates of classes
14046035553Spatrick  either marked `_LIBCPP_TYPE_VIS` or with extern template instantiation
14146035553Spatrick  declarations marked with `_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS` also get default
14246035553Spatrick  visibility when instantiated. These methods are often implicitly instantiated
14346035553Spatrick  inside other libraries which use the libc++ headers, and will therefore end up
14446035553Spatrick  being exported from those libraries, since those implicit instantiations will
14546035553Spatrick  receive default visibility. This is not acceptable for libraries that wish to
14646035553Spatrick  control their visibility, and led to PR30642.
14746035553Spatrick
14846035553Spatrick  Consequently, all such problematic method templates are explicitly marked
14946035553Spatrick  either hidden (via this macro) or inline, so that they don't leak into client
15046035553Spatrick  libraries. The problematic methods were found by running
15146035553Spatrick  `bad-visibility-finder <https://github.com/smeenai/bad-visibility-finder>`_
15246035553Spatrick  against the libc++ headers after making `_LIBCPP_TYPE_VIS` and
15346035553Spatrick  `_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS` expand to default visibility.
15446035553Spatrick
15546035553Spatrick**_LIBCPP_EXCEPTION_ABI**
15646035553Spatrick  Mark the member functions, typeinfo, and vtable of the type as being exported
15746035553Spatrick  by the libc++ library. This macro must be applied to all *exception types*.
15846035553Spatrick  Exception types should be defined directly in namespace `std` and not the
15946035553Spatrick  versioning namespace. This allows throwing and catching some exception types
16046035553Spatrick  between libc++ and libstdc++.
16146035553Spatrick
16246035553SpatrickLinks
16346035553Spatrick=====
16446035553Spatrick
16546035553Spatrick* `[cfe-dev] Visibility in libc++ - 1 <http://lists.llvm.org/pipermail/cfe-dev/2013-July/030610.html>`_
16646035553Spatrick* `[cfe-dev] Visibility in libc++ - 2 <http://lists.llvm.org/pipermail/cfe-dev/2013-August/031195.html>`_
16746035553Spatrick* `[libcxx] Visibility fixes for Windows <http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20130805/085461.html>`_
168