分类: 嵌入式
2011-04-22 17:35:50
转自:http://blog.csdn.net/ebrother/archive/2009/04/27/4127171.aspx
前段时间一直在用IAR开发一款基于Luminary LM3S系列芯片的产品,可是在加入TCP/IP协议栈的时候遇到了32K的代码限制,在google上搜索了好几天都没有找到破解版本(想省点银子),结果大失所望,要么就是4.10的keygen,5.10的keygen竟然不好用。
天无绝人之路,本还没打算这么早投入gcc怀抱,看来只能提前了。在写这篇文章的时候,已经能够用OpenOCD下载由arm-elf-gcc编译的firmware,并且能够设置断点。后续文章将做介绍,现在就把编译gcc的过程贴出来,老外写的E文,我就不翻译了,有些做了点修改。
[操作系统] UBUNTU 8.04 / 8.10。比较推荐用这个系统,好用。
This tutorial explains how you can create a GCC+Newlib toolchain that can be used to compile programs for the Cortex (Thumb2) architecture, thus making it possible to use GCC to compile programs for the increasingly number of Cortex CPUs out there (, ST, with new Cortex CPUs being announced by Atmel and other companies). I am writing this tutorial because I needed to work on a Cortex CPU for the eLua project and I couldn't find anywhere a complete set of instructions for building GCC for this architecture. You'll need such a toolchain if you want to compile eLua for Cortex-M3 CPUs.
DISCLAIMER: I'm by no means a specialist in the GCC/newlib/binutils compilation process. I'm sure that there are better ways to accomplish what I'm describing here, however I just wanted a quick and dirty way to build a toolchain, I have no intention in becoming too intimate with the build process. If you think that what I did is wrong, innacurate, or simply outrageously ugly, feel free to and I'll make the necessary corrections. And of course, this tutorial comes without any guarantees whatsoever.
PrerequisitesTo build your toolchain you'll need:
Also, you need some support programs/libraries in order to compile the toolchain. To install them:
$ sudo apt-get install flex bison libgmp3-dev libmpfr-dev autoconf texinfo build-essential
Next, decide where you want to install your toolchain. They generally go in /usr/local/, so I'm going to assume /usr/local/cross-cortex for this tutorial. To save yourself some typing, set this path into a shell variable:
$ mkdir /opt/cross-cortex
$ export TOOLPATH=/opt/cross-cortex
This is the easiest step: unpack, configure, build.
$ tar -xvjf binutils-2.19.tar.bz2 |
---|
$ cd binutils-2.19 |
$ mkdir build |
$ cd build |
$ ../configure --target=arm-elf --prefix=$TOOLPATH --enable-interwork --enable-multilib --with-gnu-as --with-gnu-ld --disable-nls |
$ make all |
$ sudo make install |
$ export PATH=${TOOLPATH}/bin:$PATH |
Now you have your ARM "binutils" (assembler, linker, disassembler ...) in your PATH. They are fully capable of handling Thumb2.
Step 2: basic GCCIn this step we build a "basic" GCC (that is, a GCC without any support libs, which we'll use in order to build all the libraries for our target). But first we need to make a slight modification in the configuration files. Out of the box, the GCC 4.3.1/newlib combo won't compile properly, giving a very weird "Link tests are not allowed after GCC_NO_EXECUTABLES" error. After a bit of googling, I found the solution for this:
$ tar -xvjf gcc-4.4.0.tar.bz2 |
---|
$ cd gcc-4.4.0/libstdc++-v3 |
$ gedit configure.ac |
Now find the line which says "AC_LIBTOOL_DLOPEN" and comment it out by adding a "#" before it:[注:这里用最新的4.4.0,而没有用原文的4.3.1,编译是一样的]
# AC_LIBTOOL_DLOPENGreat, now we know it will compile, so let's do it:
$ mkdir build |
---|
$ cd build |
$ ../configure --target=arm-elf --prefix=$TOOLPATH --enable-interwork --enable-multilib --enable-languages="c,c++" --with-newlib --without-headers --disable-shared --with-gnu-as --with-gnu-ld |
$ make all-gcc |
$ sudo make install-gcc |
Again, some modifications are in order before we start compiling. Because the CVS version of Newlib doesn't seem to have all the required support for Thumb2 yet, we need to tell Newlib to skip some of its libraries when compiling:
$ cd [directory where the newlib CVS is located] |
---|
$ gedit configure.ac |
Find this fragment of code:
arm-*-elf* | strongarm-*-elf* | xscale-*-elf* | arm*-*-eabi* )On one of the systems I ran the above sequence, it terminated with errors, complaining that autoconf 2.59 was not found. I don't know why that happens. 2.59 seems to be quite ancient, and the build ran equally well with 2.61 (the version of autoconf on the system that gave the error). If this happens to you, first execute autoconf --version to find the actual version of your autoconf, then do this:
$ joe config/override.m4Once again, now we're ready to actually compile Newlib. But we need to tell it to compile for Thumb2. As already specified, I'm not a specialist when it comes to Newlib's build system, so I chosed the quick, dirty and not so elegant solution of providing the compilation flags directly from the command line. Also, as I wanted my library to be as small as possible (as opposed to as fast as possible) and I only wanted to keep what's needed from it in the final executable, I added the "-ffunction-sections -fdata-sections" flags to allow the linker to perform dead code stripping:
$ mkdir build |
---|
$ cd build |
$ ../configure --target=arm-elf --prefix=$TOOLPATH --enable-interwork --disable-newlib-supplied-syscalls --with-gnu-ld --with-gnu-as --disable-shared |
$ make CFLAGS_FOR_TARGET="-ffunction-sections -fdata-sections -DPREFER_SIZE_OVER_SPEED -D__OPTIMIZE_SIZE__ -Os -fomit-frame-pointer -mcpu=cortex-m3 -mthumb -D__thumb2__ -D__BUFSIZ__=256" CCASFLAGS="-mcpu=cortex-m3 -mthumb -D__thumb2__" |
$ sudo make install |
Some notes about the flags used in the above sequence:
Finally, in the last step of our tutorial, we complete the GCC build. In this stage, a number of compiler support libraries are built (most notably libgcc.a). Fortunately this is simpler that the Newlib compilation step, as long as you remember that we want to build our compiler support libraries for the Cortex architecture:
$ cd gcc-4.3.1/build |
---|
$ make CFLAGS="-mcpu=cortex-m3 -mthumb" CXXFLAGS="-mcpu=cortex-m3 -mthumb" LIBCXXFLAGS="-mcpu=cortex-m3 -mthumb" all |
$ sudo make install |
参考: http://eluaproject.dreamhosters.com/en/Building_GCC_for_Cortex
附录:
arm-elf-gcc已经可以使用了,下面就利用这个工具来编译代码,
startup.c
# arm-elf-gcc -g -mcpu=cortex-m3 -mthumb startup.c -nostartfiles -T standalone.ld -c -o startup.o
# arm-elf-ld -T standalone.ld startup.o -o startup
# arm-elf-objcopy -O binary -S -R .note -R .comment startup startup.bin
这里的startup.bin就是可以烧写到flash的二进制文件,startup就是可以用gdb进行调试的,后续文章将继续介绍。