我如何mangle c++名称(对于Linux上的GCC编译对象)?

发布于 2025-02-04 08:52:57 字数 1209 浏览 2 评论 0原文

我想通过编程方式操纵C ++函数的名称或变量 - 以获取将在编译对象文件中出现的符号名称。我正在使用Linux和GCC。

现在,为什么这不是微不足道的?例如typeID(foo).name()?因为那不做您想要的事情:考虑以下程序:

#include <iostream>

extern int foo(int x) { return 0; }
extern double bar(int x) { return 1.0; }

int main()
{
    std::cout << typeid(foo).name() << std::endl;
    std::cout << typeid(bar).name() << std::endl;
}

让我们看看给我们的东西:

$ g++ -o a.o -O0 -c a.cpp
$ objdump  -t a.o | egrep "(bar|foo)"
00000000000000dd l     F .text  0000000000000015 _GLOBAL__sub_I__Z3fooi
0000000000000000 g     F .text  000000000000000e _Z3fooi
000000000000000e g     F .text  000000000000001b _Z3bari
$ ./a
FiiE
FiiE

完全不是同一件事。

注意:

  • 您是否想到名称空间ABI?我也是I. 来源似乎没有弄乱,只是删除。
  • 在其他平台+编译器组合上解决相同问题的解决方案也很有趣,如果有的话,我将扩展问题范围。
  • 动机:我想 dlsym() 外部-C功能或变量。

I want to programmatically mangle the name of a C++ function or variable - to get the symbol name which would appear in a compiled object file. I'm using Linux and GCC.

Now, why is this not trivial? e.g. typeid(foo).name()? Because that doesn't do what you want: Consider the following program:

#include <iostream>

extern int foo(int x) { return 0; }
extern double bar(int x) { return 1.0; }

int main()
{
    std::cout << typeid(foo).name() << std::endl;
    std::cout << typeid(bar).name() << std::endl;
}

Let's see what that gives us:

$ g++ -o a.o -O0 -c a.cpp
$ objdump  -t a.o | egrep "(bar|foo)"
00000000000000dd l     F .text  0000000000000015 _GLOBAL__sub_I__Z3fooi
0000000000000000 g     F .text  000000000000000e _Z3fooi
000000000000000e g     F .text  000000000000001b _Z3bari
$ ./a
FiiE
FiiE

Not at all the same thing.

Notes:

  • Were you thinking of namespace abi? So was I. The source doesn't seem to do mangling, only demangling.
  • Solutions for the same problem on other platform+compiler combinations are also interesting, and if you have them I'll expand the question scope.
  • Motivation: I want to dlsym() a non-extern-C function or a variable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文