macho-dump: Add support for dumping segment load commands.
[oota-llvm.git] / tools / macho-dump / macho-dump.cpp
1 //===-- macho-dump.cpp - Mach Object Dumping Tool -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This is a testing tool for use with the MC/Mach-O LLVM components.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Object/MachOObject.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/ManagedStatic.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/raw_ostream.h"
20 using namespace llvm;
21 using namespace llvm::object;
22
23 static cl::opt<std::string>
24 InputFile(cl::Positional, cl::desc("<input file>"), cl::init("-"));
25
26 static cl::opt<bool>
27 DumpSectionData("dump-section-data", cl::desc("Dump the contents of sections"),
28                 cl::init(false));
29
30 ///
31
32 static const char *ProgramName;
33
34 static void Message(const char *Type, const Twine &Msg) {
35   errs() << ProgramName << ": " << Type << ": " << Msg << "\n";
36 }
37
38 static int Error(const Twine &Msg) {
39   Message("error", Msg);
40   return 1;
41 }
42
43 static void Warning(const Twine &Msg) {
44   Message("warning", Msg);
45 }
46
47 ///
48
49 static int DumpHeader(MachOObject &Obj) {
50   // Read the header.
51   const macho::Header &Hdr = Obj.getHeader();
52   outs() << "('cputype', " << Hdr.CPUType << ")\n";
53   outs() << "('cpusubtype', " << Hdr.CPUSubtype << ")\n";
54   outs() << "('filetype', " << Hdr.FileType << ")\n";
55   outs() << "('num_load_commands', " << Hdr.NumLoadCommands << ")\n";
56   outs() << "('load_commands_size', " << Hdr.SizeOfLoadCommands << ")\n";
57   outs() << "('flag', " << Hdr.Flags << ")\n";
58
59   // Print extended header if 64-bit.
60   if (Obj.is64Bit()) {
61     const macho::Header64Ext &Hdr64 = Obj.getHeader64Ext();
62     outs() << "('reserved', " << Hdr64.Reserved << ")\n";
63   }
64
65   return 0;
66 }
67
68 static void DumpSegmentCommandData(StringRef Name,
69                                    uint64_t VMAddr, uint64_t VMSize,
70                                    uint64_t FileOffset, uint64_t FileSize,
71                                    uint32_t MaxProt, uint32_t InitProt,
72                                    uint32_t NumSections, uint32_t Flags) {
73   outs() << "  ('segment_name', '";
74   outs().write_escaped(Name, /*UseHexEscapes=*/true) << "')\n";
75   outs() << "  ('vm_addr', " << VMAddr << ")\n";
76   outs() << "  ('vm_size', " << VMSize << ")\n";
77   outs() << "  ('file_offset', " << FileOffset << ")\n";
78   outs() << "  ('file_size', " << FileSize << ")\n";
79   outs() << "  ('maxprot', " << MaxProt << ")\n";
80   outs() << "  ('initprot', " << InitProt << ")\n";
81   outs() << "  ('num_sections', " << NumSections << ")\n";
82   outs() << "  ('flags', " << Flags << ")\n";
83 }
84
85 static int DumpSegmentCommand(MachOObject &Obj,
86                                const MachOObject::LoadCommandInfo &LCI) {
87   InMemoryStruct<macho::SegmentLoadCommand> SLC;
88   Obj.ReadSegmentLoadCommand(LCI, SLC);
89   if (!SLC)
90     return Error("unable to read segment load command");
91
92   DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress, SLC->VMSize,
93                          SLC->FileOffset, SLC->FileSize,
94                          SLC->MaxVMProtection, SLC->InitialVMProtection,
95                          SLC->NumSections, SLC->Flags);
96
97   return 0;
98 }
99 static int DumpSegment64Command(MachOObject &Obj,
100                                const MachOObject::LoadCommandInfo &LCI) {
101   InMemoryStruct<macho::Segment64LoadCommand> SLC;
102   Obj.ReadSegment64LoadCommand(LCI, SLC);
103   if (!SLC)
104     return Error("unable to read segment load command");
105
106   DumpSegmentCommandData(StringRef(SLC->Name, 16), SLC->VMAddress, SLC->VMSize,
107                          SLC->FileOffset, SLC->FileSize,
108                          SLC->MaxVMProtection, SLC->InitialVMProtection,
109                          SLC->NumSections, SLC->Flags);
110
111   return 0;
112 }
113
114 static int DumpLoadCommand(MachOObject &Obj, unsigned Index) {
115   const MachOObject::LoadCommandInfo &LCI = Obj.getLoadCommandInfo(Index);
116   int Res = 0;
117
118   outs() << "  # Load Command " << Index << "\n"
119          << " (('command', " << LCI.Command.Type << ")\n"
120          << "  ('size', " << LCI.Command.Size << ")\n";
121   switch (LCI.Command.Type) {
122   case macho::LCT_Segment:
123     Res = DumpSegmentCommand(Obj, LCI);
124     break;
125   case macho::LCT_Segment64:
126     Res = DumpSegment64Command(Obj, LCI);
127     break;
128   default:
129     Warning("unknown load command: " + Twine(LCI.Command.Type));
130     break;
131   }
132   outs() << " ),\n";
133
134   return Res;
135 }
136
137 int main(int argc, char **argv) {
138   ProgramName = argv[0];
139   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
140
141   cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n");
142
143   // Load the input file.
144   std::string ErrorStr;
145   OwningPtr<MemoryBuffer> InputBuffer(
146     MemoryBuffer::getFileOrSTDIN(InputFile, &ErrorStr));
147   if (!InputBuffer)
148     return Error("unable to read input: '" + ErrorStr + "'");
149
150   // Construct the Mach-O wrapper object.
151   OwningPtr<MachOObject> InputObject(
152     MachOObject::LoadFromBuffer(InputBuffer.take(), &ErrorStr));
153   if (!InputObject)
154     return Error("unable to load object: '" + ErrorStr + "'");
155
156   if (int Res = DumpHeader(*InputObject))
157     return Res;
158
159   // Print the load commands.
160   int Res = 0;
161   outs() << "('load_commands', [\n";
162   for (unsigned i = 0; i != InputObject->getHeader().NumLoadCommands; ++i)
163     if ((Res = DumpLoadCommand(*InputObject, i)))
164       break;
165   outs() << "])\n";
166
167   return Res;
168 }