Skip to content

Commit

Permalink
kern: backport updates from Limine templates
Browse files Browse the repository at this point in the history
  • Loading branch information
mintsuki committed Aug 7, 2024
1 parent 05713b5 commit b9e2cfe
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 49 deletions.
14 changes: 4 additions & 10 deletions build-support/limine.conf
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
timeout: 2
timeout: 3

/Vinix
protocol: limine
kaslr: no
kernel_path: boot():/boot/vinix
module_path: boot():/boot/initramfs.tar

/Vinix (KASLR)
protocol: limine
kernel_path: boot():/boot/vinix
module_path: boot():/boot/initramfs.tar
protocol: limine
kernel_path: boot():/boot/vinix
module_path: boot():/boot/initramfs.tar
1 change: 0 additions & 1 deletion kernel/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/c/freestanding-headers
/c/flanterm
/c/printf
/printf
/bin
/obj
15 changes: 5 additions & 10 deletions kernel/GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ override KCFLAGS += \
-fno-stack-protector \
-fno-stack-check \
-fno-lto \
-fPIE \
-fno-PIC \
-ffunction-sections \
-fdata-sections \
-fno-strict-aliasing \
Expand All @@ -71,7 +71,8 @@ override KCFLAGS += \
-mno-mmx \
-mno-sse \
-mno-sse2 \
-mno-red-zone
-mno-red-zone \
-mcmodel=kernel

override KCPPFLAGS := \
-I c \
Expand All @@ -90,8 +91,7 @@ obj/c/flanterm/backends/fb.c.o: override KCPPFLAGS += \
override KLDFLAGS += \
-m elf_x86_64 \
-nostdlib \
-pie \
-z text \
-static \
-z max-page-size=0x1000 \
-T linker.ld \
-gc-sections
Expand Down Expand Up @@ -122,10 +122,6 @@ override HEADER_DEPS := $(addprefix obj/,$(CFILES:.c=.c.d) $(ASFILES:.S=.S.d))
.PHONY: all
all: bin/$(KERNEL)

# The magic printf/dd command is used to force the final ELF file type to ET_DYN.
# GNU binutils, for silly reasons, forces the ELF type to ET_EXEC even for
# relocatable PIEs, if the base load address is non-0.
# See https://sourceware.org/bugzilla/show_bug.cgi?id=31795 for more information.
bin/$(KERNEL): GNUmakefile linker.ld obj/blob.c.o $(OBJ)
mkdir -p "$$(dirname $@)"
$(KLD) obj/blob.c.o $(OBJ) $(KLDFLAGS) -o $@
Expand All @@ -134,7 +130,6 @@ bin/$(KERNEL): GNUmakefile linker.ld obj/blob.c.o $(OBJ)
$(KCC) $(KCFLAGS) $(KCPPFLAGS) -c c/symbol_table.c -o obj/c/symbol_table.c.o
$(KLD) obj/blob.c.o $(OBJ) $(KLDFLAGS) -o $@
mv symbol_table.c.tmp c/symbol_table.c
printf '\003' | dd of=$@ bs=1 count=1 seek=16 conv=notrunc 2>/dev/null

obj/blob.c.o: $(VFILES)
mkdir -p "$$(dirname $@)"
Expand All @@ -159,7 +154,7 @@ clean:

.PHONY: distclean
distclean: clean
rm -rf c/freestanding-headers c/flanterm c/printf printf
rm -rf c/freestanding-headers c/flanterm c/printf

.PHONY: install
install:
Expand Down
59 changes: 48 additions & 11 deletions kernel/get-deps
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ test -z "$srcdir" && srcdir=.

cd "$srcdir"

FREESTANDING_HEADERS_COMMIT_HASH=dd3abd2d7147efc4170dff478d3b7730bed14147
FLANTERM_COMMIT_HASH=ef07a10cc38b34aa003d17be97a9f3542e275069
EYALROZ_PRINTF_COMMIT_HASH=2d75fef6c605e699d2643267cbaeaaa49b0a2635

