Skip to content

Commit

Permalink
kern/build: Backports from C template
Browse files Browse the repository at this point in the history
  • Loading branch information
mintsuki committed Jul 9, 2024
1 parent defb990 commit b22236d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 29 deletions.
20 changes: 11 additions & 9 deletions kernel/GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ $(eval $(call DEFAULT_VAR,KTOOLCHAIN,$(DEFAULT_KTOOLCHAIN)))
override DEFAULT_KCC := $(KTOOLCHAIN)cc
$(eval $(call DEFAULT_VAR,KCC,$(DEFAULT_KCC)))

override DEFAULT_KLD := $(KTOOLCHAIN)ld
$(eval $(call DEFAULT_VAR,KLD,$(DEFAULT_KLD)))

override DEFAULT_KOBJDUMP := $(KTOOLCHAIN)objdump
$(eval $(call DEFAULT_VAR,KOBJDUMP,$(DEFAULT_KOBJDUMP)))

Expand Down Expand Up @@ -85,14 +88,13 @@ obj/c/flanterm/backends/fb.c.o: override KCPPFLAGS += \
-DFLANTERM_FB_DISABLE_BUMP_ALLOC

override KLDFLAGS += \
-m elf_x86_64 \
-nostdlib \
-Wl,-m,elf_x86_64 \
-Wl,-nostdlib \
-Wl,-pie \
-Wl,-z,text \
-Wl,-z,max-page-size=0x1000 \
-Wl,-T,linker.ld \
-Wl,-gc-sections
-pie \
-z text \
-z max-page-size=0x1000 \
-T linker.ld \
-gc-sections

override KVFLAGS += \
-os vinix \
Expand Down Expand Up @@ -126,11 +128,11 @@ all: bin/$(KERNEL)
# 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 $@)"
$(KCC) $(KCFLAGS) obj/blob.c.o $(OBJ) $(KLDFLAGS) -o $@
$(KLD) obj/blob.c.o $(OBJ) $(KLDFLAGS) -o $@
mv c/symbol_table.c symbol_table.c.tmp
./gensyms.sh $(KOBJDUMP) $@ > c/symbol_table.c
$(KCC) $(KCFLAGS) $(KCPPFLAGS) -c c/symbol_table.c -o obj/c/symbol_table.c.o
$(KCC) $(KCFLAGS) obj/blob.c.o $(OBJ) $(KLDFLAGS) -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

Expand Down
61 changes: 41 additions & 20 deletions kernel/linker.ld
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* linker.ld: Linker script of the kernel.
* Code is governed by the GPL-2.0 license.
* Copyright (C) 2021-2022 The Vinix authors.
* Copyright (C) 2021-2024 The Vinix authors.
*/

/* Tell the linker that we want an x86_64 ELF64 output file */
Expand All @@ -11,6 +11,18 @@ OUTPUT_ARCH(i386:x86-64)
/* We want the symbol main__kmain to be our entry point */
ENTRY(main__kmain)

/* Define the program headers we want so the bootloader gives us the right */
/* MMU permissions; this also allows us to exert more control over the linking */
/* process. */
PHDRS
{
headers PT_PHDR PHDRS;
text PT_LOAD FILEHDR PHDRS;
rodata PT_LOAD;
data PT_LOAD;
dynamic PT_DYNAMIC;
}

SECTIONS
{
/* We want to be placed in the topmost 2GiB of the address space, for optimisations */
Expand All @@ -21,52 +33,61 @@ SECTIONS
/* base load address. */
. = 0xffffffff80000000 + SIZEOF_HEADERS;

rodata_start = . - SIZEOF_HEADERS;
text_start = . - SIZEOF_HEADERS;

.rodata ALIGN(CONSTANT(MAXPAGESIZE)) : {
*(.rodata .rodata.*)
}
.text : {
*(.text .text.*)
} :text

.text ALIGN(CONSTANT(MAXPAGESIZE)) : {
rodata_end = .;
text_start = .;
/* Move to the next memory page for .rodata */
. = ALIGN(CONSTANT(MAXPAGESIZE));

*(.text .text.*)
}
text_end = .;
rodata_start = .;

.rodata : {
*(.rodata .rodata.*)
} :rodata

/* Move to the next memory page for .data */
. = ALIGN(CONSTANT(MAXPAGESIZE));

.data ALIGN(CONSTANT(MAXPAGESIZE)) : {
text_end = .;
data_start = .;
rodata_end = .;
data_start = .;

.data : {
*(.data .data.*)

/* Place the sections that contain the Limine requests as part of the .data */
/* output section. */
KEEP(*(.requests_start_marker))
KEEP(*(.requests))
KEEP(*(.requests_end_marker))
}
} :data

/* Dynamic section for relocations and other PIE related information. */
/* 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 the data PHDR, otherwise lots of */
/* 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 */
/* above this. */
.bss ALIGN(CONSTANT(MAXPAGESIZE)) : {
.bss : {
*(.bss .bss.*)
*(COMMON)
}
} :data

data_end = .;

/* Discard the program interpreter section since we do not need one. This is */
/* 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 b22236d

Please sign in to comment.