LLVM,获取内部结构的偏移量

问题描述 投票:3回答:2

我想在LLVM中使用每个struct元素的细粒度偏移量。例如:

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

对于struct B,我想枚举其中的每个元素,即:offset(s.c)= 0,offset(s.sa.a)= 4,offset(s.sa.b)= 8。如何在我的pass而不是dump()中获取此信息(可以使用哪种API?)?

c++ llvm
2个回答
6
投票

使用Module::getDataLayout获得一个DataLayout对象,它可以给你一个StructLayout作为DataLayout::getStructLayout的回归。 StructLayoutgetElementOffset()方法,可以做你想要的。


1
投票

这是你使用offsetof的地方

int main() {
    cout << offsetof(A, a) << ' ' << offsetof(A, b);
}
© www.soinside.com 2019 - 2024. All rights reserved.