1 //===- llvm-vtabledump.cpp - Dump vtables in an Object File -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Dumps VTables resident in object files and archives. Note, it currently only
11 // supports MS-ABI style object files.
13 //===----------------------------------------------------------------------===//
15 #include "llvm-vtabledump.h"
17 #include "llvm/ADT/ArrayRef.h"
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"
30 #include <system_error>
33 using namespace llvm::object;
34 using namespace llvm::support;
37 cl::list<std::string> InputFilenames(cl::Positional,
38 cl::desc("<input object files>"),
42 static int ReturnValue = EXIT_SUCCESS;
46 bool error(std::error_code EC) {
50 ReturnValue = EXIT_FAILURE;
51 outs() << "\nError reading file: " << EC.message() << ".\n";
58 static void reportError(StringRef Input, StringRef Message) {
62 errs() << Input << ": " << Message << "\n";
64 ReturnValue = EXIT_FAILURE;
67 static void reportError(StringRef Input, std::error_code EC) {
68 reportError(Input, EC.message());
71 static void dumpVTables(const ObjectFile *Obj) {
72 std::map<std::pair<StringRef, uint64_t>, StringRef> VFTableEntries;
73 std::map<StringRef, ArrayRef<aligned_little32_t>> VBTables;
74 for (const object::SymbolRef &Sym : Obj->symbols()) {
76 if (error(Sym.getName(SymName)))
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)))
85 if (SecI == Obj->section_end())
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())
93 StringRef RelocSymName;
94 if (error(RelocSymI->getName(RelocSymName)))
97 if (error(Reloc.getOffset(Offset)))
99 VFTableEntries[std::make_pair(SymName, Offset)] = RelocSymName;
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)))
108 if (SecI == Obj->section_end())
110 StringRef SecContents;
111 if (error(SecI->getContents(SecContents)))
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;
121 const std::pair<std::pair<StringRef, uint64_t>, StringRef> &VFTableEntry :
123 StringRef VFTableName = VFTableEntry.first.first;
124 uint64_t Offset = VFTableEntry.first.second;
125 StringRef SymName = VFTableEntry.second;
126 outs() << VFTableName << '[' << Offset << "]: " << SymName << '\n';
128 for (const std::pair<StringRef, ArrayRef<aligned_little32_t>> &VBTable :
130 StringRef VBTableName = VBTable.first;
132 for (aligned_little32_t Offset : VBTable.second) {
133 outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n';
134 Idx += sizeof(Offset);
139 static void dumpArchive(const Archive *Arc) {
140 for (const Archive::Child &ArcC : Arc->children()) {
141 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcC.getAsBinary();
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());
149 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
152 reportError(Arc->getFileName(),
153 vtabledump_error::unrecognized_file_format);
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);
164 // Attempt to open the binary.
165 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
166 if (std::error_code EC = BinaryOrErr.getError()) {
167 reportError(File, EC);
170 Binary &Binary = *BinaryOrErr.get().getBinary();
172 if (Archive *Arc = dyn_cast<Archive>(&Binary))
174 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
177 reportError(File, vtabledump_error::unrecognized_file_format);
180 int main(int argc, const char *argv[]) {
181 sys::PrintStackTraceOnErrorSignal();
182 PrettyStackTraceProgram X(argc, argv);
185 // Initialize targets.
186 llvm::InitializeAllTargetInfos();
188 // Register the target printer for --version.
189 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
191 cl::ParseCommandLineOptions(argc, argv, "LLVM VTable Dumper\n");
193 // Default to stdin if no filename is specified.
194 if (opts::InputFilenames.size() == 0)
195 opts::InputFilenames.push_back("-");
197 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),