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 | |
| 16 | #include "llvm/Object/ObjectFile.h" |
| 17 | // This config must be included before llvm-config.h. |
| 18 | #include "llvm/Config/config.h" |
| 19 | #include "../../lib/MC/MCDisassembler/EDDisassembler.h" |
| 20 | #include "../../lib/MC/MCDisassembler/EDInst.h" |
| 21 | #include "../../lib/MC/MCDisassembler/EDOperand.h" |
| 22 | #include "../../lib/MC/MCDisassembler/EDToken.h" |
| 23 | #include "llvm/ADT/OwningPtr.h" |
| 24 | #include "llvm/ADT/Triple.h" |
| 25 | #include "llvm/MC/MCAsmInfo.h" |
| 26 | #include "llvm/MC/MCDisassembler.h" |
| 27 | #include "llvm/MC/MCInst.h" |
| 28 | #include "llvm/MC/MCInstPrinter.h" |
| 29 | #include "llvm/Support/CommandLine.h" |
| 30 | #include "llvm/Support/Debug.h" |
| 31 | #include "llvm/Support/Format.h" |
| 32 | #include "llvm/Support/Host.h" |
| 33 | #include "llvm/Support/ManagedStatic.h" |
| 34 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 35 | #include "llvm/Support/MemoryObject.h" |
| 36 | #include "llvm/Support/PrettyStackTrace.h" |
| 37 | #include "llvm/Support/Signals.h" |
| 38 | #include "llvm/Support/SourceMgr.h" |
| 39 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 40 | #include "llvm/Support/system_error.h" |
| 41 | #include "llvm/Target/TargetRegistry.h" |
| 42 | #include "llvm/Target/TargetSelect.h" |
| 43 | #include <algorithm> |
| 44 | #include <cctype> |
| 45 | #include <cerrno> |
| 46 | #include <cstring> |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 47 | using namespace llvm; |
| 48 | using namespace object; |
| 49 | |
| 50 | namespace { |
| 51 | cl::list<std::string> |
| 52 | InputFilenames(cl::Positional, cl::desc("<input object files>"), |
| 53 | cl::ZeroOrMore); |
| 54 | |
| 55 | cl::opt<bool> |
| 56 | Disassemble("disassemble", |
| 57 | cl::desc("Display assembler mnemonics for the machine instructions")); |
| 58 | cl::alias |
| 59 | Disassembled("d", cl::desc("Alias for --disassemble"), |
| 60 | cl::aliasopt(Disassemble)); |
| 61 | |
| 62 | cl::opt<std::string> |
| 63 | TripleName("triple", cl::desc("Target triple to disassemble for, " |
| 64 | "see -version for available targets")); |
| 65 | |
| 66 | cl::opt<std::string> |
| 67 | ArchName("arch", cl::desc("Target arch to disassemble for, " |
| 68 | "see -version for available targets")); |
| 69 | |
| 70 | StringRef ToolName; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 71 | |
| 72 | bool error(error_code ec) { |
| 73 | if (!ec) return false; |
| 74 | |
| 75 | outs() << ToolName << ": error reading file: " << ec.message() << ".\n"; |
| 76 | outs().flush(); |
| 77 | return true; |
| 78 | } |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static const Target *GetTarget(const ObjectFile *Obj = NULL) { |
| 82 | // Figure out the target triple. |
| 83 | llvm::Triple TT("unknown-unknown-unknown"); |
Michael J. Spencer | d11699d | 2011-01-20 07:22:04 +0000 | [diff] [blame] | 84 | if (TripleName.empty()) { |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 85 | if (Obj) |
| 86 | TT.setArch(Triple::ArchType(Obj->getArch())); |
Michael J. Spencer | d11699d | 2011-01-20 07:22:04 +0000 | [diff] [blame] | 87 | } else |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 88 | TT.setTriple(Triple::normalize(TripleName)); |
| 89 | |
| 90 | if (!ArchName.empty()) |
| 91 | TT.setArchName(ArchName); |
| 92 | |
| 93 | TripleName = TT.str(); |
| 94 | |
| 95 | // Get the target specific parser. |
| 96 | std::string Error; |
| 97 | const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); |
| 98 | if (TheTarget) |
| 99 | return TheTarget; |
| 100 | |
| 101 | errs() << ToolName << ": error: unable to get target for '" << TripleName |
| 102 | << "', see --version and --triple.\n"; |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | namespace { |
| 107 | class StringRefMemoryObject : public MemoryObject { |
| 108 | private: |
| 109 | StringRef Bytes; |
| 110 | public: |
| 111 | StringRefMemoryObject(StringRef bytes) : Bytes(bytes) {} |
| 112 | |
| 113 | uint64_t getBase() const { return 0; } |
| 114 | uint64_t getExtent() const { return Bytes.size(); } |
| 115 | |
| 116 | int readByte(uint64_t Addr, uint8_t *Byte) const { |
| 117 | if (Addr > getExtent()) |
| 118 | return -1; |
| 119 | *Byte = Bytes[Addr]; |
| 120 | return 0; |
| 121 | } |
| 122 | }; |
| 123 | } |
| 124 | |
| 125 | static void DumpBytes(StringRef bytes) { |
| 126 | static char hex_rep[] = "0123456789abcdef"; |
| 127 | // FIXME: The real way to do this is to figure out the longest instruction |
| 128 | // and align to that size before printing. I'll fix this when I get |
| 129 | // around to outputting relocations. |
| 130 | // 15 is the longest x86 instruction |
| 131 | // 3 is for the hex rep of a byte + a space. |
| 132 | // 1 is for the null terminator. |
| 133 | enum { OutputSize = (15 * 3) + 1 }; |
| 134 | char output[OutputSize]; |
| 135 | |
| 136 | assert(bytes.size() <= 15 |
| 137 | && "DumpBytes only supports instructions of up to 15 bytes"); |
| 138 | memset(output, ' ', sizeof(output)); |
| 139 | unsigned index = 0; |
| 140 | for (StringRef::iterator i = bytes.begin(), |
| 141 | e = bytes.end(); i != e; ++i) { |
| 142 | output[index] = hex_rep[(*i & 0xF0) >> 4]; |
| 143 | output[index + 1] = hex_rep[*i & 0xF]; |
| 144 | index += 3; |
| 145 | } |
| 146 | |
| 147 | output[sizeof(output) - 1] = 0; |
| 148 | outs() << output; |
| 149 | } |
| 150 | |
| 151 | static void DisassembleInput(const StringRef &Filename) { |
| 152 | OwningPtr<MemoryBuffer> Buff; |
| 153 | |
| 154 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) { |
| 155 | errs() << ToolName << ": " << Filename << ": " << ec.message() << "\n"; |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | OwningPtr<ObjectFile> Obj(ObjectFile::createObjectFile(Buff.take())); |
| 160 | |
| 161 | const Target *TheTarget = GetTarget(Obj.get()); |
| 162 | if (!TheTarget) { |
| 163 | // GetTarget prints out stuff. |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | outs() << '\n'; |
| 168 | outs() << Filename |
| 169 | << ":\tfile format " << Obj->getFileFormatName() << "\n\n\n"; |
| 170 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 171 | error_code ec; |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 172 | for (ObjectFile::section_iterator i = Obj->begin_sections(), |
| 173 | e = Obj->end_sections(); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 174 | i != e; i.increment(ec)) { |
| 175 | if (error(ec)) break; |
| 176 | bool text; |
| 177 | if (error(i->isText(text))) break; |
| 178 | if (!text) continue; |
| 179 | |
| 180 | StringRef name; |
| 181 | if (error(i->getName(name))) break; |
| 182 | outs() << "Disassembly of section " << name << ":\n\n"; |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 183 | |
| 184 | // Set up disassembler. |
| 185 | OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createAsmInfo(TripleName)); |
| 186 | |
| 187 | if (!AsmInfo) { |
| 188 | errs() << "error: no assembly info for target " << TripleName << "\n"; |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | OwningPtr<const MCDisassembler> DisAsm(TheTarget->createMCDisassembler()); |
| 193 | if (!DisAsm) { |
| 194 | errs() << "error: no disassembler for target " << TripleName << "\n"; |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | int AsmPrinterVariant = AsmInfo->getAssemblerDialect(); |
| 199 | OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter( |
Evan Cheng | b262799 | 2011-07-06 19:45:42 +0000 | [diff] [blame^] | 200 | AsmPrinterVariant, *AsmInfo)); |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 201 | if (!IP) { |
| 202 | errs() << "error: no instruction printer for target " << TripleName << '\n'; |
| 203 | return; |
| 204 | } |
| 205 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 206 | StringRef Bytes; |
| 207 | if (error(i->getContents(Bytes))) break; |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 208 | StringRefMemoryObject memoryObject(Bytes); |
| 209 | uint64_t Size; |
| 210 | uint64_t Index; |
| 211 | |
| 212 | for (Index = 0; Index < Bytes.size(); Index += Size) { |
| 213 | MCInst Inst; |
| 214 | |
| 215 | # ifndef NDEBUG |
| 216 | raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls(); |
| 217 | # else |
| 218 | raw_ostream &DebugOut = nulls(); |
| 219 | # endif |
| 220 | |
| 221 | if (DisAsm->getInstruction(Inst, Size, memoryObject, Index, DebugOut)) { |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 222 | uint64_t addr; |
| 223 | if (error(i->getAddress(addr))) break; |
| 224 | outs() << format("%8x:\t", addr + Index); |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 225 | DumpBytes(StringRef(Bytes.data() + Index, Size)); |
| 226 | IP->printInst(&Inst, outs()); |
| 227 | outs() << "\n"; |
| 228 | } else { |
| 229 | errs() << ToolName << ": warning: invalid instruction encoding\n"; |
| 230 | if (Size == 0) |
| 231 | Size = 1; // skip illegible bytes |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | int main(int argc, char **argv) { |
| 238 | // Print a stack trace if we signal out. |
| 239 | sys::PrintStackTraceOnErrorSignal(); |
| 240 | PrettyStackTraceProgram X(argc, argv); |
| 241 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 242 | |
| 243 | // Initialize targets and assembly printers/parsers. |
| 244 | llvm::InitializeAllTargetInfos(); |
| 245 | // FIXME: We shouldn't need to initialize the Target(Machine)s. |
| 246 | llvm::InitializeAllTargets(); |
| 247 | llvm::InitializeAllAsmPrinters(); |
| 248 | llvm::InitializeAllAsmParsers(); |
| 249 | llvm::InitializeAllDisassemblers(); |
| 250 | |
| 251 | cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n"); |
| 252 | TripleName = Triple::normalize(TripleName); |
| 253 | |
| 254 | ToolName = argv[0]; |
| 255 | |
| 256 | // Defaults to a.out if no filenames specified. |
| 257 | if (InputFilenames.size() == 0) |
| 258 | InputFilenames.push_back("a.out"); |
| 259 | |
| 260 | // -d is the only flag that is currently implemented, so just print help if |
| 261 | // it is not set. |
| 262 | if (!Disassemble) { |
| 263 | cl::PrintHelpMessage(); |
| 264 | return 2; |
| 265 | } |
| 266 | |
| 267 | std::for_each(InputFilenames.begin(), InputFilenames.end(), |
| 268 | DisassembleInput); |
| 269 | |
| 270 | return 0; |
| 271 | } |