none
How Do I View Mangled Function Names?

    Question

  • As the title says.  How do I view mangled C++ function names?  I want to pass it into a demangler function so that I can use it in Emscripten-generated JS code.  
    Thursday, May 23, 2019 5:16 PM

All replies

  • The tool that does this for Visual C++ is called "undname" (short for "undecorate name").  It's in the same folder as the command-line compiler.

    I'm not aware of any documented function to do this.  Looking at the imports for "undname", it looks like it calls a function __unDNameEx in the runtime library, but that's undocumented.  There's also UnDecorateSymbolName in dbghelp.dll, but it doesn't get every name.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Thursday, May 23, 2019 5:26 PM
  • Is there no tool I can use to see a mapping file or something that shows me the mangled names used by the compiler?  I'm not asking for a function, just a tool that would let me see the mangled names in a list.  
    Thursday, May 23, 2019 6:00 PM
  • If you really want to do this, the way to do it would be to use dumpbin on the object file that you compiled. Something like:

    dumpbin /symbols main.obj

    This will then show you the symbol table for the object. Be aware that not all of the symbols in there are mangled, but the ones that are have the unmangled version in parenthesis after it:

    2F1 00000000 SECTCE notype ()    External     | ?initialise@graphics@1@QEAA_NPEAUHWND__@@@Z (public: bool __cdecl graphics::graphics::initialise(struct HWND__ *))

    or

    331 00000000 SECT52 notype ()    External     | ??0windows_exception@winhelper@@QEAA@AEBV01@@Z (public: __cdecl winhelper::windows_exception::windows_exception(class winhelper::windows_exception const &))

    As an example.

    As far as I know, there is no way to take a function and get the mangled name for it. The compiler has to compile the code before you know this.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Thursday, May 23, 2019 6:15 PM
  • Is there no tool I can use to see a mapping file or something that shows me the mangled names used by the compiler?  I'm not asking for a function, just a tool that would let me see the mangled names in a list.  

    Dependency Walker (depends.exe) will show the mangled and unmangled names for functions. 

    http://dependencywalker.com/

    Undecorate C++ Functions Command (View Menu)

    "Use this command to toggle the Undecorate C++ Functions option on or
    off. When this option is on, a check mark appears next to the Undecorate
    C++ Functions menu item and the Undecorate C++ Functions toolbar button
    is displayed as depressed.

    This option requires that you have IMAGEHLP.DLL on your system. If this
    DLL is not found, then the Undecorate C++ Functions option will be
    disabled. IMAGEHLP.DLL is installed with Windows NT/2000/XP/2003/Vista
    and Windows 95 OSR2 and beyond.

    When the Undecorate C++ Functions option is on, both the Parent Import
    Function List View and the Export Function List View will undecorate C++
    functions into human readable function prototypes containing parameter
    and return types. When the Undecorate C++ Functions option is off, these
    views will show C++ functions in their true decorated form. Dependency
    Walker can only undecorate functions that use the Microsoft decoration
    rules.

    This option also effects how the Copy Command and Save Command work.
    When the Undecorate C++ Functions option is on, the Copy Command will
    copy the undecorated names for C++ functions to the clipboard, otherwise
    it just copies the true decorated names. For the Save Command, text
    files will contain undecorated names for C++ functions when the
    Undecorate C++ Functions option is on and the true decorated names when
    it is off."

    Unfortunately it hasn't been updated in many, many years. A possible replacement 
    is Dependencies:

    https://github.com/lucasg/Dependencies

    However I haven't checked to see if it offers the same handling of mangled names
    as Depends.exe does.

    - Wayne

    Thursday, May 23, 2019 8:13 PM

  •  I haven't checked to see if it offers the same handling of mangled names
    as Depends.exe does.

    I just did a cursory check of Dependencies and I see that it does offer the
    display of mangled/unmangled names of functions. However copying these names
    to the clipboard appears to be limited to one at a time.

    - Wayne

    Thursday, May 23, 2019 8:38 PM
  • What are you after?  Do you just want to access the entry points being exported by a DLL?  You can use "dumpbin /exports" for that.  I don't know what good having non-exported symbols would do you; you can't call those.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Friday, May 24, 2019 7:12 AM
  • That's not it.  I already said this:

    "As the title says.  How do I view mangled C++ function names?  I want to pass it into a demangler function so that I can use it in Emscripten-generated JS code.  "

    Undname or Dependency Walker might help.

    Friday, May 24, 2019 11:18 AM
  • As I said, the way you would get a mangled C++ name is to take a function, compile it using the compiler and then using something like dumpbin to view it in the object file.

    If you don't like this then you could always write an application that gets a function to report its own name using the __FUNCDNAME__ macro.

    #include <cstdio>
    
    int cppfun()
    {
    	wprintf(L"%s\n", _CRT_WIDE(__FUNCDNAME__));
    
    	return 0;
    }
    
    int wmain()
    {
    	wprintf(L"%s\n", _CRT_WIDE(__FUNCDNAME__));
    
    	cppfun();
    
    	return 0;
    }

    wmain
    ?cppfun@@YAHXZ

    But you need to involve the compiler in order to have it decorate the name somehow. The name mangling is implementation defined and Microsoft don't have the name mangling available as a specification or as a library.

    Although thinking about it, if you can have the debug symbols available, you may be able to read it from the .pdb (code view) files. The DIA library helps you read this information. There is also the option of reading the file directly. The source files reading these are available on GitHub. So maybe that will be of some use. But these require you use /Zi or /ZI in the compiler's command prompt and /DEBUG in the linker's command prompt.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Friday, May 24, 2019 11:08 PM