2010-11-27 06:58:44 +01:00
|
|
|
//===-- macho-dump.cpp - Mach Object Dumping Tool -------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This is a testing tool for use with the MC/Mach-O LLVM components.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-04-26 22:07:33 +02:00
|
|
|
#include "llvm/Object/MachO.h"
|
2010-11-27 14:58:16 +01:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2010-11-27 08:19:41 +01:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2013-04-26 22:07:33 +02:00
|
|
|
#include "llvm/Support/Casting.h"
|
2010-11-27 06:58:44 +01:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2010-11-27 14:26:12 +01:00
|
|
|
#include "llvm/Support/Format.h"
|
2010-11-27 06:58:44 +01:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2010-11-27 07:19:17 +01:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2010-11-27 06:58:44 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-06-12 19:38:55 +02:00
|
|
|
#include <system_error>
|
2010-11-27 06:58:44 +01:00
|
|
|
using namespace llvm;
|
2010-11-27 07:19:17 +01:00
|
|
|
using namespace llvm::object;
|
2010-11-27 06:58:44 +01:00
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
InputFile(cl::Positional, cl::desc("<input file>"), cl::init("-"));
|
|
|
|
|
2010-11-27 07:19:17 +01:00
|
|
|
static cl::opt<bool>
|
2010-11-27 14:33:15 +01:00
|
|
|
ShowSectionData("dump-section-data", cl::desc("Dump the contents of sections"),
|
2010-11-27 07:19:17 +01:00
|
|
|
cl::init(false));
|
|
|
|
|
2010-11-27 08:19:41 +01:00
|
|
|
///
|
|
|
|
|
|
|
|
static const char *ProgramName;
|
|
|
|
|
|
|
|
static void Message(const char *Type, const Twine &Msg) {
|
|
|
|
errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
static int Error(const Twine &Msg) {
|
|
|
|
Message("error", Msg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Warning(const Twine &Msg) {
|
|
|
|
Message("warning", Msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
|
2010-11-27 09:22:29 +01:00
|
|
|
static void DumpSegmentCommandData(StringRef Name,
|
|
|
|
uint64_t VMAddr, uint64_t VMSize,
|
|
|
|
uint64_t FileOffset, uint64_t FileSize,
|
|
|
|
uint32_t MaxProt, uint32_t InitProt,
|
|
|
|
uint32_t NumSections, uint32_t Flags) {
|
|
|
|
outs() << " ('segment_name', '";
|
|
|
|
outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n";
|
|
|
|
outs() << " ('vm_addr', " << VMAddr << ")\n";
|
|
|
|
outs() << " ('vm_size', " << VMSize << ")\n";
|
|
|
|
outs() << " ('file_offset', " << FileOffset << ")\n";
|
|
|
|
outs() << " ('file_size', " << FileSize << ")\n";
|
|
|
|
outs() << " ('maxprot', " << MaxProt << ")\n";
|
|
|
|
outs() << " ('initprot', " << InitProt << ")\n";
|
|
|
|
outs() << " ('num_sections', " << NumSections << ")\n";
|
|
|
|
outs() << " ('flags', " << Flags << ")\n";
|
|
|
|
}
|
|
|
|
|
2013-04-26 22:07:33 +02:00
|
|
|
static int DumpSectionData(const MachOObjectFile &Obj, unsigned Index,
|
|
|
|
StringRef Name,
|
2010-11-27 14:39:48 +01:00
|
|
|
StringRef SegmentName, uint64_t Address,
|
|
|
|
uint64_t Size, uint32_t Offset,
|
|
|
|
uint32_t Align, uint32_t RelocationTableOffset,
|
|
|
|
uint32_t NumRelocationTableEntries,
|
|
|
|
uint32_t Flags, uint32_t Reserved1,
|
|
|
|
uint32_t Reserved2, uint64_t Reserved3 = ~0ULL) {
|
2010-11-27 14:33:15 +01:00
|
|
|
outs() << " # Section " << Index << "\n";
|
|
|
|
outs() << " (('section_name', '";
|
|
|
|
outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n";
|
|
|
|
outs() << " ('segment_name', '";
|
|
|
|
outs().write_escaped(SegmentName, /*UseHexEscapes=*/true) << "')\n";
|
|
|
|
outs() << " ('address', " << Address << ")\n";
|
|
|
|
outs() << " ('size', " << Size << ")\n";
|
|
|
|
outs() << " ('offset', " << Offset << ")\n";
|
|
|
|
outs() << " ('alignment', " << Align << ")\n";
|
|
|
|
outs() << " ('reloc_offset', " << RelocationTableOffset << ")\n";
|
|
|
|
outs() << " ('num_reloc', " << NumRelocationTableEntries << ")\n";
|
2010-11-27 14:58:16 +01:00
|
|
|
outs() << " ('flags', " << format("0x%x", Flags) << ")\n";
|
2010-11-27 14:33:15 +01:00
|
|
|
outs() << " ('reserved1', " << Reserved1 << ")\n";
|
|
|
|
outs() << " ('reserved2', " << Reserved2 << ")\n";
|
|
|
|
if (Reserved3 != ~0ULL)
|
|
|
|
outs() << " ('reserved3', " << Reserved3 << ")\n";
|
2010-11-27 14:39:48 +01:00
|
|
|
outs() << " ),\n";
|
|
|
|
|
|
|
|
// Dump the relocation entries.
|
|
|
|
outs() << " ('_relocations', [\n";
|
2013-04-26 22:07:33 +02:00
|
|
|
unsigned RelNum = 0;
|
2013-09-27 23:47:05 +02:00
|
|
|
for (relocation_iterator I = Obj.section_rel_begin(Index),
|
2014-01-30 03:49:50 +01:00
|
|
|
E = Obj.section_rel_end(Index);
|
|
|
|
I != E; ++I, ++RelNum) {
|
2013-09-01 06:28:48 +02:00
|
|
|
MachO::any_relocation_info RE = Obj.getRelocation(I->getRawDataRefImpl());
|
2013-04-26 22:07:33 +02:00
|
|
|
outs() << " # Relocation " << RelNum << "\n";
|
2013-09-01 06:28:48 +02:00
|
|
|
outs() << " (('word-0', " << format("0x%x", RE.r_word0) << "),\n";
|
|
|
|
outs() << " ('word-1', " << format("0x%x", RE.r_word1) << ")),\n";
|
2010-11-27 14:39:48 +01:00
|
|
|
}
|
|
|
|
outs() << " ])\n";
|
|
|
|
|
2010-11-27 14:58:16 +01:00
|
|
|
// Dump the section data, if requested.
|
|
|
|
if (ShowSectionData) {
|
|
|
|
outs() << " ('_section_data', '";
|
2013-04-26 22:07:33 +02:00
|
|
|
StringRef Data = Obj.getData().substr(Offset, Size);
|
2010-11-27 14:58:16 +01:00
|
|
|
for (unsigned i = 0; i != Data.size(); ++i) {
|
|
|
|
if (i && (i % 4) == 0)
|
|
|
|
outs() << ' ';
|
|
|
|
outs() << hexdigit((Data[i] >> 4) & 0xF, /*LowerCase=*/true);
|
|
|
|
outs() << hexdigit((Data[i] >> 0) & 0xF, /*LowerCase=*/true);
|
|
|
|
}
|
|
|
|
outs() << "')\n";
|
|
|
|
}
|
|
|
|
|
2013-04-26 22:07:33 +02:00
|
|
|
return 0;
|
2010-11-27 14:33:15 +01:00
|
|
|
}
|
|
|
|
|
2013-04-26 22:07:33 +02:00
|
|
|
static int DumpSegmentCommand(const MachOObjectFile &Obj,
|
|
|
|
const MachOObjectFile::LoadCommandInfo &LCI) {
|
2013-09-01 06:28:48 +02:00
|
|
|
MachO::segment_command SLC = Obj.getSegmentLoadCommand(LCI);
|
2010-11-27 09:22:29 +01:00
|
|
|
|
2013-09-01 06:28:48 +02:00
|
|
|
DumpSegmentCommandData(StringRef(SLC.segname, 16), SLC.vmaddr,
|
|
|
|
SLC.vmsize, SLC.fileoff, SLC.filesize,
|
|
|
|
SLC.maxprot, SLC.initprot, SLC.nsects, SLC.flags);
|
2010-11-27 09:22:29 +01:00
|
|
|
|
2010-11-27 14:33:15 +01:00
|
|
|
// Dump the sections.
|
|
|
|
outs() << " ('sections', [\n";
|
2013-09-01 06:28:48 +02:00
|
|
|
for (unsigned i = 0; i != SLC.nsects; ++i) {
|
|
|
|
MachO::section Sect = Obj.getSection(LCI, i);
|
|
|
|
DumpSectionData(Obj, i, StringRef(Sect.sectname, 16),
|
|
|
|
StringRef(Sect.segname, 16), Sect.addr,
|
|
|
|
Sect.size, Sect.offset, Sect.align,
|
|
|
|
Sect.reloff, Sect.nreloc, Sect.flags,
|
|
|
|
Sect.reserved1, Sect.reserved2);
|
2010-11-27 14:33:15 +01:00
|
|
|
}
|
|
|
|
outs() << " ])\n";
|
|
|
|
|
2013-04-26 22:07:33 +02:00
|
|
|
return 0;
|
2010-11-27 09:22:29 +01:00
|
|
|
}
|
2010-11-27 14:33:15 +01:00
|
|
|
|
2013-04-26 22:07:33 +02:00
|
|
|
static int DumpSegment64Command(const MachOObjectFile &Obj,
|
|
|
|
const MachOObjectFile::LoadCommandInfo &LCI) {
|
2013-09-01 06:28:48 +02:00
|
|
|
MachO::segment_command_64 SLC = Obj.getSegment64LoadCommand(LCI);
|
|
|
|
DumpSegmentCommandData(StringRef(SLC.segname, 16), SLC.vmaddr,
|
|
|
|
SLC.vmsize, SLC.fileoff, SLC.filesize,
|
|
|
|
SLC.maxprot, SLC.initprot, SLC.nsects, SLC.flags);
|
2010-11-27 09:22:29 +01:00
|
|
|
|
2010-11-27 14:33:15 +01:00
|
|
|
// Dump the sections.
|
|
|
|
outs() << " ('sections', [\n";
|
2013-09-01 06:28:48 +02:00
|
|
|
for (unsigned i = 0; i != SLC.nsects; ++i) {
|
|
|
|
MachO::section_64 Sect = Obj.getSection64(LCI, i);
|
|
|
|
|
|
|
|
DumpSectionData(Obj, i, StringRef(Sect.sectname, 16),
|
|
|
|
StringRef(Sect.segname, 16), Sect.addr,
|
|
|
|
Sect.size, Sect.offset, Sect.align,
|
|
|
|
Sect.reloff, Sect.nreloc, Sect.flags,
|
|
|
|
Sect.reserved1, Sect.reserved2,
|
|
|
|
Sect.reserved3);
|
2010-11-27 14:33:15 +01:00
|
|
|
}
|
|
|
|
outs() << " ])\n";
|
|
|
|
|
2013-04-26 22:07:33 +02:00
|
|
|
return 0;
|
2010-11-27 09:22:29 +01:00
|
|
|
}
|
|
|
|
|
2013-04-26 22:07:33 +02:00
|
|
|
static void DumpSymbolTableEntryData(const MachOObjectFile &Obj,
|
2010-11-27 14:52:53 +01:00
|
|
|
unsigned Index, uint32_t StringIndex,
|
|
|
|
uint8_t Type, uint8_t SectionIndex,
|
2013-04-26 22:07:33 +02:00
|
|
|
uint16_t Flags, uint64_t Value,
|
|
|
|
StringRef StringTable) {
|
|
|
|
const char *Name = &StringTable.data()[StringIndex];
|
2010-11-27 14:52:53 +01:00
|
|
|
outs() << " # Symbol " << Index << "\n";
|
|
|
|
outs() << " (('n_strx', " << StringIndex << ")\n";
|
2010-11-27 14:58:16 +01:00
|
|
|
outs() << " ('n_type', " << format("0x%x", Type) << ")\n";
|
2010-11-27 14:52:53 +01:00
|
|
|
outs() << " ('n_sect', " << uint32_t(SectionIndex) << ")\n";
|
|
|
|
outs() << " ('n_desc', " << Flags << ")\n";
|
|
|
|
outs() << " ('n_value', " << Value << ")\n";
|
2013-04-26 22:07:33 +02:00
|
|
|
outs() << " ('_string', '" << Name << "')\n";
|
2010-11-27 14:52:53 +01:00
|
|
|
outs() << " ),\n";
|
|
|
|
}
|
|
|
|
|
2013-04-26 22:07:33 +02:00
|
|
|
static int DumpSymtabCommand(const MachOObjectFile &Obj) {
|
2013-09-01 06:28:48 +02:00
|
|
|
MachO::symtab_command SLC = Obj.getSymtabLoadCommand();
|
2010-11-27 09:33:44 +01:00
|
|
|
|
2013-09-01 06:28:48 +02:00
|
|
|
outs() << " ('symoff', " << SLC.symoff << ")\n";
|
|
|
|
outs() << " ('nsyms', " << SLC.nsyms << ")\n";
|
|
|
|
outs() << " ('stroff', " << SLC.stroff << ")\n";
|
|
|
|
outs() << " ('strsize', " << SLC.strsize << ")\n";
|
2010-11-27 14:46:11 +01:00
|
|
|
|
|
|
|
// Dump the string data.
|
|
|
|
outs() << " ('_string_data', '";
|
2013-04-26 22:07:33 +02:00
|
|
|
StringRef StringTable = Obj.getStringTableData();
|
|
|
|
outs().write_escaped(StringTable,
|
2010-11-27 14:46:11 +01:00
|
|
|
/*UseHexEscapes=*/true) << "')\n";
|
|
|
|
|
2010-11-27 14:52:53 +01:00
|
|
|
// Dump the symbol table.
|
|
|
|
outs() << " ('_symbols', [\n";
|
2013-04-26 22:07:33 +02:00
|
|
|
unsigned SymNum = 0;
|
2014-03-17 08:28:19 +01:00
|
|
|
for (const SymbolRef &Symbol : Obj.symbols()) {
|
|
|
|
DataRefImpl DRI = Symbol.getRawDataRefImpl();
|
2010-11-27 14:52:53 +01:00
|
|
|
if (Obj.is64Bit()) {
|
2013-09-01 06:28:48 +02:00
|
|
|
MachO::nlist_64 STE = Obj.getSymbol64TableEntry(DRI);
|
|
|
|
DumpSymbolTableEntryData(Obj, SymNum, STE.n_strx, STE.n_type,
|
|
|
|
STE.n_sect, STE.n_desc, STE.n_value,
|
2013-04-26 22:07:33 +02:00
|
|
|
StringTable);
|
2010-11-27 14:52:53 +01:00
|
|
|
} else {
|
2013-09-01 06:28:48 +02:00
|
|
|
MachO::nlist STE = Obj.getSymbolTableEntry(DRI);
|
|
|
|
DumpSymbolTableEntryData(Obj, SymNum, STE.n_strx, STE.n_type,
|
|
|
|
STE.n_sect, STE.n_desc, STE.n_value,
|
2013-04-26 22:07:33 +02:00
|
|
|
StringTable);
|
2010-11-27 14:52:53 +01:00
|
|
|
}
|
2014-03-17 08:28:19 +01:00
|
|
|
SymNum++;
|
2010-11-27 14:52:53 +01:00
|
|
|
}
|
|
|
|
outs() << " ])\n";
|
|
|
|
|
2013-04-26 22:07:33 +02:00
|
|
|
return 0;
|
2010-11-27 09:33:44 +01:00
|
|
|
}
|
|
|
|
|
2013-04-26 22:07:33 +02:00
|
|
|
static int DumpDysymtabCommand(const MachOObjectFile &Obj) {
|
2013-09-01 06:28:48 +02:00
|
|
|
MachO::dysymtab_command DLC = Obj.getDysymtabLoadCommand();
|
|
|
|
|
|
|
|
outs() << " ('ilocalsym', " << DLC.ilocalsym << ")\n";
|
|
|
|
outs() << " ('nlocalsym', " << DLC.nlocalsym << ")\n";
|
|
|
|
outs() << " ('iextdefsym', " << DLC.iextdefsym << ")\n";
|
|
|
|
outs() << " ('nextdefsym', " << DLC.nextdefsym << ")\n";
|
|
|
|
outs() << " ('iundefsym', " << DLC.iundefsym << ")\n";
|
|
|
|
outs() << " ('nundefsym', " << DLC.nundefsym << ")\n";
|
|
|
|
outs() << " ('tocoff', " << DLC.tocoff << ")\n";
|
|
|
|
outs() << " ('ntoc', " << DLC.ntoc << ")\n";
|
|
|
|
outs() << " ('modtaboff', " << DLC.modtaboff << ")\n";
|
|
|
|
outs() << " ('nmodtab', " << DLC.nmodtab << ")\n";
|
|
|
|
outs() << " ('extrefsymoff', " << DLC.extrefsymoff << ")\n";
|
|
|
|
outs() << " ('nextrefsyms', " << DLC.nextrefsyms << ")\n";
|
|
|
|
outs() << " ('indirectsymoff', " << DLC.indirectsymoff << ")\n";
|
|
|
|
outs() << " ('nindirectsyms', " << DLC.nindirectsyms << ")\n";
|
|
|
|
outs() << " ('extreloff', " << DLC.extreloff << ")\n";
|
|
|
|
outs() << " ('nextrel', " << DLC.nextrel << ")\n";
|
|
|
|
outs() << " ('locreloff', " << DLC.locreloff << ")\n";
|
|
|
|
outs() << " ('nlocrel', " << DLC.nlocrel << ")\n";
|
2010-11-27 09:33:44 +01:00
|
|
|
|
2010-11-27 14:26:12 +01:00
|
|
|
// Dump the indirect symbol table.
|
|
|
|
outs() << " ('_indirect_symbols', [\n";
|
2013-09-01 06:28:48 +02:00
|
|
|
for (unsigned i = 0; i != DLC.nindirectsyms; ++i) {
|
|
|
|
uint32_t ISTE = Obj.getIndirectSymbolTableEntry(DLC, i);
|
2010-11-27 14:26:12 +01:00
|
|
|
outs() << " # Indirect Symbol " << i << "\n";
|
2013-09-01 06:28:48 +02:00
|
|
|
outs() << " (('symbol_index', " << format("0x%x", ISTE) << "),),\n";
|
2010-11-27 14:26:12 +01:00
|
|
|
}
|
|
|
|
outs() << " ])\n";
|
|
|
|
|
2013-04-26 22:07:33 +02:00
|
|
|
return 0;
|
2010-11-27 09:33:44 +01:00
|
|
|
}
|
|
|
|
|
2013-04-26 22:07:33 +02:00
|
|
|
static int
|
|
|
|
DumpLinkeditDataCommand(const MachOObjectFile &Obj,
|
|
|
|
const MachOObjectFile::LoadCommandInfo &LCI) {
|
2013-09-01 06:28:48 +02:00
|
|
|
MachO::linkedit_data_command LLC = Obj.getLinkeditDataLoadCommand(LCI);
|
|
|
|
outs() << " ('dataoff', " << LLC.dataoff << ")\n"
|
|
|
|
<< " ('datasize', " << LLC.datasize << ")\n"
|
2011-08-31 00:10:58 +02:00
|
|
|
<< " ('_addresses', [\n";
|
|
|
|
|
|
|
|
SmallVector<uint64_t, 8> Addresses;
|
2013-09-01 06:28:48 +02:00
|
|
|
Obj.ReadULEB128s(LLC.dataoff, Addresses);
|
2011-08-31 00:10:58 +02:00
|
|
|
for (unsigned i = 0, e = Addresses.size(); i != e; ++i)
|
|
|
|
outs() << " # Address " << i << '\n'
|
|
|
|
<< " ('address', " << format("0x%x", Addresses[i]) << "),\n";
|
|
|
|
|
|
|
|
outs() << " ])\n";
|
2011-08-30 20:33:37 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-26 22:07:33 +02:00
|
|
|
static int
|
|
|
|
DumpDataInCodeDataCommand(const MachOObjectFile &Obj,
|
|
|
|
const MachOObjectFile::LoadCommandInfo &LCI) {
|
2013-09-01 06:28:48 +02:00
|
|
|
MachO::linkedit_data_command LLC = Obj.getLinkeditDataLoadCommand(LCI);
|
|
|
|
outs() << " ('dataoff', " << LLC.dataoff << ")\n"
|
|
|
|
<< " ('datasize', " << LLC.datasize << ")\n"
|
2012-05-18 21:12:01 +02:00
|
|
|
<< " ('_data_regions', [\n";
|
|
|
|
|
2013-09-01 06:28:48 +02:00
|
|
|
unsigned NumRegions = LLC.datasize / sizeof(MachO::data_in_code_entry);
|
2012-05-18 21:12:01 +02:00
|
|
|
for (unsigned i = 0; i < NumRegions; ++i) {
|
2013-09-01 06:28:48 +02:00
|
|
|
MachO::data_in_code_entry DICE= Obj.getDataInCodeTableEntry(LLC.dataoff, i);
|
2012-05-18 21:12:01 +02:00
|
|
|
outs() << " # DICE " << i << "\n"
|
2013-09-01 06:28:48 +02:00
|
|
|
<< " ('offset', " << DICE.offset << ")\n"
|
|
|
|
<< " ('length', " << DICE.length << ")\n"
|
|
|
|
<< " ('kind', " << DICE.kind << ")\n";
|
2012-05-18 21:12:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
outs() <<" ])\n";
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-26 22:07:33 +02:00
|
|
|
static int
|
|
|
|
DumpLinkerOptionsCommand(const MachOObjectFile &Obj,
|
|
|
|
const MachOObjectFile::LoadCommandInfo &LCI) {
|
2013-09-01 06:28:48 +02:00
|
|
|
MachO::linker_options_command LOLC = Obj.getLinkerOptionsLoadCommand(LCI);
|
|
|
|
outs() << " ('count', " << LOLC.count << ")\n"
|
|
|
|
<< " ('_strings', [\n";
|
|
|
|
|
|
|
|
uint64_t DataSize = LOLC.cmdsize - sizeof(MachO::linker_options_command);
|
|
|
|
const char *P = LCI.Ptr + sizeof(MachO::linker_options_command);
|
|
|
|
StringRef Data(P, DataSize);
|
|
|
|
for (unsigned i = 0; i != LOLC.count; ++i) {
|
|
|
|
std::pair<StringRef,StringRef> Split = Data.split('\0');
|
|
|
|
outs() << "\t\"";
|
|
|
|
outs().write_escaped(Split.first);
|
|
|
|
outs() << "\",\n";
|
|
|
|
Data = Split.second;
|
|
|
|
}
|
|
|
|
outs() <<" ])\n";
|
2013-01-18 02:26:07 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-18 23:09:05 +01:00
|
|
|
static int
|
|
|
|
DumpVersionMin(const MachOObjectFile &Obj,
|
|
|
|
const MachOObjectFile::LoadCommandInfo &LCI) {
|
|
|
|
MachO::version_min_command VMLC = Obj.getVersionMinLoadCommand(LCI);
|
|
|
|
outs() << " ('version, " << VMLC.version << ")\n"
|
2014-09-04 18:54:47 +02:00
|
|
|
<< " ('sdk, " << VMLC.sdk << ")\n";
|
2014-03-18 23:09:05 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-30 16:40:57 +02:00
|
|
|
static int
|
|
|
|
DumpDylibID(const MachOObjectFile &Obj,
|
|
|
|
const MachOObjectFile::LoadCommandInfo &LCI) {
|
|
|
|
MachO::dylib_command DLLC = Obj.getDylibIDLoadCommand(LCI);
|
|
|
|
outs() << " ('install_name', '" << LCI.Ptr + DLLC.dylib.name << "')\n"
|
|
|
|
<< " ('timestamp, " << DLLC.dylib.timestamp << ")\n"
|
|
|
|
<< " ('cur_version, " << DLLC.dylib.current_version << ")\n"
|
|
|
|
<< " ('compat_version, " << DLLC.dylib.compatibility_version << ")\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-26 22:07:33 +02:00
|
|
|
static int DumpLoadCommand(const MachOObjectFile &Obj,
|
|
|
|
MachOObjectFile::LoadCommandInfo &LCI) {
|
2013-09-01 06:28:48 +02:00
|
|
|
switch (LCI.C.cmd) {
|
|
|
|
case MachO::LC_SEGMENT:
|
2013-08-27 07:16:07 +02:00
|
|
|
return DumpSegmentCommand(Obj, LCI);
|
2013-09-01 06:28:48 +02:00
|
|
|
case MachO::LC_SEGMENT_64:
|
2013-08-27 07:16:07 +02:00
|
|
|
return DumpSegment64Command(Obj, LCI);
|
2013-09-01 06:28:48 +02:00
|
|
|
case MachO::LC_SYMTAB:
|
2013-08-27 07:16:07 +02:00
|
|
|
return DumpSymtabCommand(Obj);
|
2013-09-01 06:28:48 +02:00
|
|
|
case MachO::LC_DYSYMTAB:
|
2013-08-27 07:16:07 +02:00
|
|
|
return DumpDysymtabCommand(Obj);
|
2013-09-01 06:28:48 +02:00
|
|
|
case MachO::LC_CODE_SIGNATURE:
|
|
|
|
case MachO::LC_SEGMENT_SPLIT_INFO:
|
|
|
|
case MachO::LC_FUNCTION_STARTS:
|
2013-08-27 07:16:07 +02:00
|
|
|
return DumpLinkeditDataCommand(Obj, LCI);
|
2013-09-01 06:28:48 +02:00
|
|
|
case MachO::LC_DATA_IN_CODE:
|
2013-08-27 07:16:07 +02:00
|
|
|
return DumpDataInCodeDataCommand(Obj, LCI);
|
2013-09-01 06:28:48 +02:00
|
|
|
case MachO::LC_LINKER_OPTIONS:
|
2013-08-27 07:16:07 +02:00
|
|
|
return DumpLinkerOptionsCommand(Obj, LCI);
|
2014-03-18 23:09:05 +01:00
|
|
|
case MachO::LC_VERSION_MIN_IPHONEOS:
|
|
|
|
case MachO::LC_VERSION_MIN_MACOSX:
|
|
|
|
return DumpVersionMin(Obj, LCI);
|
2014-06-30 16:40:57 +02:00
|
|
|
case MachO::LC_ID_DYLIB:
|
|
|
|
return DumpDylibID(Obj, LCI);
|
2010-11-27 08:19:41 +01:00
|
|
|
default:
|
2013-09-01 06:28:48 +02:00
|
|
|
Warning("unknown load command: " + Twine(LCI.C.cmd));
|
2013-08-27 07:16:07 +02:00
|
|
|
return 0;
|
2010-11-27 08:19:41 +01:00
|
|
|
}
|
2013-04-26 22:07:33 +02:00
|
|
|
}
|
2010-11-27 08:19:41 +01:00
|
|
|
|
2013-04-26 22:07:33 +02:00
|
|
|
|
|
|
|
static int DumpLoadCommand(const MachOObjectFile &Obj, unsigned Index,
|
|
|
|
MachOObjectFile::LoadCommandInfo &LCI) {
|
|
|
|
outs() << " # Load Command " << Index << "\n"
|
2013-09-01 06:28:48 +02:00
|
|
|
<< " (('command', " << LCI.C.cmd << ")\n"
|
|
|
|
<< " ('size', " << LCI.C.cmdsize << ")\n";
|
2013-04-26 22:07:33 +02:00
|
|
|
int Res = DumpLoadCommand(Obj, LCI);
|
|
|
|
outs() << " ),\n";
|
2010-11-27 09:22:29 +01:00
|
|
|
return Res;
|
2010-11-27 08:19:41 +01:00
|
|
|
}
|
|
|
|
|
2013-04-26 22:07:33 +02:00
|
|
|
static void printHeader(const MachOObjectFile *Obj,
|
2013-09-01 06:28:48 +02:00
|
|
|
const MachO::mach_header &Header) {
|
|
|
|
outs() << "('cputype', " << Header.cputype << ")\n";
|
|
|
|
outs() << "('cpusubtype', " << Header.cpusubtype << ")\n";
|
|
|
|
outs() << "('filetype', " << Header.filetype << ")\n";
|
|
|
|
outs() << "('num_load_commands', " << Header.ncmds << ")\n";
|
|
|
|
outs() << "('load_commands_size', " << Header.sizeofcmds << ")\n";
|
|
|
|
outs() << "('flag', " << Header.flags << ")\n";
|
2013-04-26 22:07:33 +02:00
|
|
|
|
|
|
|
// Print extended header if 64-bit.
|
|
|
|
if (Obj->is64Bit()) {
|
2013-09-01 06:28:48 +02:00
|
|
|
const MachO::mach_header_64 *Header64 =
|
|
|
|
reinterpret_cast<const MachO::mach_header_64 *>(&Header);
|
|
|
|
outs() << "('reserved', " << Header64->reserved << ")\n";
|
2013-04-26 22:07:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-27 06:58:44 +01:00
|
|
|
int main(int argc, char **argv) {
|
2010-11-27 08:19:41 +01:00
|
|
|
ProgramName = argv[0];
|
2010-11-27 06:58:44 +01:00
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
|
|
|
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n");
|
|
|
|
|
2014-08-19 20:44:46 +02:00
|
|
|
ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(InputFile);
|
2014-06-13 05:07:50 +02:00
|
|
|
if (std::error_code EC = BinaryOrErr.getError())
|
2013-04-26 22:07:33 +02:00
|
|
|
return Error("unable to read input: '" + EC.message() + "'");
|
2014-08-19 20:44:46 +02:00
|
|
|
Binary &Binary = *BinaryOrErr.get().getBinary();
|
2010-11-27 07:19:17 +01:00
|
|
|
|
2014-08-01 16:31:55 +02:00
|
|
|
const MachOObjectFile *InputObject = dyn_cast<MachOObjectFile>(&Binary);
|
2010-11-27 08:19:41 +01:00
|
|
|
if (!InputObject)
|
2013-04-26 22:07:33 +02:00
|
|
|
return Error("Not a MachO object");
|
2010-11-27 08:19:41 +01:00
|
|
|
|
2011-04-04 01:51:47 +02:00
|
|
|
// Print the header
|
2013-09-01 06:28:48 +02:00
|
|
|
MachO::mach_header_64 Header64;
|
|
|
|
MachO::mach_header *Header = reinterpret_cast<MachO::mach_header*>(&Header64);
|
|
|
|
if (InputObject->is64Bit())
|
|
|
|
Header64 = InputObject->getHeader64();
|
|
|
|
else
|
|
|
|
*Header = InputObject->getHeader();
|
|
|
|
printHeader(InputObject, *Header);
|
2010-11-27 08:19:41 +01:00
|
|
|
|
|
|
|
// Print the load commands.
|
2010-11-27 09:22:29 +01:00
|
|
|
int Res = 0;
|
2013-04-26 22:07:33 +02:00
|
|
|
MachOObjectFile::LoadCommandInfo Command =
|
|
|
|
InputObject->getFirstLoadCommandInfo();
|
2010-11-27 08:19:41 +01:00
|
|
|
outs() << "('load_commands', [\n";
|
2013-04-26 22:07:33 +02:00
|
|
|
for (unsigned i = 0; ; ++i) {
|
|
|
|
if (DumpLoadCommand(*InputObject, i, Command))
|
2010-11-27 09:22:29 +01:00
|
|
|
break;
|
2013-04-26 22:07:33 +02:00
|
|
|
|
2013-09-01 06:28:48 +02:00
|
|
|
if (i == Header->ncmds - 1)
|
2013-04-26 22:07:33 +02:00
|
|
|
break;
|
|
|
|
Command = InputObject->getNextLoadCommandInfo(Command);
|
|
|
|
}
|
2010-11-27 08:19:41 +01:00
|
|
|
outs() << "])\n";
|
2010-11-27 07:19:17 +01:00
|
|
|
|
2010-11-27 09:22:29 +01:00
|
|
|
return Res;
|
2010-11-27 06:58:44 +01:00
|
|
|
}
|