__attribute__((aligned(..))) can produce unaligned variable

Andreas Schwab schwab@suse.de
Fri Jun 23 02:42:00 GMT 2000


If a variable is declared with a special alignment which is smaller than
the natural alignment then gcc creates unaligned data.  For example,
compiling this for Alpha or IA64:

long foo = 1;
int bar = 2;
long unaligned __attribute__((aligned(4))) = 3;

creates the variable `unaligned' with 4 byte alignment, although the
natural alignment is 8.  Consequently the assembler forces the 8 byte
alignment by itself and inserts 4 bytes padding (see line 22 below):

   1              		.file	1 "align.c"
   2              		.set noat
   3              		.set noreorder
   4              		.globl foo
   5              	.section	.sdata,"aw"
   6              		.align 3
   7              		.type	 foo,@object
   8              		.size	 foo,8
   9              	foo:
  10 0000 01000000 		.quad 1
  10      00000000 
  11              		.globl bar
  12              		.align 2
  13              		.type	 bar,@object
  14              		.size	 bar,4
  15              	bar:
  16 0008 02000000 		.long 2
  17              		.globl unaligned
  18              		.align 2
  19              		.type	 unaligned,@object
  20              		.size	 unaligned,8
  21              	unaligned:
  22 000c 00000000 		.quad 3
  22      03000000 
  22      00000000 
  23              		.ident	"GCC: (GNU) 2.95.2 19991024 (release)"

The manual says that the `aligned' attribute can never decrease the
alignment, unless `packed' is specified too.  On the other hand, the
`packed' attribute is ignored for variables (DECL_PACKED is the same as
DECL_REGISTER for a VAR_DECL).

IMHO this is a bug, since the compiler silently produces wrong code.

Andreas.

-- 
Andreas Schwab                                  "And now for something
SuSE Labs                                        completely different."
Andreas.Schwab@suse.de
SuSE GmbH, Schanzäckerstr. 10, D-90443 Nürnberg


More information about the Gcc-bugs mailing list