From: David Huggins-Daines <dhd@linuxcare.com>
To: axp-list@redhat.com
Subject: Re: Fixing "unaligned trap" errors
Date: 19 May 2000 13:51:01 -0400 [thread overview]
Message-ID: <87g0re2zui.fsf@linuxcare.com> (raw)
In-Reply-To: David Rysdam's message of "Tue, 16 May 2000 05:13:26 -0700 (PDT)"
David Rysdam <drysdam@yahoo.com> writes:
> I've searched the list archives, but I can only find
> one post that addresses how to fix "unaligned trap"
> messages in one's own program.
>
> What I've been able to figure out so far:
>
> -What file/line the error is in.
You're doing pretty well, then. You still need to find out exactly
which variable or pointer is causing the fault. GDB, 'objdump
--source', or printf() should all serve equally well :-)
> What I still need to know:
>
> -What the problem is.
Alpha requires data to be "naturally aligned". In other words, data
of type X must be stored at a memory location Y such that
Y = 0 mod sizeof(X). [1]
Therefore the simplest description of the problem is that you are
accessing a 32-bit or 64-bit number via a pointer that doesn't point
to a 4 or 8-byte boundary. e.g.:
char foo[24];
int * bar = foo + 2; /* BAD: 2 mod 4 == 2 */
int * baz = foo + 8; /* GOOD: 8 mod 4 == 0 */
long * quux = foo + 12; /* BAD: 12 mod 8 == 4 */
long * argh = foo + 16; /* GOOD: 16 mod 8 == 0 */
Lots of legacy binary data formats have unaligned fields, because at
the time, saving a few bytes on the 16-bit machines of the time was a
higher priority than planning for 32 and 64-bit RISC machines of the
future.
For example ... IP headers, DOS partition tables, DWARF2 exception
frame info.
In general whenever you cast from a pointer to a smaller type to a
pointer of a larger type you have a potential alignment problem. (and
of course, if the smaller type is not char*, you will also have
problems with strict aliasing). So, for example, if you define a
global variable: (this was a bug in Linux fdisk)
char foo[512];
Then later on do:
uint64_t *bar = (uint64_t *) foo;
*bar = 42;
You have a good chance of producing an unaligned error. [2]
Another way to cause this problem is if you concatenate two structs of
different type in a single block of memory (this happens a lot when
reading and writing binary file formats). For example:
--- start program ---
#include <stdio.h>
#include <stdlib.h>
/* sizeof(struct foo) == 12 */
struct foo {
int a, b, c;
};
struct bar {
long d, e, f;
};
int main()
{
char *quux = malloc(sizeof(struct foo) + sizeof(struct bar));
struct foo *s1 = (struct foo *) quux;
struct bar *s2 = (struct bar *) (quux + sizeof(*s1));
printf("s1 @ %#p, s2 @ %#p\n", s1, s2);
s2->d = 42;
return 0;
}
--- end program ---
Results in:
[dhd@curacao]:~$ ./a.out
s1 @ 0x120100970, s2 @ 0x12010097c
[dhd@curacao]:~$ dmesg | tail -1
a.out(29802): unaligned trap at 00000001200005ec: 000000012010097c 2d 2
> -How to fix it.
Well, if you can at all help it, don't play pointer games that result
in unaligned data. In particular, if you are designing a binary file
format, make sure that all data in it are naturally aligned.
GCC has an __alignof__ keyword that works like 'sizeof' but evaluates
to the required or optimal alignment for a type or an lvalue, which
might help. You can also use the 'aligned' attribute to specify a
larger alignment for, e.g., arrays of char:
unsigned char foo[512] __attribute__ ((aligned(__alignof__(long))));
If you absolutely have to access unaligned data (for example, you are
dealing with a legacy file format or data structure that you can't
change), then you have a couple of options.
If you are reading binary data into memory you can sometimes "pad" a
block of memory to move the necessary fields into alignment. This of
course requires that you know what the alignment of the block of
memory is :-) For example:
/* struct partition contains two 32-bit fields that are 2
bytes out of alignment */
unsigned char _sect[512 + 2];
unsigned char *sect = _sect + 2;
struct partition *part = sect + 0x1be;
But that only works if _sect is already aligned to at least
sizeof(int), which may or may not be the case.
Thus, the other (more portable) way is to write a
get_unaligned_8byte() or get_unaligned_4byte() macro or function to
read and write the value in question. Unfortunately the only portable
way to do this in C (by reading a byte at a time and shifting/OR-ing
them together) is incredibly wasteful on Alpha, but then again,
unaligned traps are even more wasteful.
If you don't mind being GCC specific you can do something nasty like:
union unaligned {
void *p;
unsigned b4 __attribute__ ((mode (SI)));
unsigned b8 __attribute__ ((mode (DI)));
} __attribute__ ((packed));
static inline unsigned long
read_8byte (void *p)
{ union unaligned *up = p; return up->b8; }
static inline void
write_8byte (void *p, unsigned long val)
{ union unaligned *up = p; up->b8 = val; }
static inline unsigned int
read_4byte (void *p)
{ union unaligned *up = p; return up->b4; }
static inline void
write_4byte (void *p, unsigned int val)
{ union unaligned *up = p; up->b4 = val; }
But I'm almost 100% certain that this will not work on, for example,
CCC.
[1] Actually for 16-bit integers on non-BWX targets this isn't true
since they aren't supported in hardware, but you shouldn't rely on
that :-)
[2] If foo were an auto variable you would not have this problem since
the stack is always aligned to 8 bytes.
--
David Huggins-Daines, Senior GNU/Linux Consultant, Linuxcare, Inc.
613.562.1239 desk, 613.223.0225 mobile
dhd@linuxcare.com, http://www.linuxcare.com/
Linuxcare. Support for the revolution.
next prev parent reply other threads:[~2000-05-19 17:53 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2000-05-16 12:13 Fixing "unaligned trap" errors David Rysdam
2000-05-19 17:51 ` David Huggins-Daines [this message]
2000-05-20 17:41 ` Michal Jaegermann
2000-05-21 19:11 ` David Huggins-Daines
-- strict thread matches above, loose matches on Subject: below --
2000-05-19 15:43 William H. Magill
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox