mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-12-28 04:33:05 +00:00
llvm-vtabledump: A vtable dumper
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
This commit is contained in:
parent
d10aa6f8b2
commit
96171327db
BIN
test/tools/llvm-vtabledump/Inputs/trivial.obj.coff-i386
Normal file
BIN
test/tools/llvm-vtabledump/Inputs/trivial.obj.coff-i386
Normal file
Binary file not shown.
7
test/tools/llvm-vtabledump/trivial.test
Normal file
7
test/tools/llvm-vtabledump/trivial.test
Normal file
@ -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
|
@ -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()
|
||||
|
@ -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)
|
||||
|
10
tools/llvm-vtabledump/CMakeLists.txt
Normal file
10
tools/llvm-vtabledump/CMakeLists.txt
Normal file
@ -0,0 +1,10 @@
|
||||
set(LLVM_LINK_COMPONENTS
|
||||
${LLVM_TARGETS_TO_BUILD}
|
||||
Object
|
||||
Support
|
||||
)
|
||||
|
||||
add_llvm_tool(llvm-vtabledump
|
||||
llvm-vtabledump.cpp
|
||||
Error.cpp
|
||||
)
|
43
tools/llvm-vtabledump/Error.cpp
Normal file
43
tools/llvm-vtabledump/Error.cpp
Normal file
@ -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
|
39
tools/llvm-vtabledump/Error.h
Normal file
39
tools/llvm-vtabledump/Error.h
Normal file
@ -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
|
22
tools/llvm-vtabledump/LLVMBuild.txt
Normal file
22
tools/llvm-vtabledump/LLVMBuild.txt
Normal file
@ -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
|
18
tools/llvm-vtabledump/Makefile
Normal file
18
tools/llvm-vtabledump/Makefile
Normal file
@ -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
|
||||
|
203
tools/llvm-vtabledump/llvm-vtabledump.cpp
Normal file
203
tools/llvm-vtabledump/llvm-vtabledump.cpp
Normal file
@ -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;
|
||||
}
|
23
tools/llvm-vtabledump/llvm-vtabledump.h
Normal file
23
tools/llvm-vtabledump/llvm-vtabledump.h
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user