User account creation filtered due to spam.

Bug 41475 - common variables cannot be expected to be aligned
Summary: common variables cannot be expected to be aligned
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.4.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2009-09-26 04:04 UTC by mikulas
Modified: 2009-09-27 09:51 UTC (History)
2 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:

the first file (173 bytes, text/plain)
2009-09-26 04:05 UTC, mikulas
the second file (77 bytes, text/plain)
2009-09-26 04:06 UTC, mikulas

Note You need to log in before you can comment on or make changes to this bug.
Description mikulas 2009-09-26 04:04:08 UTC

When I compile these two sources, one with -O3 -march=pentium3 and the other with -Os, the linker warns about nonmatching alignments and the program crashes because of misaligned SSE accesses.

/usr/bin/ld: Warning: alignment 4 of symbol `array' in commonalign2.o is smaller than 32 in commonalign1.o
/usr/bin/ld: Warning: alignment 4 of symbol `array2' in commonalign2.o is smaller than 32 in commonalign1.o

The reson is that gcc aligns arrays in common section to 32 bytes and expects that they will be aligned so. This expectation is wrong, the common entries may be resolved pointing to data section in another module that doesn't meet the alignment (it may be compiled with different compiler or with the same compiler with different flags (-Os)).

For extern arrays, gcc correctly assumes that they are aligned to their ABI standard (4 bytes) and generates appropriate SSE code; for common arrays it should expect 4-byte alignment too.
Comment 1 mikulas 2009-09-26 04:05:31 UTC
Created attachment 18653 [details]
the first file

The first file of a two-file program. Compile with gcc -c -O3 -march=pentium3.
Comment 2 mikulas 2009-09-26 04:06:46 UTC
Created attachment 18654 [details]
the second file

The second file. Compile with gcc -c -Os. Then, link both object files together and run it.
Comment 3 mikulas 2009-09-26 04:14:23 UTC
BTW. this bug is in both gcc 4.3 and 4.4
Comment 4 Richard Biener 2009-09-26 11:23:56 UTC
Isn't this a linker issue in that it doesn't use the biggest alignment?
Comment 5 H.J. Lu 2009-09-26 14:14:44 UTC
(In reply to comment #4)
> Isn't this a linker issue in that it doesn't use the biggest alignment?

Linker can't change alignment on non-common symbols. I think it is wrong
to give a different alignment for the same symbol in different files.
Comment 6 Richard Biener 2009-09-26 14:37:57 UTC
The bug is about common symbols.
Comment 7 mikulas 2009-09-26 15:27:10 UTC
Richard Guenther: the bug caused by common symbol (in file commonalign1.o) with the same name as data section symbol (in file commonalign2.o). In this case, the linker redirects the common symbol to the symbol in the data section. But the linker cannot change the content of the data section, so it cannot make both symbols array and array2 aligned. Linker is innocent in this and giving a warning is the best thing it can do.
Comment 8 Andrew Pinski 2009-09-26 19:29:53 UTC
This code is undefined I think (and really it is not valid C90/C99 code).
Comment 9 mikulas 2009-09-27 09:51:35 UTC
The common linker definitions were made to exactly to make code like this work and share the array between two object.

So if you think it is undefined, don't support it (make -fno-common default and remove -fcommon at all). Or, if you want to support it, support it correctly.