获取C++成员函数以及相应的偏移量通过LLVM IR

huangapple go评论78阅读模式
英文:

Get the C++ member function and the corresponding offset via LLVM IR

问题

I would like to have an overall analysis for c++ classes, and I want to know each member function's offset. Could anyone give me any hint on how to get those information in LLVM IR? I would appreciate any help from you.

A code example could be:

class Circle {
    int radius;
public:
    void setRadius(int _radius){radius = _radius;}
    int getRadius() {return radius;}
};

I would like to know that 1st field is an integer, the 2nd and 3rd field is a member function, and also the definition of the member function.

英文:

I would like to have an overall analysis for c++ classes, and I want to know each member function's offset. Could anyone give me any hint on how to get those information in LLVM IR? I would appreciate any help from you.

A code example could be:

class Circle {
    int radius;
public:
    void setRadius(int _radius){radius = _radius;}
    int getRadius() {return radius;}
};

I would like to know that 1st field is an integer, the 2nd and 3rd field is a member function, and also the definition of the member function.

答案1

得分: 2

不真正存在所谓的“成员函数的偏移量”。函数可以被完全优化掉,就像这里一样。您可以在此处查看IR,本质上是相同的。虚函数将具有进入vtable的偏移量,但那是另一回事。

您可以通过获取函数的指针来强制存在一个地址。然后,您可以计算一个“偏移量”,但相对于什么呢?不确定LLVM是否保证将成员函数放置在文本段中。

英文:

There's not really such a thing as a "member function's offset". Functions can be totally optimized away as is the case here. You can take a look at the IR here and it's essentially the same thing. Virtual functions will have an offset into a vtable, but that's another matter.

You can force there to be an address by taking a pointer to a function. You could then calculate an "offset", but relative to what exactly? Not sure there's any guarantee from LLVM how member functions are placed in the text segment.

huangapple
  • 本文由 发表于 2023年1月6日 11:18:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定