1 //===-- 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-objdump.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm-objdump.h"
15 #include "MCFunction.h"
16 #include "llvm/Support/MachO.h"
17 #include "llvm/Object/MachOObject.h"
18 #include "llvm/ADT/OwningPtr.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/DebugInfo/DIContext.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCDisassembler.h"
24 #include "llvm/MC/MCInst.h"
25 #include "llvm/MC/MCInstPrinter.h"
26 #include "llvm/MC/MCInstrAnalysis.h"
27 #include "llvm/MC/MCInstrDesc.h"
28 #include "llvm/MC/MCInstrInfo.h"
29 #include "llvm/MC/MCSubtargetInfo.h"
30 #include "llvm/Support/CommandLine.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/Format.h"
33 #include "llvm/Support/GraphWriter.h"
34 #include "llvm/Support/MemoryBuffer.h"
35 #include "llvm/Support/TargetRegistry.h"
36 #include "llvm/Support/TargetSelect.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include "llvm/Support/system_error.h"
39 #include <algorithm>
40 #include <cstring>
41 using namespace llvm;
42 using namespace object;
43
44 static cl::opt<bool>
45 CFG("cfg", cl::desc("Create a CFG for every symbol in the object file and"
46 "write it to a graphviz file (MachO-only)"));
47
48 static cl::opt<bool>
49 UseDbg("g", cl::desc("Print line information from debug info if available"));
50
51 static cl::opt<std::string>
52 DSYMFile("dsym", cl::desc("Use .dSYM file for debug info"));
53
GetTarget(const MachOObject * MachOObj)54 static const Target *GetTarget(const MachOObject *MachOObj) {
55 // Figure out the target triple.
56 llvm::Triple TT("unknown-unknown-unknown");
57 switch (MachOObj->getHeader().CPUType) {
58 case llvm::MachO::CPUTypeI386:
59 TT.setArch(Triple::ArchType(Triple::x86));
60 break;
61 case llvm::MachO::CPUTypeX86_64:
62 TT.setArch(Triple::ArchType(Triple::x86_64));
63 break;
64 case llvm::MachO::CPUTypeARM:
65 TT.setArch(Triple::ArchType(Triple::arm));
66 break;
67 case llvm::MachO::CPUTypePowerPC:
68 TT.setArch(Triple::ArchType(Triple::ppc));
69 break;
70 case llvm::MachO::CPUTypePowerPC64:
71 TT.setArch(Triple::ArchType(Triple::ppc64));
72 break;
73 }
74
75 TripleName = TT.str();
76
77 // Get the target specific parser.
78 std::string Error;
79 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
80 if (TheTarget)
81 return TheTarget;
82
83 errs() << "llvm-objdump: error: unable to get target for '" << TripleName
84 << "', see --version and --triple.\n";
85 return 0;
86 }
87
88 struct Section {
89 char Name[16];
90 uint64_t Address;
91 uint64_t Size;
92 uint32_t Offset;
93 uint32_t NumRelocs;
94 uint64_t RelocTableOffset;
95 };
96
97 struct Symbol {
98 uint64_t Value;
99 uint32_t StringIndex;
100 uint8_t SectionIndex;
operator <Symbol101 bool operator<(const Symbol &RHS) const { return Value < RHS.Value; }
102 };
103
104 template <typename T>
copySection(const T & Sect)105 static Section copySection(const T &Sect) {
106 Section S;
107 memcpy(S.Name, Sect->Name, 16);
108 S.Address = Sect->Address;
109 S.Size = Sect->Size;
110 S.Offset = Sect->Offset;
111 S.NumRelocs = Sect->NumRelocationTableEntries;
112 S.RelocTableOffset = Sect->RelocationTableOffset;
113 return S;
114 }
115
116 template <typename T>
copySymbol(const T & STE)117 static Symbol copySymbol(const T &STE) {
118 Symbol S;
119 S.StringIndex = STE->StringIndex;
120 S.SectionIndex = STE->SectionIndex;
121 S.Value = STE->Value;
122 return S;
123 }
124
125 // Print additional information about an address, if available.
DumpAddress(uint64_t Address,ArrayRef<Section> Sections,MachOObject * MachOObj,raw_ostream & OS)126 static void DumpAddress(uint64_t Address, ArrayRef<Section> Sections,
127 MachOObject *MachOObj, raw_ostream &OS) {
128 for (unsigned i = 0; i != Sections.size(); ++i) {
129 uint64_t addr = Address-Sections[i].Address;
130 if (Sections[i].Address <= Address &&
131 Sections[i].Address + Sections[i].Size > Address) {
132 StringRef bytes = MachOObj->getData(Sections[i].Offset,
133 Sections[i].Size);
134 // Print constant strings.
135 if (!strcmp(Sections[i].Name, "__cstring"))
136 OS << '"' << bytes.substr(addr, bytes.find('\0', addr)) << '"';
137 // Print constant CFStrings.
138 if (!strcmp(Sections[i].Name, "__cfstring"))
139 OS << "@\"" << bytes.substr(addr, bytes.find('\0', addr)) << '"';
140 }
141 }
142 }
143
144 typedef std::map<uint64_t, MCFunction*> FunctionMapTy;
145 typedef SmallVector<MCFunction, 16> FunctionListTy;
createMCFunctionAndSaveCalls(StringRef Name,const MCDisassembler * DisAsm,MemoryObject & Object,uint64_t Start,uint64_t End,MCInstrAnalysis * InstrAnalysis,uint64_t Address,raw_ostream & DebugOut,FunctionMapTy & FunctionMap,FunctionListTy & Functions)146 static void createMCFunctionAndSaveCalls(StringRef Name,
147 const MCDisassembler *DisAsm,
148 MemoryObject &Object, uint64_t Start,
149 uint64_t End,
150 MCInstrAnalysis *InstrAnalysis,
151 uint64_t Address,
152 raw_ostream &DebugOut,
153 FunctionMapTy &FunctionMap,
154 FunctionListTy &Functions) {
155 SmallVector<uint64_t, 16> Calls;
156 MCFunction f =
157 MCFunction::createFunctionFromMC(Name, DisAsm, Object, Start, End,
158 InstrAnalysis, DebugOut, Calls);
159 Functions.push_back(f);
160 FunctionMap[Address] = &Functions.back();
161
162 // Add the gathered callees to the map.
163 for (unsigned i = 0, e = Calls.size(); i != e; ++i)
164 FunctionMap.insert(std::make_pair(Calls[i], (MCFunction*)0));
165 }
166
167 // Write a graphviz file for the CFG inside an MCFunction.
emitDOTFile(const char * FileName,const MCFunction & f,MCInstPrinter * IP)168 static void emitDOTFile(const char *FileName, const MCFunction &f,
169 MCInstPrinter *IP) {
170 // Start a new dot file.
171 std::string Error;
172 raw_fd_ostream Out(FileName, Error);
173 if (!Error.empty()) {
174 errs() << "llvm-objdump: warning: " << Error << '\n';
175 return;
176 }
177
178 Out << "digraph " << f.getName() << " {\n";
179 Out << "graph [ rankdir = \"LR\" ];\n";
180 for (MCFunction::iterator i = f.begin(), e = f.end(); i != e; ++i) {
181 bool hasPreds = false;
182 // Only print blocks that have predecessors.
183 // FIXME: Slow.
184 for (MCFunction::iterator pi = f.begin(), pe = f.end(); pi != pe;
185 ++pi)
186 if (pi->second.contains(i->first)) {
187 hasPreds = true;
188 break;
189 }
190
191 if (!hasPreds && i != f.begin())
192 continue;
193
194 Out << '"' << i->first << "\" [ label=\"<a>";
195 // Print instructions.
196 for (unsigned ii = 0, ie = i->second.getInsts().size(); ii != ie;
197 ++ii) {
198 // Escape special chars and print the instruction in mnemonic form.
199 std::string Str;
200 raw_string_ostream OS(Str);
201 IP->printInst(&i->second.getInsts()[ii].Inst, OS, "");
202 Out << DOT::EscapeString(OS.str()) << '|';
203 }
204 Out << "<o>\" shape=\"record\" ];\n";
205
206 // Add edges.
207 for (MCBasicBlock::succ_iterator si = i->second.succ_begin(),
208 se = i->second.succ_end(); si != se; ++si)
209 Out << i->first << ":o -> " << *si <<":a\n";
210 }
211 Out << "}\n";
212 }
213
getSectionsAndSymbols(const macho::Header & Header,MachOObject * MachOObj,InMemoryStruct<macho::SymtabLoadCommand> * SymtabLC,std::vector<Section> & Sections,std::vector<Symbol> & Symbols,SmallVectorImpl<uint64_t> & FoundFns)214 static void getSectionsAndSymbols(const macho::Header &Header,
215 MachOObject *MachOObj,
216 InMemoryStruct<macho::SymtabLoadCommand> *SymtabLC,
217 std::vector<Section> &Sections,
218 std::vector<Symbol> &Symbols,
219 SmallVectorImpl<uint64_t> &FoundFns) {
220 // Make a list of all symbols in the object file.
221 for (unsigned i = 0; i != Header.NumLoadCommands; ++i) {
222 const MachOObject::LoadCommandInfo &LCI = MachOObj->getLoadCommandInfo(i);
223 if (LCI.Command.Type == macho::LCT_Segment) {
224 InMemoryStruct<macho::SegmentLoadCommand> SegmentLC;
225 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLC);
226
227 // Store the sections in this segment.
228 for (unsigned SectNum = 0; SectNum != SegmentLC->NumSections; ++SectNum) {
229 InMemoryStruct<macho::Section> Sect;
230 MachOObj->ReadSection(LCI, SectNum, Sect);
231 Sections.push_back(copySection(Sect));
232
233 }
234 } else if (LCI.Command.Type == macho::LCT_Segment64) {
235 InMemoryStruct<macho::Segment64LoadCommand> Segment64LC;
236 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LC);
237
238 // Store the sections in this segment.
239 for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections;
240 ++SectNum) {
241 InMemoryStruct<macho::Section64> Sect64;
242 MachOObj->ReadSection64(LCI, SectNum, Sect64);
243 Sections.push_back(copySection(Sect64));
244 }
245 } else if (LCI.Command.Type == macho::LCT_FunctionStarts) {
246 // We found a function starts segment, parse the addresses for later
247 // consumption.
248 InMemoryStruct<macho::LinkeditDataLoadCommand> LLC;
249 MachOObj->ReadLinkeditDataLoadCommand(LCI, LLC);
250
251 MachOObj->ReadULEB128s(LLC->DataOffset, FoundFns);
252 }
253 }
254 // Store the symbols.
255 if (SymtabLC) {
256 for (unsigned i = 0; i != (*SymtabLC)->NumSymbolTableEntries; ++i) {
257 if (MachOObj->is64Bit()) {
258 InMemoryStruct<macho::Symbol64TableEntry> STE;
259 MachOObj->ReadSymbol64TableEntry((*SymtabLC)->SymbolTableOffset, i,
260 STE);
261 Symbols.push_back(copySymbol(STE));
262 } else {
263 InMemoryStruct<macho::SymbolTableEntry> STE;
264 MachOObj->ReadSymbolTableEntry((*SymtabLC)->SymbolTableOffset, i,
265 STE);
266 Symbols.push_back(copySymbol(STE));
267 }
268 }
269 }
270 }
271
DisassembleInputMachO(StringRef Filename)272 void llvm::DisassembleInputMachO(StringRef Filename) {
273 OwningPtr<MemoryBuffer> Buff;
274
275 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
276 errs() << "llvm-objdump: " << Filename << ": " << ec.message() << "\n";
277 return;
278 }
279
280 OwningPtr<MachOObject> MachOObj(MachOObject::LoadFromBuffer(Buff.take()));
281
282 const Target *TheTarget = GetTarget(MachOObj.get());
283 if (!TheTarget) {
284 // GetTarget prints out stuff.
285 return;
286 }
287 OwningPtr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
288 OwningPtr<MCInstrAnalysis>
289 InstrAnalysis(TheTarget->createMCInstrAnalysis(InstrInfo.get()));
290
291 // Set up disassembler.
292 OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createMCAsmInfo(TripleName));
293 OwningPtr<const MCSubtargetInfo>
294 STI(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
295 OwningPtr<const MCDisassembler> DisAsm(TheTarget->createMCDisassembler(*STI));
296 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
297 OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
298 AsmPrinterVariant, *AsmInfo, *STI));
299
300 if (!InstrAnalysis || !AsmInfo || !STI || !DisAsm || !IP) {
301 errs() << "error: couldn't initialize disassembler for target "
302 << TripleName << '\n';
303 return;
304 }
305
306 outs() << '\n' << Filename << ":\n\n";
307
308 const macho::Header &Header = MachOObj->getHeader();
309
310 const MachOObject::LoadCommandInfo *SymtabLCI = 0;
311 // First, find the symbol table segment.
312 for (unsigned i = 0; i != Header.NumLoadCommands; ++i) {
313 const MachOObject::LoadCommandInfo &LCI = MachOObj->getLoadCommandInfo(i);
314 if (LCI.Command.Type == macho::LCT_Symtab) {
315 SymtabLCI = &LCI;
316 break;
317 }
318 }
319
320 // Read and register the symbol table data.
321 InMemoryStruct<macho::SymtabLoadCommand> SymtabLC;
322 MachOObj->ReadSymtabLoadCommand(*SymtabLCI, SymtabLC);
323 MachOObj->RegisterStringTable(*SymtabLC);
324
325 std::vector<Section> Sections;
326 std::vector<Symbol> Symbols;
327 SmallVector<uint64_t, 8> FoundFns;
328
329 getSectionsAndSymbols(Header, MachOObj.get(), &SymtabLC, Sections, Symbols,
330 FoundFns);
331
332 // Make a copy of the unsorted symbol list. FIXME: duplication
333 std::vector<Symbol> UnsortedSymbols(Symbols);
334 // Sort the symbols by address, just in case they didn't come in that way.
335 array_pod_sort(Symbols.begin(), Symbols.end());
336
337 #ifndef NDEBUG
338 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
339 #else
340 raw_ostream &DebugOut = nulls();
341 #endif
342
343 StringRef DebugAbbrevSection, DebugInfoSection, DebugArangesSection,
344 DebugLineSection, DebugStrSection;
345 OwningPtr<DIContext> diContext;
346 OwningPtr<MachOObject> DSYMObj;
347 MachOObject *DbgInfoObj = MachOObj.get();
348 // Try to find debug info and set up the DIContext for it.
349 if (UseDbg) {
350 ArrayRef<Section> DebugSections = Sections;
351 std::vector<Section> DSYMSections;
352
353 // A separate DSym file path was specified, parse it as a macho file,
354 // get the sections and supply it to the section name parsing machinery.
355 if (!DSYMFile.empty()) {
356 OwningPtr<MemoryBuffer> Buf;
357 if (error_code ec = MemoryBuffer::getFileOrSTDIN(DSYMFile.c_str(), Buf)) {
358 errs() << "llvm-objdump: " << Filename << ": " << ec.message() << '\n';
359 return;
360 }
361 DSYMObj.reset(MachOObject::LoadFromBuffer(Buf.take()));
362 const macho::Header &Header = DSYMObj->getHeader();
363
364 std::vector<Symbol> Symbols;
365 SmallVector<uint64_t, 8> FoundFns;
366 getSectionsAndSymbols(Header, DSYMObj.get(), 0, DSYMSections, Symbols,
367 FoundFns);
368 DebugSections = DSYMSections;
369 DbgInfoObj = DSYMObj.get();
370 }
371
372 // Find the named debug info sections.
373 for (unsigned SectIdx = 0; SectIdx != DebugSections.size(); SectIdx++) {
374 if (!strcmp(DebugSections[SectIdx].Name, "__debug_abbrev"))
375 DebugAbbrevSection = DbgInfoObj->getData(DebugSections[SectIdx].Offset,
376 DebugSections[SectIdx].Size);
377 else if (!strcmp(DebugSections[SectIdx].Name, "__debug_info"))
378 DebugInfoSection = DbgInfoObj->getData(DebugSections[SectIdx].Offset,
379 DebugSections[SectIdx].Size);
380 else if (!strcmp(DebugSections[SectIdx].Name, "__debug_aranges"))
381 DebugArangesSection = DbgInfoObj->getData(DebugSections[SectIdx].Offset,
382 DebugSections[SectIdx].Size);
383 else if (!strcmp(DebugSections[SectIdx].Name, "__debug_line"))
384 DebugLineSection = DbgInfoObj->getData(DebugSections[SectIdx].Offset,
385 DebugSections[SectIdx].Size);
386 else if (!strcmp(DebugSections[SectIdx].Name, "__debug_str"))
387 DebugStrSection = DbgInfoObj->getData(DebugSections[SectIdx].Offset,
388 DebugSections[SectIdx].Size);
389 }
390
391 // Setup the DIContext.
392 diContext.reset(DIContext::getDWARFContext(DbgInfoObj->isLittleEndian(),
393 DebugInfoSection,
394 DebugAbbrevSection,
395 DebugArangesSection,
396 DebugLineSection,
397 DebugStrSection));
398 }
399
400 FunctionMapTy FunctionMap;
401 FunctionListTy Functions;
402
403 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
404 if (strcmp(Sections[SectIdx].Name, "__text"))
405 continue; // Skip non-text sections
406
407 // Insert the functions from the function starts segment into our map.
408 uint64_t VMAddr = Sections[SectIdx].Address - Sections[SectIdx].Offset;
409 for (unsigned i = 0, e = FoundFns.size(); i != e; ++i)
410 FunctionMap.insert(std::make_pair(FoundFns[i]+VMAddr, (MCFunction*)0));
411
412 StringRef Bytes = MachOObj->getData(Sections[SectIdx].Offset,
413 Sections[SectIdx].Size);
414 StringRefMemoryObject memoryObject(Bytes);
415 bool symbolTableWorked = false;
416
417 // Parse relocations.
418 std::vector<std::pair<uint64_t, uint32_t> > Relocs;
419 for (unsigned j = 0; j != Sections[SectIdx].NumRelocs; ++j) {
420 InMemoryStruct<macho::RelocationEntry> RE;
421 MachOObj->ReadRelocationEntry(Sections[SectIdx].RelocTableOffset, j, RE);
422 Relocs.push_back(std::make_pair(RE->Word0, RE->Word1 & 0xffffff));
423 }
424 array_pod_sort(Relocs.begin(), Relocs.end());
425
426 // Disassemble symbol by symbol.
427 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
428 // Make sure the symbol is defined in this section.
429 if ((unsigned)Symbols[SymIdx].SectionIndex - 1 != SectIdx)
430 continue;
431
432 // Start at the address of the symbol relative to the section's address.
433 uint64_t Start = Symbols[SymIdx].Value - Sections[SectIdx].Address;
434 // Stop disassembling either at the beginning of the next symbol or at
435 // the end of the section.
436 uint64_t End = (SymIdx+1 == Symbols.size() ||
437 Symbols[SymIdx].SectionIndex != Symbols[SymIdx+1].SectionIndex) ?
438 Sections[SectIdx].Size :
439 Symbols[SymIdx+1].Value - Sections[SectIdx].Address;
440 uint64_t Size;
441
442 if (Start >= End)
443 continue;
444
445 symbolTableWorked = true;
446
447 if (!CFG) {
448 // Normal disassembly, print addresses, bytes and mnemonic form.
449 outs() << MachOObj->getStringAtIndex(Symbols[SymIdx].StringIndex)
450 << ":\n";
451 DILineInfo lastLine;
452 for (uint64_t Index = Start; Index < End; Index += Size) {
453 MCInst Inst;
454
455 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index,
456 DebugOut, nulls())) {
457 outs() << format("%8llx:\t", Sections[SectIdx].Address + Index);
458 DumpBytes(StringRef(Bytes.data() + Index, Size));
459 IP->printInst(&Inst, outs(), "");
460
461 // Print debug info.
462 if (diContext) {
463 DILineInfo dli =
464 diContext->getLineInfoForAddress(Sections[SectIdx].Address +
465 Index);
466 // Print valid line info if it changed.
467 if (dli != lastLine && dli.getLine() != 0)
468 outs() << "\t## " << dli.getFileName() << ':'
469 << dli.getLine() << ':' << dli.getColumn();
470 lastLine = dli;
471 }
472 outs() << "\n";
473 } else {
474 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
475 if (Size == 0)
476 Size = 1; // skip illegible bytes
477 }
478 }
479 } else {
480 // Create CFG and use it for disassembly.
481 createMCFunctionAndSaveCalls(
482 MachOObj->getStringAtIndex(Symbols[SymIdx].StringIndex),
483 DisAsm.get(), memoryObject, Start, End, InstrAnalysis.get(),
484 Start, DebugOut, FunctionMap, Functions);
485 }
486 }
487
488 if (CFG) {
489 if (!symbolTableWorked) {
490 // Reading the symbol table didn't work, create a big __TEXT symbol.
491 createMCFunctionAndSaveCalls("__TEXT", DisAsm.get(), memoryObject,
492 0, Sections[SectIdx].Size,
493 InstrAnalysis.get(),
494 Sections[SectIdx].Offset, DebugOut,
495 FunctionMap, Functions);
496 }
497 for (std::map<uint64_t, MCFunction*>::iterator mi = FunctionMap.begin(),
498 me = FunctionMap.end(); mi != me; ++mi)
499 if (mi->second == 0) {
500 // Create functions for the remaining callees we have gathered,
501 // but we didn't find a name for them.
502 SmallVector<uint64_t, 16> Calls;
503 MCFunction f =
504 MCFunction::createFunctionFromMC("unknown", DisAsm.get(),
505 memoryObject, mi->first,
506 Sections[SectIdx].Size,
507 InstrAnalysis.get(), DebugOut,
508 Calls);
509 Functions.push_back(f);
510 mi->second = &Functions.back();
511 for (unsigned i = 0, e = Calls.size(); i != e; ++i) {
512 std::pair<uint64_t, MCFunction*> p(Calls[i], (MCFunction*)0);
513 if (FunctionMap.insert(p).second)
514 mi = FunctionMap.begin();
515 }
516 }
517
518 DenseSet<uint64_t> PrintedBlocks;
519 for (unsigned ffi = 0, ffe = Functions.size(); ffi != ffe; ++ffi) {
520 MCFunction &f = Functions[ffi];
521 for (MCFunction::iterator fi = f.begin(), fe = f.end(); fi != fe; ++fi){
522 if (!PrintedBlocks.insert(fi->first).second)
523 continue; // We already printed this block.
524
525 // We assume a block has predecessors when it's the first block after
526 // a symbol.
527 bool hasPreds = FunctionMap.find(fi->first) != FunctionMap.end();
528
529 // See if this block has predecessors.
530 // FIXME: Slow.
531 for (MCFunction::iterator pi = f.begin(), pe = f.end(); pi != pe;
532 ++pi)
533 if (pi->second.contains(fi->first)) {
534 hasPreds = true;
535 break;
536 }
537
538 // No predecessors, this is a data block. Print as .byte directives.
539 if (!hasPreds) {
540 uint64_t End = llvm::next(fi) == fe ? Sections[SectIdx].Size :
541 llvm::next(fi)->first;
542 outs() << "# " << End-fi->first << " bytes of data:\n";
543 for (unsigned pos = fi->first; pos != End; ++pos) {
544 outs() << format("%8x:\t", Sections[SectIdx].Address + pos);
545 DumpBytes(StringRef(Bytes.data() + pos, 1));
546 outs() << format("\t.byte 0x%02x\n", (uint8_t)Bytes[pos]);
547 }
548 continue;
549 }
550
551 if (fi->second.contains(fi->first)) // Print a header for simple loops
552 outs() << "# Loop begin:\n";
553
554 DILineInfo lastLine;
555 // Walk over the instructions and print them.
556 for (unsigned ii = 0, ie = fi->second.getInsts().size(); ii != ie;
557 ++ii) {
558 const MCDecodedInst &Inst = fi->second.getInsts()[ii];
559
560 // If there's a symbol at this address, print its name.
561 if (FunctionMap.find(Sections[SectIdx].Address + Inst.Address) !=
562 FunctionMap.end())
563 outs() << FunctionMap[Sections[SectIdx].Address + Inst.Address]->
564 getName() << ":\n";
565
566 outs() << format("%8llx:\t", Sections[SectIdx].Address +
567 Inst.Address);
568 DumpBytes(StringRef(Bytes.data() + Inst.Address, Inst.Size));
569
570 if (fi->second.contains(fi->first)) // Indent simple loops.
571 outs() << '\t';
572
573 IP->printInst(&Inst.Inst, outs(), "");
574
575 // Look for relocations inside this instructions, if there is one
576 // print its target and additional information if available.
577 for (unsigned j = 0; j != Relocs.size(); ++j)
578 if (Relocs[j].first >= Sections[SectIdx].Address + Inst.Address &&
579 Relocs[j].first < Sections[SectIdx].Address + Inst.Address +
580 Inst.Size) {
581 outs() << "\t# "
582 << MachOObj->getStringAtIndex(
583 UnsortedSymbols[Relocs[j].second].StringIndex)
584 << ' ';
585 DumpAddress(UnsortedSymbols[Relocs[j].second].Value, Sections,
586 MachOObj.get(), outs());
587 }
588
589 // If this instructions contains an address, see if we can evaluate
590 // it and print additional information.
591 uint64_t targ = InstrAnalysis->evaluateBranch(Inst.Inst,
592 Inst.Address,
593 Inst.Size);
594 if (targ != -1ULL)
595 DumpAddress(targ, Sections, MachOObj.get(), outs());
596
597 // Print debug info.
598 if (diContext) {
599 DILineInfo dli =
600 diContext->getLineInfoForAddress(Sections[SectIdx].Address +
601 Inst.Address);
602 // Print valid line info if it changed.
603 if (dli != lastLine && dli.getLine() != 0)
604 outs() << "\t## " << dli.getFileName() << ':'
605 << dli.getLine() << ':' << dli.getColumn();
606 lastLine = dli;
607 }
608
609 outs() << '\n';
610 }
611 }
612
613 emitDOTFile((f.getName().str() + ".dot").c_str(), f, IP.get());
614 }
615 }
616 }
617 }
618