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