2013-04-04 02:31:38 +08:00
|
|
|
//===-- MachODump.cpp - Object file dumping utility for llvm --------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the MachO-specific dumper for llvm-readobj.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Error.h"
|
|
|
|
#include "ObjDumper.h"
|
2015-06-27 07:56:53 +08:00
|
|
|
#include "StackMapPrinter.h"
|
2016-05-03 08:28:04 +08:00
|
|
|
#include "llvm-readobj.h"
|
2013-04-04 02:31:38 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2014-07-04 18:57:56 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2013-04-04 02:31:38 +08:00
|
|
|
#include "llvm/Object/MachO.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
2016-05-03 08:28:04 +08:00
|
|
|
#include "llvm/Support/ScopedPrinter.h"
|
2013-04-04 02:31:38 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace object;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class MachODumper : public ObjDumper {
|
|
|
|
public:
|
2016-05-03 08:28:04 +08:00
|
|
|
MachODumper(const MachOObjectFile *Obj, ScopedPrinter &Writer)
|
|
|
|
: ObjDumper(Writer), Obj(Obj) {}
|
2013-04-04 02:31:38 +08:00
|
|
|
|
2014-08-31 00:48:34 +08:00
|
|
|
void printFileHeaders() override;
|
|
|
|
void printSections() override;
|
|
|
|
void printRelocations() override;
|
|
|
|
void printSymbols() override;
|
|
|
|
void printDynamicSymbols() override;
|
|
|
|
void printUnwindInfo() override;
|
2015-06-27 07:56:53 +08:00
|
|
|
void printStackMap() const override;
|
2013-04-04 02:31:38 +08:00
|
|
|
|
2018-01-08 10:23:10 +08:00
|
|
|
void printNeededLibraries() override;
|
|
|
|
|
2015-08-22 04:28:30 +08:00
|
|
|
// MachO-specific.
|
|
|
|
void printMachODataInCode() override;
|
2015-08-27 23:11:32 +08:00
|
|
|
void printMachOVersionMin() override;
|
2015-09-01 03:32:31 +08:00
|
|
|
void printMachODysymtab() override;
|
2015-09-03 00:24:24 +08:00
|
|
|
void printMachOSegment() override;
|
2015-09-04 02:10:28 +08:00
|
|
|
void printMachOIndirectSymbols() override;
|
2015-09-09 08:21:18 +08:00
|
|
|
void printMachOLinkerOptions () override;
|
2015-08-22 04:28:30 +08:00
|
|
|
|
2013-04-04 02:31:38 +08:00
|
|
|
private:
|
2014-11-17 09:34:15 +08:00
|
|
|
template<class MachHeader>
|
|
|
|
void printFileHeaders(const MachHeader &Header);
|
|
|
|
|
2014-03-17 15:28:19 +08:00
|
|
|
void printSymbol(const SymbolRef &Symbol);
|
2013-04-04 02:31:38 +08:00
|
|
|
|
2014-03-14 22:22:49 +08:00
|
|
|
void printRelocation(const RelocationRef &Reloc);
|
2013-04-04 02:31:38 +08:00
|
|
|
|
2014-03-14 22:22:49 +08:00
|
|
|
void printRelocation(const MachOObjectFile *Obj, const RelocationRef &Reloc);
|
2013-04-13 09:45:40 +08:00
|
|
|
|
2013-04-19 02:08:55 +08:00
|
|
|
void printSections(const MachOObjectFile *Obj);
|
2013-04-13 09:45:40 +08:00
|
|
|
|
2013-04-19 02:08:55 +08:00
|
|
|
const MachOObjectFile *Obj;
|
2013-04-04 02:31:38 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2014-06-13 11:07:50 +08:00
|
|
|
std::error_code createMachODumper(const object::ObjectFile *Obj,
|
2016-05-03 08:28:04 +08:00
|
|
|
ScopedPrinter &Writer,
|
2014-06-13 11:07:50 +08:00
|
|
|
std::unique_ptr<ObjDumper> &Result) {
|
2013-04-19 02:08:55 +08:00
|
|
|
const MachOObjectFile *MachOObj = dyn_cast<MachOObjectFile>(Obj);
|
2013-04-04 02:31:38 +08:00
|
|
|
if (!MachOObj)
|
|
|
|
return readobj_error::unsupported_obj_file_format;
|
|
|
|
|
|
|
|
Result.reset(new MachODumper(MachOObj, Writer));
|
|
|
|
return readobj_error::success;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llvm
|
|
|
|
|
2014-11-17 09:34:15 +08:00
|
|
|
static const EnumEntry<uint32_t> MachOMagics[] = {
|
|
|
|
{ "Magic", MachO::MH_MAGIC },
|
|
|
|
{ "Cigam", MachO::MH_CIGAM },
|
|
|
|
{ "Magic64", MachO::MH_MAGIC_64 },
|
|
|
|
{ "Cigam64", MachO::MH_CIGAM_64 },
|
|
|
|
{ "FatMagic", MachO::FAT_MAGIC },
|
|
|
|
{ "FatCigam", MachO::FAT_CIGAM },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const EnumEntry<uint32_t> MachOHeaderFileTypes[] = {
|
|
|
|
{ "Relocatable", MachO::MH_OBJECT },
|
|
|
|
{ "Executable", MachO::MH_EXECUTE },
|
|
|
|
{ "FixedVMLibrary", MachO::MH_FVMLIB },
|
|
|
|
{ "Core", MachO::MH_CORE },
|
|
|
|
{ "PreloadedExecutable", MachO::MH_PRELOAD },
|
|
|
|
{ "DynamicLibrary", MachO::MH_DYLIB },
|
|
|
|
{ "DynamicLinker", MachO::MH_DYLINKER },
|
|
|
|
{ "Bundle", MachO::MH_BUNDLE },
|
|
|
|
{ "DynamicLibraryStub", MachO::MH_DYLIB_STUB },
|
|
|
|
{ "DWARFSymbol", MachO::MH_DSYM },
|
|
|
|
{ "KextBundle", MachO::MH_KEXT_BUNDLE },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const EnumEntry<uint32_t> MachOHeaderCpuTypes[] = {
|
|
|
|
{ "Any" , static_cast<uint32_t>(MachO::CPU_TYPE_ANY) },
|
|
|
|
{ "X86" , MachO::CPU_TYPE_X86 },
|
|
|
|
{ "X86-64" , MachO::CPU_TYPE_X86_64 },
|
|
|
|
{ "Mc98000" , MachO::CPU_TYPE_MC98000 },
|
|
|
|
{ "Arm" , MachO::CPU_TYPE_ARM },
|
|
|
|
{ "Arm64" , MachO::CPU_TYPE_ARM64 },
|
|
|
|
{ "Sparc" , MachO::CPU_TYPE_SPARC },
|
|
|
|
{ "PowerPC" , MachO::CPU_TYPE_POWERPC },
|
|
|
|
{ "PowerPC64" , MachO::CPU_TYPE_POWERPC64 },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesX86[] = {
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_I386_ALL),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_386),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_486),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_486SX),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_586),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTPRO),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTII_M3),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTII_M5),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_CELERON),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_CELERON_MOBILE),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_3),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_3_M),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_3_XEON),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_M),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_4),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_4_M),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ITANIUM),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ITANIUM_2),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_XEON),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_XEON_MP),
|
|
|
|
};
|
|
|
|
|
|
|
|
static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesX64[] = {
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_X86_64_ALL),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_X86_ARCH1),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_X86_64_H),
|
|
|
|
};
|
|
|
|
|
|
|
|
static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesARM[] = {
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_ALL),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V4T),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V6),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V5),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V5TEJ),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_XSCALE),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7S),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7K),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V6M),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7M),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7EM),
|
|
|
|
};
|
|
|
|
|
|
|
|
static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesARM64[] = {
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM64_ALL),
|
|
|
|
};
|
|
|
|
|
|
|
|
static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesSPARC[] = {
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_SPARC_ALL),
|
|
|
|
};
|
|
|
|
|
|
|
|
static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesPPC[] = {
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_ALL),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_601),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_602),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_603),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_603e),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_603ev),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_604),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_604e),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_620),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_750),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_7400),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_7450),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_970),
|
|
|
|
};
|
|
|
|
|
|
|
|
static const EnumEntry<uint32_t> MachOHeaderFlags[] = {
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_NOUNDEFS),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_INCRLINK),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_DYLDLINK),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_BINDATLOAD),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_PREBOUND),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_SPLIT_SEGS),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_LAZY_INIT),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_TWOLEVEL),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_FORCE_FLAT),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_NOMULTIDEFS),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_NOFIXPREBINDING),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_PREBINDABLE),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_ALLMODSBOUND),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_SUBSECTIONS_VIA_SYMBOLS),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_CANONICAL),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_WEAK_DEFINES),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_BINDS_TO_WEAK),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_ALLOW_STACK_EXECUTION),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_ROOT_SAFE),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_SETUID_SAFE),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_NO_REEXPORTED_DYLIBS),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_PIE),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_DEAD_STRIPPABLE_DYLIB),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_HAS_TLV_DESCRIPTORS),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_NO_HEAP_EXECUTION),
|
|
|
|
LLVM_READOBJ_ENUM_ENT(MachO, MH_APP_EXTENSION_SAFE),
|
|
|
|
};
|
2013-04-04 02:31:38 +08:00
|
|
|
|
|
|
|
static const EnumEntry<unsigned> MachOSectionAttributes[] = {
|
|
|
|
{ "LocReloc" , 1 << 0 /*S_ATTR_LOC_RELOC */ },
|
|
|
|
{ "ExtReloc" , 1 << 1 /*S_ATTR_EXT_RELOC */ },
|
|
|
|
{ "SomeInstructions" , 1 << 2 /*S_ATTR_SOME_INSTRUCTIONS */ },
|
|
|
|
{ "Debug" , 1 << 17 /*S_ATTR_DEBUG */ },
|
|
|
|
{ "SelfModifyingCode", 1 << 18 /*S_ATTR_SELF_MODIFYING_CODE*/ },
|
|
|
|
{ "LiveSupport" , 1 << 19 /*S_ATTR_LIVE_SUPPORT */ },
|
|
|
|
{ "NoDeadStrip" , 1 << 20 /*S_ATTR_NO_DEAD_STRIP */ },
|
|
|
|
{ "StripStaticSyms" , 1 << 21 /*S_ATTR_STRIP_STATIC_SYMS */ },
|
|
|
|
{ "NoTOC" , 1 << 22 /*S_ATTR_NO_TOC */ },
|
|
|
|
{ "PureInstructions" , 1 << 23 /*S_ATTR_PURE_INSTRUCTIONS */ },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const EnumEntry<unsigned> MachOSymbolRefTypes[] = {
|
|
|
|
{ "UndefinedNonLazy", 0 },
|
|
|
|
{ "ReferenceFlagUndefinedLazy", 1 },
|
|
|
|
{ "ReferenceFlagDefined", 2 },
|
|
|
|
{ "ReferenceFlagPrivateDefined", 3 },
|
|
|
|
{ "ReferenceFlagPrivateUndefinedNonLazy", 4 },
|
|
|
|
{ "ReferenceFlagPrivateUndefinedLazy", 5 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const EnumEntry<unsigned> MachOSymbolFlags[] = {
|
|
|
|
{ "ReferencedDynamically", 0x10 },
|
|
|
|
{ "NoDeadStrip", 0x20 },
|
|
|
|
{ "WeakRef", 0x40 },
|
2016-03-15 09:43:05 +08:00
|
|
|
{ "WeakDef", 0x80 },
|
|
|
|
{ "AltEntry", 0x200 },
|
2013-04-04 02:31:38 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static const EnumEntry<unsigned> MachOSymbolTypes[] = {
|
|
|
|
{ "Undef", 0x0 },
|
|
|
|
{ "Abs", 0x2 },
|
|
|
|
{ "Indirect", 0xA },
|
|
|
|
{ "PreboundUndef", 0xC },
|
2014-03-07 04:13:41 +08:00
|
|
|
{ "Section", 0xE }
|
2013-04-04 02:31:38 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct MachOSection {
|
|
|
|
ArrayRef<char> Name;
|
|
|
|
ArrayRef<char> SegmentName;
|
|
|
|
uint64_t Address;
|
|
|
|
uint64_t Size;
|
|
|
|
uint32_t Offset;
|
|
|
|
uint32_t Alignment;
|
|
|
|
uint32_t RelocationTableOffset;
|
|
|
|
uint32_t NumRelocationTableEntries;
|
|
|
|
uint32_t Flags;
|
|
|
|
uint32_t Reserved1;
|
|
|
|
uint32_t Reserved2;
|
2015-09-02 00:29:02 +08:00
|
|
|
uint32_t Reserved3;
|
2013-04-04 02:31:38 +08:00
|
|
|
};
|
|
|
|
|
2015-09-03 00:24:24 +08:00
|
|
|
struct MachOSegment {
|
2015-09-22 10:14:43 +08:00
|
|
|
std::string CmdName;
|
|
|
|
std::string SegName;
|
2015-09-03 00:24:24 +08:00
|
|
|
uint64_t cmdsize;
|
|
|
|
uint64_t vmaddr;
|
|
|
|
uint64_t vmsize;
|
|
|
|
uint64_t fileoff;
|
|
|
|
uint64_t filesize;
|
|
|
|
uint32_t maxprot;
|
|
|
|
uint32_t initprot;
|
|
|
|
uint32_t nsects;
|
|
|
|
uint32_t flags;
|
|
|
|
};
|
|
|
|
|
2013-04-04 02:31:38 +08:00
|
|
|
struct MachOSymbol {
|
|
|
|
uint32_t StringIndex;
|
|
|
|
uint8_t Type;
|
|
|
|
uint8_t SectionIndex;
|
|
|
|
uint16_t Flags;
|
|
|
|
uint64_t Value;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-09-03 00:24:24 +08:00
|
|
|
static std::string getMask(uint32_t prot)
|
|
|
|
{
|
|
|
|
// TODO (davide): This always assumes prot is valid.
|
|
|
|
// Catch mistakes and report if needed.
|
|
|
|
std::string Prot;
|
|
|
|
Prot = "";
|
|
|
|
Prot += (prot & MachO::VM_PROT_READ) ? "r" : "-";
|
|
|
|
Prot += (prot & MachO::VM_PROT_WRITE) ? "w" : "-";
|
|
|
|
Prot += (prot & MachO::VM_PROT_EXECUTE) ? "x" : "-";
|
|
|
|
return Prot;
|
|
|
|
}
|
|
|
|
|
2013-04-19 02:08:55 +08:00
|
|
|
static void getSection(const MachOObjectFile *Obj,
|
|
|
|
DataRefImpl Sec,
|
2013-04-13 09:45:40 +08:00
|
|
|
MachOSection &Section) {
|
2013-04-19 02:08:55 +08:00
|
|
|
if (!Obj->is64Bit()) {
|
2013-09-01 12:28:48 +08:00
|
|
|
MachO::section Sect = Obj->getSection(Sec);
|
|
|
|
Section.Address = Sect.addr;
|
|
|
|
Section.Size = Sect.size;
|
|
|
|
Section.Offset = Sect.offset;
|
|
|
|
Section.Alignment = Sect.align;
|
|
|
|
Section.RelocationTableOffset = Sect.reloff;
|
|
|
|
Section.NumRelocationTableEntries = Sect.nreloc;
|
|
|
|
Section.Flags = Sect.flags;
|
|
|
|
Section.Reserved1 = Sect.reserved1;
|
|
|
|
Section.Reserved2 = Sect.reserved2;
|
2013-04-19 02:08:55 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-09-01 12:28:48 +08:00
|
|
|
MachO::section_64 Sect = Obj->getSection64(Sec);
|
|
|
|
Section.Address = Sect.addr;
|
|
|
|
Section.Size = Sect.size;
|
|
|
|
Section.Offset = Sect.offset;
|
|
|
|
Section.Alignment = Sect.align;
|
|
|
|
Section.RelocationTableOffset = Sect.reloff;
|
|
|
|
Section.NumRelocationTableEntries = Sect.nreloc;
|
|
|
|
Section.Flags = Sect.flags;
|
|
|
|
Section.Reserved1 = Sect.reserved1;
|
|
|
|
Section.Reserved2 = Sect.reserved2;
|
2015-09-02 00:29:02 +08:00
|
|
|
Section.Reserved3 = Sect.reserved3;
|
2013-04-13 09:45:40 +08:00
|
|
|
}
|
|
|
|
|
2015-09-03 00:24:24 +08:00
|
|
|
static void getSegment(const MachOObjectFile *Obj,
|
|
|
|
const MachOObjectFile::LoadCommandInfo &L,
|
|
|
|
MachOSegment &Segment) {
|
|
|
|
if (!Obj->is64Bit()) {
|
|
|
|
MachO::segment_command SC = Obj->getSegmentLoadCommand(L);
|
|
|
|
Segment.CmdName = "LC_SEGMENT";
|
|
|
|
Segment.SegName = SC.segname;
|
|
|
|
Segment.cmdsize = SC.cmdsize;
|
|
|
|
Segment.vmaddr = SC.vmaddr;
|
|
|
|
Segment.vmsize = SC.vmsize;
|
|
|
|
Segment.fileoff = SC.fileoff;
|
|
|
|
Segment.filesize = SC.filesize;
|
|
|
|
Segment.maxprot = SC.maxprot;
|
|
|
|
Segment.initprot = SC.initprot;
|
|
|
|
Segment.nsects = SC.nsects;
|
|
|
|
Segment.flags = SC.flags;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MachO::segment_command_64 SC = Obj->getSegment64LoadCommand(L);
|
|
|
|
Segment.CmdName = "LC_SEGMENT_64";
|
|
|
|
Segment.SegName = SC.segname;
|
|
|
|
Segment.cmdsize = SC.cmdsize;
|
|
|
|
Segment.vmaddr = SC.vmaddr;
|
|
|
|
Segment.vmsize = SC.vmsize;
|
|
|
|
Segment.fileoff = SC.fileoff;
|
|
|
|
Segment.filesize = SC.filesize;
|
|
|
|
Segment.maxprot = SC.maxprot;
|
|
|
|
Segment.initprot = SC.initprot;
|
|
|
|
Segment.nsects = SC.nsects;
|
|
|
|
Segment.flags = SC.flags;
|
|
|
|
}
|
2013-04-04 02:31:38 +08:00
|
|
|
|
2013-04-19 02:08:55 +08:00
|
|
|
static void getSymbol(const MachOObjectFile *Obj,
|
2013-04-04 02:31:38 +08:00
|
|
|
DataRefImpl DRI,
|
|
|
|
MachOSymbol &Symbol) {
|
2013-04-19 02:08:55 +08:00
|
|
|
if (!Obj->is64Bit()) {
|
2013-09-01 12:28:48 +08:00
|
|
|
MachO::nlist Entry = Obj->getSymbolTableEntry(DRI);
|
|
|
|
Symbol.StringIndex = Entry.n_strx;
|
|
|
|
Symbol.Type = Entry.n_type;
|
|
|
|
Symbol.SectionIndex = Entry.n_sect;
|
|
|
|
Symbol.Flags = Entry.n_desc;
|
|
|
|
Symbol.Value = Entry.n_value;
|
2013-04-19 02:08:55 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-09-01 12:28:48 +08:00
|
|
|
MachO::nlist_64 Entry = Obj->getSymbol64TableEntry(DRI);
|
|
|
|
Symbol.StringIndex = Entry.n_strx;
|
|
|
|
Symbol.Type = Entry.n_type;
|
|
|
|
Symbol.SectionIndex = Entry.n_sect;
|
|
|
|
Symbol.Flags = Entry.n_desc;
|
|
|
|
Symbol.Value = Entry.n_value;
|
2013-04-04 02:31:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MachODumper::printFileHeaders() {
|
2014-11-17 09:34:15 +08:00
|
|
|
DictScope H(W, "MachHeader");
|
|
|
|
if (!Obj->is64Bit()) {
|
|
|
|
printFileHeaders(Obj->getHeader());
|
|
|
|
} else {
|
|
|
|
printFileHeaders(Obj->getHeader64());
|
|
|
|
W.printHex("Reserved", Obj->getHeader64().reserved);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class MachHeader>
|
|
|
|
void MachODumper::printFileHeaders(const MachHeader &Header) {
|
|
|
|
W.printEnum("Magic", Header.magic, makeArrayRef(MachOMagics));
|
|
|
|
W.printEnum("CpuType", Header.cputype, makeArrayRef(MachOHeaderCpuTypes));
|
|
|
|
uint32_t subtype = Header.cpusubtype & ~MachO::CPU_SUBTYPE_MASK;
|
|
|
|
switch (Header.cputype) {
|
|
|
|
case MachO::CPU_TYPE_X86:
|
|
|
|
W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesX86));
|
|
|
|
break;
|
|
|
|
case MachO::CPU_TYPE_X86_64:
|
|
|
|
W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesX64));
|
|
|
|
break;
|
|
|
|
case MachO::CPU_TYPE_ARM:
|
|
|
|
W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesARM));
|
|
|
|
break;
|
|
|
|
case MachO::CPU_TYPE_POWERPC:
|
|
|
|
W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesPPC));
|
|
|
|
break;
|
|
|
|
case MachO::CPU_TYPE_SPARC:
|
|
|
|
W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesSPARC));
|
|
|
|
break;
|
|
|
|
case MachO::CPU_TYPE_ARM64:
|
|
|
|
W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesARM64));
|
|
|
|
break;
|
|
|
|
case MachO::CPU_TYPE_POWERPC64:
|
|
|
|
default:
|
|
|
|
W.printHex("CpuSubtype", subtype);
|
|
|
|
}
|
|
|
|
W.printEnum("FileType", Header.filetype, makeArrayRef(MachOHeaderFileTypes));
|
|
|
|
W.printNumber("NumOfLoadCommands", Header.ncmds);
|
|
|
|
W.printNumber("SizeOfLoadCommands", Header.sizeofcmds);
|
|
|
|
W.printFlags("Flags", Header.flags, makeArrayRef(MachOHeaderFlags));
|
2013-04-04 02:31:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MachODumper::printSections() {
|
2013-04-19 02:08:55 +08:00
|
|
|
return printSections(Obj);
|
2013-04-13 09:45:40 +08:00
|
|
|
}
|
|
|
|
|
2013-04-19 02:08:55 +08:00
|
|
|
void MachODumper::printSections(const MachOObjectFile *Obj) {
|
2013-04-04 02:31:38 +08:00
|
|
|
ListScope Group(W, "Sections");
|
|
|
|
|
|
|
|
int SectionIndex = -1;
|
2014-03-13 22:37:36 +08:00
|
|
|
for (const SectionRef &Section : Obj->sections()) {
|
2013-04-04 02:31:38 +08:00
|
|
|
++SectionIndex;
|
|
|
|
|
2014-03-13 22:37:36 +08:00
|
|
|
MachOSection MOSection;
|
|
|
|
getSection(Obj, Section.getRawDataRefImpl(), MOSection);
|
|
|
|
DataRefImpl DR = Section.getRawDataRefImpl();
|
2013-04-05 23:15:22 +08:00
|
|
|
|
2013-04-04 02:31:38 +08:00
|
|
|
StringRef Name;
|
2015-07-20 11:23:55 +08:00
|
|
|
error(Section.getName(Name));
|
2013-04-04 02:31:38 +08:00
|
|
|
|
2013-04-05 23:15:22 +08:00
|
|
|
ArrayRef<char> RawName = Obj->getSectionRawName(DR);
|
|
|
|
StringRef SegmentName = Obj->getSectionFinalSegmentName(DR);
|
|
|
|
ArrayRef<char> RawSegmentName = Obj->getSectionRawFinalSegmentName(DR);
|
|
|
|
|
2013-04-04 02:31:38 +08:00
|
|
|
DictScope SectionD(W, "Section");
|
|
|
|
W.printNumber("Index", SectionIndex);
|
2013-04-05 23:15:22 +08:00
|
|
|
W.printBinary("Name", Name, RawName);
|
|
|
|
W.printBinary("Segment", SegmentName, RawSegmentName);
|
2014-03-13 22:37:36 +08:00
|
|
|
W.printHex("Address", MOSection.Address);
|
|
|
|
W.printHex("Size", MOSection.Size);
|
|
|
|
W.printNumber("Offset", MOSection.Offset);
|
|
|
|
W.printNumber("Alignment", MOSection.Alignment);
|
|
|
|
W.printHex("RelocationOffset", MOSection.RelocationTableOffset);
|
|
|
|
W.printNumber("RelocationCount", MOSection.NumRelocationTableEntries);
|
|
|
|
W.printEnum("Type", MOSection.Flags & 0xFF,
|
|
|
|
makeArrayRef(MachOSectionAttributes));
|
|
|
|
W.printFlags("Attributes", MOSection.Flags >> 8,
|
|
|
|
makeArrayRef(MachOSectionAttributes));
|
|
|
|
W.printHex("Reserved1", MOSection.Reserved1);
|
|
|
|
W.printHex("Reserved2", MOSection.Reserved2);
|
2015-09-02 00:29:02 +08:00
|
|
|
if (Obj->is64Bit())
|
|
|
|
W.printHex("Reserved3", MOSection.Reserved3);
|
2013-04-04 02:31:38 +08:00
|
|
|
|
|
|
|
if (opts::SectionRelocations) {
|
|
|
|
ListScope D(W, "Relocations");
|
2014-03-14 22:22:49 +08:00
|
|
|
for (const RelocationRef &Reloc : Section.relocations())
|
|
|
|
printRelocation(Reloc);
|
2013-04-04 02:31:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opts::SectionSymbols) {
|
|
|
|
ListScope D(W, "Symbols");
|
2014-03-17 15:28:19 +08:00
|
|
|
for (const SymbolRef &Symbol : Obj->symbols()) {
|
2014-10-08 23:28:58 +08:00
|
|
|
if (!Section.containsSymbol(Symbol))
|
2013-04-04 02:31:38 +08:00
|
|
|
continue;
|
|
|
|
|
2014-03-17 15:28:19 +08:00
|
|
|
printSymbol(Symbol);
|
2013-04-04 02:31:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts::SectionData) {
|
2014-10-08 23:28:58 +08:00
|
|
|
bool IsBSS = Section.isBSS();
|
2014-09-27 06:32:16 +08:00
|
|
|
if (!IsBSS) {
|
|
|
|
StringRef Data;
|
2015-07-20 11:23:55 +08:00
|
|
|
error(Section.getContents(Data));
|
2013-04-04 02:31:38 +08:00
|
|
|
|
2014-09-27 06:32:16 +08:00
|
|
|
W.printBinaryBlock("SectionData", Data);
|
|
|
|
}
|
2013-04-04 02:31:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachODumper::printRelocations() {
|
|
|
|
ListScope D(W, "Relocations");
|
|
|
|
|
2014-06-13 11:07:50 +08:00
|
|
|
std::error_code EC;
|
2014-03-13 22:37:36 +08:00
|
|
|
for (const SectionRef &Section : Obj->sections()) {
|
2013-04-04 02:31:38 +08:00
|
|
|
StringRef Name;
|
2015-07-20 11:23:55 +08:00
|
|
|
error(Section.getName(Name));
|
2013-04-04 02:31:38 +08:00
|
|
|
|
|
|
|
bool PrintedGroup = false;
|
2014-03-14 22:22:49 +08:00
|
|
|
for (const RelocationRef &Reloc : Section.relocations()) {
|
2013-04-04 02:31:38 +08:00
|
|
|
if (!PrintedGroup) {
|
|
|
|
W.startLine() << "Section " << Name << " {\n";
|
|
|
|
W.indent();
|
|
|
|
PrintedGroup = true;
|
|
|
|
}
|
|
|
|
|
2014-03-14 22:22:49 +08:00
|
|
|
printRelocation(Reloc);
|
2013-04-04 02:31:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (PrintedGroup) {
|
|
|
|
W.unindent();
|
|
|
|
W.startLine() << "}\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-14 22:22:49 +08:00
|
|
|
void MachODumper::printRelocation(const RelocationRef &Reloc) {
|
|
|
|
return printRelocation(Obj, Reloc);
|
2013-04-13 09:45:40 +08:00
|
|
|
}
|
|
|
|
|
2013-04-19 02:08:55 +08:00
|
|
|
void MachODumper::printRelocation(const MachOObjectFile *Obj,
|
2014-03-14 22:22:49 +08:00
|
|
|
const RelocationRef &Reloc) {
|
2015-06-30 07:29:12 +08:00
|
|
|
uint64_t Offset = Reloc.getOffset();
|
2013-04-04 02:31:38 +08:00
|
|
|
SmallString<32> RelocName;
|
2015-06-30 12:08:37 +08:00
|
|
|
Reloc.getTypeName(RelocName);
|
2013-04-04 02:31:38 +08:00
|
|
|
|
2014-03-14 22:22:49 +08:00
|
|
|
DataRefImpl DR = Reloc.getRawDataRefImpl();
|
2013-09-01 12:28:48 +08:00
|
|
|
MachO::any_relocation_info RE = Obj->getRelocation(DR);
|
2013-04-13 09:45:40 +08:00
|
|
|
bool IsScattered = Obj->isRelocationScattered(RE);
|
2015-06-19 06:38:20 +08:00
|
|
|
bool IsExtern = !IsScattered && Obj->getPlainRelocationExternal(RE);
|
|
|
|
|
|
|
|
StringRef TargetName;
|
|
|
|
if (IsExtern) {
|
2014-07-04 18:57:56 +08:00
|
|
|
symbol_iterator Symbol = Reloc.getSymbol();
|
|
|
|
if (Symbol != Obj->symbol_end()) {
|
2016-04-21 05:24:34 +08:00
|
|
|
Expected<StringRef> TargetNameOrErr = Symbol->getName();
|
|
|
|
if (!TargetNameOrErr)
|
|
|
|
error(errorToErrorCode(TargetNameOrErr.takeError()));
|
2015-07-03 04:55:21 +08:00
|
|
|
TargetName = *TargetNameOrErr;
|
2015-06-19 06:38:20 +08:00
|
|
|
}
|
|
|
|
} else if (!IsScattered) {
|
2015-06-20 01:54:28 +08:00
|
|
|
section_iterator SecI = Obj->getRelocationSection(DR);
|
2015-06-19 06:38:20 +08:00
|
|
|
if (SecI != Obj->section_end()) {
|
2015-07-20 11:23:55 +08:00
|
|
|
error(SecI->getName(TargetName));
|
2015-06-19 06:38:20 +08:00
|
|
|
}
|
2014-07-04 18:57:56 +08:00
|
|
|
}
|
2015-06-19 06:38:20 +08:00
|
|
|
if (TargetName.empty())
|
|
|
|
TargetName = "-";
|
2013-04-12 08:17:33 +08:00
|
|
|
|
2013-04-12 12:01:52 +08:00
|
|
|
if (opts::ExpandRelocs) {
|
|
|
|
DictScope Group(W, "Relocation");
|
|
|
|
W.printHex("Offset", Offset);
|
2013-04-19 02:08:55 +08:00
|
|
|
W.printNumber("PCRel", Obj->getAnyRelocationPCRel(RE));
|
|
|
|
W.printNumber("Length", Obj->getAnyRelocationLength(RE));
|
|
|
|
W.printNumber("Type", RelocName, Obj->getAnyRelocationType(RE));
|
2015-06-19 06:38:20 +08:00
|
|
|
if (IsScattered) {
|
|
|
|
W.printHex("Value", Obj->getScatteredRelocationValue(RE));
|
|
|
|
} else {
|
|
|
|
const char *Kind = IsExtern ? "Symbol" : "Section";
|
|
|
|
W.printNumber(Kind, TargetName, Obj->getPlainRelocationSymbolNum(RE));
|
|
|
|
}
|
2013-04-12 12:01:52 +08:00
|
|
|
} else {
|
2015-06-19 06:38:20 +08:00
|
|
|
SmallString<32> SymbolNameOrOffset("0x");
|
|
|
|
if (IsScattered) {
|
|
|
|
// Scattered relocations don't really have an associated symbol for some
|
|
|
|
// reason, even if one exists in the symtab at the correct address.
|
|
|
|
SymbolNameOrOffset += utohexstr(Obj->getScatteredRelocationValue(RE));
|
|
|
|
} else {
|
|
|
|
SymbolNameOrOffset = TargetName;
|
|
|
|
}
|
|
|
|
|
2013-04-12 12:01:52 +08:00
|
|
|
raw_ostream& OS = W.startLine();
|
|
|
|
OS << W.hex(Offset)
|
2013-04-19 02:08:55 +08:00
|
|
|
<< " " << Obj->getAnyRelocationPCRel(RE)
|
|
|
|
<< " " << Obj->getAnyRelocationLength(RE);
|
2013-04-12 12:01:52 +08:00
|
|
|
if (IsScattered)
|
|
|
|
OS << " n/a";
|
|
|
|
else
|
2013-04-19 02:08:55 +08:00
|
|
|
OS << " " << Obj->getPlainRelocationExternal(RE);
|
2013-04-12 12:01:52 +08:00
|
|
|
OS << " " << RelocName
|
|
|
|
<< " " << IsScattered
|
2014-07-04 18:57:56 +08:00
|
|
|
<< " " << SymbolNameOrOffset
|
2013-04-12 12:01:52 +08:00
|
|
|
<< "\n";
|
|
|
|
}
|
2013-04-04 02:31:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MachODumper::printSymbols() {
|
|
|
|
ListScope Group(W, "Symbols");
|
|
|
|
|
2014-03-17 15:28:19 +08:00
|
|
|
for (const SymbolRef &Symbol : Obj->symbols()) {
|
|
|
|
printSymbol(Symbol);
|
2013-04-04 02:31:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachODumper::printDynamicSymbols() {
|
|
|
|
ListScope Group(W, "DynamicSymbols");
|
|
|
|
}
|
|
|
|
|
2014-03-17 15:28:19 +08:00
|
|
|
void MachODumper::printSymbol(const SymbolRef &Symbol) {
|
2013-04-04 02:31:38 +08:00
|
|
|
StringRef SymbolName;
|
2016-04-21 05:24:34 +08:00
|
|
|
Expected<StringRef> SymbolNameOrErr = Symbol.getName();
|
|
|
|
if (!SymbolNameOrErr) {
|
|
|
|
// TODO: Actually report errors helpfully.
|
|
|
|
consumeError(SymbolNameOrErr.takeError());
|
|
|
|
} else
|
2015-07-03 04:55:21 +08:00
|
|
|
SymbolName = *SymbolNameOrErr;
|
2013-04-04 02:31:38 +08:00
|
|
|
|
2014-03-17 15:28:19 +08:00
|
|
|
MachOSymbol MOSymbol;
|
|
|
|
getSymbol(Obj, Symbol.getRawDataRefImpl(), MOSymbol);
|
2013-04-04 02:31:38 +08:00
|
|
|
|
2013-04-22 16:34:46 +08:00
|
|
|
StringRef SectionName = "";
|
2016-05-03 04:28:12 +08:00
|
|
|
Expected<section_iterator> SecIOrErr = Symbol.getSection();
|
|
|
|
error(errorToErrorCode(SecIOrErr.takeError()));
|
2015-08-08 07:27:14 +08:00
|
|
|
section_iterator SecI = *SecIOrErr;
|
2015-07-20 11:23:55 +08:00
|
|
|
if (SecI != Obj->section_end())
|
2014-03-17 15:28:19 +08:00
|
|
|
error(SecI->getName(SectionName));
|
2013-04-04 02:31:38 +08:00
|
|
|
|
|
|
|
DictScope D(W, "Symbol");
|
2014-03-17 15:28:19 +08:00
|
|
|
W.printNumber("Name", SymbolName, MOSymbol.StringIndex);
|
|
|
|
if (MOSymbol.Type & MachO::N_STAB) {
|
|
|
|
W.printHex("Type", "SymDebugTable", MOSymbol.Type);
|
2013-04-04 02:31:38 +08:00
|
|
|
} else {
|
2014-03-17 15:28:19 +08:00
|
|
|
if (MOSymbol.Type & MachO::N_PEXT)
|
2014-03-07 04:13:41 +08:00
|
|
|
W.startLine() << "PrivateExtern\n";
|
2014-03-17 15:28:19 +08:00
|
|
|
if (MOSymbol.Type & MachO::N_EXT)
|
2014-03-07 04:13:41 +08:00
|
|
|
W.startLine() << "Extern\n";
|
2014-03-17 15:28:19 +08:00
|
|
|
W.printEnum("Type", uint8_t(MOSymbol.Type & MachO::N_TYPE),
|
2014-03-07 04:13:41 +08:00
|
|
|
makeArrayRef(MachOSymbolTypes));
|
2013-04-04 02:31:38 +08:00
|
|
|
}
|
2014-03-17 15:28:19 +08:00
|
|
|
W.printHex("Section", SectionName, MOSymbol.SectionIndex);
|
|
|
|
W.printEnum("RefType", static_cast<uint16_t>(MOSymbol.Flags & 0xF),
|
|
|
|
makeArrayRef(MachOSymbolRefTypes));
|
|
|
|
W.printFlags("Flags", static_cast<uint16_t>(MOSymbol.Flags & ~0xF),
|
|
|
|
makeArrayRef(MachOSymbolFlags));
|
|
|
|
W.printHex("Value", MOSymbol.Value);
|
2013-04-04 02:31:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MachODumper::printUnwindInfo() {
|
|
|
|
W.startLine() << "UnwindInfo not implemented.\n";
|
|
|
|
}
|
2015-06-27 07:56:53 +08:00
|
|
|
|
|
|
|
void MachODumper::printStackMap() const {
|
|
|
|
object::SectionRef StackMapSection;
|
|
|
|
for (auto Sec : Obj->sections()) {
|
|
|
|
StringRef Name;
|
|
|
|
Sec.getName(Name);
|
|
|
|
if (Name == "__llvm_stackmaps") {
|
|
|
|
StackMapSection = Sec;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (StackMapSection == object::SectionRef())
|
|
|
|
return;
|
|
|
|
|
|
|
|
StringRef StackMapContents;
|
|
|
|
StackMapSection.getContents(StackMapContents);
|
|
|
|
ArrayRef<uint8_t> StackMapContentsArray(
|
|
|
|
reinterpret_cast<const uint8_t*>(StackMapContents.data()),
|
|
|
|
StackMapContents.size());
|
|
|
|
|
|
|
|
if (Obj->isLittleEndian())
|
2018-01-10 08:14:19 +08:00
|
|
|
prettyPrintStackMap(
|
|
|
|
W, StackMapV2Parser<support::little>(StackMapContentsArray));
|
2015-06-27 07:56:53 +08:00
|
|
|
else
|
2018-01-10 08:14:19 +08:00
|
|
|
prettyPrintStackMap(W,
|
|
|
|
StackMapV2Parser<support::big>(StackMapContentsArray));
|
2015-06-27 07:56:53 +08:00
|
|
|
}
|
2015-08-22 04:28:30 +08:00
|
|
|
|
2018-01-08 10:23:10 +08:00
|
|
|
void MachODumper::printNeededLibraries() {
|
|
|
|
ListScope D(W, "NeededLibraries");
|
|
|
|
|
|
|
|
using LibsTy = std::vector<StringRef>;
|
|
|
|
LibsTy Libs;
|
|
|
|
|
|
|
|
for (const auto &Command : Obj->load_commands()) {
|
|
|
|
if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
|
|
|
|
Command.C.cmd == MachO::LC_ID_DYLIB ||
|
|
|
|
Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
|
|
|
|
Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
|
|
|
|
Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
|
|
|
|
Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
|
|
|
|
MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
|
|
|
|
if (Dl.dylib.name < Dl.cmdsize) {
|
|
|
|
auto *P = static_cast<const char*>(Command.Ptr) + Dl.dylib.name;
|
|
|
|
Libs.push_back(P);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::stable_sort(Libs.begin(), Libs.end());
|
|
|
|
|
|
|
|
for (const auto &L : Libs) {
|
|
|
|
outs() << " " << L << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-22 04:28:30 +08:00
|
|
|
void MachODumper::printMachODataInCode() {
|
|
|
|
for (const auto &Load : Obj->load_commands()) {
|
|
|
|
if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
|
|
|
|
MachO::linkedit_data_command LLC = Obj->getLinkeditDataLoadCommand(Load);
|
|
|
|
DictScope Group(W, "DataInCode");
|
|
|
|
W.printNumber("Data offset", LLC.dataoff);
|
|
|
|
W.printNumber("Data size", LLC.datasize);
|
|
|
|
ListScope D(W, "Data entries");
|
|
|
|
unsigned NumRegions = LLC.datasize / sizeof(MachO::data_in_code_entry);
|
|
|
|
for (unsigned i = 0; i < NumRegions; ++i) {
|
|
|
|
MachO::data_in_code_entry DICE = Obj->getDataInCodeTableEntry(
|
|
|
|
LLC.dataoff, i);
|
|
|
|
DictScope Group(W, "Entry");
|
|
|
|
W.printNumber("Index", i);
|
|
|
|
W.printNumber("Offset", DICE.offset);
|
|
|
|
W.printNumber("Length", DICE.length);
|
|
|
|
W.printNumber("Kind", DICE.kind);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-27 23:11:32 +08:00
|
|
|
|
|
|
|
void MachODumper::printMachOVersionMin() {
|
|
|
|
for (const auto &Load : Obj->load_commands()) {
|
2015-10-29 06:36:05 +08:00
|
|
|
StringRef Cmd;
|
|
|
|
switch (Load.C.cmd) {
|
|
|
|
case MachO::LC_VERSION_MIN_MACOSX:
|
|
|
|
Cmd = "LC_VERSION_MIN_MACOSX";
|
|
|
|
break;
|
|
|
|
case MachO::LC_VERSION_MIN_IPHONEOS:
|
|
|
|
Cmd = "LC_VERSION_MIN_IPHONEOS";
|
|
|
|
break;
|
|
|
|
case MachO::LC_VERSION_MIN_TVOS:
|
|
|
|
Cmd = "LC_VERSION_MIN_TVOS";
|
|
|
|
break;
|
|
|
|
case MachO::LC_VERSION_MIN_WATCHOS:
|
|
|
|
Cmd = "LC_VERSION_MIN_WATCHOS";
|
|
|
|
break;
|
2017-01-24 04:07:55 +08:00
|
|
|
case MachO::LC_BUILD_VERSION:
|
|
|
|
Cmd = "LC_BUILD_VERSION";
|
|
|
|
break;
|
2015-10-29 06:36:05 +08:00
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
DictScope Group(W, "MinVersion");
|
2017-01-24 04:07:55 +08:00
|
|
|
// Handle LC_BUILD_VERSION.
|
|
|
|
if (Load.C.cmd == MachO::LC_BUILD_VERSION) {
|
|
|
|
MachO::build_version_command BVC = Obj->getBuildVersionLoadCommand(Load);
|
|
|
|
W.printString("Cmd", Cmd);
|
|
|
|
W.printNumber("Size", BVC.cmdsize);
|
|
|
|
W.printString("Platform",
|
|
|
|
MachOObjectFile::getBuildPlatform(BVC.platform));
|
|
|
|
W.printString("Version", MachOObjectFile::getVersionString(BVC.minos));
|
|
|
|
if (BVC.sdk)
|
|
|
|
W.printString("SDK", MachOObjectFile::getVersionString(BVC.sdk));
|
|
|
|
else
|
|
|
|
W.printString("SDK", StringRef("n/a"));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
MachO::version_min_command VMC = Obj->getVersionMinLoadCommand(Load);
|
2015-10-29 06:36:05 +08:00
|
|
|
W.printString("Cmd", Cmd);
|
|
|
|
W.printNumber("Size", VMC.cmdsize);
|
|
|
|
SmallString<32> Version;
|
|
|
|
Version = utostr(MachOObjectFile::getVersionMinMajor(VMC, false)) + "." +
|
|
|
|
utostr(MachOObjectFile::getVersionMinMinor(VMC, false));
|
|
|
|
uint32_t Update = MachOObjectFile::getVersionMinUpdate(VMC, false);
|
|
|
|
if (Update != 0)
|
|
|
|
Version += "." + utostr(MachOObjectFile::getVersionMinUpdate(VMC, false));
|
|
|
|
W.printString("Version", Version);
|
|
|
|
SmallString<32> SDK;
|
|
|
|
if (VMC.sdk == 0)
|
|
|
|
SDK = "n/a";
|
|
|
|
else {
|
|
|
|
SDK = utostr(MachOObjectFile::getVersionMinMajor(VMC, true)) + "." +
|
|
|
|
utostr(MachOObjectFile::getVersionMinMinor(VMC, true));
|
|
|
|
uint32_t Update = MachOObjectFile::getVersionMinUpdate(VMC, true);
|
2015-08-27 23:11:32 +08:00
|
|
|
if (Update != 0)
|
2015-10-29 06:36:05 +08:00
|
|
|
SDK += "." + utostr(MachOObjectFile::getVersionMinUpdate(VMC, true));
|
2015-08-27 23:11:32 +08:00
|
|
|
}
|
2015-10-29 06:36:05 +08:00
|
|
|
W.printString("SDK", SDK);
|
2015-08-27 23:11:32 +08:00
|
|
|
}
|
|
|
|
}
|
2015-09-01 03:32:31 +08:00
|
|
|
|
|
|
|
void MachODumper::printMachODysymtab() {
|
|
|
|
for (const auto &Load : Obj->load_commands()) {
|
|
|
|
if (Load.C.cmd == MachO::LC_DYSYMTAB) {
|
|
|
|
MachO::dysymtab_command DLC = Obj->getDysymtabLoadCommand();
|
|
|
|
DictScope Group(W, "Dysymtab");
|
|
|
|
W.printNumber("ilocalsym", DLC.ilocalsym);
|
|
|
|
W.printNumber("nlocalsym", DLC.nlocalsym);
|
|
|
|
W.printNumber("iextdefsym", DLC.iextdefsym);
|
|
|
|
W.printNumber("nextdefsym", DLC.nextdefsym);
|
|
|
|
W.printNumber("iundefsym", DLC.iundefsym);
|
|
|
|
W.printNumber("nundefsym", DLC.nundefsym);
|
|
|
|
W.printNumber("tocoff", DLC.tocoff);
|
|
|
|
W.printNumber("ntoc", DLC.ntoc);
|
|
|
|
W.printNumber("modtaboff", DLC.modtaboff);
|
|
|
|
W.printNumber("nmodtab", DLC.nmodtab);
|
|
|
|
W.printNumber("extrefsymoff", DLC.extrefsymoff);
|
|
|
|
W.printNumber("nextrefsyms", DLC.nextrefsyms);
|
|
|
|
W.printNumber("indirectsymoff", DLC.indirectsymoff);
|
|
|
|
W.printNumber("nindirectsyms", DLC.nindirectsyms);
|
|
|
|
W.printNumber("extreloff", DLC.extreloff);
|
|
|
|
W.printNumber("nextrel", DLC.nextrel);
|
|
|
|
W.printNumber("locreloff", DLC.locreloff);
|
|
|
|
W.printNumber("nlocrel", DLC.nlocrel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-03 00:24:24 +08:00
|
|
|
|
|
|
|
void MachODumper::printMachOSegment() {
|
|
|
|
for (const auto &Load : Obj->load_commands()) {
|
|
|
|
if (Load.C.cmd == MachO::LC_SEGMENT || Load.C.cmd == MachO::LC_SEGMENT_64) {
|
|
|
|
MachOSegment MOSegment;
|
|
|
|
getSegment(Obj, Load, MOSegment);
|
|
|
|
DictScope Group(W, "Segment");
|
|
|
|
W.printString("Cmd", MOSegment.CmdName);
|
|
|
|
W.printString("Name", MOSegment.SegName);
|
|
|
|
W.printNumber("Size", MOSegment.cmdsize);
|
|
|
|
W.printHex("vmaddr", MOSegment.vmaddr);
|
|
|
|
W.printHex("vmsize", MOSegment.vmsize);
|
|
|
|
W.printNumber("fileoff", MOSegment.fileoff);
|
2015-09-05 04:43:00 +08:00
|
|
|
W.printNumber("filesize", MOSegment.filesize);
|
2015-09-03 00:24:24 +08:00
|
|
|
W.printString("maxprot", getMask(MOSegment.maxprot));
|
|
|
|
W.printString("initprot", getMask(MOSegment.initprot));
|
|
|
|
W.printNumber("nsects", MOSegment.nsects);
|
|
|
|
W.printHex("flags", MOSegment.flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-04 02:10:28 +08:00
|
|
|
|
|
|
|
void MachODumper::printMachOIndirectSymbols() {
|
|
|
|
for (const auto &Load : Obj->load_commands()) {
|
|
|
|
if (Load.C.cmd == MachO::LC_DYSYMTAB) {
|
|
|
|
MachO::dysymtab_command DLC = Obj->getDysymtabLoadCommand();
|
|
|
|
DictScope Group(W, "Indirect Symbols");
|
|
|
|
W.printNumber("Number", DLC.nindirectsyms);
|
|
|
|
ListScope D(W, "Symbols");
|
|
|
|
for (unsigned i = 0; i < DLC.nindirectsyms; ++i) {
|
|
|
|
DictScope Group(W, "Entry");
|
|
|
|
W.printNumber("Entry Index", i);
|
|
|
|
W.printHex("Symbol Index", Obj->getIndirectSymbolTableEntry(DLC, i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-09 08:21:18 +08:00
|
|
|
|
|
|
|
void MachODumper::printMachOLinkerOptions() {
|
|
|
|
for (const auto &Load : Obj->load_commands()) {
|
|
|
|
if (Load.C.cmd == MachO::LC_LINKER_OPTION) {
|
|
|
|
MachO::linker_option_command LOLC = Obj->getLinkerOptionLoadCommand(Load);
|
|
|
|
DictScope Group(W, "Linker Options");
|
|
|
|
W.printNumber("Size", LOLC.cmdsize);
|
|
|
|
ListScope D(W, "Strings");
|
|
|
|
uint64_t DataSize = LOLC.cmdsize - sizeof(MachO::linker_option_command);
|
|
|
|
const char *P = Load.Ptr + sizeof(MachO::linker_option_command);
|
|
|
|
StringRef Data(P, DataSize);
|
|
|
|
for (unsigned i = 0; i < LOLC.count; ++i) {
|
|
|
|
std::pair<StringRef,StringRef> Split = Data.split('\0');
|
|
|
|
W.printString("Value", Split.first);
|
|
|
|
Data = Split.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|