我正在尝试为我自己的一个小embedded式设备编译几个程序。 这是一个小端的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
然后我下载了Debian的MIPSEL版本,并在QEMU中运行它。 当我运行上面的检索程序时,我得到:
root@debian-mipsel:/home/user/wansview/devel# ./unzip1 -bash: ./unzip1: No such file or directory
我的理解意味着它不是正确的平台。 固执地我编纂了一个小世界,然后比较ELF和文件信息。 我的hello world在Debian MIPSEL中运行正常,但是在embedded式设备上也No such file or directory
返回No such file or directory
。 这是readelf
和file
输出是惊人的相似,虽然:
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
标志为embedded式设备成功编译?
# 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上编译的二进制文件不能在目标embedded式设备上运行)
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
(在土着Linux上编译的二进制文件将在embedded式设备上运行,它可以运行从设备中检索到的二进制文件,我不满意它,因为它没有编译大型应用程序所需的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
在我的hello世界和在unzip1
aboriginal linux
和debian mipsel
上运行unzip1
的截图。 原住民的Linux运行从设备检索的应用程序就好了,如果我在原住民Linux下编译,我可以在embedded式设备上运行生成的二进制文件。 我对土着人不满意的原因是没有GNU make
和其他大型应用程序的有用工具,并没有简单的方法让他们在那里。
你显然需要一个不同的工具链。 在你的Debian-mipsel上,当你的目标使用uClibc时,你的工具链使用glibc
。
所以,也许你想使用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
一个诡计预先配置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
在Buildroot menuconfig
完成你的选择,但你也可以save
并exit
。
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机器上运行(感谢binfmt和Qemu ):
$ uname -mo x86_64 GNU/Linux $ ./hello Hello World.
干杯。
也许我错了,但在这种情况下,一个简单的修复(但不是优雅的方式)可能是缺少的文件链接到现有的:
ln -s /lib/libc.so.6 /lib/libc.so.0
就像在这种情况下一样: https : //dev.openwrt.org/ticket/3083