build: Generally rework build system to easily allow 3rd party toolchains to be used. Drop reliance on libgcc for BIOS builds by shipping our own routines instead

This commit is contained in:
mintsuki 2021-04-03 00:48:38 +02:00
parent 74ef5901a7
commit 8e4de054d7
9 changed files with 101 additions and 87 deletions

View File

@ -25,11 +25,8 @@ jobs:
- name: Install dependencies - name: Install dependencies
run: sudo apt-get update && sudo apt-get install git build-essential nasm gcc-mingw-w64 gcc-multilib wget mtools -y run: sudo apt-get update && sudo apt-get install git build-essential nasm gcc-mingw-w64 gcc-multilib wget mtools -y
- name: Build the toolchain (BIOS) - name: Build the toolchain
run: make toolchain-bios run: make toolchain
- name: Build the toolchain (UEFI)
run: make toolchain-uefi
- name: Build the bootloader - name: Build the bootloader
run: make run: make

View File

@ -2,6 +2,8 @@ CC = cc
PREFIX = /usr/local PREFIX = /usr/local
DESTDIR = DESTDIR =
TOOLCHAIN = x86_64-elf
PATH := $(shell pwd)/toolchain/bin:$(PATH) PATH := $(shell pwd)/toolchain/bin:$(PATH)
.PHONY: all bin/limine-install clean install distclean limine-bios limine-uefi limine-bios-clean limine-uefi-clean stage23-bios stage23-bios-clean stage23-uefi stage23-uefi-clean decompressor decompressor-clean toolchain test.hdd echfs-test ext2-test fat16-test fat32-test iso9660-test iso9660-uefi-test pxe-test uefi-test hybrid-iso9660-test .PHONY: all bin/limine-install clean install distclean limine-bios limine-uefi limine-bios-clean limine-uefi-clean stage23-bios stage23-bios-clean stage23-uefi stage23-uefi-clean decompressor decompressor-clean toolchain test.hdd echfs-test ext2-test fat16-test fat32-test iso9660-test iso9660-uefi-test pxe-test uefi-test hybrid-iso9660-test
@ -84,19 +86,12 @@ test-clean:
rm -rf test_image test.hdd test.iso rm -rf test_image test.hdd test.iso
toolchain: toolchain:
$(MAKE) toolchain-bios scripts/make_toolchain.sh "`realpath ./toolchain`" -j`nproc`
$(MAKE) toolchain-uefi
toolchain-bios:
scripts/make_toolchain_bios.sh "`realpath ./toolchain`" -j`nproc`
toolchain-uefi:
scripts/make_toolchain_uefi.sh "`realpath ./toolchain`" -j`nproc`
gnu-efi: gnu-efi:
git clone https://git.code.sf.net/p/gnu-efi/code --branch=3.0.12 --depth=1 $@ git clone https://git.code.sf.net/p/gnu-efi/code --branch=3.0.13 --depth=1 $@
$(MAKE) -C gnu-efi/gnuefi CC=x86_64-elf-gcc AR=x86_64-elf-ar $(MAKE) -C gnu-efi/gnuefi CC="$(TOOLCHAIN)-gcc -m64" AR=$(TOOLCHAIN)-ar
$(MAKE) -C gnu-efi/lib CC=x86_64-elf-gcc ARCH=x86_64 x86_64/efi_stub.o $(MAKE) -C gnu-efi/lib CC="$(TOOLCHAIN)-gcc -m64" ARCH=x86_64 x86_64/efi_stub.o
ovmf: ovmf:
mkdir -p ovmf mkdir -p ovmf

View File

