What is Linux utility to mangle a C++ symbol name?

南楼画角 提交于 2019-11-27 23:24:50
rts1

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. create a file with a stub with exactly one name: the name you want: void myfunction() { }

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

  3. call dlopen/dlsym on that particular file

  4. 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

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

  6. Open the file you want with dlopen

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.

It's crazy.

Dani

That's how g++ mangles names. You might implement those mangling rules on your program.

Another (crazy) solution would be to list all of the symbols in the library you want to use (it's not so difficult if you understand the format), demangle them all, and search your function's name in that list. The advantage with this method is that demangling is easier, as there is a function call to do it: abi::__cxa_demangle, from cxxabi.h header.

Name mangling is implementation specific.

There is no standard for name mangling so your best bet is to find a compiler to do it for you.

Name mangling

There is a table here that may help you if you wish to do this manually

Rémi Cohen-Scali

An easier method than the first posted. Write a little C++ program like:

#include <stdlib.h>

extern int doit(const char *toto, bool is);

int main(int argc, char *argv[])
{
  exit(doit (argv[0], true));
}

Build it with

# g++ -S test.cpp

And extract symbol name from assembler source

# cat test.s | grep call | grep doit | awk '{print $2}'

You get:

rcoscali@srjlx0001:/tmp/TestC++$ cat test.s | grep call | grep doit | awk '{print $2}'
_Z4doitPKcb
rcoscali@srjlx0001:/tmp/TestC++$ 

The doit symbol mangled is _Z4doitPKcb Use the compiler you plan to use because each compiler have its own name mangling rules (as it has been said before from one compiler to another these rules may change).

Have fun !

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!