Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LLVM, get the offset of inside struct

Tags:

c++

llvm

I want to use fine-grained offset of each struct element in LLVM. For example:

struct A{
    int a;
    int b;
};
struct B{
    int c;
    struct A sa;
};
struct B s;

For struct B, I want to enumerate each element inside it, i.e: offset(s.c) = 0, offset(s.sa.a) = 4, offset(s.sa.b)=8. How can I get this information (which API could be used?) inside my pass other than dump()?

like image 331
Crystal Avatar asked Jan 29 '23 23:01

Crystal


1 Answers

Use Module::getDataLayout to obtain a DataLayout object, which can give you a StructLayout as a return of DataLayout::getStructLayout. The StructLayout has getElementOffset() method, that does what you want.

like image 184
arrowd Avatar answered Feb 08 '23 08:02

arrowd