News Archive (1999-2012) | 2013-current at LinuxGizmos | Current Tech News Portal |    About   

Running Linux on the Sega Dreamcast (Part 3)

Sep 24, 1997 — by Rick Lehrbaum — from the LinuxDevices Archive — 5 views

Building the tools

The first step in the process of getting Linux running on the Dreamcast is to construct the tools we need, including a cross assembler, linker, compiler, and a C runtime library. In the next section we will use these tools to build the operating system, and a basic shell application that Linux will run at the end of its boot process. Finally, we'll organize everything into a ramdisk image and burn a CD.

The process is long and involved, but highly instructive.

Building a cross assembler, linker, and bootstrap compiler

We'll start by building a cross assembler and linker, and then a bootstrap compiler: a minimal compiler that can be used to build runtime libraries and operating system kernels, but not generic applications. We can't build a complete compilation environment all at once, because many of the header files needed to do so come from the C runtime library, and don't exist until the runtime library itself is built.

Log in as the root user, and follow the script shown in Figure 2. The first steps in the figure set up some environment variables to save typing later, and to make sure that /usr/local/bin is in the PATH. The source code for the binutils package is decompressed, and then patched with minor changes that make it more specific to the Dreamcast's microprocessor. The binutils package is then configured, compiled and installed; the resulting executables end up in /usr/local/bin, with names like sh4-linux-as and sh4-linux-ld.

The same process is then repeated for the bootstrap compiler, only with slightly different arguments that reflect the fact that we are not building a complete compiler setup yet. The bootstrap compiler's executable is called sh4-linux-gcc, and like the binutils package, is installed in /usr/local/bin.


    # export TARGET=sh4-linux
    # export PREFIX=/usr/local
    # export PATH=${PATH}:${PREFIX}/bin

    # tar xzf binutils-2.11.2.tar.gz
    # patch -p0 < binutils-2.11.2-sh-linux.diff
    # mkdir -p build-binutils && cd build-binutils
    # ../binutils-2.11.2/configure --target=$TARGET
    --prefix=$PREFIX
    # make all install
    # cd ..

    # tar xzf gcc-3.0.1.tar.gz
    # patch -p0 < gcc-3.0.1-sh-linux.diff
    # mkdir -p build-gcc && cd build-gcc
    # ../gcc-3.0.1/configure
    --target=$TARGET --prefix=$PREFIX
    --without-headers --with-newlib
    --disable-shared --enable-languages=c
    # make all-gcc install-gcc
    # cd ..

Figure 2: Commands to build binutils and a bootstrap gcc

Configure the kernel sources

The next step in the toolchain construction process is to configure Linux kernel header files so that the runtime library's build process can extract important bits of information from them during its build process. The idea is to run the equivalent of the configure command on the kernel source code, which you accomplish by using the procedure is shown in Figure 3. Run these commands as they appear in the figure, but don't change any of the settings in the menu that appears: the kernel is already properly configured. Simply exit menuconfig by selecting Exit with the right arrow key on your keyboard, then press ENTER.


    # tar xzf kernel-sh-linux-dreamcast.tar.gz
    # patch -p0 < kernel-sh-linux-dreamcast.diff
    # cd kernel
    # make ARCH=sh CROSS_COMPILE=sh4-linux- menuconfig
    # cd ..

Figure 3: Configuring the kernel sources.

In the instructions, the trailing dash at the end of CROSS_COMPILE is not a mistake: the CROSS_COMPILE macro is used as a prefix for the tools invoked by make; without the dash at the end, the macro won't work.

Building a runtime library

Now that we have a bootstrap compiler and properly-configured kernel, we can build a runtime library environment and header files. The runtime library we will use is GNU's glibc; this library includes familiar functions like printf(), but it also includes the dynamic linker (Linux's equivalent of Win32's DLLs) and several other programs. We can get by without these programs, however, so the steps needed to set them up will be the subject of a future article.

The procedure for building glibc is shown in Figure 4. The first commands uncompress and patch the library, then copy the Linux kernel header files into their proper locations. In contrast to the previous procedures, we actually invoke make twice when building glibc: the first invocation builds the library, but does not install it; the second step installs the library, specifying in detail the installation locations of the library's various components.

In the interest of saving some time, the touch command used between the two make invocations tricks glibc into thinking it has properly built several programs that we don't need. And finally, the echo command writes a linker command file called libc.so without path information, which (perhaps nonintuitively) enables the compiler to properly locate the installed libraries.

If you rebooted your computer or logged out since you built binutils and the bootstrap gcc, you will need to restore the values of PATH, TARGET and PREFIX to the values used in previous steps before building glibc.

Glibc is a substantial, complicated body of code, and you will notice that it takes a long time to build even on fairly powerful hardware. After you get the process going, this would be a good time to take a break.


    # tar xzf glibc-2.2.4.tar.gz
    # patch -p0 < glibc-2.2.4-sh-linux.diff
    # mkdir -p build-glibc && cd build-glibc

    # mkdir -p ${PREFIX}/${TARGET}/include
    # cp -r ../kernel/include/linux
    ${PREFIX}/${TARGET}/include
    # cp -r ../kernel/include/asm-sh
    ${PREFIX}/${TARGET}/include/asm

    # CC=sh4-linux-gcc ../glibc-2.2.4/configure
    --host=$TARGET --prefix=$PREFIX
    --disable-debug --disable-profile
    --disable-sanity-checks
    --with-headers=${PREFIX}/${TARGET}/include

    # make
    # touch iconv/iconv_prog login/pt_chown
    # make install_root=${PREFIX}/${TARGET}
    prefix="" install
    # echo "GROUP ( libc.so.6 libc_nonshared.a )"
    > ${PREFIX}/${TARGET}/lib/libc.so
    # cd ..

Figure 4: Instructions to build glibc

Rebuild the cross compiler

With properly installed header files and a runtime library, we can now build a complete c/c++ cross compiler. The commands to do so are in Figure 5. We are reusing the source tree from the bootstrap compiler, so we skip the familiar untar-plus-patch steps.


    # mkdir -p build-gcc2 && cd build-gcc2
    # ../gcc-3.0.1/configure --target=$TARGET
    --prefix=$PREFIX --enable-languages=c,c++
    # make all install
    # cd ..

Figure 5: Instructions to build the complete cross compiler

Continued



Story navigation . . .

 
This article was originally published on LinuxDevices.com and has been donated to the open source community by QuinStreet Inc. Please visit LinuxToday.com for up-to-date news and articles about Linux and open source.



Comments are closed.