David Majnemer | f45bbd0 | 2015-03-15 01:30:58 +0000 | [diff] [blame] | 1 | //===- llvm-cxxdump.cpp - Dump C++ data in an Object File -------*- C++ -*-===// |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 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 | // |
David Majnemer | f45bbd0 | 2015-03-15 01:30:58 +0000 | [diff] [blame] | 10 | // Dumps C++ data resident in object files and archives. |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
David Majnemer | f45bbd0 | 2015-03-15 01:30:58 +0000 | [diff] [blame] | 14 | #include "llvm-cxxdump.h" |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 15 | #include "Error.h" |
| 16 | #include "llvm/ADT/ArrayRef.h" |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 17 | #include "llvm/Object/Archive.h" |
| 18 | #include "llvm/Object/ObjectFile.h" |
Rafael Espindola | a4a4093 | 2015-06-23 02:20:37 +0000 | [diff] [blame] | 19 | #include "llvm/Object/SymbolSize.h" |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
| 21 | #include "llvm/Support/Endian.h" |
| 22 | #include "llvm/Support/FileSystem.h" |
| 23 | #include "llvm/Support/ManagedStatic.h" |
| 24 | #include "llvm/Support/PrettyStackTrace.h" |
| 25 | #include "llvm/Support/Signals.h" |
| 26 | #include "llvm/Support/TargetRegistry.h" |
| 27 | #include "llvm/Support/TargetSelect.h" |
Benjamin Kramer | 16132e6 | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 29 | #include <map> |
| 30 | #include <string> |
| 31 | #include <system_error> |
| 32 | |
| 33 | using namespace llvm; |
| 34 | using namespace llvm::object; |
| 35 | using namespace llvm::support; |
| 36 | |
| 37 | namespace opts { |
| 38 | cl::list<std::string> InputFilenames(cl::Positional, |
| 39 | cl::desc("<input object files>"), |
| 40 | cl::ZeroOrMore); |
| 41 | } // namespace opts |
| 42 | |
| 43 | static int ReturnValue = EXIT_SUCCESS; |
| 44 | |
| 45 | namespace llvm { |
| 46 | |
Benjamin Kramer | f044d3f | 2015-03-09 16:23:46 +0000 | [diff] [blame] | 47 | static bool error(std::error_code EC) { |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 48 | if (!EC) |
| 49 | return false; |
| 50 | |
| 51 | ReturnValue = EXIT_FAILURE; |
| 52 | outs() << "\nError reading file: " << EC.message() << ".\n"; |
| 53 | outs().flush(); |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | } // namespace llvm |
| 58 | |
| 59 | static void reportError(StringRef Input, StringRef Message) { |
| 60 | if (Input == "-") |
| 61 | Input = "<stdin>"; |
| 62 | |
| 63 | errs() << Input << ": " << Message << "\n"; |
| 64 | errs().flush(); |
| 65 | ReturnValue = EXIT_FAILURE; |
| 66 | } |
| 67 | |
| 68 | static void reportError(StringRef Input, std::error_code EC) { |
| 69 | reportError(Input, EC.message()); |
| 70 | } |
| 71 | |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 72 | static SmallVectorImpl<SectionRef> &getRelocSections(const ObjectFile *Obj, |
| 73 | const SectionRef &Sec) { |
| 74 | static bool MappingDone = false; |
| 75 | static std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap; |
| 76 | if (!MappingDone) { |
| 77 | for (const SectionRef &Section : Obj->sections()) { |
| 78 | section_iterator Sec2 = Section.getRelocatedSection(); |
| 79 | if (Sec2 != Obj->section_end()) |
| 80 | SectionRelocMap[*Sec2].push_back(Section); |
| 81 | } |
| 82 | MappingDone = true; |
| 83 | } |
| 84 | return SectionRelocMap[Sec]; |
| 85 | } |
| 86 | |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 87 | static bool collectRelocatedSymbols(const ObjectFile *Obj, |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 88 | const SectionRef &Sec, uint64_t SecAddress, |
| 89 | uint64_t SymAddress, uint64_t SymSize, |
| 90 | StringRef *I, StringRef *E) { |
| 91 | uint64_t SymOffset = SymAddress - SecAddress; |
| 92 | uint64_t SymEnd = SymOffset + SymSize; |
| 93 | for (const SectionRef &SR : getRelocSections(Obj, Sec)) { |
| 94 | for (const object::RelocationRef &Reloc : SR.relocations()) { |
| 95 | if (I == E) |
| 96 | break; |
| 97 | const object::symbol_iterator RelocSymI = Reloc.getSymbol(); |
| 98 | if (RelocSymI == Obj->symbol_end()) |
| 99 | continue; |
| 100 | StringRef RelocSymName; |
| 101 | if (error(RelocSymI->getName(RelocSymName))) |
| 102 | return true; |
Rafael Espindola | 96d071c | 2015-06-29 23:29:12 +0000 | [diff] [blame^] | 103 | uint64_t Offset = Reloc.getOffset(); |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 104 | if (Offset >= SymOffset && Offset < SymEnd) { |
| 105 | *I = RelocSymName; |
| 106 | ++I; |
| 107 | } |
| 108 | } |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 109 | } |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | static bool collectRelocationOffsets( |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 114 | const ObjectFile *Obj, const SectionRef &Sec, uint64_t SecAddress, |
| 115 | uint64_t SymAddress, uint64_t SymSize, StringRef SymName, |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 116 | std::map<std::pair<StringRef, uint64_t>, StringRef> &Collection) { |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 117 | uint64_t SymOffset = SymAddress - SecAddress; |
| 118 | uint64_t SymEnd = SymOffset + SymSize; |
| 119 | for (const SectionRef &SR : getRelocSections(Obj, Sec)) { |
| 120 | for (const object::RelocationRef &Reloc : SR.relocations()) { |
| 121 | const object::symbol_iterator RelocSymI = Reloc.getSymbol(); |
| 122 | if (RelocSymI == Obj->symbol_end()) |
| 123 | continue; |
| 124 | StringRef RelocSymName; |
| 125 | if (error(RelocSymI->getName(RelocSymName))) |
| 126 | return true; |
Rafael Espindola | 96d071c | 2015-06-29 23:29:12 +0000 | [diff] [blame^] | 127 | uint64_t Offset = Reloc.getOffset(); |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 128 | if (Offset >= SymOffset && Offset < SymEnd) |
| 129 | Collection[std::make_pair(SymName, Offset - SymOffset)] = RelocSymName; |
| 130 | } |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 131 | } |
| 132 | return false; |
| 133 | } |
| 134 | |
David Majnemer | f45bbd0 | 2015-03-15 01:30:58 +0000 | [diff] [blame] | 135 | static void dumpCXXData(const ObjectFile *Obj) { |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 136 | struct CompleteObjectLocator { |
| 137 | StringRef Symbols[2]; |
David Majnemer | 6887a25 | 2014-09-26 08:01:23 +0000 | [diff] [blame] | 138 | ArrayRef<little32_t> Data; |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 139 | }; |
| 140 | struct ClassHierarchyDescriptor { |
| 141 | StringRef Symbols[1]; |
David Majnemer | 6887a25 | 2014-09-26 08:01:23 +0000 | [diff] [blame] | 142 | ArrayRef<little32_t> Data; |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 143 | }; |
| 144 | struct BaseClassDescriptor { |
| 145 | StringRef Symbols[2]; |
David Majnemer | 6887a25 | 2014-09-26 08:01:23 +0000 | [diff] [blame] | 146 | ArrayRef<little32_t> Data; |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 147 | }; |
| 148 | struct TypeDescriptor { |
| 149 | StringRef Symbols[1]; |
David Majnemer | 6887a25 | 2014-09-26 08:01:23 +0000 | [diff] [blame] | 150 | uint64_t AlwaysZero; |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 151 | StringRef MangledName; |
| 152 | }; |
David Majnemer | f50d0a5 | 2015-02-27 00:43:58 +0000 | [diff] [blame] | 153 | struct ThrowInfo { |
| 154 | uint32_t Flags; |
| 155 | }; |
| 156 | struct CatchableTypeArray { |
| 157 | uint32_t NumEntries; |
| 158 | }; |
| 159 | struct CatchableType { |
| 160 | uint32_t Flags; |
| 161 | uint32_t NonVirtualBaseAdjustmentOffset; |
| 162 | int32_t VirtualBasePointerOffset; |
| 163 | uint32_t VirtualBaseAdjustmentOffset; |
David Majnemer | 86ee173 | 2015-02-27 22:35:25 +0000 | [diff] [blame] | 164 | uint32_t Size; |
David Majnemer | f50d0a5 | 2015-02-27 00:43:58 +0000 | [diff] [blame] | 165 | StringRef Symbols[2]; |
| 166 | }; |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 167 | std::map<std::pair<StringRef, uint64_t>, StringRef> VFTableEntries; |
David Majnemer | f50d0a5 | 2015-02-27 00:43:58 +0000 | [diff] [blame] | 168 | std::map<std::pair<StringRef, uint64_t>, StringRef> TIEntries; |
| 169 | std::map<std::pair<StringRef, uint64_t>, StringRef> CTAEntries; |
David Majnemer | 6887a25 | 2014-09-26 08:01:23 +0000 | [diff] [blame] | 170 | std::map<StringRef, ArrayRef<little32_t>> VBTables; |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 171 | std::map<StringRef, CompleteObjectLocator> COLs; |
| 172 | std::map<StringRef, ClassHierarchyDescriptor> CHDs; |
| 173 | std::map<std::pair<StringRef, uint64_t>, StringRef> BCAEntries; |
| 174 | std::map<StringRef, BaseClassDescriptor> BCDs; |
| 175 | std::map<StringRef, TypeDescriptor> TDs; |
David Majnemer | f50d0a5 | 2015-02-27 00:43:58 +0000 | [diff] [blame] | 176 | std::map<StringRef, ThrowInfo> TIs; |
| 177 | std::map<StringRef, CatchableTypeArray> CTAs; |
| 178 | std::map<StringRef, CatchableType> CTs; |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 179 | |
| 180 | std::map<std::pair<StringRef, uint64_t>, StringRef> VTableSymEntries; |
| 181 | std::map<std::pair<StringRef, uint64_t>, int64_t> VTableDataEntries; |
| 182 | std::map<std::pair<StringRef, uint64_t>, StringRef> VTTEntries; |
| 183 | std::map<StringRef, StringRef> TINames; |
| 184 | |
| 185 | uint8_t BytesInAddress = Obj->getBytesInAddress(); |
| 186 | |
Rafael Espindola | 6bf3221 | 2015-06-24 19:57:32 +0000 | [diff] [blame] | 187 | std::vector<std::pair<SymbolRef, uint64_t>> SymAddr = |
Rafael Espindola | a4a4093 | 2015-06-23 02:20:37 +0000 | [diff] [blame] | 188 | object::computeSymbolSizes(*Obj); |
Rafael Espindola | a4a4093 | 2015-06-23 02:20:37 +0000 | [diff] [blame] | 189 | |
Rafael Espindola | 6bf3221 | 2015-06-24 19:57:32 +0000 | [diff] [blame] | 190 | for (auto &P : SymAddr) { |
Rafael Espindola | a4a4093 | 2015-06-23 02:20:37 +0000 | [diff] [blame] | 191 | object::SymbolRef Sym = P.first; |
| 192 | uint64_t SymSize = P.second; |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 193 | StringRef SymName; |
| 194 | if (error(Sym.getName(SymName))) |
| 195 | return; |
David Majnemer | 601327c | 2014-09-26 22:32:19 +0000 | [diff] [blame] | 196 | object::section_iterator SecI(Obj->section_begin()); |
| 197 | if (error(Sym.getSection(SecI))) |
| 198 | return; |
| 199 | // Skip external symbols. |
| 200 | if (SecI == Obj->section_end()) |
| 201 | continue; |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 202 | const SectionRef &Sec = *SecI; |
David Majnemer | 601327c | 2014-09-26 22:32:19 +0000 | [diff] [blame] | 203 | // Skip virtual or BSS sections. |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 204 | if (Sec.isBSS() || Sec.isVirtual()) |
David Majnemer | 601327c | 2014-09-26 22:32:19 +0000 | [diff] [blame] | 205 | continue; |
| 206 | StringRef SecContents; |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 207 | if (error(Sec.getContents(SecContents))) |
David Majnemer | 601327c | 2014-09-26 22:32:19 +0000 | [diff] [blame] | 208 | return; |
Rafael Espindola | 5eb02e4 | 2015-06-01 00:27:26 +0000 | [diff] [blame] | 209 | uint64_t SymAddress; |
| 210 | if (error(Sym.getAddress(SymAddress))) |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 211 | return; |
| 212 | uint64_t SecAddress = Sec.getAddress(); |
| 213 | uint64_t SecSize = Sec.getSize(); |
| 214 | uint64_t SymOffset = SymAddress - SecAddress; |
| 215 | StringRef SymContents = SecContents.substr(SymOffset, SymSize); |
| 216 | |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 217 | // VFTables in the MS-ABI start with '??_7' and are contained within their |
| 218 | // own COMDAT section. We then determine the contents of the VFTable by |
| 219 | // looking at each relocation in the section. |
| 220 | if (SymName.startswith("??_7")) { |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 221 | // Each relocation either names a virtual method or a thunk. We note the |
| 222 | // offset into the section and the symbol used for the relocation. |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 223 | collectRelocationOffsets(Obj, Sec, SecAddress, SecAddress, SecSize, |
| 224 | SymName, VFTableEntries); |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 225 | } |
| 226 | // VBTables in the MS-ABI start with '??_8' and are filled with 32-bit |
| 227 | // offsets of virtual bases. |
| 228 | else if (SymName.startswith("??_8")) { |
David Majnemer | 6887a25 | 2014-09-26 08:01:23 +0000 | [diff] [blame] | 229 | ArrayRef<little32_t> VBTableData( |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 230 | reinterpret_cast<const little32_t *>(SymContents.data()), |
| 231 | SymContents.size() / sizeof(little32_t)); |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 232 | VBTables[SymName] = VBTableData; |
| 233 | } |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 234 | // Complete object locators in the MS-ABI start with '??_R4' |
| 235 | else if (SymName.startswith("??_R4")) { |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 236 | CompleteObjectLocator COL; |
David Majnemer | 6887a25 | 2014-09-26 08:01:23 +0000 | [diff] [blame] | 237 | COL.Data = ArrayRef<little32_t>( |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 238 | reinterpret_cast<const little32_t *>(SymContents.data()), 3); |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 239 | StringRef *I = std::begin(COL.Symbols), *E = std::end(COL.Symbols); |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 240 | if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, |
| 241 | E)) |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 242 | return; |
| 243 | COLs[SymName] = COL; |
| 244 | } |
| 245 | // Class hierarchy descriptors in the MS-ABI start with '??_R3' |
| 246 | else if (SymName.startswith("??_R3")) { |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 247 | ClassHierarchyDescriptor CHD; |
David Majnemer | 6887a25 | 2014-09-26 08:01:23 +0000 | [diff] [blame] | 248 | CHD.Data = ArrayRef<little32_t>( |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 249 | reinterpret_cast<const little32_t *>(SymContents.data()), 3); |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 250 | StringRef *I = std::begin(CHD.Symbols), *E = std::end(CHD.Symbols); |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 251 | if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, |
| 252 | E)) |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 253 | return; |
| 254 | CHDs[SymName] = CHD; |
| 255 | } |
| 256 | // Class hierarchy descriptors in the MS-ABI start with '??_R2' |
| 257 | else if (SymName.startswith("??_R2")) { |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 258 | // Each relocation names a base class descriptor. We note the offset into |
| 259 | // the section and the symbol used for the relocation. |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 260 | collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize, |
| 261 | SymName, BCAEntries); |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 262 | } |
| 263 | // Base class descriptors in the MS-ABI start with '??_R1' |
| 264 | else if (SymName.startswith("??_R1")) { |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 265 | BaseClassDescriptor BCD; |
David Majnemer | 6887a25 | 2014-09-26 08:01:23 +0000 | [diff] [blame] | 266 | BCD.Data = ArrayRef<little32_t>( |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 267 | reinterpret_cast<const little32_t *>(SymContents.data()) + 1, 5); |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 268 | StringRef *I = std::begin(BCD.Symbols), *E = std::end(BCD.Symbols); |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 269 | if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, |
| 270 | E)) |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 271 | return; |
| 272 | BCDs[SymName] = BCD; |
| 273 | } |
| 274 | // Type descriptors in the MS-ABI start with '??_R0' |
| 275 | else if (SymName.startswith("??_R0")) { |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 276 | const char *DataPtr = SymContents.drop_front(BytesInAddress).data(); |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 277 | TypeDescriptor TD; |
David Majnemer | 6887a25 | 2014-09-26 08:01:23 +0000 | [diff] [blame] | 278 | if (BytesInAddress == 8) |
| 279 | TD.AlwaysZero = *reinterpret_cast<const little64_t *>(DataPtr); |
| 280 | else |
| 281 | TD.AlwaysZero = *reinterpret_cast<const little32_t *>(DataPtr); |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 282 | TD.MangledName = SymContents.drop_front(BytesInAddress * 2); |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 283 | StringRef *I = std::begin(TD.Symbols), *E = std::end(TD.Symbols); |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 284 | if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, |
| 285 | E)) |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 286 | return; |
| 287 | TDs[SymName] = TD; |
| 288 | } |
David Majnemer | f50d0a5 | 2015-02-27 00:43:58 +0000 | [diff] [blame] | 289 | // Throw descriptors in the MS-ABI start with '_TI' |
| 290 | else if (SymName.startswith("_TI") || SymName.startswith("__TI")) { |
| 291 | ThrowInfo TI; |
| 292 | TI.Flags = *reinterpret_cast<const little32_t *>(SymContents.data()); |
| 293 | collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize, |
| 294 | SymName, TIEntries); |
| 295 | TIs[SymName] = TI; |
| 296 | } |
| 297 | // Catchable type arrays in the MS-ABI start with _CTA or __CTA. |
| 298 | else if (SymName.startswith("_CTA") || SymName.startswith("__CTA")) { |
| 299 | CatchableTypeArray CTA; |
| 300 | CTA.NumEntries = |
| 301 | *reinterpret_cast<const little32_t *>(SymContents.data()); |
| 302 | collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize, |
| 303 | SymName, CTAEntries); |
| 304 | CTAs[SymName] = CTA; |
| 305 | } |
| 306 | // Catchable types in the MS-ABI start with _CT or __CT. |
| 307 | else if (SymName.startswith("_CT") || SymName.startswith("__CT")) { |
| 308 | const little32_t *DataPtr = |
| 309 | reinterpret_cast<const little32_t *>(SymContents.data()); |
| 310 | CatchableType CT; |
| 311 | CT.Flags = DataPtr[0]; |
| 312 | CT.NonVirtualBaseAdjustmentOffset = DataPtr[2]; |
| 313 | CT.VirtualBasePointerOffset = DataPtr[3]; |
| 314 | CT.VirtualBaseAdjustmentOffset = DataPtr[4]; |
David Majnemer | 86ee173 | 2015-02-27 22:35:25 +0000 | [diff] [blame] | 315 | CT.Size = DataPtr[5]; |
David Majnemer | f50d0a5 | 2015-02-27 00:43:58 +0000 | [diff] [blame] | 316 | StringRef *I = std::begin(CT.Symbols), *E = std::end(CT.Symbols); |
| 317 | if (collectRelocatedSymbols(Obj, Sec, SecAddress, SymAddress, SymSize, I, |
| 318 | E)) |
| 319 | return; |
| 320 | CTs[SymName] = CT; |
| 321 | } |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 322 | // Construction vtables in the Itanium ABI start with '_ZTT' or '__ZTT'. |
| 323 | else if (SymName.startswith("_ZTT") || SymName.startswith("__ZTT")) { |
| 324 | collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize, |
| 325 | SymName, VTTEntries); |
| 326 | } |
| 327 | // Typeinfo names in the Itanium ABI start with '_ZTS' or '__ZTS'. |
| 328 | else if (SymName.startswith("_ZTS") || SymName.startswith("__ZTS")) { |
| 329 | TINames[SymName] = SymContents.slice(0, SymContents.find('\0')); |
| 330 | } |
| 331 | // Vtables in the Itanium ABI start with '_ZTV' or '__ZTV'. |
| 332 | else if (SymName.startswith("_ZTV") || SymName.startswith("__ZTV")) { |
| 333 | collectRelocationOffsets(Obj, Sec, SecAddress, SymAddress, SymSize, |
| 334 | SymName, VTableSymEntries); |
| 335 | for (uint64_t SymOffI = 0; SymOffI < SymSize; SymOffI += BytesInAddress) { |
| 336 | auto Key = std::make_pair(SymName, SymOffI); |
| 337 | if (VTableSymEntries.count(Key)) |
| 338 | continue; |
David Majnemer | f45bbd0 | 2015-03-15 01:30:58 +0000 | [diff] [blame] | 339 | const char *DataPtr = |
| 340 | SymContents.substr(SymOffI, BytesInAddress).data(); |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 341 | int64_t VData; |
| 342 | if (BytesInAddress == 8) |
| 343 | VData = *reinterpret_cast<const little64_t *>(DataPtr); |
| 344 | else |
| 345 | VData = *reinterpret_cast<const little32_t *>(DataPtr); |
| 346 | VTableDataEntries[Key] = VData; |
| 347 | } |
| 348 | } |
| 349 | // Typeinfo structures in the Itanium ABI start with '_ZTI' or '__ZTI'. |
| 350 | else if (SymName.startswith("_ZTI") || SymName.startswith("__ZTI")) { |
| 351 | // FIXME: Do something with these! |
| 352 | } |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 353 | } |
David Majnemer | f45bbd0 | 2015-03-15 01:30:58 +0000 | [diff] [blame] | 354 | for (const auto &VFTableEntry : VFTableEntries) { |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 355 | StringRef VFTableName = VFTableEntry.first.first; |
| 356 | uint64_t Offset = VFTableEntry.first.second; |
| 357 | StringRef SymName = VFTableEntry.second; |
| 358 | outs() << VFTableName << '[' << Offset << "]: " << SymName << '\n'; |
| 359 | } |
Richard Trieu | 46f2cc9 | 2015-04-15 03:17:49 +0000 | [diff] [blame] | 360 | for (const auto &VBTable : VBTables) { |
David Majnemer | bf32f77 | 2014-07-25 04:30:11 +0000 | [diff] [blame] | 361 | StringRef VBTableName = VBTable.first; |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 362 | uint32_t Idx = 0; |
David Majnemer | 6887a25 | 2014-09-26 08:01:23 +0000 | [diff] [blame] | 363 | for (little32_t Offset : VBTable.second) { |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 364 | outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n'; |
David Majnemer | bf32f77 | 2014-07-25 04:30:11 +0000 | [diff] [blame] | 365 | Idx += sizeof(Offset); |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
Richard Trieu | 46f2cc9 | 2015-04-15 03:17:49 +0000 | [diff] [blame] | 368 | for (const auto &COLPair : COLs) { |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 369 | StringRef COLName = COLPair.first; |
| 370 | const CompleteObjectLocator &COL = COLPair.second; |
| 371 | outs() << COLName << "[IsImageRelative]: " << COL.Data[0] << '\n'; |
| 372 | outs() << COLName << "[OffsetToTop]: " << COL.Data[1] << '\n'; |
| 373 | outs() << COLName << "[VFPtrOffset]: " << COL.Data[2] << '\n'; |
| 374 | outs() << COLName << "[TypeDescriptor]: " << COL.Symbols[0] << '\n'; |
David Majnemer | f45bbd0 | 2015-03-15 01:30:58 +0000 | [diff] [blame] | 375 | outs() << COLName << "[ClassHierarchyDescriptor]: " << COL.Symbols[1] |
| 376 | << '\n'; |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 377 | } |
Richard Trieu | 46f2cc9 | 2015-04-15 03:17:49 +0000 | [diff] [blame] | 378 | for (const auto &CHDPair : CHDs) { |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 379 | StringRef CHDName = CHDPair.first; |
| 380 | const ClassHierarchyDescriptor &CHD = CHDPair.second; |
| 381 | outs() << CHDName << "[AlwaysZero]: " << CHD.Data[0] << '\n'; |
| 382 | outs() << CHDName << "[Flags]: " << CHD.Data[1] << '\n'; |
| 383 | outs() << CHDName << "[NumClasses]: " << CHD.Data[2] << '\n'; |
| 384 | outs() << CHDName << "[BaseClassArray]: " << CHD.Symbols[0] << '\n'; |
| 385 | } |
Richard Trieu | 46f2cc9 | 2015-04-15 03:17:49 +0000 | [diff] [blame] | 386 | for (const auto &BCAEntry : BCAEntries) { |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 387 | StringRef BCAName = BCAEntry.first.first; |
| 388 | uint64_t Offset = BCAEntry.first.second; |
| 389 | StringRef SymName = BCAEntry.second; |
| 390 | outs() << BCAName << '[' << Offset << "]: " << SymName << '\n'; |
| 391 | } |
Richard Trieu | 46f2cc9 | 2015-04-15 03:17:49 +0000 | [diff] [blame] | 392 | for (const auto &BCDPair : BCDs) { |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 393 | StringRef BCDName = BCDPair.first; |
| 394 | const BaseClassDescriptor &BCD = BCDPair.second; |
| 395 | outs() << BCDName << "[TypeDescriptor]: " << BCD.Symbols[0] << '\n'; |
| 396 | outs() << BCDName << "[NumBases]: " << BCD.Data[0] << '\n'; |
| 397 | outs() << BCDName << "[OffsetInVBase]: " << BCD.Data[1] << '\n'; |
| 398 | outs() << BCDName << "[VBPtrOffset]: " << BCD.Data[2] << '\n'; |
| 399 | outs() << BCDName << "[OffsetInVBTable]: " << BCD.Data[3] << '\n'; |
| 400 | outs() << BCDName << "[Flags]: " << BCD.Data[4] << '\n'; |
David Majnemer | f45bbd0 | 2015-03-15 01:30:58 +0000 | [diff] [blame] | 401 | outs() << BCDName << "[ClassHierarchyDescriptor]: " << BCD.Symbols[1] |
| 402 | << '\n'; |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 403 | } |
Richard Trieu | 46f2cc9 | 2015-04-15 03:17:49 +0000 | [diff] [blame] | 404 | for (const auto &TDPair : TDs) { |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 405 | StringRef TDName = TDPair.first; |
| 406 | const TypeDescriptor &TD = TDPair.second; |
| 407 | outs() << TDName << "[VFPtr]: " << TD.Symbols[0] << '\n'; |
David Majnemer | 6887a25 | 2014-09-26 08:01:23 +0000 | [diff] [blame] | 408 | outs() << TDName << "[AlwaysZero]: " << TD.AlwaysZero << '\n'; |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 409 | outs() << TDName << "[MangledName]: "; |
David Majnemer | 56167c3 | 2014-09-26 05:50:45 +0000 | [diff] [blame] | 410 | outs().write_escaped(TD.MangledName.rtrim(StringRef("\0", 1)), |
| 411 | /*UseHexEscapes=*/true) |
| 412 | << '\n'; |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame] | 413 | } |
Richard Trieu | 46f2cc9 | 2015-04-15 03:17:49 +0000 | [diff] [blame] | 414 | for (const auto &TIPair : TIs) { |
David Majnemer | f50d0a5 | 2015-02-27 00:43:58 +0000 | [diff] [blame] | 415 | StringRef TIName = TIPair.first; |
| 416 | const ThrowInfo &TI = TIPair.second; |
| 417 | auto dumpThrowInfoFlag = [&](const char *Name, uint32_t Flag) { |
| 418 | outs() << TIName << "[Flags." << Name |
| 419 | << "]: " << (TI.Flags & Flag ? "true" : "false") << '\n'; |
| 420 | }; |
| 421 | auto dumpThrowInfoSymbol = [&](const char *Name, int Offset) { |
| 422 | outs() << TIName << '[' << Name << "]: "; |
| 423 | auto Entry = TIEntries.find(std::make_pair(TIName, Offset)); |
| 424 | outs() << (Entry == TIEntries.end() ? "null" : Entry->second) << '\n'; |
| 425 | }; |
| 426 | outs() << TIName << "[Flags]: " << TI.Flags << '\n'; |
| 427 | dumpThrowInfoFlag("Const", 1); |
| 428 | dumpThrowInfoFlag("Volatile", 2); |
| 429 | dumpThrowInfoSymbol("CleanupFn", 4); |
| 430 | dumpThrowInfoSymbol("ForwardCompat", 8); |
| 431 | dumpThrowInfoSymbol("CatchableTypeArray", 12); |
| 432 | } |
Richard Trieu | 46f2cc9 | 2015-04-15 03:17:49 +0000 | [diff] [blame] | 433 | for (const auto &CTAPair : CTAs) { |
David Majnemer | f50d0a5 | 2015-02-27 00:43:58 +0000 | [diff] [blame] | 434 | StringRef CTAName = CTAPair.first; |
| 435 | const CatchableTypeArray &CTA = CTAPair.second; |
| 436 | |
| 437 | outs() << CTAName << "[NumEntries]: " << CTA.NumEntries << '\n'; |
| 438 | |
| 439 | unsigned Idx = 0; |
| 440 | for (auto I = CTAEntries.lower_bound(std::make_pair(CTAName, 0)), |
| 441 | E = CTAEntries.upper_bound(std::make_pair(CTAName, UINT64_MAX)); |
| 442 | I != E; ++I) |
| 443 | outs() << CTAName << '[' << Idx++ << "]: " << I->second << '\n'; |
| 444 | } |
Richard Trieu | 46f2cc9 | 2015-04-15 03:17:49 +0000 | [diff] [blame] | 445 | for (const auto &CTPair : CTs) { |
David Majnemer | f50d0a5 | 2015-02-27 00:43:58 +0000 | [diff] [blame] | 446 | StringRef CTName = CTPair.first; |
| 447 | const CatchableType &CT = CTPair.second; |
| 448 | auto dumpCatchableTypeFlag = [&](const char *Name, uint32_t Flag) { |
| 449 | outs() << CTName << "[Flags." << Name |
| 450 | << "]: " << (CT.Flags & Flag ? "true" : "false") << '\n'; |
| 451 | }; |
| 452 | outs() << CTName << "[Flags]: " << CT.Flags << '\n'; |
| 453 | dumpCatchableTypeFlag("ScalarType", 1); |
| 454 | dumpCatchableTypeFlag("VirtualInheritance", 4); |
| 455 | outs() << CTName << "[TypeDescriptor]: " << CT.Symbols[0] << '\n'; |
| 456 | outs() << CTName << "[NonVirtualBaseAdjustmentOffset]: " |
| 457 | << CT.NonVirtualBaseAdjustmentOffset << '\n'; |
| 458 | outs() << CTName |
| 459 | << "[VirtualBasePointerOffset]: " << CT.VirtualBasePointerOffset |
| 460 | << '\n'; |
| 461 | outs() << CTName << "[VirtualBaseAdjustmentOffset]: " |
| 462 | << CT.VirtualBaseAdjustmentOffset << '\n'; |
David Majnemer | 86ee173 | 2015-02-27 22:35:25 +0000 | [diff] [blame] | 463 | outs() << CTName << "[Size]: " << CT.Size << '\n'; |
David Majnemer | f50d0a5 | 2015-02-27 00:43:58 +0000 | [diff] [blame] | 464 | outs() << CTName |
| 465 | << "[CopyCtor]: " << (CT.Symbols[1].empty() ? "null" : CT.Symbols[1]) |
| 466 | << '\n'; |
| 467 | } |
Richard Trieu | 46f2cc9 | 2015-04-15 03:17:49 +0000 | [diff] [blame] | 468 | for (const auto &VTTPair : VTTEntries) { |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 469 | StringRef VTTName = VTTPair.first.first; |
| 470 | uint64_t VTTOffset = VTTPair.first.second; |
| 471 | StringRef VTTEntry = VTTPair.second; |
| 472 | outs() << VTTName << '[' << VTTOffset << "]: " << VTTEntry << '\n'; |
| 473 | } |
Richard Trieu | 46f2cc9 | 2015-04-15 03:17:49 +0000 | [diff] [blame] | 474 | for (const auto &TIPair : TINames) { |
David Majnemer | e268361 | 2014-11-03 07:23:25 +0000 | [diff] [blame] | 475 | StringRef TIName = TIPair.first; |
| 476 | outs() << TIName << ": " << TIPair.second << '\n'; |
| 477 | } |
| 478 | auto VTableSymI = VTableSymEntries.begin(); |
| 479 | auto VTableSymE = VTableSymEntries.end(); |
| 480 | auto VTableDataI = VTableDataEntries.begin(); |
| 481 | auto VTableDataE = VTableDataEntries.end(); |
| 482 | for (;;) { |
| 483 | bool SymDone = VTableSymI == VTableSymE; |
| 484 | bool DataDone = VTableDataI == VTableDataE; |
| 485 | if (SymDone && DataDone) |
| 486 | break; |
| 487 | if (!SymDone && (DataDone || VTableSymI->first < VTableDataI->first)) { |
| 488 | StringRef VTableName = VTableSymI->first.first; |
| 489 | uint64_t Offset = VTableSymI->first.second; |
| 490 | StringRef VTableEntry = VTableSymI->second; |
| 491 | outs() << VTableName << '[' << Offset << "]: "; |
| 492 | outs() << VTableEntry; |
| 493 | outs() << '\n'; |
| 494 | ++VTableSymI; |
| 495 | continue; |
| 496 | } |
| 497 | if (!DataDone && (SymDone || VTableDataI->first < VTableSymI->first)) { |
| 498 | StringRef VTableName = VTableDataI->first.first; |
| 499 | uint64_t Offset = VTableDataI->first.second; |
| 500 | int64_t VTableEntry = VTableDataI->second; |
| 501 | outs() << VTableName << '[' << Offset << "]: "; |
| 502 | outs() << VTableEntry; |
| 503 | outs() << '\n'; |
| 504 | ++VTableDataI; |
| 505 | continue; |
| 506 | } |
| 507 | } |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | static void dumpArchive(const Archive *Arc) { |
David Majnemer | eac48b6 | 2014-09-25 22:56:54 +0000 | [diff] [blame] | 511 | for (const Archive::Child &ArcC : Arc->children()) { |
| 512 | ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcC.getAsBinary(); |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 513 | if (std::error_code EC = ChildOrErr.getError()) { |
| 514 | // Ignore non-object files. |
| 515 | if (EC != object_error::invalid_file_type) |
| 516 | reportError(Arc->getFileName(), EC.message()); |
| 517 | continue; |
| 518 | } |
| 519 | |
| 520 | if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get())) |
David Majnemer | f45bbd0 | 2015-03-15 01:30:58 +0000 | [diff] [blame] | 521 | dumpCXXData(Obj); |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 522 | else |
David Majnemer | f45bbd0 | 2015-03-15 01:30:58 +0000 | [diff] [blame] | 523 | reportError(Arc->getFileName(), cxxdump_error::unrecognized_file_format); |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
| 527 | static void dumpInput(StringRef File) { |
| 528 | // If file isn't stdin, check that it exists. |
| 529 | if (File != "-" && !sys::fs::exists(File)) { |
David Majnemer | f45bbd0 | 2015-03-15 01:30:58 +0000 | [diff] [blame] | 530 | reportError(File, cxxdump_error::file_not_found); |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 531 | return; |
| 532 | } |
| 533 | |
| 534 | // Attempt to open the binary. |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 535 | ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File); |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 536 | if (std::error_code EC = BinaryOrErr.getError()) { |
| 537 | reportError(File, EC); |
| 538 | return; |
| 539 | } |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 540 | Binary &Binary = *BinaryOrErr.get().getBinary(); |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 541 | |
Rafael Espindola | 3f6481d | 2014-08-01 14:31:55 +0000 | [diff] [blame] | 542 | if (Archive *Arc = dyn_cast<Archive>(&Binary)) |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 543 | dumpArchive(Arc); |
Rafael Espindola | 3f6481d | 2014-08-01 14:31:55 +0000 | [diff] [blame] | 544 | else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary)) |
David Majnemer | f45bbd0 | 2015-03-15 01:30:58 +0000 | [diff] [blame] | 545 | dumpCXXData(Obj); |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 546 | else |
David Majnemer | f45bbd0 | 2015-03-15 01:30:58 +0000 | [diff] [blame] | 547 | reportError(File, cxxdump_error::unrecognized_file_format); |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | int main(int argc, const char *argv[]) { |
| 551 | sys::PrintStackTraceOnErrorSignal(); |
| 552 | PrettyStackTraceProgram X(argc, argv); |
| 553 | llvm_shutdown_obj Y; |
| 554 | |
| 555 | // Initialize targets. |
| 556 | llvm::InitializeAllTargetInfos(); |
| 557 | |
| 558 | // Register the target printer for --version. |
| 559 | cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); |
| 560 | |
David Majnemer | f45bbd0 | 2015-03-15 01:30:58 +0000 | [diff] [blame] | 561 | cl::ParseCommandLineOptions(argc, argv, "LLVM C++ ABI Data Dumper\n"); |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 562 | |
| 563 | // Default to stdin if no filename is specified. |
| 564 | if (opts::InputFilenames.size() == 0) |
| 565 | opts::InputFilenames.push_back("-"); |
| 566 | |
| 567 | std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(), |
| 568 | dumpInput); |
| 569 | |
| 570 | return ReturnValue; |
| 571 | } |