20#define DEBUG_TYPE "orc"
29 Desc += (TT.getArchName() +
" slice of universal binary ").str();
30 Desc += Obj.getBufferIdentifier();
34template <
typename HeaderType>
36 bool SwapEndianness,
const Triple &TT,
41 memcpy(&Hdr,
Data.data(),
sizeof(HeaderType));
48 " is not a MachO relocatable object",
53 if (ObjArchTT.getArch() != TT.getArch())
55 objDesc(Obj, TT, ObjIsSlice) +
" (" + ObjArchTT.getArchName() +
56 "), cannot be loaded into " + TT.str() +
" process",
69 " is not a valid MachO relocatable object file (truncated header)",
87 " is not a valid MachO relocatable object (bad magic value)",
97 return std::move(Err);
98 return std::move(Obj);
103 std::optional<StringRef> IdentifierOverride) {
106 "TT must specify MachO or Unknown object format");
108 if (!IdentifierOverride)
109 IdentifierOverride = Path;
122 StringRef(
"Could not load MachO object at path ") + Path,
129 return CheckedObj.takeError();
130 return std::make_pair(std::move(*CheckedObj),
136 *IdentifierOverride);
139 Path +
" does not contain a relocatable object file compatible with " +
147 std::unique_ptr<MemoryBuffer> UBBuf,
155 return UniversalBin.takeError();
159 return SliceRange.takeError();
165 "Could not load " + TT.getArchName() +
166 " slice of MachO universal binary at path " + UBPath,
178 return CheckedObj.takeError();
179 return std::make_pair(std::move(*CheckedObj),
191 return "a mach-o relocatable object file";
193 return "a mach-o relocatable object file or archive";
201 " does not contain " + FT(),
209 for (
const auto &Obj : UB.
objects()) {
210 auto ObjTT = Obj.getTriple();
211 if (ObjTT.getArch() == TT.getArch() &&
212 ObjTT.getSubArch() == TT.getSubArch() &&
214 ObjTT.getVendor() == TT.getVendor())) {
216 return std::make_pair(Obj.getOffset(), Obj.getSize());
221 " does not contain a slice for " +
231 return UB.takeError();
239 auto LoadMember = [&]() {
247 if (
auto Err = L.add(JD, LoadMember()))
263 for (
auto Sec : MachOObj->sections()) {
265 MachOObj->getSectionFinalSegmentName(Sec.getRawDataRefImpl());
266 if (
auto SecName = Sec.getName()) {
267 if (*SecName ==
"__objc_classlist" || *SecName ==
"__objc_protolist" ||
268 *SecName ==
"__objc_clsrolist" || *SecName ==
"__objc_catlist" ||
269 *SecName ==
"__objc_catlist2" || *SecName ==
"__objc_nlcatlist" ||
270 (SegName ==
"__TEXT" && (*SecName).starts_with(
"__swift") &&
271 *SecName !=
"__swift_modhash")) {
272 if (
auto Err = L.add(JD, LoadMember()))
277 return SecName.takeError();
289 Result.push_back({CPUType, CPUSubType});
303 switch (CPUSubType) {
323 return InitCPUType.takeError();
327 return InitCPUSubType.takeError();
335 return BinFile.takeError();
337 std::unique_ptr<object::MachOObjectFile> MachOFile;
342 }
else if (
auto *MachOUni =
345 if (GetFallbackArchs)
346 ArchsToTry = GetFallbackArchs(*InitCPUType, *InitCPUSubType);
348 ArchsToTry.
push_back({*InitCPUType, *InitCPUSubType});
350 for (
auto &[CPUType, CPUSubType] : ArchsToTry) {
351 for (
auto &O : MachOUni->objects()) {
352 if (O.getCPUType() == CPUType &&
354 if (
auto Obj = O.getAsObjectFile())
355 MachOFile = std::move(*Obj);
357 return Obj.takeError();
366 "MachO universal binary at " + Path +
367 " does not contain a compatible slice for " +
379 for (
auto &Sym : MachOFile->symbols()) {
380 if (
auto Name = Sym.getName())
381 Symbols.insert(ES.
intern(*Name));
383 return Name.takeError();
386 return std::move(Symbols);
401 return Tapi.takeError();
405 return InitCPUType.takeError();
409 return InitCPUSubType.takeError();
412 if (GetFallbackArchs)
413 ArchsToTry = GetFallbackArchs(*InitCPUType, *InitCPUSubType);
415 ArchsToTry.
push_back({*InitCPUType, *InitCPUSubType});
417 auto &IF = (*Tapi)->getInterfaceFile();
419 auto ArchSet = IF.getArchitectures();
420 for (
auto [CPUType, CPUSubType] : ArchsToTry) {
422 if (ArchSet.has(
A)) {
423 if (
auto Interface = IF.extract(
A)) {
424 for (
auto *Sym : (*Interface)->exports())
425 Symbols.insert(ES.
intern(Sym->getName()));
428 return Interface.takeError();
433 "MachO interface file at " + Path +
434 " does not contain a compatible slice for " +
453 " unrecognized file type",
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Given an already-open file descriptor, read the file and return a MemoryBuffer.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFileSlice(sys::fs::file_t FD, const Twine &Filename, uint64_t MapSize, int64_t Offset, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Given an already-open file descriptor, map some slice of it into a MemoryBuffer.
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,...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Triple - Helper class for working with autoconf configuration names.
const std::string & str() const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
StringRef getFileName() const
Triple getArchTriple(const char **McpuDefault=nullptr) const
static Expected< std::unique_ptr< MachOUniversalBinary > > create(MemoryBufferRef Source)
iterator_range< object_iterator > objects() const
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
static Expected< std::unique_ptr< TapiUniversal > > create(MemoryBufferRef Source)
An ExecutionSession represents a running JIT program.
const Triple & getTargetTriple() const
Return the triple for the executor.
SymbolStringPtr intern(StringRef SymName)
Add a symbol name to the SymbolStringPool and return a pointer to it.
LLVM_ABI Expected< bool > operator()(object::Archive &A, MemoryBufferRef MemberBuf, size_t Index)
static std::unique_ptr< MemoryBuffer > createMemberBuffer(object::Archive &A, MemoryBufferRef BufRef, size_t Index)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI Expected< uint32_t > getCPUSubType(const Triple &T)
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)
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.
LLVM_ABI SmallVector< std::pair< uint32_t, uint32_t > > noFallbackArchs(uint32_t CPUType, uint32_t CPUSubType)
Match the exact CPU type/subtype only.
LLVM_ABI Expected< SymbolNameSet > getDylibInterfaceFromTapiFile(ExecutionSession &ES, Twine Path, GetFallbackArchsFn GetFallbackArchs=standardMachOFallbackArchs)
Returns a SymbolNameSet containing the exported symbols defined in the relevant slice of the TapiUniv...
LLVM_ABI Expected< std::pair< std::unique_ptr< MemoryBuffer >, LinkableFileKind > > loadLinkableSliceFromMachOUniversalBinary(sys::fs::file_t FD, std::unique_ptr< MemoryBuffer > UBBuf, const Triple &TT, LoadArchives LA, StringRef UBPath, StringRef Identifier)
Load a compatible relocatable object (if available) from a MachO universal binary.
unique_function< SmallVector< std::pair< uint32_t, uint32_t > >( uint32_t CPUType, uint32_t CPUSubType)> GetFallbackArchsFn
LLVM_ABI SmallVector< std::pair< uint32_t, uint32_t > > standardMachOFallbackArchs(uint32_t CPUType, uint32_t CPUSubType)
Match standard dynamic loader fallback rules.
LLVM_ABI Expected< SymbolNameSet > getDylibInterface(ExecutionSession &ES, Twine Path, GetFallbackArchsFn GetFallbackArchs=standardMachOFallbackArchs)
Returns a SymbolNameSet containing the exported symbols defined in the relevant slice of the given fi...
LLVM_ABI Error checkMachORelocatableObject(MemoryBufferRef Obj, const Triple &TT, bool ObjIsSlice)
Check that the given buffer contains a MachO object file compatible with the given triple.
LLVM_ABI Expected< std::pair< size_t, size_t > > getMachOSliceRangeForTriple(object::MachOUniversalBinary &UB, const Triple &TT)
Utility for identifying the file-slice compatible with TT in a universal binary.
LLVM_ABI Expected< SymbolNameSet > getDylibInterfaceFromDylib(ExecutionSession &ES, Twine Path, GetFallbackArchsFn GetFallbackArchs=standardMachOFallbackArchs)
Returns a SymbolNameSet containing the exported symbols defined in the given dylib.
DenseSet< SymbolStringPtr > SymbolNameSet
A set of symbol names (represented by SymbolStringPtrs for.
static std::string objDesc(const MemoryBufferRef &Obj, const Triple &TT, bool ObjIsSlice)
Expected< std::pair< std::unique_ptr< MemoryBuffer >, LinkableFileKind > > loadMachORelocatableObject(StringRef Path, const Triple &TT, LoadArchives LA, std::optional< StringRef > IdentifierOverride)
LLVM_ABI std::error_code closeFile(file_t &F)
Close the file object.
LLVM_ABI Expected< file_t > openNativeFileForRead(const Twine &Name, OpenFlags Flags=OF_None, SmallVectorImpl< char > *RealPath=nullptr)
Opens the file with the given name in a read-only mode, returning its open file descriptor.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
FunctionAddr VTableAddr uintptr_t uintptr_t Data
void consumeError(Error Err)
Consume a Error without doing anything.
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
@ archive
ar style archive file
@ macho_dynamically_linked_shared_lib
Mach-O dynlinked shared lib.
@ macho_universal_binary
Mach-O universal binary.
@ macho_object
Mach-O Object file.
@ tapi_file
Text-based Dynamic Library Stub file.