Skip to content

Commit

Permalink
build: Generally rework build system to easily allow 3rd party toolch…
Browse files Browse the repository at this point in the history
…ains to be used. Drop reliance on libgcc for BIOS builds by shipping our own routines instead
  • Loading branch information
mintsuki committed Apr 2, 2021
1 parent 74ef590 commit 8e4de05
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 87 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ jobs:
- name: Install dependencies
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)
run: make toolchain-bios

- name: Build the toolchain (UEFI)
run: make toolchain-uefi
- name: Build the toolchain
run: make toolchain

- name: Build the bootloader
run: make
Expand Down
17 changes: 6 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ CC = cc
PREFIX = /usr/local
DESTDIR =

TOOLCHAIN = x86_64-elf

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
Expand Down Expand Up @@ -84,19 +86,12 @@ test-clean:
rm -rf test_image test.hdd test.iso

toolchain:
$(MAKE) toolchain-bios
$(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`
scripts/make_toolchain.sh "`realpath ./toolchain`" -j`nproc`

gnu-efi:
git clone https://git.code.sf.net/p/gnu-efi/code --branch=3.0.12 --depth=1 $@
$(MAKE) -C gnu-efi/gnuefi CC=x86_64-elf-gcc AR=x86_64-elf-ar
$(MAKE) -C gnu-efi/lib CC=x86_64-elf-gcc ARCH=x86_64 x86_64/efi_stub.o
git clone https://git.code.sf.net/p/gnu-efi/code --branch=3.0.13 --depth=1 $@
$(MAKE) -C gnu-efi/gnuefi CC="$(TOOLCHAIN)-gcc -m64" AR=$(TOOLCHAIN)-ar
$(MAKE) -C gnu-efi/lib CC="$(TOOLCHAIN)-gcc -m64" ARCH=x86_64 x86_64/efi_stub.o

ovmf:
mkdir -p ovmf
Expand Down
18 changes: 12 additions & 6 deletions decompressor/Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
CC = i386-elf-gcc
LD = i386-elf-gcc
OBJCOPY = i386-elf-objcopy
BUILDDIR =

ifeq ($(BUILDDIR), )
$(error BUILDDIR not specified)
endif

TOOLCHAIN = x86_64-elf

CC = $(TOOLCHAIN)-gcc
OBJCOPY = $(TOOLCHAIN)-objcopy

CFLAGS = -flto -Os -pipe -Wall -Wextra -Werror

INTERNAL_CFLAGS = \
-m32 \
-std=gnu11 \
-ffreestanding \
-fno-stack-protector \
-fno-pic \
-fno-pie \
-fomit-frame-pointer \
-Wno-address-of-packed-member \
-masm=intel \
Expand All @@ -24,10 +28,12 @@ INTERNAL_CFLAGS = \
LDFLAGS = -flto -Os

INTERNAL_LDFLAGS = \
-lgcc \
-static-libgcc \
-m32 \
-Wl,-melf_i386 \
-nostdlib \
-no-pie \
-fno-pic \
-fno-pie \
-z max-page-size=0x1000 \
-static \
-Tlinker.ld
Expand All @@ -47,7 +53,7 @@ builddir:
for i in $(OBJ); do mkdir -p `dirname $$i`; done

$(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 $@

-include $(HEADER_DEPS)
Expand Down
6 changes: 4 additions & 2 deletions scripts/make_toolchain_uefi.sh → scripts/make_toolchain.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ TARGET=x86_64-elf
BINUTILSVERSION=2.36.1
GCCVERSION=10.2.0

CFLAGS="-O2 -pipe"

mkdir -p "$1" && cd "$1"
PREFIX="$(pwd)"

Expand All @@ -30,7 +32,7 @@ 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-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 install
cd ..
Expand All @@ -40,7 +42,7 @@ contrib/download_prerequisites
cd ..
mkdir 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-target-libgcc
make install-gcc
Expand Down
48 changes: 0 additions & 48 deletions scripts/make_toolchain_bios.sh

This file was deleted.

30 changes: 22 additions & 8 deletions stage23/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ ifeq ($(BUILDDIR), )
endif

ifeq ($(TARGET), bios)
TOOLCHAIN=i386-elf
OBJCOPY_ARCH = elf32-i386
else ifeq ($(TARGET), uefi)
TOOLCHAIN=x86_64-elf
OBJCOPY_ARCH = elf64-x86-64
else
$(error Invalid target)
endif

TOOLCHAIN = x86_64-elf

CC = $(TOOLCHAIN)-gcc
OBJCOPY = $(TOOLCHAIN)-objcopy
OBJDUMP = $(TOOLCHAIN)-objdump
Expand Down Expand Up @@ -49,15 +51,25 @@ INTERNAL_CFLAGS := \

ifeq ($(TARGET), bios)
INTERNAL_CFLAGS += \
-m32 \
-march=i386 \
-fno-pie
endif

ifeq ($(TARGET), uefi)
INTERNAL_CFLAGS += \
-m64 \
-march=x86-64 \
-I../gnu-efi/inc \
-I../gnu-efi/inc/x86_64 \
-fpie \
-mno-red-zone

INTERNAL_CFLAGS32 := \
$(INTERNAL_CFLAGS) \
-m32 \
-march=i386 \
-fpie
endif

LDFLAGS = -O3 -g
Expand All @@ -70,15 +82,17 @@ INTERNAL_LDFLAGS := \

ifeq ($(TARGET), bios)
INTERNAL_LDFLAGS += \
-m32 \
-Wl,-melf_i386 \
-static \
-no-pie \
-fno-pie \
-lgcc \
-static-libgcc
-fno-pie
endif

ifeq ($(TARGET), uefi)
INTERNAL_LDFLAGS += \
-m64 \
-Wl,-melf_x86_64 \
-shared \
-Wl,-Bsymbolic \
-Wl,--noinhibit-exec \
Expand Down Expand Up @@ -117,11 +131,11 @@ $(BUILDDIR)/sys/smp_trampoline.bin: sys/smp_trampoline.real

$(BUILDDIR)/sys/smp_trampoline.o: $(BUILDDIR)/sys/smp_trampoline.bin
cd "`dirname $<`" && \
$(OBJCOPY) -B i8086 -I binary -O default "`basename $<`" $@
$(OBJCOPY) -B i8086 -I binary -O $(OBJCOPY_ARCH) "`basename $<`" $@

$(BUILDDIR)/font.o: font.bin
cd "`dirname $<`" && \
$(OBJCOPY) -B i8086 -I binary -O default "`basename $<`" $@
$(OBJCOPY) -B i8086 -I binary -O $(OBJCOPY_ARCH) "`basename $<`" $@

ifeq ($(TARGET), bios)

Expand Down Expand Up @@ -191,7 +205,7 @@ endif

ifeq ($(TARGET), uefi)
$(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 $@
rm $@.32
endif
Expand Down
1 change: 0 additions & 1 deletion stage23/lib/libc.s2.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <stddef.h>
#include <stdint.h>
#include <lib/libc.h>
#include <limits.h>
#include <stdbool.h>
#include <lib/blib.h>

Expand Down
55 changes: 55 additions & 0 deletions stage23/lib/libgcc.s2.asm
Original file line number Diff line number Diff line change
@@ -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
6 changes: 0 additions & 6 deletions tinf/tinflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@

#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 -- */

struct tinf_tree {
Expand Down

0 comments on commit 8e4de05

Please sign in to comment.