1 //===-- MachODump.cpp - Object file dumping utility for llvm --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the MachO-specific dumper for llvm-readobj.
12 //===----------------------------------------------------------------------===//
14 #include "llvm-readobj.h"
16 #include "ObjDumper.h"
17 #include "StreamWriter.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/Object/MachO.h"
20 #include "llvm/Support/Casting.h"
23 using namespace object;
27 class MachODumper : public ObjDumper {
29 MachODumper(const MachOObjectFile *Obj, StreamWriter& Writer)
33 virtual void printFileHeaders() LLVM_OVERRIDE;
34 virtual void printSections() LLVM_OVERRIDE;
35 virtual void printRelocations() LLVM_OVERRIDE;
36 virtual void printSymbols() LLVM_OVERRIDE;
37 virtual void printDynamicSymbols() LLVM_OVERRIDE;
38 virtual void printUnwindInfo() LLVM_OVERRIDE;
41 void printSymbol(symbol_iterator SymI);
43 void printRelocation(section_iterator SecI, relocation_iterator RelI);
45 void printRelocation(const MachOObjectFile *Obj,
46 section_iterator SecI, relocation_iterator RelI);
48 void printSections(const MachOObjectFile *Obj);
50 const MachOObjectFile *Obj;
58 error_code createMachODumper(const object::ObjectFile *Obj,
60 OwningPtr<ObjDumper> &Result) {
61 const MachOObjectFile *MachOObj = dyn_cast<MachOObjectFile>(Obj);
63 return readobj_error::unsupported_obj_file_format;
65 Result.reset(new MachODumper(MachOObj, Writer));
66 return readobj_error::success;
72 static const EnumEntry<unsigned> MachOSectionTypes[] = {
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 }
97 static 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 */ },
110 static const EnumEntry<unsigned> MachOSymbolRefTypes[] = {
111 { "UndefinedNonLazy", 0 },
112 { "ReferenceFlagUndefinedLazy", 1 },
113 { "ReferenceFlagDefined", 2 },
114 { "ReferenceFlagPrivateDefined", 3 },
115 { "ReferenceFlagPrivateUndefinedNonLazy", 4 },
116 { "ReferenceFlagPrivateUndefinedLazy", 5 }
119 static const EnumEntry<unsigned> MachOSymbolFlags[] = {
120 { "ReferencedDynamically", 0x10 },
121 { "NoDeadStrip", 0x20 },
126 static const EnumEntry<unsigned> MachOSymbolTypes[] = {
131 { "PreboundUndef", 0xC },
133 { "PrivateExternal", 0x10 }
141 struct MachOSection {
143 ArrayRef<char> SegmentName;
148 uint32_t RelocationTableOffset;
149 uint32_t NumRelocationTableEntries;
156 uint32_t StringIndex;
158 uint8_t SectionIndex;
164 static void getSection(const MachOObjectFile *Obj,
166 MachOSection &Section) {
167 if (!Obj->is64Bit()) {
168 MachO::section Sect = Obj->getSection(Sec);
169 Section.Address = Sect.addr;
170 Section.Size = Sect.size;
171 Section.Offset = Sect.offset;
172 Section.Alignment = Sect.align;
173 Section.RelocationTableOffset = Sect.reloff;
174 Section.NumRelocationTableEntries = Sect.nreloc;
175 Section.Flags = Sect.flags;
176 Section.Reserved1 = Sect.reserved1;
177 Section.Reserved2 = Sect.reserved2;
180 MachO::section_64 Sect = Obj->getSection64(Sec);
181 Section.Address = Sect.addr;
182 Section.Size = Sect.size;
183 Section.Offset = Sect.offset;
184 Section.Alignment = Sect.align;
185 Section.RelocationTableOffset = Sect.reloff;
186 Section.NumRelocationTableEntries = Sect.nreloc;
187 Section.Flags = Sect.flags;
188 Section.Reserved1 = Sect.reserved1;
189 Section.Reserved2 = Sect.reserved2;
193 static void getSymbol(const MachOObjectFile *Obj,
195 MachOSymbol &Symbol) {
196 if (!Obj->is64Bit()) {
197 MachO::nlist Entry = Obj->getSymbolTableEntry(DRI);
198 Symbol.StringIndex = Entry.n_strx;
199 Symbol.Type = Entry.n_type;
200 Symbol.SectionIndex = Entry.n_sect;
201 Symbol.Flags = Entry.n_desc;
202 Symbol.Value = Entry.n_value;
205 MachO::nlist_64 Entry = Obj->getSymbol64TableEntry(DRI);
206 Symbol.StringIndex = Entry.n_strx;
207 Symbol.Type = Entry.n_type;
208 Symbol.SectionIndex = Entry.n_sect;
209 Symbol.Flags = Entry.n_desc;
210 Symbol.Value = Entry.n_value;
213 void MachODumper::printFileHeaders() {
214 W.startLine() << "FileHeaders not implemented.\n";
217 void MachODumper::printSections() {
218 return printSections(Obj);
221 void MachODumper::printSections(const MachOObjectFile *Obj) {
222 ListScope Group(W, "Sections");
224 int SectionIndex = -1;
226 for (section_iterator SecI = Obj->begin_sections(),
227 SecE = Obj->end_sections();
228 SecI != SecE; SecI.increment(EC)) {
229 if (error(EC)) break;
233 MachOSection Section;
234 getSection(Obj, SecI->getRawDataRefImpl(), Section);
235 DataRefImpl DR = SecI->getRawDataRefImpl();
238 if (error(SecI->getName(Name)))
241 ArrayRef<char> RawName = Obj->getSectionRawName(DR);
242 StringRef SegmentName = Obj->getSectionFinalSegmentName(DR);
243 ArrayRef<char> RawSegmentName = Obj->getSectionRawFinalSegmentName(DR);
245 DictScope SectionD(W, "Section");
246 W.printNumber("Index", SectionIndex);
247 W.printBinary("Name", Name, RawName);
248 W.printBinary("Segment", SegmentName, RawSegmentName);
249 W.printHex ("Address", Section.Address);
250 W.printHex ("Size", Section.Size);
251 W.printNumber("Offset", Section.Offset);
252 W.printNumber("Alignment", Section.Alignment);
253 W.printHex ("RelocationOffset", Section.RelocationTableOffset);
254 W.printNumber("RelocationCount", Section.NumRelocationTableEntries);
255 W.printEnum ("Type", Section.Flags & 0xFF,
256 makeArrayRef(MachOSectionAttributes));
257 W.printFlags ("Attributes", Section.Flags >> 8,
258 makeArrayRef(MachOSectionAttributes));
259 W.printHex ("Reserved1", Section.Reserved1);
260 W.printHex ("Reserved2", Section.Reserved2);
262 if (opts::SectionRelocations) {
263 ListScope D(W, "Relocations");
264 for (relocation_iterator RelI = SecI->begin_relocations(),
265 RelE = SecI->end_relocations();
266 RelI != RelE; RelI.increment(EC)) {
267 if (error(EC)) break;
269 printRelocation(SecI, RelI);
273 if (opts::SectionSymbols) {
274 ListScope D(W, "Symbols");
275 for (symbol_iterator SymI = Obj->begin_symbols(),
276 SymE = Obj->end_symbols();
277 SymI != SymE; SymI.increment(EC)) {
278 if (error(EC)) break;
280 bool Contained = false;
281 if (SecI->containsSymbol(*SymI, Contained) || !Contained)
288 if (opts::SectionData) {
290 if (error(SecI->getContents(Data))) break;
292 W.printBinaryBlock("SectionData", Data);
297 void MachODumper::printRelocations() {
298 ListScope D(W, "Relocations");
301 for (section_iterator SecI = Obj->begin_sections(),
302 SecE = Obj->end_sections();
303 SecI != SecE; SecI.increment(EC)) {
304 if (error(EC)) break;
307 if (error(SecI->getName(Name)))
310 bool PrintedGroup = false;
311 for (relocation_iterator RelI = SecI->begin_relocations(),
312 RelE = SecI->end_relocations();
313 RelI != RelE; RelI.increment(EC)) {
314 if (error(EC)) break;
317 W.startLine() << "Section " << Name << " {\n";
322 printRelocation(SecI, RelI);
327 W.startLine() << "}\n";
332 void MachODumper::printRelocation(section_iterator SecI,
333 relocation_iterator RelI) {
334 return printRelocation(Obj, SecI, RelI);
337 void MachODumper::printRelocation(const MachOObjectFile *Obj,
338 section_iterator SecI,
339 relocation_iterator RelI) {
341 SmallString<32> RelocName;
342 StringRef SymbolName;
343 if (error(RelI->getOffset(Offset))) return;
344 if (error(RelI->getTypeName(RelocName))) return;
345 symbol_iterator Symbol = RelI->getSymbol();
346 if (Symbol != Obj->end_symbols() &&
347 error(Symbol->getName(SymbolName)))
350 DataRefImpl DR = RelI->getRawDataRefImpl();
351 MachO::any_relocation_info RE = Obj->getRelocation(DR);
352 bool IsScattered = Obj->isRelocationScattered(RE);
354 if (opts::ExpandRelocs) {
355 DictScope Group(W, "Relocation");
356 W.printHex("Offset", Offset);
357 W.printNumber("PCRel", Obj->getAnyRelocationPCRel(RE));
358 W.printNumber("Length", Obj->getAnyRelocationLength(RE));
360 W.printString("Extern", StringRef("N/A"));
362 W.printNumber("Extern", Obj->getPlainRelocationExternal(RE));
363 W.printNumber("Type", RelocName, Obj->getAnyRelocationType(RE));
364 W.printString("Symbol", SymbolName.size() > 0 ? SymbolName : "-");
365 W.printNumber("Scattered", IsScattered);
367 raw_ostream& OS = W.startLine();
369 << " " << Obj->getAnyRelocationPCRel(RE)
370 << " " << Obj->getAnyRelocationLength(RE);
374 OS << " " << Obj->getPlainRelocationExternal(RE);
375 OS << " " << RelocName
376 << " " << IsScattered
377 << " " << (SymbolName.size() > 0 ? SymbolName : "-")
382 void MachODumper::printSymbols() {
383 ListScope Group(W, "Symbols");
386 for (symbol_iterator SymI = Obj->begin_symbols(),
387 SymE = Obj->end_symbols();
388 SymI != SymE; SymI.increment(EC)) {
389 if (error(EC)) break;
395 void MachODumper::printDynamicSymbols() {
396 ListScope Group(W, "DynamicSymbols");
399 void MachODumper::printSymbol(symbol_iterator SymI) {
400 StringRef SymbolName;
401 if (SymI->getName(SymbolName))
405 getSymbol(Obj, SymI->getRawDataRefImpl(), Symbol);
407 StringRef SectionName = "";
408 section_iterator SecI(Obj->end_sections());
409 if (!error(SymI->getSection(SecI)) &&
410 SecI != Obj->end_sections())
411 error(SecI->getName(SectionName));
413 DictScope D(W, "Symbol");
414 W.printNumber("Name", SymbolName, Symbol.StringIndex);
415 if (Symbol.Type & N_STAB) {
416 W.printHex ("Type", "SymDebugTable", Symbol.Type);
418 W.printEnum("Type", Symbol.Type, makeArrayRef(MachOSymbolTypes));
420 W.printHex ("Section", SectionName, Symbol.SectionIndex);
421 W.printEnum ("RefType", static_cast<uint16_t>(Symbol.Flags & 0xF),
422 makeArrayRef(MachOSymbolRefTypes));
423 W.printFlags ("Flags", static_cast<uint16_t>(Symbol.Flags & ~0xF),
424 makeArrayRef(MachOSymbolFlags));
425 W.printHex ("Value", Symbol.Value);
428 void MachODumper::printUnwindInfo() {
429 W.startLine() << "UnwindInfo not implemented.\n";