[llvm-dev] Determine the offset of a vtable ptr

Gaier, Bjoern via llvm-dev llvm-dev at lists.llvm.org
Fri May 3 01:32:00 PDT 2019


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

-----Original Message-----
From: Tim Northover <t.p.northover at gmail.com>
Sent: Freitag, 3. Mai 2019 09:18
To: Gaier, Bjoern <Bjoern.Gaier at horiba.com>
Cc: llvm-dev <llvm-dev at lists.llvm.org>
Subject: Re: [llvm-dev] Determine the offset of a vtable ptr

Hi Bjoern,

On Fri, 3 May 2019 at 08:02, Gaier, Bjoern via llvm-dev <llvm-dev at lists.llvm.org> wrote:
> 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.
Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, USt.ID-Nr. DE 114 165 789 Geschäftsführer: Dr. Hiroshi Nakamura, Dr. Robert Plank, Markus Bode, Heiko Lampert, Takashi Nagano, Takeshi Fukushima. Junichi Tajika


More information about the llvm-dev mailing list