1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
//===-------- GetDylibInterface.cpp - Get interface for real dylib --------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/ExecutionEngine/Orc/GetDylibInterface.h"
#include "llvm/BinaryFormat/Magic.h"
#include "llvm/Object/MachO.h"
#include "llvm/Object/MachOUniversal.h"
#include "llvm/Object/TapiUniversal.h"
#define DEBUG_TYPE "orc"
namespace llvm::orc {
Expected<SymbolNameSet> getDylibInterfaceFromDylib(ExecutionSession &ES,
Twine Path) {
auto CPUType = MachO::getCPUType(ES.getTargetTriple());
if (!CPUType)
return CPUType.takeError();
auto CPUSubType = MachO::getCPUSubType(ES.getTargetTriple());
if (!CPUSubType)
return CPUSubType.takeError();
auto Buf = MemoryBuffer::getFile(Path);
if (!Buf)
return createFileError(Path, Buf.getError());
auto BinFile = object::createBinary((*Buf)->getMemBufferRef());
if (!BinFile)
return BinFile.takeError();
std::unique_ptr<object::MachOObjectFile> MachOFile;
if (isa<object::MachOObjectFile>(**BinFile))
MachOFile.reset(dyn_cast<object::MachOObjectFile>(BinFile->release()));
else if (auto *MachOUni =
dyn_cast<object::MachOUniversalBinary>(BinFile->get())) {
for (auto &O : MachOUni->objects()) {
if (O.getCPUType() == *CPUType &&
(O.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) == *CPUSubType) {
if (auto Obj = O.getAsObjectFile())
MachOFile = std::move(*Obj);
else
return Obj.takeError();
break;
}
}
if (!MachOFile)
return make_error<StringError>("MachO universal binary at " + Path +
" does not contain a slice for " +
ES.getTargetTriple().str(),
inconvertibleErrorCode());
} else
return make_error<StringError>("File at " + Path + " is not a MachO",
inconvertibleErrorCode());
if (MachOFile->getHeader().filetype != MachO::MH_DYLIB)
return make_error<StringError>("MachO at " + Path + " is not a dylib",
inconvertibleErrorCode());
SymbolNameSet Symbols;
for (auto &Sym : MachOFile->symbols()) {
if (auto Name = Sym.getName())
Symbols.insert(ES.intern(*Name));
else
return Name.takeError();
}
return std::move(Symbols);
}
Expected<SymbolNameSet> getDylibInterfaceFromTapiFile(ExecutionSession &ES,
Twine Path) {
SymbolNameSet Symbols;
auto TapiFileBuffer = MemoryBuffer::getFile(Path);
if (!TapiFileBuffer)
return createFileError(Path, TapiFileBuffer.getError());
auto Tapi =
object::TapiUniversal::create((*TapiFileBuffer)->getMemBufferRef());
if (!Tapi)
return Tapi.takeError();
auto CPUType = MachO::getCPUType(ES.getTargetTriple());
if (!CPUType)
return CPUType.takeError();
auto CPUSubType = MachO::getCPUSubType(ES.getTargetTriple());
if (!CPUSubType)
return CPUSubType.takeError();
auto &IF = (*Tapi)->getInterfaceFile();
auto Interface =
IF.extract(MachO::getArchitectureFromCpuType(*CPUType, *CPUSubType));
if (!Interface)
return Interface.takeError();
for (auto *Sym : (*Interface)->exports())
Symbols.insert(ES.intern(Sym->getName()));
return Symbols;
}
Expected<SymbolNameSet> getDylibInterface(ExecutionSession &ES, Twine Path) {
file_magic Magic;
if (auto EC = identify_magic(Path, Magic))
return createFileError(Path, EC);
switch (Magic) {
case file_magic::macho_universal_binary:
case file_magic::macho_dynamically_linked_shared_lib:
return getDylibInterfaceFromDylib(ES, Path);
case file_magic::tapi_file:
return getDylibInterfaceFromTapiFile(ES, Path);
default:
return make_error<StringError>("Cannot get interface for " + Path +
" unrecognized file type",
inconvertibleErrorCode());
}
}
} // namespace llvm::orc
|