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