This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: target/9080: optimisation on sparc leads to unalligned access in memcpy
- From: rth at gcc dot gnu dot org
- To: 136659 at bugs dot debian dot org, gcc-bugs at gcc dot gnu dot org, gcc-prs at gcc dot gnu dot org, lists at lina dot inka dot de, nobody at gcc dot gnu dot org, rth at gcc dot gnu dot org
- Date: 26 Jan 2003 10:13:38 -0000
- Subject: Re: target/9080: optimisation on sparc leads to unalligned access in memcpy
- Reply-to: rth at gcc dot gnu dot org, 136659 at bugs dot debian dot org, gcc-bugs at gcc dot gnu dot org, gcc-prs at gcc dot gnu dot org, lists at lina dot inka dot de, nobody at gcc dot gnu dot org, rth at gcc dot gnu dot org, gcc-gnats at gcc dot gnu dot org
Synopsis: optimisation on sparc leads to unalligned access in memcpy
Responsible-Changed-From-To: unassigned->rth
Responsible-Changed-By: rth
Responsible-Changed-When: Sun Jan 26 10:13:38 2003
Responsible-Changed-Why:
.
State-Changed-From-To: open->closed
State-Changed-By: rth
State-Changed-When: Sun Jan 26 10:13:38 2003
State-Changed-Why:
I'm fairly sure this has been discussed before, but I can't find
the reference right away. The problem is known, and I consider
the input program incorrect.
The problem is that alignof(struct iphdr) == 4, and yet the
given pointer to that structure is only 2 byte aligned. This
violates section 6.3.2.3/7, to wit:
If the resulting pointer is not correctly aligned for the
pointed-to type, the behavior is undefined.
GCC uses this clause to infer from the type of the pointer the
alignment that it *must* have.
There are two solutions for this piece of code.
One, use packed data structures, so that they are 1 byte aligned.
In this case something like
struct iphdr_u {
struct iphdr h __attribute__((packed));
};
followed by
&tmp_iphdr->h.saddr
Two, don't cast to struct iphdr at all. Instead use offsetof
to find the proper displacement into the buffer.
http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=9080