Emulate ARM Cortex-M4 with qemu

Viewed 1326

I have a program consisting of multiple C source files and I would like to execute it in qemu with a Cortex-M4/M7 CPU. I am using the following command to build the objects:

"arm-none-eabi-gcc" -c -mcpu=cortex-m4 -mthumb -fomit-frame-pointer -Wimplicit-fallthrough -O3 -Wall -Wshadow -Wcast-qual -Wno-long-long -Wno-unknown-pragmas

The linker creates a library:

arm-none-eabi-ar cru lib *.o

And the main is then linked to an executable:

"arm-none-eabi-gcc" -mthumb -specs=rdimon.specs main.o lib.a  -lstdc++ -lm -L./lib/arm_none-eabi_gcc/  -o run_main

Now running this binary gives me:

qemu-arm -cpu cortex-m4 run_main
qemu: uncaught target signal 4 (Illegal instruction) - core dumped
Illegal instruction

It all works well when using Cortex-A9 as CPU, but the M-Models are troubling me. What am I doing wrong?

1 Answers

ARM Cortex-M MCUs always require custom linker script and startup code. Flash and RAM addresses are not standardized across vendors, and the interrupt vector table is device specific.

The linker command line should also specify the core via "-mcpu=cortex-m4" in order to link to the correct libraries (multilib).

Related