LLVM 23.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
21Expected<std::shared_ptr<UnwindInfoRegistrationPlugin>>
24
25 ExecutorAddr RegisterSections, DeregisterSections;
26
27 auto &EPC = ES.getExecutorProcessControl();
28 if (auto Err = EPC.getBootstrapSymbols(
29 {{RegisterSections, SNs.RegisterSectionsName},
30 {DeregisterSections, SNs.DeregisterSectionsName}}))
31 return std::move(Err);
32
33 return std::make_shared<UnwindInfoRegistrationPlugin>(ES, RegisterSections,
34 DeregisterSections);
35}
36
39 PassConfiguration &PassConfig) {
40
41 PassConfig.PostFixupPasses.push_back(
42 [this](LinkGraph &G) { return addUnwindInfoRegistrationActions(G); });
43}
44
45Error UnwindInfoRegistrationPlugin::addUnwindInfoRegistrationActions(
46 LinkGraph &G) {
47 ExecutorAddrRange EHFrameRange, UnwindInfoRange;
48
49 std::vector<Block *> CodeBlocks;
50
51 auto ScanUnwindInfoSection = [&](Section &Sec, ExecutorAddrRange &SecRange) {
52 if (Sec.empty())
53 return;
54
55 SecRange.Start = (*Sec.blocks().begin())->getAddress();
56 for (auto *B : Sec.blocks()) {
57 auto R = B->getRange();
58 SecRange.Start = std::min(SecRange.Start, R.Start);
59 SecRange.End = std::max(SecRange.End, R.End);
60 for (auto &E : B->edges()) {
61 if (E.getKind() != Edge::KeepAlive || !E.getTarget().isDefined())
62 continue;
63 auto &TargetBlock = E.getTarget().getBlock();
64 auto &TargetSection = TargetBlock.getSection();
65 if ((TargetSection.getMemProt() & MemProt::Exec) == MemProt::Exec)
66 CodeBlocks.push_back(&TargetBlock);
67 }
68 }
69 };
70
71 if (auto *EHFrame = G.findSectionByName(MachOEHFrameSectionName))
72 ScanUnwindInfoSection(*EHFrame, EHFrameRange);
73
74 if (auto *UnwindInfo = G.findSectionByName(MachOUnwindInfoSectionName))
75 ScanUnwindInfoSection(*UnwindInfo, UnwindInfoRange);
76
77 if (CodeBlocks.empty())
78 return Error::success();
79
80 if ((EHFrameRange == ExecutorAddrRange() &&
81 UnwindInfoRange == ExecutorAddrRange()))
82 return Error::success();
83
84 llvm::sort(CodeBlocks, [](const Block *LHS, const Block *RHS) {
85 return LHS->getAddress() < RHS->getAddress();
86 });
87
89 for (auto *B : CodeBlocks) {
90 if (CodeRanges.empty() || CodeRanges.back().End != B->getAddress())
91 CodeRanges.push_back(B->getRange());
92 else
93 CodeRanges.back().End = B->getRange().End;
94 }
95
96 ExecutorAddr DSOBase;
97 if (auto *DSOBaseSym = G.findAbsoluteSymbolByName(DSOBaseName))
98 DSOBase = DSOBaseSym->getAddress();
99 else if (auto *DSOBaseSym = G.findExternalSymbolByName(DSOBaseName))
100 DSOBase = DSOBaseSym->getAddress();
101 else if (auto *DSOBaseSym = G.findDefinedSymbolByName(DSOBaseName))
102 DSOBase = DSOBaseSym->getAddress();
103 else
104 return make_error<StringError>("In " + G.getName() +
105 " could not find dso base symbol",
107
108 using namespace shared;
109 using SPSRegisterSectionsArgs =
110 SPSArgList<SPSSequence<SPSExecutorAddrRange>, SPSExecutorAddr,
112 using SPSDeregisterSectionsArgs =
113 SPSArgList<SPSSequence<SPSExecutorAddrRange>>;
114
115 G.allocActions().push_back(
116 {cantFail(WrapperFunctionCall::Create<SPSRegisterSectionsArgs>(
117 RegisterSections, CodeRanges, DSOBase, EHFrameRange,
118 UnwindInfoRange)),
119 cantFail(WrapperFunctionCall::Create<SPSDeregisterSectionsArgs>(
120 DeregisterSections, CodeRanges))});
121
122 return Error::success();
123}
124
125} // 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:55
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
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An ExecutionSession represents a running JIT program.
Definition Core.h:1131
ExecutorProcessControl & getExecutorProcessControl()
Get the ExecutorProcessControl object associated with this ExecutionSession.
Definition Core.h:1171
Represents an address in the executor process.
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition Core.h:369
void modifyPassConfig(MaterializationResponsibility &MR, jitlink::LinkGraph &G, jitlink::PassConfiguration &PassConfig) override
static Expected< std::shared_ptr< UnwindInfoRegistrationPlugin > > Create(ExecutionSession &ES, rt::MachOUnwindInfoRegistrarSymbolNames SNs=rt::orc_rt_MachOUnwindInfoRegistrarSPSSymbols)
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:94
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1636
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
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.
Symbol names for the ORC runtime's StandaloneMachOUnwindInfoRegistrar SPS interface.
Definition OrcRTBridge.h:98