Table of Contents

MIPS Cross Compiler build

Commercial Version

There is a commercial free version available from Mentor Graphics.

Sourcery CodeBench Lite Edition

Build from source

What we need:

From a bare ubuntu 12.04 LTS you need to add the following packages:

sudo apt-get install build-essential m4 texinfo libncurses5-dev bzip2

Copy the script “build_mips.sh” from below to your home directory. Open a terminal window in ubuntu by starting dash and typing terminal. The click terminal which opens the terminal. To change the current directory to your home directory type

cd

To see the full path of the current directory:

pwd

List the directory contents with

ls -la

Change the file attributes of “build_mips.sh” to executable with

chmod a+x build_mips.sh

Look into the bash script with

less build_mips.sh

Extend the search path by appending “export PATH=$HOME/site/bin:$PATH” to your .profile file

Log out and login again to make the PATH variable active. Check your PATH variable with

echo $PATH

You should see the <..>/site/bin as the first entry.

Run the build script with

./build_mips.sh

Here is the shell script for building the cross compiler chain. Handmade stuff so you need to adapt. The script will build to a place in your home directory. So first add this place to your PATH variable.

build_mips.sh
#!/bin/bash -ev
# -e => exit on error
# -v => verbose output
 
# Mips Cross Compiler
 
# Base directory
mkdir -p mips
# src directory
mkdir -p mips/src
# build directory
mkdir -p mips/build
# original archives
mkdir -p mips/orig
 
# Set the destination
export MYMIPS=${HOME}/site
 
# Versions
GMPVERSION="4.3.2"
PPLVERSION="0.12.1"
BINUTILSVERSION="2.24"
MPFRVERSION="2.4.2"
MPCVERSION="1.0.2"
ISLVERSION="0.12.2"
CLOOGVERSION="0.18.1"
GCCVERSION="4.8.2"
NEWLIBVERSION="1.20.0"
GDBVERSION="7.5.1"
 
 
########################################
# Binutils
########################################
 
# Get the archives
cd mips/orig 
if [ ! -e "binutils-${BINUTILSVERSION}.tar.bz2" ] ; then
  wget http://ftp.gnu.org/gnu/binutils/binutils-${BINUTILSVERSION}.tar.bz2
fi
 
# Unpack to source directory
cd ../src
if [ ! -d "binutils-${BINUTILSVERSION}" ] ; then
tar -xvjf ../orig/binutils-${BINUTILSVERSION}.tar.bz2
fi 
 
cd ../build
mkdir -p binutils
cd binutils
if [ ! -e "config.status" ] ; then
../../src/binutils-${BINUTILSVERSION}/configure --target=mipsel-none-elf \
--prefix=$MYMIPS
fi
 
if [ ! -e "${MYMIPS}/bin/mipsel-none-elf-ld" ] ; then
make
make install
fi
cd ../../..
 
########################################
# GMP
########################################
 
# Get the archives
cd mips/orig 
if [ ! -e "gmp-${GMPVERSION}.tar.bz2" ] ; then
  wget ftp://ftp.halifax.rwth-aachen.de/gnu/gmp/gmp-4.3.2.tar.bz2
fi
 
# Unpack to source directory
cd ../src
if [ ! -d "gmp-${GMPVERSION}" ] ; then
tar -xjvf ../orig/gmp-${GMPVERSION}.tar.bz2
fi 
 
# Build
cd ../build
mkdir -p gmp
cd gmp
if [ ! -e "config.status" ] ; then	
../../src/gmp-${GMPVERSION}/configure --prefix=$MYMIPS --enable-cxx
fi
if [ ! -e "${MYMIPS}/lib/libgmp.a" ] ; then
make
make install
fi
cd ../../..
 
 
########################################
# PPL
########################################
 
# Get the archives
#cd mips/orig 
#if [ ! -e "ppl-${PPLVERSION}.tar.bz2" ] ; then
#  wget ftp://ftp.cs.unipr.it/pub/ppl/releases/${PPLVERSION}/ppl-${PPLVERSION}.tar.bz2
#fi
 
# Unpack to source directory
#cd ../src
#if [ ! -d "ppl-${PPLVERSION}" ] ; then
#tar -xjvf ../orig/ppl-${PPLVERSION}.tar.bz2
#fi 
 
#cd ../build
#mkdir -p ppl
#cd ppl
#if [ ! -e "config.status" ] ; then
#../../src/ppl-${PPLVERSION}/configure --prefix=$MYMIPS --with-gmp=$MYMIPS --with-sysroot=$MYMIPS
#fi
#if [ ! -e "${MYMIPS}/lib/libppl.a" ] ; then
#make 
#make install
#fi
#cd ../../..
 
 
######################################
# MPFR library
#######################################
 
# Get the archives
cd mips/orig 
if [ ! -e "mpfr-${MPFRVERSION}.tar.bz2" ] ; then
  wget ftp://ftp.halifax.rwth-aachen.de/gnu/mpfr/mpfr-${MPFRVERSION}.tar.bz2
