9

I'm quite familiar with gcc assembly... Recently I was forced to use g++ for some code cleanup. Let me mention I'm very familiar with assembly, hence out of curiosity I often take a look at how good the compiler generated asm is.

But the naming conventions with g++ are just bizarre. I was wondering if there are any guidelines on how to read its asm output ?

Thanks a lot.

Llamageddon
  • 3,306
  • 4
  • 25
  • 44

5 Answers5

26

I don't find g++'s asm 'ugly' or hard to understand, though I've been working with GCC for over 8 years now.

On Linux, function labels usually go by _ZN, The "_ZN" prefix being a token that designates C++ name mangling (as opposed to C), followed by namespace the function belongs, then function names and argument types, then templates, if any.

Example:

    // tests::vec4::testEquality()
    _ZN5tests4vec412testEqualityEv

    _ZN - C++ mangling, 'N' for member (_ZZ for const or others)
    5tests - length (5 chars) + name
    4vec4 -length (4 chars) + sub namespace
    12testEquality - length (12 chars) + function name
    Ev - void argument (none)
LiraNuna
  • 64,916
  • 15
  • 117
  • 140
  • 2
    Based on your example, I have to agree with Sam, that's ugly! They call it name "Mangling" for a reason! Although, you did provide the best answer so far. – NoMoreZealots Jul 19 '09 at 21:58
  • 2
    I think "ID" in your example is the length of the next argument. _ZN, 5 "tests", 4 "vec4", 12 "testEquality", E, v. – strager Jul 19 '09 at 22:58
  • I think output of g++ generated assembly is ugly. it makes harder to understand. – Kavar Rushikesh Jan 01 '22 at 07:44
20

From man g++:

-fverbose-asm
Put extra commentary information in the generated assembly code to make it more readable. This option is generally only of use to those who actually need to read the generated assembly code (perhaps while debugging the compiler itself).

Eliseo Ocampos
  • 2,473
  • 4
  • 20
  • 32
  • 3
    Dislamer: not as 'verbose' as you think. – LiraNuna Jul 19 '09 at 20:57
  • LiraNuna is right. It doesn't make it more readable, to me at least. It doesn't demangle labels for you, which is what the OP seems to want. – strager Jul 19 '09 at 23:25
  • no, it's pretty. You are freed from guessing where variable , what you are searching for, is located in the stack - name of variables are superscribed from the left side. Much simpler – Tebe Aug 08 '12 at 18:38
14

If you're looking at the naming convention for external symbols then this will follow the name mangling convention of the platform that you are using. It can be reversed with the c++filt program which will give you the human readable version of C++ function names, although they will (in all probability) no longer be valid linker symbols.

If you're just looking at local function labels, then you're out of luck. g++'s assembler output is for talking to the assembler and not really designed for ease of human comprehension. It's going to generate a set of relatively meaningless labels.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
5

If the code has debugging information, objdump can provide a more helpful disassembly :

-S, --source             Intermix source code with disassembly
-l, --line-numbers             Include line numbers and filenames in output
Bastien Léonard
  • 60,478
  • 20
  • 78
  • 95
0

For people who are working on demangling those names inside the program (like me), hopefully this thread helps.

def demangle(name):
    import subprocess as sp
    stdout, _ = sp.Popen(['c++filt', name], 
                         stdin=sp.PIPE, stdout=sp.PIPE).communicate()
    return stdout.split("\n")[0]

print demangle('_ZNSt15basic_stringbufIcSt11char_traitsIcESaIcEE17_M_stringbuf_initESt13_Ios_Openmode')
Bojian Zheng
  • 2,167
  • 3
  • 13
  • 17
  • 1
    Is that function for use within GDB or something? GDB / objdump already have an option to demangle when disassembling: `objdump -C`. I usually use `objdump -drwC -Mintel`. But anyway, if you have g++-generated asm, I think you can just pipe it through `c++filt`. – Peter Cordes Feb 22 '19 at 04:43
  • @PeterCordes The function is written in Python because in my case I am writing a parser that needs to understand the function names of C++. – Bojian Zheng Feb 22 '19 at 16:51