From ba2ecd725f5610e037827e03caff9452a4656ebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Lov=C3=A9n?= Date: Tue, 29 Nov 2016 09:19:44 +0100 Subject: [PATCH] [TOOLCHAIN] Hosted toolchain with musl libc --- .gitignore | 2 + Makefile | 14 ++++- activate | 2 +- libc/Makefile | 30 +++++++++++ libc/crt0.S | 25 +++++++++ util/binutils.patch | 113 ++++++++++++++++++++++++++++++++++++++++ util/build_toolchain.sh | 50 ++++++++++++++++-- util/gcc.patch | 108 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 336 insertions(+), 8 deletions(-) create mode 100644 libc/Makefile create mode 100644 libc/crt0.S create mode 100644 util/binutils.patch create mode 100644 util/gcc.patch diff --git a/.gitignore b/.gitignore index 4bfe27b..cb25845 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ sysroot/ *.img *.log kernel/obj/ +*.o +*.a tags diff --git a/Makefile b/Makefile index 51ce646..df74f7d 100644 --- a/Makefile +++ b/Makefile @@ -4,14 +4,14 @@ ifeq ($(MITTOS64),) $(error Build environment is not activated. Please source activate) endif -.PHONY: all clean kernel +.PHONY: all clean kernel libc SHELL := bash CC=$(TARGET)-gcc FLAGS_TO_PASS:= \ CC=$(CC) -all: kernel +all: kernel libc # A trick to only build phony target if necessary kernel: @@ -20,9 +20,19 @@ ifeq ($(shell make -sqC kernel || echo 1), 1) $(MAKE) -C kernel install $(FLAGS_TO_PASS) endif +libc: +ifeq ($(shell make -sqC libc || echo 1), 1) + @(. util/helpers.sh; print_info "Building c library") + $(MAKE) -C libc install $(FLAGS_TO_PASS) +endif + +tags: + ctags -R kernel + clean: @(. util/helpers.sh; print_info "Cleaning up") $(MAKE) -C kernel clean rm -f mittos64.iso + $(MAKE) -C libc clean rm -f qemu-error.log rm -f serial.log diff --git a/activate b/activate index b03366e..d4202e7 100755 --- a/activate +++ b/activate @@ -10,7 +10,7 @@ export BUILDROOT=`pwd` export SYSROOT=${BUILDROOT}/sysroot export TOOLCHAIN=${BUILDROOT}/toolchain export PATH=${TOOLCHAIN}/bin:${PATH} -export TARGET=x86_64-elf +export TARGET=x86_64-mittos64 # shortcuts for some useful tools alias dbg="x86_64-elf-linux-gdb" diff --git a/libc/Makefile b/libc/Makefile new file mode 100644 index 0000000..1ee77b5 --- /dev/null +++ b/libc/Makefile @@ -0,0 +1,30 @@ +ifeq ($(MITTOS64),) + $(error Build environment is not activated. Please source activate) +endif + +LIBDIR := $(SYSROOT)/usr/lib + +CRT := crt0 +CRT_OBJ := $(addprefix obj/, $(patsubst %,%.o,$(CRT))) + +ASFLAGS := -ggdb + +all: $(CRT_OBJ) + +OBJ_DIRS := $(sort $(dir $(CRT_OBJ))) +$(CRT_OBJ): | $(OBJ_DIRS) +$(OBJ_DIRS): + mkdir -p $@ + +obj/%.o:%.S + $(COMPILE.S) $^ -o $@ + +$(LIBDIR)/%: obj/% + cp $< $@ + +install: $(patsubst %,$(LIBDIR)/%.o,$(CRT)) + +clean: + rm -rf obj/ + +.PHONY: all clean install diff --git a/libc/crt0.S b/libc/crt0.S new file mode 100644 index 0000000..c8bfc72 --- /dev/null +++ b/libc/crt0.S @@ -0,0 +1,25 @@ + .intel_syntax noprefix + .section .text + .global _start + + _start: + movq rbp, 0 + push rbp + push rbp + mov rbp, rsp + + push rsi + push rdi + + call _init + + pop rdi + pop rsi + + call main + + call _fini + + jmp $ + +.size _start, . - _start diff --git a/util/binutils.patch b/util/binutils.patch new file mode 100644 index 0000000..0af1d8b --- /dev/null +++ b/util/binutils.patch @@ -0,0 +1,113 @@ +--- config.sub 2016-12-01 11:48:25.000000000 +0100 ++++ config.sub 2016-12-01 11:48:36.000000000 +0100 +@@ -1377,6 +1377,7 @@ + -gnu* | -bsd* | -mach* | -minix* | -genix* | -ultrix* | -irix* \ + | -*vms* | -sco* | -esix* | -isc* | -aix* | -cnk* | -sunos | -sunos[34]*\ + | -hpux* | -unos* | -osf* | -luna* | -dgux* | -auroraux* | -solaris* \ ++ | -mittos64* \ + | -sym* | -kopensolaris* | -plan9* \ + | -amigaos* | -amigados* | -msdos* | -newsos* | -unicos* | -aof* \ + | -aos* | -aros* | -cloudabi* \ + +--- bfd/config.bfd 2016-12-01 11:52:56.000000000 +0100 ++++ bfd/config.bfd 2016-12-01 11:57:59.000000000 +0100 +@@ -168,6 +168,18 @@ + ;; + + # START OF targmatch.h ++ i[3-7]86-*-mittos64*) ++ targ_defvec=i386_elf32_vec ++ targ_selvecs= ++ targ64_selvecs=x86_64_elf64_vec ++ ;; ++#ifdef BFD64 ++ x86_64-*-mittos64*) ++ targ_defvec=x86_64_elf64_vec ++ targ_selvecs=i386_elf32_vec ++ want64=true ++ ;; ++#endif + #ifdef BFD64 + aarch64-*-elf) + targ_defvec=aarch64_elf64_le_vec + +--- gas/configure.tgt 2016-12-01 12:00:43.000000000 +0100 ++++ gas/configure.tgt 2016-12-01 12:02:07.000000000 +0100 +@@ -120,6 +120,7 @@ + generic_target=${cpu_type}-$vendor-$os + # Note: This table is alpha-sorted, please try to keep it that way. + case ${generic_target} in ++ i386-*-mittos64*) fmt=elf em=gnu;; + aarch64*-*-elf) fmt=elf;; + aarch64*-*-linux*) fmt=elf em=linux ;; + + +--- ld/configure.tgt 2016-12-01 12:03:56.000000000 +0100 ++++ ld/configure.tgt 2016-12-01 12:09:12.000000000 +0100 +@@ -45,6 +45,15 @@ + # architecture variants should be kept together even if their names + # break the alpha sorting. + case "${targ}" in ++i[3-7]86-*-mittos64*) ++ targ_emul=elf_i386_mittos64 ++ targ_extra_emuls=elf_i386 ++ targ64_extra_emuls="elf_x86_64_mittos64 elf_x86_64" ++ ;; ++x86_64-*-mittos64*) ++ targ_emul=elf_x86_64_mittos64 ++ targ_extra_emuls="elf_i386_mittos64 elf_x86_64 elf_i386" ++ ;; + aarch64_be-*-elf) targ_emul=aarch64elfb + targ_extra_emuls="aarch64elf aarch64elf32 aarch64elf32b armelfb armelf" ;; + aarch64-*-elf) targ_emul=aarch64elf + +--- /dev/null 2016-12-01 12:13:02.000000000 +0100 ++++ ld/emulparams/elf_i386_mittos64.sh 2016-12-01 12:14:00.000000000 +0100 +@@ -0,0 +1 @@ ++. ${srcdir}/emulparams/elf_i386.sh + +--- /dev/null 2016-12-01 12:15:22.000000000 +0100 ++++ ld/emulparams/elf_x86_64_mittos64.sh 2016-12-01 12:15:52.000000000 +0100 +@@ -0,0 +1 @@ ++. ${srcdir}/emulparams/elf_x86_64.sh + +--- ld/Makefile.am 2016-12-01 12:22:53.000000000 +0100 ++++ ld/Makefile.am 2016-12-01 13:26:20.000000000 +0100 +@@ -280,6 +280,7 @@ + eelf32xstormy16.c \ + eelf32xtensa.c \ + eelf_i386.c \ ++ eelf_i386_mittos64.c \ + eelf_i386_be.c \ + eelf_i386_chaos.c \ + eelf_i386_fbsd.c \ +@@ -496,6 +497,7 @@ + eelf_k1om.c \ + eelf_k1om_fbsd.c \ + eelf_x86_64.c \ ++ eelf_x86_64_mittos64.c \ + eelf_x86_64_cloudabi.c \ + eelf_x86_64_fbsd.c \ + eelf_x86_64_nacl.c \ +@@ -1295,6 +1297,10 @@ + eelf_i386.c: $(srcdir)/emulparams/elf_i386.sh \ + $(ELF_DEPS) $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS} + ++eelf_i386_mittos64.c: $(srcdir)/emulparams/elf_i386_mittos64.sh \ ++ $(ELF_DEPS) $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS} ++ ${GENSCRIPTS} elf_i386_mittos64 "$(tdir_elf_i386_mittos64)" ++ + eelf_i386_be.c: $(srcdir)/emulparams/elf_i386_be.sh \ + $(ELF_DEPS) $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS} + +@@ -1987,6 +1993,10 @@ + eelf_x86_64.c: $(srcdir)/emulparams/elf_x86_64.sh \ + $(ELF_DEPS) $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS} + ++eelf_x86_64_mittos64.c: $(srcdir)/emulparams/elf_x86_64_mittos64.sh \ ++ $(ELF_DEPS) $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS} ++ ${GENSCRIPTS} elf_x86_64_mittos64 "$(tdir_elf_x86_64_mittos64)" ++ + eelf_x86_64_cloudabi.c: $(srcdir)/emulparams/elf_x86_64_cloudabi.sh \ + $(srcdir)/emulparams/elf_x86_64.sh \ + $(ELF_DEPS) $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS} diff --git a/util/build_toolchain.sh b/util/build_toolchain.sh index d2096f9..31cd0ae 100755 --- a/util/build_toolchain.sh +++ b/util/build_toolchain.sh @@ -4,33 +4,60 @@ bindir=${TOOLCHAIN}/bin -packages=(binutils gcc gdb) +packages=(automake autoconf musl_headers binutils gcc musl gdb) if [[ `uname` == 'Darwin' ]]; then packages=("${packages[@]}" objconv) fi packages=("${packages[@]}" grub) +musl_checkfile=${SYSROOT}/usr/lib/libc.a +musl_filename=musl +musl_url=git://git.musl-libc.org/musl +musl_config="--target=${TARGET} \ + --prefix=${SYSROOT}/usr \ + --disable-shared \ + --enable-debug \ + CFLAGS=-O0" + +musl_headers_checkfile=${SYSROOT}/usr/include/complex.h +musl_headers_filename=${musl_filename} +musl_headers_url=${musl_url} +function musl_headers_install() { + local package=musl + + pushd ${STASH}/${package} >/dev/null + + echo "Configuring" + echo "ARCH=x86_64">config.mak + echo "prefix=${SYSROOT}/usr">>config.mak + echo "includedir=\$(prefix)/include">> config.mak + echo "Compiling" + make install-headers \ + >/dev/null 2>>"${STASH}/error-${package}.log" || fail + popd >/dev/null +} + # Binutils 2.26 -# Build with --with-sysroot for use in the future binutils_checkfile=${bindir}/${TARGET}-ld binutils_filename=binutils-2.26 binutils_url=ftp://ftp.gnu.org/gnu/binutils/${binutils_filename}.tar.gz binutils_config="--target=${TARGET} \ - --with-sysroot \ + --with-sysroot=${SYSROOT} \ --disable-nls \ --disable-werror" +binutils_patchcmd="patch -p0 -N < ${BUILDROOT}/util/binutils.patch; cd ld; automake" # GCC 6.1.0 -# Build with --without-headers for now - this will be replaced later gcc_checkfile=${bindir}/${TARGET}-gcc gcc_filename=gcc-6.1.0 gcc_url=ftp://ftp.gnu.org/gnu/gcc/${gcc_filename}/${gcc_filename}.tar.gz gcc_config="--target=${TARGET} \ --disable-nls \ --enable-languages=c,c++ \ - --without-headers" + --with-sysroot=${SYSROOT}" gcc_make="all-gcc all-target-libgcc" gcc_install="install-gcc install-target-libgcc" +gcc_patchcmd="patch -p0 -N < ${BUILDROOT}/util/gcc.patch; cd libstdc++-v3; autoconf" # GDB 7.12 gdb_checkfile=${bindir}/x86_64-elf-linux-gdb @@ -73,6 +100,18 @@ grub_config="--target=x86_64-elf \ TARGET_RANLIB=${TARGET}-ranlib" grub_patchcmd="autogen.sh" +# automake 1.11.1 +# GCC requires exactly this version +automake_checkfile=${TOOLCHAIN}/bin/automake +automake_filename=automake-1.11.1 +automake_url=ftp://ftp.gnu.org/gnu/automake/${automake_filename}.tar.gz + +# autoconf 2.64 +# GCC requires exactly this version +autoconf_checkfile=${TOOLCHAIN}/bin/autoconf +autoconf_filename=autoconf-2.64 +autoconf_url=ftp://ftp.gnu.org/gnu/autoconf/${autoconf_filename}.tar.gz + function checkall() { for package in "${packages[@]}" do @@ -96,6 +135,7 @@ function main() { print_info "Preparing toolchain" mkdir -p "${STASH}" + mkdir -p "${SYSROOT}/usr/include" PATH=${TOOLCHAIN}/bin:${PATH} for package in "${packages[@]}" diff --git a/util/gcc.patch b/util/gcc.patch new file mode 100644 index 0000000..d37452e --- /dev/null +++ b/util/gcc.patch @@ -0,0 +1,108 @@ +--- config.sub 2016-12-01 14:15:39.000000000 +0100 ++++ config.sub 2016-12-01 14:16:53.000000000 +0100 +@@ -1376,6 +1376,7 @@ + -gnu* | -bsd* | -mach* | -minix* | -genix* | -ultrix* | -irix* \ + | -*vms* | -sco* | -esix* | -isc* | -aix* | -cnk* | -sunos | -sunos[34]*\ + | -hpux* | -unos* | -osf* | -luna* | -dgux* | -auroraux* | -solaris* \ ++ | -mittos64* \ + | -sym* | -kopensolaris* | -plan9* \ + | -amigaos* | -amigados* | -msdos* | -newsos* | -unicos* | -aof* \ + | -aos* | -aros* | -cloudabi* | -sortix* \ + +--- gcc/config.gcc 2016-12-01 16:08:28.000000000 +0100 ++++ gcc/config.gcc 2016-12-01 16:08:12.000000000 +0100 +@@ -612,6 +612,12 @@ + + # Common parts for widely ported systems. + case ${target} in ++*-*-mittos64) ++ gas=yes ++ gnu_ld=yes ++ default_use_cxa_atexit=yes ++ use_gcc_stdint=wrap ++ ;; + *-*-darwin*) + tmake_file="t-darwin ${cpu_type}/t-darwin" + tm_file="${tm_file} darwin.h" +@@ -907,6 +912,12 @@ + esac + + case ${target} in ++i[34567]86-*-mittos64*) ++ tm_file="${tm_file} i386/unix.h i386/att.h dbxelf.h elfos.h newlib-stdint.h i386/i386elf.h mittos64.h" ++ ;; ++x86_64-*-mittos64*) ++ tm_file="${tm_file} i386/unix.h i386/att.h dbxelf.h elfos.h newlib-stdint.h i386/i386elf.h i386/x86-64.h mittos64.h" ++ ;; + aarch64*-*-elf | aarch64*-*-rtems*) + tm_file="${tm_file} dbxelf.h elfos.h newlib-stdint.h" + tm_file="${tm_file} aarch64/aarch64-elf.h aarch64/aarch64-elf-raw.h" + +--- /dev/null 2016-12-01 16:23:01.000000000 +0100 ++++ gcc/config/mittos64.h 2016-12-01 16:15:32.000000000 +0100 +@@ -0,0 +1,20 @@ ++#undef TARGET_MITTOS64 ++#define TARGET_MITTOS64 1 ++ ++#define LIB_SPEC "-lc" ++ ++#define STARTFILE_SPEC "crt0.o%s crti.o%s crtbegin.o%s" ++#define ENDFILE_SPEC "crtend.o%s crtn.o%s" ++ ++#undef NO_IMPLICIT_EXTERN_C ++#define NO_IMPLICIT_EXTERN_C 1 ++ ++#undef TARGET_OS_CPP_BUILTINS ++#define TARGET_OS_CPP_BUILTINS() \ ++ do { \ ++ builtin_define ("__mittos64__"); \ ++ builtin_define ("__unix__"); \ ++ builtin_assert ("system=mittos64"); \ ++ builtin_assert ("system=unix"); \ ++ builtin_assert ("system=posix"); \ ++ } while(0); + +--- libstdc++-v3/crossconfig.m4 2016-12-01 16:16:26.000000000 +0100 ++++ libstdc++-v3/crossconfig.m4 2016-12-01 16:17:22.000000000 +0100 +@@ -5,6 +5,12 @@ + AC_DEFUN([GLIBCXX_CROSSCONFIG],[ + # Base decisions on target environment. + case "${host}" in ++ *-mittos64*) ++ GLIBCXX_CHECK_COMPILER_FEATURES ++ GLIBCXX_CHECK_LINKER_FEATURES ++ GLIBCXX_CHECK_MATH_SUPPORT ++ GLIBCXX_CHECK_STDLIB_SUPPORT ++ ;; + arm*-*-symbianelf*) + # This is a freestanding configuration; there is nothing to do here. + ;; + +--- libgcc/config.host 2016-12-01 16:17:44.000000000 +0100 ++++ libgcc/config.host 2016-12-01 16:20:42.000000000 +0100 +@@ -327,6 +327,14 @@ + esac + + case ${host} in ++i[34567]86-*-mittos64*) ++ extra_parts="$extra_parts crtbegin.o crtend.o" ++ tmake_file="$tmake_file i386/t-crtstuff t-crtstuff-pic t-libgcc-pic" ++ ;; ++x86_64-*-mittos64*) ++ extra_parts="$extra_parts crtbegin.o crtend.o" ++ tmake_file="$tmake_file i386/t-crtstuff t-crtstuff-pic t-libgcc-pic" ++ ;; + aarch64*-*-elf | aarch64*-*-rtems*) + extra_parts="$extra_parts crtbegin.o crtend.o crti.o crtn.o" + extra_parts="$extra_parts crtfastmath.o" + +--- fixincludes/mkfixinc.sh 2016-12-01 16:22:08.000000000 +0100 ++++ fixincludes/mkfixinc.sh 2016-12-01 16:22:41.000000000 +0100 +@@ -11,6 +11,7 @@ + + # Check for special fix rules for particular targets + case $machine in ++ *-mittos64* | \ + i?86-*-cygwin* | \ + i?86-*-mingw32* | \ + x86_64-*-mingw32* | \