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/ADT/OwningPtr.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
Michael J. Spencer | 1e8ba3f | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringExtras.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Triple.h" |
| 22 | #include "llvm/MC/MCAsmInfo.h" |
| 23 | #include "llvm/MC/MCDisassembler.h" |
| 24 | #include "llvm/MC/MCInst.h" |
| 25 | #include "llvm/MC/MCInstPrinter.h" |
Craig Topper | 17463b3 | 2012-04-02 06:09:36 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCInstrInfo.h" |
Jim Grosbach | c6449b6 | 2012-03-05 19:33:20 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCRegisterInfo.h" |
James Molloy | b950585 | 2011-09-07 17:24:38 +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/Archive.h" |
| 30 | #include "llvm/Object/COFF.h" |
| 31 | #include "llvm/Object/ObjectFile.h" |
Michael J. Spencer | 27781b7 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Casting.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" |
| 34 | #include "llvm/Support/Debug.h" |
Michael J. Spencer | 27781b7 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 35 | #include "llvm/Support/FileSystem.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Format.h" |
Benjamin Kramer | 853b0fd | 2011-07-25 23:04:36 +0000 | [diff] [blame] | 37 | #include "llvm/Support/GraphWriter.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Host.h" |
| 39 | #include "llvm/Support/ManagedStatic.h" |
| 40 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 41 | #include "llvm/Support/MemoryObject.h" |
| 42 | #include "llvm/Support/PrettyStackTrace.h" |
| 43 | #include "llvm/Support/Signals.h" |
| 44 | #include "llvm/Support/SourceMgr.h" |
Evan Cheng | 3e74d6f | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 45 | #include "llvm/Support/TargetRegistry.h" |
| 46 | #include "llvm/Support/TargetSelect.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 47 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 48 | #include "llvm/Support/system_error.h" |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 49 | #include <algorithm> |
Benjamin Kramer | 81bbdfd | 2012-03-23 11:49:32 +0000 | [diff] [blame] | 50 | #include <cctype> |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 51 | #include <cstring> |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 52 | using namespace llvm; |
| 53 | using namespace object; |
| 54 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 55 | static cl::list<std::string> |
| 56 | InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore); |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 57 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 58 | static cl::opt<bool> |
| 59 | Disassemble("disassemble", |
| 60 | cl::desc("Display assembler mnemonics for the machine instructions")); |
| 61 | static cl::alias |
| 62 | Disassembled("d", cl::desc("Alias for --disassemble"), |
| 63 | cl::aliasopt(Disassemble)); |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 64 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 65 | static cl::opt<bool> |
Michael J. Spencer | 27781b7 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 66 | Relocations("r", cl::desc("Display the relocation entries in the file")); |
| 67 | |
| 68 | static cl::opt<bool> |
Michael J. Spencer | 1e8ba3f | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 69 | SectionContents("s", cl::desc("Display the content of each section")); |
| 70 | |
| 71 | static cl::opt<bool> |
Michael J. Spencer | 22ff0f3 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 72 | SymbolTable("t", cl::desc("Display the symbol table")); |
| 73 | |
| 74 | static cl::opt<bool> |
Rafael Espindola | cd7ee1c | 2012-12-19 14:48:05 +0000 | [diff] [blame] | 75 | MachO("macho", cl::desc("Use MachO specific object file parser")); |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 76 | static cl::alias |
Rafael Espindola | cd7ee1c | 2012-12-19 14:48:05 +0000 | [diff] [blame] | 77 | MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachO)); |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 78 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 79 | cl::opt<std::string> |
| 80 | llvm::TripleName("triple", cl::desc("Target triple to disassemble for, " |
| 81 | "see -version for available targets")); |
| 82 | |
| 83 | cl::opt<std::string> |
| 84 | llvm::ArchName("arch", cl::desc("Target arch to disassemble for, " |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 85 | "see -version for available targets")); |
| 86 | |
Nick Lewycky | 023bb15 | 2011-10-10 21:21:34 +0000 | [diff] [blame] | 87 | static cl::opt<bool> |
| 88 | SectionHeaders("section-headers", cl::desc("Display summaries of the headers " |
| 89 | "for each section.")); |
| 90 | static cl::alias |
| 91 | SectionHeadersShort("headers", cl::desc("Alias for --section-headers"), |
| 92 | cl::aliasopt(SectionHeaders)); |
| 93 | static cl::alias |
| 94 | SectionHeadersShorter("h", cl::desc("Alias for --section-headers"), |
| 95 | cl::aliasopt(SectionHeaders)); |
| 96 | |
Jack Carter | fd6d165 | 2012-08-28 19:24:49 +0000 | [diff] [blame] | 97 | static cl::list<std::string> |
| 98 | MAttrs("mattr", |
| 99 | cl::CommaSeparated, |
| 100 | cl::desc("Target specific attributes"), |
| 101 | cl::value_desc("a1,+a2,-a3,...")); |
| 102 | |
Eli Bendersky | 8b9da53 | 2012-11-20 22:57:02 +0000 | [diff] [blame] | 103 | static cl::opt<bool> |
| 104 | NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling instructions, " |
| 105 | "do not print the instruction bytes.")); |
| 106 | |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 107 | static cl::opt<bool> |
| 108 | UnwindInfo("unwind-info", cl::desc("Display unwind information")); |
| 109 | |
| 110 | static cl::alias |
| 111 | UnwindInfoShort("u", cl::desc("Alias for --unwind-info"), |
| 112 | cl::aliasopt(UnwindInfo)); |
| 113 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 114 | static StringRef ToolName; |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 115 | |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 116 | bool llvm::error(error_code ec) { |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 117 | if (!ec) return false; |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 118 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 119 | outs() << ToolName << ": error reading file: " << ec.message() << ".\n"; |
| 120 | outs().flush(); |
| 121 | return true; |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Jim Grosbach | 3f5d1a2 | 2012-08-07 17:53:14 +0000 | [diff] [blame] | 124 | static const Target *getTarget(const ObjectFile *Obj = NULL) { |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 125 | // Figure out the target triple. |
Kevin Enderby | 9ed9e5d | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 126 | llvm::Triple TheTriple("unknown-unknown-unknown"); |
Michael J. Spencer | d11699d | 2011-01-20 07:22:04 +0000 | [diff] [blame] | 127 | if (TripleName.empty()) { |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 128 | if (Obj) |
Kevin Enderby | 9ed9e5d | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 129 | TheTriple.setArch(Triple::ArchType(Obj->getArch())); |
Michael J. Spencer | d11699d | 2011-01-20 07:22:04 +0000 | [diff] [blame] | 130 | } else |
Kevin Enderby | 9ed9e5d | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 131 | TheTriple.setTriple(Triple::normalize(TripleName)); |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 132 | |
| 133 | // Get the target specific parser. |
| 134 | std::string Error; |
Kevin Enderby | 9ed9e5d | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 135 | const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple, |
| 136 | Error); |
| 137 | if (!TheTarget) { |
| 138 | errs() << ToolName << ": " << Error; |
| 139 | return 0; |
| 140 | } |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 141 | |
Kevin Enderby | 9ed9e5d | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 142 | // Update the triple name and return the found target. |
| 143 | TripleName = TheTriple.getTriple(); |
| 144 | return TheTarget; |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 145 | } |
| 146 | |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 147 | void llvm::StringRefMemoryObject::anchor() { } |
| 148 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 149 | void llvm::DumpBytes(StringRef bytes) { |
| 150 | static const char hex_rep[] = "0123456789abcdef"; |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 151 | // FIXME: The real way to do this is to figure out the longest instruction |
| 152 | // and align to that size before printing. I'll fix this when I get |
| 153 | // around to outputting relocations. |
| 154 | // 15 is the longest x86 instruction |
| 155 | // 3 is for the hex rep of a byte + a space. |
| 156 | // 1 is for the null terminator. |
| 157 | enum { OutputSize = (15 * 3) + 1 }; |
| 158 | char output[OutputSize]; |
| 159 | |
| 160 | assert(bytes.size() <= 15 |
| 161 | && "DumpBytes only supports instructions of up to 15 bytes"); |
| 162 | memset(output, ' ', sizeof(output)); |
| 163 | unsigned index = 0; |
| 164 | for (StringRef::iterator i = bytes.begin(), |
| 165 | e = bytes.end(); i != e; ++i) { |
| 166 | output[index] = hex_rep[(*i & 0xF0) >> 4]; |
| 167 | output[index + 1] = hex_rep[*i & 0xF]; |
| 168 | index += 3; |
| 169 | } |
| 170 | |
| 171 | output[sizeof(output) - 1] = 0; |
| 172 | outs() << output; |
| 173 | } |
| 174 | |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 175 | bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) { |
Michael J. Spencer | 942eb00 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 176 | uint64_t a_addr, b_addr; |
| 177 | if (error(a.getAddress(a_addr))) return false; |
| 178 | if (error(b.getAddress(b_addr))) return false; |
| 179 | return a_addr < b_addr; |
| 180 | } |
| 181 | |
| 182 | static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) { |
Jim Grosbach | 3f5d1a2 | 2012-08-07 17:53:14 +0000 | [diff] [blame] | 183 | const Target *TheTarget = getTarget(Obj); |
| 184 | // getTarget() will have already issued a diagnostic if necessary, so |
| 185 | // just bail here if it failed. |
| 186 | if (!TheTarget) |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 187 | return; |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 188 | |
Jack Carter | fd6d165 | 2012-08-28 19:24:49 +0000 | [diff] [blame] | 189 | // Package up features to be passed to target/subtarget |
| 190 | std::string FeaturesStr; |
| 191 | if (MAttrs.size()) { |
| 192 | SubtargetFeatures Features; |
| 193 | for (unsigned i = 0; i != MAttrs.size(); ++i) |
| 194 | Features.AddFeature(MAttrs[i]); |
| 195 | FeaturesStr = Features.getString(); |
| 196 | } |
| 197 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 198 | error_code ec; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 199 | for (section_iterator i = Obj->begin_sections(), |
Michael J. Spencer | 27781b7 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 200 | e = Obj->end_sections(); |
| 201 | i != e; i.increment(ec)) { |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 202 | if (error(ec)) break; |
| 203 | bool text; |
| 204 | if (error(i->isText(text))) break; |
| 205 | if (!text) continue; |
| 206 | |
Michael J. Spencer | 942eb00 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 207 | uint64_t SectionAddr; |
| 208 | if (error(i->getAddress(SectionAddr))) break; |
| 209 | |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 210 | // Make a list of all the symbols in this section. |
| 211 | std::vector<std::pair<uint64_t, StringRef> > Symbols; |
Michael J. Spencer | 4344b1e | 2011-10-07 19:25:32 +0000 | [diff] [blame] | 212 | for (symbol_iterator si = Obj->begin_symbols(), |
Michael J. Spencer | 27781b7 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 213 | se = Obj->end_symbols(); |
| 214 | si != se; si.increment(ec)) { |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 215 | bool contains; |
| 216 | if (!error(i->containsSymbol(*si, contains)) && contains) { |
| 217 | uint64_t Address; |
Danil Malyshev | b0436a7 | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 218 | if (error(si->getAddress(Address))) break; |
Cameron Zwarich | aab2191 | 2012-02-03 04:13:37 +0000 | [diff] [blame] | 219 | Address -= SectionAddr; |
| 220 | |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 221 | StringRef Name; |
| 222 | if (error(si->getName(Name))) break; |
| 223 | Symbols.push_back(std::make_pair(Address, Name)); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // Sort the symbols by address, just in case they didn't come in that way. |
| 228 | array_pod_sort(Symbols.begin(), Symbols.end()); |
| 229 | |
Michael J. Spencer | 942eb00 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 230 | // Make a list of all the relocations for this section. |
| 231 | std::vector<RelocationRef> Rels; |
| 232 | if (InlineRelocs) { |
| 233 | for (relocation_iterator ri = i->begin_relocations(), |
| 234 | re = i->end_relocations(); |
Jim Grosbach | 3f5d1a2 | 2012-08-07 17:53:14 +0000 | [diff] [blame] | 235 | ri != re; ri.increment(ec)) { |
Michael J. Spencer | 942eb00 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 236 | if (error(ec)) break; |
| 237 | Rels.push_back(*ri); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // Sort relocations by address. |
| 242 | std::sort(Rels.begin(), Rels.end(), RelocAddressLess); |
| 243 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 244 | StringRef name; |
| 245 | if (error(i->getName(name))) break; |
Rafael Espindola | cd7ee1c | 2012-12-19 14:48:05 +0000 | [diff] [blame] | 246 | outs() << "Disassembly of section " << name << ':'; |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 247 | |
| 248 | // If the section has no symbols just insert a dummy one and disassemble |
| 249 | // the whole section. |
| 250 | if (Symbols.empty()) |
| 251 | Symbols.push_back(std::make_pair(0, name)); |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 252 | |
| 253 | // Set up disassembler. |
Evan Cheng | 1abf2cb | 2011-07-14 23:50:31 +0000 | [diff] [blame] | 254 | OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createMCAsmInfo(TripleName)); |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 255 | |
| 256 | if (!AsmInfo) { |
| 257 | errs() << "error: no assembly info for target " << TripleName << "\n"; |
| 258 | return; |
| 259 | } |
| 260 | |
Michael J. Spencer | 27781b7 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 261 | OwningPtr<const MCSubtargetInfo> STI( |
Jack Carter | fd6d165 | 2012-08-28 19:24:49 +0000 | [diff] [blame] | 262 | TheTarget->createMCSubtargetInfo(TripleName, "", FeaturesStr)); |
James Molloy | b950585 | 2011-09-07 17:24:38 +0000 | [diff] [blame] | 263 | |
| 264 | if (!STI) { |
| 265 | errs() << "error: no subtarget info for target " << TripleName << "\n"; |
| 266 | return; |
| 267 | } |
| 268 | |
Owen Anderson | 10c044e | 2011-10-27 21:55:13 +0000 | [diff] [blame] | 269 | OwningPtr<const MCDisassembler> DisAsm( |
Michael J. Spencer | 27781b7 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 270 | TheTarget->createMCDisassembler(*STI)); |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 271 | if (!DisAsm) { |
| 272 | errs() << "error: no disassembler for target " << TripleName << "\n"; |
| 273 | return; |
| 274 | } |
| 275 | |
Jim Grosbach | c6449b6 | 2012-03-05 19:33:20 +0000 | [diff] [blame] | 276 | OwningPtr<const MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName)); |
| 277 | if (!MRI) { |
| 278 | errs() << "error: no register info for target " << TripleName << "\n"; |
| 279 | return; |
| 280 | } |
| 281 | |
Craig Topper | 17463b3 | 2012-04-02 06:09:36 +0000 | [diff] [blame] | 282 | OwningPtr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo()); |
| 283 | if (!MII) { |
| 284 | errs() << "error: no instruction info for target " << TripleName << "\n"; |
| 285 | return; |
| 286 | } |
| 287 | |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 288 | int AsmPrinterVariant = AsmInfo->getAssemblerDialect(); |
| 289 | OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter( |
Craig Topper | 17463b3 | 2012-04-02 06:09:36 +0000 | [diff] [blame] | 290 | AsmPrinterVariant, *AsmInfo, *MII, *MRI, *STI)); |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 291 | if (!IP) { |
Michael J. Spencer | 27781b7 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 292 | errs() << "error: no instruction printer for target " << TripleName |
| 293 | << '\n'; |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 294 | return; |
| 295 | } |
| 296 | |
Michael J. Spencer | 25b1577 | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 297 | StringRef Bytes; |
| 298 | if (error(i->getContents(Bytes))) break; |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 299 | StringRefMemoryObject memoryObject(Bytes); |
| 300 | uint64_t Size; |
| 301 | uint64_t Index; |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 302 | uint64_t SectSize; |
| 303 | if (error(i->getSize(SectSize))) break; |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 304 | |
Michael J. Spencer | 942eb00 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 305 | std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin(); |
| 306 | std::vector<RelocationRef>::const_iterator rel_end = Rels.end(); |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 307 | // Disassemble symbol by symbol. |
| 308 | for (unsigned si = 0, se = Symbols.size(); si != se; ++si) { |
| 309 | uint64_t Start = Symbols[si].first; |
Michael J. Spencer | 178dbd4 | 2011-10-13 20:37:08 +0000 | [diff] [blame] | 310 | uint64_t End; |
| 311 | // The end is either the size of the section or the beginning of the next |
| 312 | // symbol. |
| 313 | if (si == se - 1) |
| 314 | End = SectSize; |
| 315 | // Make sure this symbol takes up space. |
| 316 | else if (Symbols[si + 1].first != Start) |
| 317 | End = Symbols[si + 1].first - 1; |
| 318 | else |
| 319 | // This symbol has the same address as the next symbol. Skip it. |
| 320 | continue; |
| 321 | |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 322 | outs() << '\n' << Symbols[si].second << ":\n"; |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 323 | |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 324 | #ifndef NDEBUG |
| 325 | raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls(); |
| 326 | #else |
| 327 | raw_ostream &DebugOut = nulls(); |
| 328 | #endif |
| 329 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 330 | for (Index = Start; Index < End; Index += Size) { |
| 331 | MCInst Inst; |
Owen Anderson | 98c5dda | 2011-09-15 23:38:46 +0000 | [diff] [blame] | 332 | |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 333 | if (DisAsm->getInstruction(Inst, Size, memoryObject, Index, |
| 334 | DebugOut, nulls())) { |
Eli Bendersky | 8b9da53 | 2012-11-20 22:57:02 +0000 | [diff] [blame] | 335 | outs() << format("%8" PRIx64 ":", SectionAddr + Index); |
| 336 | if (!NoShowRawInsn) { |
| 337 | outs() << "\t"; |
| 338 | DumpBytes(StringRef(Bytes.data() + Index, Size)); |
| 339 | } |
Benjamin Kramer | 0b8b771 | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 340 | IP->printInst(&Inst, outs(), ""); |
| 341 | outs() << "\n"; |
| 342 | } else { |
| 343 | errs() << ToolName << ": warning: invalid instruction encoding\n"; |
| 344 | if (Size == 0) |
| 345 | Size = 1; // skip illegible bytes |
Benjamin Kramer | 739b65b | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 346 | } |
Michael J. Spencer | 942eb00 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 347 | |
| 348 | // Print relocation for instruction. |
| 349 | while (rel_cur != rel_end) { |
Owen Anderson | 0685e94 | 2011-10-25 20:35:53 +0000 | [diff] [blame] | 350 | bool hidden = false; |
Michael J. Spencer | 942eb00 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 351 | uint64_t addr; |
| 352 | SmallString<16> name; |
| 353 | SmallString<32> val; |
Owen Anderson | 0685e94 | 2011-10-25 20:35:53 +0000 | [diff] [blame] | 354 | |
| 355 | // If this relocation is hidden, skip it. |
| 356 | if (error(rel_cur->getHidden(hidden))) goto skip_print_rel; |
| 357 | if (hidden) goto skip_print_rel; |
| 358 | |
Michael J. Spencer | 942eb00 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 359 | if (error(rel_cur->getAddress(addr))) goto skip_print_rel; |
| 360 | // Stop when rel_cur's address is past the current instruction. |
Owen Anderson | 34749ce | 2011-10-25 20:15:39 +0000 | [diff] [blame] | 361 | if (addr >= Index + Size) break; |
Michael J. Spencer | 942eb00 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 362 | if (error(rel_cur->getTypeName(name))) goto skip_print_rel; |
| 363 | if (error(rel_cur->getValueString(val))) goto skip_print_rel; |
| 364 | |
Benjamin Kramer | 51cf866 | 2012-03-10 02:04:38 +0000 | [diff] [blame] | 365 | outs() << format("\t\t\t%8" PRIx64 ": ", SectionAddr + addr) << name |
| 366 | << "\t" << val << "\n"; |
Michael J. Spencer | 942eb00 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 367 | |
| 368 | skip_print_rel: |
| 369 | ++rel_cur; |
| 370 | } |
Benjamin Kramer | 685a250 | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 371 | } |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
Michael J. Spencer | 27781b7 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 376 | static void PrintRelocations(const ObjectFile *o) { |
| 377 | error_code ec; |
| 378 | for (section_iterator si = o->begin_sections(), se = o->end_sections(); |
| 379 | si != se; si.increment(ec)){ |
| 380 | if (error(ec)) return; |
| 381 | if (si->begin_relocations() == si->end_relocations()) |
| 382 | continue; |
| 383 | StringRef secname; |
| 384 | if (error(si->getName(secname))) continue; |
| 385 | outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n"; |
| 386 | for (relocation_iterator ri = si->begin_relocations(), |
| 387 | re = si->end_relocations(); |
| 388 | ri != re; ri.increment(ec)) { |
| 389 | if (error(ec)) return; |
| 390 | |
Owen Anderson | 0685e94 | 2011-10-25 20:35:53 +0000 | [diff] [blame] | 391 | bool hidden; |
Michael J. Spencer | 27781b7 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 392 | uint64_t address; |
| 393 | SmallString<32> relocname; |
| 394 | SmallString<32> valuestr; |
Owen Anderson | 0685e94 | 2011-10-25 20:35:53 +0000 | [diff] [blame] | 395 | if (error(ri->getHidden(hidden))) continue; |
| 396 | if (hidden) continue; |
Michael J. Spencer | 27781b7 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 397 | if (error(ri->getTypeName(relocname))) continue; |
| 398 | if (error(ri->getAddress(address))) continue; |
| 399 | if (error(ri->getValueString(valuestr))) continue; |
| 400 | outs() << address << " " << relocname << " " << valuestr << "\n"; |
| 401 | } |
| 402 | outs() << "\n"; |
| 403 | } |
| 404 | } |
| 405 | |
Nick Lewycky | 023bb15 | 2011-10-10 21:21:34 +0000 | [diff] [blame] | 406 | static void PrintSectionHeaders(const ObjectFile *o) { |
| 407 | outs() << "Sections:\n" |
| 408 | "Idx Name Size Address Type\n"; |
| 409 | error_code ec; |
| 410 | unsigned i = 0; |
| 411 | for (section_iterator si = o->begin_sections(), se = o->end_sections(); |
| 412 | si != se; si.increment(ec)) { |
| 413 | if (error(ec)) return; |
| 414 | StringRef Name; |
| 415 | if (error(si->getName(Name))) return; |
| 416 | uint64_t Address; |
| 417 | if (error(si->getAddress(Address))) return; |
| 418 | uint64_t Size; |
| 419 | if (error(si->getSize(Size))) return; |
| 420 | bool Text, Data, BSS; |
| 421 | if (error(si->isText(Text))) return; |
| 422 | if (error(si->isData(Data))) return; |
| 423 | if (error(si->isBSS(BSS))) return; |
| 424 | std::string Type = (std::string(Text ? "TEXT " : "") + |
Michael J. Spencer | 14a5f46 | 2011-10-13 20:37:20 +0000 | [diff] [blame] | 425 | (Data ? "DATA " : "") + (BSS ? "BSS" : "")); |
Benjamin Kramer | 51cf866 | 2012-03-10 02:04:38 +0000 | [diff] [blame] | 426 | outs() << format("%3d %-13s %09" PRIx64 " %017" PRIx64 " %s\n", |
| 427 | i, Name.str().c_str(), Size, Address, Type.c_str()); |
Nick Lewycky | 023bb15 | 2011-10-10 21:21:34 +0000 | [diff] [blame] | 428 | ++i; |
| 429 | } |
| 430 | } |
| 431 | |
Michael J. Spencer | 1e8ba3f | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 432 | static void PrintSectionContents(const ObjectFile *o) { |
| 433 | error_code ec; |
| 434 | for (section_iterator si = o->begin_sections(), |
| 435 | se = o->end_sections(); |
| 436 | si != se; si.increment(ec)) { |
| 437 | if (error(ec)) return; |
| 438 | StringRef Name; |
| 439 | StringRef Contents; |
| 440 | uint64_t BaseAddr; |
| 441 | if (error(si->getName(Name))) continue; |
| 442 | if (error(si->getContents(Contents))) continue; |
| 443 | if (error(si->getAddress(BaseAddr))) continue; |
| 444 | |
| 445 | outs() << "Contents of section " << Name << ":\n"; |
| 446 | |
| 447 | // Dump out the content as hex and printable ascii characters. |
| 448 | for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) { |
Benjamin Kramer | 51cf866 | 2012-03-10 02:04:38 +0000 | [diff] [blame] | 449 | outs() << format(" %04" PRIx64 " ", BaseAddr + addr); |
Michael J. Spencer | 1e8ba3f | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 450 | // Dump line of hex. |
| 451 | for (std::size_t i = 0; i < 16; ++i) { |
| 452 | if (i != 0 && i % 4 == 0) |
| 453 | outs() << ' '; |
| 454 | if (addr + i < end) |
| 455 | outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true) |
| 456 | << hexdigit(Contents[addr + i] & 0xF, true); |
| 457 | else |
| 458 | outs() << " "; |
| 459 | } |
| 460 | // Print ascii. |
| 461 | outs() << " "; |
| 462 | for (std::size_t i = 0; i < 16 && addr + i < end; ++i) { |
| 463 | if (std::isprint(Contents[addr + i] & 0xFF)) |
| 464 | outs() << Contents[addr + i]; |
| 465 | else |
| 466 | outs() << "."; |
| 467 | } |
| 468 | outs() << "\n"; |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
Michael J. Spencer | 22ff0f3 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 473 | static void PrintCOFFSymbolTable(const COFFObjectFile *coff) { |
| 474 | const coff_file_header *header; |
| 475 | if (error(coff->getHeader(header))) return; |
| 476 | int aux_count = 0; |
| 477 | const coff_symbol *symbol = 0; |
| 478 | for (int i = 0, e = header->NumberOfSymbols; i != e; ++i) { |
| 479 | if (aux_count--) { |
| 480 | // Figure out which type of aux this is. |
| 481 | if (symbol->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC |
| 482 | && symbol->Value == 0) { // Section definition. |
| 483 | const coff_aux_section_definition *asd; |
| 484 | if (error(coff->getAuxSymbol<coff_aux_section_definition>(i, asd))) |
| 485 | return; |
| 486 | outs() << "AUX " |
| 487 | << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x " |
| 488 | , unsigned(asd->Length) |
| 489 | , unsigned(asd->NumberOfRelocations) |
| 490 | , unsigned(asd->NumberOfLinenumbers) |
| 491 | , unsigned(asd->CheckSum)) |
| 492 | << format("assoc %d comdat %d\n" |
| 493 | , unsigned(asd->Number) |
| 494 | , unsigned(asd->Selection)); |
Jim Grosbach | 3f5d1a2 | 2012-08-07 17:53:14 +0000 | [diff] [blame] | 495 | } else |
Michael J. Spencer | 22ff0f3 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 496 | outs() << "AUX Unknown\n"; |
Michael J. Spencer | 22ff0f3 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 497 | } else { |
| 498 | StringRef name; |
| 499 | if (error(coff->getSymbol(i, symbol))) return; |
| 500 | if (error(coff->getSymbolName(symbol, name))) return; |
| 501 | outs() << "[" << format("%2d", i) << "]" |
| 502 | << "(sec " << format("%2d", int(symbol->SectionNumber)) << ")" |
| 503 | << "(fl 0x00)" // Flag bits, which COFF doesn't have. |
| 504 | << "(ty " << format("%3x", unsigned(symbol->Type)) << ")" |
| 505 | << "(scl " << format("%3x", unsigned(symbol->StorageClass)) << ") " |
| 506 | << "(nx " << unsigned(symbol->NumberOfAuxSymbols) << ") " |
| 507 | << "0x" << format("%08x", unsigned(symbol->Value)) << " " |
| 508 | << name << "\n"; |
| 509 | aux_count = symbol->NumberOfAuxSymbols; |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | static void PrintSymbolTable(const ObjectFile *o) { |
| 515 | outs() << "SYMBOL TABLE:\n"; |
| 516 | |
| 517 | if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) |
| 518 | PrintCOFFSymbolTable(coff); |
| 519 | else { |
| 520 | error_code ec; |
| 521 | for (symbol_iterator si = o->begin_symbols(), |
| 522 | se = o->end_symbols(); si != se; si.increment(ec)) { |
| 523 | if (error(ec)) return; |
| 524 | StringRef Name; |
Danil Malyshev | b0436a7 | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 525 | uint64_t Address; |
Michael J. Spencer | 22ff0f3 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 526 | SymbolRef::Type Type; |
Michael J. Spencer | 22ff0f3 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 527 | uint64_t Size; |
David Meyer | c46255a | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 528 | uint32_t Flags; |
Michael J. Spencer | 22ff0f3 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 529 | section_iterator Section = o->end_sections(); |
| 530 | if (error(si->getName(Name))) continue; |
Danil Malyshev | b0436a7 | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 531 | if (error(si->getAddress(Address))) continue; |
David Meyer | c46255a | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 532 | if (error(si->getFlags(Flags))) continue; |
Michael J. Spencer | 22ff0f3 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 533 | if (error(si->getType(Type))) continue; |
Michael J. Spencer | 22ff0f3 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 534 | if (error(si->getSize(Size))) continue; |
| 535 | if (error(si->getSection(Section))) continue; |
| 536 | |
David Meyer | c46255a | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 537 | bool Global = Flags & SymbolRef::SF_Global; |
| 538 | bool Weak = Flags & SymbolRef::SF_Weak; |
| 539 | bool Absolute = Flags & SymbolRef::SF_Absolute; |
| 540 | |
Danil Malyshev | b0436a7 | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 541 | if (Address == UnknownAddressOrSize) |
| 542 | Address = 0; |
| 543 | if (Size == UnknownAddressOrSize) |
| 544 | Size = 0; |
Michael J. Spencer | 22ff0f3 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 545 | char GlobLoc = ' '; |
David Meyer | 2c67727 | 2012-02-29 02:11:55 +0000 | [diff] [blame] | 546 | if (Type != SymbolRef::ST_Unknown) |
Michael J. Spencer | 22ff0f3 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 547 | GlobLoc = Global ? 'g' : 'l'; |
| 548 | char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File) |
| 549 | ? 'd' : ' '; |
| 550 | char FileFunc = ' '; |
| 551 | if (Type == SymbolRef::ST_File) |
| 552 | FileFunc = 'f'; |
| 553 | else if (Type == SymbolRef::ST_Function) |
| 554 | FileFunc = 'F'; |
| 555 | |
Benjamin Kramer | 51cf866 | 2012-03-10 02:04:38 +0000 | [diff] [blame] | 556 | outs() << format("%08" PRIx64, Address) << " " |
Michael J. Spencer | 22ff0f3 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 557 | << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' ' |
| 558 | << (Weak ? 'w' : ' ') // Weak? |
| 559 | << ' ' // Constructor. Not supported yet. |
| 560 | << ' ' // Warning. Not supported yet. |
| 561 | << ' ' // Indirect reference to another symbol. |
| 562 | << Debug // Debugging (d) or dynamic (D) symbol. |
| 563 | << FileFunc // Name of function (F), file (f) or object (O). |
| 564 | << ' '; |
| 565 | if (Absolute) |
| 566 | outs() << "*ABS*"; |
| 567 | else if (Section == o->end_sections()) |
| 568 | outs() << "*UND*"; |
| 569 | else { |
| 570 | StringRef SectionName; |
| 571 | if (error(Section->getName(SectionName))) |
| 572 | SectionName = ""; |
| 573 | outs() << SectionName; |
| 574 | } |
| 575 | outs() << '\t' |
Benjamin Kramer | 51cf866 | 2012-03-10 02:04:38 +0000 | [diff] [blame] | 576 | << format("%08" PRIx64 " ", Size) |
Michael J. Spencer | 22ff0f3 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 577 | << Name |
| 578 | << '\n'; |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 583 | static void PrintUnwindInfo(const ObjectFile *o) { |
| 584 | outs() << "Unwind info:\n\n"; |
| 585 | |
| 586 | if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) { |
| 587 | printCOFFUnwindInfo(coff); |
| 588 | } else { |
| 589 | // TODO: Extract DWARF dump tool to objdump. |
| 590 | errs() << "This operation is only currently supported " |
| 591 | "for COFF object files.\n"; |
| 592 | return; |
| 593 | } |
| 594 | } |
| 595 | |
Michael J. Spencer | 27781b7 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 596 | static void DumpObject(const ObjectFile *o) { |
Michael J. Spencer | 1e8ba3f | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 597 | outs() << '\n'; |
| 598 | outs() << o->getFileName() |
| 599 | << ":\tfile format " << o->getFileFormatName() << "\n\n"; |
| 600 | |
Michael J. Spencer | 27781b7 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 601 | if (Disassemble) |
Michael J. Spencer | 942eb00 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 602 | DisassembleObject(o, Relocations); |
| 603 | if (Relocations && !Disassemble) |
Michael J. Spencer | 27781b7 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 604 | PrintRelocations(o); |
Nick Lewycky | 023bb15 | 2011-10-10 21:21:34 +0000 | [diff] [blame] | 605 | if (SectionHeaders) |
| 606 | PrintSectionHeaders(o); |
Michael J. Spencer | 1e8ba3f | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 607 | if (SectionContents) |
| 608 | PrintSectionContents(o); |
Michael J. Spencer | 22ff0f3 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 609 | if (SymbolTable) |
| 610 | PrintSymbolTable(o); |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 611 | if (UnwindInfo) |
| 612 | PrintUnwindInfo(o); |
Michael J. Spencer | 27781b7 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | /// @brief Dump each object file in \a a; |
| 616 | static void DumpArchive(const Archive *a) { |
| 617 | for (Archive::child_iterator i = a->begin_children(), |
| 618 | e = a->end_children(); i != e; ++i) { |
| 619 | OwningPtr<Binary> child; |
| 620 | if (error_code ec = i->getAsBinary(child)) { |
Michael J. Spencer | f81285c | 2011-11-16 01:24:41 +0000 | [diff] [blame] | 621 | // Ignore non-object files. |
| 622 | if (ec != object_error::invalid_file_type) |
| 623 | errs() << ToolName << ": '" << a->getFileName() << "': " << ec.message() |
| 624 | << ".\n"; |
Michael J. Spencer | 27781b7 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 625 | continue; |
| 626 | } |
| 627 | if (ObjectFile *o = dyn_cast<ObjectFile>(child.get())) |
| 628 | DumpObject(o); |
| 629 | else |
| 630 | errs() << ToolName << ": '" << a->getFileName() << "': " |
| 631 | << "Unrecognized file type.\n"; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | /// @brief Open file and figure out how to dump it. |
| 636 | static void DumpInput(StringRef file) { |
| 637 | // If file isn't stdin, check that it exists. |
| 638 | if (file != "-" && !sys::fs::exists(file)) { |
| 639 | errs() << ToolName << ": '" << file << "': " << "No such file\n"; |
| 640 | return; |
| 641 | } |
| 642 | |
Rafael Espindola | cd7ee1c | 2012-12-19 14:48:05 +0000 | [diff] [blame] | 643 | if (MachO && Disassemble) { |
Michael J. Spencer | 27781b7 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 644 | DisassembleInputMachO(file); |
| 645 | return; |
| 646 | } |
| 647 | |
| 648 | // Attempt to open the binary. |
| 649 | OwningPtr<Binary> binary; |
| 650 | if (error_code ec = createBinary(file, binary)) { |
| 651 | errs() << ToolName << ": '" << file << "': " << ec.message() << ".\n"; |
| 652 | return; |
| 653 | } |
| 654 | |
Jim Grosbach | 3f5d1a2 | 2012-08-07 17:53:14 +0000 | [diff] [blame] | 655 | if (Archive *a = dyn_cast<Archive>(binary.get())) |
Michael J. Spencer | 27781b7 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 656 | DumpArchive(a); |
Jim Grosbach | 3f5d1a2 | 2012-08-07 17:53:14 +0000 | [diff] [blame] | 657 | else if (ObjectFile *o = dyn_cast<ObjectFile>(binary.get())) |
Michael J. Spencer | 27781b7 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 658 | DumpObject(o); |
Jim Grosbach | 3f5d1a2 | 2012-08-07 17:53:14 +0000 | [diff] [blame] | 659 | else |
Michael J. Spencer | 27781b7 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 660 | errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n"; |
Michael J. Spencer | 27781b7 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 663 | int main(int argc, char **argv) { |
| 664 | // Print a stack trace if we signal out. |
| 665 | sys::PrintStackTraceOnErrorSignal(); |
| 666 | PrettyStackTraceProgram X(argc, argv); |
| 667 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 668 | |
| 669 | // Initialize targets and assembly printers/parsers. |
| 670 | llvm::InitializeAllTargetInfos(); |
Evan Cheng | e78085a | 2011-07-22 21:58:54 +0000 | [diff] [blame] | 671 | llvm::InitializeAllTargetMCs(); |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 672 | llvm::InitializeAllAsmParsers(); |
| 673 | llvm::InitializeAllDisassemblers(); |
| 674 | |
Pete Cooper | ff20496 | 2012-05-03 23:20:10 +0000 | [diff] [blame] | 675 | // Register the target printer for --version. |
| 676 | cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); |
| 677 | |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 678 | cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n"); |
| 679 | TripleName = Triple::normalize(TripleName); |
| 680 | |
| 681 | ToolName = argv[0]; |
| 682 | |
| 683 | // Defaults to a.out if no filenames specified. |
| 684 | if (InputFilenames.size() == 0) |
| 685 | InputFilenames.push_back("a.out"); |
| 686 | |
Michael J. Spencer | 22ff0f3 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 687 | if (!Disassemble |
| 688 | && !Relocations |
| 689 | && !SectionHeaders |
| 690 | && !SectionContents |
Michael J. Spencer | eef7b62 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 691 | && !SymbolTable |
| 692 | && !UnwindInfo) { |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 693 | cl::PrintHelpMessage(); |
| 694 | return 2; |
| 695 | } |
| 696 | |
Michael J. Spencer | 27781b7 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 697 | std::for_each(InputFilenames.begin(), InputFilenames.end(), |
| 698 | DumpInput); |
Michael J. Spencer | 92e1deb | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 699 | |
| 700 | return 0; |
| 701 | } |