diff --git a/.gitignore b/.gitignore index ea292fbf..4a6d47c3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ /**/*.o /**/*.a -/**/*.bc /**/*.bin /**/*.bin.gz /**/*.elf diff --git a/Makefile b/Makefile index 52cbfe3e..353fd3a8 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,8 @@ DESTDIR = PREFIX = /usr/local OS := $(shell uname) -CC = clang -OBJCOPY = llvm-objcopy +CC = cc +OBJCOPY = objcopy CFLAGS = -O2 -pipe -Wall -Wextra .PHONY: all install clean echfs-test ext2-test test.img @@ -21,12 +21,8 @@ src/limine.bin: $(MAKE) -C src all limine-install: src/limine.bin limine-install.c - $(CC) $(CFLAGS) -c limine-install.c -o limine-install.o - # FIXME: GNU objcopy supports `-O default` but for some stupid reason - # llvm-objcopy does not. This needs to be worked around. - # For now hardcode elf64-x86-64. - $(OBJCOPY) -I binary -O elf64-x86-64 src/limine.bin limine.o - $(CC) $(CFLAGS) limine.o limine-install.o -o limine-install + $(OBJCOPY) -I binary -O default src/limine.bin limine.o + $(CC) $(CFLAGS) limine.o limine-install.c -o limine-install test.img: rm -f test.img diff --git a/src/Makefile b/src/Makefile index 2a10aaf7..efba6e88 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,11 +1,13 @@ -OPT_LEVEL = z -CFLAGS = -pipe -Wall -Wextra +CC = ../toolchain/bin/i386-elf-gcc +LD = ../toolchain/bin/i386-elf-gcc -INTERNAL_CFLAGS = \ - -O$(OPT_LEVEL) \ +CFLAGS = -flto -Os -pipe -Wall -Wextra + +INTERNAL_CFLAGS = \ -std=gnu99 \ -ffreestanding \ - -flto \ + -fno-stack-protector \ + -fno-pic \ -mno-80387 \ -mno-mmx \ -mno-sse \ @@ -13,37 +15,37 @@ INTERNAL_CFLAGS = \ -I. \ -Wno-address-of-packed-member +LDFLAGS = -flto -Os + INTERNAL_LDFLAGS = \ - -static \ + -lgcc \ + -static-libgcc \ -nostdlib \ - -Tlinker.ld \ - -no-pie + -no-pie \ + -static \ + -Tlinker.ld .PHONY: all clean C_FILES := $(shell find ./ -type f -name '*.c' | grep -v bootsect | grep -v decompressor | sort) ASM_FILES := $(shell find ./ -type f -name '*.asm' | grep -v bootsect | grep -v decompressor | sort) -ASM_OBJ := $(ASM_FILES:.asm=.o) -BC := $(C_FILES:.c=.bc) +OBJ := $(ASM_FILES:.asm=.o) $(C_FILES:.c=.o) all: limine.bin -limine.bin: $(BC) $(ASM_OBJ) - llvm-link $(BC) -o bundle.bc - opt --O$(OPT_LEVEL) bundle.bc -o optimised_bundle.bc - clang --target=i386-elf -O$(OPT_LEVEL) -c optimised_bundle.bc -o optimised_bundle.o - ld.lld optimised_bundle.o $(ASM_OBJ) $(INTERNAL_LDFLAGS) -o stage2.elf - llvm-objcopy -O binary stage2.elf stage2.bin +limine.bin: $(OBJ) + $(LD) $(OBJ) $(LDFLAGS) $(INTERNAL_LDFLAGS) -o stage2.elf + objcopy -O binary stage2.elf stage2.bin gzip -9 < stage2.bin > stage2.bin.gz $(MAKE) -C decompressor cd bootsect && nasm bootsect.asm -fbin -o ../limine.bin -%.bc: %.c - clang --target=i386-elf $(CFLAGS) $(INTERNAL_CFLAGS) -c $< -o $@ +%.o: %.c + $(CC) $(CFLAGS) $(INTERNAL_CFLAGS) -c $< -o $@ %.o: %.asm nasm $< -f elf32 -o $@ clean: $(MAKE) -C decompressor clean - rm -f stage2.bin.gz limine.bin $(ASM_OBJ) $(BC) + rm -f stage2.bin.gz stage2.bin stage2.elf limine.bin $(OBJ) diff --git a/src/decompressor/Makefile b/src/decompressor/Makefile index 4a774b94..f5f0f3a4 100644 --- a/src/decompressor/Makefile +++ b/src/decompressor/Makefile @@ -1,11 +1,13 @@ -OPT_LEVEL = z -CFLAGS = -pipe -Wall -Wextra +CC = ../../toolchain/bin/i386-elf-gcc +LD = ../../toolchain/bin/i386-elf-gcc -INTERNAL_CFLAGS = \ - -O$(OPT_LEVEL) \ +CFLAGS = -flto -Os -pipe -Wall -Wextra + +INTERNAL_CFLAGS = \ -std=gnu99 \ -ffreestanding \ - -flto \ + -fno-stack-protector \ + -fno-pic \ -mno-80387 \ -mno-mmx \ -mno-sse \ @@ -13,28 +15,29 @@ INTERNAL_CFLAGS = \ -I. \ -Wno-address-of-packed-member +LDFLAGS = -flto -Os + INTERNAL_LDFLAGS = \ - -static \ + -lgcc \ + -static-libgcc \ -nostdlib \ - -Tlinker.ld \ - -no-pie + -no-pie \ + -static \ + -Tlinker.ld .PHONY: all clean C_FILES := $(shell find ./ -type f -name '*.c' | sort) -BC := $(C_FILES:.c=.bc) +OBJ := $(C_FILES:.c=.o) all: decompressor.bin -decompressor.bin: $(BC) - llvm-link $(BC) -o bundle.bc - opt --O$(OPT_LEVEL) bundle.bc -o optimised_bundle.bc - clang --target=i386-elf -O$(OPT_LEVEL) -c optimised_bundle.bc -o optimised_bundle.o - ld.lld optimised_bundle.o $(INTERNAL_LDFLAGS) -o decompressor.elf - llvm-objcopy -O binary decompressor.elf decompressor.bin +decompressor.bin: $(OBJ) + $(LD) $(OBJ) $(LDFLAGS) $(INTERNAL_LDFLAGS) -o decompressor.elf + objcopy -O binary decompressor.elf decompressor.bin -%.bc: %.c - clang --target=i386-elf $(CFLAGS) $(INTERNAL_CFLAGS) -c $< -o $@ +%.o: %.c + $(CC) $(CFLAGS) $(INTERNAL_CFLAGS) -c $< -o $@ clean: - rm -f decompressor.bin $(BC) + rm -f decompressor.bin decompressor.elf $(OBJ) diff --git a/src/main.c b/src/main.c index 08ee961f..e8e75e0a 100644 --- a/src/main.c +++ b/src/main.c @@ -12,8 +12,7 @@ ASM_BASIC( "sub ecx, OFFSET bss_begin\n\t" "rep stosb\n\t" - "mov ebx, OFFSET main\n\t" - "jmp ebx\n\t" + "jmp main\n\t" ); #include diff --git a/toolchain/.gitignore b/toolchain/.gitignore new file mode 100644 index 00000000..fd038def --- /dev/null +++ b/toolchain/.gitignore @@ -0,0 +1,3 @@ +* +!.gitignore +!make_toolchain.sh diff --git a/toolchain/make_toolchain.sh b/toolchain/make_toolchain.sh new file mode 100755 index 00000000..00eb5230 --- /dev/null +++ b/toolchain/make_toolchain.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env bash + +set -e +set -x + +PREFIX="$(pwd)" +TARGET=i386-elf +BINUTILSVERSION=2.35 +GCCVERSION=10.2.0 + +if [ -z "$MAKEFLAGS" ]; then + MAKEFLAGS="$1" +fi +export MAKEFLAGS + +export PATH="$PREFIX/bin:$PATH" + +if [ -x "$(command -v gmake)" ]; then + mkdir -p "$PREFIX/bin" + cat <"$PREFIX/bin/make" +#!/usr/bin/env sh +gmake "\$@" +EOF + chmod +x "$PREFIX/bin/make" +fi + +mkdir -p build +cd build + +if [ ! -f binutils-$BINUTILSVERSION.tar.gz ]; then + wget -4 https://ftp.gnu.org/gnu/binutils/binutils-$BINUTILSVERSION.tar.gz # Force IPv4 otherwise wget hangs +fi +if [ ! -f gcc-$GCCVERSION.tar.gz ]; then + wget -4 https://ftp.gnu.org/gnu/gcc/gcc-$GCCVERSION/gcc-$GCCVERSION.tar.gz # Same as above +fi + +tar -xf binutils-$BINUTILSVERSION.tar.gz +tar -xf gcc-$GCCVERSION.tar.gz + +rm -rf build-gcc build-binutils + +mkdir build-binutils +cd build-binutils +../binutils-$BINUTILSVERSION/configure --target=$TARGET --prefix="$PREFIX" --with-sysroot --disable-nls --disable-werror +make +make install +cd .. + +cd gcc-$GCCVERSION +contrib/download_prerequisites +cd .. +mkdir build-gcc +cd build-gcc +../gcc-$GCCVERSION/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --enable-languages=c --without-headers +make all-gcc +make all-target-libgcc +make install-gcc +make install-target-libgcc