[llvm-dev] Question: How to access c++ vtable pointer to use as Value* in LLVM pass

K Jelesnianski via llvm-dev llvm-dev at lists.llvm.org
Thu Apr 18 21:12:50 PDT 2019

Dear Mailing List,

This might sound unconventional, but I am trying to access a C++
objects vtable to pass as an argument to a function call for a library
function I created. Creating & inserting a function call at the
correct location in LLVM is done.

I have learned that C++ objects are represented as struct types. But
I'm just not quite sure how to get at the vtable pointer within, when
looking at the interface of Value:: class. clang, more specifically
CGClass.cpp, deals with C++ initializing constructors and destructors
and its API is straightforward while I can't find the similar API
calls in the LLVM counter part.

So far I am able to get the class object itself from a loadInst or
CallInst and I can iterate through the StructType, and the structs
"Types" contained within via element_begin()/element_end() to confirm
what I am looking at is the object. e.g.:

i32 (...)***    (this is how vtable is represented according to online
sources as a generic pointer)
i32              (class member in this case an int)

But this doesn't give me a Value* handle i can grab to and use later.
How can I leverage this Value to get that contained ??

2nd question: What happens if the struct object is from a derived
class; iterating over the struct again, it looks like the vtable ptr
is tangled even deeper within the object:

%class.Base.base = type <{i32 (...)**, i32 }>

I looked at the ThreadSanitizer.cpp pass for inspiration, and it seems
they are also using MD_tbaa as hints for whether a load/store
isVTableAccess(), but doesn't need the Value. Maybe MDNode metadata
could be of use here?

TLDR: How can I leverage a Value that is of StructType generated from
a C++ object to get its vtable ptr in LLVM to use as a Value for a
to-be-inserted function call??

Thank you in advance!


Christopher Jelesnianski
Graduate Research Assistant, Virginia Tech

More information about the llvm-dev mailing list