[Bug c/82914] 'struct __attribute__ ((aligned (N))) s' ignores 'aligned' attribute
rguenth at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Thu Nov 9 08:32:00 GMT 2017
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82914
Richard Biener <rguenth at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution|--- |INVALID
--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
(In reply to Paul Eggert from comment #1)
> (In reply to Paul Eggert from comment #0)
>
> Sorry, but my example in comment #0 (although it illustrates a bug) doesn't
> illustrate the bug that crashed GCC. Here's a better example:
>
> struct t { long mem; };
> __attribute__ ((aligned (2))) struct t a;
> struct __attribute__ ((aligned (2))) t b;
> struct t __attribute__ ((aligned (2))) c;
> struct t d __attribute__ ((aligned (2)));
>
> This compiles into:
>
> .comm a,8,2
> .comm b,8,8
> .comm c,8,2
> .comm d,8,2
>
> Here, only 'b' is aligned correctly. The variables a, c, and d have an
> alignment of only 2, but they should have an alignment of 8 because
> __attribute__ ((aligned (8))) is documented to never decrease the alignment
> of a structure, only to increase it. The GCC 7.2 documentation
> <https://gcc.gnu.org/onlinedocs/gcc-7.2.0/gcc/Common-Variable-Attributes.
> html> says, "When used on a struct, or struct member, the 'aligned'
> attribute can only increase the alignment; in order to decrease it, the
> 'packed' attribute must be specified as well."
I think this applies to types but not to variables. IIRC there's no packed
attribute for variables:
int a __attribute__((aligned(2),packed));
> gcc-7 -S t.c
t.c:1:1: warning: ‘packed’ attribute ignored [-Wattributes]
int a __attribute__((aligned(2),packed));
^~~
so yes, for type definitions you should need packed to decrease alignment
but for variable declarations aligned is taken literally.
You are not using aligned on a 'struct or struct member' but on the variable
in all but (b).
More information about the Gcc-bugs
mailing list