NOTE, Nov. 6, 2005

The instructions on this page for building a toolchain for either SH3 or SH4 are not entirely correct anymore as there are a couple patches that you need in some cases that don't come with crosstool-0.38. Rather than clutter up the instructions with workarounds, the next version of crosstool is imminent so be patient for a few days, as that version should work out of the box.


This page shows you how to build an SH-based cross-compiler toolchain using Dan Kegel's crosstool utility, which you can find [WWW] here. This new set of instructions improves on the old set in a couple of ways:

  1. It allows you to build a toolchain using newer distros of Linux that have the gcc-4.0.x compiler. This newer compiler will fail to compile some of the older GNU software components so you can't just use the older "demo" scripts that come with crosstool if you're building on, say, Fedora Core 4 or similarly recent or upgraded distros.

  2. It shows how to use the "sysroot" feature to build a slightly cleaner chroot-style directory for the target system.

  3. Instead of using an actual kernel source tree, it uses the sanitized kernel headers to bypass the kernel configuration and build step entirely.

Space requirements

You'll need space for the following parts of the build (all reasonably accurate estimates):

  1. The crosstool directory itself: 50M.

  2. The downloaded tarballs directory: 60M.

  3. The "build" directory: 1.2G.

  4. The "results" directory: 170M.

Note that, depending on where you place some of the above, you may have to combine some of these values. For example, by default, the "build" directory will be placed in the "crosstool" directory itself, so lay out your build structure accordingly.

The software components

The current version of crosstool-0.38 supports building a toolchain with the following components:

which is about as new as you're going to get. (Actually, if you're really ambitious and you want to build the toolchain with gcc-4.0.2, read the section at the bottom of the page about how to add newer, unsupported software versions into crosstool. At the moment, gcc-4.0.2 is the only part that you'd have to do extra work for and it will certainly be incorporated into the next version of crosstool anyway.)

The configuration files

Here are the three files you need to create and that you run from within the crosstool-0.38 directory to create your toolchain. Customize to taste. (Obviously, the filenames are flexible.)


set -ex
PREFIX=~/ct/sh3 ; mkdir -p ${PREFIX}
export GCC_LANGUAGES="c,c++"

eval `cat sh3-sw.dat sh3-vars.dat` sh --notest

echo Done.

Note how you can explicitly set the PREFIX variable, rather than the more common RESULTS_TOP variable. This lets you avoid creating what I consider the really annoying, long-winded intermediate directories in the path to the resulting toolchain. But it's a matter of taste.


Make your choice in software components:



Finally, set some of the build options:

TARGET_CFLAGS="-O -m3 -ml"

The above is clearly building a little-endian toolchain. If you want a big-endian result, make the following changes:

TARGET_CFLAGS="-O -m3 -mb"

(I'm also open to suggestions as to other possible options that should be set here.)

Building the toolchain

Copy your three configuration files and scripts into the crosstool directory, then run (in this case):

$ ./

Go for dinner. Eventually, the entire toolchain (and more) will show up in your ${PREFIX} directory.

(Technically, you should make sure the LD_LIBRARY_PATH variable is unset before you build your toolchain.)

Using the toolchain

Assuming you leave the toolchain where it was generated, you can add the directory ${PREFIX}/bin (or whatever is appropriate for you) to your search path, then test compile a short program with something like:

$ sh3-unknown-linux-gnu-gcc fred.c

If the compile appears to succeed, running the file command on the resulting executable should confirm that it is, indeed, an SH-based executable with the correct endianness.

Example wmat@dim:~/dev/sh3/bin$ file a.out a.out: ELF 32-bit LSB executable, Hitachi SH, version 1 (SYSV), for GNU/Linux 2.4.3, dynamically linked (uses shared libs), not stripped

NOTE: The reference to "for GNU/Linux 2.4.3" in the output is the lowest kernel version the executable will run on.

Updating to leading-edge software components

If you're determined to use the absolutely latest GNU software components that aren't yet supported in your version of crosstool, it's not hard. As an example, crosstool-0.38 doesn't officially support gcc-4.0.2 but adding in that support is easy.

If that software doesn't need any crosstool patches, you don't need to do anything, just select that version and rebuild the toolchain. On the other hand, it's possible that you'll have to pull some patches forward from the earlier version of that software if some of the issues addressed by those patches still exist.

In the case of gcc-4.0.2, it appears that you'd want to create a gcc-4.0.2 directory under crosstool's patches directory, and copy over the patches fix-fixincl.patch and pr20815-fix.patch from the gcc-4.0.1 directory. Then build as before.

Note that you san safely select the component of linux-libc-headers- even though there is no patches subdirectory for it, simply because it requires no patches.

Outstanding issues

This build incorporates linuxthreads into the final result. I'd like to know how to incorporate NPTL instead, but trying to select NPTL causes build errors. I'm open to suggestions.

last edited 2005-12-21 20:36:08 by wmat