[LLVMdev] clang change function name

Nick Lewycky nicholas at mxc.ca
Sun Mar 8 13:09:54 PDT 2015


Haopeng Liu wrote:
> Thanks for all your help.
>
> "__cxa_demangle" can decode the mangled function name as expected.
>
> Another question is that given an unmangled function name, how to get
> the corresponding mangled name in llvm?

The unmangled function name is insufficient. Computing the mangled name 
requires the C++ context inside of which the function is defined (any 
namespaces, structs, etc.) as well as all the arguments. See the 
clang::MangleContext in clang, 
http://clang.llvm.org/doxygen/classclang_1_1MangleContext.html and the 
ABI document which defines this stuff in the first place: 
http://mentorembedded.github.io/cxx-abi/abi.html#mangling .

If what you have is guaranteed to be a plain function (not a template, 
constructor, operator, etc.) defined in the top-level (not inside any 
namespaces, structs, etc.) and the arguments are all going to be builtin 
types (int, char, etc.) then you can simplify the problem to:

   _Z <length of function name> <function name> <arguments...>

where arguments are b for bool, c for char, i for int, j for unsigned 
int, l for long, m for unsigned long. Prefix with 'P' to indicate a
"pointer to". For example, _Z3foojPv is "foo(unsigned int, void*)".

Nick

> On 3/2/15 3:22 AM, mats petersson wrote:
>> Depending on what you want to achieve, one possibility is wrapping
>> function declaration with
>>
>> extern "C" void somefunction(void);
>>
>> or
>>
>> extern "C" {
>> void somefunction();
>> int otherfunction();
>> }
>>
>> will make those functions have their name unmangled - this is used
>> when interfacing between C and C++ functions, since the C compiler
>> will NOT name-mangle.
>>
>> This does however also affect some other aspects of the code e.g.
>> linker can't check parameter passing and you can't have more than one
>> function with the same name, with different function argument types -
>> like you can in C++ [in fact these are the two main reasons for using
>> name-mangling]. I can't remember if it also affects the ability for
>> example to handle exceptions from C++.
>>
>> --
>> Mats
>>
>> On 2 March 2015 at 09:03, serge guelton <sguelton at quarkslab.com> wrote:
>>> On Mon, Mar 02, 2015 at 12:12:34AM -0500, John Criswell wrote:
>>>> On 3/2/15 12:07 AM, Haopeng Liu wrote:
>>>>> Got it, thanks. But in my pass, I use function name to locate. Can I
>>>>> disable mangling in clang?
>>>> No, but you can probably fine a library that can either mangle the
>>>> original
>>>> name or demangle the name you're seeing in the LLVM bitcode.
>>>>
>>>> As an FYI, on Unix, the c++filt program will demangle names (although
>>>> sometimes you have to remove an extra '_' from the front of the name
>>>> to get
>>>> it to work).
>>> there's also a __cxa_demangle function in
>>>
>>> http://llvm.org/svn/llvm-project/libcxxabi/trunk/include/cxxabi.h
>>> _______________________________________________
>>> LLVM Developers mailing list
>>> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu
>>> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>




More information about the llvm-dev mailing list