21

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.

Jason R
  • 10,110
  • 5
  • 44
  • 72

2 Answers2

25

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.

20

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

smparkes
  • 13,460
  • 4
  • 34
  • 62
  • 5
    Looks like that would work. I also just found the `-C` option for `objdump`, which seems to work for my application at least. – Jason R Feb 23 '12 at 18:51
  • @JasonR even better. Historically there have been options and variant names like `nm++` but they were hit and miss and I got into the habit of just piping everything. – smparkes Feb 23 '12 at 18:57
  • 1
    @JasonR, thanks for posting this... `-C` was undocumented in my version of `objdump` (not shown with `--help`) but it still worked. – Benoit Miller Nov 25 '13 at 14:51