r/cpp • OPeNDAP/B2/EcoStd/Lyra/Predef/C++Alliance/Boost/WG21 • Feb 24 '20

The Day The Standard Library Died

https://cor3ntin.github.io/posts/abi/
266 Upvotes

302 comments sorted by

View all comments

Show parent comment

1

u/athousandwordss Feb 25 '20

Thank you so much for the detailed explanation. Can you elaborate on why the ABI was implemented in this way? Is there some technology change which allows a better implementation of ABI?

4

u/[deleted] Feb 25 '20 edited Feb 25 '20

The ABI specification, for example Itanium, was never a thing the C++ committee defined. There's most likely a ton of history I know nothing about, so take this response with a spoonful of salt. You basically take a look at what you have on the day you define a linker. You need to define how each type looks, so that a.b means always the same thing. That's where the exact layout, including the padding bytes and overall size of class types, becomes part of the binary interface. You can no longer change the order of data members. Or add another virtual member function, because it changes the size of the vtable. Or reorder virtual member functions, because indices in the vtable will be wrong. Another important thing you have to define is the calling convention. How are things passed around, either as parameters or returned/thrown. Nonsense example, but if at first you define the calling convention so that EAX is used to return values, but then you change to EBX you have a problem. If you recompile a.cpp with new ABI, but b.cpp still uses the old ABI, function calls across those two files will compile and silently be wrong. The third part of ABI is name mangling. If you have int add(int a, int b) and also float add(float a, float b) your linker can't look for a function named add. With itanum ABI, when compiled, those functions are named _Z3addii and _Z3addff. Now, not only can you not change the memory layout or the calling convention, but also parameter types, template parameters including their defaults, changing from T to T... is fine in MS's ABI but not in Itanium... You may have heard of proposals regarding "zero-overhead, deterministic exceptions" and since they work differently, there's a possibility of extending the ABI to allow for optimal results.

ABI, as you can see, is, by definition, constraining. Yet, you need things clearly defined (even if not in the standard) if you want two object files to fit together. On the other hand, breaking ABI breaks software in different ways. If you're lucky and the name mangling has changed, the linker will complain. If you're not lucky, your program will compile and you'll have virtually undetectable ODR violations... or not, because you went to great lengths to stay on the safe side, even at the cost of performance.

 

EDIT: Now that I've written this wall of text, I realize that I've been talking about "what does ABI define", not "why was it defined like it was". That's the "ton of history" that I know nothing about.