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
9#include "llvm/ExecutionEngine/Orc/UnwindInfoRegistrationPlugin.h"
10
11#include "llvm/ExecutionEngine/Orc/LookupAndApply.h"
12#include "llvm/ExecutionEngine/Orc/Shared/MachOObjectFormat.h"
13#include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
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>>
23UnwindInfoRegistrationPlugin::Create(
24 ExecutionSession &ES, rt::MachOUnwindInfoRegistrarSymbolNames SNs) {
25
26 ExecutorAddr RegisterSections, DeregisterSections;
27
28 if (auto Err = lookupAndApply(
29 JD&: ES.getBootstrapJITDylib(),
30 PrepareFns: {recordAddr(Name: SNs.RegisterSectionsName, A: &RegisterSections),
31 recordAddr(Name: SNs.DeregisterSectionsName, A: &DeregisterSections)}))
32 return std::move(Err);
33
34 return std::make_shared<UnwindInfoRegistrationPlugin>(args&: ES, args&: RegisterSections,
35 args&: DeregisterSections);
36}
37
38void UnwindInfoRegistrationPlugin::modifyPassConfig(
39 MaterializationResponsibility &MR, LinkGraph &G,
40 PassConfiguration &PassConfig) {
41
42 PassConfig.PostFixupPasses.push_back(
43 x: [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(a: SecRange.Start, b: R.Start);
60 SecRange.End = std::max(a: SecRange.End, b: 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(x: &TargetBlock);
68 }
69 }
70 };
71
72 if (auto *EHFrame = G.findSectionByName(Name: MachOEHFrameSectionName))
73 ScanUnwindInfoSection(*EHFrame, EHFrameRange);
74
75 if (auto *UnwindInfo = G.findSectionByName(Name: 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(C&: CodeBlocks, Comp: [](const Block *LHS, const Block *RHS) {
86 return LHS->getAddress() < RHS->getAddress();
87 });
88
89 SmallVector<ExecutorAddrRange> CodeRanges;
90 for (auto *B : CodeBlocks) {
91 if (CodeRanges.empty() || CodeRanges.back().End != B->getAddress())
92 CodeRanges.push_back(Elt: B->getRange());
93 else
94 CodeRanges.back().End = B->getRange().End;
95 }
96
97 ExecutorAddr DSOBase;
98 if (auto *DSOBaseSym = G.findAbsoluteSymbolByName(Name: DSOBaseName))
99 DSOBase = DSOBaseSym->getAddress();
100 else if (auto *DSOBaseSym = G.findExternalSymbolByName(Name: DSOBaseName))
101 DSOBase = DSOBaseSym->getAddress();
102 else if (auto *DSOBaseSym = G.findDefinedSymbolByName(Name: DSOBaseName))
103 DSOBase = DSOBaseSym->getAddress();
104 else
105 return make_error<StringError>(Args: "In " + G.getName() +
106 " could not find dso base symbol",
107 Args: inconvertibleErrorCode());
108
109 using namespace shared;
110 using SPSRegisterSectionsArgs =
111 SPSArgList<SPSSequence<SPSExecutorAddrRange>, SPSExecutorAddr,
112 SPSExecutorAddrRange, SPSExecutorAddrRange>;
113 using SPSDeregisterSectionsArgs =
114 SPSArgList<SPSSequence<SPSExecutorAddrRange>>;
115
116 G.allocActions().push_back(
117 x: {.Finalize: cantFail(ValOrErr: WrapperFunctionCall::Create<SPSRegisterSectionsArgs>(
118 FnAddr: RegisterSections, Args: CodeRanges, Args: DSOBase, Args: EHFrameRange,
119 Args: UnwindInfoRange)),
120 .Dealloc: cantFail(ValOrErr: WrapperFunctionCall::Create<SPSDeregisterSectionsArgs>(
121 FnAddr: DeregisterSections, Args: CodeRanges))});
122
123 return Error::success();
124}
125
126} // namespace llvm::orc
127