[LLVMdev] [cfe-dev] global alignment

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


On Aug 14, 2012, at 10:32 AM, John Criswell wrote:
> On 8/14/12 12:19 PM, John McCall wrote:
>> On Aug 14, 2012, at 7:31 AM, Baozeng wrote:
>>> 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).
> 
> I don't think that's the issue.  I've had problems aligning global variables on a 4096 byte boundary with Clang/LLVM 3.0.  Using hand-written assembly code to force the alignment fixed the issue.

Does it actually fix the issue, or does it produce slightly different code such that you win the 50% lottery of being 8K-aligned?  Does the assembly output from LLVM look actively wrong?

> Additionally, OS page alignment has nothing to do with symbol alignment.  Symbol alignment is done by the loader/linker, and from what I've seen, ELF places no restriction on alignment size (other than it being a 32-bit or 64-bit value).

I agree that ELF is certainly not going to hard-code any OS limitations, but that doesn't mean much.  The linker is just going to place things appropriately-aligned relative to the start of the section, and there are certainly loaders that will just map sections to any old page.

>> 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.
> 
> Baggy Bounds Checking: http://static.usenix.org/event/sec09/tech/full_papers/sec09_memory.pdf

Ok.

John.



More information about the llvm-dev mailing list