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 | |
| 71 | static void dumpVTables(const ObjectFile *Obj) { |
| 72 | std::map<std::pair<StringRef, uint64_t>, StringRef> VFTableEntries; |
David Majnemer | bf32f77 | 2014-07-25 04:30:11 +0000 | [diff] [blame] | 73 | std::map<StringRef, ArrayRef<aligned_little32_t>> VBTables; |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 74 | for (const object::SymbolRef &Sym : Obj->symbols()) { |
| 75 | StringRef SymName; |
| 76 | if (error(Sym.getName(SymName))) |
| 77 | return; |
| 78 | // VFTables in the MS-ABI start with '??_7' and are contained within their |
| 79 | // own COMDAT section. We then determine the contents of the VFTable by |
| 80 | // looking at each relocation in the section. |
| 81 | if (SymName.startswith("??_7")) { |
| 82 | object::section_iterator SecI(Obj->section_begin()); |
| 83 | if (error(Sym.getSection(SecI))) |
| 84 | return; |
| 85 | if (SecI == Obj->section_end()) |
| 86 | continue; |
| 87 | // Each relocation either names a virtual method or a thunk. We note the |
| 88 | // offset into the section and the symbol used for the relocation. |
| 89 | for (const object::RelocationRef &Reloc : SecI->relocations()) { |
| 90 | const object::symbol_iterator RelocSymI = Reloc.getSymbol(); |
| 91 | if (RelocSymI == Obj->symbol_end()) |
| 92 | continue; |
| 93 | StringRef RelocSymName; |
| 94 | if (error(RelocSymI->getName(RelocSymName))) |
| 95 | return; |
| 96 | uint64_t Offset; |
| 97 | if (error(Reloc.getOffset(Offset))) |
| 98 | return; |
| 99 | VFTableEntries[std::make_pair(SymName, Offset)] = RelocSymName; |
| 100 | } |
| 101 | } |
| 102 | // VBTables in the MS-ABI start with '??_8' and are filled with 32-bit |
| 103 | // offsets of virtual bases. |
| 104 | else if (SymName.startswith("??_8")) { |
| 105 | object::section_iterator SecI(Obj->section_begin()); |
| 106 | if (error(Sym.getSection(SecI))) |
| 107 | return; |
| 108 | if (SecI == Obj->section_end()) |
| 109 | continue; |
| 110 | StringRef SecContents; |
| 111 | if (error(SecI->getContents(SecContents))) |
| 112 | return; |
| 113 | |
| 114 | ArrayRef<aligned_little32_t> VBTableData( |
| 115 | reinterpret_cast<const aligned_little32_t *>(SecContents.data()), |
| 116 | SecContents.size() / sizeof(aligned_little32_t)); |
| 117 | VBTables[SymName] = VBTableData; |
| 118 | } |
| 119 | } |
| 120 | for ( |
| 121 | const std::pair<std::pair<StringRef, uint64_t>, StringRef> &VFTableEntry : |
| 122 | VFTableEntries) { |
| 123 | StringRef VFTableName = VFTableEntry.first.first; |
| 124 | uint64_t Offset = VFTableEntry.first.second; |
| 125 | StringRef SymName = VFTableEntry.second; |
| 126 | outs() << VFTableName << '[' << Offset << "]: " << SymName << '\n'; |
| 127 | } |
David Majnemer | bf32f77 | 2014-07-25 04:30:11 +0000 | [diff] [blame] | 128 | for (const std::pair<StringRef, ArrayRef<aligned_little32_t>> &VBTable : |
| 129 | VBTables) { |
| 130 | StringRef VBTableName = VBTable.first; |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 131 | uint32_t Idx = 0; |
David Majnemer | bf32f77 | 2014-07-25 04:30:11 +0000 | [diff] [blame] | 132 | for (aligned_little32_t Offset : VBTable.second) { |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 133 | outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n'; |
David Majnemer | bf32f77 | 2014-07-25 04:30:11 +0000 | [diff] [blame] | 134 | Idx += sizeof(Offset); |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | static void dumpArchive(const Archive *Arc) { |
David Majnemer | eac48b6 | 2014-09-25 22:56:54 +0000 | [diff] [blame] | 140 | for (const Archive::Child &ArcC : Arc->children()) { |
| 141 | ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcC.getAsBinary(); |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 142 | if (std::error_code EC = ChildOrErr.getError()) { |
| 143 | // Ignore non-object files. |
| 144 | if (EC != object_error::invalid_file_type) |
| 145 | reportError(Arc->getFileName(), EC.message()); |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get())) |
| 150 | dumpVTables(Obj); |
| 151 | else |
| 152 | reportError(Arc->getFileName(), |
| 153 | vtabledump_error::unrecognized_file_format); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | static void dumpInput(StringRef File) { |
| 158 | // If file isn't stdin, check that it exists. |
| 159 | if (File != "-" && !sys::fs::exists(File)) { |
| 160 | reportError(File, vtabledump_error::file_not_found); |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | // Attempt to open the binary. |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 165 | ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File); |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 166 | if (std::error_code EC = BinaryOrErr.getError()) { |
| 167 | reportError(File, EC); |
| 168 | return; |
| 169 | } |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 170 | Binary &Binary = *BinaryOrErr.get().getBinary(); |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 171 | |
Rafael Espindola | 3f6481d | 2014-08-01 14:31:55 +0000 | [diff] [blame] | 172 | if (Archive *Arc = dyn_cast<Archive>(&Binary)) |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 173 | dumpArchive(Arc); |
Rafael Espindola | 3f6481d | 2014-08-01 14:31:55 +0000 | [diff] [blame] | 174 | else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary)) |
David Majnemer | 72ab1a5 | 2014-07-24 23:14:40 +0000 | [diff] [blame] | 175 | dumpVTables(Obj); |
| 176 | else |
| 177 | reportError(File, vtabledump_error::unrecognized_file_format); |
| 178 | } |
| 179 | |
| 180 | int main(int argc, const char *argv[]) { |
| 181 | sys::PrintStackTraceOnErrorSignal(); |
| 182 | PrettyStackTraceProgram X(argc, argv); |
| 183 | llvm_shutdown_obj Y; |
| 184 | |
| 185 | // Initialize targets. |
| 186 | llvm::InitializeAllTargetInfos(); |
| 187 | |
| 188 | // Register the target printer for --version. |
| 189 | cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); |
| 190 | |
| 191 | cl::ParseCommandLineOptions(argc, argv, "LLVM VTable Dumper\n"); |
| 192 | |
| 193 | // Default to stdin if no filename is specified. |
| 194 | if (opts::InputFilenames.size() == 0) |
| 195 | opts::InputFilenames.push_back("-"); |
| 196 | |
| 197 | std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(), |
| 198 | dumpInput); |
| 199 | |
| 200 | return ReturnValue; |
| 201 | } |