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() 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;
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 std::unique_ptr<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[] = {
130 { "PreboundUndef", 0xC },
135 struct MachOSection {
137 ArrayRef<char> SegmentName;
142 uint32_t RelocationTableOffset;
143 uint32_t NumRelocationTableEntries;
150 uint32_t StringIndex;
152 uint8_t SectionIndex;
158 static void getSection(const MachOObjectFile *Obj,
160 MachOSection &Section) {
161 if (!Obj->is64Bit()) {
162 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;
174 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;
187 static void getSymbol(const MachOObjectFile *Obj,
189 MachOSymbol &Symbol) {
190 if (!Obj->is64Bit()) {
191 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;
199 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;
207 void MachODumper::printFileHeaders() {
208 W.startLine() << "FileHeaders not implemented.\n";
211 void MachODumper::printSections() {
212 return printSections(Obj);
215 void MachODumper::printSections(const MachOObjectFile *Obj) {
216 ListScope Group(W, "Sections");
218 int SectionIndex = -1;
219 for (section_iterator SecI = Obj->section_begin(),
220 SecE = Obj->section_end();
221 SecI != SecE; ++SecI) {
224 MachOSection Section;
225 getSection(Obj, SecI->getRawDataRefImpl(), Section);
226 DataRefImpl DR = SecI->getRawDataRefImpl();
229 if (error(SecI->getName(Name)))
232 ArrayRef<char> RawName = Obj->getSectionRawName(DR);
233 StringRef SegmentName = Obj->getSectionFinalSegmentName(DR);
234 ArrayRef<char> RawSegmentName = Obj->getSectionRawFinalSegmentName(DR);
236 DictScope SectionD(W, "Section");
237 W.printNumber("Index", SectionIndex);
238 W.printBinary("Name", Name, RawName);
239 W.printBinary("Segment", SegmentName, RawSegmentName);
240 W.printHex ("Address", Section.Address);
241 W.printHex ("Size", Section.Size);
242 W.printNumber("Offset", Section.Offset);
243 W.printNumber("Alignment", Section.Alignment);
244 W.printHex ("RelocationOffset", Section.RelocationTableOffset);
245 W.printNumber("RelocationCount", Section.NumRelocationTableEntries);
246 W.printEnum ("Type", Section.Flags & 0xFF,
247 makeArrayRef(MachOSectionAttributes));
248 W.printFlags ("Attributes", Section.Flags >> 8,
249 makeArrayRef(MachOSectionAttributes));
250 W.printHex ("Reserved1", Section.Reserved1);
251 W.printHex ("Reserved2", Section.Reserved2);
253 if (opts::SectionRelocations) {
254 ListScope D(W, "Relocations");
255 for (relocation_iterator RelI = SecI->relocation_begin(),
256 RelE = SecI->relocation_end();
257 RelI != RelE; ++RelI)
258 printRelocation(SecI, RelI);
261 if (opts::SectionSymbols) {
262 ListScope D(W, "Symbols");
263 for (symbol_iterator SymI = Obj->symbol_begin(),
264 SymE = Obj->symbol_end();
265 SymI != SymE; ++SymI) {
266 bool Contained = false;
267 if (SecI->containsSymbol(*SymI, Contained) || !Contained)
274 if (opts::SectionData) {
276 if (error(SecI->getContents(Data))) break;
278 W.printBinaryBlock("SectionData", Data);
283 void MachODumper::printRelocations() {
284 ListScope D(W, "Relocations");
287 for (section_iterator SecI = Obj->section_begin(),
288 SecE = Obj->section_end();
289 SecI != SecE; ++SecI) {
291 if (error(SecI->getName(Name)))
294 bool PrintedGroup = false;
295 for (relocation_iterator RelI = SecI->relocation_begin(),
296 RelE = SecI->relocation_end();
297 RelI != RelE; ++RelI) {
299 W.startLine() << "Section " << Name << " {\n";
304 printRelocation(SecI, RelI);
309 W.startLine() << "}\n";
314 void MachODumper::printRelocation(section_iterator SecI,
315 relocation_iterator RelI) {
316 return printRelocation(Obj, SecI, RelI);
319 void MachODumper::printRelocation(const MachOObjectFile *Obj,
320 section_iterator SecI,
321 relocation_iterator RelI) {
323 SmallString<32> RelocName;
324 StringRef SymbolName;
325 if (error(RelI->getOffset(Offset))) return;
326 if (error(RelI->getTypeName(RelocName))) return;
327 symbol_iterator Symbol = RelI->getSymbol();
328 if (Symbol != Obj->symbol_end() &&
329 error(Symbol->getName(SymbolName)))
332 DataRefImpl DR = RelI->getRawDataRefImpl();
333 MachO::any_relocation_info RE = Obj->getRelocation(DR);
334 bool IsScattered = Obj->isRelocationScattered(RE);
336 if (opts::ExpandRelocs) {
337 DictScope Group(W, "Relocation");
338 W.printHex("Offset", Offset);
339 W.printNumber("PCRel", Obj->getAnyRelocationPCRel(RE));
340 W.printNumber("Length", Obj->getAnyRelocationLength(RE));
342 W.printString("Extern", StringRef("N/A"));
344 W.printNumber("Extern", Obj->getPlainRelocationExternal(RE));
345 W.printNumber("Type", RelocName, Obj->getAnyRelocationType(RE));
346 W.printString("Symbol", SymbolName.size() > 0 ? SymbolName : "-");
347 W.printNumber("Scattered", IsScattered);
349 raw_ostream& OS = W.startLine();
351 << " " << Obj->getAnyRelocationPCRel(RE)
352 << " " << Obj->getAnyRelocationLength(RE);
356 OS << " " << Obj->getPlainRelocationExternal(RE);
357 OS << " " << RelocName
358 << " " << IsScattered
359 << " " << (SymbolName.size() > 0 ? SymbolName : "-")
364 void MachODumper::printSymbols() {
365 ListScope Group(W, "Symbols");
367 for (symbol_iterator SymI = Obj->symbol_begin(), SymE = Obj->symbol_end();
368 SymI != SymE; ++SymI) {
373 void MachODumper::printDynamicSymbols() {
374 ListScope Group(W, "DynamicSymbols");
377 void MachODumper::printSymbol(symbol_iterator SymI) {
378 StringRef SymbolName;
379 if (SymI->getName(SymbolName))
383 getSymbol(Obj, SymI->getRawDataRefImpl(), Symbol);
385 StringRef SectionName = "";
386 section_iterator SecI(Obj->section_begin());
387 if (!error(SymI->getSection(SecI)) &&
388 SecI != Obj->section_end())
389 error(SecI->getName(SectionName));
391 DictScope D(W, "Symbol");
392 W.printNumber("Name", SymbolName, Symbol.StringIndex);
393 if (Symbol.Type & MachO::N_STAB) {
394 W.printHex ("Type", "SymDebugTable", Symbol.Type);
396 if (Symbol.Type & MachO::N_PEXT)
397 W.startLine() << "PrivateExtern\n";
398 if (Symbol.Type & MachO::N_EXT)
399 W.startLine() << "Extern\n";
400 W.printEnum("Type", Symbol.Type & MachO::N_TYPE,
401 makeArrayRef(MachOSymbolTypes));
403 W.printHex ("Section", SectionName, Symbol.SectionIndex);
404 W.printEnum ("RefType", static_cast<uint16_t>(Symbol.Flags & 0xF),
405 makeArrayRef(MachOSymbolRefTypes));
406 W.printFlags ("Flags", static_cast<uint16_t>(Symbol.Flags & ~0xF),
407 makeArrayRef(MachOSymbolFlags));
408 W.printHex ("Value", Symbol.Value);
411 void MachODumper::printUnwindInfo() {
412 W.startLine() << "UnwindInfo not implemented.\n";