什么是用于破坏 C++ 符号名称的 Linux 实用程序?

我有 c++filt 命令来对符号进行分解,有什么工具可以做相反的事情并破坏符号名称?

I have c++filt command to demangle a symbol, what is the tool to do the opposite and mangle a symbol name?

如果我想在一个损坏的 C++ 函数名称上调用 dlsym(),这将很有用.我宁愿不要在代码中硬编码这个名字,因为它可能会随着时间的推移而改变,因为新的编译器版本或新的编译器品牌正在使用,或者目前由于为多个平台编译.

This would be useful if I were to want to call dlsym() on a mangled C++ function name. I'd rather not hard code the name mangling in the code since it could change over time due to new complier versions or new compiler brands being used or at present due to compiling for multiple platforms.

是否有一种编程方式可以在运行时获取表示 C++ 函数的字符串,以便代码独立于编译器?一种可能的方法是在编译时调用一个实用程序,该实用程序为正在使用的编译器执行名称修改并将适当的修改后的 C++ 符号名称插入到字符串中以供 dlsym() 使用.

Is there a programatic way to get the string that represents a C++ function at runtime so that the code is compiler independent? One way to possibly do this would be to call a utility at compile time that performs the name mangling for the compiler being used and inserts the appropriate mangled C++ symbol name into a string for dlsym() to use.

这是最接近我在此站点上找到的解决方案,它是通过使用固定的 C 样式完成的名称间接指向您希望 dlsym() 的库中定义的 C++ 符号,但如果您无法控制该库提供的内容,则不能选择.

Here is the closest to a solution I've found on this site which is accomplished by using a fixed C style name to indirect to C++ symbols that are defined in the library you wish to dlsym(), but if you do not have control over what that library provides, this is not an option.

推荐答案

看符号表或许能得到你想要的.so 您正在查看的内容:其他人已经回答了这个问题返回共享库符号表.

You may be able to get what you want by looking at the symbol table of the .so you are looking at: Someone else answered this already Returning a shared library symbol table.

但是,如果符号太多......那可能不起作用.
所以这是一个疯狂的想法.警告购买者!

However, if there are too many symbols ... that may not work.
So here's a crazy idea. Caveat emptor!

一个潜在的解决方案是:

A potential solution is to:

  1. 创建一个文件,其存根只有一个名称:您想要的名称:void myfunction() { }

  1. create a file with a stub with exactly one name: the name you want: void myfunction() { }

编译该文件(使用 -fPIC 和 -shared,因此它是一个动态库)

compile that file (with -fPIC and -shared so it's a dynamic library)

对该特定文件调用 dlopen/dlsym

call dlopen/dlsym on that particular file

遍历符号(应该只有一个想要的符号加上您可以过滤的其他常规垃圾).遍历符号很笨拙,但您可以这样做:返回共享库符号表

Iterate through the symbols (there should just be only the one want plus other regular junk you can filter). Iterating through the symbols is clumsy, but you can do it: Returning a shared library symbol table

dlclose() 释放它(从你的符号中丢失存根)

dlclose() to free it up (lose the stub out of your symbols)

用dlopen打开你想要的文件

Open the file you want with dlopen

基本上,你会从你的代码中调用编译器,它会创建a .so 您可以查看,取出唯一的值,然后卸载该 .so所以你可以加载你想要的.

Basically, you would invoke the compiler from your code, it would create a .so you could look at, get the only value out, then unload that .so so you could load in the one you want.

这太疯狂了.

相关文章