Building LinuxSH from Scratch
This document describes how to build (almost¹) everything you need to get
a Linux system up and running on hardware based around a SuperH processor,
building everything from the source code.
This is not a trivial exercise, and
it will take some time, but you will end up with a system where you can
make changes easily, and start configuring the system for your individual
needs. However if all you need is the ability to run code, you may be
better off downloading pre-built binary files, see
here for more details.
¹. This document does not describe how to boot the kernel onto your
hardware. There are many ways to do this, which you use will normally
depends on what hardware you are using, and what
facilities you have available in the target firmware.
Getting the source
First you will need to download the sources you need. Most of this is
available from SourceForge, where
it can either be checked out from the
CVS repository
or downloaded as a tarball.
You will need the following components from SourceForge:
-
binutils Object file handling tools (linker, librarian etc.).
-
gcc-core The gcc C compiler and associated libraries.
-
kernel The Linux kernel itself.
While sources are still being checked into SourceForge, you will need to
obtain some files from other places. These consist of the standard distributions,
plus patches for LinuxSH:
-
glibc The GNU C library.
-
Download the base 2.1.3 release, plus the crypt and linuxthreads
add-ons (available here
or your favorite GNU archive).
-
Apply Kojima-san's
patch.
Building the basic tools
Once you have all the sources, you can start building the tools. First you
need to decide where things are going to be installed. Throughout these
instructions I have used two environment variables:
-
SH_BASE to point to the directory containing the source
code. In particular $SH_BASE/kernel must contain the Linux
kernel sources.
-
SH_INSTALL to point to where you want to install the built tools.
for example:
export SH_BASE=/home/rockhopper/users/stuart/sourceforge
export SH_INSTALL=/usr/local/sh
binutils
Nice and simple to get you started:
cd binutils
./configure --target=sh-linux-gnu --prefix=$SH_INSTALL
make
make install
cd ..
Finally add the built tools to your path:
PATH=$SH_INSTALL/bin:$PATH
gcc
The gcc build is similar, except that it is recommended to build in a
separate directory, and not use the source directory for building:
mkdir gcc-sh-linux-gnu
cd gcc-sh-linux-gnu
../gcc-core/configure --target=sh-linux-gnu --prefix=$SH_INSTALL
make
make install
cd ..
Note that if you get errors when the make tries to call gperf, there are two
possible solutions:
-
the easiest is to touch gcc-core/gcc/c-gperf.h to bring it up to
date.
-
alternatively install gperf!
Linux kernel
You now have all the tools you need to build the Linux kernel. As long
as you are building for one of the supported systems, this should be
pretty simple. If however you are building for an unsupported system it
is probably a good idea to start with the generic configuration,
and get that working, before trying anything more complex.
First configure the kernel. I prefer menuconfig but you could
use config or xconfig:
cd kernel
make ARCH=sh menuconfig
Pay particular attention to the options in the:
Processor type and features
section. It is also worth checking most of the other sections to see if
something is enabled which shouldn't be. If you are using the GDB
booting mechanism then you will need to enable:
Kernel hacking - GDB Stub kernel debug
Finally make sure:
Character devices - Serial interface type
is correctly set for SCI or SCIF.
Then do the kernel build:
make ARCH=sh dep
make ARCH=sh
Don't forget the ARCH=sh argument, as unlike previous kernel releases
the default is to not cross compile, so without it you'll probably end
up trying to build an x86 kernel, which won't work!
User applications
The first step in building user applications is to build the C library.
This is probably the most complex part of the build, you must use the
correct tool revisions, and be very careful changing any other options
or deviating from these instructions.
The glibc build appears to be very sensitive to
small configuration problems, and frequently appears to fail in ways
which are unrelated to the original problem. There is no warranty
on these instructions if you have changed anything!
First if you are building for SH4 you need to make a small change to the
kernel. In kernel/include/asm-sh/sigcontext.h replace:
unsigned long long sc_xdregs[8];
with
unsigned long sc_xfpregs[16];
Next create the build directory:
mkdir glibc-sh-linux-gnu
cd glibc-sh-linux-gnu
and create the file configparms in it (take care over the spelling
of this filename, it may not be what you thought it was at first glance!):
CFLAGS := -ml -m4 -O $(CFLAGS)
ASFLAGS-.o := -ml
LDFLAGS-relocatable := -ml
crts-predefined=yes
build-programs=no
CC=sh-linux-gnu-gcc -ml -m4
RANLIB=sh-linux-gnu-ranlib
AS=sh-linux-gnu-gcc -ml
LD=sh-linux-gnu-ld -ml
AR=sh-linux-gnu-ar
BUILD_CC=gcc
The -m4 and -ml options specify the CPU and endian you are building for, change
these as required.
Next configure the library and perform the build:
CC="sh-linux-gnu-gcc -ml -m4" ../glibc-2.1.3/configure \
--host=sh4-linux-gnu --build=`arch`-linux \
--disable-debug --disable-profile \
--with-headers=$SH_BASE/kernel/include --prefix=$SH_INSTALL \
--enable-add-ons=crypt,linuxthreads
make
Finally install the library. As gcc is being used in a cross development
environment, the libraries are installed in a different place to normal:
touch iconv/iconv_prog login/pt_chown
make install_root=$SH_INSTALL/sh-linux-gnu prefix="" install
and edit $SH_INSTALL/sh-linux-gnu/lib/libc.so to
remove the /lib/ from the start of the two filenames.
Finally remember to install the links to the kernel include directories:
ln -s $SH_BASE/kernel/include/asm-sh $SH_INSTALL/sh-linux-gnu/include/asm
ln -s $SH_BASE/kernel/include/linux $SH_INSTALL/sh-linux-gnu/include/linux
You should now be able to build a test application, for example:
sh-linux-gnu-gcc -ml -m4 hello.c -o hello
Building a root file system
The easiest way to get started is to use a pre-built root file system image.
However if you are starting from scratch, this is a brief overview of
what is needed.
First construct the file system you want on the host system. For example:
mkdir rootfs
mkdir rootfs/dev
mknod rootfs/dev/console c 5 1
mknod rootfs/dev/ttyS0 c 4 64
mkdir rootfs/lib
(cd $SH_INSTALL/sh-linux-gnu/lib ; tar cf - *.so*) | (cd rootfs/lib ; tar xf -)
mkdir rootfs/{proc,sbin,bin,usr,etc} rootfs/usr/{bin,sbin}
Populate the file system with the executables you need, not forgetting an
/sbin/init and shell of some form.
Next generate an image of the root file system. For example:
dd if=/dev/zero of=/tmp/image bs=1k count=4096
mke2fs -F -vm0 /tmp/image 4096
mkdir /tmp/fs
mount -o loop /tmp/image /tmp/fs
(cd rootfs ; tar cf - .) | (cd /tmp/fs ; tar xvf -)
umount /tmp/fs
echo Compressing...
cat /tmp/image | gzip -c > initrd.img
rm /tmp/image /tmp/fs
|