Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can objdump un-mangle names of C++ template functions?

I have a C++ object file that contains instantiations of some C++ template functions. The object file in question instantiates the same function for a few different combinations of template parameters. I'm trying to debug a problem and would like to look at the disassembly of a specific instantiation of the template function (that is, I know the template parameters for the function that I want to examine). I would typically do this using objdump to disassemble the object file, but it (at least by default) isn't able to de-mangle the C++ function names. Is there any way to do this? The object files were created using gcc 4.6.1.

like image 863
Jason R Avatar asked Feb 23 '12 18:02

Jason R


2 Answers

objdump -C

The -C flag enables demangling:

printf '
template<typename T>
T add(T x, T y) {
    return x + y;
}

void h() {
    add(1, 2);
    add(1.0, 2.0);
}
' > a.cpp
g++ -g -std=c++11 a.cpp
objdump -CS a.out

The output contains demangled names:

int main() {
    add(1, 2);
 60c:   e8 28 00 00 00          callq  639 <int add<int>(int, int)>
    add(1.0, 2.0);
 62d:   e8 1b 00 00 00          callq  64d <double add<double>(double, double)>

0000000000000639 <int add<int>(int, int)>:

000000000000064d <double add<double>(double, double)>:

Without -C, it contains mangled names instead:

0000000000000639 <_Z3addIiET_S0_S0_>:
000000000000064d <_Z3addIdET_S0_S0_>:

man objdump says:

Decode (demangle) low-level symbol names into user-level names. Besides removing any initial underscore prepended by the system, this makes C++ function names readable. Different compilers have different mangling styles. The optional demangling style argument can be used to choose an appropriate demangling style for your compiler.

nm also has the -C option.

Tested in Ubuntu 18.04, g++ 7.3.0, objdump 2.30.


Pipe it through c++filt? Might need to give it -n depending on whether the symbols come w/ or w/o the leading underscore.

like image 45
smparkes Avatar answered Nov 19 '22 02:11

smparkes