Subject: Re: Exactly what is wrong with our compiler?
To: None <eeh@netbsd.org>
From: Lloyd Parkes <lloyd@must-have-coffee.gen.nz>
List: port-sparc64
Date: 01/04/2002 14:48:34
> As far as I know the NTP problem is not a sparc64 specific problem but
> a general gcc foulup that happens to hit sparc64 because it's a 64-bit
> platform and doesn't to alignment fixups.

I have looked into this more deeply in the hope of providing someone
somewhere with a fix. I don't know if this is a compiler foul up or not.
I have lost my copy of K&R2, so I can't check some of the issues
surrounding pointers. 

The problem with the memory copy optimiser is that it assumes that the
pointer it is being passed is correctly aligned based on its type. For
instance, no assumptions are made about pointers to void or chars, but
pointers to larger objects that must be aligned are assumed to be
aligned. The problem with NTP is that it uses such a pointer to point to
an unaligned object. Without more information on the ANSI C standard, I
don't know whether or not this falls into the category of making a
broken program worse.

Here is a summary of what NTP does, followed by an example of a more
normal way to do the same thing.

	char *buf;
	int offset;
	struct big data, *ptr;

	/* The NTP way. */
	ptr = (struct big *)(buf + offset);
	data = *ptr;		/* ptr does not point to aligned storage */

	/* The normal way */
	memcpy (&data, buf + offset, sizeof (data));

Note that gcc treats the use of memcpy and structure copies the same
way. 

Cheers,
Lloyd