xref: /freebsd/contrib/llvm-project/libcxx/src/include/overridable_function.h (revision 7a6dacaca14b62ca4b74406814becb87a3fefac0)
1*7a6dacacSDimitry Andric // -*- C++ -*-
2*7a6dacacSDimitry Andric //===----------------------------------------------------------------------===//
3*7a6dacacSDimitry Andric //
4*7a6dacacSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5*7a6dacacSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
6*7a6dacacSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7*7a6dacacSDimitry Andric //
8*7a6dacacSDimitry Andric //===----------------------------------------------------------------------===//
9*7a6dacacSDimitry Andric 
10*7a6dacacSDimitry Andric #ifndef _LIBCPP_SRC_INCLUDE_OVERRIDABLE_FUNCTION_H
11*7a6dacacSDimitry Andric #define _LIBCPP_SRC_INCLUDE_OVERRIDABLE_FUNCTION_H
12*7a6dacacSDimitry Andric 
13*7a6dacacSDimitry Andric #include <__config>
14*7a6dacacSDimitry Andric #include <cstdint>
15*7a6dacacSDimitry Andric 
16*7a6dacacSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17*7a6dacacSDimitry Andric #  pragma GCC system_header
18*7a6dacacSDimitry Andric #endif
19*7a6dacacSDimitry Andric 
20*7a6dacacSDimitry Andric //
21*7a6dacacSDimitry Andric // This file provides the std::__is_function_overridden utility, which allows checking
22*7a6dacacSDimitry Andric // whether an overridable function (typically a weak symbol) like `operator new`
23*7a6dacacSDimitry Andric // has been overridden by a user or not.
24*7a6dacacSDimitry Andric //
25*7a6dacacSDimitry Andric // This is a low-level utility which does not work on all platforms, since it needs
26*7a6dacacSDimitry Andric // to make assumptions about the object file format in use. Furthermore, it requires
27*7a6dacacSDimitry Andric // the "base definition" of the function (the one we want to check whether it has been
28*7a6dacacSDimitry Andric // overridden) to be annotated with the _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE macro.
29*7a6dacacSDimitry Andric //
30*7a6dacacSDimitry Andric // This currently works with Mach-O files (used on Darwin) and with ELF files (used on Linux
31*7a6dacacSDimitry Andric // and others). On platforms where we know how to implement this detection, the macro
32*7a6dacacSDimitry Andric // _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION is defined to 1, and it is defined to 0 on
33*7a6dacacSDimitry Andric // other platforms. The _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE macro is defined to
34*7a6dacacSDimitry Andric // nothing on unsupported platforms so that it can be used to decorate functions regardless
35*7a6dacacSDimitry Andric // of whether detection is actually supported.
36*7a6dacacSDimitry Andric //
37*7a6dacacSDimitry Andric // How does this work?
38*7a6dacacSDimitry Andric // -------------------
39*7a6dacacSDimitry Andric //
40*7a6dacacSDimitry Andric // Let's say we want to check whether a weak function `f` has been overridden by the user.
41*7a6dacacSDimitry Andric // The general mechanism works by placing `f`'s definition (in the libc++ built library)
42*7a6dacacSDimitry Andric // inside a special section, which we do using the `__section__` attribute via the
43*7a6dacacSDimitry Andric // _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE macro.
44*7a6dacacSDimitry Andric //
45*7a6dacacSDimitry Andric // Then, when comes the time to check whether the function has been overridden, we take
46*7a6dacacSDimitry Andric // the address of the function and we check whether it falls inside the special function
47*7a6dacacSDimitry Andric // we created. This can be done by finding pointers to the start and the end of the section
48*7a6dacacSDimitry Andric // (which is done differently for ELF and Mach-O), and then checking whether `f` falls
49*7a6dacacSDimitry Andric // within those bounds. If it falls within those bounds, then `f` is still inside the
50*7a6dacacSDimitry Andric // special section and so it is the version we defined in the libc++ built library, i.e.
51*7a6dacacSDimitry Andric // it was not overridden. Otherwise, it was overridden by the user because it falls
52*7a6dacacSDimitry Andric // outside of the section.
53*7a6dacacSDimitry Andric //
54*7a6dacacSDimitry Andric // Important note
55*7a6dacacSDimitry Andric // --------------
56*7a6dacacSDimitry Andric //
57*7a6dacacSDimitry Andric // This mechanism should never be used outside of the libc++ built library. In particular,
58*7a6dacacSDimitry Andric // attempting to use this within the libc++ headers will not work at all because we don't
59*7a6dacacSDimitry Andric // want to be defining special sections inside user's executables which use our headers.
60*7a6dacacSDimitry Andric // This is provided inside libc++'s include tree solely to make it easier to share with
61*7a6dacacSDimitry Andric // libc++abi, which needs the same mechanism.
62*7a6dacacSDimitry Andric //
63*7a6dacacSDimitry Andric 
64*7a6dacacSDimitry Andric #if defined(_LIBCPP_OBJECT_FORMAT_MACHO)
65*7a6dacacSDimitry Andric 
66*7a6dacacSDimitry Andric #  define _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION 1
67*7a6dacacSDimitry Andric #  define _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE                                                                 \
68*7a6dacacSDimitry Andric     __attribute__((__section__("__TEXT,__lcxx_override,regular,pure_instructions")))
69*7a6dacacSDimitry Andric 
70*7a6dacacSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
71*7a6dacacSDimitry Andric template <class _Ret, class... _Args>
72*7a6dacacSDimitry Andric _LIBCPP_HIDE_FROM_ABI bool __is_function_overridden(_Ret (*__fptr)(_Args...)) noexcept {
73*7a6dacacSDimitry Andric   // Declare two dummy bytes and give them these special `__asm` values. These values are
74*7a6dacacSDimitry Andric   // defined by the linker, which means that referring to `&__lcxx_override_start` will
75*7a6dacacSDimitry Andric   // effectively refer to the address where the section starts (and same for the end).
76*7a6dacacSDimitry Andric   extern char __lcxx_override_start __asm("section$start$__TEXT$__lcxx_override");
77*7a6dacacSDimitry Andric   extern char __lcxx_override_end __asm("section$end$__TEXT$__lcxx_override");
78*7a6dacacSDimitry Andric 
79*7a6dacacSDimitry Andric   // Now get a uintptr_t out of these locations, and out of the function pointer.
80*7a6dacacSDimitry Andric   uintptr_t __start = reinterpret_cast<uintptr_t>(&__lcxx_override_start);
81*7a6dacacSDimitry Andric   uintptr_t __end   = reinterpret_cast<uintptr_t>(&__lcxx_override_end);
82*7a6dacacSDimitry Andric   uintptr_t __ptr   = reinterpret_cast<uintptr_t>(__fptr);
83*7a6dacacSDimitry Andric 
84*7a6dacacSDimitry Andric   // Finally, the function was overridden if it falls outside of the section's bounds.
85*7a6dacacSDimitry Andric   return __ptr < __start || __ptr > __end;
86*7a6dacacSDimitry Andric }
87*7a6dacacSDimitry Andric _LIBCPP_END_NAMESPACE_STD
88*7a6dacacSDimitry Andric 
89*7a6dacacSDimitry Andric #elif defined(_LIBCPP_OBJECT_FORMAT_ELF)
90*7a6dacacSDimitry Andric 
91*7a6dacacSDimitry Andric #  define _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION 1
92*7a6dacacSDimitry Andric #  define _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE __attribute__((__section__("__lcxx_override")))
93*7a6dacacSDimitry Andric 
94*7a6dacacSDimitry Andric // This is very similar to what we do for Mach-O above. The ELF linker will implicitly define
95*7a6dacacSDimitry Andric // variables with those names corresponding to the start and the end of the section.
96*7a6dacacSDimitry Andric //
97*7a6dacacSDimitry Andric // See https://stackoverflow.com/questions/16552710/how-do-you-get-the-start-and-end-addresses-of-a-custom-elf-section
98*7a6dacacSDimitry Andric extern char __start___lcxx_override;
99*7a6dacacSDimitry Andric extern char __stop___lcxx_override;
100*7a6dacacSDimitry Andric 
101*7a6dacacSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
102*7a6dacacSDimitry Andric template <class _Ret, class... _Args>
103*7a6dacacSDimitry Andric _LIBCPP_HIDE_FROM_ABI bool __is_function_overridden(_Ret (*__fptr)(_Args...)) noexcept {
104*7a6dacacSDimitry Andric   uintptr_t __start = reinterpret_cast<uintptr_t>(&__start___lcxx_override);
105*7a6dacacSDimitry Andric   uintptr_t __end   = reinterpret_cast<uintptr_t>(&__stop___lcxx_override);
106*7a6dacacSDimitry Andric   uintptr_t __ptr   = reinterpret_cast<uintptr_t>(__fptr);
107*7a6dacacSDimitry Andric 
108*7a6dacacSDimitry Andric   return __ptr < __start || __ptr > __end;
109*7a6dacacSDimitry Andric }
110*7a6dacacSDimitry Andric _LIBCPP_END_NAMESPACE_STD
111*7a6dacacSDimitry Andric 
112*7a6dacacSDimitry Andric #else
113*7a6dacacSDimitry Andric 
114*7a6dacacSDimitry Andric #  define _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION 0
115*7a6dacacSDimitry Andric #  define _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE /* nothing */
116*7a6dacacSDimitry Andric 
117*7a6dacacSDimitry Andric #endif
118*7a6dacacSDimitry Andric 
119*7a6dacacSDimitry Andric #endif // _LIBCPP_SRC_INCLUDE_OVERRIDABLE_FUNCTION_H
120