fireaxe 阅读(83) 评论(0)
最近在做一个ARM板子的linux 系统时,发生了一个诡异的问题:busybox失败,定位后发现如果open一个不存在的文件,则会直接产生SIGSEGV异常,而不是open返回-1,如果打开存在的文件,则不会有问题。怀疑失败原因是glibc库与kernel的版本不匹配,于是干脆自己动手做了个toolchain。
经过一个周末的折腾,在"http://crosstool.net/"上面script的帮助下,终于制作出了自己的tool chain :)
有了与kernel匹配的glibc库,之前open的问题就消失了。

下面的我的编译脚本:

点击(此处)折叠或打开

  1. #!/bin/bash

  2. # author: fireaxe.hq
  3. # description: The script for building crosstool automatically
  4. # usage: chmod +x crosstool-4.8.2.sh && ./crosstool-4.8.2.sh
  5. # linux-3.12.17
  6. # binutils-2.24 glibc-2.18 glibc-ports-2.16.0 gcc-4.8.2

  7. function set_sys
  8. {
  9.     apt-get update && apt-get install build-essential libncurses5-dev gawk libmpfr-dev -y
  10. }

  11. function set_env
  12. {
  13.     export USRROOT=/home/hao1204/Desktop
  14.     export KRNROOT=${USRROOT}/kernel/linux-3.12.17
  15.     export SRCROOT=${USRROOT}/toolchain

  16.     export TARGET=arm-qhao-linux-gnueabi

  17.     export PREFIX=/opt/mytoolchain
  18.     export BUILD=${SRCROOT}/build-tools
  19.     export TARGET_PREFIX=${PREFIX}/${TARGET}
  20.     export PATH=${PREFIX}/bin:${PATH}
  21. }

  22. function create_dir
  23. {
  24.     mkdir -p ${BUILD}/build-binutils ${BUILD}/build-boot-gcc ${BUILD}/build-gcc ${BUILD}/build-glibc ${BUILD}/build-glibc-headers
  25.     mkdir -p ${TARGET_PREFIX}
  26. }

  27. function install_cross_binutils
  28. {
  29.     cd ${BUILD}
  30.     cd build-binutils
  31.     ../../binutils-2.24/configure --target=${TARGET} --prefix=${PREFIX} --with-sysroot=${TARGET_PREFIX}
  32.     make -j6
  33.     make install
  34. }

  35. function install_linux_headers
  36. {
  37.     cd ${KRNROOT}
  38.     make headers_install
  39.     mkdir -p ${TARGET_PREFIX}/usr
  40.     cp -r tmparm/usr/include ${TARGET_PREFIX}/usr
  41. }

  42. function install_glibc_headers
  43. {
  44.     cd ${BUILD}
  45.     cp -r ../glibc-ports-2.16.0 ../glibc-2.18/ports
  46.     cd build-glibc-headers
  47.     echo "libc_cv_forced_unwind=yes" > config.cache
  48.     echo "libc_cv_c_cleanup=yes" >> config.cache
  49.     echo "libc_cv_arm_tls=yes" >> config.cache
  50.     ../../glibc-2.18/configure --host=${TARGET} --prefix="/usr" --with-sysroot=${TARGET_PREFIX} --enable-add-ons --with-headers=${TARGET_PREFIX}/usr/include --cache-file=config.cache
  51.     make cross-compiling=yes install_root=${TARGET_PREFIX}/usr prefix="" install-headers
  52. }

  53. function install_cross_gcc
  54. {
  55.     cd ${BUILD}
  56.     cp -r ../gmp-6.0.0 ../gcc-4.8.2/gmp
  57.     cp -r ../mpc-1.0.2 ../gcc-4.8.2/mpfr
  58.     cp -r ../mpfr-3.1.2 ../gcc-4.8.2/mpc
  59.     cd build-boot-gcc
  60.     mkdir -p ${TARGET_PREFIX}/include/gnu
  61.     touch ${TARGET_PREFIX}/include/gnu/stubs.h
  62.     ../../gcc-4.8.2/configure --target=${TARGET} --prefix=${PREFIX} --with-sysroot=${TARGET_PREFIX} --with-arch=armv7-a --disable-shared --without-headers --with-newlib --enable-languages=c,c++ -with-gmp=/usr/local/gmp-6.0.0 -with-mpfr=/usr/local/mpfr-3.1.2 -with-mpc=/usr/local/mpc-1.0.2
  63.     make -j6 all-gcc all-target-libgcc
  64.     make install-gcc install-target-libgcc
  65. }

  66. function install_glibc
  67. {
  68.     cd ${BUILD}/build-glibc
  69.     echo "libc_cv_forced_unwind=yes" > config.cache
  70.     echo "libc_cv_c_cleanup=yes" >> config.cache
  71.     CC=${TARGET}-gcc -fPIC ../../glibc-2.18/configure --host=${TARGET} --prefix="/usr" --with-sysroot=${TARGET_PREFIX} --enable-add-ons --with-headers=${TARGET_PREFIX}/usr/include --cache-file=config.cache
  72.     ln -s ${PREFIX}/lib/gcc/ arm-qhao-linux-gnueabi/4.8.2/libgcc.a ${PREFIX}/lib/gcc/ arm-qhao-linux-gnueabi/4.8.2/libgcc_s.a
  73.     ln -s ${PREFIX}/lib/gcc/ arm-qhao-linux-gnueabi/4.8.2/libgcc.a ${PREFIX}/lib/gcc/ arm-qhao-linux-gnueabi/4.8.2/libgcc_eh.a
  74.     make -j6
  75.     make install_root=${TARGET_PREFIX} prefix="" install
  76. }

  77. function install_gcc
  78. {
  79.     cd ${BUILD}/build-gcc
  80.     ../../gcc-4.8.2/configure --target=${TARGET} --prefix=${PREFIX} --with-sysroot=${TARGET_PREFIX} --with-arch=armv7-a --enable-shared --enable-__cxa_atexit --disable-nls --enable-c99 --enable-long-long --enable-threads=posix --enable-languages=c,c++ -with-gmp=/usr/local/gmp-6.0.0 -with-mpfr=/usr/local/mpfr-3.1.2 -with-mpc=/usr/local/mpc-1.0.2
  81.     sed -i 's/\/lib\///g' ${TARGET_PREFIX}/lib/libc.so
  82.     sed -i 's/\/lib\///g' ${TARGET_PREFIX}/lib/libpthread.so
  83.     make -j6 all
  84.     make install
  85. }

  86. function test
  87. {
  88.     cd ${SRCROOT}
  89.     arm-qhao-linux-gnueabi-gcc -o helloworld helloworld.c
  90.     if [ -e helloworld ]; then
  91.         echo "Build crosstool successed!"
  92.     else
  93.         echo "Build crosstool failed!"
  94.     fi
  95. }

  96. echo =============== set_sys
  97. set_sys
  98. echo =============== set_env
  99. set_env
  100. echo =============== start create_dir
  101. create_dir
  102. echo =============== start install_cross_binutils
  103. install_cross_binutils
  104. echo =============== start install_linux_headers
  105. install_linux_headers
  106. echo =============== start install_glibc_headers
  107. install_glibc_headers
  108. echo =============== install_cross_gcc
  109. install_cross_gcc
  110. echo =============== start install_glibc
  111. install_glibc
  112. echo =============== start install_gcc
  113. install_gcc
  114. echo =============== start test
  115. test

特别感谢:
http://crosstool.net/

其他参考链接: