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