This is the mail archive of the
mailing list for the GCC project.
Re: GCC ARM: aligned access
- From: Joel Sherrill <joel dot sherrill at oarcorp dot com>
- To: Peng Fan <van dot freenix at gmail dot com>,"gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>
- Date: Sun, 31 Aug 2014 13:32:25 -0500
- Subject: Re: GCC ARM: aligned access
- Authentication-results: sourceware.org; auth=none
- References: <540320F5 dot 9030501 at gmail dot com>
On August 31, 2014 8:19:49 AM CDT, Peng Fan <email@example.com> wrote:
>I am writing some code and found that system crashed. I found it was
>unaligned access which causes `data abort` exception. I write a piece
>of code and objdump
>it. I am not sure this is right or not.
>arm-poky-linux-gnueabi-gcc -marm -mno-thumb-interwork -mabi=aapcs-linux
>-mword-relocations -march=armv7-a -mno-unaligned-access
>-ffunction-sections -fdata-sections -fno-common -ffixed-r9 -msoft-float
>-pipe -O2 -c 2.c -o 2.o
>arch is armv7-a and used '-mno-unaligned access'
>typedef unsigned char u8;
>int func(u8 *data)
>return *(unsigned int *)data;
>The objdumped asm code is:
>Disassembly of section .text.func:
>0: e5900000 ldr r0, [r0]
> 4: e12fff1e bx lr
>from the asm code, we can see that 'ldr r0, [r0]' corresponding to
>'*(unsigned int*)data'. is this correct?
>we can not guarantee that pointer data is 4bytes aligned. If pointer
>data is not 4bytes aligned, and aligned
>access check is enabled by setting a hardware bit in arm coprocessor,
>then `data abort` may occur.
I think this is totally expected. You were passed a u8 pointer which is aligned for that type (no restrictions likely). You cast it to a type with stricter alignment requirements. The code is just flawed. Some CPUs handle unaligned accesses but not your ARM.
If you turn on Wall, do you get a warning?