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