xref: /freebsd/contrib/llvm-project/llvm/lib/ExecutionEngine/Orc/EPCDebugObjectRegistrar.cpp (revision 397e83df75e0fcd0d3fcb95ae4d794cb7600fc89)
1 //===----- EPCDebugObjectRegistrar.cpp - EPC-based debug 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/EPCDebugObjectRegistrar.h"
10 
11 #include "llvm/ExecutionEngine/Orc/Core.h"
12 #include "llvm/ExecutionEngine/Orc/Shared/SimplePackedSerialization.h"
13 #include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderGDB.h"
14 #include "llvm/Support/BinaryStreamWriter.h"
15 
16 namespace llvm {
17 namespace orc {
18 
19 Expected<std::unique_ptr<EPCDebugObjectRegistrar>> createJITLoaderGDBRegistrar(
20     ExecutionSession &ES,
21     std::optional<ExecutorAddr> RegistrationFunctionDylib) {
22   auto &EPC = ES.getExecutorProcessControl();
23 
24   if (!RegistrationFunctionDylib) {
25     if (auto D = EPC.loadDylib(nullptr))
26       RegistrationFunctionDylib = *D;
27     else
28       return D.takeError();
29   }
30 
31   SymbolStringPtr RegisterFn =
32       EPC.getTargetTriple().isOSBinFormatMachO()
33           ? EPC.intern("_llvm_orc_registerJITLoaderGDBWrapper")
34           : EPC.intern("llvm_orc_registerJITLoaderGDBWrapper");
35 
36   SymbolLookupSet RegistrationSymbols;
37   RegistrationSymbols.add(RegisterFn);
38 
39   auto Result =
40       EPC.lookupSymbols({{*RegistrationFunctionDylib, RegistrationSymbols}});
41   if (!Result)
42     return Result.takeError();
43 
44   assert(Result->size() == 1 && "Unexpected number of dylibs in result");
45   assert((*Result)[0].size() == 1 &&
46          "Unexpected number of addresses in result");
47 
48   ExecutorAddr RegisterAddr = (*Result)[0][0].getAddress();
49   return std::make_unique<EPCDebugObjectRegistrar>(ES, RegisterAddr);
50 }
51 
52 Error EPCDebugObjectRegistrar::registerDebugObject(ExecutorAddrRange TargetMem,
53                                                    bool AutoRegisterCode) {
54   return ES.callSPSWrapper<void(shared::SPSExecutorAddrRange, bool)>(
55       RegisterFn, TargetMem, AutoRegisterCode);
56 }
57 
58 } // namespace orc
59 } // namespace llvm
60