Building a Win32 hosted sh4-crosscompiler using GCC
This page describes for short and specifically, how to built a sh4-crosscompiler that
will run in the CygWin environment hosted on win32 (The linux dudes just ignore the
Cygnus specific stuff). This example uses a whole lot of
default values/paths etc, there may be better ways to do it, but this seemed to have worked
for me.
note: the procedure is exactly the same for building the ARM toolchain (for use with the AICA/Sound Coprocessor inside the dc), except you use arm-elf for the target and leave out any sh4 specific switches in any commands.
Install CygWin and make sure the environment and everything else (/tmp dir for bash!) is set up right. Try compiling hello.c to be sure.
In order to have quick access to a ready to use unix/linux style environment that CygWin provides, create a batchfile like this:
@ECHO OFF
SET MAKE_MODE=UNIX
SET PATH=C:GNUCYGWINBIN;%PATH%
SET PATH=C:GNUCYGWINlibgcc-libi686-pc-cygwin2.95.2;%PATH%
SET PATH=C:GNUCYGWINUSRLOCALBIN;%PATH%
BASH
Now run this batchfile, and check your mountpoints.
$ mount
The output should look like this:
Device Directory Type Flags
C:GNUCYGWINUSRINCLUDE /include user binmode
C:GNUCYGWINBIN /usr/bin user binmode
C:GNUCYGWINLIB /usr/lib user binmode
C:GNUCYGWIN / user binmode
C: /c user binmode
If needed, use umount/mount to adjust the mountpoints.
binutils-2.10.tar.gz
gcc-core-2.95.2.tar.gz
gcc-g++-2.95.2.tar.gz (if you want cpp support)
newlib-1.8.2.tar.gz
Be sure to keep the tarballs for later experiments. (notice that you will need a certain amount of free disc space in order to built the binaries of the crosscompiler.)
Get the tar'ed source and unpack it with 'gnutar' or 'star', don't use winzip to unpack the tar archive, it will not unpack symlinks correctly. (Wincommander seems to work however.)
gcc/binutils-2.10 // put the sources here
gcc-2.95.2
newlib-1.8.2
build-binutils // working dirs during build
build-gcc
build-newlib
note: the directory must be located in the same filesystem as the cygwin stuff. (you gotta mount it properly...)
Now start a CygWin environment using the previously created batchfile.
Setup some environment variables:
$ target=sh-elf
$ prefix=/usr/local // toplevel installation directory (default is /usr/local).
$ i=$prefix/bin
$ PATH=$i:$PATH
$ cd gcc
$ ../binutils-2.10/configure --target=$target --prefix=$prefix
... this will take a while ...
$ make all install
... and after a short (*G*) time you will find your SH Binutils in /usr/local/bin
$ cd ../build-gcc
$ ../gcc-2.95.2/configure --target=$target --prefix=$prefix --without-headers --without-libs --with-cpu=sh4
... patience ...
now in the makefile that has been created in build-gcc/gcc/ search for the line
# Additional target-dependent options for compiling libgcc2.a.
TARGET_LIBGCC2_CFLAGS =
and add the option -Dinhibit_libc
$ make LANGUAGES="c" all install
... and voila ... compiler is in /usr/local/bin as well.
note: if GCC fails to build, have a look at this hack.
$ ../newlib-1.8.2/configure --target=$target --prefix=$prefix
...mmmh...
$ make all install
CC_FOR_TARGET=$i/${target}-gcc
AS_FOR_TARGET=$i/${target}-as
LD_FOR_TARGET=$i/${target}-ld
AR_FOR_TARGET=$i/${target}-ar
RANLIB_FOR_TARGET=$i/${target}-ranlib
... dumdidum ...
So far so good. However, some targets seem to need a patch to fix some endian problem.
... so that's it! Have Fun! To use the sh-tools, start a cygwin environment using that batchfile you created in the beginning. (Adding your crossgcc install dir to your path may come in handy at this point... ;=P).
void start(void) {
}
compile ...
$ sh-elf-gcc -ml -Wl,-Ttext,0x8c010000 -nostartfiles -nostdlib -e _start test.c -o test.elf -lgcc
to get a flat binary type:
$ sh-elf-objcopy -O binary test.elf test.bin
08.07.2000 Groepaz/Hitmen
13.08.2000 changed main to start in test proggy.
04.09.2000 added some details, note on arm stuff.
note: the procedure is exactly the same for building the ARM toolchain (for use with the AICA/Sound Coprocessor inside the dc), except you use arm-elf for the target and leave out any sh4 specific switches in any commands.
Preparing CygWin
Get the latest Cygnus GCC distribution here. Lazy People just download this file which will, when run, grab all the tarballs and install CygWin for you. Be SURE to get the most recent version of the whole toolchain, there were certain problems with older versions. (Don't use the b20 thing that came in one big install file once.)Install CygWin and make sure the environment and everything else (/tmp dir for bash!) is set up right. Try compiling hello.c to be sure.
In order to have quick access to a ready to use unix/linux style environment that CygWin provides, create a batchfile like this:
@ECHO OFF
SET MAKE_MODE=UNIX
SET PATH=C:GNUCYGWINBIN;%PATH%
SET PATH=C:GNUCYGWINlibgcc-libi686-pc-cygwin2.95.2;%PATH%
SET PATH=C:GNUCYGWINUSRLOCALBIN;%PATH%
BASH
Now run this batchfile, and check your mountpoints.
$ mount
The output should look like this:
Device Directory Type Flags
C:GNUCYGWINUSRINCLUDE /include user binmode
C:GNUCYGWINBIN /usr/bin user binmode
C:GNUCYGWINLIB /usr/lib user binmode
C:GNUCYGWIN / user binmode
C: /c user binmode
If needed, use umount/mount to adjust the mountpoints.
Get Binutils/GCC Source
go to your favourite GNU ftp site (or just try again here) and grab the following files:binutils-2.10.tar.gz
gcc-core-2.95.2.tar.gz
gcc-g++-2.95.2.tar.gz (if you want cpp support)
newlib-1.8.2.tar.gz
Be sure to keep the tarballs for later experiments. (notice that you will need a certain amount of free disc space in order to built the binaries of the crosscompiler.)
Get the tar'ed source and unpack it with 'gnutar' or 'star', don't use winzip to unpack the tar archive, it will not unpack symlinks correctly. (Wincommander seems to work however.)
Prepare environment for building
Create a directory structure like this:gcc/binutils-2.10 // put the sources here
gcc-2.95.2
newlib-1.8.2
build-binutils // working dirs during build
build-gcc
build-newlib
note: the directory must be located in the same filesystem as the cygwin stuff. (you gotta mount it properly...)
Now start a CygWin environment using the previously created batchfile.
Setup some environment variables:
$ target=sh-elf
$ prefix=/usr/local // toplevel installation directory (default is /usr/local).
$ i=$prefix/bin
$ PATH=$i:$PATH
$ cd gcc
Building Binutils
$ cd build-binutils$ ../binutils-2.10/configure --target=$target --prefix=$prefix
... this will take a while ...
$ make all install
... and after a short (*G*) time you will find your SH Binutils in /usr/local/bin
Building GCC
now we are ready to built the compiler...$ cd ../build-gcc
$ ../gcc-2.95.2/configure --target=$target --prefix=$prefix --without-headers --without-libs --with-cpu=sh4
... patience ...
now in the makefile that has been created in build-gcc/gcc/ search for the line
# Additional target-dependent options for compiling libgcc2.a.
TARGET_LIBGCC2_CFLAGS =
and add the option -Dinhibit_libc
$ make LANGUAGES="c" all install
... and voila ... compiler is in /usr/local/bin as well.
note: if GCC fails to build, have a look at this hack.
Building Newlib
$ cd ../build-newlib$ ../newlib-1.8.2/configure --target=$target --prefix=$prefix
...mmmh...
$ make all install
CC_FOR_TARGET=$i/${target}-gcc
AS_FOR_TARGET=$i/${target}-as
LD_FOR_TARGET=$i/${target}-ld
AR_FOR_TARGET=$i/${target}-ar
RANLIB_FOR_TARGET=$i/${target}-ranlib
... dumdidum ...
So far so good. However, some targets seem to need a patch to fix some endian problem.
... so that's it! Have Fun! To use the sh-tools, start a cygwin environment using that batchfile you created in the beginning. (Adding your crossgcc install dir to your path may come in handy at this point... ;=P).
Test the Compiler
create a little dummy program (test.c) ...void start(void) {
}
compile ...
$ sh-elf-gcc -ml -Wl,-Ttext,0x8c010000 -nostartfiles -nostdlib -e _start test.c -o test.elf -lgcc
to get a flat binary type:
$ sh-elf-objcopy -O binary test.elf test.bin
08.07.2000 Groepaz/Hitmen
13.08.2000 changed main to start in test proggy.
04.09.2000 added some details, note on arm stuff.