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