我正在准备演示 Qemu 的用户模式(qemu-user 包)qemu-arm。为此,我使用了一个简单的 hello world C 程序
hello.c
:
#include <stdio.h>
int main()
{
printf("Oi, Qemu!\nPrograma C aqui!\n");
}
为了交叉编译它(静态链接),我使用了
gcc-arm-linux-gnueabihf
中的交叉工具链:
$ arm-linux-gnueabihf-gcc --version
arm-linux-gnueabihf-gcc (Ubuntu 9.3.0-10ubuntu1) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ arm-linux-gnueabihf-gcc hello.c -o hello_c_static -static
输出在 qemu-arm、Beaglebone Black 和 PC 上运行。
怎么可能?!
关于编译后的可执行文件:
file hello_c_static
hello_c_static: ELF 32-bit LSB executable, ARM, EABI5 version 1
(GNU/Linux), statically linked,
BuildID[sha1]=6a33aaa5abb9a14fbc0ca4f2e7b432d6fa5d7067, for GNU/Linux 3.2.0,
not stripped
检查
ls -l /proc/sys/fs/binfmt_misc/
,看看您的 x86 系统是否设置为在 ARM 二进制文件上透明地运行 qemu,就像在 Windows 可执行文件或使用 Linux binfmt_misc 的任何东西上运行 WIINE 一样。 https://en.wikipedia.org/wiki/Binfmt_misc
管理指南 https://www.kernel.org/doc/Documentation/admin-guide/binfmt-misc.rst
qemu-user
软件包的安装程序可能已注册其支持的可执行格式。
如果没有 binfmts 寄存器,您将只有
register
和 status
“文件”。在我的系统上,WINE 和 Mono 启动脚本已注册处理程序,因此我还看到 CLR
和 DOSWin
文件。例如
$ cat /proc/sys/fs/binfmt_misc/DOSWin
enabled
interpreter /usr/bin/wine
flags:
offset 0
magic 4d5a
如果存在某种不同的机制,请尝试使用
strace ./some_arm_program
来查看执行它时会发生什么系统调用。
也许还可以在运行时暂停它(control-z)并查看
/proc/$(pidof some_arm_program)/maps
和其他文件。
(最后一节是在OP评论说他们只看到
register
和status
文件,而不是qemu-arm
之后写的,但他们后来改变了他们的评论。看起来 binfmt-support 就是答案。)