local variable alignment on Solaris/SPARC, with gcc 4.x

Amruth Laxman laxmans@bellsouth.net
Fri Sep 21 11:26:00 GMT 2007


Hi,

Relevant info:
- GCC version: 4.1.0, configured for 32-bit compiles
- Operating System: Solaris 10, SPARC V9

In migrating from GCC 3.3.5 to GCC 4.1.0, we have observed a change in 
alignment of local (stack) variables. GCC 3.3.5 (and GCC 3.4.6) align 
local variables on 4-byte boundaries by default. In contrast, GCC 4.1.0 
(going back to GCC 4.0.2) seems to align variables at their 'default' 
alignment.

Take for example the following code:

void someFunction()
{
   char buf[10];
   ...
}

Under gcc 3.3.5, buf is aligned on a 4-byte address, while on gcc 4.1.0, 
it is aligned on a 1-byte address (presumably because buf is a character 
array). This happens with or without optimization. Both the 3.3.5 and 
4.1.0 compilers are configured similarly and execute on the same 
machine. Compilation is done with the same options in both cases - 
'no-options' and '-O2' produce the same results.

The consequence of this change is that code that is dependent on the 
alignment of the buffer fails with the newer compiler. I understand that 
such code is inherently non-portable and unstable, but is unfortunately 
part of our application.

My questions are as follows:
1. Has anyone else experienced the same behavior? I'd like to rule out 
configuration issues.
2. Assuming this is not a configuration issue, does anyone have 
information as to why such a change was made? For example, was the 
change made to comply with the SPARC ABI? [Incidentally, the Sun Studio 
compiler also aligns at 1-byte, but moves up to 4-byte alignment with 
optimization]
3. I understand that it is possible to override the alignment using the 
'__attribute__((aligned))' directive, but this is not a trivial change 
across several thousand lines of code. Does anyone know of any compiler 
options that would produce the same results globally?

-Amruth



More information about the Gcc-help mailing list