Page 1 of 1

Homebrew demangler

Posted: Sat Mar 01, 2008 8:09 am
by JamesM
I thought as I'd spent an entire week working on this and produced 1240 lines of code that I'm quite proud of, that I'd post it here.

I needed a C++ symbol name demangler for my (our) OS project. Obviously because we're not hosted we can't use c++filt or cxx::abi::demangle so I had to make my own.

My first attempt was based on me playing with c++filt and working out by hand how the symbols were created, and worked pretty well. But not well enough for me! I found the mangling spec that GCC uses (the Itanium IA64 ABI), which included a grammar and a brief explanation of the substitution/compression techniques used (the most difficult bit!). So after much typing, debugging, and removal of left recursion (damn them, I'm not implementing a bottom-up parser by myself, and I don't want the bloat that bison gives in a core kernel file (plus I would then be bound by the GPL - eek!), I came up with this:

.cc file
.h file

It can be run under linux (possibly windows too but I haven't tried) - just comment out the #includes there and uncomment the #includes / #defines below (and the main() function).

If someone does fancy using it under linux (not sure why, but meh just in case) you'll also need the StaticString header file:

StaticString.h

I hope that it brings joy to some child, somewhere.

Cheers,

James

EDIT: forgot to mention that if you change the "#undef MANGLE_DEBUG" to "#define MANGLE_DEBUG" you get some rather nice debug output :D

EDIT2: It working:

Code: Select all

[13:31:29] ~/pedigree $ ./testmangle _ZN12StaticStringILj32EEpLERKi
_ZN12StaticStringILj32EEpLERKi -> StaticString<unsigned int=32>::operator+=(const int&)
Substitutions:
         S_:  StaticString
         S0_: StaticString<unsigned int=32>
         S1_: const int
         S2_: const int&
Template params:
[13:32:03] ~/pedigree $

Posted: Sun Mar 02, 2008 7:52 am
by AJ
Hi,

I was just thinking about this recently myself and was planning to wait until I could port some GCC utils. Are you happy with it being distributed compiled as part of an OS? Just wanted to be sure that the notice at the top of the header doesn't just relate to the source.

I'm just going to start playing with this new toy now!

Cheers,
Adam

Posted: Sun Mar 02, 2008 10:13 am
by JamesM
Hi AJ,

Glad it can be of use to you!

That was an error on my part, the .cc file should have the same licence as the header. Standard BSD "do-what-you-want-as-long-as-you-keep-this-licence" licence, belonging to myself and all other Pedigree contributors.

Cheers,

James

EDIT: Do tell me if you manage to throw anything at it which makes it choke. I haven't as yet, but I know that if you template a function on a member function pointer which returns a function pointer as a result, then the member bit (A::*) will appear in the wrong place. But if that happens, you've got less to worry about than proper demangling... :S

Posted: Mon Mar 03, 2008 2:13 pm
by AJ
Do tell me if you manage to throw anything at it which makes it choke.
Will do. I currently use an elf symbol table to display the function where an exception occurred - I'm currently just intending to use the code to display de-mangled function names. I'll let you know how it goes.

Cheers,
Adam

Posted: Mon Mar 03, 2008 2:49 pm
by lukem95
That looks very useful, i'm gonna store that for later use if thats ok.

i really wouldn't know where to start on a project like that one :|

Posted: Mon Mar 03, 2008 4:17 pm
by JamesM
AJ wrote:
Do tell me if you manage to throw anything at it which makes it choke.
Will do. I currently use an elf symbol table to display the function where an exception occurred - I'm currently just intending to use the code to display de-mangled function names. I'll let you know how it goes.

Cheers,
Adam
Hi AJ - I updated the file today, a small change which fixes an issue I found (choking on a template argument list after a substitute parameter). Small diff, but worth having I feel!

lukem: Go for it, but please maintain the copyright notices.

Posted: Mon Mar 03, 2008 4:24 pm
by lukem95
of course, id hate it if i spent a week on some code, and had it ripped away from me for being nice enough to share it.