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