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