Compiling the ARM Cortex M4 Toolchain Yourself

Nabil has a great blog post for compiling the ARM Cortex M3 toolchain yourself.

It appears that using his method for compiling the ARM toolchain allows you to make use of the FPU.

Nabil’s directions were great, but they only directly apply to Mac OS X. The German site UbuntuUsers also had a great article in their wiki that helped me get through some sticky places. Here are some directions for getting the ARM Cortex M4 toolchain built on Ubuntu:

First, we’ll need to get download some source packages:

You can do this by hand via the links below, either click on the project name to locate the latest releases yourself, or click on the package (.tar.gz) name to directly download the package I used:

binutilsbinutils-2.21.1a.tar.bz2
newlibnewlib-1.19.0.tar.gz
GCCgcc-4.6.2.tar.bz2
GDBgdb-7.3.1.tar.bz2

Or here is a quick set of wgets you could use:

mkdir src
cd src
wget http://ftp.gnu.org/gnu/binutils/binutils-2.21.1a.tar.bz2
tar jxvf binutils-2.21.1a.tar.bz2
wget ftp://sources.redhat.com/pub/newlib/newlib-1.19.0.tar.gz
tar zxvf newlib-1.19.0.tar.gz
wget ftp://mirrors.kernel.org/gnu/gcc/gcc-4.6.2/gcc-4.6.2.tar.bz2
tar jxvf gcc-4.6.2.tar.bz2
wget http://ftp.gnu.org/gnu/gdb/gdb-7.3.1.tar.bz2
tar jxvf gdb-7.3.1.tar.bz2

Second, let’s get some dependancies:

$ sudo apt-get install flex bison libgmp3-dev libmpfr-dev libncurses5-dev 
libmpc-dev autoconf texinfo build-essential libftdi-dev libexpat1 libexpat1-dev

Now, we’ll compile our toolchain:

Be sure to change /home/csamuelson/mat to the directory you would like to have the toolchain installed to.

Compile binutils

cd binutils-2.21.1/
./configure  --target=arm-none-eabi --prefix=/home/csamuelson/mat --enable-interwork --enable-multilib --disable-nls --disable-libssp
make all
make install
cd ..

Now start to compile GCC

We just get gcc bootstrapped (make all-gcc). Make sure –with-headers is set to the correct directory.

cd gcc-4.6.2/
mkdir objdir
cd objdir/
../configure --target=arm-none-eabi --prefix=/home/csamuelson/mat/ --enable-interwork --enable-multilib --enable-languages="c" --with-newlib --with-headers=../../newlib-1.19.0/newlib/libc/include/ --disable-libssp --disable-nls --with-system-zlib
make all-gcc
make install-gcc
cd ../..

Compile newlib:

cd newlib-1.19.0/
./configure --target=arm-none-eabi --prefix=/home/csamuelson/mat --enable-interwork --enable-multilib --disable-libssp --disable-nls
make all
make install
cd ..

Finish compiling GCC:

cd gcc-4.6.2/objdir/
make all
sudo make install
cd ../..

Now make GDB:

cd gdb-7.3.1/
./configure --target=arm-none-eabi --prefix=/home/csamuelson/mat/ --enable-interwork --enable-multilib --disable-libssp --disable-nls
make all
sudo make install

Some options explanations:
–prefix=/home/csamuelson/mat/: Set the install directory. I put it in a directory call /home/csamuelson/mat/ You will probably want to change this to a directory you want it installed in!
–enable-interwork: Allows ARM and Thumb code to be used
–enable-multilib: Build multible versions of some libs. E.g. one with soft float and one with hard
–disable-nls: Tells gcc to only support American English output messages
–disable-libssp: Don’t include stack smashing protection
–with-system-zlib: Fixes the error: configure: error: Link tests are not allowed after GCC_NO_EXECUTABLES

Hardware Floating Point
Now, when you run arm-none-eabi-gcc -print-multi-lib you will see support for the FPU:

$ arm-none-eabi-gcc -print-multi-lib
.;
thumb;@mthumb
fpu;@mfloat-abi=hard

8 Responses

  1. JohanS November 30, 2011 / 7:25 am

    This works great!

    Two minors that made it work for me:
    For binutils ./configure: Add –no-werror

    After first gcc install:
    Create a link between cc and gcc

  2. DaveS January 31, 2012 / 11:42 pm

    I recently picked up a F4DISCOVERY board and was playing with it. I had trouble with a call to strncpy() but just wrote my own version. When strncmp() started causing trouble, I tried rebuilding the tools as described here. But that didn’t help.
    The problem was that in spite of compiling with -mthumb, the code had a 32 bit form of the blx instruction for this call. Which is illegal for the thumb.
    I fixed that problem by rebuilding newlib. Not really knowing what to try I used –enable-thumb and –disable-interwork for configure and it works for now.

    • Voisin April 4, 2012 / 9:27 am

      Hello,
      I also got problem with the libraries which are not compiled in thumb mode, but in arm mode, even in /thumb directory.
      Recompiling using -enable-thumb and -disable-interwork does not work on my side.
      If anybody had a clue, I’m interested …
      Tx in advance, and tx for the tuto.

  3. Andre March 28, 2012 / 3:15 pm

    I built my toolchain according your suggestions here and tried to integrate the Toolchain into Code::Blocks

    Now I’m using gcc with the following flags to create the object-files:
    -mthumb -mthumb-interwork -mlittle-endian -Wall -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard
    which works fine

    and the gcc to do the linking with the following flags:
    -o bin/Debug/STM32F4-Test.elf
    -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mthumb-interwork -mthumb -Wall -mlittle-endian -T”/home/andre/code/STM32F4-Test/stm32_flash.ld” -Wl,-Map,STM32F4-Test.map -g

    but when it comes to the Linking I get the following:
    error: obj/Debug/main.c.o uses VFP register arguments, bin/Debug/STM32F4-Test.elf does not

    I hope you can give me a hint where the problem might be. (when I’m using -mfloat-abi=soft everything runs without any problems but it wouldn’t use the Hardware-FPU)

  4. manuelk July 25, 2012 / 8:23 pm

    hi,

    works fine =) i used it with
    binutils-2.22
    gcc-4.7.1
    newlib-1.20.0
    gdb-7.4

    the only think what makes a littl probs is the newlib-1.20.0 …

    after the ./configure –target=arm-none-e………
    you need to do a
    ln -s arm-none-eabi-gcc arm-none-eabi-cc
    (newlib use arm-none-eabi-cc …)

    and do a
    export PATH=$PATH:/your_install/bin

    so it works fine =)

    greez
    Manuel

  5. Thomas September 28, 2012 / 12:36 pm

    Adding a “–with-fpu=vfp” to the gcc configure even enables the fpu ;-)

  6. Shiv April 5, 2013 / 10:39 pm

    Hi. I am getting an error when installing NewLib. It says: .././libgloss/arm/crt0.S:37: Error: unknown architecture `armv6s-m’
    What does this mean? Can you please help me with this.

    Thanks,
    Shiv

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>