Cross Compile Toolchains
TyeYeah Lv4

Just now I found the site toolchains.bootlin.com to download suitable prebuilt toolchain, it provides detailed GCC/GDB/Linux Header versions.

Before knowing this site, I tried buildroot and crosstool-ng to build my own toolchain, here is to share the experience.

But to be honest the toolchains.bootlin.com seems better, because it built tools for us, and you can choose version of GCC and GDB, while buildroot and crosstool-ng can’t.

Buildroot

Buildroot is a framework to build embedded Linux software on Linux. It is made up of makefile and Kconfig configuration files. L

ike compiling Linux kernel, configure buildroot and edit menuconfig to compile a complete Linux system software (including boot, kernel, rootfs and libs) that can be directly burned and run on the machine.

Download buildroot.

1
2
3
$ wget http://buildroot.uclibc.org/downloads/snapshots/buildroot-snapshot.tar.bz2
$ tar -jxvf buildroot-snapshot.tar.bz2
$ cd buildroot

Configure buildroot.

1
2
3
$ sudo apt-get install libncurses-dev patch
$ make clean
$ make menuconfig

When interface shows up like this,
Target Option
Set Target Architecture as MIPS (little endian) (depending on your target);
Target Architecture
Set Kernel Headers as your host Linux version,because it will run on your pc.
Kernel Headers

Install it.

1
2
3
4
$ sudo apt-get install texinfo
$ sudo apt-get install bison
$ sudo apt-get install flex
$ sudo make

Compiled files will be found in directory output. Cross compile utils will be in buildroot/output/host/usr/bin, and the compiler is mips-linux-gcc.

Configure environment variables and test.

1
2
3
4
5
$ gedit ~/.bashrc
export PATH=$PATH:Path/to/buildroot/output/host/usr/bin
$ source ~/.bashrc

$ mips-linux-gcc -o test test.c -static

Crosstool-NG

You can visit official site for its introduction and documentation.

I installed it by source code

1
2
3
4
5
6
$ git clone https://github.com/crosstool-ng/crosstool-ng
$ ./bootstrap # needed before running ./configure
$ ./configure --prefix=/some/place
$ make
$ make install
$ export PATH="${PATH}:/some/place/bin"

If you are root user, you’ll get

1
2
3
$ ./ct-ng build
[ERROR] You must NOT be root to run crosstool-NG
[00:00] / gmake: *** [ct-ng:262: build] Error 1

To solve it, add things to ./ct-ng

1
2
3
4
5
$ vim ./ct-ng
... # add the following
export CT_EXPERIMENTAL:=y
export CT_ALLOW_BUILD_AS_ROOT:=y
export CT_ALLOW_BUILD_AS_ROOT_SURE:=Y

It can be operated like buildroot

1
2
3
4
$ cd /some/place/bin
$ ls -a
. .. ct-ng
$ ./ct-ng menuconfig

Or a more convenient way, use the templates/samples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
$ ./ct-ng list-samples      # show templates
Status Sample name
[G..X] aarch64-ol7u9-linux-gnu
[G..X] aarch64-rpi3-linux-gnu
[G..X] aarch64-unknown-linux-android
[G..X] aarch64-unknown-linux-gnu
[G..X] aarch64-unknown-linux-uclibc
...
$ ./ct-ng mips-malta-linux-gnu # choose one
CONF mips-malta-linux-gnu
#
# configuration written to .config
#

***********************************************************

Initially reported by: Antony Pavlov
URL:

Comment:
Experimental toolchain for MIPS32r2

***********************************************************

Now configured for "mips-malta-linux-gnu"
$ ./ct-ng build
[INFO ] Performing some trivial sanity checks
[WARN ] Number of open files 1024 may not be sufficient to build the toolchain; increasing to 2048
...
$ ls -a
. .. .build build.log .config .config.old ct-ng
$ ls .build/
mips-malta-linux-gnu src tarballs tools

Now the ct-ng downloads what it needs to .build dir according to .config (made automatically).
However these are not what produced. Check your home dir u will find x-tools

1
2
3
4
$ ls
mips-malta-linux-gnu
$ ls mips-malta-linux-gnu/
bin build.log.bz2 include lib libexec mips-malta-linux-gnu share

These are the built toolchain.

The truth is, the binarys compiled by it cannot run on old kernels (encounter FATAL:kernel too old in QEMU), that might because the libs (~/x-tools/mips-malta-linux-gnu/mips-malta-linux-gnu/sysroot/lib/ld-2.32.so) it use are latest.

So for old devices or QEMU, better use buildroot, or try toolchains.bootlin.com.

Compile GDB&GDBserver

It is for dynamic analysis, and I almost messed up by this. Get source code on FTP

First see how to set configuration

1
2
3
4
5
6
$ ./configure --target=mipsel-linux --host=mipsel-linux --program-prefix=mipsel-linux  --prefix=`echo $PWD`/bin CC=/path/to/buildroot/output/host/bin/mipsel-linux-gcc CXX=/path/to/buildroot/output/host/bin/mipsel-linux-g++  AR=/path/to/buildroot/output/host/bin/mipsel-linux-ar LD=/path/to/buildroot/output/host/bin/mipsel-linux-ld RANLIB=/path/to/buildroot/output/host/bin/mipsel-linux-ranlib  STRIP=/path/to/buildroot/output/host/bin/mipsel-linux-strip CFLAGS="-w -static" CXXFLAGS="-w -static" LDFLAGS="-static"
... # make sure paths are absolute
$ sudo make -j8
...
$ make install
...

Recommended versions includes 7.10
Versions too high cause configure: error: ***A compiler with support for c++11 language features is required, while lower versions gives configure: error: no termcap library found.

However problems still come out, if your toolchain is so latest, or if some other libraries are required.

The built gdbservers are off the shelf, you can use them or things built by others directly.

Powered by Hexo & Theme Keep
Total words 135.7k