@ -1,19 +1,23 @@
CC = i386-elf-gcc
LD = i386-elf-gcc
OBJCOPY = i386-elf-objcopy
BUILDDIR = BUILDDIR =
ifeq ($(BUILDDIR), ) ifeq ($(BUILDDIR), )
$(error BUILDDIR not specified) $(error BUILDDIR not specified)
endif endif
TOOLCHAIN = x86_64-elf
CC = $(TOOLCHAIN)-gcc
OBJCOPY = $(TOOLCHAIN)-objcopy
CFLAGS = -flto -Os -pipe -Wall -Wextra -Werror CFLAGS = -flto -Os -pipe -Wall -Wextra -Werror
INTERNAL_CFLAGS = \ INTERNAL_CFLAGS = \
-m32 \
-std=gnu11 \ -std=gnu11 \
-ffreestanding \ -ffreestanding \
-fno-stack-protector \ -fno-stack-protector \
-fno-pic \ -fno-pic \
-fno-pie \
-fomit-frame-pointer \ -fomit-frame-pointer \
-Wno-address-of-packed-member \ -Wno-address-of-packed-member \
-masm=intel \ -masm=intel \
@ -24,10 +28,12 @@ INTERNAL_CFLAGS = \
LDFLAGS = -flto -Os LDFLAGS = -flto -Os
INTERNAL_LDFLAGS = \ INTERNAL_LDFLAGS = \
-lgcc \ -m32 \
-static-libgcc \ -Wl,-melf_i386 \
-nostdlib \ -nostdlib \
-no-pie \ -no-pie \
-fno-pic \
-fno-pie \
-z max-page-size=0x1000 \ -z max-page-size=0x1000 \
-static \ -static \
-Tlinker.ld -Tlinker.ld
@ -47,7 +53,7 @@ builddir:
for i in $(OBJ); do mkdir -p `dirname $$i`; done for i in $(OBJ); do mkdir -p `dirname $$i`; done
$(BUILDDIR)/decompressor.bin: $(OBJ) $(BUILDDIR)/decompressor.bin: $(OBJ)
$(LD) $(OBJ) $(LDFLAGS) $(INTERNAL_LDFLAGS) -o $(BUILDDIR)/decompressor.elf $(CC) $(OBJ) $(LDFLAGS) $(INTERNAL_LDFLAGS) -o $(BUILDDIR)/decompressor.elf
$(OBJCOPY) -O binary $(BUILDDIR)/decompressor.elf $@ $(OBJCOPY) -O binary $(BUILDDIR)/decompressor.elf $@
-include $(HEADER_DEPS) -include $(HEADER_DEPS)

View File

@ -7,6 +7,8 @@ TARGET=x86_64-elf
BINUTILSVERSION=2.36.1 BINUTILSVERSION=2.36.1
GCCVERSION=10.2.0 GCCVERSION=10.2.0
CFLAGS="-O2 -pipe"
mkdir -p "$1" && cd "$1" mkdir -p "$1" && cd "$1"
PREFIX="$(pwd)" PREFIX="$(pwd)"
@ -30,7 +32,7 @@ tar -xf ../gcc-$GCCVERSION.tar.gz
mkdir build-binutils mkdir build-binutils
cd build-binutils cd build-binutils
../binutils-$BINUTILSVERSION/configure --target=$TARGET --prefix="$PREFIX" --with-sysroot --disable-nls --disable-werror --enable-targets=x86_64-elf,x86_64-pe --enable-64-bit-bfd ../binutils-$BINUTILSVERSION/configure CFLAGS="$CFLAGS" CXXFLAGS="$CFLAGS" --target=$TARGET --prefix="$PREFIX" --with-sysroot --disable-nls --disable-werror --enable-targets=x86_64-elf,x86_64-pe
make make
make install make install
cd .. cd ..
@ -40,7 +42,7 @@ contrib/download_prerequisites
cd .. cd ..
mkdir build-gcc mkdir build-gcc
cd build-gcc cd build-gcc
../gcc-$GCCVERSION/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --enable-languages=c --without-headers ../gcc-$GCCVERSION/configure CFLAGS="$CFLAGS" CXXFLAGS="$CFLAGS" --target=$TARGET --prefix="$PREFIX" --disable-nls --enable-languages=c --without-headers
make all-gcc make all-gcc
make all-target-libgcc make all-target-libgcc
make install-gcc make install-gcc

