MachODump.cpp 23 KB

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