LLVM 22.0.0git
MachOObjectFile.cpp File Reference
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/ADT/Twine.h"
#include "llvm/ADT/bit.h"
#include "llvm/BinaryFormat/MachO.h"
#include "llvm/BinaryFormat/Swift.h"
#include "llvm/Object/Error.h"
#include "llvm/Object/MachO.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Object/SymbolicFile.h"
#include "llvm/Support/DataExtractor.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/LEB128.h"
#include "llvm/Support/MemoryBufferRef.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/SwapByteOrder.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TargetParser/Host.h"
#include "llvm/TargetParser/Triple.h"
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <limits>
#include <list>
#include <memory>
#include <system_error>
#include "llvm/BinaryFormat/Swift.def"

Go to the source code of this file.

Classes

struct  MachOElement

Macros

#define HANDLE_SWIFT_SECTION(KIND, MACHO, ELF, COFF)

Functions

static Error malformedError (const Twine &Msg)
template<typename T>
static T getStruct (const MachOObjectFile &O, const char *P)
template<typename T>
static Expected< TgetStructOrErr (const MachOObjectFile &O, const char *P)
static const chargetSectionPtr (const MachOObjectFile &O, MachOObjectFile::LoadCommandInfo L, unsigned Sec)
static const chargetPtr (const MachOObjectFile &O, size_t Offset, size_t MachOFilesetEntryOffset=0)
static MachO::nlist_base getSymbolTableEntryBase (const MachOObjectFile &O, DataRefImpl DRI)
static StringRef parseSegmentOrSectionName (const char *P)
static unsigned getCPUType (const MachOObjectFile &O)
static unsigned getCPUSubType (const MachOObjectFile &O)
static uint32_t getPlainRelocationAddress (const MachO::any_relocation_info &RE)
static unsigned getScatteredRelocationAddress (const MachO::any_relocation_info &RE)
static bool getPlainRelocationPCRel (const MachOObjectFile &O, const MachO::any_relocation_info &RE)
static bool getScatteredRelocationPCRel (const MachO::any_relocation_info &RE)
static unsigned getPlainRelocationLength (const MachOObjectFile &O, const MachO::any_relocation_info &RE)
static unsigned getScatteredRelocationLength (const MachO::any_relocation_info &RE)
static unsigned getPlainRelocationType (const MachOObjectFile &O, const MachO::any_relocation_info &RE)
static uint32_t getSectionFlags (const MachOObjectFile &O, DataRefImpl Sec)
static Expected< MachOObjectFile::LoadCommandInfogetLoadCommandInfo (const MachOObjectFile &Obj, const char *Ptr, uint32_t LoadCommandIndex)
static Expected< MachOObjectFile::LoadCommandInfogetFirstLoadCommandInfo (const MachOObjectFile &Obj)
static Expected< MachOObjectFile::LoadCommandInfogetNextLoadCommandInfo (const MachOObjectFile &Obj, uint32_t LoadCommandIndex, const MachOObjectFile::LoadCommandInfo &L)
template<typename T>
static void parseHeader (const MachOObjectFile &Obj, T &Header, Error &Err)
static Error checkOverlappingElement (std::list< MachOElement > &Elements, uint64_t Offset, uint64_t Size, const char *Name)
template<typename Segment, typename Section>
static Error parseSegmentLoadCommand (const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, SmallVectorImpl< const char * > &Sections, bool &IsPageZeroSegment, uint32_t LoadCommandIndex, const char *CmdName, uint64_t SizeOfHeaders, std::list< MachOElement > &Elements)
static Error checkSymtabCommand (const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, const char **SymtabLoadCmd, std::list< MachOElement > &Elements)
static Error checkDysymtabCommand (const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, const char **DysymtabLoadCmd, std::list< MachOElement > &Elements)
static Error checkLinkeditDataCommand (const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, const char **LoadCmd, const char *CmdName, std::list< MachOElement > &Elements, const char *ElementName)
static Error checkDyldInfoCommand (const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, const char **LoadCmd, const char *CmdName, std::list< MachOElement > &Elements)
static Error checkDylibCommand (const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, const char *CmdName)
static Error checkDylibIdCommand (const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, const char **LoadCmd)
static Error checkDyldCommand (const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, const char *CmdName)
static Error checkVersCommand (const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, const char **LoadCmd, const char *CmdName)
static Error checkNoteCommand (const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, std::list< MachOElement > &Elements)
static Error parseBuildVersionCommand (const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, SmallVectorImpl< const char * > &BuildTools, uint32_t LoadCommandIndex)
static Error checkRpathCommand (const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex)
static Error checkEncryptCommand (const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, uint64_t cryptoff, uint64_t cryptsize, const char **LoadCmd, const char *CmdName)
static Error checkLinkerOptCommand (const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex)
static Error checkSubCommand (const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, const char *CmdName, size_t SizeOfCmd, const char *CmdStructName, uint32_t PathOffset, const char *PathFieldName)
static Error checkThreadCommand (const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, const char *CmdName)
static Error checkTwoLevelHintsCommand (const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load, uint32_t LoadCommandIndex, const char **LoadCmd, std::list< MachOElement > &Elements)
static bool isLoadCommandObsolete (uint32_t cmd)
template<typename T>
static int getEncodedOrdinal (T Value)
template<typename T, unsigned N>
static std::array< T, NgetArray (const MachOObjectFile &O, const void *Ptr)

