1//===----------------- MachO.cpp - MachO format utilities -----------------===// 
  3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 
  4// See https://llvm.org/LICENSE.txt for license information. 
  5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 
  7//===----------------------------------------------------------------------===// 
  18 #define DEBUG_TYPE "orc" 
  27 Desc += (TT.getArchName() + 
" slice of universal binary").str();
 
 
  32template <
typename HeaderType>
 
  34 bool SwapEndianness, 
const Triple &TT,
 
  39 memcpy(&Hdr, 
Data.data(), 
sizeof(HeaderType));
 
  46 " is not a MachO relocatable object",
 
  50 if (ObjArch != TT.getArch())
 
  53 ", cannot be loaded into " + TT.str() + 
" process",
 
 
  66 " is not a valid MachO relocatable object file (truncated header)",
 
  84 " is not a valid MachO relocatable object (bad magic value)",
 
 
  94 return std::move(Err);
 
  95 return std::move(Obj);
 
 
  100 std::optional<StringRef> IdentifierOverride) {
 
  103 "TT must specify MachO or Unknown object format");
 
  105 if (!IdentifierOverride)
 
  106 IdentifierOverride = Path;
 
  119 StringRef(
"Could not load MachO object at path ") + Path,
 
  126 return CheckedObj.takeError();
 
  127 return std::make_pair(std::move(*CheckedObj),
 
  133 *IdentifierOverride);
 
  136 Path + 
" does not contain a relocatable object file compatible with " +
 
 
  144 std::unique_ptr<MemoryBuffer> UBBuf,
 
  152 return UniversalBin.takeError();
 
  156 return SliceRange.takeError();
 
  162 "Could not load " + TT.getArchName() +
 
  163 " slice of MachO universal binary at path " + UBPath,
 
  175 return CheckedObj.takeError();
 
  176 return std::make_pair(std::move(*CheckedObj),
 
  188 return "a mach-o relocatable object file";
 
  190 return "a mach-o relocatable object file or archive";
 
  198 " does not contain " + FT(),
 
 
  206 for (
const auto &Obj : UB.
objects()) {
 
  207 auto ObjTT = Obj.getTriple();
 
  208 if (ObjTT.getArch() == TT.getArch() &&
 
  209 ObjTT.getSubArch() == TT.getSubArch() &&
 
  211 ObjTT.getVendor() == TT.getVendor())) {
 
  212 // We found a match. Return the range for the slice. 
  213 return std::make_pair(Obj.getOffset(), Obj.getSize());
 
  218 " does not contain a slice for " +
 
 
  228 return UB.takeError();
 
 
  236 auto LoadMember = [&]() {
 
  242 // If we're loading all files then just load the buffer immediately. Return 
  243 // false to indicate that there's no further loading to do here. 
  244 if (
auto Err = L.add(JD, LoadMember()))
 
  249 // We need to check whether this archive member contains any Objective-C 
  250 // or Swift metadata. 
  253 // Invalid files are not loadable, but don't invalidate the archive. 
  259 // Load the object if any recognized special section is present. 
  260 for (
auto Sec : MachOObj->sections()) {
 
  262 MachOObj->getSectionFinalSegmentName(Sec.getRawDataRefImpl());
 
  263 if (
auto SecName = Sec.getName()) {
 
  264 if (*SecName == 
"__objc_classlist" || *SecName == 
"__objc_protolist" ||
 
  265 *SecName == 
"__objc_clsrolist" || *SecName == 
"__objc_catlist" ||
 
  266 *SecName == 
"__objc_catlist2" || *SecName == 
"__objc_nlcatlist" ||
 
  267 (SegName == 
"__TEXT" && (*SecName).starts_with(
"__swift") &&
 
  268 *SecName != 
"__swift_modhash")) {
 
  269 if (
auto Err = L.add(JD, LoadMember()))
 
  274 return SecName.takeError();
 
  278 // This is an object file but we didn't load it, so return true to indicate 
  279 // that it's still loadable. 
 
  283} 
// End namespace orc. 
  284} 
// End namespace llvm. 
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.
 
StringRef getBufferIdentifier() const
 
StringRef getBuffer() const
 
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.
 
StringRef - Represent a constant reference to a string, i.e.
 
Triple - Helper class for working with autoconf configuration names.
 
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)
 
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< 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.
 
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.
 
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...
 
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.
 
@ archive
ar style archive file
 
@ macho_universal_binary
Mach-O universal binary.
 
@ macho_object
Mach-O Object file.