segfault in memset

Asked by Ken Sarkies

Hi All

Question is - should I report this as a bug or am I the guilty one?

I setup a very simple test program to run FreeRTOS with libopencm3on an STM32F103 Cortex-M3 processor. It compiled and ran fine when compiled with the now unmaintained summon-arm toolchain. When I compiled it recently with gcc-arm-embedded it segfaulted. The location is in a call to memset within FreeRTOS tasks.c, in a section of code not related to the hardware drivers and that should be considered very well tested.

The disassembly of memset starts with
 0x08000d04 <+0>: movs r3, r0
 0x08000d06 <+2>: b.n 0x800132a

The branch to 0x800132a goes outside the program, the location containing 0xffffffff (unprogrammed). Contents of memory are the same as that in the below source listing

Source listing however seems to have a different idea of what the code does
 8000d04: e3100003 tst r0, #3
 8000d08: e92d0030 push {r4, r5}
 8000d0c: e1a03000 mov r3, r0
 8000d10: 0a000037 beq 8000df4 <memset+0xf0>

Just some possibly useless details:

Board is ET-STM32F103 (also happens on an ET-STAMP-STM32 which has a larger capacity device). interestingly the test program worked on an STM32F407 Cortex-M4 but I'll have to check it again in more detail if important.

Ubuntu 13.04 with gcc-arm-none-eabi version 4-7-2013q3-0raring7

gdb is the one provided with the toolchain, using Black Magic Probe for JTAG/SWD.

I have tried other applications I've written and they also segfault, but I haven't yet tracked down where (backtrace unhelpful).

Please let me know if anything else is needed.

cheers, Ken

Question information

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

Rule No.1: feel free to report any suspicious things without feeling guilty, because the discussion here might help someone sooner or later.

Back to the question, it looks to me that a wrong address is used as the entry address to library function memset. Are you using memset from libraries in our tool chain or from libraries provided by others? Is it possible for you implement a small case for us to reproduce this issue?

Revision history for this message
Terry Guo (terry.guo) said :
#2

Seems like you are linking a ARM-mode library to Thumb-mode applicaiton. Can you please share your final link command? Experience shows it's better to use command like "arm-none-eabi-gcc -mthumb -mcpu=cortex-m3 ..." to perform the link, such command will find correct library automatically.

Revision history for this message
Ken Sarkies (ksarkies) said :
#3

Hi Terry

Thanks for your response. The final link command is, in summary (full command at end of post):

arm-none-eabi-gcc -o freertos-usart-et-stm32f103.elf (object files) -Os -g -Wall -Wextra -I. (other -I's) -fno-common -mthumb -MD -ffunction-sections -fdata-sections -mcpu=cortex-m3 -msoft-float -lc -Tstm32-h103RBT6.ld -nostartfiles -Wl,--gc-sections -march=armv7 -mfix-cortex-m3-ldrd

I can see in there the two switches you mentioned. I gather your concern is that it is picking up wrong libc.a.

A portion of Makefile may be a bit clearer (some options are redundant for linking):

------------------------------------
INCLUDES = $(patsubst %,-I%,$(DRIVERS_INC) $(FREERTOS_INC) $(FREERTOS_DEV))

CFLAGS += -Os -g -Wall -Wextra -I. $(INCLUDES) -fno-common -mthumb -MD
CFLAGS += -DGCC_ARMCM3 -ffunction-sections -fdata-sections
CFLAGS += -mcpu=cortex-m3 -DSTM32F1 -msoft-float

LDSCRIPT = stm32-h103RBT6.ld

LDFLAGS += -I . -lc -T$(LDSCRIPT) -L$(DRIVERS_DIR)/lib -nostartfiles -Wl,--gc-sections -mthumb
LDFLAGS += -march=armv7 -mfix-cortex-m3-ldrd -msoft-float -lopencm3_stm32f1

CFILES = buffer.c $(PROJECT).c rcc.c gpio.c
CFILES += tasks.c list.c queue.c timers.c port.c heap_1.c

OBJS = $(CFILES:.c=.o)

all: $(PROJECT).elf $(PROJECT).bin $(PROJECT).hex $(PROJECT).list $(PROJECT).sym
------------------------------------

I replaced -lc with the full path to the thumb libc. The program now works.

Can you think of a reason why it isn't finding the thumb libc?

Ken

arm-none-eabi-gcc -o freertos-usart-et-stm32f103.elf buffer.o freertos-usart-et-stm32f103.o ../../arm-library/libopencm3-examples/libopencm3/lib/stm32/f1/rcc.o ../../arm-library/libopencm3-examples/libopencm3/lib/stm32/f1/gpio.o tasks.o list.o queue.o timers.o port.o heap_1.o -Os -g -Wall -Wextra -I. -I../../arm-library/libopencm3-examples/libopencm3/include -I../../arm-library/FreeRTOS/Source/include -I../../arm-library/FreeRTOS/Source/portable/GCC/ARM_CM3 -fno-common -mthumb -MD -DGCC_ARMCM3 -ffunction-sections -fdata-sections -mcpu=cortex-m3 -DSTM32F1 -msoft-float -I . -lc -Tstm32-h103RBT6.ld -L../../arm-library/libopencm3-examples/libopencm3/lib -nostartfiles -Wl,--gc-sections -mthumb -march=armv7 -mfix-cortex-m3-ldrd -msoft-float -lopencm3_stm32f1

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

use armv7m instead of armv7. Actually the preferred option is -mcpu=cortex-m3. Then you can remove -march=armv7, which is inaccurate, and remove -mfix-cortex-m3-ldrd, which is included.

s/-march=armv7/-march=armv7m/

Thanks,
Joey

Revision history for this message
Ken Sarkies (ksarkies) said :
#5

Hi Joey

Looks like your suggestion of -march=armv7-m works OK on all my programs.

I really appreciate your help. I was a bit stumped there for a few days.

cheers and thanks, Ken

Revision history for this message
Ken Sarkies (ksarkies) said :
#6

Thanks Joey Ye, that solved my question.

Revision history for this message
Tamas Kleiber (kleiber-tamas) said :
#7

Hi Joey Ye,

 I am a bit confused... may I ask what did you meant by "and remove -mfix-cortex-m3-ldrd, which is included"?

Does that mean that whenever I build something with -mcpu=cortex-m3 the ldrd errata related workaround of GCC is included implicitly?

I am using an MCU which comes with an r2p1 cortex-m3 core which supposedly fixes the ldrd error according to the errata sheet: http://www.ath.bielsko.pl/~kkajstura/mwa/Cortex-M3-Errata-r2p1-v3.pdf

If my understanding is correct and the fix is really included implicitly, is there a way to switch it off?

Thank you in advance for your kind response.

Best regards,
 Tamas

_