[U-Boot-Users] struct NS16550 {...} __attribute__ ((packed)) at ns16550.h

Wolfgang Denk wd at denx.de
Tue Apr 26 09:21:45 CEST 2005

In message <426DDE65.4060009 at intracom.gr> you wrote:
> > ...which is broken, especially when there is no need to do this since
> > the fields are properly aligned.
> The compiler does not know this. A pointer to the structure could be
> pointing to an non-aligned address, and when an aligned field within
> the structure is accessed it will be not aligned.

This is no reason for anything.  The  same  could  be  true  for  ANY
structures, even without using the packed attribute. It is the user's
problem if he passes incorrectly aligned pointers.

> This typically happens when networking.

Yes, and usually you will have to explicitely deal with this.

> IMHO __attribute((packed)) is deceivingly named, since it implies
> two things.

I think you are wrong. You are trying to explain the  current  imple-
mentation of GCC for ARM, assuming silently that the implementation
is correct. I say: it is broken.

> 1) Don't leave any space between fields of the structure.

Correct - this is what the documentation says:

    The `packed' attribute specifies that a variable or structure field
     should have the smallest possible alignment--one byte for a
     variable, and one bit for a field, unless you specify a larger
     value with the `aligned' attribute.

     Here is a structure in which the field `x' is packed, so that it
     immediately follows `a':

          struct foo
            char a;
            int x[2] __attribute__ ((packed));

> 2) Access the fields of the structure even when the alignment of the
>     pointer pointing to it is wrong.

Wrong. Remember that a structure may be a representation  for  things
like  hardware  registers.  Registers may have funny properties, like
auto-incrementing with each access, or auto-resetting on read.  If  I
perform  a  32 bit read on such an object the compiler MUST NOT break
this up into any other combination of accesses.

> Just by the name, you expect only (1).

By the name, and the documentation, and common sense.

I think we have covered all relevant facts now. I understand that  we
won't  have  GCC  fixed  soon,  and  I already suggested what I think
should be used to work around this compiler bug (using aligned).

Best regards,

Wolfgang Denk

Software Engineering:  Embedded and Realtime Systems,  Embedded Linux
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Quote from the Boss... "I didn't say it was your fault.  I said I was
going to blame it on you."

More information about the U-Boot mailing list