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