This is the mail archive of the
libc-help@sourceware.org
mailing list for the glibc project.
Re: are statically allocated structs always aligned to a machine word on x86/x86_64?
- From: Florian Weimer <fweimer at redhat dot com>
- To: john smith <wempwer at gmail dot com>, libc-help at sourceware dot org
- Date: Sun, 23 Aug 2015 10:24:46 +0200
- Subject: Re: are statically allocated structs always aligned to a machine word on x86/x86_64?
- Authentication-results: sourceware.org; auth=none
- References: <CAKmQUfZm+LwSpV4bdCnzXAH=tEKay2b9RK0qLoNtMukFi88HBw at mail dot gmail dot com>
On 08/21/2015 05:54 PM, john smith wrote:
> I didn't find any information about alignment requirements for
> statically allocated objects in glibc manual (or I have missed because
> the manual is huge). I noted that sometimes variables such as int are
> not aligned on word boundary in x86 and x86_64 but I have never seen a
> struct that wouldn't be allocated at address that isn't a multiple or
> 4/8. I am asking this question because I would like to know whether
> it's safe to assume that struct will be always assigned at a word
> boundary and therefore it's possible to correctly calculate a struct
> size without running a program.
We looked at a similar question when we assessed the impact of glibc bug
18287. In the end, we could not find a case where a top-level char
array with automatic storage duration was not aligned to a machine word.
However, none of the GCC engineers I talked could confirm that this
might never happen, under no circumstances.
So the answer to your question is: no, you cannot assume word alignment.
Even if you'll never see misaligned variables in practice.
--
Florian Weimer / Red Hat Product Security