制作S3C6410 的交叉编译链(arm-linux-gcc 4.6.0)

最近在做一些嵌入式的软件开发工作,在Linux下的交叉编译链一直都是使用别人编译好的交叉编译器。想自己制作一个属于自己的交叉编译器,了解一下构建嵌入式系统开发工具的一些方法。

下面说一下大致的方法和步骤。

从gnu网站上下载所需要的源码包,然后进行配置,编译,链接,安装。

还有就是使用第三方用于编译生成交叉编译链的管理工具:crosstool-ng-1.19.0(目前为最新版本)官方网站:http://www.crosstool-ng.org/

所需要的源码包有:GNU Binunitls,GCC, Glibc, Linux内核,gmp,mpfr,等                            所需的软件包大都可以从中国科技大学的镜像服务器上下载:http://oss.ustc.edu.cn

GNU Binunitls是一组二进制工具集。这个工具需要利用i386  linux本身的编译器进行编译,且在i386 linux上运行。编译S3C6410交叉编译链的时候要用到这些工具。

GNU Binunitls主要包括以下工具:

as                                     assembler popularly known as GAS (Gnu ASsembler)
ld                                     linker
gprof                            profiler
addr2line                    convert address to file and line
ar                                     create, modify, and extract from archives
c++filt                             demangling filter for C++ symbols
dlltool                             creation of Windows dynamic-link libraries
gold                             alternative linker
nlmconv                            object file conversion to a NetWare Loadable Module
nm                                    list symbols in object files
objcopy                            copy object files, possibly making changes
objdump                  dump information about object files
ranlib                          generate indexes for archives
readelf                         display content of ELF files
size                                 list total and section sizes
strings                         list printable strings
strip                         remove symbols from an object file
windmc                         generates Windows message resources
windres                          compiler for Windows resource files

其中GCC需要编译两次,第一次编译是生成支持c语言的交叉编译器,目的是用它来交叉编译后面的Glibc库。因为生成完整的GCC交叉编译器需要Glibc库的支持,但是现在还没有用于ARM平台的Glibc库,所以我们先生成一个简化的GCC,用它来编译Glibc,有了Glibc后再重新编译GCC生成完整的ARM-GCC。所以第一次编译GCC的配置选项会禁止很多功能。另外,因为要支持软浮点(Soft Float),GCC需要同时编译GMP和MPFR。GMP是实现任意精度算术运算的软件包,可以完成有符号整数、有理数和浮点数的运算。只要计算机内存的满足需要,GMP的运算精度没有任何限制。MPFR是一个用于高精度浮点运算的C库。


Glibc也就是C库函数,包括分配内存、搜索目录、打开和关闭文件、读和写文件、字符串操作、模式匹配、代数运算等。 libc-ports软件包的作用是移植,把Glibc库移植到ARM平台时需要它,解压后拷贝到glibc目录,并命名为 ports即可。

第二次编译gcc,这次是编译完整的gcc了。

为什么要编译两次GCC

第一遍只编译一个支持c的gcc,原因是要编译出一个支持交叉的c++,必须有一个编译好的用于目标体系平台的glibc,而不是只有glibc的头文件就可以的,好在编译glibc有c支持就够了,所以编译glibc也成了第一遍的gcc唯一的理由和作用。工具链中gcc的第一次和第二次编译都是由主系统的gcc和binutils来完成的(之前没有提及binutils,只是为了理解方便,但实际上编译后是少不了链接过程的,这个过程是要binutils来完成的)。到目前为止只有在编译glibc的时候用到了交叉版本的binutils,其它部分的链接都是由主系统的binutils来完成的。

关于crosstool-ng-1.19.0

crosstool-ng-1.19.0是一个帮助你自动下载源码,提供界面菜单配置,并生成完整makefile的工具。

crosstool-ng-1.19.0参考资料:http://www.crifan.com/files/doc/docbook/crosstool_ng/release/html/crosstool_ng.html


下面说一下我的机器的具体编译步骤。

我是利用crosstool-ng-1.19.0工具编译的gcc4.6.0的交叉编译工具。

编译平台:ubuntu12.04 

