Skip to content

Commit 17f0afe

Browse files
authored
[ORC] Merge GetDylibInterface.h APIs into MachO.h. (#168462)
These APIs are MachO specific, and the interfaces are about to be extended to support more MachO-specific behavior. For now it makes sense to group them with other MachO specific APIs in MachO.h.
1 parent f6ebb35 commit 17f0afe

File tree

6 files changed

+128
-171
lines changed

6 files changed

+128
-171
lines changed

llvm/include/llvm/ExecutionEngine/Orc/GetDylibInterface.h

Lines changed: 0 additions & 41 deletions
This file was deleted.

llvm/include/llvm/ExecutionEngine/Orc/MachO.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef LLVM_EXECUTIONENGINE_ORC_MACHO_H
1414
#define LLVM_EXECUTIONENGINE_ORC_MACHO_H
1515

16+
#include "llvm/ExecutionEngine/Orc/CoreContainers.h"
1617
#include "llvm/ExecutionEngine/Orc/LoadLinkableFile.h"
1718
#include "llvm/Object/Archive.h"
1819
#include "llvm/Support/Compiler.h"
@@ -31,6 +32,7 @@ class MachOUniversalBinary;
3132

3233
namespace orc {
3334

35+
class ExecutionSession;
3436
class JITDylib;
3537
class ObjectLayer;
3638

@@ -93,6 +95,22 @@ class ForceLoadMachOArchiveMembers {
9395
bool ObjCOnly;
9496
};
9597

98+
/// Returns a SymbolNameSet containing the exported symbols defined in the
99+
/// given dylib.
100+
LLVM_ABI Expected<SymbolNameSet>
101+
getDylibInterfaceFromDylib(ExecutionSession &ES, Twine Path);
102+
103+
/// Returns a SymbolNameSet containing the exported symbols defined in the
104+
/// relevant slice of the TapiUniversal file.
105+
LLVM_ABI Expected<SymbolNameSet>
106+
getDylibInterfaceFromTapiFile(ExecutionSession &ES, Twine Path);
107+
108+
/// Returns a SymbolNameSet containing the exported symbols defined in the
109+
/// relevant slice of the given file, which may be either a dylib or a tapi
110+
/// file.
111+
LLVM_ABI Expected<SymbolNameSet> getDylibInterface(ExecutionSession &ES,
112+
Twine Path);
113+
96114
} // namespace orc
97115
} // namespace llvm
98116

