2

lately I have been trying to execute code on Leon3 processor using qemu. My code looks like this:

#include <stdio.h>
#include <stdint.h>


int main(void) {
    
    
    int a,b,c,d,e;  
    printf("hello world!");
   
    
    
    if(a){
        a++;
    };
    
    if(b){
        b++;
    };
    
    if(c){
        c++;
    };
    
    if(d){
        d++;
    };
    
    
    if(e){
        e++;
    };
    
    
    while(1){
    }
    
    
    
    
   
    return 0;
}

so it's a pretty simple program. I start Qemu with following command:qemu-system-sparc.exe -nographic -M leon3_generic -m 64M -kernel testapp.elf

but I don't get any outupt. Qemu seems to start and immidiately quits, not even hanging on infinite loop. Trying to debug leads to crash(?) with following code: qemu-system-sparc.exe: QEMU: Terminated via GDBstub

I think that maybe i don't undesrtand something about qemu, but I cannot find any help regarding running .elf files.

Maybe anyone have done this and can help me?

Kestrel
  • 29
  • 5
  • 1
    stdio.h? printf? `-kernel`?? The C code looks like a user-space binary employing the standard library. If you try to boot it as an OS kernel, it will not do anything useful. If you want to make your own kernel, you have not done so yet. If you want to run the program as-is, you need to install an OS to run it in first. –  Aug 09 '21 at 11:19
  • Thanks for reply! So loading and .elf file with - kernel is not the right way to execute code? What i was trying to achieve is basically flash this program to memory and run it like in a microcontroller. Is it even possible with qemu? – Kestrel Aug 09 '21 at 11:38
  • With qemu yes, with that program, no. Or do you have a standalone implementation of printf etc. that could be used that way? Where do the prints go? If you do, you may want to elaborate on how you compiled that. –  Aug 09 '21 at 11:47
  • qemu's -kernel option wants a file that is either in Linux kernel or multiboot format. Even with the right executable code you may still need to do some extra steps to package it correctly. –  Aug 09 '21 at 11:50
  • Ok, so there is no "easy" (by easy I mean load and run) approach using qemu. I need a system image even if I only want to run bare C application? – Kestrel Aug 09 '21 at 13:55
  • If you're writing a bare-metal C application you are providing the system image, by definition. -kernel will load an ELF file for sparc targets, but that ELF file has to be prepared to handle being bare-metal. – Peter Maydell Aug 09 '21 at 15:47
  • I have tried loading a binary using objcopy, but I guess it's not the proper way to obtain bare-metal apps. I have also tried -device loader,file=... parameters, to no effect. What should I look up? Most tutorials are about lanuching linux image or launching ARM targets directly from eclipse. – Kestrel Aug 09 '21 at 16:14
  • The posted code will not cleanly compile as it is accessing several variables before those variables have been set to a known value – user3629249 Aug 10 '21 at 15:03
  • regarding: `#include ` `stdint.h contains macros for `int8_t`. etc but your code is not using anything from that header file. – user3629249 Aug 10 '21 at 15:08
  • if you are bare metal you are without a(n operating) system, without an operating system how do you expect printf to work? Have you linked it with a C library and some backend you wrote that connects the C library to the hardware/system so that it sends output to the uart? – old_timer Aug 10 '21 at 22:28
  • The `stdout` stream is buffered, so nothing will be output to the terminal until one of several events occurs. suggest changing:: `printf("hello world!");` to `printf("hello world!\n"):` Note the addition of \n to the end of the output string. and even then your code needs a driver function to handle the printf functionality – user3629249 Aug 11 '21 at 04:06
  • I have managed to get this kind of thing working with QEMU 5.2 and a patch file distributed by Gaisler (makers of leon3) (http://gaisler.org/qemu/qemu-5.2.0-leon3.patch) but not with QEMU 6. – alastairtree Sep 30 '21 at 14:17

1 Answers1

0

OK, so I have found a way to do this.

As Peter mentioned in the comments: Qemu needs "something" more than bare metal .elf file.

I have found that there is a tool MKPROM2 provided by Cobham-Gaisler that takes an .elf file and creates a ROM image that later can be loaded to Qemu with -kernel option.

The ROM image is different from bare metal .elf in a way that ROM image contains some initalization code that allows to run the program we initally wanted to execute.

So finally all that has to be done is running: mkprom2 -freq 20 -romres testapp.elf

This outputs prom.out file that can be loaded to qemu.

Kestrel
  • 29
  • 5