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
19using namespace __sanitizer;
20
21extern "C" {
22extern const XRaySledEntry __start_xray_instr_map[] __attribute__((weak))
23__attribute__((visibility("hidden")));
24extern const XRaySledEntry __stop_xray_instr_map[] __attribute__((weak))
25__attribute__((visibility("hidden")));
26extern const XRayFunctionSledIndex __start_xray_fn_idx[] __attribute__((weak))
27__attribute__((visibility("hidden")));
28extern 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.
34extern const XRaySledEntry __start_xray_instr_map[] = {};
35extern const XRaySledEntry __stop_xray_instr_map[] = {};
36extern const XRayFunctionSledIndex __start_xray_fn_idx[] = {};
37extern const XRayFunctionSledIndex __stop_xray_fn_idx[] = {};
38#endif
39}
40
41// Handler functions to call in the patched entry/exit sled.
42extern atomic_uintptr_t XRayPatchedFunction;
43extern atomic_uintptr_t XRayArgLogger;
44extern atomic_uintptr_t XRayPatchedCustomEvent;
45extern atomic_uintptr_t XRayPatchedTypedEvent;
46
47static 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