Building a GNU/Linux ARM Toolchain (from scratch)
This article describes the steps necessary to build a cross-compiler toolchain for targeting ARM processors from an i386 workstation running the GNU/Linux operating system. The procedure described can be run automatically using a shell script, or you can just download the RPMs from the table below. Those interested in what's going on under the hood should read on.
N.B., These RPMs were built on Red Hat Desktop v.3
N.B., because of the bootstrapping problem described below, building any one of the gcc, glibc or kernel RPMs from the corresponding source RPM requires installing the other two binary RPMs. The only way to build the whole set from scratch is to use the shell script.
N.B., this procedure builds a little-endian toolchain. Follow the link to learn why big-endianness is wrong.
The files are installed in the directory /usr/arm. The build process described here assumes that all work is being done under this directory. This means that you must have read, write and execute permissions on this directory and all subdirectories and files contained therein to proceed. Probably that means you must be root.
There's a bootstrapping problem that requires some of the components to be built twice. Briefly, the problem is that building the C cross-compiler requires having the ARM GNU C Library, but of course, building the ARM GNU C Library requires a C cross-compiler. The solution to this circular dependency is to first install the GNU C library headers, then build the C compiler without the C Library, use it to build the C library, then rebuild the full set of GNU compilers. Lather, rinse, repeat.
The GNU C Library cannot be built without the Linux kernel headers. Most C library functions are implemented using system calls, and the kernel headers define the system call interface. The headers come with the kernel source, of course, but the source must be patched and configured for the ARM architecture before they can be used to build the C library.
Setup
To start out, set up some variables and create the toplevel ARM directory and the source subdirectory. Also, make sure that the utilties we build will be on your PATH after they are installed.
TARGET=arm-unknown-linux-gnu PREFIX=/usr/arm SYSROOT=${PREFIX}/sysroot export ARCH=arm export CROSS_COMPILE=${TARGET}- export PATH=$PATH:${PREFIX}/bin mkdir -p ${PREFIX}/src
Get the sources
The versions chosen are not necessarily the most recent, but they do seem to work together with a minimum of patching. The last two files are only necessary if you are targeting the Atmel AT91RM9200 CPU.
cd ${PREFIX}/src for URL in / http://ftp.gnu.org/gnu/binutils/binutils-2.16.tar.gz / http://ftp.gnu.org/gnu/gcc/gcc-3.4.4/gcc-3.4.4.tar.bz2 / "http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/flow.c.diff?cvsroot=gcc&only_with_tag=csl-arm-branch&r1=1.563.4.2&r2=1.563.4.3" / http://frank.harvard.edu/~coldwell/toolchain/t-linux.diff / http://ftp.gnu.org/gnu/glibc/glibc-2.3.5.tar.gz / http://ftp.gnu.org/gnu/glibc/glibc-linuxthreads-2.3.5.tar.gz / http://frank.harvard.edu/~coldwell/toolchain/ioperm.c.diff / http://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.10.tar.gz / http://maxim.org.za/AT91RM9200/2.6/2.6.10-at91.patch.gz / http://maxim.org.za/AT91RM9200/2.6/26_at91_serial.c.gz do FILE=${URL##*/} FILE=${FILE%%/?*} [ -f ${FILE} ] || wget -O ${FILE} ${URL} done
GNU binutils
These utilities (as, ar, ranlib, ld, etc.) are a prerequisite for building even the runt C compiler. In fact, many of the phases of compilation are accomplished by explicitly invoking some of these utilities (e.g as or ld).
Fortunately, setting up the GNU binutils for targeting the ARM architecture is very straightforward:
cd ${PREFIX}/src tar xvfz binutils-2.16.tar.gz mkdir -p BUILD/binutils-2.16 cd BUILD/binutils-2.16 ../../binutils-2.16/configure --prefix=${PREFIX} --target=${TARGET} --with-sysroot=${SYSROOT} 2>&1 | tee configure.out make 2>&1 | tee make.out make install 2>&1 | tee -a make.out
Linux Kernel Headers
In this step, the Linux kernel headers are installed in the SYSROOT directory. In the example below, the kernel is configured for the AT91RM9200 processor; default configurations for other processors are available in the linux-2.6.10/arch/arm/configs directory. Two patches are applied to the kernel source in order to build for the AT91RM9200; if this isn't your target CPU these patches are optional/irrelevant/harmless.
cd ${PREFIX}/src tar xvfz linux-2.6.10.tar.gz ln -s linux-2.6.10 linux zcat 2.6.10-at91.patch.gz | patch -d linux -p1 zcat 26_at91_serial.c.gz >linux/drivers/serial/at91_serial.c cd linux make at91rm9200dk_defconfig make include/linux/version.h mkdir -p ${SYSROOT}/usr/include cp -a ${PREFIX}/src/linux/include/linux ${SYSROOT}/usr/include/linux cp -a ${PREFIX}/src/linux/include/asm-arm ${SYSROOT}/usr/include/asm cp -a ${PREFIX}/src/linux/include/asm-generic ${SYSROOT}/usr/include/asm-generic
Glibc headers
This step installs the header files that come with glibc. Two of them are generated during the glibc build, which we don't do until later on. Fortunately, it is sufficient to substitute empty files.
cd ${PREFIX}/src tar xvfz glibc-2.3.5.tar.gz patch -d glibc-2.3.5 -p1 <ioperm.c.diff cd glibc-2.3.5 tar xvfz ../glibc-linuxthreads-2.3.5.tar.gz cd .. mkdir -p BUILD/glibc-2.3.5-headers cd BUILD/glibc-2.3.5-headers ../../glibc-2.3.5/configure --prefix=/usr --host=${TARGET} --enable-add-ons=linuxthreads --with-headers=${SYSROOT}/usr/include 2>&1 | tee configure.out make cross-compiling=yes install_root=${SYSROOT} install-headers 2>&1 | tee make.out touch ${SYSROOT}/usr/include/gnu/stubs.h touch ${SYSROOT}/usr/include/bits/stdio_lim.h
Stage 1 GCC
Here we apply two patches to the GCC source. The first one forces the build process to generate the C Run Time files crti.o and crtn.o, which are needed later in the gcc build process. The second one fixes a bug that would otherwise cause an internal compiler error (ICE) during the glibc build process.
cd ${PREFIX}/src bunzip2 -c gcc-3.4.4.tar.bz2 | tar xvf - patch -d gcc-3.4.4 -p1 < flow.c.diff patch -d gcc-3.4.4 -p1 < t-linux.diff mkdir -p BUILD/gcc-3.4.4-stage1 cd BUILD/gcc-3.4.4-stage1 ../../gcc-3.4.4/configure --prefix=${PREFIX} --target=${TARGET} --enable-languages=c --with-sysroot=${SYSROOT} 2>&1 | tee configure.out make 2>&1 | tee make.out make install 2>&1 | tee -a make.out
GNU C Library
Glibc builds without any patching, provided you use the right configure switches.
cd ${PREFIX}/src mkdir -p BUILD/glibc-2.3.5 cd BUILD/glibc-2.3.5 BUILD_CC=gcc CC=${CROSS_COMPILE}gcc AR=${CROSS_COMPILE}ar RANLIB=${CROSS_COMPILE}ranlib AS=${CROSS_COMPILE}as LD=${CROSS_COMPILE}ld ../../glibc-2.3.5/configure --prefix=/usr --build=i386-redhat-linux --host=arm-unknown-linux-gnu --target=arm-unknown-linux-gnu --without-__thread --enable-add-ons=linuxthreads --with-headers=${SYSROOT}/usr/include 2>&1 | tee configure.out make 2>&1 | tee make.out make install_root=${SYSROOT} install
Stage 2 GCC
Now rebuild gcc again, using the GNU C Library we just built.
cd ${PREFIX}/src mkdir -p BUILD/gcc-3.4.4 cd BUILD/gcc-3.4.4 ../../gcc-3.4.4/configure --prefix=${PREFIX} --target=${TARGET} --enable-languages=c --with-sysroot=${SYSROOT} 2>&1 | tee configure.out make 2>&1 | tee make.out make install 2>&1 | tee -a make.out
Linux Kernel Image
Finally, build a Linux kernel that can be used to boot the target.
cd ${PREFIX}/src/linux make zImage make modules make INSTALL_MOD_PATH=${SYSROOT} modules_install
The kernel will be in arch/arm/boot/zImage and the corresponding loadable modules will be installed /usr/arm/sysroot/lib/modules/[kernel-version]. Note that the directory /usr/arm/sysroot has been set that it is suitable for NFS export as a root filesystem for the target system.
Resources
- GNU Binutils http://www.gnu.org/software/binutils/
- GNU Compiler Collection (GCC) http://gcc.gnu.org/
- GNU C Library (glibc) http://www.gnu.org/software/libc/libc.html
- Linux kernel sources http://www.kernel.org
- ARM Linux http://www.arm.linux.org.uk/
- Atmel AT91RM9200 patches for Linux 2.6 http://maxim.org.za/AT91RM9200/2.6/
- Atmel AT91 processors http://www.atmel.com/products/AT91/
- Software floating point in GCC http://gcc.gnu.org/wiki/Software%20floating%20point
![Valid XHTML 1.0!](https://i-blog.csdnimg.cn/blog_migrate/f6385cf516fca3331ec495ab5bca68c4.png)
![GNU's Not Unix!](http://frank.harvard.edu/~coldwell/toolchain/gnubanner.png)
![Created with Emacs!](http://frank.harvard.edu/~coldwell/toolchain/emacs.png)