Building a GNU/Linux ARM Toolchain (from scratch)

Charles M. "Chip" Coldwell  http://frank.harvard.edu/~coldwell/toolchain/

 

Note: (Feb 28, 2008) these days I don't roll my own toolchains anymore. It's just too much trouble, and it's just so easy to get toolchains from places like CodeSourcery these days.

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.

Package Binary Source
GNU Binutils arm-binutils-2.16-1.i386.rpm arm-binutils-2.16-1.src.rpm
GNU Compiler Collection arm-gcc-3.4.4-1.i386.rpm arm-gcc-3.4.4-1.src.rpm
GNU C Library arm-glibc-2.3.5-1.i386.rpm arm-glibc-2.3.5-1.src.rpm
Linux Kernel arm-kernel-2.6.10-1.i386.rpm arm-kernel-2.6.10-1.src.rpm

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 /
   /
   /
   /
   /
   /
   /
   /
   /
   /
  
  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 
  ln -s linux-2.6.10 linux
  zcat  | patch -d linux -p1
  zcat  >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 <
  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 < 
  patch -d gcc-3.4.4 -p1 < 
  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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章