Homebrew demangler

Programming, for all ages and all languages.
Post Reply
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Homebrew demangler

Post 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 $
Last edited by JamesM on Mon Mar 03, 2008 10:07 am, edited 1 time in total.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post 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
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post 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
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post 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
User avatar
lukem95
Member
Member
Posts: 536
Joined: Fri Aug 03, 2007 6:03 am
Location: Cambridge, UK

Post 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 :|
~ Lukem95 [ Cake ]
Release: 0.08b
Image
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post 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.
User avatar
lukem95
Member
Member
Posts: 536
Joined: Fri Aug 03, 2007 6:03 am
Location: Cambridge, UK

Post 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.
~ Lukem95 [ Cake ]
Release: 0.08b
Image
Post Reply