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