Macro Definition Documentation

◆ HANDLE_SWIFT_SECTION

#define HANDLE_SWIFT_SECTION ( KIND,
MACHO,
ELF,
COFF )
Value:
.Case(MACHO, llvm::binaryformat::Swift5ReflectionSectionKind::KIND)

Function Documentation

◆ checkDyldCommand()

Error checkDyldCommand ( const MachOObjectFile & Obj,
const MachOObjectFile::LoadCommandInfo & Load,
uint32_t LoadCommandIndex,
const char * CmdName )
static

Definition at line 787 of file MachOObjectFile.cpp.

References D(), getStructOrErr(), malformedError(), P, and llvm::Error::success().

◆ checkDyldInfoCommand()

◆ checkDylibCommand()

Error checkDylibCommand ( const MachOObjectFile & Obj,
const MachOObjectFile::LoadCommandInfo & Load,
uint32_t LoadCommandIndex,
const char * CmdName )
static

Definition at line 738 of file MachOObjectFile.cpp.

References D(), getStructOrErr(), malformedError(), P, and llvm::Error::success().

Referenced by checkDylibIdCommand().

◆ checkDylibIdCommand()

◆ checkDysymtabCommand()

◆ checkEncryptCommand()

Error checkEncryptCommand ( const MachOObjectFile & Obj,
const MachOObjectFile::LoadCommandInfo & Load,
uint32_t LoadCommandIndex,
uint64_t cryptoff,
uint64_t cryptsize,
const char ** LoadCmd,
const char * CmdName )
static

◆ checkLinkeditDataCommand()

◆ checkLinkerOptCommand()

Error checkLinkerOptCommand ( const MachOObjectFile & Obj,
const MachOObjectFile::LoadCommandInfo & Load,
uint32_t LoadCommandIndex )
static

◆ checkNoteCommand()

◆ checkOverlappingElement()

◆ checkRpathCommand()

Error checkRpathCommand ( const MachOObjectFile & Obj,
const MachOObjectFile::LoadCommandInfo & Load,
uint32_t LoadCommandIndex )
static

Definition at line 886 of file MachOObjectFile.cpp.

References getStructOrErr(), malformedError(), P, and llvm::Error::success().

◆ checkSubCommand()

Error checkSubCommand ( const MachOObjectFile & Obj,
const MachOObjectFile::LoadCommandInfo & Load,
uint32_t LoadCommandIndex,
const char * CmdName,
size_t SizeOfCmd,
const char * CmdStructName,
uint32_t PathOffset,
const char * PathFieldName )
static

Definition at line 982 of file MachOObjectFile.cpp.

References malformedError(), P, and llvm::Error::success().

◆ checkSymtabCommand()

◆ checkThreadCommand()

◆ checkTwoLevelHintsCommand()

◆ checkVersCommand()

Error checkVersCommand ( const MachOObjectFile & Obj,
const MachOObjectFile::LoadCommandInfo & Load,
uint32_t LoadCommandIndex,
const char ** LoadCmd,
const char * CmdName )
static

Definition at line 819 of file MachOObjectFile.cpp.

References malformedError(), and llvm::Error::success().

◆ getArray()

template<typename T, unsigned N>
std::array< T, N > getArray ( const MachOObjectFile & O,
const void * Ptr )
static

◆ getCPUSubType()

◆ getCPUType()

◆ getEncodedOrdinal()

