LLVM  13.0.0git
TPCEHFrameRegistrar.cpp
Go to the documentation of this file.
1 //===------ TPCEHFrameRegistrar.cpp - TPC-based eh-frame 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 
11 
12 namespace llvm {
13 namespace orc {
14 
15 Expected<std::unique_ptr<TPCEHFrameRegistrar>>
17  // FIXME: Proper mangling here -- we really need to decouple linker mangling
18  // from DataLayout.
19 
20  // Find the addresses of the registration/deregistration functions in the
21  // target process.
22  auto ProcessHandle = TPC.loadDylib(nullptr);
23  if (!ProcessHandle)
24  return ProcessHandle.takeError();
25 
26  std::string RegisterWrapperName, DeregisterWrapperName;
27  if (TPC.getTargetTriple().isOSBinFormatMachO()) {
28  RegisterWrapperName += '_';
29  DeregisterWrapperName += '_';
30  }
31  RegisterWrapperName += "llvm_orc_registerEHFrameSectionWrapper";
32  DeregisterWrapperName += "llvm_orc_deregisterEHFrameSectionWrapper";
33 
34  SymbolLookupSet RegistrationSymbols;
35  RegistrationSymbols.add(TPC.intern(RegisterWrapperName));
36  RegistrationSymbols.add(TPC.intern(DeregisterWrapperName));
37 
38  auto Result = TPC.lookupSymbols({{*ProcessHandle, RegistrationSymbols}});
39  if (!Result)
40  return Result.takeError();
41 
42  assert(Result->size() == 1 && "Unexpected number of dylibs in result");
43  assert((*Result)[0].size() == 2 &&
44  "Unexpected number of addresses in result");
45 
46  auto RegisterEHFrameWrapperFnAddr = (*Result)[0][0];
47  auto DeregisterEHFrameWrapperFnAddr = (*Result)[0][1];
48 
49  return std::make_unique<TPCEHFrameRegistrar>(
50  TPC, RegisterEHFrameWrapperFnAddr, DeregisterEHFrameWrapperFnAddr);
51 }
52 
54  size_t EHFrameSectionSize) {
55  constexpr size_t ArgBufferSize = sizeof(uint64_t) + sizeof(uint64_t);
56  uint8_t ArgBuffer[ArgBufferSize];
57  BinaryStreamWriter ArgWriter(
58  MutableArrayRef<uint8_t>(ArgBuffer, ArgBufferSize),
60  cantFail(ArgWriter.writeInteger(static_cast<uint64_t>(EHFrameSectionAddr)));
61  cantFail(ArgWriter.writeInteger(static_cast<uint64_t>(EHFrameSectionSize)));
62 
63  return TPC.runWrapper(RegisterEHFrameWrapperFnAddr, ArgBuffer).takeError();
64 }
65 
67  JITTargetAddress EHFrameSectionAddr, size_t EHFrameSectionSize) {
68  constexpr size_t ArgBufferSize = sizeof(uint64_t) + sizeof(uint64_t);
69  uint8_t ArgBuffer[ArgBufferSize];
70  BinaryStreamWriter ArgWriter(
71  MutableArrayRef<uint8_t>(ArgBuffer, ArgBufferSize),
73  cantFail(ArgWriter.writeInteger(static_cast<uint64_t>(EHFrameSectionAddr)));
74  cantFail(ArgWriter.writeInteger(static_cast<uint64_t>(EHFrameSectionSize)));
75 
76  return TPC.runWrapper(DeregisterEHFrameWrapperFnAddr, ArgBuffer).takeError();
77 }
78 
79 } // end namespace orc
80 } // end namespace llvm
llvm
Definition: AllocatorList.h:23
llvm::BinaryStreamWriter::writeInteger
Error writeInteger(T Value)
Write the integer Value to the underlying stream in the specified endianness.
Definition: BinaryStreamWriter.h:64
llvm::BinaryStreamWriter
Provides write only access to a subclass of WritableBinaryStream.
Definition: BinaryStreamWriter.h:31
llvm::orc::SymbolLookupSet
A set of symbols to look up, each associated with a SymbolLookupFlags value.
Definition: Core.h:173
llvm::orc::TPCEHFrameRegistrar::registerEHFrames
Error registerEHFrames(JITTargetAddress EHFrameSectionAddr, size_t EHFrameSectionSize) override
Definition: TPCEHFrameRegistrar.cpp:53
llvm::MutableArrayRef< uint8_t >
llvm::orc::TargetProcessControl::getTargetTriple
const Triple & getTargetTriple() const
Return the Triple for the target process.
Definition: TargetProcessControl.h:110
llvm::Triple::isOSBinFormatMachO
bool isOSBinFormatMachO() const
Tests whether the environment is MachO.
Definition: Triple.h:645
llvm::orc::SymbolLookupSet::add
SymbolLookupSet & add(SymbolStringPtr Name, SymbolLookupFlags Flags=SymbolLookupFlags::RequiredSymbol)
Add an element to the set.
Definition: Core.h:222
llvm::orc::TargetProcessControl::intern
SymbolStringPtr intern(StringRef SymName)
Intern a symbol name in the SymbolStringPool.
Definition: TargetProcessControl.h:104
llvm::orc::TargetProcessControl::loadDylib
virtual Expected< tpctypes::DylibHandle > loadDylib(const char *DylibPath)=0
Load the dynamic library at the given path and return a handle to it.
llvm::orc::TPCEHFrameRegistrar::Create
static Expected< std::unique_ptr< TPCEHFrameRegistrar > > Create(TargetProcessControl &TPC)
Create from a TargetProcessControl instance alone.
Definition: TPCEHFrameRegistrar.cpp:16
llvm::orc::TargetProcessControl
TargetProcessControl supports interaction with a JIT target process.
Definition: TargetProcessControl.h:32
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::orc::TPCEHFrameRegistrar::deregisterEHFrames
Error deregisterEHFrames(JITTargetAddress EHFrameSectionAddr, size_t EHFrameSectionSize) override
Definition: TPCEHFrameRegistrar.cpp:66
llvm::size
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1463
llvm::orc::TargetProcessControl::lookupSymbols
virtual Expected< std::vector< tpctypes::LookupResult > > lookupSymbols(ArrayRef< LookupRequest > Request)=0
Search for symbols in the target process.
llvm::cantFail
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:708
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::JITTargetAddress
uint64_t JITTargetAddress
Represents an address in the target process's address space.
Definition: JITSymbol.h:42
TPCEHFrameRegistrar.h
BinaryStreamWriter.h
llvm::support::big
@ big
Definition: Endian.h:27
llvm::orc::TargetProcessControl::runWrapper
virtual Expected< tpctypes::WrapperFunctionResult > runWrapper(JITTargetAddress WrapperFnAddr, ArrayRef< uint8_t > ArgBuffer)=0
Run a wrapper function with signature: