免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1862 | 回复: 0
打印 上一主题 下一主题

[转]Building a GNU/Linux ARM Toolchain(from scratch [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-17 09:13 |只看该作者 |倒序浏览

Building a GNU/Linux ARM Toolchain (from scratch)

Charles M. "Chip" Coldwell

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 \
  
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 &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 &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

mhtml:file://E:\embeded linux\buiding toolchain\Building a GNU-Linux ARM Toolchain.mht!http://www.w3.org/Icons/valid-xhtml10

mhtml:file://E:\embeded linux\buiding toolchain\Building a GNU-Linux ARM Toolchain.mht!http://frank.harvard.edu/~coldwell/toolchain/gnubanner.png

mhtml:file://E:\embeded linux\buiding toolchain\Building a GNU-Linux ARM Toolchain.mht!http://frank.harvard.edu/~coldwell/toolchain/emacs.png


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/88620/showart_1724548.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP