【问题标题】:Hello world cross compiled for ARM is runing on both x86_64 and ARMARM 的 Hello World 交叉编译正在 x86_64 和 ARM 上运行
【发布时间】:2020-08-15 12:23:59
【问题描述】:

我正在准备用户模式 ​​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

【问题讨论】:

    标签: linux x86 arm cross-compiling qemu


    【解决方案1】:

    检查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 寄存器,您将只有 registerstatus“文件”。在我的系统上,WINE 和 Mono 引导脚本已注册处理程序,因此我还看到了 CLRDOSWin 文件。例如

    $ 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 评论说他们只看到 registerstatus 文件,而不是 qemu-arm 之后编写的,但他们已经改变了他们的评论。看起来 binfmt-support 是答案。 )

    【讨论】:

    • 看来你是对的!那里有一个qemu-arm!我该如何撤消此操作?也许只是从那里删除这个文件?
    • @griloHBG:当然,这可能只适用于一次启动;如果不阅读文档。您可能可以使用 systemd 命令或编辑配置文件以阻止它在以后的引导时重新注册。或者可能是echo disabled | sudo tee /proc/sys/fs/binfmt_misc/status - 在我的系统上,文件当前读取为enabled。我认为这是一个全局启用/禁用。
    • 你是对的。而且,实际上,要禁用它,必须使用0,所以命令是echo 0 | sudo tee /proc/sys/fs/binfmt_misc/status。此外,禁用它只会持续到重新启动。最后,非常感谢!
    • @griloHBG:对,0 更有意义。当我输入它时,Disabled 感觉像是一个糟糕的猜测。 /proc/sys 不存在于磁盘上,它只是内核公开配置接口的一种方式。手动更改 /sys/proc/sys never 中的内容在重启后仍然存在;它只是反映了初始化脚本/systemd/其他在启动时所做的默认+修改。 (通常基于 /etc 中的配置文件)
    猜你喜欢
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    • 2014-06-13
    • 1970-01-01
    相关资源
    最近更新 更多