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