Bug 40333

Summary: g++ does not align static variables properly
Product: gcc Reporter: GCC user <gccsse>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: normal CC: billingd, dannysmith, dave.korn.cygwin, gcc-bugs, gccsse, nickc, oysteijo, sherpya, zuxy.meng
Priority: P3    
Version: 4.3.2   
Target Milestone: ---   
Host: i686-pc-cygwin Target: i686-pc-cygwin
Build: i686-pc-cygwin Known to work:
Known to fail: Last reconfirmed:

Description GCC user 2009-06-03 21:22:32 UTC
The following SSE2 code crashes because the non-static global variable breaks the alignment of the static data section.

align.cpp:

#include <emmintrin.h>
int nonstatic_padding;
static int static_padding[3];
static __attribute__((aligned(16))) __m128i sse_data;
int main() {
    static_padding[0] = 1234;
    sse_data = _mm_set1_epi32(123);
    return 0;
}

The example uses SSE2 because that's how I discovered the bug and because it results in a nice crash, though I don't think it's essential at all.

Results:
- crashes (even when static_padding is removed)
- no longer crashes when removing nonstatic_padding (even when static_padding is present)
- no longer crashes when making sse_data non-static

Tested with:
g++-4 (GCC) 4.3.2 20080827 (beta) 2
and g++ (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)
(I know they're a bit old, but Cygwin doesn't provide more recent binaries)

If you rename the file to align.c gcc compiles it correctly and the program does not crash (gcc places static variables before non-static ones, unlike g++).
Comment 1 Andrew Pinski 2009-06-03 21:26:41 UTC

*** This bug has been marked as a duplicate of 37216 ***
Comment 2 roger pack 2009-08-24 04:38:38 UTC
(In reply to comment #0)
> The following SSE2 code crashes because the non-static global variable breaks
> the alignment of the static data section.

Is this fixed if you use 4.5.0?
Comment 3 Danny Smith 2009-08-24 06:15:39 UTC
(In reply to comment #2)
> (In reply to comment #0)
> > The following SSE2 code crashes because the non-static global variable breaks
> > the alignment of the static data section.
> 
> Is this fixed if you use 4.5.0?
> 
This is fixed with 4.5.0 and a recent binutils (Nick Clifton's patch to GAS was commuitted on 2008-09-03)
Danny