制作mipsel 交叉工具链

来源:互联网 发布:android 6.0 java版本 编辑:程序博客网 时间:2024/04/28 11:59
1. 在 ~/code 下建立目录 cross-compile, 在 cross-compile 下建立setup, 保存下载的文件:

 ls ~/code/cross-compile/setup/
binutils-2.24.tar.bz2  gcc-4.6.4.tar.bz2  glibc-2.18.tar.bz2  glibc-linuxthreads-2.5.tar.bz2  linux-3.2.tar.bz2

在 cross-compile 下建立 build目录,在 build下面建立 编译子目录 binutils  gcc  glibc,用来编译 binutils, gcc 和 glibc:
ls ~/code/cross-compile/build/
binutils  gcc  glibc

建立  src目录,用来存放解压的代码
建立 kernel 目录,保存 kernel 的代码
建立embedded-toolchain/tool-chain/目录,存放最后编译出来的工具链。
 ls ~/code/cross-compile/
build  embedded-toolchain  gmp-4.3.2.tar.bz2  mpc-0.8.1         mpfr-2.4.2          setup
doc    gmp-4.3.2           kernel             mpc-0.8.1.tar.gz  mpfr-2.4.2.tar.bz2  src


2. 编译 binutils:

cd src/
tar jxvf ../setup/binutils-2.24.tar.bz2

 cd  ..
cd build/binutils/
../../src/binutils-2.24/configure --target=mipsel-linux --prefix=/home/charles/code/cross-compile/embedded-toolchain/tool-chain

在 build/binutils下面生成makefile文件:

bfd  binutils  config.log  config.status  etc  gas  gprof  intl  ld  libiberty  Makefile  opcodes  serdep.tmp

执行  make, make install, 会安装到embedded-toolchain/tool-chain里。

ls embedded-toolchain/tool-chain/bin/
mipsel-linux-addr2line  mipsel-linux-c++filt  mipsel-linux-gcc        mipsel-linux-ld       mipsel-linux-objdump  mipsel-linux-strings
mipsel-linux-ar         mipsel-linux-cpp      mipsel-linux-gcc-4.6.4  mipsel-linux-ld.bfd   mipsel-linux-ranlib   mipsel-linux-strip
mipsel-linux-as         mipsel-linux-elfedit  mipsel-linux-gcov       mipsel-linux-nm       mipsel-linux-readelf
mipsel-linux-c++        mipsel-linux-g++      mipsel-linux-gprof      mipsel-linux-objcopy  mipsel-linux-size

3. 设置内核头文件

cd kernel/
tar jxvf ../setup/linux-3.2.tar.bz2  
cd linux-3.2/
make ARCH=mips CROSS_COMPILE=mipsel-linux- menuconfig
make ARCH=mips CROSS_COMPILE=mipsel-linux-

在 make menuconfig 的时候, machine 选择 MIPS malta board, CPU选择  MIPS32 release 2, Endianess Selection 选择 "little endian"
这一步肯定出错,但只要生成了version.h 和 autoconf.h就达到了目的。
下面拷贝头文件:

先 cd 到 embedded-toolchain/tool-chain, 建立 include 目录:

cp -r ../../kernel/linux-3.2/include/linux include/
cp -r ../../kernel/linux-3.2/include/asm-generic include/
cp  -r ../../kernel/linux-3.2/arch/mips/include/asm include/

4. 建立初始编译器(bootstrap gcc)
 
 ../../src/gcc-4.6.4/configure  --target=mipsel-linux --prefix=/home/charles/code/cross-compile/embedded-toolchain/tool-chain --without-headers --enable-languages=c,c++ --disable-shared --disable-threads --disable-decimal-float --disable-libmudflap --disable-lipssp --with-float=soft

make all-gcc -j10 -i -k
make install-gcc
make all-target-libgcc
make install-target-libgcc


 ls  ../../embedded-toolchain/tool-chain/bin/
mipsel-linux-addr2line  mipsel-linux-c++filt  mipsel-linux-gcc        mipsel-linux-ld       mipsel-linux-objdump  mipsel-linux-strings
mipsel-linux-ar         mipsel-linux-cpp      mipsel-linux-gcc-4.6.4  mipsel-linux-ld.bfd   mipsel-linux-ranlib   mipsel-linux-strip
mipsel-linux-as         mipsel-linux-elfedit  mipsel-linux-gcov       mipsel-linux-nm       mipsel-linux-readelf
mipsel-linux-c++        mipsel-linux-g++      mipsel-linux-gprof      mipsel-linux-objcopy  mipsel-linux-size

5. 编译 glibc

 cd src/
 tar  ../setup/glibc-2.18.tar.bz2
 tar jxvf ../setup/glibc-linuxthreads-2.5.tar.bz2  --directory=glibc-2.18

 cd ..
 cd  build/
 cd glibc
CC=mipsel-linux-gcc AR=mipsel-linux-ar RANLIB=mipsel-linux-ranlib  ../../src/glibc-2.18/configure --host=mipsel-linux --prefix=/home/charles/code/cross-compile/embedded-toolchain/tool-chain/mipsel-linux --enable-add-ons  --with-headers=/home/charles/code/cross-compile/embedded-toolchain/tool-chain/mipsel-linux/include --with-fp=no

