[head tinderbox] failure on arm/arm

Giorgos Keramidas keramida at freebsd.org
Sun Nov 12 07:12:35 PST 2006


On 2006-11-12 17:51, Ruslan Ermilov <ru at FreeBSD.org> wrote:
> On Sun, Nov 12, 2006 at 03:42:30PM +0100, Giorgos Keramidas wrote:
> > On 2006-11-12 17:00, Ruslan Ermilov <ru at freebsd.org> wrote:
> > > %%%
> > > $ cat a.c
> > > struct foo {
> > > 	char x;
> > > };
> > >
> > > struct foo *
> > > bubu(char *s)
> > > {
> > >
> > > 	return (struct foo *)s;
> > > }
> > > $ cc -c -Wcast-align a.c
> > > a.c: In function `bubu':
> > > a.c:9: warning: cast increases required alignment of target type
> > > %%%
> > >
> > > (None of other supported architecutes see the issue here.)
> > 
> > You can't cast any random (char *) pointer to a pointer of a type which
> > is (potentially) larger than 1 byte.  It's the same sort of warning you
> > will get if you try to:
> > 
> >     char ch[] = "\x00\x00\x00\x00";
> >     char *p = &(ch[0]);
> >     unsigned long *lptr = (unsigned long *)p;
> > 
> > You cannot guarantee that `ch' is stored in an address that is properly
> > aligned for (unsigned long), and this is what GCC warns about here.
> 
> No, your example I perfectly understand but it is completely different.
> Note that the first (and only) member in my structure is "char", so it
> doesn't need to be more than sizeof(char) aligned.

Ah, but the tricky part is that inside bubu() there is no knowledge that
`s' may be properly aligned for a (struct foo *) pointer.  All the
compiler knows is that it is a (char *), possibly misaligned for any
pointer whose object has a size > 1.

> > On 2006-11-12 15:27, Stefan Farfeleder <stefan at fafoe.narf.at> wrote:
> > > What is sizeof(struct foo)?  If it's > 1 it makes sense.
> > 
> > Exactly :)
> 
> Still doesn't make much sense to me.  If all structure members are chars
> (like is the case with "struct ar_hdr" from <ar.h> which GCC complains
> about, and in my example, the required alignment shouldn't be more than
> sizeof(char).  What am I missing?

You are missing that inside bubu() the compiler 'believes' that:

    * The `s' pointer is (char *)-aligned.

    * The sizeof(struct foo) is >1.

    * You are trying to assign `s' (with it's possibly misaligned
      value) to the `return value' place, whose type is (at
      least, as far as the compiler knows) is (struct foo *).

That may break alignment assumptions the compiler is making inside bubu(),
especially about the `s' pointer, hence the warning.

- Giorgos



More information about the freebsd-current mailing list