llvm/lib/ExecutionEngine/Orc/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ add_llvm_component_library(LLVMOrcJIT
2626
ExecutionUtils.cpp
2727
ExecutorResolutionGenerator.cpp
2828
ObjectFileInterface.cpp
29-
GetDylibInterface.cpp
3029
IndirectionUtils.cpp
3130
InProcessMemoryAccess.cpp
3231
IRCompileLayer.cpp

llvm/lib/ExecutionEngine/Orc/GetDylibInterface.cpp

Lines changed: 0 additions & 128 deletions
This file was deleted.

llvm/lib/ExecutionEngine/Orc/MachO.cpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010

1111
#include "llvm/ADT/ScopeExit.h"
1212
#include "llvm/BinaryFormat/MachO.h"
13+
#include "llvm/BinaryFormat/Magic.h"
1314
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
1415
#include "llvm/ExecutionEngine/Orc/Layer.h"
1516
#include "llvm/Object/MachOUniversal.h"
17+
#include "llvm/Object/TapiUniversal.h"
1618
#include "llvm/Support/FileSystem.h"
1719

1820
#define DEBUG_TYPE "orc"
@@ -280,5 +282,113 @@ Expected<bool> ForceLoadMachOArchiveMembers::operator()(
280282
return true;
281283
}
282284

285+
Expected<SymbolNameSet> getDylibInterfaceFromDylib(ExecutionSession &ES,
286+
Twine Path) {
287+
auto CPUType = MachO::getCPUType(ES.getTargetTriple());
288+
if (!CPUType)
289+
return CPUType.takeError();
290+
291+
auto CPUSubType = MachO::getCPUSubType(ES.getTargetTriple());
292+
if (!CPUSubType)
293+
return CPUSubType.takeError();
294+
295+
auto Buf = MemoryBuffer::getFile(Path);
296+
if (!Buf)
297+
return createFileError(Path, Buf.getError());
298+
299+
auto BinFile = object::createBinary((*Buf)->getMemBufferRef());
300+
if (!BinFile)
301+
return BinFile.takeError();
302+
303+
std::unique_ptr<object::MachOObjectFile> MachOFile;
304+
if (isa<object::MachOObjectFile>(**BinFile))
305+
MachOFile.reset(dyn_cast<object::MachOObjectFile>(BinFile->release()));
306+
else if (auto *MachOUni =
307+
dyn_cast<object::MachOUniversalBinary>(BinFile->get())) {
308+
for (auto &O : MachOUni->objects()) {
309+
if (O.getCPUType() == *CPUType &&
310+
(O.getCPUSubType() & ~MachO::CPU_SUBTYPE_MASK) == *CPUSubType) {
311+
if (auto Obj = O.getAsObjectFile())
312+
MachOFile = std::move(*Obj);
313+
else
314+
return Obj.takeError();
315+
break;
316+
}
317+
}
318+
if (!MachOFile)
319+
return make_error<StringError>("MachO universal binary at " + Path +
320+
" does not contain a slice for " +
321+
ES.getTargetTriple().str(),
322+
inconvertibleErrorCode());
323+
} else
324+
return make_error<StringError>("File at " + Path + " is not a MachO",
325+
inconvertibleErrorCode());
326+
327+
if (MachOFile->getHeader().filetype != MachO::MH_DYLIB)
328+
return make_error<StringError>("MachO at " + Path + " is not a dylib",
329+
inconvertibleErrorCode());
330+
331+
SymbolNameSet Symbols;
332+
for (auto &Sym : MachOFile->symbols()) {
333+
if (auto Name = Sym.getName())
334+
Symbols.insert(ES.intern(*Name));
335+
else
336+
return Name.takeError();
337+
}
338+
339+
return std::move(Symbols);
340+
}
341+
342+
Expected<SymbolNameSet> getDylibInterfaceFromTapiFile(ExecutionSession &ES,
343+
Twine Path) {
344+
SymbolNameSet Symbols;
345+
346+
auto TapiFileBuffer = MemoryBuffer::getFile(Path);
347+
if (!TapiFileBuffer)
348+
return createFileError(Path, TapiFileBuffer.getError());
349+
350+
auto Tapi =
351+
object::TapiUniversal::create((*TapiFileBuffer)->getMemBufferRef());
352+
if (!Tapi)
353+
return Tapi.takeError();
354+
355+
auto CPUType = MachO::getCPUType(ES.getTargetTriple());
356+
if (!CPUType)
357+
return CPUType.takeError();
358+
359+
auto CPUSubType = MachO::getCPUSubType(ES.getTargetTriple());
360+
if (!CPUSubType)
361+
return CPUSubType.takeError();
362+
363+
auto &IF = (*Tapi)->getInterfaceFile();
364+
auto Interface =
365+
IF.extract(MachO::getArchitectureFromCpuType(*CPUType, *CPUSubType));
366+
if (!Interface)
367+
return Interface.takeError();
368+
369+
for (auto *Sym : (*Interface)->exports())
370+
Symbols.insert(ES.intern(Sym->getName()));
371+
372+
return Symbols;
373+
}
374+
375+
Expected<SymbolNameSet> getDylibInterface(ExecutionSession &ES, Twine Path) {
376+
file_magic Magic;
377+
if (auto EC = identify_magic(Path, Magic))
378+
return createFileError(Path, EC);
379+
380+
switch (Magic) {
381+
case file_magic::macho_universal_binary:
382+
case file_magic::macho_dynamically_linked_shared_lib:
383+
return getDylibInterfaceFromDylib(ES, Path);
384+
case file_magic::tapi_file:
385+
return getDylibInterfaceFromTapiFile(ES, Path);
386+
default:
387+
return make_error<StringError>("Cannot get interface for " + Path +
388+
" unrecognized file type",
389+
inconvertibleErrorCode());
390+
}
391+
}
392+
283393
} // End namespace orc.
284394
} // End namespace llvm.

llvm/tools/llvm-jitlink/llvm-jitlink.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "llvm/ExecutionEngine/Orc/EPCDebugObjectRegistrar.h"
2828
#include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
2929
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
30-
#include "llvm/ExecutionEngine/Orc/GetDylibInterface.h"
3130
#include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
3231
#include "llvm/ExecutionEngine/Orc/JITLinkRedirectableSymbolManager.h"
3332
#include "llvm/ExecutionEngine/Orc/JITLinkReentryTrampolines.h"

0 commit comments

Comments
 (0)