[cfe-dev] global alignment

John McCall rjmccall at apple.com
Tue Aug 14 10:19:25 PDT 2012


On Aug 14, 2012, at 7:31 AM, Baozeng wrote:
> Hello all,
> I found that the alignment for stack value has no limitation, but for global value, it has a limitation.
> 
> Here is an example:
> 
> #include <stdio.h>
> #include <stdlib.h>
> 
> char x[4000] __attribute__((aligned(4096)));
> 
> int
> main (int argc, char ** argv) {
>   char y[4000] __attribute__((aligned(4096)));
>   printf("x is %p\n", x);
>   printf("y is %p\n", y);
>   
>   return 0;
> }
> 
> After compiled with clang, the result is:
> x is 0x804b000
> y is 0xbf9d8000
> 
> They are both aligned to be 4096 as we expected.
> 
> Then we change the example as the following:
> 
> char x[4000] __attribute__((aligned(8192)));
> 
> int
> main (int argc, char ** argv) {
>   char y[4000] __attribute__((aligned(8192)));
>   printf("x is %p\n", x);
>   printf("y is %p\n", y);
>   
>   return 0;
> }
> 
> The result is :
> x is 0x804d000
> y is 0xbffd2000
> 
> We can see that the stack value y is aligned to be 8192, but the global value x is not!
> 
> My target OS is 32-bit Linux. Anyone can explain this? or is this a bug of clang? 

It is very unlikely to be a frontend bug, although I suppose it's possible that it's a backend problem.

It is much more likely that your environment just doesn't support aligning symbols at granularities larger than a page.  The page size on x86-32 is 4K because that's what the hardware supports (or 4M if you're using PSE, which Linux does support, but it's not the default, and I don't think it'd solve your problem).

The stack example works because it has to dynamically realign anyway, i.e. it's just %ing the stack pointer, which has no inherent limitations.  Note that this is quite expensive and is leaving a potentially huge gap on your stack.

I'll admit to being curious as to why you actually need larger-than-page-size alignment.

John.



More information about the cfe-dev mailing list