Cross compiling for MIPS router from x86
You are right, you need a proper mips toolchain to cross-compile your application and Buildroot can do that. But you may need to tweak buildroot's menuconfig options. Depending on the output of file
, your options may change. On my system, binary apps inform the following:
ELF 32-bit MSB executable, MIPS, MIPS32 rel2 version 1 (SYSV)
These are the options I have enabled for my Buildroot's 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 --->
The toolchain itself is installed at /opt/cross-mips-buildroot. You can find the compiler and other tools on /opt/cross-mips-buildroot/usr/bin/
Try to compile a simple hello world application and see if you can run it inside the mips system.
Note: this configuration will not build a C++ compiler. If you need it, you can grep LIBSTDCPP .config
and check if it's enable or not and change it to your likes. Then make menuconfig
to make it happen.
please feel free to look into dockcross project. They offer cross tool-chains as docker containers for various architectures.
Personally, I prefer to keep my host system as clean as possible, so this is a perfect match for me.To get a simple hello world example up and running please just follow the steps from the README.rst.
HelloWorld.c on MIPS
However, please checkout my hello world compilation for a Netgear N600 wndr3700v2 router running DD-WRT. (I have linked the openWRT wiki page instead of the dd-wrt, prefer this one).
Check which arch is used on the router, please trust the wiki pages or just connect via ssh/telnet and run uname -a command.
root@DD-WRT:~# uname -aLinux DD-WRT 3.10.108-d10 #63184 Tue Nov 3 05:20:50 +03 2020 mips DD-WRT
So we can pull the mips container from dockerhub:
# pull dockcross container for mips# repo: dockerhub -> https://hub.docker.com/r/dockcross/linux-mipsuser@x86-host:~# docker pull dockcross/linux-mips:latest# check if everything went correctuser@x86-host:~# docker imagesdockcross/linux-mips latest cf6e2d5003c8 3 years ago 1.03GB# create dockcross runneruser@x86-host:~# docker run --rm dockcross/linux-mips > ./dockercross-mipsuser@x86-host:~# chmod +x ./dockercross-mips# this will create a dockercross runner script in the current directory
Let's create a simple project folder called helloWorld
and but some code into it.
# helloWorld.c#include <stdio.h>#include <stdlib.h>int main(int argc, char *argv[]){ printf("Hello World from dockercrossMips\n"); return EXIT_SUCCESS;}
Now we can compile it with dockcross
.
# check if all files existsuser@x86-host:~# lltotal 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 ELFuser@x86-host:~# ./dockercross-mips bash -c '$CC ./helloWorld.c -o helloWorld'# check ELF file -> should show the proper type and machineuser@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 ...
Now we are ready to transfer and run your helloWorld
executable.
# copy via scp, use your favorite method user@x86-host:~# scp helloWorld root@192.168.0.2:/tmp/root/# run itroot@DD-WRT:~# ./helloWorld # if you get some error like this one: -sh: ./helloWorld: not found# please just start it via your loaderroot@DD-WRT:~# /lib/ld-musl-mips-sf.so.1 helloWorld # and you should see the desire output.Hello World from dockercrossMips
In case you do not know where your loader is located, please use file command. In case the command is not available, please checkout entware project.Here would be the official dd-wrt install tut here