相同(几乎)ELF头但可执行文件不会在彼此的系统上运行

Jui*_*icy 10 c linux embedded mips

我正在尝试为我拥有的一个小型嵌入式设备编译几个程序.它是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
Run Code Online (Sandbox Code Playgroud)

然后我下载了MIPSEL版本的Debian,我在QEMU中运行它.当我运行上面检索的程序时,我得到:

root@debian-mipsel:/home/user/wansview/devel# ./unzip1
-bash: ./unzip1: No such file or directory
Run Code Online (Sandbox Code Playgroud)

据我所知,这意味着它不是正确的平台.尽管如此,我仍然编译了一个小小的hello世界来比较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
Run Code Online (Sandbox Code Playgroud)

我试图更好地理解我的两个系统如何不同以及为什么可执行文件不会同时运行.我是否可以添加任何标志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
Run Code Online (Sandbox Code Playgroud)

有关更多信息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
Run Code Online (Sandbox Code Playgroud)

有关更多信息Aboriginal Linux Mipsel

(在原住民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
Run Code Online (Sandbox Code Playgroud)

LDD

这是一个ldd针对我的hello世界的反对截图,并反对unzip1两者aboriginal linuxdebian mipsel.原住民Linux运行从设备检索的应用程序就好了,如果我在原住民Linux下编译,我可以在嵌入式设备上运行生成的二进制文件.我不满足于原住民的原因是没有GNU make和其他有用的工具用于大型应用程序,并没有简单的方法来实现它们.

在此输入图像描述

lev*_*vif 13

你显然需要一个不同的工具链.在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
Run Code Online (Sandbox Code Playgroud)

一个手法,预先配置mipsel,R1没有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
Run Code Online (Sandbox Code Playgroud)

在Buildroot中完成您的选择menuconfig,但您也可以使用save和保持这样exit.

make menuconfig   # tweak options at your will,
make -j8          # takes 8 minutes on my machine
Run Code Online (Sandbox Code Playgroud)

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

一个真实的例子:

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

并使用全新的uClibc GCC编译器进行编译

output/host/usr/bin/mipsel-buildroot-linux-uclibc-gcc -o hello hello.c
Run Code Online (Sandbox Code Playgroud)

一瞥该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
Run Code Online (Sandbox Code Playgroud)

它完成了工具链并编译您的程序.现在你有时间:-)看看Buildroot提供了什么:output/target/在许多架构中为嵌入式系统提供完整的分发(in ).

编辑:更好的执行机会

您可以静态链接您的程序,以便最大限度地在任何目标上运行代码.

$ 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
Run Code Online (Sandbox Code Playgroud)

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

$ uname -mo   
x86_64 GNU/Linux
$ ./hello
Hello World.
Run Code Online (Sandbox Code Playgroud)

干杯.