fi
 
# Unpack to source directory
cd ../src
if [ ! -d "mpfr-${MPFRVERSION}" ] ; then
tar -xvjf ../orig/mpfr-${MPFRVERSION}.tar.bz2
fi
 
cd ../build
mkdir -p mpfr
cd mpfr
if [ ! -e "config.status" ] ; then
../../src/mpfr-${MPFRVERSION}/configure --prefix=$MYMIPS --with-gmp=$MYMIPS 
fi
 
if [ ! -e "${MYMIPS}/lib/libmpfr.a" ] ; then
make
make install
fi
cd ../../.. 
 
######################################
# MPC library
#######################################
 
# Get the archives
cd mips/orig 
if [ ! -e "mpc-${MPCVERSION}.tar.gz" ] ; then
  wget ftp://ftp.halifax.rwth-aachen.de/gnu/mpc/mpc-${MPCVERSION}.tar.gz
fi
 
# Unpack to source directory
cd ../src
if [ ! -d "mpc-${MPCVERSION}" ] ; then
tar -xvzf ../orig/mpc-${MPCVERSION}.tar.gz
fi
 
cd ../build
mkdir -p mpc
cd mpc
if [ ! -e "config.status" ] ; then
../../src/mpc-${MPCVERSION}/configure --prefix=$MYMIPS --with-gmp=$MYMIPS --with-mpfr=$MYMIPS
fi
 
if [ ! -e "${MYMIPS}/lib/libmpc.a" ] ; then
make
make install
fi
cd ../../.. 
 
 
 
##############
# ISL
##############
 
# Get the archives
cd mips/orig 
if [ ! -e "isl-${ISLVERSION}.tar.bz2" ] ; then
  wget  ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-${ISLVERSION}.tar.bz2
fi
 
 
# Unpack to source directory
cd ../src
if [ ! -d "isl-${ISLVERSION}" ] ; then
tar -xvjf ../orig/isl-${ISLVERSION}.tar.bz2
fi
 
cd ../build
mkdir -p isl
cd isl
if [ ! -e "config.status" ] ; then
../../src/isl-${ISLVERSION}/configure --prefix=$MYMIPS --with-gmp-prefix=$MYMIPS 
fi
 
if [ ! -e "${MYMIPS}/lib/libisl.a" ] ; then
make
make install
fi
cd ../../.. 
 
##############
# CLOOG
##############
 
# Get the archives
cd mips/orig 
if [ ! -e "cloog-${CLOOGVERSION}.tar.gz" ] ; then
  wget  ftp://gcc.gnu.org/pub/gcc/infrastructure/cloog-${CLOOGVERSION}.tar.gz
fi
 
 
# Unpack to source directory
cd ../src
if [ ! -d "cloog-${CLOOGVERSION}" ] ; then
tar -xvzf ../orig/cloog-${CLOOGVERSION}.tar.gz
fi
 
cd ../build
mkdir -p cloog
cd cloog
if [ ! -e "config.status" ] ; then
../../src/cloog-${CLOOGVERSION}/configure --prefix=$MYMIPS --with-gmp-prefix=$MYMIPS --with-isl=system --with-isl-prefix=$MYMIPS
fi
 
if [ ! -e "${MYMIPS}/lib/libcloog-isl.a" ] ; then
make
make install
fi
cd ../../.. 
 
 
########################################
# newlib
########################################
 
# Get the archives
cd mips/orig
 
if [ ! -e "newlib-${NEWLIBVERSION}.tar.gz" ] ; then
  wget ftp://sourceware.org/pub/newlib/newlib-${NEWLIBVERSION}.tar.gz
fi
 
# Unpack to source directory
cd ../src
if [ ! -d "newlib-${NEWLIBVERSION}" ] ; then
tar -xvzf ../orig/newlib-${NEWLIBVERSION}.tar.gz
fi 
 
cd ../..
 
########################################
# gcc first stage
########################################
 
# Get the archives
cd mips/orig 
if [ ! -e "gcc-${GCCVERSION}.tar.bz2" ] ; then
  wget ftp://ftp.halifax.rwth-aachen.de/gnu/gcc/gcc-${GCCVERSION}/gcc-${GCCVERSION}.tar.bz2
fi
 
# Unpack to source directory
cd ../src
if [ ! -d "gcc-${GCCVERSION}" ] ; then
tar -xvjf ../orig/gcc-${GCCVERSION}.tar.bz2
fi 
 
cd ../build
mkdir -p gcc-stage1
cd gcc-stage1
if [ ! -e "config.status" ] ; then
LDFLAGS="-Wl,-rpath,$MYMIPS/lib" \
../../src/gcc-${GCCVERSION}/configure --target=mipsel-none-elf \
--prefix=$MYMIPS \
--with-gmp=$MYMIPS \
--with-mpfr=$MYMIPS \
--with-mpc=$MYMIPS \
--with-isl=$MYMIPS \
--with-newlib --without-headers \
--disable-shared --disable-threads --disable-libssp \
--disable-libgomp --disable-libmudflap \
--enable-languages="c"  
fi
 
