LLVM 22.0.0git
UnwindInfoRegistrationPlugin.cpp
Go to the documentation of this file.
1//===----- UnwindInfoRegistrationPlugin.cpp - libunwind registration ------===//
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
10
13#include "llvm/IR/Module.h"
14
15#define DEBUG_TYPE "orc"
16
17using namespace llvm::jitlink;
18
19namespace llvm::orc {
20
23
24 ExecutorAddr Register, Deregister;
25
26 auto &EPC = ES.getExecutorProcessControl();
27 if (auto Err = EPC.getBootstrapSymbols(
28 {{Register, rt_alt::UnwindInfoManagerRegisterActionName},
29 {Deregister, rt_alt::UnwindInfoManagerDeregisterActionName}}))
30 return std::move(Err);
31
32 return std::make_shared<UnwindInfoRegistrationPlugin>(ES, Register,
33 Deregister);
34}
35
36void UnwindInfoRegistrationPlugin::modifyPassConfig(
38 PassConfiguration &PassConfig) {
39
40 PassConfig.PostFixupPasses.push_back(
41 [this](LinkGraph &G) { return addUnwindInfoRegistrationActions(G); });
42}
43
44Error UnwindInfoRegistrationPlugin::addUnwindInfoRegistrationActions(
45 LinkGraph &G) {
46 ExecutorAddrRange EHFrameRange, UnwindInfoRange;
47
48 std::vector<Block *> CodeBlocks;
49
50 auto ScanUnwindInfoSection = [&](Section &Sec, ExecutorAddrRange &SecRange) {
51 if (Sec.empty())
52 return;
53
54 SecRange.Start = (*Sec.blocks().begin())->getAddress();
55 for (auto *B : Sec.blocks()) {
56 auto R = B->getRange();
57 SecRange.Start = std::min(SecRange.Start, R.Start);
58 SecRange.End = std::max(SecRange.End, R.End);
59 for (auto &E : B->edges()) {
60 if (E.getKind() != Edge::KeepAlive || !E.getTarget().isDefined())
61 continue;
62 auto &TargetBlock = E.getTarget().getBlock();
63 auto &TargetSection = TargetBlock.getSection();
64 if ((TargetSection.getMemProt() & MemProt::Exec) == MemProt::Exec)
65 CodeBlocks.push_back(&TargetBlock);
66 }
67 }
68 };
69
70 if (auto *EHFrame = G.findSectionByName(MachOEHFrameSectionName))
71 ScanUnwindInfoSection(*EHFrame, EHFrameRange);
72
73 if (auto *UnwindInfo = G.findSectionByName(MachOUnwindInfoSectionName))
74 ScanUnwindInfoSection(*UnwindInfo, UnwindInfoRange);
75
76 if (CodeBlocks.empty())
77 return Error::success();
78
79 if ((EHFrameRange == ExecutorAddrRange() &&
80 UnwindInfoRange == ExecutorAddrRange()))
81 return Error::success();
82
83 llvm::sort(CodeBlocks, [](const Block *LHS, const Block *RHS) {
84 return LHS->getAddress() < RHS->getAddress();
85 });
86
88 for (auto *B : CodeBlocks) {
89 if (CodeRanges.empty() || CodeRanges.back().End != B->getAddress())
90 CodeRanges.push_back(B->getRange());
91 else
92 CodeRanges.back().End = B->getRange().End;
93 }
94
95 ExecutorAddr DSOBase;
96 if (auto *DSOBaseSym = G.findAbsoluteSymbolByName(DSOBaseName))
97 DSOBase = DSOBaseSym->getAddress();
98 else if (auto *DSOBaseSym = G.findExternalSymbolByName(DSOBaseName))
99 DSOBase = DSOBaseSym->getAddress();
100 else if (auto *DSOBaseSym = G.findDefinedSymbolByName(DSOBaseName))
101 DSOBase = DSOBaseSym->getAddress();
102 else
103 return make_error<StringError>("In " + G.getName() +
104 " could not find dso base symbol",
106
107 using namespace shared;
108 using SPSRegisterArgs =
109 SPSArgList<SPSSequence<SPSExecutorAddrRange>, SPSExecutorAddr,
111 using SPSDeregisterArgs = SPSArgList<SPSSequence<SPSExecutorAddrRange>>;
112
113 G.allocActions().push_back(
114 {cantFail(WrapperFunctionCall::Create<SPSRegisterArgs>(
115 Register, CodeRanges, DSOBase, EHFrameRange, UnwindInfoRange)),
116 cantFail(WrapperFunctionCall::Create<SPSDeregisterArgs>(Deregister,
117 CodeRanges))});
118
119 return Error::success();
120}
121
122} // namespace llvm::orc
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Module.h This file contains the declarations for the Module class.
#define G(x, y, z)
Definition: MD5.cpp:56
Value * RHS
Value * LHS
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
static ErrorSuccess success()
Create a success value.
Definition: Error.h:336
Tagged union holding either a T or a Error.
Definition: Error.h:485
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
An ExecutionSession represents a running JIT program.
Definition: Core.h:1355
ExecutorProcessControl & getExecutorProcessControl()
Get the ExecutorProcessControl object associated with this ExecutionSession.
Definition: Core.h:1395
Represents an address in the executor process.
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:576
static Expected< std::shared_ptr< UnwindInfoRegistrationPlugin > > Create(ExecutionSession &ES, ExecutorAddr Register, ExecutorAddr Deregister)
SPSTuple< SPSExecutorAddr, SPSExecutorAddr > SPSExecutorAddrRange
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:98
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1669
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:769
Represents an address range in the exceutor process.