Building GCC 6.2.0 for the Amiga

There are some ready build cross compilers for the Amiga like Amidevcpp, but no ready build cross compiler uses gcc 5.* or 6.*.
Well, let' see what happens:
To play around I create a working folder in /opt/cross and change to that directory:
mkdir /opt/cross
cd /opt/cross
Let's pick a mirror from https://www.gnu.org/prep/ftp.html and get the recent binutils and gcc.
cd /opt/cross
wget http://ftp.hawo.stw.uni-erlangen.de/gnu/binutils/binutils-2.27.tar.bz2
wget http://ftp.hawo.stw.uni-erlangen.de/gnu/gcc/gcc-6.2.0/gcc-6.2.0.tar.bz2
tar xjf binutils-2.27.tar.bz2
tar xjf gcc-6.2.0.tar.bz2
To build each package I create separate folders:
mkdir build-binutils-2.27
mkdir build-gcc-6.2.0
And start with binutils:
cd /opt/cross/build-binutils-2.27
/opt/cross/binutils-2.27/configure --prefix=/opt/cross --target=m68k-*-amigaos --program-prefix=m68k-amigaos-
make -j8
Whoops:
< *** BFD does not support target m68k-*-amigaos.
Maybe I deal with amigaos later since a workaround might be using the elf format and elf2hunk.
So I try again:
cd build-binutils-2.27
/opt/cross/binutils-2.27/configure --prefix=/opt/cross --target=m68k-elf --program-prefix=m68k-elf-
Ok - works.
Later I detected that I have to patch binutils because the library call macros do not use %reg:
vi /opt/cross/binutils-2.27/gas/config/tc-m68k.h
and replace
#if defined (M68KCOFF) || defined (OBJ_ELF)
#define REGISTER_PREFIX_OPTIONAL 0
#else
with
#if defined (M68KCOFF) || defined (OBJ_ELF)
#define REGISTER_PREFIX_OPTIONAL 1
#else 
then save and quit (:wq)
Now build it:
make -j8
Ok. Install it:
make install
Ok. Add the bin folder to the path:
export PATH=$PATH:/opt/cross/bin

Now build gcc.
cd /opt/cross/build-gcc-6.2.0/
/opt/cross/gcc-6.2.0/configure --prefix=/opt/cross --target=m68k-elf --program-prefix=m68k-elf-
Ok. Build it:
make -j8
Whoa - it's building many languages. Let's abort and reconfigure:
rm -rf *
/opt/cross/gcc-6.2.0/configure --prefix=/opt/cross --target=m68k-elf --program-prefix=m68k-elf- --enable-languages=c,c++ 
make -j8
Error:
...
< checking for shl_load... configure: error: Link tests are not allowed after GCC_NO_EXECUTABLES.
< make[1]: *** [configure-target-libstdc++-v3] Error 1
Let's exclude libstdc++:
/opt/cross/gcc-6.2.0/configure --prefix=/opt/cross --target=m68k-elf --program-prefix=m68k-elf- --enable-languages=c,c++ --disable-libstdcxx 
make -j8
Error:
...
< make[3]: *** [ssp.lo] Error 1
Also exclude libssp:
/opt/cross/gcc-6.2.0/configure --prefix=/opt/cross --target=m68k-elf --program-prefix=m68k-elf- --enable-languages=c,c++ --disable-libstdcxx --disable-libssp
make -j8
Ok. Install it:
make install
Now we need a simple C file to test. I'll use this hello.c:
#include <stdio.h>
int main(int argc, char **argv) {
        puts("hello world!");
        printf("argc=%ld\r\n", argc);
        for (int i = 0; i < argc; ++i) {
                printf("arg=%s\r\n", argv[i]);
        }
        return 0;
}
And compile it:
m68k-elf-gcc hello.c -o hello.elf
And? Ups:
< hello.c:1:19: fatal error: stdio.h: No such file or directory
< #include <stdio.h>
Oh yes, I need the header files. And I need libc.a...
Build glibc!? Hm - guess not.
Since I plan to compile for the Amiga plattform I take the headers from the Amidevcpp package. Unfortunally I have to install it using Windows to grab the headers or use some InstallShield extractor (isxunpack).
I put the include folders into '/opt/cross/m68k-elf'.
Try again:
m68k-elf-gcc hello.c -o hello.elf
And it compiles - but does not link:
< /opt/cross/lib/gcc/m68k-elf/6.2.0/../../../../m68k-elf/bin/ld: cannot find -lc
Well - without proper crtbegin/crtend and proper lib, it won't run/link. But the compiler produces 68k output.

mini crt stubs and a mini lib

I created own crt stubs, and a libc with printf - it's to long to describe here - and put the files to /opt/cross/lib/gcc/m68k-elf/6.2.0/ and try again: That's enough for the hello.c example.
m68k-elf-gcc hello.c -o hello.elf
Hm:
< /opt/cross/lib/gcc/m68k-elf/6.2.0/../../../../m68k-elf/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000080000074
Let's try the -r option:
m68k-elf-gcc -r hello.c -o hello.elf
Bingo!

convert elf file to hunk

After searching the for elf2hunk I got a version, but it did not work perfect. Some debugging later: elf2hunk.c
gcc elf2hunk.c -o elf2hunk
and
./elf2hunk -v hello.elf hello
Tada - there is a hello executable in hunk format. Let's test it on an Amiga (or WinUAE):
hello abcd test 1 2 3 4
Woot:
< hello world!
< argc=7
< arg=hello
< arg=abcd
< arg=test
< arg=1
< arg=2
< arg=3
< arg=4

That's enough for today :-)

Update

Meanwhile I ported the patches to gcc 6.2.0 and it starts to work.
 m68k-amiga-gcc hello.c -noixemul -o hello 
Compiles and links an executable and the result is running :-)
Also the dhrystone test compiles (with plenty warnings) and is running. I updated the benchmarks :-)

Update 2

The current state is found at https://github.com/bebbo. You'll find there the complete amigaos-cross-toolchain or only the binutils and the gcc 6
rev: 1.23