[OpenBIOS] [PATCH] Fix some unaligned memory accesses

Paul Brook paul at codesourcery.com
Sun Apr 15 02:38:03 CEST 2007

On Sunday 15 April 2007 01:23, Stefan Reinauer wrote:
> * Aurelien Jarno <aurelien at aurel32.net> [070401 22:05]:
> > Hi all,
> >
> > The current SVN version currently fails to work on machines that have
> > strict alignment requirements, for example the SPARC target. This is due
> > to the way the struct fat_bpb in fs/grubfs/fat.h is defined and accessed
> > (through FAT_CVT_U16) to avoid padding. Some 16 bits fields like
> > bytes_per_sect are thus accessed unaligned.
> Why is this causing trouble? bytes_per_sect was defined as an array of
> u8, which you would not need to access aligned anyways. Is gcc doing
> some assumptions here?

It was accessed through a (uint16_t *) though.

gcc only generates special code to handle misaligned accesses when the access 
if a member of an explicitly packed (ie. misaligned) structure.

Because compensating for a misaligned access is so expensive the only sane 
option is to assume that normal pointers are properly aligned.

By taking the address and casting to (uint16_t *) you're hiding the member 
access, and allowing gcc to assume it is a properly aligned 16-bit value.


More information about the OpenBIOS mailing list