summaryrefslogtreecommitdiff
path: root/tools/macho-dump/macho-dump.cpp
blob: 34a9527665263346861cf2513d47f483ace7b002 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//===-- 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.
//
//===----------------------------------------------------------------------===//

#include "llvm/Object/MachOObject.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
using namespace llvm::object;

static cl::opt<std::string>
InputFile(cl::Positional, cl::desc("<input file>"), cl::init("-"));

static cl::opt<bool>
DumpSectionData("dump-section-data", cl::desc("Dump the contents of sections"),
                cl::init(false));

///

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);
}

///

static int DumpHeader(MachOObject &Obj) {
  // Read the header.
  const macho::Header &Hdr = Obj.getHeader();
  outs() << "('cputype', " << Hdr.CPUType << ")\n";
  outs() << "('cpusubtype', " << Hdr.CPUSubtype << ")\n";
  outs() << "('filetype', " << Hdr.FileType << ")\n";
  outs() << "('num_load_commands', " << Hdr.NumLoadCommands << ")\n";
  outs() << "('load_commands_size', " << Hdr.SizeOfLoadCommands << ")\n";
  outs() << "('flag', " << Hdr.Flags << ")\n";

  // Print extended header if 64-bit.
  if (Obj.is64Bit()) {
    const macho::Header64Ext &Hdr64 = Obj.getHeader64Ext();
    outs() << "('reserved', " << Hdr64.Reserved << ")\n";
  }

  return 0;
}

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";
}

static int DumpSegmentCommand(MachOObject &Obj,
                               const MachOObject::LoadCommandInfo &LCI) {
  InMemoryStruct<macho::SegmentLoadCommand> SLC;
  Obj.ReadSegmentLoadCommand(LCI, SLC);
  if (!SLC)
    return Error("unable to read segment load command");

  DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress, SLC->VMSize,
                         SLC->FileOffset, SLC->FileSize,
                         SLC->MaxVMProtection, SLC->InitialVMProtection,
                         SLC->NumSections, SLC->Flags);

  return 0;
}
static int DumpSegment64Command(MachOObject &Obj,
                               const MachOObject::LoadCommandInfo &LCI) {
  InMemoryStruct<macho::Segment64LoadCommand> SLC;
  Obj.ReadSegment64LoadCommand(LCI, SLC);
  if (!SLC)
    return Error("unable to read segment load command");

  DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress, SLC->VMSize,
                         SLC->FileOffset, SLC->FileSize,
                         SLC->MaxVMProtection, SLC->InitialVMProtection,
                         SLC->NumSections, SLC->Flags);

  return 0;
}

static int DumpLoadCommand(MachOObject &Obj, unsigned Index) {
  const MachOObject::LoadCommandInfo &LCI = Obj.getLoadCommandInfo(Index);
  int Res = 0;

  outs() << "  # Load Command " << Index << "\n"
         << " (('command', " << LCI.Command.Type << ")\n"
         << "  ('size', " << LCI.Command.Size << ")\n";
  switch (LCI.Command.Type) {
  case macho::LCT_Segment:
    Res = DumpSegmentCommand(Obj, LCI);
    break;
  case macho::LCT_Segment64:
    Res = DumpSegment64Command(Obj, LCI);
    break;
  default:
    Warning("unknown load command: " + Twine(LCI.Command.Type));
    break;
  }
  outs() << " ),\n";

  return Res;
}

int main(int argc, char **argv) {
  ProgramName = argv[0];
  llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.

  cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n");

  // Load the input file.
  std::string ErrorStr;
  OwningPtr<MemoryBuffer> InputBuffer(
    MemoryBuffer::getFileOrSTDIN(InputFile, &ErrorStr));
  if (!InputBuffer)
    return Error("unable to read input: '" + ErrorStr + "'");

  // Construct the Mach-O wrapper object.
  OwningPtr<MachOObject> InputObject(
    MachOObject::LoadFromBuffer(InputBuffer.take(), &ErrorStr));
  if (!InputObject)
    return Error("unable to load object: '" + ErrorStr + "'");

  if (int Res = DumpHeader(*InputObject))
    return Res;

  // Print the load commands.
  int Res = 0;
  outs() << "('load_commands', [\n";
  for (unsigned i = 0; i != InputObject->getHeader().NumLoadCommands; ++i)
    if ((Res = DumpLoadCommand(*InputObject, i)))
      break;
  outs() << "])\n";

  return Res;
}