xref: /freebsd/contrib/llvm-project/llvm/lib/ExecutionEngine/Orc/EHFrameRegistrationPlugin.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
1*700637cbSDimitry Andric //===--------- EHFrameRegistrationPlugin.cpp - Register eh-frames ---------===//
2*700637cbSDimitry Andric //
3*700637cbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*700637cbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*700637cbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*700637cbSDimitry Andric //
7*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
8*700637cbSDimitry Andric 
9*700637cbSDimitry Andric #include "llvm/ExecutionEngine/Orc/EHFrameRegistrationPlugin.h"
10*700637cbSDimitry Andric 
11*700637cbSDimitry Andric #include "llvm/ExecutionEngine/JITLink/EHFrameSupport.h"
12*700637cbSDimitry Andric #include "llvm/ExecutionEngine/Orc/Shared/MachOObjectFormat.h"
13*700637cbSDimitry Andric #include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
14*700637cbSDimitry Andric 
15*700637cbSDimitry Andric #define DEBUG_TYPE "orc"
16*700637cbSDimitry Andric 
17*700637cbSDimitry Andric using namespace llvm::jitlink;
18*700637cbSDimitry Andric 
19*700637cbSDimitry Andric namespace llvm::orc {
20*700637cbSDimitry Andric 
21*700637cbSDimitry Andric Expected<std::unique_ptr<EHFrameRegistrationPlugin>>
Create(ExecutionSession & ES)22*700637cbSDimitry Andric EHFrameRegistrationPlugin::Create(ExecutionSession &ES) {
23*700637cbSDimitry Andric   // Lookup addresseses of the registration/deregistration functions in the
24*700637cbSDimitry Andric   // bootstrap map.
25*700637cbSDimitry Andric   ExecutorAddr RegisterEHFrameSectionAllocAction;
26*700637cbSDimitry Andric   ExecutorAddr DeregisterEHFrameSectionAllocAction;
27*700637cbSDimitry Andric   if (auto Err = ES.getExecutorProcessControl().getBootstrapSymbols(
28*700637cbSDimitry Andric           {{RegisterEHFrameSectionAllocAction,
29*700637cbSDimitry Andric             rt::RegisterEHFrameSectionAllocActionName},
30*700637cbSDimitry Andric            {DeregisterEHFrameSectionAllocAction,
31*700637cbSDimitry Andric             rt::DeregisterEHFrameSectionAllocActionName}}))
32*700637cbSDimitry Andric     return std::move(Err);
33*700637cbSDimitry Andric 
34*700637cbSDimitry Andric   return std::make_unique<EHFrameRegistrationPlugin>(
35*700637cbSDimitry Andric       RegisterEHFrameSectionAllocAction, DeregisterEHFrameSectionAllocAction);
36*700637cbSDimitry Andric }
37*700637cbSDimitry Andric 
modifyPassConfig(MaterializationResponsibility & MR,LinkGraph & LG,PassConfiguration & PassConfig)38*700637cbSDimitry Andric void EHFrameRegistrationPlugin::modifyPassConfig(
39*700637cbSDimitry Andric     MaterializationResponsibility &MR, LinkGraph &LG,
40*700637cbSDimitry Andric     PassConfiguration &PassConfig) {
41*700637cbSDimitry Andric   if (LG.getTargetTriple().isOSBinFormatMachO())
42*700637cbSDimitry Andric     PassConfig.PrePrunePasses.insert(
43*700637cbSDimitry Andric         PassConfig.PrePrunePasses.begin(), [](LinkGraph &G) {
44*700637cbSDimitry Andric           if (auto *CUSec = G.findSectionByName(MachOCompactUnwindSectionName))
45*700637cbSDimitry Andric             G.removeSection(*CUSec);
46*700637cbSDimitry Andric           return Error::success();
47*700637cbSDimitry Andric         });
48*700637cbSDimitry Andric 
49*700637cbSDimitry Andric   PassConfig.PostFixupPasses.push_back([this](LinkGraph &G) -> Error {
50*700637cbSDimitry Andric     if (auto *EHFrame = getEHFrameSection(G)) {
51*700637cbSDimitry Andric       using namespace shared;
52*700637cbSDimitry Andric       auto R = SectionRange(*EHFrame).getRange();
53*700637cbSDimitry Andric       G.allocActions().push_back(
54*700637cbSDimitry Andric           {cantFail(
55*700637cbSDimitry Andric                WrapperFunctionCall::Create<SPSArgList<SPSExecutorAddrRange>>(
56*700637cbSDimitry Andric                    RegisterEHFrame, R)),
57*700637cbSDimitry Andric            cantFail(
58*700637cbSDimitry Andric                WrapperFunctionCall::Create<SPSArgList<SPSExecutorAddrRange>>(
59*700637cbSDimitry Andric                    DeregisterEHFrame, R))});
60*700637cbSDimitry Andric     }
61*700637cbSDimitry Andric     return Error::success();
62*700637cbSDimitry Andric   });
63*700637cbSDimitry Andric }
64*700637cbSDimitry Andric 
65*700637cbSDimitry Andric } // namespace llvm::orc
66