xref: /freebsd/contrib/llvm-project/compiler-rt/lib/xray/xray_dso_init.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
1*700637cbSDimitry Andric //===-- xray_init.cpp -------------------------------------------*- C++ -*-===//
2*700637cbSDimitry Andric //
3*700637cbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*700637cbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*700637cbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*700637cbSDimitry Andric //
7*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
8*700637cbSDimitry Andric //
9*700637cbSDimitry Andric // This file is a part of XRay, a dynamic runtime instrumentation system.
10*700637cbSDimitry Andric //
11*700637cbSDimitry Andric // XRay initialisation logic for DSOs.
12*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
13*700637cbSDimitry Andric 
14*700637cbSDimitry Andric #include "sanitizer_common/sanitizer_atomic.h"
15*700637cbSDimitry Andric #include "xray_defs.h"
16*700637cbSDimitry Andric #include "xray_flags.h"
17*700637cbSDimitry Andric #include "xray_interface_internal.h"
18*700637cbSDimitry Andric 
19*700637cbSDimitry Andric using namespace __sanitizer;
20*700637cbSDimitry Andric 
21*700637cbSDimitry Andric extern "C" {
22*700637cbSDimitry Andric extern const XRaySledEntry __start_xray_instr_map[] __attribute__((weak))
23*700637cbSDimitry Andric __attribute__((visibility("hidden")));
24*700637cbSDimitry Andric extern const XRaySledEntry __stop_xray_instr_map[] __attribute__((weak))
25*700637cbSDimitry Andric __attribute__((visibility("hidden")));
26*700637cbSDimitry Andric extern const XRayFunctionSledIndex __start_xray_fn_idx[] __attribute__((weak))
27*700637cbSDimitry Andric __attribute__((visibility("hidden")));
28*700637cbSDimitry Andric extern const XRayFunctionSledIndex __stop_xray_fn_idx[] __attribute__((weak))
29*700637cbSDimitry Andric __attribute__((visibility("hidden")));
30*700637cbSDimitry Andric 
31*700637cbSDimitry Andric #if SANITIZER_APPLE
32*700637cbSDimitry Andric // HACK: This is a temporary workaround to make XRay build on
33*700637cbSDimitry Andric // Darwin, but it will probably not work at runtime.
34*700637cbSDimitry Andric extern const XRaySledEntry __start_xray_instr_map[] = {};
35*700637cbSDimitry Andric extern const XRaySledEntry __stop_xray_instr_map[] = {};
36*700637cbSDimitry Andric extern const XRayFunctionSledIndex __start_xray_fn_idx[] = {};
37*700637cbSDimitry Andric extern const XRayFunctionSledIndex __stop_xray_fn_idx[] = {};
38*700637cbSDimitry Andric #endif
39*700637cbSDimitry Andric }
40*700637cbSDimitry Andric 
41*700637cbSDimitry Andric // Handler functions to call in the patched entry/exit sled.
42*700637cbSDimitry Andric extern atomic_uintptr_t XRayPatchedFunction;
43*700637cbSDimitry Andric extern atomic_uintptr_t XRayArgLogger;
44*700637cbSDimitry Andric extern atomic_uintptr_t XRayPatchedCustomEvent;
45*700637cbSDimitry Andric extern atomic_uintptr_t XRayPatchedTypedEvent;
46*700637cbSDimitry Andric 
47*700637cbSDimitry Andric static int __xray_object_id{-1};
48*700637cbSDimitry Andric 
49*700637cbSDimitry Andric // Note: .preinit_array initialization does not work for DSOs
50*700637cbSDimitry Andric __attribute__((constructor(0))) static void
__xray_init_dso()51*700637cbSDimitry Andric __xray_init_dso() XRAY_NEVER_INSTRUMENT {
52*700637cbSDimitry Andric   // Register sleds in main XRay runtime.
53*700637cbSDimitry Andric   __xray_object_id =
54*700637cbSDimitry Andric       __xray_register_dso(__start_xray_instr_map, __stop_xray_instr_map,
55*700637cbSDimitry Andric                           __start_xray_fn_idx, __stop_xray_fn_idx, {});
56*700637cbSDimitry Andric }
57*700637cbSDimitry Andric 
58*700637cbSDimitry Andric __attribute__((destructor(0))) static void
__xray_finalize_dso()59*700637cbSDimitry Andric __xray_finalize_dso() XRAY_NEVER_INSTRUMENT {
60*700637cbSDimitry Andric   // Inform the main runtime that this DSO is no longer used.
61*700637cbSDimitry Andric   __xray_deregister_dso(__xray_object_id);
62*700637cbSDimitry Andric }
63