Accueil > Uncategorized > A minimal linux system – Part 2

A minimal linux system – Part 2

Ok, we have a cross-compiler toolchain. now we can deal with the real thing. At this point, I was tempted to compile standard C and C++ standard libraries – But this is simply not possible without the header files provided .. by the linux kernel.
It is quite logical because implementations of standard C functions rely on system calls (it seems stupid once said) – So one cannot simply compile a standard library without kernel binaries, that’s why it is called a kernel after all.

so the most dramatic moment of this tutorial begins now : building the linux kernel itself.

Linux kernel

ARCH=x86
LINUX_KERNEL_VERSION=3.6.11

mkdir -p $TOOLCHAIN_INSTALL_DIR/src
pushd $TOOLCHAIN_INSTALL_DIR/src
curl "http://www.kernel.org/pub/linux/kernel/v3.0/linux-$LINUX_KERNEL_VERSION.tar.bz2" -o linux-$LINUX_KERNEL_VERSION.tar.bz2
tar -jxvf linux-$LINUX_KERNEL_VERSION.tar.bz2
ln -s linux-$LINUX_KERNEL_VERSION linux
pushd linux

#linux kernel compilation options is normally setup by editing a .config file at the root of the linux source tree.
#This configuration file may be easily modified by doing :
# apt-get install libncurses5-dev
# make menuconfig
#
# I have my own settings right in this file, so
curl "http://julien-rialland.fr/pub/linux/linux-x86-config" -o .config

#At this step I can apply various kernel patches, for example Prempt-RT (http://en.wikipedia.org/wiki/RTLinux)
#curl "http://www.kernel.org/pub/linux/kernel/projects/rt/3.6/patch-3.6.11-rt25.patch.bz2" -o patch-3.6.11-rt25.patch.bz2
#cat patch-3.6.11-rt25.patch.bz2 | bunzip2 | patch -p1

make "ARCH=$ARCH" "CROSS_COMPILE=$TOOLCHAIN_TARGET-" bzImage
make "ARCH=$ARCH" "INSTALL_HDR_PATH=$TOOLCHAIN_INSTALL_DIR" headers_install

#we also copy some headers that will be needed by libc headers
for h in `find ./include/asm-generic -name "*.h"`; do
    name=`basename $h`
    if [ ! -f "$TOOLCHAIN_INSTALL_DIR/include/asm/$name" ]; then
        ln -s $h $TOOLCHAIN_INSTALL_DIR/include/asm/$name
    fi
done

popd
popd

standard C library : uclibc

On a decent platform, executable are linked with a standard library, which provides implementation of all standard C functions (no ‘hello world’ without it 🙂 )
We use our freshly-compiled compiler in order to compile it (uh, a lot of compilations don’t you think ?)
There are several implementation of the standard C library : the GNU libc (which may be quite bloated for my use), newlib, dietlibc, musl, uclibc. I choosed uclibc because I have no imagination, but I think that any of the listed ones would be fine.

UCLIBC_VERSION=0.9.33.1
push $TOOLCHAIN_INSTALL_DIR/src
curl "http://www.uclibc.org/downloads/uClibc-$UCLIBC_VERSION.tar.bz2" -o uClibc-$UCLIBC_VERSION.tar.bz2
tar -jxvf uClibc-$UCLIBC_VERSION.tar.bz2
ln -s uClibc-$UCLIBC_VERSION uClibc
pushd uClibc

#we have to provide a configuration - it can be done by running make menuconfig just like linux kernel, or you can reuse mine
curl http://julien-rialland.fr/pub/uclibc.config -o .config
make "PREFIX=$TOOLCHAIN_INSTALL_DIR" "CROSS=$TOOLCHAIN_TARGET-" "KERNEL_HEADERS=$TOOLCHAIN_INSTALL_DIR/include"
make "PREFIX=$TOOLCHAIN_INSTALL_DIR" "CROSS=$TOOLCHAIN_TARGET-" "KERNEL_HEADERS=$TOOLCHAIN_INSTALL_DIR/include" install_dev install

#and copy the headers and libraries at the right places into the toolchain's tree : 
cp $TOOLCHAIN_INSTALL_DIR/usr/$TOOLCHAIN_TARGET/usr/lib/* "$TOOLCHAIN_INSTALL_DIR/$TOOLCHAIN_TARGET/lib"
cp -Rf $TOOLCHAIN_INSTALL_DIR/usr/$TOOLCHAIN_TARGET/usr/include/* "$TOOLCHAIN_INSTALL_DIR/include"
popd
popd

Now the build chain is quite complete, we can now build plain executables for our linux system. The only missing thing is the c++ runtime, which is only useful if you are going to run apps written in c++ on your system – as it is not the case with mine, I skipped it. But here are the steps for building it :

C++ runtime

As usual, fetch the sources, configure, and compile !

UCLIBCXX_VERSION=0.2.4
push $TOOLCHAIN_INSTALL_DIR/src
curl "http://cxx.uclibc.org/src/uClibc++-$UCLIBCXX_VERSION.tar.bz2" -o uClibc++-$UCLIBCXX_VERSION.tar.bz2
tar -jxvf uClibc++-$UCLIBCXX_VERSION.tar.bz2
ln -s uClibc++-$UCLIBCXX_VERSION uClibc++
pushd uClibc++
     #make menuconfig, or get my .config at julien-rialland.fr
     make "PREFIX=$TOOLCHAIN_INSTALL_DIR" "CROSS=$TOOLCHAIN_TARGET-" "KERNEL_HEADERS=$TOOLCHAIN_INSTALL_DIR/include"
popd
popd
Publicités
Catégories :Uncategorized
  1. Aucun commentaire pour l’instant.
  1. No trackbacks yet.

Laisser un commentaire

Choisissez une méthode de connexion pour poster votre commentaire:

Logo WordPress.com

Vous commentez à l'aide de votre compte WordPress.com. Déconnexion /  Changer )

Photo Google+

Vous commentez à l'aide de votre compte Google+. Déconnexion /  Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion /  Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion /  Changer )

Connexion à %s

%d blogueurs aiment cette page :