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