1 //===--------- EHFrameSupport.h - JITLink eh-frame utils --------*- 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 // EHFrame registration support for JITLink.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_EXECUTIONENGINE_JITLINK_EHFRAMESUPPORT_H
14 #define LLVM_EXECUTIONENGINE_JITLINK_EHFRAMESUPPORT_H
15 
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/ExecutionEngine/JITLink/JITLink.h"
18 #include "llvm/ExecutionEngine/JITSymbol.h"
19 #include "llvm/Support/Error.h"
20 
21 namespace llvm {
22 namespace jitlink {
23 
24 /// Registers all FDEs in the given eh-frame section with the current process.
25 Error registerEHFrameSection(const void *EHFrameSectionAddr,
26                              size_t EHFrameSectionSize);
27 
28 /// Deregisters all FDEs in the given eh-frame section with the current process.
29 Error deregisterEHFrameSection(const void *EHFrameSectionAddr,
30                                size_t EHFrameSectionSize);
31 
32 /// Supports registration/deregistration of EH-frames in a target process.
33 class EHFrameRegistrar {
34 public:
35   virtual ~EHFrameRegistrar();
36   virtual Error registerEHFrames(JITTargetAddress EHFrameSectionAddr,
37                                  size_t EHFrameSectionSize) = 0;
38   virtual Error deregisterEHFrames(JITTargetAddress EHFrameSectionAddr,
39                                    size_t EHFrameSectionSize) = 0;
40 };
41 
42 /// Registers / Deregisters EH-frames in the current process.
43 class InProcessEHFrameRegistrar final : public EHFrameRegistrar {
44 public:
45   /// Get a reference to the InProcessEHFrameRegistrar singleton.
46   static InProcessEHFrameRegistrar &getInstance();
47 
48   InProcessEHFrameRegistrar(const InProcessEHFrameRegistrar &) = delete;
49   InProcessEHFrameRegistrar &
50   operator=(const InProcessEHFrameRegistrar &) = delete;
51 
52   InProcessEHFrameRegistrar(InProcessEHFrameRegistrar &&) = delete;
53   InProcessEHFrameRegistrar &operator=(InProcessEHFrameRegistrar &&) = delete;
54 
55   Error registerEHFrames(JITTargetAddress EHFrameSectionAddr,
56                          size_t EHFrameSectionSize) override {
57     return registerEHFrameSection(
58         jitTargetAddressToPointer<void *>(EHFrameSectionAddr),
59         EHFrameSectionSize);
60   }
61 
62   Error deregisterEHFrames(JITTargetAddress EHFrameSectionAddr,
63                            size_t EHFrameSectionSize) override {
64     return deregisterEHFrameSection(
65         jitTargetAddressToPointer<void *>(EHFrameSectionAddr),
66         EHFrameSectionSize);
67   }
68 
69 private:
70   InProcessEHFrameRegistrar();
71 };
72 
73 using StoreFrameRangeFunction =
74   std::function<void(JITTargetAddress EHFrameSectionAddr,
75                      size_t EHFrameSectionSize)>;
76 
77 /// Creates a pass that records the address and size of the EH frame section.
78 /// If no eh-frame section is found then the address and size will both be given
79 /// as zero.
80 ///
81 /// Authors of JITLinkContexts can use this function to register a post-fixup
82 /// pass that records the range of the eh-frame section. This range can
83 /// be used after finalization to register and deregister the frame.
84 LinkGraphPassFunction
85 createEHFrameRecorderPass(const Triple &TT,
86                           StoreFrameRangeFunction StoreFrameRange);
87 
88 } // end namespace jitlink
89 } // end namespace llvm
90 
91 #endif // LLVM_EXECUTIONENGINE_JITLINK_EHFRAMESUPPORT_H
92