Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ name mangling by hand

I am writing a script for the IDA Pro disassembler in Python using the idapython plugin. Using this, I am able to fill in the gaps where IDA's auto-analysis falls short.

One area that has me stumped is naming locations/functions with (for want of a better term) "pretty names". An example of what I mean is illustrated below:

IDA pretty names sample screenshot

idapython and IDA Pro itself only allow me to enter basic C-ish function names. If I enter disallowed symbols (e.g. the scope resolution operator), they're replaced with underscores. However, if I enter a mangled name by hand (e.g. __ZN9IOService15powerChangeDoneEm), IDA Pro will prettify this for me.

Hence my question: how can I generate mangled names to pass through idapython? Is there a name-mangling library available? Is one available in Python? Is my only hope to tear the mangling functionality out of g++ and work around that?

like image 441
Sedate Alien Avatar asked Jan 12 '11 09:01

Sedate Alien


People also ask

Does C have name mangling?

Since C is a programming language that does not support name function overloading, it does no name mangling.

What is name mangling technique?

Name mangling is the encoding of function and variable names into unique names so that linkers can separate common names in the language. Type names may also be mangled. Name mangling is commonly used to facilitate the overloading feature and visibility within different scopes.

Is name mangling consistent?

A particular version of a compiler will mangle names consistently, otherwise it wouldn't be able to link with things it produced. Other than that, all bets are off.

What is name mangling in Java?

Name mangling is a term that denotes the process of mapping a name that is valid in a particular programming language to a name that is valid in the CORBA Interface Definition Language (IDL).


3 Answers

I finally got around to dig a little.

Unfortunately I could not find tool, I did find resources though.

If all you want is mangle names in the gcc3 fashion, then know that gcc3 use the Itanium C++ ABI, which has a standardized name mangling scheme. I found two documents:

  • The Itanium C++ ABI page
  • A Calling Convention PDF document, which describe the schemes of various compilers

For reference, both come from the Wikipedia page on Name Mangling.

like image 188
Matthieu M. Avatar answered Oct 05 '22 06:10

Matthieu M.


One simple (alebit hacky) method would be to compile a C++ file with the symbol you want in it, then extract the necessary symbols from the .o file's symbol table. With a bit of work this might be nicely scriptable.

like image 42
bdonlan Avatar answered Oct 05 '22 06:10

bdonlan


Here is an article that explains how mangling is done by Visual compiler. For mangling done by gcc, I think you can find the information in the source of the binutils package.

like image 39
Sylvain Defresne Avatar answered Oct 05 '22 05:10

Sylvain Defresne