llvm-vtabledump: A vtable dumper
authorDavid Majnemer <david.majnemer@gmail.com>
Thu, 24 Jul 2014 23:14:40 +0000 (23:14 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Thu, 24 Jul 2014 23:14:40 +0000 (23:14 +0000)
This tool's job is to dump the vtables inside object files.  It is
currently limited to MS ABI vf- and vb-tables but it will eventually
support Itanium-style v-tables as well.

Differential Revision: http://reviews.llvm.org/D4584

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@213903 91177308-0d34-0410-b5e6-96231b3b80d8

test/tools/llvm-vtabledump/Inputs/trivial.obj.coff-i386 [new file with mode: 0644]
test/tools/llvm-vtabledump/trivial.test [new file with mode: 0644]
tools/CMakeLists.txt
tools/Makefile
tools/llvm-vtabledump/CMakeLists.txt [new file with mode: 0644]
tools/llvm-vtabledump/Error.cpp [new file with mode: 0644]
tools/llvm-vtabledump/Error.h [new file with mode: 0644]
tools/llvm-vtabledump/LLVMBuild.txt [new file with mode: 0644]
tools/llvm-vtabledump/Makefile [new file with mode: 0644]
tools/llvm-vtabledump/llvm-vtabledump.cpp [new file with mode: 0644]
tools/llvm-vtabledump/llvm-vtabledump.h [new file with mode: 0644]

diff --git a/test/tools/llvm-vtabledump/Inputs/trivial.obj.coff-i386 b/test/tools/llvm-vtabledump/Inputs/trivial.obj.coff-i386
new file mode 100644 (file)
index 0000000..3b93955
Binary files /dev/null and b/test/tools/llvm-vtabledump/Inputs/trivial.obj.coff-i386 differ
diff --git a/test/tools/llvm-vtabledump/trivial.test b/test/tools/llvm-vtabledump/trivial.test
new file mode 100644 (file)
index 0000000..e24d6ad
--- /dev/null
@@ -0,0 +1,7 @@
+RUN: llvm-vtabledump %p/Inputs/trivial.obj.coff-i386 \
+RUN:   | FileCheck %s
+
+CHECK:      ??_7S@@6B@[0]: ??_R4S@@6B@
+CHECK-NEXT: ??_7S@@6B@[4]: ??_GS@@UAEPAXI@Z
+CHECK-NEXT: ??_8S@@7B@[0]: -4
+CHECK-NEXT: ??_8S@@7B@[4]: 4
index 846ad1e..acc4eb1 100644 (file)
@@ -30,6 +30,7 @@ add_llvm_tool_subdirectory(llvm-objdump)
 add_llvm_tool_subdirectory(llvm-readobj)
 add_llvm_tool_subdirectory(llvm-rtdyld)
 add_llvm_tool_subdirectory(llvm-dwarfdump)
+add_llvm_tool_subdirectory(llvm-vtabledump)
 if( LLVM_USE_INTEL_JITEVENTS )
   add_llvm_tool_subdirectory(llvm-jitlistener)
 else()
index 97ad99a..012270d 100644 (file)
@@ -31,7 +31,8 @@ PARALLEL_DIRS := opt llvm-as llvm-dis llc llvm-ar llvm-nm llvm-link \
                  lli llvm-extract llvm-mc bugpoint llvm-bcanalyzer llvm-diff \
                  macho-dump llvm-objdump llvm-readobj llvm-rtdyld \
                  llvm-dwarfdump llvm-cov llvm-size llvm-stress llvm-mcmarkup \
-                 llvm-profdata llvm-symbolizer obj2yaml yaml2obj llvm-c-test
+                 llvm-profdata llvm-symbolizer obj2yaml yaml2obj llvm-c-test \
+                 llvm-vtabledump
 
 # If Intel JIT Events support is configured, build an extra tool to test it.
 ifeq ($(USE_INTEL_JITEVENTS), 1)
diff --git a/tools/llvm-vtabledump/CMakeLists.txt b/tools/llvm-vtabledump/CMakeLists.txt
new file mode 100644 (file)
index 0000000..4fe205b
--- /dev/null
@@ -0,0 +1,10 @@
+set(LLVM_LINK_COMPONENTS
+  ${LLVM_TARGETS_TO_BUILD}
+  Object
+  Support
+  )
+
+add_llvm_tool(llvm-vtabledump
+  llvm-vtabledump.cpp
+  Error.cpp
+  )
diff --git a/tools/llvm-vtabledump/Error.cpp b/tools/llvm-vtabledump/Error.cpp
new file mode 100644 (file)
index 0000000..c5de895
--- /dev/null
@@ -0,0 +1,43 @@
+//===- Error.cpp - system_error extensions for llvm-vtabledump --*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This defines a new error_category for the llvm-vtabledump tool.
+//
+//===----------------------------------------------------------------------===//
+
+#include "Error.h"
+#include "llvm/Support/ErrorHandling.h"
+
+using namespace llvm;
+
+namespace {
+class vtabledump_error_category : public std::error_category {
+public:
+  const char *name() const LLVM_NOEXCEPT override { return "llvm.vtabledump"; }
+  std::string message(int ev) const override {
+    switch (static_cast<vtabledump_error>(ev)) {
+    case vtabledump_error::success:
+      return "Success";
+    case vtabledump_error::file_not_found:
+      return "No such file.";
+    case vtabledump_error::unrecognized_file_format:
+      return "Unrecognized file type.";
+    }
+    llvm_unreachable(
+        "An enumerator of vtabledump_error does not have a message defined.");
+  }
+};
+} // namespace
+
+namespace llvm {
+const std::error_category &vtabledump_category() {
+  static vtabledump_error_category o;
+  return o;
+}
+} // namespace llvm
diff --git a/tools/llvm-vtabledump/Error.h b/tools/llvm-vtabledump/Error.h
new file mode 100644 (file)
index 0000000..73e0d1a
--- /dev/null
@@ -0,0 +1,39 @@
+//===- Error.h - system_error extensions for llvm-vtabledump ----*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This declares a new error_category for the llvm-vtabledump tool.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_TOOLS_VTABLEDUMP_ERROR_H
+#define LLVM_TOOLS_VTABLEDUMP_ERROR_H
+
+#include <system_error>
+
+namespace llvm {
+const std::error_category &vtabledump_category();
+
+enum class vtabledump_error {
+  success = 0,
+  file_not_found,
+  unrecognized_file_format,
+};
+
+inline std::error_code make_error_code(vtabledump_error e) {
+  return std::error_code(static_cast<int>(e), vtabledump_category());
+}
+
+} // namespace llvm
+
+namespace std {
+template <>
+struct is_error_code_enum<llvm::vtabledump_error> : std::true_type {};
+}
+
+#endif
diff --git a/tools/llvm-vtabledump/LLVMBuild.txt b/tools/llvm-vtabledump/LLVMBuild.txt
new file mode 100644 (file)
index 0000000..6a3cbff
--- /dev/null
@@ -0,0 +1,22 @@
+;===- ./tools/llvm-vtabledump/LLVMBuild.txt --------------------*- Conf -*--===;
+;
+;                     The LLVM Compiler Infrastructure
+;
+; This file is distributed under the University of Illinois Open Source
+; License. See LICENSE.TXT for details.
+;
+;===------------------------------------------------------------------------===;
+;
+; This is an LLVMBuild description file for the components in this subdirectory.
+;
+; For more information on the LLVMBuild system, please see:
+;
+;   http://llvm.org/docs/LLVMBuild.html
+;
+;===------------------------------------------------------------------------===;
+
+[component_0]
+type = Tool
+name = llvm-vtabledump
+parent = Tools
+required_libraries = all-targets BitReader Object
diff --git a/tools/llvm-vtabledump/Makefile b/tools/llvm-vtabledump/Makefile
new file mode 100644 (file)
index 0000000..596c64c
--- /dev/null
@@ -0,0 +1,18 @@
+##===- tools/llvm-vtabledump/Makefile ----------------------*- Makefile -*-===##
+# 
+#                     The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+# 
+##===----------------------------------------------------------------------===##
+
+LEVEL := ../..
+TOOLNAME := llvm-vtabledump
+LINK_COMPONENTS := bitreader object all-targets
+
+# This tool has no plugins, optimize startup time.
+TOOL_NO_EXPORTS := 1
+
+include $(LEVEL)/Makefile.common
+
diff --git a/tools/llvm-vtabledump/llvm-vtabledump.cpp b/tools/llvm-vtabledump/llvm-vtabledump.cpp
new file mode 100644 (file)
index 0000000..ce2e9b6
--- /dev/null
@@ -0,0 +1,203 @@
+//===- llvm-vtabledump.cpp - Dump vtables in an Object File -----*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Dumps VTables resident in object files and archives.  Note, it currently only
+// supports MS-ABI style object files.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm-vtabledump.h"
+#include "Error.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/StringMap.h"
+#include "llvm/Object/Archive.h"
+#include "llvm/Object/ObjectFile.h"
+#include "llvm/Support/Debug.h"
+#include "llvm/Support/Endian.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/ManagedStatic.h"
+#include "llvm/Support/PrettyStackTrace.h"
+#include "llvm/Support/Signals.h"
+#include "llvm/Support/TargetRegistry.h"
+#include "llvm/Support/TargetSelect.h"
+#include <map>
+#include <string>
+#include <system_error>
+
+using namespace llvm;
+using namespace llvm::object;
+using namespace llvm::support;
+
+namespace opts {
+cl::list<std::string> InputFilenames(cl::Positional,
+                                     cl::desc("<input object files>"),
+                                     cl::ZeroOrMore);
+} // namespace opts
+
+static int ReturnValue = EXIT_SUCCESS;
+
+namespace llvm {
+
+bool error(std::error_code EC) {
+  if (!EC)
+    return false;
+
+  ReturnValue = EXIT_FAILURE;
+  outs() << "\nError reading file: " << EC.message() << ".\n";
+  outs().flush();
+  return true;
+}
+
+} // namespace llvm
+
+static void reportError(StringRef Input, StringRef Message) {
+  if (Input == "-")
+    Input = "<stdin>";
+
+  errs() << Input << ": " << Message << "\n";
+  errs().flush();
+  ReturnValue = EXIT_FAILURE;
+}
+
+static void reportError(StringRef Input, std::error_code EC) {
+  reportError(Input, EC.message());
+}
+
+static void dumpVTables(const ObjectFile *Obj) {
+  std::map<std::pair<StringRef, uint64_t>, StringRef> VFTableEntries;
+  StringMap<ArrayRef<aligned_little32_t>> VBTables;
+  for (const object::SymbolRef &Sym : Obj->symbols()) {
+    StringRef SymName;
+    if (error(Sym.getName(SymName)))
+      return;
+    // VFTables in the MS-ABI start with '??_7' and are contained within their
+    // own COMDAT section.  We then determine the contents of the VFTable by
+    // looking at each relocation in the section.
+    if (SymName.startswith("??_7")) {
+      object::section_iterator SecI(Obj->section_begin());
+      if (error(Sym.getSection(SecI)))
+        return;
+      if (SecI == Obj->section_end())
+        continue;
+      // Each relocation either names a virtual method or a thunk.  We note the
+      // offset into the section and the symbol used for the relocation.
+      for (const object::RelocationRef &Reloc : SecI->relocations()) {
+        const object::symbol_iterator RelocSymI = Reloc.getSymbol();
+        if (RelocSymI == Obj->symbol_end())
+          continue;
+        StringRef RelocSymName;
+        if (error(RelocSymI->getName(RelocSymName)))
+          return;
+        uint64_t Offset;
+        if (error(Reloc.getOffset(Offset)))
+          return;
+        VFTableEntries[std::make_pair(SymName, Offset)] = RelocSymName;
+      }
+    }
+    // VBTables in the MS-ABI start with '??_8' and are filled with 32-bit
+    // offsets of virtual bases.
+    else if (SymName.startswith("??_8")) {
+      object::section_iterator SecI(Obj->section_begin());
+      if (error(Sym.getSection(SecI)))
+        return;
+      if (SecI == Obj->section_end())
+        continue;
+      StringRef SecContents;
+      if (error(SecI->getContents(SecContents)))
+        return;
+
+      ArrayRef<aligned_little32_t> VBTableData(
+          reinterpret_cast<const aligned_little32_t *>(SecContents.data()),
+          SecContents.size() / sizeof(aligned_little32_t));
+      VBTables[SymName] = VBTableData;
+    }
+  }
+  for (
+      const std::pair<std::pair<StringRef, uint64_t>, StringRef> &VFTableEntry :
+      VFTableEntries) {
+    StringRef VFTableName = VFTableEntry.first.first;
+    uint64_t Offset = VFTableEntry.first.second;
+    StringRef SymName = VFTableEntry.second;
+    outs() << VFTableName << '[' << Offset << "]: " << SymName << '\n';
+  }
+  for (const StringMapEntry<ArrayRef<aligned_little32_t>> &VBTable : VBTables) {
+    StringRef VBTableName = VBTable.getKey();
+    uint32_t Idx = 0;
+    for (aligned_little32_t Offset : VBTable.getValue()) {
+      outs() << VBTableName << '[' << Idx << "]: " << Offset << '\n';
+      Idx += sizeof(aligned_little32_t);
+    }
+  }
+}
+
+static void dumpArchive(const Archive *Arc) {
+  for (Archive::child_iterator ArcI = Arc->child_begin(),
+                               ArcE = Arc->child_end();
+       ArcI != ArcE; ++ArcI) {
+    ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcI->getAsBinary();
+    if (std::error_code EC = ChildOrErr.getError()) {
+      // Ignore non-object files.
+      if (EC != object_error::invalid_file_type)
+        reportError(Arc->getFileName(), EC.message());
+      continue;
+    }
+
+    if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
+      dumpVTables(Obj);
+    else
+      reportError(Arc->getFileName(),
+                  vtabledump_error::unrecognized_file_format);
+  }
+}
+
+static void dumpInput(StringRef File) {
+  // If file isn't stdin, check that it exists.
+  if (File != "-" && !sys::fs::exists(File)) {
+    reportError(File, vtabledump_error::file_not_found);
+    return;
+  }
+
+  // Attempt to open the binary.
+  ErrorOr<Binary *> BinaryOrErr = createBinary(File);
+  if (std::error_code EC = BinaryOrErr.getError()) {
+    reportError(File, EC);
+    return;
+  }
+  std::unique_ptr<Binary> Binary(BinaryOrErr.get());
+
+  if (Archive *Arc = dyn_cast<Archive>(Binary.get()))
+    dumpArchive(Arc);
+  else if (ObjectFile *Obj = dyn_cast<ObjectFile>(Binary.get()))
+    dumpVTables(Obj);
+  else
+    reportError(File, vtabledump_error::unrecognized_file_format);
+}
+
+int main(int argc, const char *argv[]) {
+  sys::PrintStackTraceOnErrorSignal();
+  PrettyStackTraceProgram X(argc, argv);
+  llvm_shutdown_obj Y;
+
+  // Initialize targets.
+  llvm::InitializeAllTargetInfos();
+
+  // Register the target printer for --version.
+  cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
+
+  cl::ParseCommandLineOptions(argc, argv, "LLVM VTable Dumper\n");
+
+  // Default to stdin if no filename is specified.
+  if (opts::InputFilenames.size() == 0)
+    opts::InputFilenames.push_back("-");
+
+  std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
+                dumpInput);
+
+  return ReturnValue;
+}
diff --git a/tools/llvm-vtabledump/llvm-vtabledump.h b/tools/llvm-vtabledump/llvm-vtabledump.h
new file mode 100644 (file)
index 0000000..16a752b
--- /dev/null
@@ -0,0 +1,23 @@
+//===-- llvm-vtabledump.h ---------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_TOOLS_VTABLEDUMP_H
+#define LLVM_TOOLS_VTABLEDUMP_H
+
+#include "llvm/Support/CommandLine.h"
+#include <string>
+
+namespace opts {
+extern llvm::cl::list<std::string> InputFilenames;
+} // namespace opts
+
+#define LLVM_VTABLEDUMP_ENUM_ENT(ns, enum)                                     \
+  { #enum, ns::enum }
+
+#endif