Alignment Problems


Pickle

Mega GP Mania
Joined
May 30, 2006
Messages
5,518
Location
Detroit, Michigan
Website
Visit site
Theres a opensource project im trying to port and it appears to have the problem with alignments

An example would be:
CODE
int32_t myvariable = *( int32_t *) mypointer;


It seem some doc's on this "feature" of ARM devices and it appears if use the packed attribute it might behave correctly.

Ive also just come across a GCC item -fpack-struct that might fix the problem, but it will have a high overhead cost. I havnt tried this yet.

The third option is to change all of these casts, but it doesnt look easy. I did the first few but that opene the door to more problems.

Any other options or ideas?
 
I think packing the structs would make the problem worse, not better. This doesn't really sound struct related anyway (not specifically, at least), but a matter of trying to read words out from the middle of a stream of bytes.

Unfortunately the ARM920T is not capable of performing unaligned reads, at least not in the way you want it to. If you try it'll force alignment on the address then rotate the result. There's really no way around this than to find all of the offending locations and replace the cast + indirection with something that constructs the word either from four byte loads or from two word loads with some shifts and masking.

It's possible to get the ARM920T to throw exceptions on unaligned loads, although I don't know if Linux will signal you. This might be helpful in tracking them down. You could also use this to emulate unaligned loads but if they're used a lot it'll cause a major performance hit.

What are you trying to port? Maybe someone here can help you audit the code for this.
 
You can make Linux to send your program a signal when it does unaligned access, just type:
CODE

echo 4 > /proc/cpu/alignment



This will crash your program when it does unaligned accesses, but you can then use gdb and it will show you exact location in source.

See this page for more solutions of the problem.
 
Back
Top