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
  • 19,888
  • 14
  • 67
  • 116
  • 2
    Hm. Would "No" qualify as an answer? – Eugene Sh. Aug 04 '17 at 17:20
  • 2
    Yes. If the answer is no then a simple counter-example would do. (Even better would be a general description of when they do not, but that's just for extra credit.) – Praxeolitic Aug 04 '17 at 17:20
  • 1
    Pretty sure this is part of the ABI; otherwise code produced by different compilers couldn't pass structs back and forth. – melpomene Aug 04 '17 at 17:25
  • @melpomene It can't be it as many compilers have an option to pack structures. – Eugene Sh. Aug 04 '17 at 17:27
  • @melpomene afaik it's a de facto ABI – bolov Aug 04 '17 at 17:27
  • @melpomene Yeah, it seems so but I scanned the table of contents and 'ctrl-f'ed through the System V ABI and couldn't find a specification for struct layouts. – Praxeolitic Aug 04 '17 at 17:27
  • 2
    @EugeneSh. Non sequitur. – melpomene Aug 04 '17 at 17:28
  • [Strongly related](https://stackoverflow.com/questions/19804655/are-c-structs-with-the-same-members-types-guaranteed-to-have-the-same-layout-in), though not an exact duplicate since you want to narrow down to GCC and Clang. – Cody Gray Aug 04 '17 at 20:55

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
  • 14,462
  • 4
  • 48
  • 70
  • 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
  • 130,082
  • 41
  • 248
  • 315
  • 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. 1.8e9-where's-my-share m. 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
  • 29,521
  • 3
  • 37
  • 79