memcpy fails at len 3,7,11...

Asked by Erik Schieweck

today I switched from gnu tools "4.8 2013q4" to "5.2 2015q4". I do not compile it self I use the current download.
What happens my application is no more able to run. I found out the app is crashing at a point where I use memcpy of the len=3.
I make some test an are the opinion all len (n*4-1) will be crash in this version.
What could be the reason? Can I report this to bugs?

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
Andre Vieira (andre-simoesdiasvieira) said :
#1

Hi Erik,

We will need more information before we can investigate this. Can you please provide a reduced testcase which shows this behavior and your compilation command line with "-v" with the verbose output produced by it?

Cheers,
Andre

Revision history for this message
Erik Schieweck (up-info) said :
#2

Hello Andre,

here a simple test which sould pass without problems.

int x;
char fieldA[8]={1,2,3,4,5,6,7,8};
char fieldB[8]={0};

for(x=0;x<8;x++)
{
 memcpy(fieldB, fieldA, x);
}

But it produce an abort if x=3 and 7.

Here the output with the "-v" option.

Building target: WP3_universal.elf
Invoking: Cross ARM C Linker
arm-none-eabi-gcc -mcpu=cortex-a5 -march=armv7-a -mthumb -mfloat-abi=hard -mfpu=vfpv4-d16 -mno-unaligned-access -Os -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -Wall -g -T "C:\Users\memsolution\git\WP3_uni_repository\WP3_universal\ld\sram.ld" -nostartfiles -Xlinker --gc-sections -Wl,-Map,"WP3_universal.map" -v -o "WP3_universal.elf" ./source/SI5351A.o ./source/at45_access.o ./source/audio.o ./source/backlight_task.o ./source/config_menu.o ./source/fat.o ./source/i2c.o ./source/i2c_hw.o ./source/init_all.o ./source/interrupts.o ./source/intrinsics.o ./source/jumper.o ./source/keys.o ./source/lcd_display.o ./source/location_ptr_array.o ./source/ltc.o ./source/main.o ./source/main_task.o ./source/performance_task.o ./source/play_wav.o ./source/printf-stdarg.o ./source/security.o ./source/serial_protokoll.o ./source/set_fs.o ./source/sha204_access.o ./source/sha204_init.o ./source/support_task.o ./source/user_interface.o ./libsdmmc/source/sdmmc.o ./libchip_sama5d3x/source/aic.o ./libchip_sama5d3x/source/dmac.o ./libchip_sama5d3x/source/hsmci.o ./libchip_sama5d3x/source/mmu.o ./libchip_sama5d3x/source/pio.o ./libchip_sama5d3x/source/pio_it.o ./libchip_sama5d3x/source/pit.o ./libchip_sama5d3x/source/pmc.o ./libchip_sama5d3x/source/sha.o ./libchip_sama5d3x/source/spi.o ./libchip_sama5d3x/source/ssc.o ./libchip_sama5d3x/source/tc.o ./libchip_sama5d3x/source/twi.o ./libchip_sama5d3x/source/twid.o ./libchip_sama5d3x/source/usart.o ./libchip_sama5d3x/source/wdt.o ./libchip_sama5d3x/cp15/cp15.o ./libchip_sama5d3x/cp15/cp15_asm_gcc.o ./libboard_sama5d3x-ek/source/at45_spi.o ./libboard_sama5d3x-ek/source/board_cstartup_gnu.o ./libboard_sama5d3x-ek/source/board_lowlevel.o ./libboard_sama5d3x-ek/source/board_memories.o ./libboard_sama5d3x-ek/source/dbgu_console.o ./libboard_sama5d3x-ek/source/dma_hardware_interface.o ./libboard_sama5d3x-ek/source/dmad.o ./libboard_sama5d3x-ek/source/math.o ./libboard_sama5d3x-ek/source/mcid_dma.o ./libboard_sama5d3x-ek/source/spid_dma.o ./SHA256/sha256.o ./FreeRTOS/portable/context.o ./FreeRTOS/portable/heap_4.o ./FreeRTOS/portable/port.o ./FreeRTOS/portable/portASM.o ./FreeRTOS/FreeRTOS_tick_config.o ./FreeRTOS/list.o ./FreeRTOS/queue.o ./FreeRTOS/syscalls.o ./FreeRTOS/tasks.o
Using built-in specs.
COLLECT_GCC=arm-none-eabi-gcc
COLLECT_LTO_WRAPPER=c:/program\ files\ (x86)/gnu\ tools\ arm\ embedded/5.2\ 2015q4/bin/../lib/gcc/arm-none-eabi/5.2.1/lto-wrapper.exe
Target: arm-none-eabi
Configured with: /home/build/work/GCC-5-0-build/src/gcc/configure --build=i686-linux-gnu --host=i686-w64-mingw32 --target=arm-none-eabi --prefix=/home/build/work/GCC-5-0-build/install-mingw --libexecdir=/home/build/work/GCC-5-0-build/install-mingw/lib --infodir=/home/build/work/GCC-5-0-build/install-mingw/share/doc/gcc-arm-none-eabi/info --mandir=/home/build/work/GCC-5-0-build/install-mingw/share/doc/gcc-arm-none-eabi/man --htmldir=/home/build/work/GCC-5-0-build/install-mingw/share/doc/gcc-arm-none-eabi/html --pdfdir=/home/build/work/GCC-5-0-build/install-mingw/share/doc/gcc-arm-none-eabi/pdf --enable-languages=c,c++ --disable-decimal-float --disable-libffi --disable-libgomp --disable-libmudflap --disable-libquadmath --disable-libssp --disable-libstdcxx-pch --disable-nls --disable-shared --disable-threads --disable-tls --with-gnu-as --with-gnu-ld --with-headers=yes --with-newlib --with-python-dir=share/gcc-arm-none-eabi --with-sysroot=/home/build/work/GCC-5-0-build/install-mingw/arm-none-eabi --with-libiconv-prefix=/home/build/work/GCC-5-0-build/build-mingw/host-libs/usr --with-gmp=/home/build/work/GCC-5-0-build/build-mingw/host-libs/usr --with-mpfr=/home/build/work/GCC-5-0-build/build-mingw/host-libs/usr --with-mpc=/home/build/work/GCC-5-0-build/build-mingw/host-libs/usr --with-isl=/home/build/work/GCC-5-0-build/build-mingw/host-libs/usr --with-cloog=/home/build/work/GCC-5-0-build/build-mingw/host-libs/usr --with-libelf=/home/build/work/GCC-5-0-build/build-mingw/host-libs/usr --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm' --with-pkgversion='GNU Tools for ARM Embedded Processors' --with-multilib-list=armv6-m,armv7-m,armv7e-m,armv7-r,armv8-m.base,armv8-m.main
Thread model: single
gcc version 5.2.1 20151202 (release) [ARM/embedded-5-branch revision 231848] (GNU Tools for ARM Embedded Processors)
COMPILER_PATH=c:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../lib/gcc/arm-none-eabi/5.2.1/;c:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../lib/gcc/;c:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../lib/gcc/arm-none-eabi/5.2.1/../../../../arm-none-eabi/bin/
LIBRARY_PATH=c:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../lib/gcc/arm-none-eabi/5.2.1/armv7-ar/thumb/fpu/;c:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../lib/gcc/arm-none-eabi/5.2.1/../../../../arm-none-eabi/lib/armv7-ar/thumb/fpu/;c:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../arm-none-eabi/lib/armv7-ar/thumb/fpu/;c:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../lib/gcc/arm-none-eabi/5.2.1/;c:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../lib/gcc/;c:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../lib/gcc/arm-none-eabi/5.2.1/../../../../arm-none-eabi/lib/;c:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../arm-none-eabi/lib/
COLLECT_GCC_OPTIONS='-mcpu=cortex-a5' '-march=armv7-a' '-mthumb' '-mfloat-abi=hard' '-mfpu=vfpv4-d16' '-mno-unaligned-access' '-Os' '-fmessage-length=0' '-fsigned-char' '-ffunction-sections' '-fdata-sections' '-Wall' '-g' '-T' 'C:\Users\memsolution\git\WP3_uni_repository\WP3_universal\ld\sram.ld' '-nostartfiles' '-v' '-o' 'WP3_universal.elf'
 c:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../lib/gcc/arm-none-eabi/5.2.1/collect2.exe -plugin c:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../lib/gcc/arm-none-eabi/5.2.1/liblto_plugin-0.dll -plugin-opt=c:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../lib/gcc/arm-none-eabi/5.2.1/lto-wrapper.exe -plugin-opt=-fresolution=C:\Users\MEMSOL~1\AppData\Local\Temp\cc6OAgV1.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lg -plugin-opt=-pass-through=-lc --sysroot=c:\program files (x86)\gnu tools arm embedded\5.2 2015q4\bin\../arm-none-eabi -X -o WP3_universal.elf -Lc:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../lib/gcc/arm-none-eabi/5.2.1/armv7-ar/thumb/fpu -Lc:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../lib/gcc/arm-none-eabi/5.2.1/../../../../arm-none-eabi/lib/armv7-ar/thumb/fpu -Lc:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../arm-none-eabi/lib/armv7-ar/thumb/fpu -Lc:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../lib/gcc/arm-none-eabi/5.2.1 -Lc:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../lib/gcc -Lc:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../lib/gcc/arm-none-eabi/5.2.1/../../../../arm-none-eabi/lib -Lc:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../arm-none-eabi/lib --gc-sections -Map WP3_universal.map ./source/SI5351A.o ./source/at45_access.o ./source/audio.o ./source/backlight_task.o ./source/config_menu.o ./source/fat.o ./source/i2c.o ./source/i2c_hw.o ./source/init_all.o ./source/interrupts.o ./source/intrinsics.o ./source/jumper.o ./source/keys.o ./source/lcd_display.o ./source/location_ptr_array.o ./source/ltc.o ./source/main.o ./source/main_task.o ./source/performance_task.o ./source/play_wav.o ./source/printf-stdarg.o ./source/security.o ./source/serial_protokoll.o ./source/set_fs.o ./source/sha204_access.o ./source/sha204_init.o ./source/support_task.o ./source/user_interface.o ./libsdmmc/source/sdmmc.o ./libchip_sama5d3x/source/aic.o ./libchip_sama5d3x/source/dmac.o ./libchip_sama5d3x/source/hsmci.o ./libchip_sama5d3x/source/mmu.o ./libchip_sama5d3x/source/pio.o ./libchip_sama5d3x/source/pio_it.o ./libchip_sama5d3x/source/pit.o ./libchip_sama5d3x/source/pmc.o ./libchip_sama5d3x/source/sha.o ./libchip_sama5d3x/source/spi.o ./libchip_sama5d3x/source/ssc.o ./libchip_sama5d3x/source/tc.o ./libchip_sama5d3x/source/twi.o ./libchip_sama5d3x/source/twid.o ./libchip_sama5d3x/source/usart.o ./libchip_sama5d3x/source/wdt.o ./libchip_sama5d3x/cp15/cp15.o ./libchip_sama5d3x/cp15/cp15_asm_gcc.o ./libboard_sama5d3x-ek/source/at45_spi.o ./libboard_sama5d3x-ek/source/board_cstartup_gnu.o ./libboard_sama5d3x-ek/source/board_lowlevel.o ./libboard_sama5d3x-ek/source/board_memories.o ./libboard_sama5d3x-ek/source/dbgu_console.o ./libboard_sama5d3x-ek/source/dma_hardware_interface.o ./libboard_sama5d3x-ek/source/dmad.o ./libboard_sama5d3x-ek/source/math.o ./libboard_sama5d3x-ek/source/mcid_dma.o ./libboard_sama5d3x-ek/source/spid_dma.o ./SHA256/sha256.o ./FreeRTOS/portable/context.o ./FreeRTOS/portable/heap_4.o ./FreeRTOS/portable/port.o ./FreeRTOS/portable/portASM.o ./FreeRTOS/FreeRTOS_tick_config.o ./FreeRTOS/list.o ./FreeRTOS/queue.o ./FreeRTOS/syscalls.o ./FreeRTOS/tasks.o --start-group -lgcc -lg -lc --end-group -T C:\Users\memsolution\git\WP3_uni_repository\WP3_universal\ld\sram.ld
