pre { margin: 5px 20px; border: 1px dashed #666; padding: 5px; background: #f8f8f8; white-space: pre-wrap; /* css-3 */ white-space: -moz-pre-wrap; /* Mozilla, since 1999 */ white-space: -pre-wrap; /* Opera 4-6 */ white-space: -o-pre-wrap; /* Opera 7 */ word-wrap: break-word; /* Internet Explorer 5.5+ */ } Linux Embedded: 2013

Pagine

venerdì 11 gennaio 2013

Build a cortex-M3 toolchain with semihosting support

I have a STM32F4 Discovery board and I use codesourcery lite toolchain to develop on it.
Unfortunately there's no semihosting support with that toolchain. So i decided to build my own from scratch.

Prerequisites

I do all the work with a Kubuntu 12.10 distro. So all the commands in this tutorial are referred to that. First of all we have to install al tools and libraries required:
sudo apt-get install flex bison libgmp3-dev libmpfr-dev \
libncurses5-dev libmpc-dev autoconf texinfo build-essential \
libftdi-dev libexpat1 libexpat1-dev

Get the source

To start we have to download all the source we need:

Build binutils

Extract binutils sources and then configure and build as follow:
export PREFIX=/usr/local/cross-cortex
mkdir binutils-2.23.1/build
cd binutils-2.23.1/build
../configure --prefix=$PREFIX --target=arm-none-eabi \
--enable-interwork --enable-multilib --disable-nls \
--disable-libssp
make all
sudo make install
export PATH=${PREFIX}/bin:$PATH
Now we have ARM "binutils" (assembler, linker, disassembler ...) in the PATH. They are fully capable of handling Thumb2.

Build gcc bootstrap

Extract gcc and newlib sources but before start building we have to edit some files.
In gcc/config/arm/t-arm-elf check that the following lines are not commented (there are probably commented out with #):
MULTILIB_OPTIONS      += march=armv7
MULTILIB_DIRNAMES     += thumb2
MULTILIB_EXCEPTIONS   += march=armv7* marm/*march=armv7*
MULTILIB_MATCHES      += march?armv7=march?armv7-a
MULTILIB_MATCHES      += march?armv7=march?armv7-r
MULTILIB_MATCHES      += march?armv7=march?armv7-m
MULTILIB_MATCHES      += march?armv7=mcpu?cortex-a8
MULTILIB_MATCHES      += march?armv7=mcpu?cortex-r4
MULTILIB_MATCHES      += march?armv7=mcpu?cortex-m3
In gcc/config/arm/elf.h find the definition "#define SUBTARGET_ASM_FLOAT_SPEC" and change as follow:
#define SUBTARGET_ASM_FLOAT_SPEC "%{mapcs-float:-mfloat} \ 
%{mhard-float:-mfpu=fpa} \ 
%{!mhard-float:-mfpu=softfpa}" 
#endif
Now we are ready to build gcc:
mkdir gcc-4.7.2/build
cd gcc-4.7.2/build
../configure --target=arm-none-eabi -prefix=$PREFIX \
--enable-interwork --enable-multilib --enable-languages="c" \
--with-newlib --with-headers=../../newlib-2.0.0/newlib/libc/include/ \
--disable-libssp --disable-nls --with-system-zlib --with-float=soft \
-with-gnu-as --with-gnu-ld --with-cpu=cortex-m3 \
--with-tune=cortex-m3 --with-mode=thumb
make all-gcc
sudo make install-gcc
Now we can check if the compiler can generate thumb2 code. Try the following code and we should see something similar:
$ arm-none-eabi-gcc --print-multi-lib
.;
thumb;@mthumb
thumb/thumb2;@mthumb@march=armv7
If the result is different, we miss something in the files modified earlier. (gcc/config/arm/t-arm-elf and gcc/config/arm/elf.h)

Build newlib

Now we are ready to build newlib:
cd newlib-2.0.0
mkdir build
cd build
export PATH=$PATH:$PREFIX/bin
../configure --target=arm-none-eabi --prefix=$PREFIX \
--enable-interwork --enable-multilib --disable-libssp --disable-nls \
--with-float=soft --with-gnu-as --with-gnu-ld \
make CFLAGS_FOR_TARGET="-ffunction-sections \
                        -fdata-sections \
                        -DPREFER_SIZE_OVER_SPEED \
                        -D__OPTIMIZE_SIZE__ \
                        -Os \
                        -fomit-frame-pointer \
                        -mcpu=cortex-m3 \
                        -mthumb \
                        -mfix-cortex-m3-ldrd \
                        -mfloat-abi=softfp \
                        -D__thumb2__ \
                        -D__BUFSIZ__=256" \
               CCASFLAGS="-mcpu=cortex-m3 \
                          -mthumb \
                          -mfix-cortex-m3-ldrd \
                          -D__thumb2__"
sudo su
export PATH=$PATH:/usr/local/cross-cortex/bin
make install
Note that in all the guide you found, in the configuration line command add the option --disable-newlib-supplied-syscalls. This is useful if you want to redirect standard output to a serial line for example. But if you want do use ARM semihosting facility it disable it.

Build full GCC

Now we can complete the GCC build:
cd gcc-4.7.2/build
make all
sudo make install
At this point you should have a fully functional Cortex GCC toolchain with semihosting support.