Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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.

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.

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.

like image 809
WilliamKF Avatar asked Jul 04 '12 21:07

WilliamKF


People also ask

Does C have name mangling?

Since C is a programming language that does not support name function overloading, it does no name mangling.

What does c++ filt do?

The c++filt utility is a filter that copies characters from file names or standard input to standard output, replacing all mangled names with their corresponding demangled names.


2 Answers

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.

like image 59
Dani Avatar answered Oct 07 '22 02:10

Dani


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.

like image 32
rts1 Avatar answered Oct 07 '22 02:10

rts1