Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is name mangling not standardized

I'm just wondering why name mangling was never standardized by the C++ standard. Clearly having different name mangling algorithms hurts interoperability[1] and I don't see any advantage of having it implementation-defined.

That is, contrary to say calling conventions or size of primitives the machine itself doesn't care or even know how the function is called. So why wasn't it standardized and why is it still not standardized? Compilers have changed the rules in the past anyhow between versions.

[1] all those people exporting functions as extern "C" speak volumes.

like image 449
Voo Avatar asked Apr 12 '12 16:04

Voo


People also ask

What is the point of name mangling?

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 C++ name mangling standard?

The C++ standard therefore does not attempt to standardize name mangling.

What is name mangling in Python?

The name mangling process helps to access the class variables from outside the class. The class variables can be accessed by adding _classname to it. The name mangling is closest to private not exactly private.

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).


2 Answers

The standard does not address implementation details. There are many, many things which depend on the implementation, and which prevent programs from working together: how the classes are laid out, the structure of the vtable, etc. In general, compilers will change the name mangling if they change any of these. This is intentional, as it prevents code which would not work from linking.

It is possible for a given platform to define a C++ ABI; all compilers which adhere to it would use compatible implementations, and have a common name mangling. This is an issue for the platform vendors, however; for whatever reasons, very few vendors have defined a C++ ABI.

And the reason extern "C" works is because almost all platforms define a C ABI.

like image 92
James Kanze Avatar answered Sep 21 '22 03:09

James Kanze


The Standard doesn't actually require name mangling. For that matter, the Standard doesn't require IEEE floating point numbers, or any number of other things.

Until there was a widespread ABI it could rely on, GCC actually went out of its way to use a different name mangling scheme than its competitors:

G++ does not do name mangling in the same way as other C++ compilers. This means that object files compiled with one compiler cannot be used with another.

This effect is intentional, to protect you from more subtle problems. Compilers differ as to many internal details of C++ implementation, including: how class instances are laid out, how multiple inheritance is implemented, and how virtual function calls are handled. If the name encoding were made the same, your programs would link against libraries provided from other compilers--but the programs would then crash when run. Incompatible libraries are then detected at link time, rather than at run time.

Name mangling is also more complex than many programmers realize. For instance how would the Standard specify acceptable calling conventions for all platforms that C++ could run on? Should a RISC system be required to support x86 stdcall even though RISC systems usually pass their arguments in registers instead of on the stack?

like image 32
Max Lybbert Avatar answered Sep 21 '22 03:09

Max Lybbert