View File

@ -1,48 +0,0 @@
#!/bin/sh
set -e
set -x
TARGET=i386-elf
BINUTILSVERSION=2.36.1
GCCVERSION=10.2.0
mkdir -p "$1" && cd "$1"
PREFIX="$(pwd)"
export MAKEFLAGS="$2"
export PATH="$PREFIX/bin:$PATH"
if [ ! -f binutils-$BINUTILSVERSION.tar.gz ]; then
wget https://ftp.gnu.org/gnu/binutils/binutils-$BINUTILSVERSION.tar.gz
fi
if [ ! -f gcc-$GCCVERSION.tar.gz ]; then
wget https://ftp.gnu.org/gnu/gcc/gcc-$GCCVERSION/gcc-$GCCVERSION.tar.gz
fi
rm -rf build
mkdir build
cd build
tar -xf ../binutils-$BINUTILSVERSION.tar.gz
tar -xf ../gcc-$GCCVERSION.tar.gz
mkdir build-binutils
cd build-binutils
../binutils-$BINUTILSVERSION/configure --target=$TARGET --prefix="$PREFIX" --with-sysroot --disable-nls --disable-werror --enable-64-bit-bfd
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
cd ..

View File

@ -6,13 +6,15 @@ ifeq ($(BUILDDIR), )
endif endif
ifeq ($(TARGET), bios) ifeq ($(TARGET), bios)
TOOLCHAIN=i386-elf OBJCOPY_ARCH = elf32-i386
else ifeq ($(TARGET), uefi) else ifeq ($(TARGET), uefi)
TOOLCHAIN=x86_64-elf OBJCOPY_ARCH = elf64-x86-64
else else
$(error Invalid target) $(error Invalid target)
endif endif
TOOLCHAIN = x86_64-elf
CC = $(TOOLCHAIN)-gcc CC = $(TOOLCHAIN)-gcc
OBJCOPY = $(TOOLCHAIN)-objcopy OBJCOPY = $(TOOLCHAIN)-objcopy
OBJDUMP = $(TOOLCHAIN)-objdump OBJDUMP = $(TOOLCHAIN)-objdump
@ -49,15 +51,25 @@ INTERNAL_CFLAGS := \
ifeq ($(TARGET), bios) ifeq ($(TARGET), bios)
INTERNAL_CFLAGS += \ INTERNAL_CFLAGS += \
-m32 \
-march=i386 \
-fno-pie -fno-pie
endif endif
ifeq ($(TARGET), uefi) ifeq ($(TARGET), uefi)
INTERNAL_CFLAGS += \ INTERNAL_CFLAGS += \
-m64 \
-march=x86-64 \
-I../gnu-efi/inc \ -I../gnu-efi/inc \
-I../gnu-efi/inc/x86_64 \ -I../gnu-efi/inc/x86_64 \
-fpie \ -fpie \
-mno-red-zone -mno-red-zone
INTERNAL_CFLAGS32 := \
$(INTERNAL_CFLAGS) \
-m32 \
-march=i386 \
-fpie
endif endif
LDFLAGS = -O3 -g LDFLAGS = -O3 -g
@ -70,15 +82,17 @@ INTERNAL_LDFLAGS := \
ifeq ($(TARGET), bios) ifeq ($(TARGET), bios)
INTERNAL_LDFLAGS += \ INTERNAL_LDFLAGS += \
-m32 \
-Wl,-melf_i386 \
-static \ -static \
-no-pie \ -no-pie \
-fno-pie \ -fno-pie
-lgcc \
-static-libgcc
endif endif
ifeq ($(TARGET), uefi) ifeq ($(TARGET), uefi)
INTERNAL_LDFLAGS += \ INTERNAL_LDFLAGS += \
-m64 \
-Wl,-melf_x86_64 \
-shared \ -shared \
-Wl,-Bsymbolic \ -Wl,-Bsymbolic \
-Wl,--noinhibit-exec \ -Wl,--noinhibit-exec \
@ -117,11 +131,11 @@ $(BUILDDIR)/sys/smp_trampoline.bin: sys/smp_trampoline.real
$(BUILDDIR)/sys/smp_trampoline.o: $(BUILDDIR)/sys/smp_trampoline.bin $(BUILDDIR)/sys/smp_trampoline.o: $(BUILDDIR)/sys/smp_trampoline.bin
cd "`dirname $<`" && \ cd "`dirname $<`" && \
$(OBJCOPY) -B i8086 -I binary -O default "`basename $<`" $@ $(OBJCOPY) -B i8086 -I binary -O $(OBJCOPY_ARCH) "`basename $<`" $@
$(BUILDDIR)/font.o: font.bin $(BUILDDIR)/font.o: font.bin
cd "`dirname $<`" && \ cd "`dirname $<`" && \
$(OBJCOPY) -B i8086 -I binary -O default "`basename $<`" $@ $(OBJCOPY) -B i8086 -I binary -O $(OBJCOPY_ARCH) "`basename $<`" $@
ifeq ($(TARGET), bios) ifeq ($(TARGET), bios)
@ -191,7 +205,7 @@ endif
ifeq ($(TARGET), uefi) ifeq ($(TARGET), uefi)
$(BUILDDIR)/%.32.o: %.32.c $(BUILDDIR)/%.32.o: %.32.c
$(CC) -m32 $(CFLAGS) $(INTERNAL_CFLAGS) -c $< -o $@.32 $(CC) $(CFLAGS) $(INTERNAL_CFLAGS32) -c $< -o $@.32
$(OBJCOPY) -I elf32-i386 -O elf64-x86-64 $@.32 $@ $(OBJCOPY) -I elf32-i386 -O elf64-x86-64 $@.32 $@
rm $@.32 rm $@.32
endif endif

View File

@ -1,7 +1,6 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <lib/libc.h> #include <lib/libc.h>
#include <limits.h>
#include <stdbool.h> #include <stdbool.h>
#include <lib/blib.h> #include <lib/blib.h>

55
stage23/lib/libgcc.s2.asm Normal file
View File

@ -0,0 +1,55 @@
section .text
global __udivdi3
__udivdi3:
mov eax, dword [esp+4]
mov edx, dword [esp+8]
div dword [esp+12]
xor edx, edx
ret
global __divdi3
__divdi3:
mov eax, dword [esp+4]
mov edx, dword [esp+8]
idiv dword [esp+12]
xor edx, edx
ret
global __umoddi3
__umoddi3:
mov eax, dword [esp+4]
mov edx, dword [esp+8]
div dword [esp+12]
mov eax, edx
xor edx, edx
ret
global __moddi3
__moddi3:
mov eax, dword [esp+4]
mov edx, dword [esp+8]
idiv dword [esp+12]
mov eax, edx
xor edx, edx
ret
global __udivmoddi4
__udivmoddi4:
mov eax, dword [esp+4]
mov edx, dword [esp+8]
div dword [esp+12]
mov ecx, dword [esp+20]
mov dword [ecx], edx
xor edx, edx
ret
global __udivmoddi4
__divmoddi4:
mov eax, dword [esp+4]
mov edx, dword [esp+8]
idiv dword [esp+12]
mov ecx, dword [esp+20]
mov dword [ecx], edx
xor edx, edx
ret

View File

@ -25,12 +25,6 @@
#include "tinf.h" #include "tinf.h"
#include <limits.h>
#if defined(UINT_MAX) && (UINT_MAX) < 0xFFFFFFFFUL
# error "tinf requires unsigned int to be at least 32-bit"
#endif
/* -- Internal data structures -- */ /* -- Internal data structures -- */
struct tinf_tree { struct tinf_tree {