几乎相同的ELF头文件,但可执行文件在另一个系统上无法运行。

12

我正在尝试为我拥有的一个小型嵌入式设备编译几个程序。它是一个Little-endian MIPS(mipsel)处理器。我通过telnet和内置的ftp客户端从中检索了这个可执行文件:

root@debian-mipsel:/home/user/wansview/devel# readelf -h unzip1 
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:                           MIPS R3000
  Version:                           0x1
  Entry point address:               0x401cc0
  Start of program headers:          52 (bytes into file)
  Start of section headers:          169960 (bytes into file)
  Flags:                             0x10001007, noreorder, pic, cpic, o32, mips2
  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:         24
  Section header string table index: 23

root@debian-mipsel:/home/user/wansview/devel# file unzip1
unzip1: ELF 32-bit LSB executable, MIPS, MIPS-II version 1 (SYSV), dynamically linked (uses shared libs), stripped

我随后下载了MIPSEL版本的Debian并在QEMU中运行它。当我运行上面检索到的程序时,会得到以下输出:

root@debian-mipsel:/home/user/wansview/devel# ./unzip1
-bash: ./unzip1: No such file or directory

我了解它的意思是这不是正确的平台。尽管如此,我还是编译了一个小的hello world程序来比较ELF和文件信息。我的hello world程序在Debian MIPSEL上可以正常运行,但在嵌入式设备上返回“No such file or directory”。然而,它的readelffile输出非常相似:

root@debian-mipsel:/home/user/wansview/devel# readelf -h hello
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:                           MIPS R3000
  Version:                           0x1
  Entry point address:               0x400740
  Start of program headers:          52 (bytes into file)
  Start of section headers:          3652 (bytes into file)
  Flags:                             0x10001005, noreorder, cpic, o32, mips2
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         10
  Size of section headers:           40 (bytes)
  Number of section headers:         36
  Section header string table index: 35

root@debian-mipsel:/home/user/wansview/devel# file hello
hello: ELF 32-bit LSB executable, MIPS, MIPS-II version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.26, BuildID[sha1]=0xeb3877062337a3dfd15cc09305691685ac0e8c57, with unknown capability 0xf41 = 0x756e6700, with unknown capability 0x70100 = 0x1040000, stripped

我想更好地理解我的两个系统之间的差异,以及为什么可执行文件不能在两个系统上运行。有没有任何标志可以添加到gcc中,以成功地编译嵌入式设备?

关于设备的更多信息

# cat /proc/cpuinfo
system type             : Ralink SoC
processor               : 0
cpu model               : MIPS 24K V4.12
BogoMIPS                : 239.10
wait instruction        : yes
microsecond timers      : yes
tlb_entries             : 32
extra interrupt vector  : yes
hardware watchpoint     : yes
ASEs implemented        : mips16 dsp
VCED exceptions         : not available
VCEI exceptions         : not available

关于Debian MIPSEL的更多信息

(在debian-mipsel上编译的二进制文件无法在目标嵌入式设备上运行)

root@debian-mipsel:/home/user/wansview/devel# cat /proc/cpuinfo 
system type     : MIPS Malta
processor       : 0
cpu model       : MIPS 24Kc V0.0  FPU V0.0
BogoMIPS        : 1038.33
wait instruction    : yes
microsecond timers  : yes
tlb_entries     : 16
extra interrupt vector  : yes
hardware watchpoint : yes, count: 1, address/irw mask: [0x0ff8]
ASEs implemented    : mips16
shadow register sets    : 1
kscratch registers  : 0
core            : 0
VCED exceptions     : not available
VCEI exceptions     : not available

关于Aboriginal Linux Mipsel的更多信息

(在Aboriginal Linux上编译的二进制文件可以在嵌入式设备上运行,并且可以运行从设备检索出来的二进制文件。我对此并不满意,因为它缺少我编译较大应用程序所需的make和其他工具)

(mipsel:1) /home/wansview # cat /proc/cpuinfo 
system type     : MIPS Malta
machine         : Unknown
processor       : 0
cpu model       : MIPS 24Kc V0.0  FPU V0.0
BogoMIPS        : 1013.76
wait instruction    : yes
microsecond timers  : yes
tlb_entries     : 16
extra interrupt vector  : yes
hardware watchpoint : yes, count: 1, address/irw mask: [0x0ff8]
isa         : mips1 mips2 mips32r1 mips32r2
ASEs implemented    : mips16
shadow register sets    : 1
kscratch registers  : 0
core            : 0
VCED exceptions     : not available
VCEI exceptions     : not available

LDD

这里是运行 ldd 命令对比我的hello world和unzip1在aboriginal linux和debian mipsel上的截图。从设备中检索的应用在Aboriginal Linux上可以正常运行,如果我在Aboriginal Linux下编译则可以在嵌入式设备上运行生成的二进制文件。我不满意Aboriginal Linux的原因是它没有GNU make 和其他更适用于大型应用的有用工具,并且没有简单的方法将它们安装到此设备上。

