LLVM 24.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
14#include "llvm/IR/Module.h"
15
16#define DEBUG_TYPE "orc"
17
18using namespace llvm::jitlink;
19
20namespace llvm::orc {
21
22Expected<std::shared_ptr<UnwindInfoRegistrationPlugin>>
25
26 ExecutorAddr RegisterSections, DeregisterSections;
27
28 if (auto Err = lookupAndApply(
30 {recordAddr(SNs.RegisterSectionsName, &RegisterSections),
31 recordAddr(SNs.DeregisterSectionsName, &DeregisterSections)}))
32 return std::move(Err);
33
34 return std::make_shared<UnwindInfoRegistrationPlugin>(ES, RegisterSections,
35 DeregisterSections);
36}
37
40 PassConfiguration &PassConfig) {
41
42 PassConfig.PostFixupPasses.push_back(
43 [this](LinkGraph &G) { return addUnwindInfoRegistrationActions(G); });
44}
45
46Error UnwindInfoRegistrationPlugin::addUnwindInfoRegistrationActions(
47 LinkGraph &G) {
48 ExecutorAddrRange EHFrameRange, UnwindInfoRange;
49
50 std::vector<Block *> CodeBlocks;
51
52 auto ScanUnwindInfoSection = [&](Section &Sec, ExecutorAddrRange &SecRange) {
53 if (Sec.empty())
54 return;
55
56 SecRange.Start = (*Sec.blocks().begin())->getAddress();
57 for (auto *B : Sec.blocks()) {
58 auto R = B->getRange();
59 SecRange.Start = std::min(SecRange.Start, R.Start);
60 SecRange.End = std::max(SecRange.End, R.End);
61 for (auto &E : B->edges()) {
62 if (E.getKind() != Edge::KeepAlive || !E.getTarget().isDefined())
63 continue;
64 auto &TargetBlock = E.getTarget().getBlock();
65 auto &TargetSection = TargetBlock.getSection();
66 if ((TargetSection.getMemProt() & MemProt::Exec) == MemProt::Exec)
67 CodeBlocks.push_back(&TargetBlock);
68 }
69 }
70 };
71
72 if (auto *EHFrame = G.findSectionByName(MachOEHFrameSectionName))
73 ScanUnwindInfoSection(*EHFrame, EHFrameRange);
74
75 if (auto *UnwindInfo = G.findSectionByName(MachOUnwindInfoSectionName))
76 ScanUnwindInfoSection(*UnwindInfo, UnwindInfoRange);
77
78 if (CodeBlocks.empty())
79 return Error::success();
80
81 if ((EHFrameRange == ExecutorAddrRange() &&
82 UnwindInfoRange == ExecutorAddrRange()))
83 return Error::success();
84
85 llvm::sort(CodeBlocks, [](const Block *LHS, const Block *RHS) {
86 return LHS->getAddress() < RHS->getAddress();
87 });
88
90 for (auto *B : CodeBlocks) {
91 if (CodeRanges.empty() || CodeRanges.back().End != B->getAddress())
92 CodeRanges.push_back(B->getRange());
93 else
94 CodeRanges.back().End = B->getRange().End;
95 }
96
97 ExecutorAddr DSOBase;
98 if (auto *DSOBaseSym = G.findAbsoluteSymbolByName(DSOBaseName))
99 DSOBase = DSOBaseSym->getAddress();
100 else if (auto *DSOBaseSym = G.findExternalSymbolByName(DSOBaseName))
101 DSOBase = DSOBaseSym->getAddress();
102 else if (auto *DSOBaseSym = G.findDefinedSymbolByName(DSOBaseName))
103 DSOBase = DSOBaseSym->getAddress();
104 else
105 return make_error<StringError>("In " + G.getName() +
106 " could not find dso base symbol",
108
109 using namespace shared;
110 using SPSRegisterSectionsArgs =
111 SPSArgList<SPSSequence<SPSExecutorAddrRange>, SPSExecutorAddr,
113 using SPSDeregisterSectionsArgs =
114 SPSArgList<SPSSequence<SPSExecutorAddrRange>>;
115
116 G.allocActions().push_back(
117 {cantFail(WrapperFunctionCall::Create<SPSRegisterSectionsArgs>(
118 RegisterSections, CodeRanges, DSOBase, EHFrameRange,
119 UnwindInfoRange)),
120 cantFail(WrapperFunctionCall::Create<SPSDeregisterSectionsArgs>(
121 DeregisterSections, CodeRanges))});
122
123 return Error::success();
124}
125
126} // 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
An ExecutionSession represents a running JIT program.
Definition Core.h:1111
JITDylib & getBootstrapJITDylib()
Returns a reference to the bootstrap JITDylib.
Definition Core.h:1177
Represents an address in the executor process.
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition Core.h:349
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 void lookupAndApply(unique_function< void(Error)> OnApplied, LookupKind K, const JITDylibSearchOrder &SearchOrder, ArrayRef< LookupPrepareFn > PrepareFns)
Resolve the symbols contributed by every prepare function with a single lookup, then let each of thei...
LLVM_ABI StringRef MachOEHFrameSectionName
LLVM_ABI StringRef MachOUnwindInfoSectionName
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:1652
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
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:37