blob: 968d5deca15e76af255e790917a213b869a2e499 [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"
Tim Northover07f99fb2014-07-04 10:57:56 +000019#include "llvm/ADT/StringExtras.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000020#include "llvm/Object/MachO.h"
21#include "llvm/Support/Casting.h"
22
23using namespace llvm;
24using namespace object;
25
26namespace {
27
28class MachODumper : public ObjDumper {
29public:
Rafael Espindola56f976f2013-04-18 18:08:55 +000030 MachODumper(const MachOObjectFile *Obj, StreamWriter& Writer)
Eric Christopher9cad53c2013-04-03 18:31:38 +000031 : ObjDumper(Writer)
32 , Obj(Obj) { }
33
Craig Topperfd38cbe2014-08-30 16:48:34 +000034 void printFileHeaders() override;
35 void printSections() override;
36 void printRelocations() override;
37 void printSymbols() override;
38 void printDynamicSymbols() override;
39 void printUnwindInfo() override;
Eric Christopher9cad53c2013-04-03 18:31:38 +000040
41private:
Alexey Samsonov464d2e42014-03-17 07:28:19 +000042 void printSymbol(const SymbolRef &Symbol);
Eric Christopher9cad53c2013-04-03 18:31:38 +000043
Alexey Samsonovaa4d2952014-03-14 14:22:49 +000044 void printRelocation(const RelocationRef &Reloc);
Eric Christopher9cad53c2013-04-03 18:31:38 +000045
Alexey Samsonovaa4d2952014-03-14 14:22:49 +000046 void printRelocation(const MachOObjectFile *Obj, const RelocationRef &Reloc);
Rafael Espindola9b709252013-04-13 01:45:40 +000047
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
Rafael Espindola4453e42942014-06-13 03:07:50 +000058std::error_code createMachODumper(const object::ObjectFile *Obj,
59 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 },
Eric Christopher9cad53c2013-04-03 18:31:38 +0000128 { "Abs", 0x2 },
129 { "Indirect", 0xA },
130 { "PreboundUndef", 0xC },
Rafael Espindola1194e692014-03-06 20:13:41 +0000131 { "Section", 0xE }
Eric Christopher9cad53c2013-04-03 18:31:38 +0000132};
133
134namespace {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000135 struct MachOSection {
136 ArrayRef<char> Name;
137 ArrayRef<char> SegmentName;
138 uint64_t Address;
139 uint64_t Size;
140 uint32_t Offset;
141 uint32_t Alignment;
142 uint32_t RelocationTableOffset;
143 uint32_t NumRelocationTableEntries;
144 uint32_t Flags;
145 uint32_t Reserved1;
146 uint32_t Reserved2;
147 };
148
149 struct MachOSymbol {
150 uint32_t StringIndex;
151 uint8_t Type;
152 uint8_t SectionIndex;
153 uint16_t Flags;
154 uint64_t Value;
155 };
156}
157
Rafael Espindola56f976f2013-04-18 18:08:55 +0000158static void getSection(const MachOObjectFile *Obj,
159 DataRefImpl Sec,
Rafael Espindola9b709252013-04-13 01:45:40 +0000160 MachOSection &Section) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000161 if (!Obj->is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000162 MachO::section Sect = Obj->getSection(Sec);
163 Section.Address = Sect.addr;
164 Section.Size = Sect.size;
165 Section.Offset = Sect.offset;
166 Section.Alignment = Sect.align;
167 Section.RelocationTableOffset = Sect.reloff;
168 Section.NumRelocationTableEntries = Sect.nreloc;
169 Section.Flags = Sect.flags;
170 Section.Reserved1 = Sect.reserved1;
171 Section.Reserved2 = Sect.reserved2;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000172 return;
173 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000174 MachO::section_64 Sect = Obj->getSection64(Sec);
175 Section.Address = Sect.addr;
176 Section.Size = Sect.size;
177 Section.Offset = Sect.offset;
178 Section.Alignment = Sect.align;
179 Section.RelocationTableOffset = Sect.reloff;
180 Section.NumRelocationTableEntries = Sect.nreloc;
181 Section.Flags = Sect.flags;
182 Section.Reserved1 = Sect.reserved1;
183 Section.Reserved2 = Sect.reserved2;
Rafael Espindola9b709252013-04-13 01:45:40 +0000184}
185
Eric Christopher9cad53c2013-04-03 18:31:38 +0000186
Rafael Espindola56f976f2013-04-18 18:08:55 +0000187static void getSymbol(const MachOObjectFile *Obj,
Rafael Espindola9b709252013-04-13 01:45:40 +0000188 DataRefImpl DRI,
189 MachOSymbol &Symbol) {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000190 if (!Obj->is64Bit()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000191 MachO::nlist Entry = Obj->getSymbolTableEntry(DRI);
192 Symbol.StringIndex = Entry.n_strx;
193 Symbol.Type = Entry.n_type;
194 Symbol.SectionIndex = Entry.n_sect;
195 Symbol.Flags = Entry.n_desc;
196 Symbol.Value = Entry.n_value;
Rafael Espindola56f976f2013-04-18 18:08:55 +0000197 return;
198 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000199 MachO::nlist_64 Entry = Obj->getSymbol64TableEntry(DRI);
200 Symbol.StringIndex = Entry.n_strx;
201 Symbol.Type = Entry.n_type;
202 Symbol.SectionIndex = Entry.n_sect;
203 Symbol.Flags = Entry.n_desc;
204 Symbol.Value = Entry.n_value;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000205}
206
207void MachODumper::printFileHeaders() {
208 W.startLine() << "FileHeaders not implemented.\n";
209}
210
211void MachODumper::printSections() {
Rafael Espindola56f976f2013-04-18 18:08:55 +0000212 return printSections(Obj);
Rafael Espindola9b709252013-04-13 01:45:40 +0000213}
214
Rafael Espindola56f976f2013-04-18 18:08:55 +0000215void MachODumper::printSections(const MachOObjectFile *Obj) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000216 ListScope Group(W, "Sections");
217
218 int SectionIndex = -1;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000219 for (const SectionRef &Section : Obj->sections()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000220 ++SectionIndex;
221
Alexey Samsonov48803e52014-03-13 14:37:36 +0000222 MachOSection MOSection;
223 getSection(Obj, Section.getRawDataRefImpl(), MOSection);
224 DataRefImpl DR = Section.getRawDataRefImpl();
Rafael Espindolab0f76a42013-04-05 15:15:22 +0000225
Eric Christopher9cad53c2013-04-03 18:31:38 +0000226 StringRef Name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000227 if (error(Section.getName(Name)))
228 Name = "";
Eric Christopher9cad53c2013-04-03 18:31:38 +0000229
Rafael Espindolab0f76a42013-04-05 15:15:22 +0000230 ArrayRef<char> RawName = Obj->getSectionRawName(DR);
231 StringRef SegmentName = Obj->getSectionFinalSegmentName(DR);
232 ArrayRef<char> RawSegmentName = Obj->getSectionRawFinalSegmentName(DR);
233
Eric Christopher9cad53c2013-04-03 18:31:38 +0000234 DictScope SectionD(W, "Section");
235 W.printNumber("Index", SectionIndex);
Rafael Espindolab0f76a42013-04-05 15:15:22 +0000236 W.printBinary("Name", Name, RawName);
237 W.printBinary("Segment", SegmentName, RawSegmentName);
Alexey Samsonov48803e52014-03-13 14:37:36 +0000238 W.printHex("Address", MOSection.Address);
239 W.printHex("Size", MOSection.Size);
240 W.printNumber("Offset", MOSection.Offset);
241 W.printNumber("Alignment", MOSection.Alignment);
242 W.printHex("RelocationOffset", MOSection.RelocationTableOffset);
243 W.printNumber("RelocationCount", MOSection.NumRelocationTableEntries);
244 W.printEnum("Type", MOSection.Flags & 0xFF,
245 makeArrayRef(MachOSectionAttributes));
246 W.printFlags("Attributes", MOSection.Flags >> 8,
247 makeArrayRef(MachOSectionAttributes));
248 W.printHex("Reserved1", MOSection.Reserved1);
249 W.printHex("Reserved2", MOSection.Reserved2);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000250
251 if (opts::SectionRelocations) {
252 ListScope D(W, "Relocations");
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000253 for (const RelocationRef &Reloc : Section.relocations())
254 printRelocation(Reloc);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000255 }
256
257 if (opts::SectionSymbols) {
258 ListScope D(W, "Symbols");
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000259 for (const SymbolRef &Symbol : Obj->symbols()) {
Rafael Espindola80291272014-10-08 15:28:58 +0000260 if (!Section.containsSymbol(Symbol))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000261 continue;
262
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000263 printSymbol(Symbol);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000264 }
265 }
266
267 if (opts::SectionData) {
Rafael Espindola80291272014-10-08 15:28:58 +0000268 bool IsBSS = Section.isBSS();
David Majnemerdac39852014-09-26 22:32:16 +0000269 if (!IsBSS) {
270 StringRef Data;
271 if (error(Section.getContents(Data)))
272 break;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000273
David Majnemerdac39852014-09-26 22:32:16 +0000274 W.printBinaryBlock("SectionData", Data);
275 }
Eric Christopher9cad53c2013-04-03 18:31:38 +0000276 }
277 }
278}
279
280void MachODumper::printRelocations() {
281 ListScope D(W, "Relocations");
282
Rafael Espindola4453e42942014-06-13 03:07:50 +0000283 std::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 Samsonovaa4d2952014-03-14 14:22:49 +0000290 for (const RelocationRef &Reloc : Section.relocations()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000291 if (!PrintedGroup) {
292 W.startLine() << "Section " << Name << " {\n";
293 W.indent();
294 PrintedGroup = true;
295 }
296
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000297 printRelocation(Reloc);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000298 }
299
300 if (PrintedGroup) {
301 W.unindent();
302 W.startLine() << "}\n";
303 }
304 }
305}
306
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000307void MachODumper::printRelocation(const RelocationRef &Reloc) {
308 return printRelocation(Obj, Reloc);
Rafael Espindola9b709252013-04-13 01:45:40 +0000309}
310
Rafael Espindola56f976f2013-04-18 18:08:55 +0000311void MachODumper::printRelocation(const MachOObjectFile *Obj,
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000312 const RelocationRef &Reloc) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000313 uint64_t Offset;
314 SmallString<32> RelocName;
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000315 if (error(Reloc.getOffset(Offset)))
316 return;
317 if (error(Reloc.getTypeName(RelocName)))
318 return;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000319
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000320 DataRefImpl DR = Reloc.getRawDataRefImpl();
Charles Davis8bdfafd2013-09-01 04:28:48 +0000321 MachO::any_relocation_info RE = Obj->getRelocation(DR);
Rafael Espindola9b709252013-04-13 01:45:40 +0000322 bool IsScattered = Obj->isRelocationScattered(RE);
Tim Northover07f99fb2014-07-04 10:57:56 +0000323 SmallString<32> SymbolNameOrOffset("0x");
324 if (IsScattered) {
325 // Scattered relocations don't really have an associated symbol
326 // for some reason, even if one exists in the symtab at the correct address.
327 SymbolNameOrOffset += utohexstr(Obj->getScatteredRelocationValue(RE));
328 } else {
329 symbol_iterator Symbol = Reloc.getSymbol();
330 if (Symbol != Obj->symbol_end()) {
331 StringRef SymbolName;
332 if (error(Symbol->getName(SymbolName)))
333 return;
334 SymbolNameOrOffset = SymbolName;
335 } else
336 SymbolNameOrOffset += utohexstr(Obj->getPlainRelocationSymbolNum(RE));
337 }
Rafael Espindolaecf13202013-04-12 00:17:33 +0000338
Nico Rieckf3f0b792013-04-12 04:01:52 +0000339 if (opts::ExpandRelocs) {
340 DictScope Group(W, "Relocation");
341 W.printHex("Offset", Offset);
Rafael Espindola56f976f2013-04-18 18:08:55 +0000342 W.printNumber("PCRel", Obj->getAnyRelocationPCRel(RE));
343 W.printNumber("Length", Obj->getAnyRelocationLength(RE));
Nico Rieckf3f0b792013-04-12 04:01:52 +0000344 if (IsScattered)
345 W.printString("Extern", StringRef("N/A"));
346 else
Rafael Espindola56f976f2013-04-18 18:08:55 +0000347 W.printNumber("Extern", Obj->getPlainRelocationExternal(RE));
348 W.printNumber("Type", RelocName, Obj->getAnyRelocationType(RE));
Tim Northover07f99fb2014-07-04 10:57:56 +0000349 W.printString("Symbol", SymbolNameOrOffset);
Nico Rieckf3f0b792013-04-12 04:01:52 +0000350 W.printNumber("Scattered", IsScattered);
351 } else {
352 raw_ostream& OS = W.startLine();
353 OS << W.hex(Offset)
Rafael Espindola56f976f2013-04-18 18:08:55 +0000354 << " " << Obj->getAnyRelocationPCRel(RE)
355 << " " << Obj->getAnyRelocationLength(RE);
Nico Rieckf3f0b792013-04-12 04:01:52 +0000356 if (IsScattered)
357 OS << " n/a";
358 else
Rafael Espindola56f976f2013-04-18 18:08:55 +0000359 OS << " " << Obj->getPlainRelocationExternal(RE);
Nico Rieckf3f0b792013-04-12 04:01:52 +0000360 OS << " " << RelocName
361 << " " << IsScattered
Tim Northover07f99fb2014-07-04 10:57:56 +0000362 << " " << SymbolNameOrOffset
Nico Rieckf3f0b792013-04-12 04:01:52 +0000363 << "\n";
364 }
Eric Christopher9cad53c2013-04-03 18:31:38 +0000365}
366
367void MachODumper::printSymbols() {
368 ListScope Group(W, "Symbols");
369
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000370 for (const SymbolRef &Symbol : Obj->symbols()) {
371 printSymbol(Symbol);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000372 }
373}
374
375void MachODumper::printDynamicSymbols() {
376 ListScope Group(W, "DynamicSymbols");
377}
378
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000379void MachODumper::printSymbol(const SymbolRef &Symbol) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000380 StringRef SymbolName;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000381 if (Symbol.getName(SymbolName))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000382 SymbolName = "";
383
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000384 MachOSymbol MOSymbol;
385 getSymbol(Obj, Symbol.getRawDataRefImpl(), MOSymbol);
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());
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000389 if (!error(Symbol.getSection(SecI)) && SecI != Obj->section_end())
390 error(SecI->getName(SectionName));
Eric Christopher9cad53c2013-04-03 18:31:38 +0000391
392 DictScope D(W, "Symbol");
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000393 W.printNumber("Name", SymbolName, MOSymbol.StringIndex);
394 if (MOSymbol.Type & MachO::N_STAB) {
395 W.printHex("Type", "SymDebugTable", MOSymbol.Type);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000396 } else {
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000397 if (MOSymbol.Type & MachO::N_PEXT)
Rafael Espindola1194e692014-03-06 20:13:41 +0000398 W.startLine() << "PrivateExtern\n";
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000399 if (MOSymbol.Type & MachO::N_EXT)
Rafael Espindola1194e692014-03-06 20:13:41 +0000400 W.startLine() << "Extern\n";
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000401 W.printEnum("Type", uint8_t(MOSymbol.Type & MachO::N_TYPE),
Rafael Espindola1194e692014-03-06 20:13:41 +0000402 makeArrayRef(MachOSymbolTypes));
Eric Christopher9cad53c2013-04-03 18:31:38 +0000403 }
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000404 W.printHex("Section", SectionName, MOSymbol.SectionIndex);
405 W.printEnum("RefType", static_cast<uint16_t>(MOSymbol.Flags & 0xF),
406 makeArrayRef(MachOSymbolRefTypes));
407 W.printFlags("Flags", static_cast<uint16_t>(MOSymbol.Flags & ~0xF),
408 makeArrayRef(MachOSymbolFlags));
409 W.printHex("Value", MOSymbol.Value);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000410}
411
412void MachODumper::printUnwindInfo() {
413 W.startLine() << "UnwindInfo not implemented.\n";
414}