[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.
This commit is contained in:
Lang Hames
2025-11-18 11:51:08 +11:00
committed by GitHub
parent f6ebb357e5
commit 17f0afe40a
6 changed files with 128 additions and 171 deletions

View File

@@ -1,41 +0,0 @@
//===---- GetDylibInterface.h - Get interface for real dylib ----*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// Get symbol interface from a real dynamic library or TAPI file. These
// interfaces can be used to simulate weak linking (ld64 -weak-lx /
// -weak_library) against a library that is absent at runtime.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_EXECUTIONENGINE_ORC_GETDYLIBINTERFACE_H
#define LLVM_EXECUTIONENGINE_ORC_GETDYLIBINTERFACE_H
#include "llvm/ExecutionEngine/Orc/Core.h"
#include "llvm/Support/Compiler.h"
namespace llvm::orc {
/// Returns a SymbolNameSet containing the exported symbols defined in the
/// given dylib.
LLVM_ABI Expected<SymbolNameSet>
getDylibInterfaceFromDylib(ExecutionSession &ES, Twine Path);
/// Returns a SymbolNameSet containing the exported symbols defined in the
/// relevant slice of the TapiUniversal file.
LLVM_ABI Expected<SymbolNameSet>
getDylibInterfaceFromTapiFile(ExecutionSession &ES, Twine Path);
/// Returns a SymbolNameSet containing the exported symbols defined in the
/// relevant slice of the given file, which may be either a dylib or a tapi
/// file.
LLVM_ABI Expected<SymbolNameSet> getDylibInterface(ExecutionSession &ES,
Twine Path);
} // namespace llvm::orc
#endif // LLVM_EXECUTIONENGINE_ORC_GETDYLIBINTERFACE_H

View File

@@ -13,6 +13,7 @@
#ifndef LLVM_EXECUTIONENGINE_ORC_MACHO_H
#define LLVM_EXECUTIONENGINE_ORC_MACHO_H
#include "llvm/ExecutionEngine/Orc/CoreContainers.h"
#include "llvm/ExecutionEngine/Orc/LoadLinkableFile.h"
#include "llvm/Object/Archive.h"
#include "llvm/Support/Compiler.h"
@@ -31,6 +32,7 @@ class MachOUniversalBinary;
namespace orc {
class ExecutionSession;
class JITDylib;
class ObjectLayer;
@@ -93,6 +95,22 @@ private:
bool ObjCOnly;
};
/// Returns a SymbolNameSet containing the exported symbols defined in the
/// given dylib.
LLVM_ABI Expected<SymbolNameSet>
getDylibInterfaceFromDylib(ExecutionSession &ES, Twine Path);
/// Returns a SymbolNameSet containing the exported symbols defined in the
/// relevant slice of the TapiUniversal file.
LLVM_ABI Expected<SymbolNameSet>
getDylibInterfaceFromTapiFile(ExecutionSession &ES, Twine Path);
/// Returns a SymbolNameSet containing the exported symbols defined in the
/// relevant slice of the given file, which may be either a dylib or a tapi
/// file.
LLVM_ABI Expected<SymbolNameSet> getDylibInterface(ExecutionSession &ES,
Twine Path);
} // namespace orc
} // namespace llvm

View File

@@ -26,7 +26,6 @@ add_llvm_component_library(LLVMOrcJIT
ExecutionUtils.cpp
ExecutorResolutionGenerator.cpp
ObjectFileInterface.cpp
GetDylibInterface.cpp
IndirectionUtils.cpp
InProcessMemoryAccess.cpp
IRCompileLayer.cpp

View File

@@ -1,128 +0,0 @@
//===-------- 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

View File

@@ -10,9 +10,11 @@
#include "llvm/ADT/ScopeExit.h"
#include "llvm/BinaryFormat/MachO.h"
#include "llvm/BinaryFormat/Magic.h"
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
#include "llvm/ExecutionEngine/Orc/Layer.h"
#include "llvm/Object/MachOUniversal.h"
#include "llvm/Object/TapiUniversal.h"
#include "llvm/Support/FileSystem.h"
#define DEBUG_TYPE "orc"
@@ -280,5 +282,113 @@ Expected<bool> ForceLoadMachOArchiveMembers::operator()(
return true;
}
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());
}
}
} // End namespace orc.
} // End namespace llvm.

View File

@@ -27,7 +27,6 @@
#include "llvm/ExecutionEngine/Orc/EPCDebugObjectRegistrar.h"
#include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
#include "llvm/ExecutionEngine/Orc/GetDylibInterface.h"
#include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
#include "llvm/ExecutionEngine/Orc/JITLinkRedirectableSymbolManager.h"
#include "llvm/ExecutionEngine/Orc/JITLinkReentryTrampolines.h"