]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/xray/xray_init.cc
Merge from vendor branch importing dtc 1.4.3
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / xray / xray_init.cc
1 //===-- xray_init.cc --------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of XRay, a dynamic runtime instrumentation system.
11 //
12 // XRay initialisation logic.
13 //===----------------------------------------------------------------------===//
14
15 #include <atomic>
16 #include <fcntl.h>
17 #include <strings.h>
18 #include <unistd.h>
19
20 #include "sanitizer_common/sanitizer_common.h"
21 #include "xray_defs.h"
22 #include "xray_flags.h"
23 #include "xray_interface_internal.h"
24
25 extern "C" {
26 void __xray_init();
27 extern const XRaySledEntry __start_xray_instr_map[] __attribute__((weak));
28 extern const XRaySledEntry __stop_xray_instr_map[] __attribute__((weak));
29 }
30
31 using namespace __sanitizer;
32 using namespace __xray;
33
34 // When set to 'true' this means the XRay runtime has been initialised. We use
35 // the weak symbols defined above (__start_xray_inst_map and
36 // __stop_xray_instr_map) to initialise the instrumentation map that XRay uses
37 // for runtime patching/unpatching of instrumentation points.
38 //
39 // FIXME: Support DSO instrumentation maps too. The current solution only works
40 // for statically linked executables.
41 std::atomic<bool> XRayInitialized{false};
42
43 // This should always be updated before XRayInitialized is updated.
44 std::atomic<__xray::XRaySledMap> XRayInstrMap{};
45
46 // __xray_init() will do the actual loading of the current process' memory map
47 // and then proceed to look for the .xray_instr_map section/segment.
48 void __xray_init() XRAY_NEVER_INSTRUMENT {
49   InitializeFlags();
50   if (__start_xray_instr_map == nullptr) {
51     Report("XRay instrumentation map missing. Not initializing XRay.\n");
52     return;
53   }
54
55   // Now initialize the XRayInstrMap global struct with the address of the
56   // entries, reinterpreted as an array of XRaySledEntry objects. We use the
57   // virtual pointer we have from the section to provide us the correct
58   // information.
59   __xray::XRaySledMap SledMap{};
60   SledMap.Sleds = __start_xray_instr_map;
61   SledMap.Entries = __stop_xray_instr_map - __start_xray_instr_map;
62   XRayInstrMap.store(SledMap, std::memory_order_release);
63   XRayInitialized.store(true, std::memory_order_release);
64
65   if (flags()->patch_premain)
66     __xray_patch();
67 }
68
69 __attribute__((section(".preinit_array"),
70                used)) void (*__local_xray_preinit)(void) = __xray_init;