LLVM 22.0.0git
GetDylibInterface.cpp
Go to the documentation of this file.
1//===-------- GetDylibInterface.cpp - Get interface for real dylib --------===//
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
10
12#include "llvm/Object/MachO.h"
15
16#define DEBUG_TYPE "orc"
17
18namespace llvm::orc {
19
21 Twine Path) {
22 auto CPUType = MachO::getCPUType(ES.getTargetTriple());
23 if (!CPUType)
24 return CPUType.takeError();
25
26 auto CPUSubType = MachO::getCPUSubType(ES.getTargetTriple());
27 if (!CPUSubType)
28 return CPUSubType.takeError();
29
30 auto Buf = MemoryBuffer::getFile(Path);
31 if (!Buf)
32 return createFileError(Path, Buf.getError());
33
34 auto BinFile = object::createBinary((*Buf)->getMemBufferRef());
35 if (!BinFile)
36 return BinFile.takeError();
37
38 std::unique_ptr<object::MachOObjectFile> MachOFile;
39 if (isa<object::MachOObjectFile>(**BinFile))
40 MachOFile.reset(dyn_cast<object::MachOObjectFile>(BinFile->release()));
41 else if (auto *MachOUni =
42 dyn_cast<object::MachOUniversalBinary>(BinFile->get())) {
43 for (auto &O : MachOUni->objects()) {
44 if (O.getCPUType() == *CPUType &&
45 (O.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) == *CPUSubType) {
46 if (auto Obj = O.getAsObjectFile())
47 MachOFile = std::move(*Obj);
48 else
49 return Obj.takeError();
50 break;
51 }
52 }
53 if (!MachOFile)
54 return make_error<StringError>("MachO universal binary at " + Path +
55 " does not contain a slice for " +
56 ES.getTargetTriple().str(),
58 } else
59 return make_error<StringError>("File at " + Path + " is not a MachO",
61
62 if (MachOFile->getHeader().filetype != MachO::MH_DYLIB)
63 return make_error<StringError>("MachO at " + Path + " is not a dylib",
65
66 SymbolNameSet Symbols;
67 for (auto &Sym : MachOFile->symbols()) {
68 if (auto Name = Sym.getName())
69 Symbols.insert(ES.intern(*Name));
70 else
71 return Name.takeError();
72 }
73
74 return std::move(Symbols);
75}
76
78 Twine Path) {
79 SymbolNameSet Symbols;
80
81 auto TapiFileBuffer = MemoryBuffer::getFile(Path);
82 if (!TapiFileBuffer)
83 return createFileError(Path, TapiFileBuffer.getError());
84
85 auto Tapi =
86 object::TapiUniversal::create((*TapiFileBuffer)->getMemBufferRef());
87 if (!Tapi)
88 return Tapi.takeError();
89
90 auto CPUType = MachO::getCPUType(ES.getTargetTriple());
91 if (!CPUType)
92 return CPUType.takeError();
93
94 auto CPUSubType = MachO::getCPUSubType(ES.getTargetTriple());
95 if (!CPUSubType)
96 return CPUSubType.takeError();
97
98 auto &IF = (*Tapi)->getInterfaceFile();
99 auto Interface =
100 IF.extract(MachO::getArchitectureFromCpuType(*CPUType, *CPUSubType));
101 if (!Interface)
102 return Interface.takeError();
103
104 for (auto *Sym : (*Interface)->exports())
105 Symbols.insert(ES.intern(Sym->getName()));
106
107 return Symbols;
108}
109
111 file_magic Magic;
112 if (auto EC = identify_magic(Path, Magic))
113 return createFileError(Path, EC);
114
115 switch (Magic) {
118 return getDylibInterfaceFromDylib(ES, Path);
120 return getDylibInterfaceFromTapiFile(ES, Path);
121 default:
122 return make_error<StringError>("Cannot get interface for " + Path +
123 " unrecognized file type",
125 }
126}
127
128} // namespace llvm::orc
std::string Name
Symbol * Sym
Definition: ELF_riscv.cpp:479
Tagged union holding either a T or a Error.
Definition: Error.h:485
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
const std::string & str() const
Definition: Triple.h:475
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:82
static Expected< std::unique_ptr< TapiUniversal > > create(MemoryBufferRef Source)
An ExecutionSession represents a running JIT program.
Definition: Core.h:1355
const Triple & getTargetTriple() const
Return the triple for the executor.
Definition: Core.h:1398
SymbolStringPtr intern(StringRef SymName)
Add a symbol name to the SymbolStringPool and return a pointer to it.
Definition: Core.h:1409
@ CPU_SUBTYPE_MASK
Definition: MachO.h:1581
LLVM_ABI Expected< uint32_t > getCPUSubType(const Triple &T)
Definition: MachO.cpp:95
@ MH_DYLIB
Definition: MachO.h:48
LLVM_ABI Architecture getArchitectureFromCpuType(uint32_t CPUType, uint32_t CPUSubType)
Convert a CPU Type and Subtype pair to an architecture slice.
LLVM_ABI Expected< uint32_t > getCPUType(const Triple &T)
Definition: MachO.cpp:77
LLVM_ABI Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr, bool InitContent=true)
Create a Binary from Source, autodetecting the file type.
Definition: Binary.cpp:45
LLVM_ABI Expected< SymbolNameSet > getDylibInterfaceFromDylib(ExecutionSession &ES, Twine Path)
Returns a SymbolNameSet containing the exported symbols defined in the given dylib.
LLVM_ABI Expected< SymbolNameSet > getDylibInterface(ExecutionSession &ES, Twine Path)
Returns a SymbolNameSet containing the exported symbols defined in the relevant slice of the given fi...
LLVM_ABI Expected< SymbolNameSet > getDylibInterfaceFromTapiFile(ExecutionSession &ES, Twine Path)
Returns a SymbolNameSet containing the exported symbols defined in the relevant slice of the TapiUniv...
LLVM_ABI file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Magic.cpp:33
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Definition: Error.h:1399
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:98
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: Magic.h:21
@ macho_dynamically_linked_shared_lib
Mach-O dynlinked shared lib.
Definition: Magic.h:38
@ macho_universal_binary
Mach-O universal binary.
Definition: Magic.h:44
@ tapi_file
Text-based Dynamic Library Stub file.
Definition: Magic.h:56