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