
就一个简单的 c 程序:
#include <stdio.h> int main() { printf("Hello, World\n"); } 使用如下命令编译:
riscv64-linux-gnu-gcc --static hello.c -o hello_riscv64 居然可以直接运行
> ./hello_riscv64 Hello, World 使用 qemu 也能运行:
> qemu-riscv64-static hello_riscv64 Hello, World readelf 结果:
> readelf -h hello_riscv64 ELF Header: Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 Class: ELF64 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: EXEC (Executable file) Machine: RISC-V Version: 0x1 Entry point address: 0x1054c Start of program headers: 64 (bytes into file) Start of section headers: 501880 (bytes into file) Flags: 0x5, RVC, double-float ABI Size of this header: 64 (bytes) Size of program headers: 56 (bytes) Number of program headers: 7 Size of section headers: 64 (bytes) Number of section headers: 28 Section header string table index: 27 1 koebehshian 2024-04-04 18:13:35 +08:00 又不是裸机,操作系统解释了吧 |
2 SteveLee 2024-04-04 18:21:30 +08:00 via iPhone 可能 binfmt_misc 自动调用 qemu 了,可以 ls /proc/sys/fs/binfmt_misc/ 看下 |
3 rookiemaster OP @SteveLee 谢谢,我使用 strace 命令观察系统调用(strace ./hello_riscv64),发了一个系统调用为 readlink("/proc/self/exe", "/usr/bin/qemu-riscv64-static", 4096) = 28 。 但是不知道是什么意思 |
4 anntt 2024-04-04 18:36:22 +08:00 via iPhone 亲测不能运行,不知道题主那里的问题 |
5 rookiemaster OP @anntt 我试试在没有装 qemu 的机子上试试 |
6 pagxir 2024-04-04 18:54:37 +08:00 via Android 因为是用 qemu-user 解释执行了。效率很低,arm64/arm 一样可以解释执行。 |
7 peachpeach 2024-04-04 19:04:07 +08:00 你可以理解 qemu 就是个虚拟机, 虚拟各种 CPU 架构, 甚至可以虚拟嵌入式开发板 |
8 rookiemaster OP 应该是 qemu 会自动执行,把 qemu 卸载了就会正常报错了 |
9 jim9606 2024-04-05 00:28:59 +08:00 可能是利用了内核的 binfmt_misc 机制自动调用 qemu 运行了。 |