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