Function to mangle/demangle functions
Use the c++filt
command line tool to demangle the name.
Is there a simple way to have ld output demangled funtion names?
ld
(The GNU Linker) is able to demangle C++ function names. ld
documentation on demangling from it's man
page: (available here online)
--demangle[=style]
--no-demangle
These options control whether to demangle symbol names in error
messages and other output. When the linker is told to demangle,
it tries to present symbol names in a readable fashion: it strips
leading underscores if they are used by the object file format,
and converts C++ mangled symbol names into user readable names.
Different compilers have different mangling styles. The optional
demangling style argument can be used to choose an appropriate
demangling style for your compiler. The linker will demangle by
default unless the environment variable COLLECT_NO_DEMANGLE is
set. These options may be used to override the default.
Let's see an example:
void foo();
void foo(int);
int main() {
foo();
foo(5);
}
This is a simple valid code. This will compile but fail to link successfully because there is no implementation of foo()
and foo(int)
here. Now we'll compile it with the following command:
g++ main.cpp -c -o main.o
It will compile successfully. Now let's try to link it with demangling disabled with the following command:
g++ main.o -Wl,--no-demangle
It should show linking errors with some weird mangled name like this:
main.o: In function `main':
main.cpp:(.text+0x5): undefined reference to `_Z3foov'
main.cpp:(.text+0xf): undefined reference to `_Z3fooi'
collect2: error: ld returned 1 exit status
See live on Coliru
Now let's try to link with demangling enabled with the following command:
g++ main.o -Wl,--demangle
We'll get errors with demangled function names with their arguments like this:
main.o: In function `main':
main.cpp:(.text+0x5): undefined reference to `foo()'
main.cpp:(.text+0xf): undefined reference to `foo(int)'
collect2: error: ld returned 1 exit status
See live on Coliru
Here -Wl
means arguments to linker.
As far as I know, g++
enables demangling automatically.
Can objdump un-mangle names of C++ template functions?
Pipe it through c++filt
? Might need to give it -n
depending on whether the symbols come w/ or w/o the leading underscore.
Getting mangled name from demangled name
You can simply use g++ to compile an empty function with the signature you require and extract the name from that. For example:
echo "int f1(char *, int) {} " | g++ -x c++ -S - -o- | grep "^_.*:$" | sed -e 's/:$//'
gives output
_Z2f1Pci
which is I think what you require. Make sure that you include any relevant header files as they will affect the way the symbols are mangled.
Swift mangled function name mapping
I found this Mach-O file format reference:
https://github.com/aidansteele/osx-abi-macho-file-format-reference
So the answer to my question is that there is a special struct called nlist_64, which contains the address of the function in the executable and the index of the mangled name of that function in the symbol table.
Related Topics
How to Declare Variables of Different Types in the Initialization of a for Loop
How Is Heap and Stack Memories Managed, Implemented, Allocated
Catching "Stack Overflow" Exceptions in Recursive C++ Functions
How to Check If Window Is "Always on Top"
Why How to Call Base Template Class Method from Derived Class
High Delay in Rs232 Communication on a Pxa270
Should I Use Wchar_T When Using Utf-8
What C++ Pitfalls Should I Avoid
How to Call on a Function Found on Another File
C++ Gdb Python Pretty Printing Tutorial
How to Read from a Text File, Character by Character in C++
Write and Read String to Binary File C++
What Is the Meaning of Auto When Using C++ Trailing Return Type
What Does "Void *(*)(Void *)" Mean in C++
Getting Big Random Numbers in C/C++
How to Compare Two Time Stamp in Format "Month Date Hh:Mm:Ss" to Check +Ve or -Ve Value