Finished building target: WP3_universal.elf

Best Erik

Revision history for this message
Andre Vieira (andre-simoesdiasvieira) said :
#3

Hi Erik,

I'm not able to reproduce the faulty behavior using the sbrk implementation in librdimon.a (using --specs=rdimon.specs), which leads me to believe it might be related to the sbrk implementation for your board. I suggest debugging the calls to sbrk to find out where its going wrong.

Hope this helps.

Cheers,
Andre

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

Hi, Erik,

It is unlikely to be something wrong with memcpy, which will fall back to byte-to-byte copy is any of input addresses is unaligned. What else you can do to help us reproducing the crash?

Thanks,
Joey

Revision history for this message
Erik Schieweck (up-info) said :
#5

Hello Joey,

the problem seems to be if the input address aligned but the len =3.
memcpy is trying to made a byte transfer and then a halfword transfer, and this halfword transfer
is a self made unaligned access which fails. This is what I can see if I debug the crash.

Best Erik

Revision history for this message
Andre Vieira (andre-simoesdiasvieira) said :
#7

Hi Erik,

Sorry about my message earlier about sbrk... for some reason my head was stuck on malloc. Memcpy obviously doesnt need sbrk for anything.

Now you mention that you see memcpy use a halfword transfer, which is weird. Looking at the implementation of newlib's memcpy we see that for small memcpys a byte-by-byte copy is used as Joey points out.

