为什么我静态编译的 BusyBox ARM 二进制文件可以在我的 x86_64 PC 上运行?

Nic*_*oux 1 busybox cross-compilation

那里的情况有点奇怪。我为 ARM 平台(32 位)编译了 BusyBox 1.32.1 的静态可执行文件,奇怪的是它在两个平台上都没有问题。自己看:

root@smallbuntu /m/n/b/i/n/rootfs# readelf -h bin/busybox-initrd
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           ARM
  Version:                           0x1
  Entry point address:               0x1f419
  Start of program headers:          52 (bytes into file)
  Start of section headers:          1482800 (bytes into file)
  Flags:                             0x5000002, Version5 EABI, <unknown>
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         6
  Size of section headers:           40 (bytes)
  Number of section headers:         27
  Section header string table index: 26
root@smallbuntu /m/n/b/i/n/rootfs# 
Run Code Online (Sandbox Code Playgroud)

然后:

root@smallbuntu /m/n/b/i/n/rootfs# bin/busybox-initrd ls
bin         home        media       proc        srv         var
boot        kobo        mnt         root        sys
dev         lib         modules     run         tmp
etc         lost+found  opt         sbin        usr
root@smallbuntu /m/n/b/i/n/rootfs# bin/busybox-initrd uname -a
Linux smallbuntu 5.8.0-50-generic #56-Ubuntu SMP Mon Apr 12 17:18:36 UTC 2021 armv7l GNU/Linux
root@smallbuntu /m/n/b/i/n/rootfs# 
Run Code Online (Sandbox Code Playgroud)

uname -a似乎armv7l由 busybox 二进制文件返回。这是正常的输出:

Linux smallbuntu 5.8.0-50-generic #56-Ubuntu SMP Mon Apr 12 17:18:36 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
Run Code Online (Sandbox Code Playgroud)

知道发生了什么吗?
谢谢!

Ste*_*itt 5

如果您查看内部/proc/sys/fs/binfmt_misc,您可能会看到一个名为qemu-arm或类似的文件,其内容类似于

enabled
interpreter /usr/bin/qemu-arm-static
flags: OCF
offset 0
magic 7f454c4601010100000000000000000002002800
mask ffffffffffffff00fffffffffffffffffeffffff
Run Code Online (Sandbox Code Playgroud)

这指示内核使用/usr/bin/qemu-arm-static. 这允许它使用 QEMU 来模拟 ARM CPU(并修复系统调用以匹配 ARM ABI)并在 QEMU 可以模拟 ARM CPU 的任何系统上透明地运行 ARM 二进制文件,包括您的 64 位 x86 PC。

在您的情况下,由于 ARM 二进制文件是静态链接的,因此不需要额外的设置。动态链接的二进制文件也需要它们的本地库可用。

在基于 Debian 的系统上,包括基于 Ubuntu 的系统,这是由qemu-user-staticbinfmt-support包设置的:该qemu-user-static包注册binfmt_miscQEMU 可以使用 处理的配置update-binfmts,并且该binfmt-support包确保将注册的配置加载到内核中(binfmt_misc是一个内核模块)。

另请参阅Linux 上存在哪些类型的可执行文件?, /proc/sys/fs/binfmt_misc/下的文件允许什么样的可执行格式?,以及Mono 有什么神奇之处?