编译的时候报错
cannot compute suffix of object files: cannot compile
经过查看,发现是 mipsel-linux-gcc 没有添加到 PATH所致,执行:
export PATH=$PATH:/home/charles/code/cross-compile/embedded-toolchain/tool-chain/bin
然后,重新执行上面的配置命令,可以通过。
最后,执行  make, make install.

6. 编译完整的gcc

 ../../src/gcc-4.6.4/configure --target=mipsel-linux --prefix=/home/charles/code/cross-compile/embedded-toolchain/tool-chain --disable-multilib --disable-libssp  --disable-nls --disable-libstdcxx-pch --with-arch=mips32r2  --with-mips-plt --with-float=soft --with-ppl=/usr/local --disable-libgomp --enable-poison-system-directories --enable-symvers=gnu --enable-long-long --enable-threads --enable-languages=c,c++ --enable-shared --enable-lto --enable-__cxa_atexit --with-gnu-as --with-gnu-ld

然后 , 执行:

make all-gcc -j10 -i -k

make install-gcc

需要注意的问题:

1)最后一步编译  gcc 的时候,如果直接执行  make 会报错,正确的方法是  make all-gcc

2)因为 对gcc做了  配置 --with-float=soft, 在编译 glibc的时候也要做相应的配置: with-fp=no(不使用hardware float),否则生成的gcc 编译文件的时候,会报找不到文件gnu/stubs-o32_soft.h 的错误.

最后可以查看一下 生成的 gcc信息:

$ ./mipsel-linux-gcc -vUsing built-in specs.COLLECT_GCC=./mipsel-linux-gccCOLLECT_LTO_WRAPPER=/home/charles/code/cross-compile/embedded-toolchain/tool-chain/libexec/gcc/mipsel-linux/4.6.4/lto-wrapperTarget: mipsel-linuxConfigured with: ../../src/gcc-4.6.4/configure --target=mipsel-linux --prefix=/home/charles/code/cross-compile/embedded-toolchain/tool-chain --without-headers --enable-languages=c,c++ --disable-shared --disable-threads --disable-decimal-float --disable-libmudflap --disable-lipssp --with-float=soft : (reconfigured) ../../src/gcc-4.6.4/configure --target=mipsel-linux --prefix=/home/charles/code/cross-compile/embedded-toolchain/tool-chain --disable-multilib --disable-libssp --disable-nls --disable-libstdcxx-pch --with-arch=mips32r2 --with-mips-plt --with-float=soft --with-ppl=/usr/local --disable-libgomp --enable-poison-system-directories --enable-symvers=gnu --enable-long-long --enable-threads --enable-languages=c,c++ --enable-shared --enable-lto --enable-__cxa_atexit --with-gnu-as --with-gnu-ldThread model: posixgcc version 4.6.4 (GCC) 

7.编译  gdb

这个比较简单,下载代码gdb-7.6.2.tar.bz2, 解压后,配置如下:

 ../../src/gdb-7.6.2/configure --build=i686-pc-linux-gnu  --target=mipsel-linux --prefix=/home/charles/code/cross-compile/embedded-toolchain/tool-chain
然后执行  make, make install 既可以了。

最后,可以用一个mips的镜像测试一下:

./mipsel-linux-gcc -g test.c -o testcharles@taotao:~/code/cross-compile/embedded-toolchain/tool-chain/bin$ file testtest: ELF 32-bit LSB executable, MIPS, MIPS32 rel2 version 1, dynamically linked (uses shared libs), for GNU/Linux 2.6.16, with unknown capability 0xf41 = 0x756e6700, with unknown capability 0x70100 = 0x3040000, not strippedcharles@taotao:~/code/cross-compile/embedded-toolchain/tool-chain/bin$ ./mipsel-linux-gdb GNU gdb (GDB) 7.6.2Copyright (C) 2013 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>This is free software: you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law.  Type "show copying"and "show warranty" for details.This GDB was configured as "--host=i686-pc-linux-gnu --target=mipsel-linux".For bug reporting instructions, please see:<http://www.gnu.org/software/gdb/bugs/>.(gdb) file testReading symbols from /home/charles/code/cross-compile/embedded-toolchain/tool-chain/bin/test...done.(gdb) disdisable      disassemble  disconnect   display      (gdb) disassemble No frame selected.(gdb) disassemble mainDump of assembler code for function main:   0x00400700 <+0>:addiusp,sp,-32   0x00400704 <+4>:swra,28(sp)   0x00400708 <+8>:sws8,24(sp)   0x0040070c <+12>:moves8,sp   0x00400710 <+16>:luiv0,0x40   0x00400714 <+20>:addiua0,v0,2288   0x00400718 <+24>:jal0x400940 <puts@plt>   0x0040071c <+28>:nop   0x00400720 <+32>:movesp,s8   0x00400724 <+36>:lwra,28(sp)   0x00400728 <+40>:lws8,24(sp)   0x0040072c <+44>:addiusp,sp,32   0x00400730 <+48>:jrra   0x00400734 <+52>:nopEnd of assembler dump.



0 0