从x86平台交叉编译到MIPS路由器

10
我的最终目标是为旧的Actiontec调制解调器/路由器编译无线工具,以便我可以将其配置为无线以太网桥接。目前,它的无线功能(似乎)由管理大部分Web界面的相同二进制文件控制,但它似乎至少在某些功能上使用了无线工具内部使用的库。
我以前从未交叉编译过不同的CPU体系结构,也不确定需要做什么。我正在尝试使用uClibc,因为它似乎在系统的其他部分中使用,但我不知道如何为调制解调器环境配置buildroot。基于下面proc中的信息,我进行了最佳猜测 配置,但有些东西出了问题,因为只返回0的简单C应用程序编译失败了。
# cat /proc/version 
Linux version 2.4.17_mvl21-malta-mips_fp_le (root@localhost.localdomain) (gcc version 2.95.3 20010315 (release/MontaVista)) #1 Thu Apr 21 18:04:37 PDT 2005
# cat /proc/cpuinfo 
processor               : 0
cpu model               : MIPS 4KEc V4.8
BogoMIPS                : 149.91
wait instruction        : no
microsecond timers      : yes
extra interrupt vector  : yes
hardware watchpoint     : yes
VCED exceptions         : not available
VCEI exceptions         : not available

请在MIPS系统上查找任何二进制应用程序,并对其使用file命令进行执行,然后与我们分享输出结果。 - karlphillip
好的,系统中可执行文件的输出为:ELF 32位LSB可执行文件,MIPS,MIPS-I版本1(SYSV),动态链接(使用共享库),已剥离感谢您迄今为止提供的所有帮助。 - sagenite
可能是使用gcc将C转换为MIPS的方法?的重复问题。 - user2284570
3个回答

10
你说得对,你需要一个合适的 MIPS 工具链来交叉编译你的应用程序,而 Buildroot 可以做到这一点。但你可能需要微调 buildroot 的 menuconfig 选项。根据我的系统上二进制应用程序输出的结果, 你的选项可能会有所不同: ELF 32-bit MSB executable, MIPS, MIPS32 rel2 version 1 (SYSV) 这些是我在 Buildroot 的 menuconfig 中启用的选项:
Target Architecture (mips)  ---> 
Target Architecture Variant (mips 32r2)  --->                                                            
Target ABI (o32)  --->                                                                                   
Target options  --->                                                                                     
Build options  --->   
    (/opt/cross-mips-buildroot) Toolchain and header file location?                                                                                   
Toolchain  --->        
    Toolchain type (Buildroot toolchain)  ---> 
    Kernel Headers (Linux 2.6.34.x kernel headers)  --->
    uClibc C library Version (uClibc 0.9.31.x)  ---> 
    [*] Build/install a shared libgcc?
    [*] Enable compiler tls support       
    [*] Build gdb debugger for the Target
    [*] Build gdb server for the Target
    [*] Build gdb for the Host
        GDB debugger Version (gdb 6.8)  --->
    [*] Enable large file (files > 2 GB) support?
    [*] Enable WCHAR support
    [*] Use software floating point by default
    [*] Enable stack protection support
    [*] Build/install c++ compiler and libstdc++?
    [*] Include target utils in cross toolchain  
Package Selection for the target  --->   
    [*] BusyBox
    [*]   Run BusyBox's own full installation
    Libraries  ---> 
        Networking  ---> 
            [*] libcurl
        Text and terminal handling  ---> 
            [*] icu
            -*- ncurses    
Target filesystem options  --->                                                                          
Bootloaders  --->                                                                                        
Kernel  --->

工具链本身安装在/opt/cross-mips-buildroot下。您可以在/opt/cross-mips-buildroot/usr/bin/中找到编译器和其他工具。

尝试编译一个简单的hello world应用程序,并查看是否可以在mips系统中运行。

注意:此配置不会构建C ++编译器。如果需要,您可以使用grep LIBSTDCPP .config检查它是否启用,并根据需要更改它。然后使用make menuconfig使其生效。


@nightfly19 你可能需要更改目标架构变体,因为你的是发布1,而不是2。 - karlphillip
1
谢谢!你提供了我需要让工具链正常工作的所有帮助。我的配置也被证明是小端,对于那些遇到类似情况的人来说,架构需要是mipsel。 - sagenite

1

0

请随意查看dockcross项目。他们提供各种架构的docker容器作为交叉工具链。

个人而言,我更喜欢保持我的主机系统尽可能干净,所以这是一个完美的选择。 要运行一个简单的hello world示例,请按照README.rst中的步骤进行。

MIPS上的HelloWorld.c

但是,请查看我为Netgear N600 wndr3700v2路由器运行DD-WRT的hello world编译。(我已经链接了openWRT维基页面而不是dd-wrt,更喜欢这个)。

检查路由器使用的架构,请相信维基页面或只需通过ssh/telnet连接并运行uname -a命令。

root@DD-WRT:~# uname -a
Linux DD-WRT 3.10.108-d10 #63184 Tue Nov 3 05:20:50 +03 2020 mips DD-WRT

因此,我们可以从DockerHub拉取MIPS容器:

# pull dockcross container for mips
# repo: dockerhub -> https://hub.docker.com/r/dockcross/linux-mips
user@x86-host:~# docker pull dockcross/linux-mips:latest

# check if everything went correct
user@x86-host:~# docker images
dockcross/linux-mips   latest    cf6e2d5003c8   3 years ago    1.03GB

# create dockcross runner
user@x86-host:~# docker run --rm dockcross/linux-mips > ./dockercross-mips
user@x86-host:~# chmod +x ./dockercross-mips
# this will create a dockercross runner script in the current directory

让我们创建一个名为helloWorld的简单项目文件夹,并将一些代码放入其中。

# helloWorld.c
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    printf("Hello World from dockercrossMips\n");
    return EXIT_SUCCESS;
}             

现在我们可以使用 dockcross 进行编译。
# check if all files exists
user@x86-host:~# ll
total 12K
-rwxr-xr-x 1 user user 5.5K Feb 12 19:22 dockercross-mips
-rw-r--r-- 1 user user  151 Feb 12 18:51 helloWorld.c

# compile source into ELF
user@x86-host:~# ./dockercross-mips bash -c '$CC ./helloWorld.c -o helloWorld'

# check ELF file -> should show the proper type and machine
user@x86-host:~# readelf -h helloWorld 
ELF Header:
  Magic:   7f 45 4c 46 01 02 01 00 01 00 00 00 00 00 00 00 
  Class:                             ELF32
  ...
  OS/ABI:                            UNIX - System V
  ABI Version:                       1
  Type:                              EXEC (Executable file)
  Machine:                           MIPS R3000
  ...

现在我们已经准备好传输并运行您的helloWorld可执行文件。

# copy via scp, use your favorite method 
user@x86-host:~# scp helloWorld root@192.168.0.2:/tmp/root/

# run it
root@DD-WRT:~# ./helloWorld 
# if you get some error like this one: -sh: ./helloWorld: not found
# please just start it via your loader
root@DD-WRT:~# /lib/ld-musl-mips-sf.so.1 helloWorld 
# and you should see the desire output.
Hello World from dockercrossMips

如果您不知道加载程序的位置,请使用file命令。如果该命令不可用,请查看entware项目。这里是官方的dd-wrt安装教程here


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