Gaier, Bjoern via llvm-dev
2019-May-03 07:02 UTC
[llvm-dev] 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 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190503/d4bdff52/attachment.html>
Tim Northover via llvm-dev
2019-May-03 07:17 UTC
[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.
Gaier, Bjoern via llvm-dev
2019-May-03 08:32 UTC
[llvm-dev] Determine the offset of a vtable ptr
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 offsetsIsn'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