blx instruction on cortex-m3

Asked by soul

I would like to use float library in cortex-m3 cible.
When i compile and link a blx immediate instruction is generated.
This instruction generates a hardware fault.
Here is a beggining of my function
   30014: b570 push {r4, r5, r6, lr}
   30016: 4d0f ldr r5, [pc, #60] ; (30054 <cycle_test_Nop+0x40>)
   30018: f04f 517e mov.w r1, #1065353216 ; 0x3f800000
   3001c: 682c ldr r4, [r5, #0]
   3001e: 4606 mov r6, r0
   30020: 4620 mov r0, r4
   30022: f000 ebf6 blx 30810 <__addsf3>
   30026: 6028 str r0, [r5, #0]
   30028: 4620 mov r0, r4
   3002a: f000 ed42 blx 30ab0 <__aeabi_f2uiz>
   3002e: 07c3 lsls r3, r0, #31
...

chip = LPC1768
CFLAGS = -c -fno-common -mcpu=cortex-m3 -march=armv7-m -mthumb-interwork -mthumb -msoft-float -mfpu=vfp -mfloat-abi=soft
LDFLAGS =-T$(LINKER_SCRIPT) -nostartfiles -Wl,-Map=$(OUTDIR)/$(TARGET).map,--cref -msoft-float -mfpu=vfp -mfloat-abi=soft

I need help ...

Question information

Language:
English Edit question
Status:
Solved
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Solved by:
Terry Guo
Solved:
Last query:
Last reply:
Revision history for this message
Freddie Chopin (freddie-chopin) said :
#1

You're probably linking it wrong - make sure to use g++, not ld directly, to do the linking. And I have no idea what for do you need the interwork flag that is meant to interwork thumb with ARM (which you cannot have)...

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

Hi Soul,

This looks more like a problem related to Multilib. I know nothing about cortex-m3 cible. But the function name __addsf3 in blx instruction comes from library libgcc.a. In this tool chain, we have more than one libgcc.a variants, such as one for ARM mode, one for Thumb mode. For your target cortex-m3, the one for Thumb mode should be linked, then there will be no blx.

 The GCC Multilib automatically assist GCC to choose right libgcc.a based on the command line options. If you have two-steps build i.e. compile source files to object files with arm-none-eabi-gcc and then link them together using arm-none-eabi-ld, you need to specify the right libraries in command line. If you use arm-none-eabi-gcc in second step, you only need to provide correct target options, the arm-none-eabi-gcc will find correct libraries for you.

Revision history for this message
soul (souleyman-sall) said :
#3

Hi Terry,
Thank you very much.
I solve it by loading with the correct libgcc;

EXTRA_LIBDIRS ="C:/GNU Tools ARM Embedded/lib/gcc/arm-none-eabi/4.7.3/armv7e-m/softfp"
EXTRA_LIBS = gcc
LDFLAGS =-T$(LINKER_SCRIPT) -nostartfiles -nostdlib -L$(EXTRA_LIBDIRS) -Wl,-Map=$(OUTDIR)/$(TARGET).map,--cref -msoft-float -mfpu=vfp -mfloat-abi=soft

I link with
arm-none-eabi-gcc $(LDFLAGS) -o $(OUTDIR)/$(TARGET).elf $(ALLOBJ) -l$(EXTRA_LIBS)

Revision history for this message
Freddie Chopin (freddie-chopin) said :
#4

This is not a solution - you should use -mcpu=cortex-m3 and -mthumb flags for linking and you wouldn't need any of these directories stated explicitly...

Revision history for this message
soul (souleyman-sall) said :
#5

ok

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

Although it is marked a solved, I'd like to make it clear that:
1. -mthumb-interwork should never be used for cortex-m, which is the reason why blx is generated
2. Do not hard code GCC library path in in linker option. Instead, rely on GCC multilib mechanism to find it. In this specify example, using armv7e-m for cortex-m3 is dangerous. It might result in program fail to run.

Revision history for this message
soul (souleyman-sall) said :
#7

to summarize:

compilation flag:
CFLAGS = -c -fno-common -O0 -mcpu=cortex-m3 -mthumb -msoft-float -mfloat-abi=soft
link flag:
LDFLAGS = -mcpu=cortex-m3 -mthumb -T$(LINKER_SCRIPT) -nostartfiles -Wl,-Map=$(OUTDIR)/$(TARGET).map,--cref -msoft-float -mfloat-abi=soft

I use arm-none-eabi-gcc for compilation and link.
Then " ... armv7-m\libgcc.a" and "... armv7-m\libc.a" libraries are automaticaly loaded and no hardware fault is generated.