Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 1 | //===-- llvm-objdump.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 program is a utility that works like binutils "objdump", that is, it |
| 11 | // dumps out a plethora of information about an object file depending on the |
| 12 | // flags. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 16 | #include "MCFunction.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 17 | #include "llvm/Object/ObjectFile.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/OwningPtr.h" |
| 19 | #include "llvm/ADT/Triple.h" |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCAsmInfo.h" |
| 22 | #include "llvm/MC/MCDisassembler.h" |
| 23 | #include "llvm/MC/MCInst.h" |
| 24 | #include "llvm/MC/MCInstPrinter.h" |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCInstrDesc.h" |
| 26 | #include "llvm/MC/MCInstrInfo.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CommandLine.h" |
| 28 | #include "llvm/Support/Debug.h" |
| 29 | #include "llvm/Support/Format.h" |
Benjamin Kramer | 853b0fd | 2011-07-25 23:04:36 +0000 | [diff] [blame^] | 30 | #include "llvm/Support/GraphWriter.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Host.h" |
| 32 | #include "llvm/Support/ManagedStatic.h" |
| 33 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 34 | #include "llvm/Support/MemoryObject.h" |
| 35 | #include "llvm/Support/PrettyStackTrace.h" |
| 36 | #include "llvm/Support/Signals.h" |
| 37 | #include "llvm/Support/SourceMgr.h" |
| 38 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 39 | #include "llvm/Support/system_error.h" |
| 40 | #include "llvm/Target/TargetRegistry.h" |
| 41 | #include "llvm/Target/TargetSelect.h" |
| 42 | #include <algorithm> |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 43 | #include <cstring> |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 44 | using namespace llvm; |
| 45 | using namespace object; |
| 46 | |
| 47 | namespace { |
| 48 | cl::list<std::string> |
| 49 | InputFilenames(cl::Positional, cl::desc("<input object files>"), |
| 50 | cl::ZeroOrMore); |
| 51 | |
| 52 | cl::opt<bool> |
| 53 | Disassemble("disassemble", |
| 54 | cl::desc("Display assembler mnemonics for the machine instructions")); |
| 55 | cl::alias |
| 56 | Disassembled("d", cl::desc("Alias for --disassemble"), |
| 57 | cl::aliasopt(Disassemble)); |
| 58 | |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 59 | cl::opt<bool> |
| 60 | CFG("cfg", cl::desc("Create a CFG for every symbol in the object file and" |
| 61 | "write it to a graphviz file")); |
| 62 | |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 63 | cl::opt<std::string> |
| 64 | TripleName("triple", cl::desc("Target triple to disassemble for, " |
| 65 | "see -version for available targets")); |
| 66 | |
| 67 | cl::opt<std::string> |
| 68 | ArchName("arch", cl::desc("Target arch to disassemble for, " |
| 69 | "see -version for available targets")); |
| 70 | |
| 71 | StringRef ToolName; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 72 | |
| 73 | bool error(error_code ec) { |
| 74 | if (!ec) return false; |
| 75 | |
| 76 | outs() << ToolName << ": error reading file: " << ec.message() << ".\n"; |
| 77 | outs().flush(); |
| 78 | return true; |
| 79 | } |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static const Target *GetTarget(const ObjectFile *Obj = NULL) { |
| 83 | // Figure out the target triple. |
| 84 | llvm::Triple TT("unknown-unknown-unknown"); |
Michael J. Spencer | d11699d | 2011-01-20 07:22:04 +0000 | [diff] [blame] | 85 | if (TripleName.empty()) { |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 86 | if (Obj) |
| 87 | TT.setArch(Triple::ArchType(Obj->getArch())); |
Michael J. Spencer | d11699d | 2011-01-20 07:22:04 +0000 | [diff] [blame] | 88 | } else |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 89 | TT.setTriple(Triple::normalize(TripleName)); |
| 90 | |
| 91 | if (!ArchName.empty()) |
| 92 | TT.setArchName(ArchName); |
| 93 | |
| 94 | TripleName = TT.str(); |
| 95 | |
| 96 | // Get the target specific parser. |
| 97 | std::string Error; |
| 98 | const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); |
| 99 | if (TheTarget) |
| 100 | return TheTarget; |
| 101 | |
| 102 | errs() << ToolName << ": error: unable to get target for '" << TripleName |
| 103 | << "', see --version and --triple.\n"; |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | namespace { |
| 108 | class StringRefMemoryObject : public MemoryObject { |
| 109 | private: |
| 110 | StringRef Bytes; |
| 111 | public: |
| 112 | StringRefMemoryObject(StringRef bytes) : Bytes(bytes) {} |
| 113 | |
| 114 | uint64_t getBase() const { return 0; } |
| 115 | uint64_t getExtent() const { return Bytes.size(); } |
| 116 | |
| 117 | int readByte(uint64_t Addr, uint8_t *Byte) const { |
Benjamin Kramer | 14c9246 | 2011-07-19 22:59:25 +0000 | [diff] [blame] | 118 | if (Addr >= getExtent()) |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 119 | return -1; |
| 120 | *Byte = Bytes[Addr]; |
| 121 | return 0; |
| 122 | } |
| 123 | }; |
| 124 | } |
| 125 | |
| 126 | static void DumpBytes(StringRef bytes) { |
| 127 | static char hex_rep[] = "0123456789abcdef"; |
| 128 | // FIXME: The real way to do this is to figure out the longest instruction |
| 129 | // and align to that size before printing. I'll fix this when I get |
| 130 | // around to outputting relocations. |
| 131 | // 15 is the longest x86 instruction |
| 132 | // 3 is for the hex rep of a byte + a space. |
| 133 | // 1 is for the null terminator. |
| 134 | enum { OutputSize = (15 * 3) + 1 }; |
| 135 | char output[OutputSize]; |
| 136 | |
| 137 | assert(bytes.size() <= 15 |
| 138 | && "DumpBytes only supports instructions of up to 15 bytes"); |
| 139 | memset(output, ' ', sizeof(output)); |
| 140 | unsigned index = 0; |
| 141 | for (StringRef::iterator i = bytes.begin(), |
| 142 | e = bytes.end(); i != e; ++i) { |
| 143 | output[index] = hex_rep[(*i & 0xF0) >> 4]; |
| 144 | output[index + 1] = hex_rep[*i & 0xF]; |
| 145 | index += 3; |
| 146 | } |
| 147 | |
| 148 | output[sizeof(output) - 1] = 0; |
| 149 | outs() << output; |
| 150 | } |
| 151 | |
| 152 | static void DisassembleInput(const StringRef &Filename) { |
| 153 | OwningPtr<MemoryBuffer> Buff; |
| 154 | |
| 155 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) { |
| 156 | errs() << ToolName << ": " << Filename << ": " << ec.message() << "\n"; |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | OwningPtr<ObjectFile> Obj(ObjectFile::createObjectFile(Buff.take())); |
| 161 | |
| 162 | const Target *TheTarget = GetTarget(Obj.get()); |
| 163 | if (!TheTarget) { |
| 164 | // GetTarget prints out stuff. |
| 165 | return; |
| 166 | } |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 167 | const MCInstrInfo *InstrInfo = TheTarget->createMCInstrInfo(); |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 168 | |
| 169 | outs() << '\n'; |
| 170 | outs() << Filename |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 171 | << ":\tfile format " << Obj->getFileFormatName() << "\n\n"; |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 172 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 173 | error_code ec; |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 174 | for (ObjectFile::section_iterator i = Obj->begin_sections(), |
| 175 | e = Obj->end_sections(); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 176 | i != e; i.increment(ec)) { |
| 177 | if (error(ec)) break; |
| 178 | bool text; |
| 179 | if (error(i->isText(text))) break; |
| 180 | if (!text) continue; |
| 181 | |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 182 | // Make a list of all the symbols in this section. |
| 183 | std::vector<std::pair<uint64_t, StringRef> > Symbols; |
| 184 | for (ObjectFile::symbol_iterator si = Obj->begin_symbols(), |
| 185 | se = Obj->end_symbols(); |
| 186 | si != se; si.increment(ec)) { |
| 187 | bool contains; |
| 188 | if (!error(i->containsSymbol(*si, contains)) && contains) { |
| 189 | uint64_t Address; |
| 190 | if (error(si->getAddress(Address))) break; |
| 191 | StringRef Name; |
| 192 | if (error(si->getName(Name))) break; |
| 193 | Symbols.push_back(std::make_pair(Address, Name)); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // Sort the symbols by address, just in case they didn't come in that way. |
| 198 | array_pod_sort(Symbols.begin(), Symbols.end()); |
| 199 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 200 | StringRef name; |
| 201 | if (error(i->getName(name))) break; |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 202 | outs() << "Disassembly of section " << name << ':'; |
| 203 | |
| 204 | // If the section has no symbols just insert a dummy one and disassemble |
| 205 | // the whole section. |
| 206 | if (Symbols.empty()) |
| 207 | Symbols.push_back(std::make_pair(0, name)); |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 208 | |
| 209 | // Set up disassembler. |
Evan Cheng | 1abf2cb | 2011-07-14 23:50:31 +0000 | [diff] [blame] | 210 | OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createMCAsmInfo(TripleName)); |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 211 | |
| 212 | if (!AsmInfo) { |
| 213 | errs() << "error: no assembly info for target " << TripleName << "\n"; |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | OwningPtr<const MCDisassembler> DisAsm(TheTarget->createMCDisassembler()); |
| 218 | if (!DisAsm) { |
| 219 | errs() << "error: no disassembler for target " << TripleName << "\n"; |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | int AsmPrinterVariant = AsmInfo->getAssemblerDialect(); |
| 224 | OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter( |
Evan Cheng | b262799 | 2011-07-06 19:45:42 +0000 | [diff] [blame] | 225 | AsmPrinterVariant, *AsmInfo)); |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 226 | if (!IP) { |
| 227 | errs() << "error: no instruction printer for target " << TripleName << '\n'; |
| 228 | return; |
| 229 | } |
| 230 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 231 | StringRef Bytes; |
| 232 | if (error(i->getContents(Bytes))) break; |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 233 | StringRefMemoryObject memoryObject(Bytes); |
| 234 | uint64_t Size; |
| 235 | uint64_t Index; |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 236 | uint64_t SectSize; |
| 237 | if (error(i->getSize(SectSize))) break; |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 238 | |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 239 | // Disassemble symbol by symbol. |
| 240 | for (unsigned si = 0, se = Symbols.size(); si != se; ++si) { |
| 241 | uint64_t Start = Symbols[si].first; |
| 242 | uint64_t End = si == se-1 ? SectSize : Symbols[si + 1].first - 1; |
| 243 | outs() << '\n' << Symbols[si].second << ":\n"; |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 244 | |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 245 | #ifndef NDEBUG |
| 246 | raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls(); |
| 247 | #else |
| 248 | raw_ostream &DebugOut = nulls(); |
| 249 | #endif |
| 250 | |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 251 | for (Index = Start; Index < End; Index += Size) { |
| 252 | MCInst Inst; |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 253 | if (DisAsm->getInstruction(Inst, Size, memoryObject, Index, DebugOut)) { |
| 254 | uint64_t addr; |
| 255 | if (error(i->getAddress(addr))) break; |
| 256 | outs() << format("%8x:\t", addr + Index); |
| 257 | DumpBytes(StringRef(Bytes.data() + Index, Size)); |
| 258 | IP->printInst(&Inst, outs()); |
| 259 | outs() << "\n"; |
| 260 | } else { |
| 261 | errs() << ToolName << ": warning: invalid instruction encoding\n"; |
| 262 | if (Size == 0) |
| 263 | Size = 1; // skip illegible bytes |
| 264 | } |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 265 | } |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 266 | |
| 267 | if (CFG) { |
| 268 | MCFunction f = |
| 269 | MCFunction::createFunctionFromMC(Symbols[si].second, DisAsm.get(), |
| 270 | memoryObject, Start, End, InstrInfo, |
| 271 | DebugOut); |
| 272 | |
| 273 | // Start a new dot file. |
| 274 | std::string Error; |
| 275 | raw_fd_ostream Out((f.getName().str() + ".dot").c_str(), Error); |
Benjamin Kramer | a503ede | 2011-07-22 18:35:11 +0000 | [diff] [blame] | 276 | if (!Error.empty()) { |
| 277 | errs() << ToolName << ": warning: " << Error << '\n'; |
| 278 | continue; |
| 279 | } |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 280 | |
| 281 | Out << "digraph " << f.getName() << " {\n"; |
| 282 | Out << "graph [ rankdir = \"LR\" ];\n"; |
| 283 | for (MCFunction::iterator i = f.begin(), e = f.end(); i != e; ++i) { |
Benjamin Kramer | 853b0fd | 2011-07-25 23:04:36 +0000 | [diff] [blame^] | 284 | bool hasPreds = false; |
| 285 | // Only print blocks that have predecessors. |
| 286 | // FIXME: Slow. |
| 287 | for (MCFunction::iterator pi = f.begin(), pe = f.end(); pi != pe; |
| 288 | ++pi) |
| 289 | for (pi->second->contains(&i->second)) { |
| 290 | hasPreds = true; |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | if (!hasPreds && i != f.begin()) |
| 295 | continue; |
| 296 | |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 297 | Out << '"' << (uintptr_t)&i->second << "\" [ label=\"<a>"; |
| 298 | // Print instructions. |
| 299 | for (unsigned ii = 0, ie = i->second.getInsts().size(); ii != ie; |
| 300 | ++ii) { |
Benjamin Kramer | 853b0fd | 2011-07-25 23:04:36 +0000 | [diff] [blame^] | 301 | // Escape special chars and print the instruction in mnemonic form. |
| 302 | std::string Str; |
| 303 | raw_string_ostream OS(Str); |
| 304 | IP->printInst(&i->second.getInsts()[ii].Inst, OS); |
| 305 | Out << DOT::EscapeString(OS.str()) << '|'; |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 306 | } |
| 307 | Out << "<o>\" shape=\"record\" ];\n"; |
| 308 | |
| 309 | // Add edges. |
| 310 | for (MCBasicBlock::succ_iterator si = i->second.succ_begin(), |
| 311 | se = i->second.succ_end(); si != se; ++si) |
| 312 | Out << (uintptr_t)&i->second << ":o -> " << (uintptr_t)*si <<":a\n"; |
| 313 | } |
| 314 | Out << "}\n"; |
| 315 | } |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | int main(int argc, char **argv) { |
| 321 | // Print a stack trace if we signal out. |
| 322 | sys::PrintStackTraceOnErrorSignal(); |
| 323 | PrettyStackTraceProgram X(argc, argv); |
| 324 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 325 | |
| 326 | // Initialize targets and assembly printers/parsers. |
| 327 | llvm::InitializeAllTargetInfos(); |
Evan Cheng | e78085a | 2011-07-22 21:58:54 +0000 | [diff] [blame] | 328 | llvm::InitializeAllTargetMCs(); |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 329 | llvm::InitializeAllAsmParsers(); |
| 330 | llvm::InitializeAllDisassemblers(); |
| 331 | |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 332 | cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n"); |
| 333 | TripleName = Triple::normalize(TripleName); |
| 334 | |
| 335 | ToolName = argv[0]; |
| 336 | |
| 337 | // Defaults to a.out if no filenames specified. |
| 338 | if (InputFilenames.size() == 0) |
| 339 | InputFilenames.push_back("a.out"); |
| 340 | |
| 341 | // -d is the only flag that is currently implemented, so just print help if |
| 342 | // it is not set. |
| 343 | if (!Disassemble) { |
| 344 | cl::PrintHelpMessage(); |
| 345 | return 2; |
| 346 | } |
| 347 | |
| 348 | std::for_each(InputFilenames.begin(), InputFilenames.end(), |
| 349 | DisassembleInput); |
| 350 | |
| 351 | return 0; |
| 352 | } |