Determine the offset of a vtable ptr

Dear LLVM-Mailing-List,

inspired by a Mail some days ago I wondered, if it is possible to determine the offset of a pointer to a specific vtable of a class via the llvm::Module.

For this I created two classes A & B which both used virtual functions. Now I created a class C which inherits A & B. In my simple understanding of C++ class ‘C’ would have two additional vtable pointers for A & B - and I would want to know there offset.

I compiled the code the LLVM assembly and tried to locate the offset there. Sadly - I was not possible to understand the assembly well enough to find those offsets. Because of this, I was not even able to try this via the functions of the llvm::Module.

So… Is that possible? And how would I do this via llvm::Module?

I’m really sorry if this question a duplication of if the answers if obvious. I’m not much experienced with the LLVM at all.

Thank you in advance

Kind greetings

Björn

Hi Bjoern,

For this I created two classes A & B which both used virtual functions. Now I created a class C which inherits A & B. In my simple understanding of C++ class 'C' would have two additional vtable pointers for A & B - and I would want to know there offset.

The canonical documentation for this is here (at least on most
non-Windows platforms):
https://itanium-cxx-abi.github.io/cxx-abi/abi.html

There are a couple of Clang options that can help you by printing out
where Clang thinks these things are: "-Xclang -fdump-record-layouts"
prints the layout of every struct that's actually used in a source
file, and "-Xclang -fdump-vtable-layouts" prints what pointers Clang
puts at each location in the vtables it generates.

So... Is that possible? And how would I do this via llvm::Module?

There's nothing in Module to help you determine the offsets, you have
to know or discover them by implementing the C++ ABI. Once you do know
the offsets you obviously use a getelementptr instruction to access
the pointers stored there.

Cheers.

Tim.

Hi Tim,

There are a couple of Clang options that can help you by printing out where Clang thinks these things are: "-Xclang -fdump-record-layouts"
prints the layout of every struct that's actually used in a source file, and "-Xclang -fdump-vtable-layouts" prints what pointers Clang puts at each location in the vtables it generates.

Those dumping functions are really cool! Thank you!

There's nothing in Module to help you determine the offsets

Isn't it possible to access structures and there members via llvm::Module? So wouldn't I be able to find the vtables there?

Kind greetings
Björn

If you've got a pointer to a class then you can use IRBuilder to
access its vtable (assuming you know the offset in the object) and
functions within that vtable. At the Module level if you know the
specific class you want a vtable for you can get it by name (e.g.
@_ZTV3Foo for "struct Foo").

Cheers.

Tim.