4

Are gcc and clang designed to produce the same memory layout for a given struct definition?

Obviously the precise memory layout of structs isn't mandated by the C standard, but gcc and clang might still produce the same memory layout for other reasons. Maybe clang was explicitly designed to do so to be compatible with gcc. Maybe there's some other standard that both follow, similar to the situation with name-mangling and the Itanium ABI.

Praxeolitic
  • 22,455
  • 16
  • 75
  • 126

3 Answers3

1

There is one fundamentally incompatible case: on Windows, since MinGW(GCC) and MSVC are incompatible, clang can only be compatible with one of them at a time (although with a lot of work, it is possible to make them communicate).

o11c
  • 15,265
  • 4
  • 50
  • 75
  • Interesting. Source? – Praxeolitic Aug 04 '17 at 22:22
  • Mostly it's "common knowledge" among compiler nerds ... there is https://clang.llvm.org/docs/UsersManual.html#operating-system-features-and-limitations and https://clang.llvm.org/doxygen/ToolChains_8h_source.html (separate classes) – o11c Aug 05 '17 at 22:58
  • For general info and workarounds, see e.g. http://www.mingw.org/wiki/Interoperability_of_Libraries_Created_by_Different_Compiler_Brands – o11c Aug 05 '17 at 23:00
0

Yes, you can rely on Clang and GCC producing the same layout for identical structures. I can't find which layer of the platform stack defines struct layout right now, but it's a stated goal of Clang on Linux to link against libraries compiled with GCC.

You can refer to the list of incompatibilities between Clang and GCC.

zneak
  • 134,922
  • 42
  • 253
  • 328
  • While I do think 'yes' is the answer, I don't know if different struct layouts would be considered an incompatibility. (Huh... just noticed I used 'compatible' in my question...) It looks like that list is for incompatibilities in what clang and gcc will accept and compile. – Praxeolitic Aug 04 '17 at 17:32
  • This answer would be better if it cited clang documentation to support it. The list of incompatibilities that is linked seems to be about *language* compatibility, not binary compatibility, and inasmuch as structure layout is implementation-dependent, it is a binary compatibility concern. – John Bollinger Aug 04 '17 at 17:43
  • 1
    Actually, if we look at https://github.com/hjl-tools/x86-psABI/wiki/x86-64-psABI-r252.pdf page 15, it does mandate the alignment and padding, but does not mandate the content of the padding. – Eugene Sh. Aug 04 '17 at 17:46
  • @JohnBollinger, the intent comes up once in a while on the mailing list, but I don't know where it's been formally captured. Here's [one time it came up](http://lists.llvm.org/pipermail/cfe-dev/2015-April/042357.html) that I could find. Clang aims to be ABI-compatible with GCC, even with C++. – zneak Aug 04 '17 at 17:49
  • Ok, @EugeneSh., that one is new to me, but I'm not sure what its scope or significance is. Can you elaborate? – John Bollinger Aug 04 '17 at 17:49
  • @JohnBollinger It's a specific ABI doc as it is :) Not sure how much I can elaborate here. – Eugene Sh. Aug 04 '17 at 17:52
  • @EugeneSh., I mean what authority produced and maintains it? Who recognizes its authority? I see the authors and their affiliations, but I can't tell whether the document describes *an* ABI or *the-one-and-only* ABI. – John Bollinger Aug 04 '17 at 18:03
  • @JohnBollinger I can only throw links at you :) https://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI – Eugene Sh. Aug 04 '17 at 18:20
  • Thanks, @EugeneSh., that's sufficient for me. – John Bollinger Aug 04 '17 at 18:35
  • @EugeneSh. The part of the System V ABI that you pointed out mostly answers the question. It should be added as an answer or one of the existing answers should add a direct quotation of it. – Praxeolitic Aug 04 '17 at 18:59
  • @Praxeolitic Compatibility is defined as an ability to mix and match objects compiled with the two compilers, so yes, different layouts would naturally result in incompatibility. – n. m. could be an AI Aug 04 '17 at 19:59
0

For the simple cases (no bitfields, no passing of structs by value or returning them, no C++) and the same architecture, the layout is the same. For the complex matters (bitfields, calling conventions), there are some differences, but they are not greater than those between different versions of GCC, and both compilers try to align their respective ABIs.

While the C and C++ standards do not define an ABI, there are separat ABI standards for those languages, such as the System V psABI and its architecture supplements, and the Itanium C++ ABI (which is just a historical name, it's the default Clang/GCC C++ ABI).

Florian Weimer
  • 32,022
  • 3
  • 48
  • 92