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