[J-core] Setting up a bare metal compiler for J2

Joh-Tob Schäg johtobsch at gmail.com
Sun Oct 13 15:51:26 UTC 2019


So far i have only been using a SH2 compiler without the J-Core
patches but after half a day of work and a better understanding what
is going on when using or compiling gcc i made an automated script to
create a j2 capable cross-compiler from scratch.
All the hard work was done by other people and it took so long only
because i lacked understanding. I hope that this script and save
similarly less knowledgeable people some time. Please correct and
comment.

# We assume the script is executed in an empty directory
export PREFIX="$(pwd)/cross" ## The results will be collect here
mkdir cross

# the architecture and executable format we're targeting
export TARGET=sh2-elf ## The underlying pattern is
cpu_architecture-compiler_vendor-Operating_System-ABI

## I am picking sh2 as cpu architecture because the patches do not add
j-core as an target which is fine. sh would have been fine as well and
i have no strong reason to pick sh2 over sh. I think both should work
## vendor and OS assume their default value, that means our linker and
compiler will not include OS specific header files. If you want your
code to run on the Linux port to the Mimas "sh2-linux-elf" is the
right choice.
## Since we have an elf loader on the Mimas we want the ABI to be elf.
The abi has influence how the code is loaded in to ram among other
things like calling conventions, edianess ...

## add the cross-compiler to our PATH, the creation of gcc requires
access to a compiled binutils for this terminal session only all
gcc/binutils binaries will be found via the path mechanism (this
allows you to write sh2-elf-gcc instead of having to give a complete
path to the executable)
## if you want this to be persistent you should write this line in a
place that get's executed each time a shell is created like .bashrc or
something
export PATH="$PREFIX/bin:$PATH"

#Download fitting versions of gcc and binutils
wget https://ftp.gnu.org/gnu/binutils/binutils-2.32.tar.gz
wget https://ftp.gnu.org/gnu/gcc/gcc-8.3.0/gcc-8.3.0.tar.gz
tar -xzf binutils-2.32.tar.gz
tar -xzf gcc-8.3.0.tar.gz

## The patches to add j2 support come from here. It also adds support
for the musl c library, the fdpic binary format ... we do not need all
of this. fdpic is not going to be activated during the compilation
since we do not provide the appropriate flag.
git clone https://github.com/richfelker/musl-cross-make.git

## Applies all the patches even the once for ARM but that does no harm i think
for file in musl-cross-make/patches/binutils-2.32/* ; do patch -d
binutils-2.32/ -p1 < ${file}; done

## Does include a kitchen sink of patches but i rather include all
patches than hope the file name will stay unchanged
for file in musl-cross-make/patches/gcc-8.3.0/* ; do patch -d
gcc-8.3.0/ -p1 < ${file}; done

## Start building binutils
cd binutils-2.32/

mkdir build
cd build
../configure --target=$TARGET --prefix="$PREFIX" --with-cpu=mj2
--with-sysroot --disable-nls --disable-werror
## --with-cpu=mj2 enables support for j2 you can check that it is
enabled by running "sh2-elf-gcc -Q --help=target" later
make
make install

cd ../..


## Start gcc
cd gcc-8.3.0/

contrib/download_prerequisites # 30 MBs of additional downloads
dependencies to build gcc
mkdir build
cd build
../configure --target=$TARGET --prefix="$PREFIX" --with-cpu=mj2
--disable-nls --enable-languages=c,c++ --without-headers
## Using --without-headers causes the gcc support library to be built
without requiring any library support. This disables a few features
which are generally uninteresting for embedded systems.


# -j n tries to parrelize the compilation of binares over n process
since my CPU has 8 hardware threads and i did not want to assure
responsiveness of the OS is choose 7
make -j 7 all-gcc
make -j 7 all-target-libgcc
make -j 7 install-gcc
make -j 7 install-target-libgcc

cd ../.. ## go back to where you stared from.

## What was going in the bigger picture: We used a c/c++ compiler to
compile version of GCC which compiles J-core to the native hardware.
## We have a resulting binary which runs on the machine you compiled
it on. This binary is a C/C++ compiler which has the patches to be
able to compile j2 binaries.
## How this compiler is properly used is still an open question to me.

Thank you for your attention. Please correct anything stupid, wrong or
misleading.
When doing 'sh2-elf-gcc -Q --help=targets' it has an empty value for the keys:
  -matomic-model=
  -mdiv=
  -mfixed-range=
  -mfused-madd

is that fine?


More information about the J-core mailing list