if [ ! -e "${MYMIPS}/bin/mipsel-none-elf-gcc" ] ; then
make all-gcc
make install-gcc
fi
cd ../../..
 
########################################
# newlib
########################################
 
# Build
cd mips/build
mkdir -p newlib
cd newlib
if [ ! -e "config.status" ] ; then
../../src/newlib-${NEWLIBVERSION}/configure --prefix=$MYMIPS --target=mipsel-none-elf 
fi
 
if [ ! -e "${MYMIPS}/mipsel-none-elf/lib/libc.a" ] ; then
make
make install
fi
cd ../../.. 
 
########################################
# gcc second stage
########################################
 
cd mips/build
mkdir -p gcc-stage2
cd gcc-stage2
if [ ! -e "config.status" ] ; then
LDFLAGS="-Wl,-rpath,$MYMIPS/lib" \
../../src/gcc-${GCCVERSION}/configure --target=mipsel-none-elf \
--prefix=$MYMIPS \
--with-gmp=$MYMIPS \
--with-mpfr=$MYMIPS \
--with-mpc=$MYMIPS \
--with-isl=$MYMIPS \
--with-newlib  \
--disable-shared --disable-threads --disable-libssp \
--disable-libgomp --disable-libmudflap \
--enable-languages="c,c++"  
fi
 
if [ ! -e "${MYMIPS}/bin/mipsel-none-elf-g++" ] ; then
make all
make install
fi
cd ../../..
 
 
########################################
# GDB
########################################
 
# Get the archives
cd mips/orig 
if [ ! -e "gdb-${GDBVERSION}.tar.bz2" ] ; then
  wget ftp://ftp.halifax.rwth-aachen.de/gnu/gdb/gdb-${GDBVERSION}.tar.bz2
fi
 
# Unpack to source directory
cd ../src
if [ ! -d "gdb-${GDBVERSION}" ] ; then
tar -xvjf ../orig/gdb-${GDBVERSION}.tar.bz2
fi 
 
cd ../build
mkdir -p gdb
cd gdb
if [ ! -e "config.status" ] ; then
../../src/gdb-${GDBVERSION}/configure --target=mipsel-none-elf  \
--enable-sim-trace \
--enable-sim-stdio \
--prefix=$MYMIPS 
fi
 
if [ ! -e "${MYMIPS}/bin/mipsel-none-elf-gdb" ] ; then
make
make install
fi
cd ../../..

Compile and Run an example

Here is an example mini c code to test the compiler.

hello.c
/* Hello world */
#include <stdio.h>
 
int myfunc(int a, int b){
  int k; 
  k = a + b; 
 
  k += 7; 
  k *= 6; 
  return k; 
}
 
int main(){
  int i,j,m;  
  j = 3;
  m = 2;
 
  for(i=0;i<5;i++){
    m += myfunc(i,j);  
    printf("m is %d\n",m);
  }
  return 0;
}

Compile to Assembler for viewing

mipsel-none-elf-gcc -S hello.c

The output assembler code is in hello.s.

Compile and link ready for simulation with instruction set simulator

mipsel-none-elf-gcc -o hello -Tidt.ld hello.c

Run the code

mipsel-none-elf-run hello

Analyze

Produce an annoted source code showing how often lines are executed.

gcc -fprofile-arcs -ftest-coverage -o hello hello.c
./hello
gcov hello.c

This produces the file hello.c.gcov showing the annotated source file.

Tracing in instruction set simulator

Run with instruction tracing in the simulator

mipsel-none-elf-gcc -Tidt.ld -o hello hello.c
mipsel-none-elf-run --trace-insn=on --trace-file trace hello

Test the compiler

sudo apt-get install dejagnu
cd
cd mips/build/gcc-stage2
make check-gcc RUNTESTFLAGS=--target_board=mips-sim

Install git and download a simple project

Install git and download a simple project

sudo apt-get install git
cd
mkdir projects
cd projects
git clone https://git.etech.fh-augsburg.de/friedrich.beckmann/myfirst.git

Now you have the simple project “myfirst” in your directory.

Try the MIPS Cross Compiler

Change to the src directory and compile the code with the cross compiler.

cd myfirst
cd src
mipsel-none-elf-gcc -S hello.c
less hello.s

Now you have the compiled assembler code “hello.s”. To compile to binary do:

mipsel-none-elf-gcc -o hello -Tidt.ld hello.c

Now you have the binary “hello”. You can run the binary in the instruction set simulator with

mipsel-none-elf-run hello

This will run the binary with the mips instruction set simulator. You should see “Hello World”.

Open OCD

dt_openocd