[cortex-m0] hard fault when -fpack-struct option

Asked by Mallikarjun

Hi,
I have following test case:

typedef struct
{
char a;
long b;
} st_A;

st_A varstructA[3];

int main()
{
       long *pb;
       int i;

       for(i=0;i<3;i++)
       {
              varstructA[i].a=10+i;
              varstructA[i].b=20+i;
       }

       pb=&varstructA[1].b;
       (*pb)++;
       pb=&varstructA[0].b;
       (*pb)++;

       return 0;
}

i compile with: arm-none-eabi-gcc -c -mcpu=cortex-m0 -mthumb -O0 -fpack-struct

I get hard fault when executing instructyions of (*pb)++. Because of packing, LDR is trying to access unaligned memory which results in hard fault. This means, we can not use -fpack-struct for cortex-m0 devices at all (as this device does not support unaligned access ldr/str instructions)?

Please let me know.

Thanks,
Mallikarjuna

Question information

Language:
English Edit question
Status:
Answered
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Terry Guo (terry.guo) said :
#1

Dealing unaligned address with LDR on cortex-m0 is illegal. But still GCC can generate proper code to handle such case. If you check the assembly code for statement "varstructA[i].b=20+i;", you can see what I said.

But for statement "pb=&varstructA[1].b;", I really think it is something related to how the pb is defined and used. Firstly your define pb as "long *pb", the natural alignment of "long" is 32bit, so pb will hold only aligned address. But lately you have "pb=&varstructA[1].b;", an unaligned address is assigned to pb, the compiler still thinks pb is an aligned address.

Revision history for this message
Joey Ye (jinyun-ye) said :
#2

Change status to answered

Can you help with this problem?

Provide an answer of your own, or ask Mallikarjun for more information if necessary.

To post a message you must log in.