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