Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use custom mangling in g++/clang++?

When using c++ template, and especially tuples, I often get very long mangled names like

_ZN11__sanitizer13InternalAllocEmPNS_28SizeClassAllocatorLocalCacheINS_20SizeClassAllocator32ILm0ELy140737488355328ELm0ENS_12SizeClassMapILm3ELm4ELm8ELm17ELm64ELm14EEELm20ENS_15TwoLevelByteMapILy32768ELy4096ENS_20NoOpMapUnmapCallbackEEES5_EEEEm

This is obviously fine in nominal case, and ease debugging but, when I use large tuples of custom types (with large namespaces) this ends up with very huge binaries just because of mangled names; Some tools (like valgrind) even have a symbol name maximum length that make analysis almost impossible.

I was wondering if there is a way to ask g++/clang++ to use a custom mangling function (say md5) so that the very long symbol becomes 5c66b1073e1b453900bd7d32cb79fc0e which is way shorter.

like image 342
OznOg Avatar asked Feb 05 '19 21:02

OznOg


People also ask

Does Clang work with MSVC?

MSVC compatibility¶. When Clang compiles C++ code for Windows, it attempts to be compatible with MSVC. There are multiple dimensions to compatibility. First, Clang attempts to be ABI-compatible, meaning that Clang-compiled code should be able to link against MSVC-compiled code successfully.

What is Clang compiler?

The Clang Compiler is an open-source compiler for the C family of programming languages, aiming to be the best in class implementation of these languages. Clang builds on the LLVM optimizer and code generator, allowing it to provide high-quality optimization and code generation support for many targets.

What is the difference between GCC mangled name and Clang name?

Codes were as simple as the one shown below. The first name in the comment is the GCC mangled name (g++ 8.3) and the second name is the Clang mangled name (clang++ 9.0). So, at least given this small set of samples, there do not seem to be differences.

What is Clang-Cl and what is it used for?

Clang also provides an alternative driver, clang-cl, that is designed to be compatible with the Visual C++ compiler, cl.exe. In addition to language specific features, Clang has a variety of features that depend on what CPU architecture or operating system is being compiled for.


1 Answers

The absence of this innovation in any important C++ implementation is accounted for by the fact that a C++ compiler's name-mangling protocol is part of its ABI and can't be the user's choice at the same time.

Notoriously, C++ code compiled with GCC is not interoperable with code compiled with Microsoft compilers because, inter alia, they use different name-mangling protocols. And the fact that they do use different name-mangling protocols, and cannot be directed to use the same one, guarantees that subtler ABI incompatibilities will not survive an attempted linkage.

The constancy of the C++ compiler's name-mangling protocol is also assumed in other toolchain utilities, e.g. binutils nm, objdump.

like image 73
Mike Kinghan Avatar answered Oct 06 '22 01:10

Mike Kinghan