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",
52 if (ObjArch != TT.getArch())
55 ", cannot be loaded into " + TT.str() +
" process",
68 " is not a valid MachO relocatable object file (truncated header)",
86 " is not a valid MachO relocatable object (bad magic value)",
96 return std::move(Err);
97 return std::move(Obj);
102 std::optional<StringRef> IdentifierOverride) {
105 "TT must specify MachO or Unknown object format");
107 if (!IdentifierOverride)
108 IdentifierOverride = Path;
121 StringRef(
"Could not load MachO object at path ") + Path,
128 return CheckedObj.takeError();
129 return std::make_pair(std::move(*CheckedObj),
135 *IdentifierOverride);
138 Path +
" does not contain a relocatable object file compatible with " +
146 std::unique_ptr<MemoryBuffer> UBBuf,
154 return UniversalBin.takeError();
158 return SliceRange.takeError();
164 "Could not load " + TT.getArchName() +
165 " slice of MachO universal binary at path " + UBPath,
177 return CheckedObj.takeError();
178 return std::make_pair(std::move(*CheckedObj),
190 return "a mach-o relocatable object file";
192 return "a mach-o relocatable object file or archive";
200 " does not contain " + FT(),
208 for (
const auto &Obj : UB.
objects()) {
209 auto ObjTT = Obj.getTriple();
210 if (ObjTT.getArch() == TT.getArch() &&
211 ObjTT.getSubArch() == TT.getSubArch() &&
213 ObjTT.getVendor() == TT.getVendor())) {
215 return std::make_pair(Obj.getOffset(), Obj.getSize());
220 " does not contain a slice for " +
230 return UB.takeError();
238 auto LoadMember = [&]() {
246 if (
auto Err = L.add(JD, LoadMember()))
262 for (
auto Sec : MachOObj->sections()) {
264 MachOObj->getSectionFinalSegmentName(Sec.getRawDataRefImpl());
265 if (
auto SecName = Sec.getName()) {
266 if (*SecName ==
"__objc_classlist" || *SecName ==
"__objc_protolist" ||
267 *SecName ==
"__objc_clsrolist" || *SecName ==
"__objc_catlist" ||
268 *SecName ==
"__objc_catlist2" || *SecName ==
"__objc_nlcatlist" ||
269 (SegName ==
"__TEXT" && (*SecName).starts_with(
"__swift") &&
270 *SecName !=
"__swift_modhash")) {
271 if (
auto Err = L.add(JD, LoadMember()))
276 return SecName.takeError();
288 Result.push_back({CPUType, CPUSubType});
302 switch (CPUSubType) {
322 return InitCPUType.takeError();
326 return InitCPUSubType.takeError();
334 return BinFile.takeError();
336 std::unique_ptr<object::MachOObjectFile> MachOFile;
341 }
else if (
auto *MachOUni =
344 if (GetFallbackArchs)
345 ArchsToTry = GetFallbackArchs(*InitCPUType, *InitCPUSubType);
347 ArchsToTry.
push_back({*InitCPUType, *InitCPUSubType});
349 for (
auto &[CPUType, CPUSubType] : ArchsToTry) {
350 for (
auto &O : MachOUni->objects()) {
351 if (O.getCPUType() == CPUType &&
353 if (
auto Obj = O.getAsObjectFile())
354 MachOFile = std::move(*Obj);
356 return Obj.takeError();
365 "MachO universal binary at " + Path +
366 " does not contain a compatible slice for " +
378 for (
auto &Sym : MachOFile->symbols()) {
379 if (
auto Name = Sym.getName())
380 Symbols.insert(ES.
intern(*Name));
382 return Name.takeError();
385 return std::move(Symbols);
400 return Tapi.takeError();
404 return InitCPUType.takeError();
408 return InitCPUSubType.takeError();
411 if (GetFallbackArchs)
412 ArchsToTry = GetFallbackArchs(*InitCPUType, *InitCPUSubType);
414 ArchsToTry.
push_back({*InitCPUType, *InitCPUSubType});
416 auto &IF = (*Tapi)->getInterfaceFile();
418 auto ArchSet = IF.getArchitectures();
419 for (
auto [CPUType, CPUSubType] : ArchsToTry) {
421 if (ArchSet.has(
A)) {
422 if (
auto Interface = IF.extract(
A)) {
423 for (
auto *Sym : (*Interface)->exports())
424 Symbols.insert(ES.
intern(Sym->getName()));
427 return Interface.takeError();
432 "MachO interface file at " + Path +
433 " does not contain a compatible slice for " +
452 " 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
static LLVM_ABI StringRef getArchTypeName(ArchType Kind)
Get the canonical name for the Kind architecture.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
StringRef getFileName() const
Triple::ArchType getArch() const override
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.
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
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.