◆ getFirstLoadCommandInfo()

◆ getLoadCommandInfo()

◆ getNextLoadCommandInfo()

◆ getPlainRelocationAddress()

◆ getPlainRelocationLength()

◆ getPlainRelocationPCRel()

◆ getPlainRelocationType()

◆ getPtr()

◆ getScatteredRelocationAddress()

unsigned getScatteredRelocationAddress ( const MachO::any_relocation_info & RE)
static

◆ getScatteredRelocationLength()

unsigned getScatteredRelocationLength ( const MachO::any_relocation_info & RE)
static

◆ getScatteredRelocationPCRel()

bool getScatteredRelocationPCRel ( const MachO::any_relocation_info & RE)
static

◆ getSectionFlags()

◆ getSectionPtr()

◆ getStruct()

template<typename T>
T getStruct ( const MachOObjectFile & O,
const char * P )
static

Definition at line 71 of file MachOObjectFile.cpp.

References llvm::sys::IsLittleEndianHost, P, llvm::report_fatal_error(), llvm::MachO::swapStruct(), and T.

Referenced by llvm::object::MachOObjectFile::getBuildToolVersion(), llvm::object::MachOObjectFile::getBuildVersionLoadCommand(), llvm::object::MachOObjectFile::getDataInCodeLoadCommand(), llvm::object::MachOObjectFile::getDataInCodeTableEntry(), llvm::object::MachOObjectFile::getDice(), llvm::object::MachOObjectFile::getDyldInfoLoadCommand(), llvm::object::MachOObjectFile::getDylibIDLoadCommand(), llvm::object::MachOObjectFile::getDylinkerCommand(), llvm::object::MachOObjectFile::getDysymtabLoadCommand(), llvm::object::MachOObjectFile::getEncryptionInfoCommand(), llvm::object::MachOObjectFile::getEncryptionInfoCommand64(), llvm::object::MachOObjectFile::getEntryPointCommand(), llvm::object::MachOObjectFile::getFilesetEntryLoadCommand(), llvm::object::MachOObjectFile::getIndirectSymbolTableEntry(), llvm::object::MachOObjectFile::getLinkeditDataLoadCommand(), llvm::object::MachOObjectFile::getLinkerOptionLoadCommand(), llvm::object::MachOObjectFile::getLinkOptHintsLoadCommand(), llvm::object::MachOObjectFile::getNoteLoadCommand(), llvm::object::MachOObjectFile::getRelocation(), llvm::object::MachOObjectFile::getRoutinesCommand(), llvm::object::MachOObjectFile::getRoutinesCommand64(), llvm::object::MachOObjectFile::getRpathCommand(), llvm::object::MachOObjectFile::getSection(), llvm::object::MachOObjectFile::getSection(), llvm::object::MachOObjectFile::getSection64(), llvm::object::MachOObjectFile::getSection64(), llvm::object::MachOObjectFile::getSegment64LoadCommand(), llvm::object::MachOObjectFile::getSegmentLoadCommand(), llvm::object::MachOObjectFile::getSourceVersionCommand(), llvm::object::MachOObjectFile::getSubClientCommand(), llvm::object::MachOObjectFile::getSubFrameworkCommand(), llvm::object::MachOObjectFile::getSubLibraryCommand(), llvm::object::MachOObjectFile::getSubUmbrellaCommand(), llvm::object::MachOObjectFile::getSymbol64TableEntry(), llvm::object::MachOObjectFile::getSymbolTableEntry(), getSymbolTableEntryBase(), llvm::object::MachOObjectFile::getSymtabLoadCommand(), llvm::object::MachOObjectFile::getThreadCommand(), llvm::object::MachOObjectFile::getUuidCommand(), and llvm::object::MachOObjectFile::getVersionMinLoadCommand().

◆ getStructOrErr()

◆ getSymbolTableEntryBase()

◆ isLoadCommandObsolete()

bool isLoadCommandObsolete ( uint32_t cmd)
static

Definition at line 1239 of file MachOObjectFile.cpp.

References llvm::MachO::linker_option_command::cmd.

◆ malformedError()

◆ parseBuildVersionCommand()

◆ parseHeader()

template<typename T>
void parseHeader ( const MachOObjectFile & Obj,
T & Header,
Error & Err )
static

◆ parseSegmentLoadCommand()

◆ parseSegmentOrSectionName()

StringRef parseSegmentOrSectionName ( const char * P)
static