[U-Boot] [PATCH] fat: fix unaligned errors

Wolfgang Denk wd at denx.de
Sat Jan 3 00:48:50 CET 2009


Dear Mike,

In message <200901021742.05363.vapier at gentoo.org> you wrote:
>
> > > +__u8 get_vfatname_block[MAX_CLUSTSIZE] __attribute__
> > > ((aligned(sizeof(__u16))));
> > ...
> > > +__u8 get_dentfromdir_block[MAX_CLUSTSIZE] __attribute__
> > > ((aligned(sizeof(__u32))));
> > ...
> > > +__u8 do_fat_read_block[MAX_CLUSTSIZE] __attribute__
> > > ((aligned(sizeof(__u32))));
> >
> > What makes you sure that a 16 resp. 32 bit alignment is sufficient,
> > and that gcc does not decide to align such structures even stricter?
> 
> read the structures in question.  the largest member is 16bit/32bit.  i really 
> dont know what other alignment gcc could randomly generate.

It is not (only) the size of the "largest" member in a struct which
determins global alignment, but also the size of the struct itself.

See for example
http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Type-Attributes.html

Read especially this part:

	In the example above, if the size of each short is 2 bytes,
	then the size of the entire struct S type is 6 bytes. The
	smallest power of two which is greater than or equal to that
	is 8, so the compiler sets the alignment for the entire struct
	S type to 8 bytes. 

> > Wouldn't it make more sense to use "__alignof__ ()" here to be on the
> > safe side?
>
> i found no such alignment attribute in the gcc docs which is why i suggested 
> Bryan use aligned(sizeof(...)).
> http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Function-Attributes.html
> http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Variable-Attributes.html
> http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Type-Attributes.html

Maybe you should have searched for the (IMHO pretty obvious) term
"Alignment" as well, i.e. look at
http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Alignment.html#Alignment

Quote:

	The keyword __alignof__ allows you to inquire about how an
	object is aligned, or the minimum alignment usually required
	by a type. Its syntax is just like sizeof. 


Best regards,

Wolfgang Denk

-- 
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
The more sins you confess, the more books you will sell.


More information about the U-Boot mailing list