我如何mangle c++名称(对于Linux上的GCC编译对象)?
我想通过编程方式操纵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
完全不是同一件事。
注意:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论