3 Replies Latest reply on Jul 25, 2019 2:34 PM by ilg

    On QEMU, return code from main?




      I'm using GNU MCU QEMU (v2.8.0-6-20190517-1329) for unittesting purposes and I'm having trouble getting it to turn 1 (or any nonzero code) through the semihosting interface. I'm currently using rdimon for semihosting support. I've had a look at https://gnu-mcu-eclipse.github.io/qemu/options/ for information on exit(int) but clearly I'm still missing something.


      Starting with a trivial example project's main.c:

      #include <stdlib.h>

      #include <stdio.h>


      int main() {

          printf("Hello World!\n");

          return 1;


      And compiling with (sysinit is a dummy file to provide the SystemInit symbol in the startup code, startup code is modified to branch to "exit" when main returns instead of infinite loop, and qemu_mem.ld is ST's linker file with the __end__ symbol added to satisfy the linker):

      arm-none-eabi-gcc -o test -mthumb -mcpu=cortex-m4 -g --specs=rdimon.specs --specs=nano.specs -T qemu_mem.ld -Wl,-lrdimon main.c startup_stm32f407xx.s sysinit.c


      Then running with:

      qemu-system-gnuarmeclipse --nographic --mcu STM32F407VG --verbose --verbose --gdb tcp::1234 -d unimp,guest_errors --semihosting-config enable=on,target=native --image test --semihosting-cmdline test; echo $?


      I get:

      Hello World!

      QEMU semihosting exit(0)



      I'm thinking my semihosting is at least partially working because I can get output through printf, but I'm clearly missing something after that. When instruction stepping through main as it returns and then calls exit, I can see the return code of 1 appear in $r0, but it doesn't propagate. Any suggestions would be much appreciated.