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