blob: 79a04c81a6f997ee9a859fc41f67ef78b5b0c225 [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
Alexey Samsonov48803e52014-03-13 14:37:36 +000043 void printRelocation(relocation_iterator RelI);
Eric Christopher9cad53c2013-04-03 18:31:38 +000044
Alexey Samsonov48803e52014-03-13 14:37:36 +000045 void printRelocation(const MachOObjectFile *Obj, relocation_iterator RelI);
Rafael Espindola9b709252013-04-13 01:45:40 +000046
Rafael Espindola56f976f2013-04-18 18:08:55 +000047 void printSections(const MachOObjectFile *Obj);
Rafael Espindola9b709252013-04-13 01:45:40 +000048
Rafael Espindola56f976f2013-04-18 18:08:55 +000049 const MachOObjectFile *Obj;
Eric Christopher9cad53c2013-04-03 18:31:38 +000050};
51
52} // namespace
53
54
55namespace llvm {
56
57error_code createMachODumper(const object::ObjectFile *Obj,
Ahmed Charles56440fd2014-03-06 05:51:42 +000058 StreamWriter &Writer,
59 std::unique_ptr<ObjDumper> &Result) {
Rafael Espindola56f976f2013-04-18 18:08:55 +000060 const MachOObjectFile *MachOObj = dyn_cast<MachOObjectFile>(Obj);
Eric Christopher9cad53c2013-04-03 18:31:38 +000061 if (!MachOObj)
62 return readobj_error::unsupported_obj_file_format;
63
64 Result.reset(new MachODumper(MachOObj, Writer));
65 return readobj_error::success;
66}
67
68} // namespace llvm
69
70
71static const EnumEntry<unsigned> MachOSectionTypes[] = {
72 { "Regular" , 0x00 },
73 { "ZeroFill" , 0x01 },
74 { "CStringLiterals" , 0x02 },
75 { "4ByteLiterals" , 0x03 },
76 { "8ByteLiterals" , 0x04 },
77 { "LiteralPointers" , 0x05 },
78 { "NonLazySymbolPointers" , 0x06 },
79 { "LazySymbolPointers" , 0x07 },
80 { "SymbolStubs" , 0x08 },
81 { "ModInitFuncs" , 0x09 },
82 { "ModTermFuncs" , 0x0A },
83 { "Coalesced" , 0x0B },
84 { "GBZeroFill" , 0x0C },
85 { "Interposing" , 0x0D },
86 { "16ByteLiterals" , 0x0E },
87 { "DTraceDOF" , 0x0F },
88 { "LazyDylibSymbolPoints" , 0x10 },
89 { "ThreadLocalRegular" , 0x11 },
90 { "ThreadLocalZerofill" , 0x12 },
91 { "ThreadLocalVariables" , 0x13 },
92 { "ThreadLocalVariablePointers" , 0x14 },
93 { "ThreadLocalInitFunctionPointers", 0x15 }
94};
95
96static const EnumEntry<unsigned> MachOSectionAttributes[] = {
97 { "LocReloc" , 1 << 0 /*S_ATTR_LOC_RELOC */ },
98 { "ExtReloc" , 1 << 1 /*S_ATTR_EXT_RELOC */ },
99 { "SomeInstructions" , 1 << 2 /*S_ATTR_SOME_INSTRUCTIONS */ },
100 { "Debug" , 1 << 17 /*S_ATTR_DEBUG */ },
101 { "SelfModifyingCode", 1 << 18 /*S_ATTR_SELF_MODIFYING_CODE*/ },
102 { "LiveSupport" , 1 << 19 /*S_ATTR_LIVE_SUPPORT */ },
103 { "NoDeadStrip" , 1 << 20 /*S_ATTR_NO_DEAD_STRIP */ },
104 { "StripStaticSyms" , 1 << 21 /*S_ATTR_STRIP_STATIC_SYMS */ },
105 { "NoTOC" , 1 << 22 /*S_ATTR_NO_TOC */ },
106 { "PureInstructions" , 1 << 23 /*S_ATTR_PURE_INSTRUCTIONS */ },
107};
108
109static const EnumEntry<unsigned> MachOSymbolRefTypes[] = {
110 { "UndefinedNonLazy", 0 },
111 { "ReferenceFlagUndefinedLazy", 1 },
112 { "ReferenceFlagDefined", 2 },
113 { "ReferenceFlagPrivateDefined", 3 },
114 { "ReferenceFlagPrivateUndefinedNonLazy", 4 },
115 { "ReferenceFlagPrivateUndefinedLazy", 5 }
116};
117
118static const EnumEntry<unsigned> MachOSymbolFlags[] = {
119 { "ReferencedDynamically", 0x10 },
120 { "NoDeadStrip", 0x20 },
121 { "WeakRef", 0x40 },
122 { "WeakDef", 0x80 }
123};
124
125static const EnumEntry<unsigned> MachOSymbolTypes[] = {
126 { "Undef", 0x0 },
Eric Christopher9cad53c2013-04-03 18:31:38 +0000127 { "Abs", 0x2 },
128 { "Indirect", 0xA },
129 { "PreboundUndef", 0xC },
Rafael Espindola1194e692014-03-06 20:13:41 +0000130 { "Section", 0xE }
Eric Christopher9cad53c2013-04-03 18:31:38 +0000131};
132
133namespace {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000134 struct MachOSection {
135 ArrayRef<char> Name;
136 ArrayRef<char> SegmentName;
137 uint64_t Address;
138 uint64_t Size;
139 uint32_t Offset;
140 uint32_t Alignment;
141 uint32_t RelocationTableOffset;
142 uint32_t NumRelocationTableEntries;
143 uint32_t Flags;
144 uint32_t Reserved1;
145 uint32_t Reserved2;
146 };
147
148 struct MachOSymbol {
149 uint32_t StringIndex;
150 uint8_t Type;
151 uint8_t SectionIndex;
152 uint16_t Flags;
153 uint64_t Value;
154 };
155}
156
Rafael Espindola56f976f2013-04-18 18:08:55 +0000157static void getSection(const MachOObjectFile *Obj,
158 DataRefImpl Sec,
Rafael Espindola9b709252013-04-13 01:45:40 +0000159 MachOSection &Section) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000160 if (!Obj->is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000161 MachO::section Sect = Obj->getSection(Sec);
162 Section.Address = Sect.addr;
163 Section.Size = Sect.size;
164 Section.Offset = Sect.offset;
165 Section.Alignment = Sect.align;
166 Section.RelocationTableOffset = Sect.reloff;
167 Section.NumRelocationTableEntries = Sect.nreloc;
168 Section.Flags = Sect.flags;
169 Section.Reserved1 = Sect.reserved1;
170 Section.Reserved2 = Sect.reserved2;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000171 return;
172 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000173 MachO::section_64 Sect = Obj->getSection64(Sec);
174 Section.Address = Sect.addr;
175 Section.Size = Sect.size;
176 Section.Offset = Sect.offset;
177 Section.Alignment = Sect.align;
178 Section.RelocationTableOffset = Sect.reloff;
179 Section.NumRelocationTableEntries = Sect.nreloc;
180 Section.Flags = Sect.flags;
181 Section.Reserved1 = Sect.reserved1;
182 Section.Reserved2 = Sect.reserved2;
Rafael Espindola9b709252013-04-13 01:45:40 +0000183}
184
Eric Christopher9cad53c2013-04-03 18:31:38 +0000185
Rafael Espindola56f976f2013-04-18 18:08:55 +0000186static void getSymbol(const MachOObjectFile *Obj,
Rafael Espindola9b709252013-04-13 01:45:40 +0000187 DataRefImpl DRI,
188 MachOSymbol &Symbol) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000189 if (!Obj->is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000190 MachO::nlist Entry = Obj->getSymbolTableEntry(DRI);
191 Symbol.StringIndex = Entry.n_strx;
192 Symbol.Type = Entry.n_type;
193 Symbol.SectionIndex = Entry.n_sect;
194 Symbol.Flags = Entry.n_desc;
195 Symbol.Value = Entry.n_value;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000196 return;
197 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000198 MachO::nlist_64 Entry = Obj->getSymbol64TableEntry(DRI);
199 Symbol.StringIndex = Entry.n_strx;
200 Symbol.Type = Entry.n_type;
201 Symbol.SectionIndex = Entry.n_sect;
202 Symbol.Flags = Entry.n_desc;
203 Symbol.Value = Entry.n_value;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000204}
205
206void MachODumper::printFileHeaders() {
207 W.startLine() << "FileHeaders not implemented.\n";
208}
209
210void MachODumper::printSections() {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000211 return printSections(Obj);
Rafael Espindola9b709252013-04-13 01:45:40 +0000212}
213
Rafael Espindola56f976f2013-04-18 18:08:55 +0000214void MachODumper::printSections(const MachOObjectFile *Obj) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000215 ListScope Group(W, "Sections");
216
217 int SectionIndex = -1;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000218 for (const SectionRef &Section : Obj->sections()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000219 ++SectionIndex;
220
Alexey Samsonov48803e52014-03-13 14:37:36 +0000221 MachOSection MOSection;
222 getSection(Obj, Section.getRawDataRefImpl(), MOSection);
223 DataRefImpl DR = Section.getRawDataRefImpl();
Rafael Espindolab0f76a42013-04-05 15:15:22 +0000224
Eric Christopher9cad53c2013-04-03 18:31:38 +0000225 StringRef Name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000226 if (error(Section.getName(Name)))
227 Name = "";
Eric Christopher9cad53c2013-04-03 18:31:38 +0000228
Rafael Espindolab0f76a42013-04-05 15:15:22 +0000229 ArrayRef<char> RawName = Obj->getSectionRawName(DR);
230 StringRef SegmentName = Obj->getSectionFinalSegmentName(DR);
231 ArrayRef<char> RawSegmentName = Obj->getSectionRawFinalSegmentName(DR);
232
Eric Christopher9cad53c2013-04-03 18:31:38 +0000233 DictScope SectionD(W, "Section");
234 W.printNumber("Index", SectionIndex);
Rafael Espindolab0f76a42013-04-05 15:15:22 +0000235 W.printBinary("Name", Name, RawName);
236 W.printBinary("Segment", SegmentName, RawSegmentName);
Alexey Samsonov48803e52014-03-13 14:37:36 +0000237 W.printHex("Address", MOSection.Address);
238 W.printHex("Size", MOSection.Size);
239 W.printNumber("Offset", MOSection.Offset);
240 W.printNumber("Alignment", MOSection.Alignment);
241 W.printHex("RelocationOffset", MOSection.RelocationTableOffset);
242 W.printNumber("RelocationCount", MOSection.NumRelocationTableEntries);
243 W.printEnum("Type", MOSection.Flags & 0xFF,
244 makeArrayRef(MachOSectionAttributes));
245 W.printFlags("Attributes", MOSection.Flags >> 8,
246 makeArrayRef(MachOSectionAttributes));
247 W.printHex("Reserved1", MOSection.Reserved1);
248 W.printHex("Reserved2", MOSection.Reserved2);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000249
250 if (opts::SectionRelocations) {
251 ListScope D(W, "Relocations");
Alexey Samsonov48803e52014-03-13 14:37:36 +0000252 for (relocation_iterator RelI = Section.relocation_begin(),
253 RelE = Section.relocation_end();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000254 RelI != RelE; ++RelI)
Alexey Samsonov48803e52014-03-13 14:37:36 +0000255 printRelocation(RelI);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000256 }
257
258 if (opts::SectionSymbols) {
259 ListScope D(W, "Symbols");
Alexey Samsonov48803e52014-03-13 14:37:36 +0000260 for (symbol_iterator SymI = Obj->symbol_begin(), SymE = Obj->symbol_end();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000261 SymI != SymE; ++SymI) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000262 bool Contained = false;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000263 if (Section.containsSymbol(*SymI, Contained) || !Contained)
Eric Christopher9cad53c2013-04-03 18:31:38 +0000264 continue;
265
266 printSymbol(SymI);
267 }
268 }
269
270 if (opts::SectionData) {
271 StringRef Data;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000272 if (error(Section.getContents(Data)))
273 break;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000274
275 W.printBinaryBlock("SectionData", Data);
276 }
277 }
278}
279
280void MachODumper::printRelocations() {
281 ListScope D(W, "Relocations");
282
283 error_code EC;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000284 for (const SectionRef &Section : Obj->sections()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000285 StringRef Name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000286 if (error(Section.getName(Name)))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000287 continue;
288
289 bool PrintedGroup = false;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000290 for (relocation_iterator RelI = Section.relocation_begin(),
291 RelE = Section.relocation_end();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000292 RelI != RelE; ++RelI) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000293 if (!PrintedGroup) {
294 W.startLine() << "Section " << Name << " {\n";
295 W.indent();
296 PrintedGroup = true;
297 }
298
Alexey Samsonov48803e52014-03-13 14:37:36 +0000299 printRelocation(RelI);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000300 }
301
302 if (PrintedGroup) {
303 W.unindent();
304 W.startLine() << "}\n";
305 }
306 }
307}
308
Alexey Samsonov48803e52014-03-13 14:37:36 +0000309void MachODumper::printRelocation(relocation_iterator RelI) {
310 return printRelocation(Obj, RelI);
Rafael Espindola9b709252013-04-13 01:45:40 +0000311}
312
Rafael Espindola56f976f2013-04-18 18:08:55 +0000313void MachODumper::printRelocation(const MachOObjectFile *Obj,
Rafael Espindola9b709252013-04-13 01:45:40 +0000314 relocation_iterator RelI) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000315 uint64_t Offset;
316 SmallString<32> RelocName;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000317 StringRef SymbolName;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000318 if (error(RelI->getOffset(Offset))) return;
319 if (error(RelI->getTypeName(RelocName))) return;
Rafael Espindola806f0062013-06-05 01:33:53 +0000320 symbol_iterator Symbol = RelI->getSymbol();
Rafael Espindolab5155a52014-02-10 20:24:04 +0000321 if (Symbol != Obj->symbol_end() &&
Rafael Espindola806f0062013-06-05 01:33:53 +0000322 error(Symbol->getName(SymbolName)))
Rafael Espindola75c30362013-04-24 19:47:55 +0000323 return;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000324
Rafael Espindolaecf13202013-04-12 00:17:33 +0000325 DataRefImpl DR = RelI->getRawDataRefImpl();
Charles Davis8bdfafd2013-09-01 04:28:48 +0000326 MachO::any_relocation_info RE = Obj->getRelocation(DR);
Rafael Espindola9b709252013-04-13 01:45:40 +0000327 bool IsScattered = Obj->isRelocationScattered(RE);
Rafael Espindolaecf13202013-04-12 00:17:33 +0000328
Nico Rieckf3f0b792013-04-12 04:01:52 +0000329 if (opts::ExpandRelocs) {
330 DictScope Group(W, "Relocation");
331 W.printHex("Offset", Offset);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000332 W.printNumber("PCRel", Obj->getAnyRelocationPCRel(RE));
333 W.printNumber("Length", Obj->getAnyRelocationLength(RE));
Nico Rieckf3f0b792013-04-12 04:01:52 +0000334 if (IsScattered)
335 W.printString("Extern", StringRef("N/A"));
336 else
Rafael Espindola56f976f2013-04-18 18:08:55 +0000337 W.printNumber("Extern", Obj->getPlainRelocationExternal(RE));
338 W.printNumber("Type", RelocName, Obj->getAnyRelocationType(RE));
Nico Rieckf3f0b792013-04-12 04:01:52 +0000339 W.printString("Symbol", SymbolName.size() > 0 ? SymbolName : "-");
340 W.printNumber("Scattered", IsScattered);
341 } else {
342 raw_ostream& OS = W.startLine();
343 OS << W.hex(Offset)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000344 << " " << Obj->getAnyRelocationPCRel(RE)
345 << " " << Obj->getAnyRelocationLength(RE);
Nico Rieckf3f0b792013-04-12 04:01:52 +0000346 if (IsScattered)
347 OS << " n/a";
348 else
Rafael Espindola56f976f2013-04-18 18:08:55 +0000349 OS << " " << Obj->getPlainRelocationExternal(RE);
Nico Rieckf3f0b792013-04-12 04:01:52 +0000350 OS << " " << RelocName
351 << " " << IsScattered
352 << " " << (SymbolName.size() > 0 ? SymbolName : "-")
353 << "\n";
354 }
Eric Christopher9cad53c2013-04-03 18:31:38 +0000355}
356
357void MachODumper::printSymbols() {
358 ListScope Group(W, "Symbols");
359
Rafael Espindolab5155a52014-02-10 20:24:04 +0000360 for (symbol_iterator SymI = Obj->symbol_begin(), SymE = Obj->symbol_end();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000361 SymI != SymE; ++SymI) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000362 printSymbol(SymI);
363 }
364}
365
366void MachODumper::printDynamicSymbols() {
367 ListScope Group(W, "DynamicSymbols");
368}
369
370void MachODumper::printSymbol(symbol_iterator SymI) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000371 StringRef SymbolName;
372 if (SymI->getName(SymbolName))
373 SymbolName = "";
374
Eric Christopher9cad53c2013-04-03 18:31:38 +0000375 MachOSymbol Symbol;
Rafael Espindola79bb5502013-04-07 15:35:18 +0000376 getSymbol(Obj, SymI->getRawDataRefImpl(), Symbol);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000377
Nico Riecka8de6532013-04-22 08:34:46 +0000378 StringRef SectionName = "";
Rafael Espindolab5155a52014-02-10 20:24:04 +0000379 section_iterator SecI(Obj->section_begin());
Nico Riecka8de6532013-04-22 08:34:46 +0000380 if (!error(SymI->getSection(SecI)) &&
Rafael Espindolab5155a52014-02-10 20:24:04 +0000381 SecI != Obj->section_end())
Nico Riecka8de6532013-04-22 08:34:46 +0000382 error(SecI->getName(SectionName));
Eric Christopher9cad53c2013-04-03 18:31:38 +0000383
384 DictScope D(W, "Symbol");
385 W.printNumber("Name", SymbolName, Symbol.StringIndex);
Rafael Espindolac68b0f72014-03-06 19:58:56 +0000386 if (Symbol.Type & MachO::N_STAB) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000387 W.printHex ("Type", "SymDebugTable", Symbol.Type);
388 } else {
Rafael Espindola1194e692014-03-06 20:13:41 +0000389 if (Symbol.Type & MachO::N_PEXT)
390 W.startLine() << "PrivateExtern\n";
391 if (Symbol.Type & MachO::N_EXT)
392 W.startLine() << "Extern\n";
Rafael Espindola39a09652014-03-06 20:16:24 +0000393 W.printEnum("Type", uint8_t(Symbol.Type & MachO::N_TYPE),
Rafael Espindola1194e692014-03-06 20:13:41 +0000394 makeArrayRef(MachOSymbolTypes));
Eric Christopher9cad53c2013-04-03 18:31:38 +0000395 }
396 W.printHex ("Section", SectionName, Symbol.SectionIndex);
397 W.printEnum ("RefType", static_cast<uint16_t>(Symbol.Flags & 0xF),
398 makeArrayRef(MachOSymbolRefTypes));
399 W.printFlags ("Flags", static_cast<uint16_t>(Symbol.Flags & ~0xF),
400 makeArrayRef(MachOSymbolFlags));
401 W.printHex ("Value", Symbol.Value);
402}
403
404void MachODumper::printUnwindInfo() {
405 W.startLine() << "UnwindInfo not implemented.\n";
406}