Hacking cross compiling tools

This page describes a rather hacked method of producing a set of cross compile tools from one Linux architecture to another.  It isn't clean, it isn't pleasent, and it doesn't always worked.  It is primarily there to build kernels not user applications, but that sometimes works. You've been warned. I use m68k as the example architecture here; this set of instructions works for most architectures (and act as a grounding for others!).

1. Directorys and the like

I've not built this around packaging the end result - because it seemed like too much hastle.  What I do is I have is:
/discs/cross


Root of all my cross compilation work (I have a 26GB partition for this)
/discs/cross
m68k

General stuff for m68k, boot loaders, hacks, patches, notes
/discs/cross/
kernels

Where I keep all the kernels I'm cross building
/discs/cross/
kernels/
m68k
Where I keep the m68k kernel trees I build.
/discs/cross/
tools

Where all my cross compilation tools live.
/discs/cross/
source

Where I open out the source for cross compilation tools
/discs/cross/
tools/
m68k-linux
The set of tools for cross compiling to the m68k-linux target (for example)

Thus for a target m68k you set the CROSS_COMPILE variable in the kernel makefile to /discs/cross/tools/m68k-linux/bin/m68k-linux- .

2. binutils

The first thing is to get a working binutils. I tend to use H.J. Lu's Beta binutils sets for Linux and am currently on 2.11.92.0.10 (lovely numbering system hey!).  These tend to have Linux specific fixes in.  I have this unpacked in /discs/cross/source/binutils-2.11.92.0.10 but like most GNU based tools it can be compiled in a seperate directory; this is nice since it means you only need one source copy unpacked (well if it works on all your targets).  So from a temporary directory issue the following:

            /discs/cross/source/binutils-2.11.92.0.10/configure --prefix=/discs/cross/tools/m68k-linux --target=m68k-linux
if that works then you can do:


            make && make install

(I actually run a little for loop to build for 5 or 6 platforms).  That should give you a set of binutils; so you should be able to do:

            /discs/cross/tools/m68k-linux/bin/m68k-linux-as --version

If you are going to build sun3 userland binaries with this you are going to need to patch the linker script to base executables at a low address.

3. libraries

To build gcc on many architectures you need a set of C library include headers (and sometimes library binaries); I use a little script: get-libdebs that lives in /discs/cross/tools to download the Debian libraries for the platform.  Note that this script uses quite a few of the debian utilities.  I decided to take this route since there is no point in building cross library sets when someone already has a full set of libraries for your target platform and Debian has them for most Linux architectures.

This script downloads the latest libc6-dev package from the debian testing distribution and unpacks it such that:
/discs/cross/tools/m68k-linux/m68k-linux/include and /discs/cross/tools/m68k-linux/m68k-linux/lib contain the include and library files for the target - this is exactly where the gcc build is going to look for them.
One problem which may cause problems on some cross builds is that the libc.so file is actually a little text file containing an indirection to a couple of other files, typically /lib/libc.so.6 and /usr/lib/libc_nonshared.a ; since these are actually host rather than target files this needs to be edited by hand.   At the moment I remove the libc.so.6 entry and put the full patch to libc_nonshared.a in as /discs/cross/tools/m68k-linux/m68k-linux/lib/libc_nonshared.a .  Note that this is not done by the script.

4. compilers

Ah compilers.  One of the problems is that there are many patches for each architecture that are needed to produce a compiler that actually works.  Originally I started with the vanilla gcc-2.95.3 source and it produced bad kernels on ARM and I found there was a patch.  Recently I've started using the Debian gcc-2.95 source distribution since it includes a lot of architecture specific patches.
Note that this does not work for HP-PA (you need the gcc source out of their CVS).  Nor does it work for crossing to Alpha - so far I have not found a working x86->Alpha combination; I have found a number (few and far between) that actually compile, but they all produce duff kernels when crossing.
My build-gcc-deb-script builds from the debian source.  It has an 'apt-get' commented out that can be used to fetch the source from the debian archives; or if you do that once (in my case in /discs/cross/source/debian) then pulls it from the one place you got it.
This builds the C, C++ and Java compilers (since these seem to compile with little trouble) - it does not compile all the surrounding libraries (e.g. libstdc++ ) - if you want these then just get the pre-built binaries out of the Debian distribution.

5. Success?

Please tell me about whether this set of instructions has worked for you.  Remember whatever you are doing you could be getting cross compilation errors so never trust the compiler!


Dave Gilbert ( cross AT treblig.org )

and back to my home page