armv7-m memcpy usage fault

Bug #1303369 reported by Martin Velek
12
This bug affects 2 people
Affects Status Importance Assigned to Milestone
GNU Arm Embedded Toolchain
Won't Fix
Low
Terry Guo

Bug Description

Hello,

the memcpy causes usage fault on Cortex M3 when UNALIGN_TRP is set in CCR.

Test case:
#include <string.h>
int main(void)
{
 memcpy((void *)0x200050d0, (void *)0xa0ffe7b6, 42);
 return 1;
}

Cross ARM Compiler
arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -mno-unaligned-access -O0 -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fno-common -Wall -g3 -std=gnu99 -Wa,-adhlns="main.o.lst" -fstack-usage -funwind-tables -MMD -MP -MF"main.d" -MT"main.d" -c -o "main.o" "../main.c"

Cross ARM C Linker
arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -mno-unaligned-access -O0 -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fno-common -Wall -g3 -T "rom.ld" -Xlinker --gc-sections -Xlinker --wrap=_free_r -Xlinker --wrap=_malloc_r -Xlinker --wrap=_calloc_r -Xlinker --wrap=_realloc_r -Wl,-Map,"App.map" -o "App.elf" ./main.o ./startup.o ./startup_ARMCM3.o

memcpy asm:
00009902: bne.n 0x9998 <memcpy+160>
00009904: subs r2, #64 ; 0x40
00009906: bcc.n 0x9950 <memcpy+88>
-->>>00009908: ldr r3, [r1, #0] ; ldr from 0xa0ffe7b6, unaligned fault
0000990a: str r3, [r0, #0]
0000990c: ldr r3, [r1, #4]

Revision history for this message
Martin Velek (martin-velek) wrote :

I did some inspection and the reason for this error is following:

arm-none-eabi-gcc -mcpu=cortex-m3 by default assumes that the unaligned access is enabled. The macro __ARM_FEATURE_UNALIGNED is set. The source code in newlib machine/arm/memcpy-armv7m.S is built with this macro. The result is weird. It is bug and it is not bug.

I think there should be mentioned in the readme.txt to NOT TO SET UNALIGN_TRP in CCR. Anyway, the GDB with UNALIGN_TRP behaves strange. I was unable to get stack list, debugger reported read from non existent addresses etc. I have disabled UNALIGN_TRP bit and it works now fine.

Best
Martin

Terry Guo (terry.guo)
Changed in gcc-arm-embedded:
assignee: nobody → Terry Guo (terry.guo)
Revision history for this message
Terry Guo (terry.guo) wrote :

You set UNALIGN_TRP in CCR and then you got UsageFault for unaligned address lately. I think this is correct behavior. About the strange behavior of gdb when you in UsageFault handler, it maybe a bug of gdb, but not related to memcpy function itself in my opinion.

Revision history for this message
Joey Ye (jinyun-ye) wrote :

Martin,

This is expected bahavior, as the memcpy in libc was built assuming UNALIGN_TRP unset, which is most of cases.

To make it work in your case, you can either rebuild library with -mno-unaligned-access, or just use libc for armv6-m. To use armv6-m lib you can specify -march=armv6-m in linker option.

Thanks,
Joey

Changed in gcc-arm-embedded:
status: New → Won't Fix
importance: Undecided → Low
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.