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