图片描述


4
“没有这样的文件或目录”不是ABI不正确时的错误信息。你确定吗?另外,我注意到您在使用readelf和file命令时使用了“unzip1”,但在运行时使用了“unzip”。 - srd
@srd 抱歉打错字了,我可以确认我正在尝试运行的是同一个文件,并且这正是我遇到的错误。 - Juicy
一个使用某种libc类型编译的程序,除非该libc存在于目标系统上(即使出现错误,也会类似于“ld:无法找到libxxx.so”),否则不会执行。您可以使用ldd轻松查看是否是这种情况(原始映像上有它)。请检查两个设备是否具有相同的libc(和相关库),并且在两种情况下ldd都不报错。 - srd
@srd,我已经更新了ldd输出的截图。你似乎是对的,其中一个正在寻找libc.so.6,另一个正在寻找libc.so.0。有什么解决方案吗? - Juicy
看起来这是工具链和运行时libc不匹配的问题。我不确定为什么会出现“没有这个文件或目录”的消息,但您必须使用与目标上的运行时环境匹配的工具链。 - srd
2个回答

14

显然你需要一个不同的工具链。在你的Debian-mipsel上,你的工具链使用glibc,而你的目标使用uClibc

因此,也许你想使用Buildroot自行生成:

wget http://buildroot.uclibc.org/downloads/buildroot-2014.11.tar.gz
tar zxf http://buildroot.uclibc.org/downloads/buildroot-2014.11.tar.gz
cd buildroot-2014.11

预配置 mipselR1 没有 soft-float 的技巧(我的意愿,请检查您的):

cat <<_EOF > .config
BR2_HAVE_DOT_CONFIG=y
BR2_mipsel=y
BR2_ARCH="mipsel"
BR2_ENDIAN="LITTLE"
BR2_GCC_TARGET_ARCH="mips32"
BR2_GCC_TARGET_ABI="32"
BR2_ARCH_HAS_ATOMICS=y
BR2_mips_32=y
# BR2_MIPS_SOFT_FLOAT is not set
BR2_MIPS_OABI32=y
_EOF

在 Buildroot 的 menuconfig 中完成你的选择,但你也可以使用 saveexit 保留这种设置。

make menuconfig   # tweak options at your will,
make -j8          # takes 8 minutes on my machine

然后,您的编译器可以在 ./output/host/usr/bin 中找到。

一个实际的例子:

echo '#include <stdio.h>                                                                                                                                                                   
 int main(int argc, char* argv[]) {
     printf("Hello World.\n");
     return 0;
 }' > hello.c

使用全新的 uClibc GCC 编译器编译它

output/host/usr/bin/mipsel-buildroot-linux-uclibc-gcc -o hello hello.c

了解 hello 程序的一瞥:(没时间修复我的 ldd...)

$ file hello
hello: ELF 32-bit LSB executable, MIPS, MIPS32 version 1 (SYSV), dynamically linked (uses shared libs), not stripped
$ strings hello | grep "lib.*so*"
/lib/ld-uClibc.so.0
libgcc_s.so.1
libc.so.0

使用工具链编译您的程序,现在您有时间了 :-) 看看Buildroot提供了什么:

针对多种体系结构的嵌入式系统的完整分发(在output/target/中)。

编辑:更好地执行的机会

您可以静态链接您的程序,以最大化在任何目标上运行代码的机会。

$ output/host/usr/bin/mipsel-linux-gcc -Wall -o hello -static hello.c
$ file ./hello
./hello: ELF 32-bit LSB executable, MIPS, MIPS32 version 1 (SYSV), dynamically linked (uses shared libs), not stripped

现在,由于这个静态版本不再依赖任何外部库(只有 uClibc 在此),这个 MIPS 可执行文件甚至可以在我的 x86_64 机器上运行(感谢 binfmtQemu):

$ uname -mo   
x86_64 GNU/Linux
$ ./hello
Hello World.

干杯。


不错的回答,但是当你为MIPS编译时,这个可执行文件如何在x86系统上运行呢?如果我理解正确,这些是不同的架构 - 不同的CPU指令。或者你的系统默认调用qemu来运行MIPS可执行文件吗? - redbeam_
你说得对,我安装了许多(21个)Qemu架构 + Java支持 + Python + Wine。实际上,Debian软件包已经为我完成了这项工作。简而言之,有特殊的文件描述魔术数字(长达128字节)及其关联的文件解释器。请注意,shebang #! 直接由Linux内核中的 binfmt 处理 fs/binfmt_script.c - levif
我明白了。我建议进行一些小的编辑,以免其他用户在看到这个答案时感到困惑。 - redbeam_

0
也许我是错了,但在这种情况下一个简单但不太优美的解决方法可能是将缺失的文件链接到现有文件中:
ln -s /lib/libc.so.6 /lib/libc.so.0

就像在这种情况下: https://dev.openwrt.org/ticket/3083


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接