Can you compile with '-Wl,-Map=map.output' appended to your command line, then paste all lines in map.output that include memcpy. This will let us know which memcpy is being used, just to be sure we are looking at the same thing.

Cheers,
Andre

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

Hi, Erik,

After a deeper diving into memcpy, there are two problems. One is in our side we picked the unexpected version of memcpy implementation which has optimizations to copy misaligned address. We will work on it for a fix. It explains why 4.8 works while 5.2 doesn't.

But in the other hand, libraries of this toolchain was not built to run on CPU that doesn't support mis-aligned memory access. They were built with assumption that mis-aligned memory address is supported. Memcpy in 4.8 for armv7-ar happen to work, but it is likely that other library function will break at mis-aligned access. The solution is to rebuild the libraries with flag -mno-unaligned-access

Please confirm if the CPU you are running does not support mis-aligned memory access.

Thanks,
Joey

Revision history for this message
Erik Schieweck (up-info) said :
#9

Hi all,

ok understand.

Here the copy of the map file:

c:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../lib/gcc/arm-none-eabi/5.2.1/../../../../arm-none-eabi/lib/armv7-ar/thumb/fpu\libg.a(lib_a-memcpy.o)
    ./source/init_all.o (memcpy)

.data 0x00000000 0x0 c:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../lib/gcc/arm-none-eabi/5.2.1/../../../../arm-none-eabi/lib/armv7-ar/thumb/fpu\libg.a(lib_a-memcpy.o)

 .bss 0x00000000 0x0 c:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../lib/gcc/arm-none-eabi/5.2.1/../../../../arm-none-eabi/lib/armv7-ar/thumb/fpu\libg.a(lib_a-memcpy.o)

.text 0x0030a23c 0xec c:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../lib/gcc/arm-none-eabi/5.2.1/../../../../arm-none-eabi/lib/armv7-ar/thumb/fpu\libg.a(lib_a-memcpy.o)
                0x0030a23c memcpy

.ARM.attributes
                0x00000e92 0x19 c:/program files (x86)/gnu tools arm embedded/5.2 2015q4/bin/../lib/gcc/arm-none-eabi/5.2.1/../../../../arm-none-eabi/lib/armv7-ar/thumb/fpu\libg.a(lib_a-memcpy.o)

Maybe you mean this lib.
In the meantime I have found out and use the nano part of the lib which has not the problem.
The CPU I have supports mis-aligned access not in the normal mode. There is a bit in CP15 which bings back this possibility.
But is is not recommend to use it. Because it has some additionally CPU cyles required for this. In the end the memcpy was the only problem. Because of this I would not switch the mode.

Best Erik

Can you help with this problem?

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

To post a message you must log in.