Search code examples
armqemucortex-m

Compiling and running ARM assembly binary on Cortex-M4 (simulated in QEMU)


I successfully compiled and executed ARM binary file on a virtual QEMU embedded system connex using this procedure:

arm-none-eabi-as -o program.o program.s
arm-none-eabi-ld -Ttext=0x0 -o program.elf program.o  
arm-none-eabi-objcopy -O binary program.elf program.bin
dd if=/dev/zero of=flash.bin bs=4096 count=4096
dd if=program.bin of=flash.bin bs=4096 conv=notrunc
qemu-system-arm -M connex -pflash flash.bin -nographic -serial /dev/null

In line four I created a zeroed out empty disk which represents flash and in line five I copied my binary into flash.

So this works like a charm, but it simulates an entire embedded system while I only want to simulate ARM core, for example Cortex-M4. This is why I am trying to just use qemu-arm instead of qemu-system-arm.

So I 1st tried to compile and run my program like this (lines 1-3 are same as above):

arm-none-eabi-as -o program.o program.s
arm-none-eabi-ld -Ttext=0x0 -o program.elf program.o  
arm-none-eabi-objcopy -O binary program.elf program.bin
qemu-arm -cpu cortex-m4 program.bin

And this doesn't work - it says:

Error while loading program.bin: Exec format error

So I tried to create flash image like before (because it worked):

arm-none-eabi-as -o program.o program.s
arm-none-eabi-ld -Ttext=0x0 -o program.elf program.o  
arm-none-eabi-objcopy -O binary program.elf program.bin
dd if=/dev/zero of=flash.bin bs=4096 count=4096
dd if=program.bin of=flash.bin bs=4096 conv=notrunc
qemu-arm -cpu cortex-m4 flash.bin

And I get this:

Error while loading flash.bin: Permission denied

Can anyone help me a bit? Using sudo doesn't help.


Solution

  • qemu-arm's purpose is not "simulate just an ARM core". It is "run a single Linux binary", and it expects that the binary file you provide it is a Linux format ELF executable. Trying to feed it something else is not going to work.

    Since Linux assumes A-profile cores, not M-profile cores, anything you do with -cpu cortex-m4 on qemu-arm will only be working by luck, not deliberately. (We don't disable those CPU types since there are some GCC test case scenarios that use semihosting which sort-of-work and which we don't want to deliberately break. But those are working as much by luck as anything else.)