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 | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame^] | 71 | static bool collectRelocatedSymbols(const ObjectFile *Obj, |
| 72 | object::section_iterator SecI, StringRef *I, |
| 73 | StringRef *E) { |
| 74 | for (const object::RelocationRef &Reloc : SecI->relocations()) { |
| 75 | if (I == E) |
| 76 | break; |
| 77 | const object::symbol_iterator RelocSymI = Reloc.getSymbol(); |
| 78 | if (RelocSymI == Obj->symbol_end()) |
| 79 | continue; |
| 80 | StringRef RelocSymName; |
| 81 | if (error(RelocSymI->getName(RelocSymName))) |
| 82 | return true; |
| 83 | *I = RelocSymName; |
| 84 | ++I; |
| 85 | } |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | static bool collectRelocationOffsets( |
| 90 | const ObjectFile *Obj, object::section_iterator SecI, StringRef SymName, |
| 91 | std::map<std::pair<StringRef, uint64_t>, StringRef> &Collection) { |
| 92 | for (const object::RelocationRef &Reloc : SecI->relocations()) { |
| 93 | const object::symbol_iterator RelocSymI = Reloc.getSymbol(); |
| 94 | if (RelocSymI == Obj->symbol_end()) |
| 95 | continue; |
| 96 | StringRef RelocSymName; |
| 97 | if (error(RelocSymI->getName(RelocSymName))) |
| 98 | return true; |
| 99 | uint64_t Offset; |
| 100 | if (error(Reloc.getOffset(Offset))) |
| 101 | return true; |
| 102 | Collection[std::make_pair(SymName, Offset)] = RelocSymName; |
| 103 | } |
| 104 | return false; |
| 105 | } |
| 106 | |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 107 | static void dumpVTables(const ObjectFile *Obj) { |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame^] | 108 | struct CompleteObjectLocator { |
| 109 | StringRef Symbols[2]; |
| 110 | ArrayRef<aligned_little32_t> Data; |
| 111 | }; |
| 112 | struct ClassHierarchyDescriptor { |
| 113 | StringRef Symbols[1]; |
| 114 | ArrayRef<aligned_little32_t> Data; |
| 115 | }; |
| 116 | struct BaseClassDescriptor { |
| 117 | StringRef Symbols[2]; |
| 118 | ArrayRef<aligned_little32_t> Data; |
| 119 | }; |
| 120 | struct TypeDescriptor { |
| 121 | StringRef Symbols[1]; |
| 122 | ArrayRef<aligned_little32_t> Data; |
| 123 | StringRef MangledName; |
| 124 | }; |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 125 | std::map<std::pair<StringRef, uint64_t>, StringRef> VFTableEntries; |
David Majnemer | bf32f77 | 2014-07-25 04:30:11 +0000 | [diff] [blame] | 126 | std::map<StringRef, ArrayRef<aligned_little32_t>> VBTables; |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame^] | 127 | std::map<StringRef, CompleteObjectLocator> COLs; |
| 128 | std::map<StringRef, ClassHierarchyDescriptor> CHDs; |
| 129 | std::map<std::pair<StringRef, uint64_t>, StringRef> BCAEntries; |
| 130 | std::map<StringRef, BaseClassDescriptor> BCDs; |
| 131 | std::map<StringRef, TypeDescriptor> TDs; |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 132 | for (const object::SymbolRef &Sym : Obj->symbols()) { |
| 133 | StringRef SymName; |
| 134 | if (error(Sym.getName(SymName))) |
| 135 | return; |
| 136 | // VFTables in the MS-ABI start with '??_7' and are contained within their |
| 137 | // own COMDAT section. We then determine the contents of the VFTable by |
| 138 | // looking at each relocation in the section. |
| 139 | if (SymName.startswith("??_7")) { |
| 140 | object::section_iterator SecI(Obj->section_begin()); |
| 141 | if (error(Sym.getSection(SecI))) |
| 142 | return; |
| 143 | if (SecI == Obj->section_end()) |
| 144 | continue; |
| 145 | // Each relocation either names a virtual method or a thunk. We note the |
| 146 | // offset into the section and the symbol used for the relocation. |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame^] | 147 | collectRelocationOffsets(Obj, SecI, SymName, VFTableEntries); |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 148 | } |
| 149 | // VBTables in the MS-ABI start with '??_8' and are filled with 32-bit |
| 150 | // offsets of virtual bases. |
| 151 | else if (SymName.startswith("??_8")) { |
| 152 | object::section_iterator SecI(Obj->section_begin()); |
| 153 | if (error(Sym.getSection(SecI))) |
| 154 | return; |
| 155 | if (SecI == Obj->section_end()) |
| 156 | continue; |
| 157 | StringRef SecContents; |
| 158 | if (error(SecI->getContents(SecContents))) |
| 159 | return; |
| 160 | |
| 161 | ArrayRef<aligned_little32_t> VBTableData( |
| 162 | reinterpret_cast<const aligned_little32_t *>(SecContents.data()), |
| 163 | SecContents.size() / sizeof(aligned_little32_t)); |
| 164 | VBTables[SymName] = VBTableData; |
| 165 | } |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame^] | 166 | // Complete object locators in the MS-ABI start with '??_R4' |
| 167 | else if (SymName.startswith("??_R4")) { |
| 168 | object::section_iterator SecI(Obj->section_begin()); |
| 169 | if (error(Sym.getSection(SecI))) |
| 170 | return; |
| 171 | StringRef SecContents; |
| 172 | if (error(SecI->getContents(SecContents))) |
| 173 | return; |
| 174 | CompleteObjectLocator COL; |
| 175 | COL.Data = ArrayRef<aligned_little32_t>( |
| 176 | reinterpret_cast<const aligned_little32_t *>(SecContents.data()), 3); |
| 177 | StringRef *I = std::begin(COL.Symbols), *E = std::end(COL.Symbols); |
| 178 | if (collectRelocatedSymbols(Obj, SecI, I, E)) |
| 179 | return; |
| 180 | COLs[SymName] = COL; |
| 181 | } |
| 182 | // Class hierarchy descriptors in the MS-ABI start with '??_R3' |
| 183 | else if (SymName.startswith("??_R3")) { |
| 184 | object::section_iterator SecI(Obj->section_begin()); |
| 185 | if (error(Sym.getSection(SecI))) |
| 186 | return; |
| 187 | StringRef SecContents; |
| 188 | if (error(SecI->getContents(SecContents))) |
| 189 | return; |
| 190 | ClassHierarchyDescriptor CHD; |
| 191 | CHD.Data = ArrayRef<aligned_little32_t>( |
| 192 | reinterpret_cast<const aligned_little32_t *>(SecContents.data()), 3); |
| 193 | StringRef *I = std::begin(CHD.Symbols), *E = std::end(CHD.Symbols); |
| 194 | if (collectRelocatedSymbols(Obj, SecI, I, E)) |
| 195 | return; |
| 196 | CHDs[SymName] = CHD; |
| 197 | } |
| 198 | // Class hierarchy descriptors in the MS-ABI start with '??_R2' |
| 199 | else if (SymName.startswith("??_R2")) { |
| 200 | object::section_iterator SecI(Obj->section_begin()); |
| 201 | if (error(Sym.getSection(SecI))) |
| 202 | return; |
| 203 | if (SecI == Obj->section_end()) |
| 204 | continue; |
| 205 | // Each relocation names a base class descriptor. We note the offset into |
| 206 | // the section and the symbol used for the relocation. |
| 207 | collectRelocationOffsets(Obj, SecI, SymName, BCAEntries); |
| 208 | } |
| 209 | // Base class descriptors in the MS-ABI start with '??_R1' |
| 210 | else if (SymName.startswith("??_R1")) { |
| 211 | object::section_iterator SecI(Obj->section_begin()); |
| 212 | if (error(Sym.getSection(SecI))) |
| 213 | return; |
| 214 | StringRef SecContents; |
| 215 | if (error(SecI->getContents(SecContents))) |
| 216 | return; |
| 217 | BaseClassDescriptor BCD; |
| 218 | BCD.Data = ArrayRef<aligned_little32_t>( |
| 219 | reinterpret_cast<const aligned_little32_t *>(SecContents.data()) + 1, |
| 220 | 5); |
| 221 | StringRef *I = std::begin(BCD.Symbols), *E = std::end(BCD.Symbols); |
| 222 | if (collectRelocatedSymbols(Obj, SecI, I, E)) |
| 223 | return; |
| 224 | BCDs[SymName] = BCD; |
| 225 | } |
| 226 | // Type descriptors in the MS-ABI start with '??_R0' |
| 227 | else if (SymName.startswith("??_R0")) { |
| 228 | object::section_iterator SecI(Obj->section_begin()); |
| 229 | if (error(Sym.getSection(SecI))) |
| 230 | return; |
| 231 | StringRef SecContents; |
| 232 | if (error(SecI->getContents(SecContents))) |
| 233 | return; |
| 234 | TypeDescriptor TD; |
| 235 | TD.Data = makeArrayRef( |
| 236 | reinterpret_cast<const aligned_little32_t *>( |
| 237 | SecContents.drop_front(Obj->getBytesInAddress()).data()), |
| 238 | Obj->getBytesInAddress() / sizeof(aligned_little32_t)); |
| 239 | TD.MangledName = SecContents.drop_front(Obj->getBytesInAddress() * 2); |
| 240 | StringRef *I = std::begin(TD.Symbols), *E = std::end(TD.Symbols); |
| 241 | if (collectRelocatedSymbols(Obj, SecI, I, E)) |
| 242 | return; |
| 243 | TDs[SymName] = TD; |
| 244 | } |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 245 | } |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame^] | 246 | for (const std::pair<std::pair<StringRef, uint64_t>, StringRef> &VFTableEntry : |
| 247 | VFTableEntries) { |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 248 | StringRef VFTableName = VFTableEntry.first.first; |
| 249 | uint64_t Offset = VFTableEntry.first.second; |
| 250 | StringRef SymName = VFTableEntry.second; |
| 251 | outs() << VFTableName << '[' << Offset << "]: " << SymName << '\n'; |
| 252 | } |
David Majnemer | bf32f77 | 2014-07-25 04:30:11 +0000 | [diff] [blame] | 253 | for (const std::pair<StringRef, ArrayRef<aligned_little32_t>> &VBTable : |
| 254 | VBTables) { |
| 255 | StringRef VBTableName = VBTable.first; |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 256 | uint32_t Idx = 0; |
David Majnemer | bf32f77 | 2014-07-25 04:30:11 +0000 | [diff] [blame] | 257 | for (aligned_little32_t Offset : VBTable.second) { |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 258 | outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n'; |
David Majnemer | bf32f77 | 2014-07-25 04:30:11 +0000 | [diff] [blame] | 259 | Idx += sizeof(Offset); |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 260 | } |
| 261 | } |
David Majnemer | 1ac52eb | 2014-09-26 04:21:51 +0000 | [diff] [blame^] | 262 | for (const std::pair<StringRef, CompleteObjectLocator> &COLPair : COLs) { |
| 263 | StringRef COLName = COLPair.first; |
| 264 | const CompleteObjectLocator &COL = COLPair.second; |
| 265 | outs() << COLName << "[IsImageRelative]: " << COL.Data[0] << '\n'; |
| 266 | outs() << COLName << "[OffsetToTop]: " << COL.Data[1] << '\n'; |
| 267 | outs() << COLName << "[VFPtrOffset]: " << COL.Data[2] << '\n'; |
| 268 | outs() << COLName << "[TypeDescriptor]: " << COL.Symbols[0] << '\n'; |
| 269 | outs() << COLName << "[ClassHierarchyDescriptor]: " << COL.Symbols[1] << '\n'; |
| 270 | } |
| 271 | for (const std::pair<StringRef, ClassHierarchyDescriptor> &CHDPair : CHDs) { |
| 272 | StringRef CHDName = CHDPair.first; |
| 273 | const ClassHierarchyDescriptor &CHD = CHDPair.second; |
| 274 | outs() << CHDName << "[AlwaysZero]: " << CHD.Data[0] << '\n'; |
| 275 | outs() << CHDName << "[Flags]: " << CHD.Data[1] << '\n'; |
| 276 | outs() << CHDName << "[NumClasses]: " << CHD.Data[2] << '\n'; |
| 277 | outs() << CHDName << "[BaseClassArray]: " << CHD.Symbols[0] << '\n'; |
| 278 | } |
| 279 | for (const std::pair<std::pair<StringRef, uint64_t>, StringRef> &BCAEntry : |
| 280 | BCAEntries) { |
| 281 | StringRef BCAName = BCAEntry.first.first; |
| 282 | uint64_t Offset = BCAEntry.first.second; |
| 283 | StringRef SymName = BCAEntry.second; |
| 284 | outs() << BCAName << '[' << Offset << "]: " << SymName << '\n'; |
| 285 | } |
| 286 | for (const std::pair<StringRef, BaseClassDescriptor> &BCDPair : BCDs) { |
| 287 | StringRef BCDName = BCDPair.first; |
| 288 | const BaseClassDescriptor &BCD = BCDPair.second; |
| 289 | outs() << BCDName << "[TypeDescriptor]: " << BCD.Symbols[0] << '\n'; |
| 290 | outs() << BCDName << "[NumBases]: " << BCD.Data[0] << '\n'; |
| 291 | outs() << BCDName << "[OffsetInVBase]: " << BCD.Data[1] << '\n'; |
| 292 | outs() << BCDName << "[VBPtrOffset]: " << BCD.Data[2] << '\n'; |
| 293 | outs() << BCDName << "[OffsetInVBTable]: " << BCD.Data[3] << '\n'; |
| 294 | outs() << BCDName << "[Flags]: " << BCD.Data[4] << '\n'; |
| 295 | outs() << BCDName << "[ClassHierarchyDescriptor]: " << BCD.Symbols[1] << '\n'; |
| 296 | } |
| 297 | for (const std::pair<StringRef, TypeDescriptor> &TDPair : TDs) { |
| 298 | StringRef TDName = TDPair.first; |
| 299 | const TypeDescriptor &TD = TDPair.second; |
| 300 | outs() << TDName << "[VFPtr]: " << TD.Symbols[0] << '\n'; |
| 301 | uint32_t AlwaysZero = 0; |
| 302 | for (aligned_little32_t Data : TD.Data) |
| 303 | AlwaysZero |= Data; |
| 304 | outs() << TDName << "[AlwaysZero]: " << AlwaysZero << '\n'; |
| 305 | outs() << TDName << "[MangledName]: "; |
| 306 | outs().write_escaped(TD.MangledName, /*UseHexEscapes=*/true) << '\n'; |
| 307 | } |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | static void dumpArchive(const Archive *Arc) { |
David Majnemer | eac48b6 | 2014-09-25 22:56:54 +0000 | [diff] [blame] | 311 | for (const Archive::Child &ArcC : Arc->children()) { |
| 312 | ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcC.getAsBinary(); |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 313 | if (std::error_code EC = ChildOrErr.getError()) { |
| 314 | // Ignore non-object files. |
| 315 | if (EC != object_error::invalid_file_type) |
| 316 | reportError(Arc->getFileName(), EC.message()); |
| 317 | continue; |
| 318 | } |
| 319 | |
| 320 | if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get())) |
| 321 | dumpVTables(Obj); |
| 322 | else |
| 323 | reportError(Arc->getFileName(), |
| 324 | vtabledump_error::unrecognized_file_format); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | static void dumpInput(StringRef File) { |
| 329 | // If file isn't stdin, check that it exists. |
| 330 | if (File != "-" && !sys::fs::exists(File)) { |
| 331 | reportError(File, vtabledump_error::file_not_found); |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | // Attempt to open the binary. |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 336 | ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File); |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 337 | if (std::error_code EC = BinaryOrErr.getError()) { |
| 338 | reportError(File, EC); |
| 339 | return; |
| 340 | } |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 341 | Binary &Binary = *BinaryOrErr.get().getBinary(); |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 342 | |
Rafael Espindola | 3f6481d | 2014-08-01 14:31:55 +0000 | [diff] [blame] | 343 | if (Archive *Arc = dyn_cast<Archive>(&Binary)) |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 344 | dumpArchive(Arc); |
Rafael Espindola | 3f6481d | 2014-08-01 14:31:55 +0000 | [diff] [blame] | 345 | else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary)) |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 346 | dumpVTables(Obj); |
| 347 | else |
| 348 | reportError(File, vtabledump_error::unrecognized_file_format); |
| 349 | } |
| 350 | |
| 351 | int main(int argc, const char *argv[]) { |
| 352 | sys::PrintStackTraceOnErrorSignal(); |
| 353 | PrettyStackTraceProgram X(argc, argv); |
| 354 | llvm_shutdown_obj Y; |
| 355 | |
| 356 | // Initialize targets. |
| 357 | llvm::InitializeAllTargetInfos(); |
| 358 | |
| 359 | // Register the target printer for --version. |
| 360 | cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); |
| 361 | |
| 362 | cl::ParseCommandLineOptions(argc, argv, "LLVM VTable Dumper\n"); |
| 363 | |
| 364 | // Default to stdin if no filename is specified. |
| 365 | if (opts::InputFilenames.size() == 0) |
| 366 | opts::InputFilenames.push_back("-"); |
| 367 | |
| 368 | std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(), |
| 369 | dumpInput); |
| 370 | |
| 371 | return ReturnValue; |
| 372 | } |