The name mangling schemes of C++ compilers vary, but they are documented publicly. Why aren't linkers made to decode a mangled symbol from an object file and attempt to find a mangled version via any of the mangling conventions across the other object files/static libraries? If linkers could do this, wouldn't it help alleviate the problem with C++ libraries that they have to be re-compiled by each compiler to have symbols that can be resolved?

3 Answers
3

Object layout is only defined in the C++ standard for a very restricted set of classes (essentially only standard layout types - and then only as much as the C standard does, alignment and padding are still to be considered). For anything that has virtuals, any form of non-trivial inheritance, mixed public and private members, etc. the standard doesn't say how they should be layed out in memory.

Two compilers can (and this is not purely hypothetical, this does happen in practice) return different values for sizeof(std::string) for instance. There is nothing in the standard that says: an std::string is represented like this in memory. So interoperability at the object file level doesn't exist.

Binary compatibility between C++ compilers is a much larger problem than just name mangling. You'd need to standardize much more than what is currently specified.

The name mangling scheme is only a tiny part of the ABI. Calling conventions, structure padding, vtable contents, sizes and internal layout of non-POD classes, etc. also vary among compilers. Getting past the differences in name mangling doesn't help with that, it just means silencing a link-time diagnostic for a condition (ABI mismatch) that's bound to lead to silent run-time errors (object slicing, bogus parameters, overwriting unrelated data, disagreeing which pieces of an object are to be used for what purpose, and many more).

I think name mangling mismatch is good as having an uniform name mangling or auto detection name mangler/mangler could create havoc by linking objects of different kind which would otherwise be incomparable in regards to calling convention, structure padding and alignment, and others as mentioned in the previous two questions.

Unfortunately, these are all run-time mismatch and a linker could seldom handle these scenarios. Thus thankfully, because of name mangling mismatch, linkers can refrain from linking objects of different ABI.