编译时的主目录,/home/zhanglianpin

所需具体源码列表:


1,创建一些目录,以备后面编译交叉编译链使用。

在主目录下创建以下目录,

crosstool-ng-1.19.0_build    (用于保存使用crosstool-ng-1.19.0这个工具时所下载的源码文件,解压后的源码,以及编译日志等

crosstool-ng-1.19.0_install    (用于安装crosstool-ng-1.19.0这个工具的目录)

S3C6410/src                       (用于保存下载的源码)

S3C5410/toolchain              (用于保存编译好的交叉编译链)

2,下载crosstool-ng-1.19.0

打开官网(http://www.crosstool-ng.org/),最新版本为crosstool-ng-1.19.0,点击下载,此版本没有补丁文件,如果你所下载的版本有patch,则需要下载下来,使用之前,按照说明先打上补丁。

3,安装crosstool-ng-1.19.0

tag:因为crosstool-ng-1.19.0不允许使用root权限运行,所以,创建目录及安装,请使用非root用户。

将下载的crosstool-ng-1.19.0.tar.bz2复制到主目录,/home/zhanglianpin,然后解压

#tar -zxf  crosstool-ng-1.19.0.tar.bz2 

然后 解压完后主目录下多出一个crosstool-ng-1.19.0的文件夹

#cd  crosstool-ng-1.19.0

#./configure --prefix=/home/zhanglianpin/crosstool-ng-1.19.0_install     //配置crosstool-ng  ,此软件将安装到crosstool-ng-1.19.0_install 的目录下

配置时若提示错误,请参见附录1:使用crosstool-ng 缺少的开发工具。

#sudo make                      //编译crosstool-ng

#sudo make install            //安装crosstool-ng

# echo "PATH=$PATH:/home/zhanglianpin/crosstool-ng-1.19.0_install /bin" >> ~/.bashrc  //为后面调用ct-ng命令增加环境变量
# source ~/.bashrc

# ct-ng help

hens@hens-desktop:/work/tools/crosstool-ng-1.9.3$ ct-ng help

This is crosstool-NG version 1.19.0
Copyright (C) 2008  Yann E. MORIN
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
See below for a list of available actions, listed by category:


Configuration actions:
  menuconfig         - Update current config using a menu based program
  oldconfig          - Update current config using a provided .config as base
  extractconfig      - Extract to stdout the configuration items from a build.log file piped to stdin
  savedefconfig      - Save current config as a mini-defconfig to ${DEFCONFIG}
  defconfig          - Update config from a mini-defconfig ${DEFCONFIG} (default: ${DEFCONFIG}=./defconfig)
  saveconfig         - Save current config as a preconfigured target
  show-tuple         - Print the tuple of the currently configured toolchain
Preconfigured toolchains (#: force number of // jobs):
  list-samples       - prints the list of all samples (for scripting)
  show-      - show a brief overview of (list with list-samples)
            - preconfigure crosstool-NG with (list with list-samples)
  build-all[.#]      - Build *all* samples (list with list-samples) and installin ${CT_PREFIX} (which you must set)
Build actions (#: force number of // jobs):
  build[.#]          - Build the currently configured toolchain
  list-steps         - List all build steps
Clean actions:
  clean              - Remove generated files
  distclean          - Remove generated files, configuration and build directories
Distribution actions:
  wiki-samples       - Print a DokuWiki table of samples
  updatetools        - Update the config tools
  tarball            - Build a tarball of the configured toolchain

Environment variables (see /home/zhanglianpin/crosstool-ng/crosstool-ng-1.19.0_install/share/doc/crosstool-ng/ct-ng.1.19.0/0 - Table of content.txt):
  STOP=step          - Stop the build just after this step (list with list-steps)
  RESTART=step       - Restart the build just before this step (list with list-steps)
  CT_PREFIX=dir      - install samples in dir (see action "build-all", above).
  V=0|1|2            - 0 => show only human-readable messages (default)
                       1 => show only the commands being executed
                       2 => show both
Use action "menuconfig" to configure your toolchain
Use action "build" to build your toolchain
Use action "version" to see the version
See "man 1 ct-ng" for some help as well

crosstool-ng安装成功.

4,使用crosstool-ng修改配置,用于生成最后的Makefile文件。

#cd  /home/zhanglianpin/crosstool-ng-1.19.0_build  (以后的操作都在此目录下进行,配置文件,Makefile 下载下来的源码,解压出来的源文件,编译log都放在此目录)


#ct-ng list-samples 

ct-ng  list-samples


Status  Sample name
  LN    config
  MKDIR config.gen
  IN    config.gen/arch.in
  IN    config.gen/kernel.in
  IN    config.gen/cc.in
  IN    config.gen/binutils.in
  IN    config.gen/libc.in
  IN    config.gen/debug.in
[G.X]   alphaev56-unknown-linux-gnu
[G.X]   alphaev67-unknown-linux-gnu
[G.X]   arm-bare_newlib_cortex_m3_nommu-eabi
[G.X]   arm-cortex_a15-linux-gnueabi
[G..]    arm-cortex_a8-linux-gnueabi
[G.X]   arm-cortexa9_neon-linux-gnueabihf
[G..]    arm-davinci-linux-gnueabi
[G..]    armeb-unknown-eabi
[G.X]   armeb-unknown-linux-gnueabi
[G.X]   armeb-unknown-linux-uclibcgnueabi
[G..]    arm-unknown-eabi
[G..]    arm-unknown-linux-gnueabi
[G.X]   arm-unknown-linux-uclibcgnueabi
[G.X]   armv6-rpi-linux-gnueabi
[G..]    avr32-unknown-none
[G..]    bfin-unknown-linux-uclibc
[G..]    i586-geode-linux-uclibc
[G.X]   i586-mingw32msvc,i686-none-linux-gnu
[G.X]   i686-nptl-linux-gnu
[G.X]   i686-unknown-mingw32
[G.X]   m68k-unknown-elf
[G.X]   m68k-unknown-uclinux-uclibc
[G.X]   mips64el-n32-linux-uclibc
[G.X]   mips64el-n64-linux-uclibc
[G.X]   mips-ar2315-linux-gnu
[G..]    mipsel-sde-elf
[G..]    mipsel-unknown-linux-gnu
[G.X]   mips-malta-linux-gnu
[G..]    mips-unknown-elf
[G.X]   mips-unknown-linux-uclibc
[G..]    powerpc-405-linux-gnu
[G.X]   powerpc64-unknown-linux-gnu
[G..]    powerpc-860-linux-gnu
[G.X]   powerpc-e300c3-linux-gnu
[G.X]   powerpc-e500v2-linux-gnuspe
[G..]    powerpc-unknown-linux-gnu
[G..]    powerpc-unknown-linux-uclibc
[G..]    powerpc-unknown_nofpu-linux-gnu
[G.X]   s390-ibm-linux-gnu
[G.X]   s390x-ibm-linux-gnu
[G..]    sh4-unknown-linux-gnu
[G..]    x86_64-unknown-linux-gnu
[G..]    x86_64-unknown-linux-uclibc
[G.X]   x86_64-unknown-mingw32
 L (Local)       : sample was found in current directory
 G (Global)      : sample was installed with crosstool-NG
 X (EXPERIMENTAL): sample may use EXPERIMENTAL features
 B (BROKEN)      : sample is currently broken

以上列出了所有的配置samples,我们编译的是S3C6410的交叉编译链(一般还用于嵌入式linux的开发),所以我们选择arm-unknown-linux-gnueabi这个配置文件,在这个配置文件基础上再增加自己的配置。

#ct-ng  arm-unknown-linux-gnueabi 

下面就可以直接依据此配置文件来配置自己的交叉编译链属性了。

#ct-ng menuconfig

接着会打开配置界面菜单。

制作S3C6410 的交叉编译链(arm-linux-gcc 4.6.0)_第1张图片

S3C6410的CPU架构 为ARMV6的,对其进行配置。此菜单的操作方式很简单,选中条目,Enter键进入条目,若是需要填写的条目,按enter键进入编辑。若是需要选择的条目,按空格键选择或者取消选择。(有*号表示选择上,没有*号表示没有选择上)。 使用tab键来选择