1

I compiled a #include<stdio.h> int main() { printf("hello world\n"); return 0; } program with riscv64-unknown-linux-gnu-gcc ./test.c -o test on my x86_64-Ubuntu22.04 host, then I put this executable into the shared folder mnt. When I execute ./test I just get -/bin/sh: ./test: not found from terminal. Isn't it just a piece of binary code without shared lib?

My options of qemu startup are qemu-system-riscv64 -nographic -m 256M -machine virt -kernel ~/linux-kernel/arch/riscv/boot/Image -append \"root=/dev/vda rw console=ttyS0\" -drive file=rootfs.img,format=raw,id=hd0 -device virtio-blk-device,drive=hd0 -fsdev local,security_model=passthrough,id=fsdev0,path=./host_mnt -device virtio-9p pci,id=fs0,fsdev=fsdev0,mount_tag=hostshare. And I create my ext4 rootfs just with dd and busybox. Here are the basic commands I used.

$ qemu-img create rootfs.img  8g
$ mkfs.ext4 rootfs.img
$ mkdir rootfs
$ sudo mount -o loop rootfs.img  rootfs
$ cd rootfs
$ sudo cp -r ../busyboxsource/_install/* .
$ sudo mkdir proc sys dev etc etc/init.d
$ cd etc/init.d/
$ sudo touch rcS
$ sudo vi rcS
    #!/bin/sh
    mount -t proc none /proc
    mount -t sysfs none /sys
    /sbin/mdev -s
    mount -t 9p -o trans=virtio,version=9p2000.L hostshare /mnt
$ sudo chmod +x rcS
$ sudo umount rootfs
(creating rootfs finished)

And the versions of riscv64-unknown-linux-gnu-gcc, QEMU and linux kernel(in qemu) are:

riscv64-unknown-linux-gnu-gcc (g2ee5e430018) 12.2.0
QEMU emulator version 8.0.0 (v8.0.0)
v6.0.0

I am confused why the elf compiled on host can not be executed inside qemu. Is it because my rootfs is created with busybox and not based on, say, ubuntu rootfs, or something else? I can offer more information about my environment if needed. I'd appreciate it if anyone can explain this for me.

Thanks!

I tried ldd test on host and got not a dynamic executable.

I will try to build rootfs based on ubuntu if this is the reason.

beyond
  • 11
  • 2
  • gcc usually creates dynamically linked executables by default. What happens if you compile with `-static`? I'm not sure that the `ldd` output of "not a dynamic executable" is meaningful, since that is the ldd for a different architecture. – Nate Eldredge Jul 20 '23 at 16:30
  • You also made sure that you preserved the execute permissions on `test`? Check with `ls -l test` inside the guest. – Nate Eldredge Jul 20 '23 at 16:31
  • Yes! I just got home and turned on my computer to try `-static` option(I saw it from somewhere before but overlooked it). And it indeed works. But is it a good and lasting solution? If not, what should I do? Thanks for your reply! – beyond Jul 20 '23 at 16:55
  • Indeed building the root filesystem based on debian/ubuntu will be better. – beyond Jul 22 '23 at 13:09

0 Answers0