blob: 6077334de2c9f8745c8361aa5eb4af46f20e798d [file] [log] [blame]
Eric Christopher9cad53c2013-04-03 18:31:38 +00001//===-- MachODump.cpp - Object file dumping utility for llvm --------------===//
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 file implements the MachO-specific dumper for llvm-readobj.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm-readobj.h"
15#include "Error.h"
16#include "ObjDumper.h"
17#include "StreamWriter.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000018#include "llvm/ADT/SmallString.h"
19#include "llvm/Object/MachO.h"
20#include "llvm/Support/Casting.h"
21
22using namespace llvm;
23using namespace object;
24
25namespace {
26
27class MachODumper : public ObjDumper {
28public:
Rafael Espindola56f976f2013-04-18 18:08:55 +000029 MachODumper(const MachOObjectFile *Obj, StreamWriter& Writer)
Eric Christopher9cad53c2013-04-03 18:31:38 +000030 : ObjDumper(Writer)
31 , Obj(Obj) { }
32
Craig Topper73156022014-03-02 09:09:27 +000033 virtual void printFileHeaders() override;
34 virtual void printSections() override;
35 virtual void printRelocations() override;
36 virtual void printSymbols() override;
37 virtual void printDynamicSymbols() override;
38 virtual void printUnwindInfo() override;
Eric Christopher9cad53c2013-04-03 18:31:38 +000039
40private:
41 void printSymbol(symbol_iterator SymI);
42
43 void printRelocation(section_iterator SecI, relocation_iterator RelI);
44
Rafael Espindola56f976f2013-04-18 18:08:55 +000045 void printRelocation(const MachOObjectFile *Obj,
Rafael Espindola9b709252013-04-13 01:45:40 +000046 section_iterator SecI, relocation_iterator RelI);
47
Rafael Espindola56f976f2013-04-18 18:08:55 +000048 void printSections(const MachOObjectFile *Obj);
Rafael Espindola9b709252013-04-13 01:45:40 +000049
Rafael Espindola56f976f2013-04-18 18:08:55 +000050 const MachOObjectFile *Obj;
Eric Christopher9cad53c2013-04-03 18:31:38 +000051};
52
53} // namespace
54
55
56namespace llvm {
57
58error_code createMachODumper(const object::ObjectFile *Obj,
Ahmed Charles56440fd2014-03-06 05:51:42 +000059 StreamWriter &Writer,
60 std::unique_ptr<ObjDumper> &Result) {
Rafael Espindola56f976f2013-04-18 18:08:55 +000061 const MachOObjectFile *MachOObj = dyn_cast<MachOObjectFile>(Obj);
Eric Christopher9cad53c2013-04-03 18:31:38 +000062 if (!MachOObj)
63 return readobj_error::unsupported_obj_file_format;
64
65 Result.reset(new MachODumper(MachOObj, Writer));
66 return readobj_error::success;
67}
68
69} // namespace llvm
70
71
72static const EnumEntry<unsigned> MachOSectionTypes[] = {
73 { "Regular" , 0x00 },
74 { "ZeroFill" , 0x01 },
75 { "CStringLiterals" , 0x02 },
76 { "4ByteLiterals" , 0x03 },
77 { "8ByteLiterals" , 0x04 },
78 { "LiteralPointers" , 0x05 },
79 { "NonLazySymbolPointers" , 0x06 },
80 { "LazySymbolPointers" , 0x07 },
81 { "SymbolStubs" , 0x08 },
82 { "ModInitFuncs" , 0x09 },
83 { "ModTermFuncs" , 0x0A },
84 { "Coalesced" , 0x0B },
85 { "GBZeroFill" , 0x0C },
86 { "Interposing" , 0x0D },
87 { "16ByteLiterals" , 0x0E },
88 { "DTraceDOF" , 0x0F },
89 { "LazyDylibSymbolPoints" , 0x10 },
90 { "ThreadLocalRegular" , 0x11 },
91 { "ThreadLocalZerofill" , 0x12 },
92 { "ThreadLocalVariables" , 0x13 },
93 { "ThreadLocalVariablePointers" , 0x14 },
94 { "ThreadLocalInitFunctionPointers", 0x15 }
95};
96
97static const EnumEntry<unsigned> MachOSectionAttributes[] = {
98 { "LocReloc" , 1 << 0 /*S_ATTR_LOC_RELOC */ },
99 { "ExtReloc" , 1 << 1 /*S_ATTR_EXT_RELOC */ },
100 { "SomeInstructions" , 1 << 2 /*S_ATTR_SOME_INSTRUCTIONS */ },
101 { "Debug" , 1 << 17 /*S_ATTR_DEBUG */ },
102 { "SelfModifyingCode", 1 << 18 /*S_ATTR_SELF_MODIFYING_CODE*/ },
103 { "LiveSupport" , 1 << 19 /*S_ATTR_LIVE_SUPPORT */ },
104 { "NoDeadStrip" , 1 << 20 /*S_ATTR_NO_DEAD_STRIP */ },
105 { "StripStaticSyms" , 1 << 21 /*S_ATTR_STRIP_STATIC_SYMS */ },
106 { "NoTOC" , 1 << 22 /*S_ATTR_NO_TOC */ },
107 { "PureInstructions" , 1 << 23 /*S_ATTR_PURE_INSTRUCTIONS */ },
108};
109
110static const EnumEntry<unsigned> MachOSymbolRefTypes[] = {
111 { "UndefinedNonLazy", 0 },
112 { "ReferenceFlagUndefinedLazy", 1 },
113 { "ReferenceFlagDefined", 2 },
114 { "ReferenceFlagPrivateDefined", 3 },
115 { "ReferenceFlagPrivateUndefinedNonLazy", 4 },
116 { "ReferenceFlagPrivateUndefinedLazy", 5 }
117};
118
119static const EnumEntry<unsigned> MachOSymbolFlags[] = {
120 { "ReferencedDynamically", 0x10 },
121 { "NoDeadStrip", 0x20 },
122 { "WeakRef", 0x40 },
123 { "WeakDef", 0x80 }
124};
125
126static const EnumEntry<unsigned> MachOSymbolTypes[] = {
127 { "Undef", 0x0 },
128 { "External", 0x1 },
129 { "Abs", 0x2 },
130 { "Indirect", 0xA },
131 { "PreboundUndef", 0xC },
132 { "Section", 0xE },
133 { "PrivateExternal", 0x10 }
134};
135
136namespace {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000137 struct MachOSection {
138 ArrayRef<char> Name;
139 ArrayRef<char> SegmentName;
140 uint64_t Address;
141 uint64_t Size;
142 uint32_t Offset;
143 uint32_t Alignment;
144 uint32_t RelocationTableOffset;
145 uint32_t NumRelocationTableEntries;
146 uint32_t Flags;
147 uint32_t Reserved1;
148 uint32_t Reserved2;
149 };
150
151 struct MachOSymbol {
152 uint32_t StringIndex;
153 uint8_t Type;
154 uint8_t SectionIndex;
155 uint16_t Flags;
156 uint64_t Value;
157 };
158}
159
Rafael Espindola56f976f2013-04-18 18:08:55 +0000160static void getSection(const MachOObjectFile *Obj,
161 DataRefImpl Sec,
Rafael Espindola9b709252013-04-13 01:45:40 +0000162 MachOSection &Section) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000163 if (!Obj->is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000164 MachO::section Sect = Obj->getSection(Sec);
165 Section.Address = Sect.addr;
166 Section.Size = Sect.size;
167 Section.Offset = Sect.offset;
168 Section.Alignment = Sect.align;
169 Section.RelocationTableOffset = Sect.reloff;
170 Section.NumRelocationTableEntries = Sect.nreloc;
171 Section.Flags = Sect.flags;
172 Section.Reserved1 = Sect.reserved1;
173 Section.Reserved2 = Sect.reserved2;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000174 return;
175 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000176 MachO::section_64 Sect = Obj->getSection64(Sec);
177 Section.Address = Sect.addr;
178 Section.Size = Sect.size;
179 Section.Offset = Sect.offset;
180 Section.Alignment = Sect.align;
181 Section.RelocationTableOffset = Sect.reloff;
182 Section.NumRelocationTableEntries = Sect.nreloc;
183 Section.Flags = Sect.flags;
184 Section.Reserved1 = Sect.reserved1;
185 Section.Reserved2 = Sect.reserved2;
Rafael Espindola9b709252013-04-13 01:45:40 +0000186}
187
Eric Christopher9cad53c2013-04-03 18:31:38 +0000188
Rafael Espindola56f976f2013-04-18 18:08:55 +0000189static void getSymbol(const MachOObjectFile *Obj,
Rafael Espindola9b709252013-04-13 01:45:40 +0000190 DataRefImpl DRI,
191 MachOSymbol &Symbol) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000192 if (!Obj->is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000193 MachO::nlist Entry = Obj->getSymbolTableEntry(DRI);
194 Symbol.StringIndex = Entry.n_strx;
195 Symbol.Type = Entry.n_type;
196 Symbol.SectionIndex = Entry.n_sect;
197 Symbol.Flags = Entry.n_desc;
198 Symbol.Value = Entry.n_value;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000199 return;
200 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000201 MachO::nlist_64 Entry = Obj->getSymbol64TableEntry(DRI);
202 Symbol.StringIndex = Entry.n_strx;
203 Symbol.Type = Entry.n_type;
204 Symbol.SectionIndex = Entry.n_sect;
205 Symbol.Flags = Entry.n_desc;
206 Symbol.Value = Entry.n_value;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000207}
208
209void MachODumper::printFileHeaders() {
210 W.startLine() << "FileHeaders not implemented.\n";
211}
212
213void MachODumper::printSections() {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000214 return printSections(Obj);
Rafael Espindola9b709252013-04-13 01:45:40 +0000215}
216
Rafael Espindola56f976f2013-04-18 18:08:55 +0000217void MachODumper::printSections(const MachOObjectFile *Obj) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000218 ListScope Group(W, "Sections");
219
220 int SectionIndex = -1;
Rafael Espindolab5155a52014-02-10 20:24:04 +0000221 for (section_iterator SecI = Obj->section_begin(),
222 SecE = Obj->section_end();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000223 SecI != SecE; ++SecI) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000224 ++SectionIndex;
225
Eric Christopher9cad53c2013-04-03 18:31:38 +0000226 MachOSection Section;
Rafael Espindola6f5d6c72013-04-07 15:05:12 +0000227 getSection(Obj, SecI->getRawDataRefImpl(), Section);
Rafael Espindolab0f76a42013-04-05 15:15:22 +0000228 DataRefImpl DR = SecI->getRawDataRefImpl();
229
Eric Christopher9cad53c2013-04-03 18:31:38 +0000230 StringRef Name;
231 if (error(SecI->getName(Name)))
232 Name = "";
233
Rafael Espindolab0f76a42013-04-05 15:15:22 +0000234 ArrayRef<char> RawName = Obj->getSectionRawName(DR);
235 StringRef SegmentName = Obj->getSectionFinalSegmentName(DR);
236 ArrayRef<char> RawSegmentName = Obj->getSectionRawFinalSegmentName(DR);
237
Eric Christopher9cad53c2013-04-03 18:31:38 +0000238 DictScope SectionD(W, "Section");
239 W.printNumber("Index", SectionIndex);
Rafael Espindolab0f76a42013-04-05 15:15:22 +0000240 W.printBinary("Name", Name, RawName);
241 W.printBinary("Segment", SegmentName, RawSegmentName);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000242 W.printHex ("Address", Section.Address);
243 W.printHex ("Size", Section.Size);
244 W.printNumber("Offset", Section.Offset);
245 W.printNumber("Alignment", Section.Alignment);
246 W.printHex ("RelocationOffset", Section.RelocationTableOffset);
247 W.printNumber("RelocationCount", Section.NumRelocationTableEntries);
248 W.printEnum ("Type", Section.Flags & 0xFF,
249 makeArrayRef(MachOSectionAttributes));
250 W.printFlags ("Attributes", Section.Flags >> 8,
251 makeArrayRef(MachOSectionAttributes));
252 W.printHex ("Reserved1", Section.Reserved1);
253 W.printHex ("Reserved2", Section.Reserved2);
254
255 if (opts::SectionRelocations) {
256 ListScope D(W, "Relocations");
Rafael Espindolab5155a52014-02-10 20:24:04 +0000257 for (relocation_iterator RelI = SecI->relocation_begin(),
258 RelE = SecI->relocation_end();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000259 RelI != RelE; ++RelI)
Eric Christopher9cad53c2013-04-03 18:31:38 +0000260 printRelocation(SecI, RelI);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000261 }
262
263 if (opts::SectionSymbols) {
264 ListScope D(W, "Symbols");
Rafael Espindolab5155a52014-02-10 20:24:04 +0000265 for (symbol_iterator SymI = Obj->symbol_begin(),
266 SymE = Obj->symbol_end();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000267 SymI != SymE; ++SymI) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000268 bool Contained = false;
269 if (SecI->containsSymbol(*SymI, Contained) || !Contained)
270 continue;
271
272 printSymbol(SymI);
273 }
274 }
275
276 if (opts::SectionData) {
277 StringRef Data;
278 if (error(SecI->getContents(Data))) break;
279
280 W.printBinaryBlock("SectionData", Data);
281 }
282 }
283}
284
285void MachODumper::printRelocations() {
286 ListScope D(W, "Relocations");
287
288 error_code EC;
Rafael Espindolab5155a52014-02-10 20:24:04 +0000289 for (section_iterator SecI = Obj->section_begin(),
290 SecE = Obj->section_end();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000291 SecI != SecE; ++SecI) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000292 StringRef Name;
293 if (error(SecI->getName(Name)))
294 continue;
295
296 bool PrintedGroup = false;
Rafael Espindolab5155a52014-02-10 20:24:04 +0000297 for (relocation_iterator RelI = SecI->relocation_begin(),
298 RelE = SecI->relocation_end();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000299 RelI != RelE; ++RelI) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000300 if (!PrintedGroup) {
301 W.startLine() << "Section " << Name << " {\n";
302 W.indent();
303 PrintedGroup = true;
304 }
305
306 printRelocation(SecI, RelI);
307 }
308
309 if (PrintedGroup) {
310 W.unindent();
311 W.startLine() << "}\n";
312 }
313 }
314}
315
316void MachODumper::printRelocation(section_iterator SecI,
317 relocation_iterator RelI) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000318 return printRelocation(Obj, SecI, RelI);
Rafael Espindola9b709252013-04-13 01:45:40 +0000319}
320
Rafael Espindola56f976f2013-04-18 18:08:55 +0000321void MachODumper::printRelocation(const MachOObjectFile *Obj,
Rafael Espindola9b709252013-04-13 01:45:40 +0000322 section_iterator SecI,
323 relocation_iterator RelI) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000324 uint64_t Offset;
325 SmallString<32> RelocName;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000326 StringRef SymbolName;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000327 if (error(RelI->getOffset(Offset))) return;
328 if (error(RelI->getTypeName(RelocName))) return;
Rafael Espindola806f0062013-06-05 01:33:53 +0000329 symbol_iterator Symbol = RelI->getSymbol();
Rafael Espindolab5155a52014-02-10 20:24:04 +0000330 if (Symbol != Obj->symbol_end() &&
Rafael Espindola806f0062013-06-05 01:33:53 +0000331 error(Symbol->getName(SymbolName)))
Rafael Espindola75c30362013-04-24 19:47:55 +0000332 return;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000333
Rafael Espindolaecf13202013-04-12 00:17:33 +0000334 DataRefImpl DR = RelI->getRawDataRefImpl();
Charles Davis8bdfafd2013-09-01 04:28:48 +0000335 MachO::any_relocation_info RE = Obj->getRelocation(DR);
Rafael Espindola9b709252013-04-13 01:45:40 +0000336 bool IsScattered = Obj->isRelocationScattered(RE);
Rafael Espindolaecf13202013-04-12 00:17:33 +0000337
Nico Rieckf3f0b792013-04-12 04:01:52 +0000338 if (opts::ExpandRelocs) {
339 DictScope Group(W, "Relocation");
340 W.printHex("Offset", Offset);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000341 W.printNumber("PCRel", Obj->getAnyRelocationPCRel(RE));
342 W.printNumber("Length", Obj->getAnyRelocationLength(RE));
Nico Rieckf3f0b792013-04-12 04:01:52 +0000343 if (IsScattered)
344 W.printString("Extern", StringRef("N/A"));
345 else
Rafael Espindola56f976f2013-04-18 18:08:55 +0000346 W.printNumber("Extern", Obj->getPlainRelocationExternal(RE));
347 W.printNumber("Type", RelocName, Obj->getAnyRelocationType(RE));
Nico Rieckf3f0b792013-04-12 04:01:52 +0000348 W.printString("Symbol", SymbolName.size() > 0 ? SymbolName : "-");
349 W.printNumber("Scattered", IsScattered);
350 } else {
351 raw_ostream& OS = W.startLine();
352 OS << W.hex(Offset)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000353 << " " << Obj->getAnyRelocationPCRel(RE)
354 << " " << Obj->getAnyRelocationLength(RE);
Nico Rieckf3f0b792013-04-12 04:01:52 +0000355 if (IsScattered)
356 OS << " n/a";
357 else
Rafael Espindola56f976f2013-04-18 18:08:55 +0000358 OS << " " << Obj->getPlainRelocationExternal(RE);
Nico Rieckf3f0b792013-04-12 04:01:52 +0000359 OS << " " << RelocName
360 << " " << IsScattered
361 << " " << (SymbolName.size() > 0 ? SymbolName : "-")
362 << "\n";
363 }
Eric Christopher9cad53c2013-04-03 18:31:38 +0000364}
365
366void MachODumper::printSymbols() {
367 ListScope Group(W, "Symbols");
368
Rafael Espindolab5155a52014-02-10 20:24:04 +0000369 for (symbol_iterator SymI = Obj->symbol_begin(), SymE = Obj->symbol_end();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000370 SymI != SymE; ++SymI) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000371 printSymbol(SymI);
372 }
373}
374
375void MachODumper::printDynamicSymbols() {
376 ListScope Group(W, "DynamicSymbols");
377}
378
379void MachODumper::printSymbol(symbol_iterator SymI) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000380 StringRef SymbolName;
381 if (SymI->getName(SymbolName))
382 SymbolName = "";
383
Eric Christopher9cad53c2013-04-03 18:31:38 +0000384 MachOSymbol Symbol;
Rafael Espindola79bb5502013-04-07 15:35:18 +0000385 getSymbol(Obj, SymI->getRawDataRefImpl(), Symbol);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000386
Nico Riecka8de6532013-04-22 08:34:46 +0000387 StringRef SectionName = "";
Rafael Espindolab5155a52014-02-10 20:24:04 +0000388 section_iterator SecI(Obj->section_begin());
Nico Riecka8de6532013-04-22 08:34:46 +0000389 if (!error(SymI->getSection(SecI)) &&
Rafael Espindolab5155a52014-02-10 20:24:04 +0000390 SecI != Obj->section_end())
Nico Riecka8de6532013-04-22 08:34:46 +0000391 error(SecI->getName(SectionName));
Eric Christopher9cad53c2013-04-03 18:31:38 +0000392
393 DictScope D(W, "Symbol");
394 W.printNumber("Name", SymbolName, Symbol.StringIndex);
Rafael Espindolac68b0f72014-03-06 19:58:56 +0000395 if (Symbol.Type & MachO::N_STAB) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000396 W.printHex ("Type", "SymDebugTable", Symbol.Type);
397 } else {
398 W.printEnum("Type", Symbol.Type, makeArrayRef(MachOSymbolTypes));
399 }
400 W.printHex ("Section", SectionName, Symbol.SectionIndex);
401 W.printEnum ("RefType", static_cast<uint16_t>(Symbol.Flags & 0xF),
402 makeArrayRef(MachOSymbolRefTypes));
403 W.printFlags ("Flags", static_cast<uint16_t>(Symbol.Flags & ~0xF),
404 makeArrayRef(MachOSymbolFlags));
405 W.printHex ("Value", Symbol.Value);
406}
407
408void MachODumper::printUnwindInfo() {
409 W.startLine() << "UnwindInfo not implemented.\n";
410}