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