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 | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 16 | #include "llvm-objdump.h" |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 17 | #include "MCFunction.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 18 | #include "llvm/Object/ObjectFile.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/OwningPtr.h" |
| 20 | #include "llvm/ADT/Triple.h" |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/STLExtras.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +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" |
Evan Cheng | 7801136 | 2011-08-23 20:15:21 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCInstrAnalysis.h" |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCInstrDesc.h" |
| 28 | #include "llvm/MC/MCInstrInfo.h" |
James Molloy | b950585 | 2011-09-07 17:24:38 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCSubtargetInfo.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CommandLine.h" |
| 31 | #include "llvm/Support/Debug.h" |
| 32 | #include "llvm/Support/Format.h" |
Benjamin Kramer | 853b0fd | 2011-07-25 23:04:36 +0000 | [diff] [blame] | 33 | #include "llvm/Support/GraphWriter.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Host.h" |
| 35 | #include "llvm/Support/ManagedStatic.h" |
| 36 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 37 | #include "llvm/Support/MemoryObject.h" |
| 38 | #include "llvm/Support/PrettyStackTrace.h" |
| 39 | #include "llvm/Support/Signals.h" |
| 40 | #include "llvm/Support/SourceMgr.h" |
Evan Cheng | 3e74d6f | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 41 | #include "llvm/Support/TargetRegistry.h" |
| 42 | #include "llvm/Support/TargetSelect.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 43 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 44 | #include "llvm/Support/system_error.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 45 | #include <algorithm> |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 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 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 50 | static cl::list<std::string> |
| 51 | InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore); |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 52 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 53 | static cl::opt<bool> |
| 54 | Disassemble("disassemble", |
| 55 | cl::desc("Display assembler mnemonics for the machine instructions")); |
| 56 | static cl::alias |
| 57 | Disassembled("d", cl::desc("Alias for --disassemble"), |
| 58 | cl::aliasopt(Disassemble)); |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 59 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 60 | static cl::opt<bool> |
| 61 | MachO("macho", cl::desc("Use MachO specific object file parser")); |
| 62 | static cl::alias |
| 63 | MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachO)); |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 64 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 65 | cl::opt<std::string> |
| 66 | llvm::TripleName("triple", cl::desc("Target triple to disassemble for, " |
| 67 | "see -version for available targets")); |
| 68 | |
| 69 | cl::opt<std::string> |
| 70 | llvm::ArchName("arch", cl::desc("Target arch to disassemble for, " |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 71 | "see -version for available targets")); |
| 72 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 73 | static StringRef ToolName; |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 74 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 75 | static bool error(error_code ec) { |
| 76 | if (!ec) return false; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 77 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 78 | outs() << ToolName << ": error reading file: " << ec.message() << ".\n"; |
| 79 | outs().flush(); |
| 80 | return true; |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static const Target *GetTarget(const ObjectFile *Obj = NULL) { |
| 84 | // Figure out the target triple. |
| 85 | llvm::Triple TT("unknown-unknown-unknown"); |
Michael J. Spencer | d11699d | 2011-01-20 07:22:04 +0000 | [diff] [blame] | 86 | if (TripleName.empty()) { |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 87 | if (Obj) |
| 88 | TT.setArch(Triple::ArchType(Obj->getArch())); |
Michael J. Spencer | d11699d | 2011-01-20 07:22:04 +0000 | [diff] [blame] | 89 | } else |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 90 | TT.setTriple(Triple::normalize(TripleName)); |
| 91 | |
| 92 | if (!ArchName.empty()) |
| 93 | TT.setArchName(ArchName); |
| 94 | |
| 95 | TripleName = TT.str(); |
| 96 | |
| 97 | // Get the target specific parser. |
| 98 | std::string Error; |
| 99 | const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); |
| 100 | if (TheTarget) |
| 101 | return TheTarget; |
| 102 | |
| 103 | errs() << ToolName << ": error: unable to get target for '" << TripleName |
| 104 | << "', see --version and --triple.\n"; |
| 105 | return 0; |
| 106 | } |
| 107 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 108 | void llvm::DumpBytes(StringRef bytes) { |
| 109 | static const char hex_rep[] = "0123456789abcdef"; |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 110 | // FIXME: The real way to do this is to figure out the longest instruction |
| 111 | // and align to that size before printing. I'll fix this when I get |
| 112 | // around to outputting relocations. |
| 113 | // 15 is the longest x86 instruction |
| 114 | // 3 is for the hex rep of a byte + a space. |
| 115 | // 1 is for the null terminator. |
| 116 | enum { OutputSize = (15 * 3) + 1 }; |
| 117 | char output[OutputSize]; |
| 118 | |
| 119 | assert(bytes.size() <= 15 |
| 120 | && "DumpBytes only supports instructions of up to 15 bytes"); |
| 121 | memset(output, ' ', sizeof(output)); |
| 122 | unsigned index = 0; |
| 123 | for (StringRef::iterator i = bytes.begin(), |
| 124 | e = bytes.end(); i != e; ++i) { |
| 125 | output[index] = hex_rep[(*i & 0xF0) >> 4]; |
| 126 | output[index + 1] = hex_rep[*i & 0xF]; |
| 127 | index += 3; |
| 128 | } |
| 129 | |
| 130 | output[sizeof(output) - 1] = 0; |
| 131 | outs() << output; |
| 132 | } |
| 133 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 134 | void llvm::DisassembleInputLibObject(StringRef Filename) { |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 135 | OwningPtr<MemoryBuffer> Buff; |
| 136 | |
| 137 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) { |
| 138 | errs() << ToolName << ": " << Filename << ": " << ec.message() << "\n"; |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | OwningPtr<ObjectFile> Obj(ObjectFile::createObjectFile(Buff.take())); |
| 143 | |
| 144 | const Target *TheTarget = GetTarget(Obj.get()); |
| 145 | if (!TheTarget) { |
| 146 | // GetTarget prints out stuff. |
| 147 | return; |
| 148 | } |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 149 | const MCInstrInfo *InstrInfo = TheTarget->createMCInstrInfo(); |
Benjamin Kramer | 41ab14b | 2011-08-08 18:56:44 +0000 | [diff] [blame] | 150 | OwningPtr<MCInstrAnalysis> |
| 151 | InstrAnalysis(TheTarget->createMCInstrAnalysis(InstrInfo)); |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 152 | |
| 153 | outs() << '\n'; |
| 154 | outs() << Filename |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 155 | << ":\tfile format " << Obj->getFileFormatName() << "\n\n"; |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 156 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 157 | error_code ec; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame^] | 158 | for (section_iterator i = Obj->begin_sections(), |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 159 | e = Obj->end_sections(); |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 160 | i != e; i.increment(ec)) { |
| 161 | if (error(ec)) break; |
| 162 | bool text; |
| 163 | if (error(i->isText(text))) break; |
| 164 | if (!text) continue; |
| 165 | |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 166 | // Make a list of all the symbols in this section. |
| 167 | std::vector<std::pair<uint64_t, StringRef> > Symbols; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame^] | 168 | for (symbol_iterator si = Obj->begin_symbols(), |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 169 | se = Obj->end_symbols(); |
| 170 | si != se; si.increment(ec)) { |
| 171 | bool contains; |
| 172 | if (!error(i->containsSymbol(*si, contains)) && contains) { |
| 173 | uint64_t Address; |
Benjamin Kramer | ac241fe | 2011-09-14 01:22:52 +0000 | [diff] [blame] | 174 | if (error(si->getOffset(Address))) break; |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 175 | StringRef Name; |
| 176 | if (error(si->getName(Name))) break; |
| 177 | Symbols.push_back(std::make_pair(Address, Name)); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // Sort the symbols by address, just in case they didn't come in that way. |
| 182 | array_pod_sort(Symbols.begin(), Symbols.end()); |
| 183 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 184 | StringRef name; |
| 185 | if (error(i->getName(name))) break; |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 186 | outs() << "Disassembly of section " << name << ':'; |
| 187 | |
| 188 | // If the section has no symbols just insert a dummy one and disassemble |
| 189 | // the whole section. |
| 190 | if (Symbols.empty()) |
| 191 | Symbols.push_back(std::make_pair(0, name)); |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 192 | |
| 193 | // Set up disassembler. |
Evan Cheng | 1abf2cb | 2011-07-14 23:50:31 +0000 | [diff] [blame] | 194 | OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createMCAsmInfo(TripleName)); |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 195 | |
| 196 | if (!AsmInfo) { |
| 197 | errs() << "error: no assembly info for target " << TripleName << "\n"; |
| 198 | return; |
| 199 | } |
| 200 | |
James Molloy | b950585 | 2011-09-07 17:24:38 +0000 | [diff] [blame] | 201 | OwningPtr<const MCSubtargetInfo> STI(TheTarget->createMCSubtargetInfo(TripleName, "", "")); |
| 202 | |
| 203 | if (!STI) { |
| 204 | errs() << "error: no subtarget info for target " << TripleName << "\n"; |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | OwningPtr<const MCDisassembler> DisAsm(TheTarget->createMCDisassembler(*STI)); |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 209 | if (!DisAsm) { |
| 210 | errs() << "error: no disassembler for target " << TripleName << "\n"; |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | int AsmPrinterVariant = AsmInfo->getAssemblerDialect(); |
| 215 | OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter( |
James Molloy | b950585 | 2011-09-07 17:24:38 +0000 | [diff] [blame] | 216 | AsmPrinterVariant, *AsmInfo, *STI)); |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 217 | if (!IP) { |
| 218 | errs() << "error: no instruction printer for target " << TripleName << '\n'; |
| 219 | return; |
| 220 | } |
| 221 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 222 | StringRef Bytes; |
| 223 | if (error(i->getContents(Bytes))) break; |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 224 | StringRefMemoryObject memoryObject(Bytes); |
| 225 | uint64_t Size; |
| 226 | uint64_t Index; |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 227 | uint64_t SectSize; |
| 228 | if (error(i->getSize(SectSize))) break; |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 229 | |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 230 | // Disassemble symbol by symbol. |
| 231 | for (unsigned si = 0, se = Symbols.size(); si != se; ++si) { |
| 232 | uint64_t Start = Symbols[si].first; |
| 233 | uint64_t End = si == se-1 ? SectSize : Symbols[si + 1].first - 1; |
| 234 | outs() << '\n' << Symbols[si].second << ":\n"; |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 235 | |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 236 | #ifndef NDEBUG |
| 237 | raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls(); |
| 238 | #else |
| 239 | raw_ostream &DebugOut = nulls(); |
| 240 | #endif |
| 241 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 242 | for (Index = Start; Index < End; Index += Size) { |
| 243 | MCInst Inst; |
Owen Anderson | 98c5dda | 2011-09-15 23:38:46 +0000 | [diff] [blame] | 244 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 245 | if (DisAsm->getInstruction(Inst, Size, memoryObject, Index, |
| 246 | DebugOut, nulls())) { |
| 247 | uint64_t addr; |
| 248 | if (error(i->getAddress(addr))) break; |
| 249 | outs() << format("%8x:\t", addr + Index); |
| 250 | DumpBytes(StringRef(Bytes.data() + Index, Size)); |
| 251 | IP->printInst(&Inst, outs(), ""); |
| 252 | outs() << "\n"; |
| 253 | } else { |
| 254 | errs() << ToolName << ": warning: invalid instruction encoding\n"; |
| 255 | if (Size == 0) |
| 256 | Size = 1; // skip illegible bytes |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 257 | } |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 258 | } |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | int main(int argc, char **argv) { |
| 264 | // Print a stack trace if we signal out. |
| 265 | sys::PrintStackTraceOnErrorSignal(); |
| 266 | PrettyStackTraceProgram X(argc, argv); |
| 267 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 268 | |
| 269 | // Initialize targets and assembly printers/parsers. |
| 270 | llvm::InitializeAllTargetInfos(); |
Evan Cheng | e78085a | 2011-07-22 21:58:54 +0000 | [diff] [blame] | 271 | llvm::InitializeAllTargetMCs(); |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 272 | llvm::InitializeAllAsmParsers(); |
| 273 | llvm::InitializeAllDisassemblers(); |
| 274 | |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 275 | cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n"); |
| 276 | TripleName = Triple::normalize(TripleName); |
| 277 | |
| 278 | ToolName = argv[0]; |
| 279 | |
| 280 | // Defaults to a.out if no filenames specified. |
| 281 | if (InputFilenames.size() == 0) |
| 282 | InputFilenames.push_back("a.out"); |
| 283 | |
| 284 | // -d is the only flag that is currently implemented, so just print help if |
| 285 | // it is not set. |
| 286 | if (!Disassemble) { |
| 287 | cl::PrintHelpMessage(); |
| 288 | return 2; |
| 289 | } |
| 290 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 291 | if (MachO) |
| 292 | std::for_each(InputFilenames.begin(), InputFilenames.end(), |
| 293 | DisassembleInputMachO); |
| 294 | else |
| 295 | std::for_each(InputFilenames.begin(), InputFilenames.end(), |
| 296 | DisassembleInputLibObject); |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 297 | |
| 298 | return 0; |
| 299 | } |