clone_repo_commit() {
if test -d $2/.git; then
git -C $2 reset --hard
Expand All @@ -30,11 +26,52 @@ clone_repo_commit() {
fi
}

#if test -d .git; then
clone_repo_commit https://github.com/osdev0/freestanding-headers.git c/freestanding-headers $FREESTANDING_HEADERS_COMMIT_HASH
clone_repo_commit https://github.com/mintsuki/flanterm.git c/flanterm $FLANTERM_COMMIT_HASH
download_by_hash() {
DOWNLOAD_COMMAND="curl -Lo"
if ! command -v $DOWNLOAD_COMMAND >/dev/null 2>&1; then
DOWNLOAD_COMMAND="wget -O"
if ! command -v $DOWNLOAD_COMMAND >/dev/null 2>&1; then
echo "error: Neither curl nor wget found"
exit 1
fi
fi
SHA256_COMMAND="sha256sum"
if ! command -v $SHA256_COMMAND >/dev/null 2>&1; then
SHA256_COMMAND="sha256"
if ! command -v $SHA256_COMMAND >/dev/null 2>&1; then
echo "error: Cannot find sha256(sum) command"
exit 1
fi
fi
if ! test -f $2 || ! $SHA256_COMMAND $2 | grep $3 >/dev/null 2>&1; then
rm -f $2
mkdir -p $2 && rm -rf $2
$DOWNLOAD_COMMAND $2 $1
if ! $SHA256_COMMAND $2 | grep $3 >/dev/null 2>&1; then
echo "error: Cannot download file '$2' by hash"
exit 1
fi
fi
}

if ! test -f version; then
clone_repo_commit \
https://github.com/osdev0/freestanding-headers.git \
c/freestanding-headers \
dd3abd2d7147efc4170dff478d3b7730bed14147

clone_repo_commit \
https://github.com/mintsuki/flanterm.git \
c/flanterm \
ef07a10cc38b34aa003d17be97a9f3542e275069

download_by_hash \
https://github.com/eyalroz/printf/raw/2d75fef6c605e699d2643267cbaeaaa49b0a2635/src/printf/printf.c \
c/printf/printf.c \
bae9609d84dc7a56ff0b3d0d3787d33edbd1ec224f0f7ff7edefd6b6ae613395

clone_repo_commit https://github.com/eyalroz/printf.git printf $EYALROZ_PRINTF_COMMIT_HASH
mkdir -p c/printf
cp printf/src/printf/printf.c printf/src/printf/printf.h c/printf/
#fi
download_by_hash \
https://github.com/eyalroz/printf/raw/2d75fef6c605e699d2643267cbaeaaa49b0a2635/src/printf/printf.h \
c/printf/printf.h \
f63b84650f29394fd1bc6f6dce889d11570fcc152c5d512bfe6b68174c5aed56
fi
20 changes: 3 additions & 17 deletions kernel/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

/* Tell the linker that we want an x86_64 ELF64 output file */
OUTPUT_FORMAT(elf64-x86-64)
OUTPUT_ARCH(i386:x86-64)

/* We want the symbol main__kmain to be our entry point */
ENTRY(main__kmain)
Expand All @@ -16,11 +15,9 @@ ENTRY(main__kmain)
/* process. */
PHDRS
{
headers PT_PHDR PHDRS;
text PT_LOAD FILEHDR PHDRS;
text PT_LOAD;
rodata PT_LOAD;
data PT_LOAD;
dynamic PT_DYNAMIC;
}

SECTIONS
Expand All @@ -29,11 +26,9 @@ SECTIONS
/* and because that is what the Limine spec mandates. */
/* Any address in this region will do, but often 0xffffffff80000000 is chosen as */
/* that is the beginning of the region. */
/* Additionally, leave space for the ELF headers by adding SIZEOF_HEADERS to the */
/* base load address. */
. = 0xffffffff80000000 + SIZEOF_HEADERS;
. = 0xffffffff80000000;

text_start = . - SIZEOF_HEADERS;
text_start = .;

.text : {
*(.text .text.*)
Expand Down Expand Up @@ -65,11 +60,6 @@ SECTIONS
KEEP(*(.requests_end_marker))
} :data

/* Dynamic section for relocations, both in its own PHDR and inside data PHDR. */
.dynamic : {
*(.dynamic)
} :data :dynamic

/* NOTE: .bss needs to be the last thing mapped to :data, otherwise lots of */
/* unnecessary zeros will be written to the binary. */
/* If you need, for example, .init_array and .fini_array, those should be placed */
Expand All @@ -82,12 +72,8 @@ SECTIONS
data_end = .;

/* Discard .note.* and .eh_frame* since they may cause issues on some hosts. */
/* Also discard the program interpreter section since we do not need one. This is */
/* more or less equivalent to the --no-dynamic-linker linker flag, except that it */
/* works with ld.gold. */
/DISCARD/ : {
*(.eh_frame*)
*(.note .note.*)
*(.interp)
}
}

0 comments on commit b9e2cfe

Please sign in to comment.