I'm using an AT91SAM7 uC and have inherited some C code which is giving me fits. When I run this code I get a data abort error. I've tracked down the offending line of code, and it appears to be caused by something like this:
n = (UINT8) ((UINT16*)(pObject->pVar))[0];
pObject->pVar points to an instance of a structure like this:
typedef __packed struct{ UINT16 v1; UINT32 v2; UINT32 v3; UINT32 v4; UINT32 v5; }OBJTYPE; OBJTYPE xxx = {0,0,0,0,0};
When I stop the debugger on the line that causes the data abort error, I can see that pObject->pVar is pointing to 0x00200173. Is the data abort error happening because that object is not on a 32-bit boundary in memory? If so, how can I [easily] fix that? I've been going through all the online doc's reading about __packed, #pragma pack(n), adding unions to typedefs, etc.; but all the things I've been trying have not fixed the problem.
Is there an easy way (besides the __at__ attribute) to get variables like the above structure to be aligned on 32-bit boundaries?
Can anyone give me a suggestion for how to resolve this problem?
Help... Dave.
The bottom line is that there's too much of this inherited code, and it's full of stuff like this.
So you've got yourselves wedged between a rock and a hard place. That means soft measures will no longer work. You have to bite the bullet and get into gear to fix things in earnest. Yes, that'll hurt. But you'll be better off having done it in the medium term. It's already quite amazing that this cruft ever appeared to work correctly --- but that's no excuse to leave it in that state now you've diagnosed it.
but it sounds like the easiest way to be able to use __packed and be able to use typecasts to get stuff out of all these objects at runtime.
No. The easiest way would be to remove any and all appearances of __packed from that source code. They're causing you nothing but problems the rest of the code is ill-prepared to handle, and with the method you've described you would be losing any advantages it promises anyway. So just lose __packed completely, and never look back.