From 233ea7eb1ded63f5e9c067e5b97851bd2de6cdbb Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Thu, 31 Oct 2019 12:01:13 -0600 Subject: [PATCH] Kernel: Add bare minimum for global constructors (#707) Add text.startup to the .text block, add .ctors as well. Use them in init.cpp to call global constructors after gtd and idt init. That way any funky constructors should be ok. Also defines some Itanium C++ ABI methods that probably shouldn't be, but without them the linker gets very angry. If the code ever actually tries to use __dso_handle or call __cxa_atexit, there's bigger problems with the kernel. Bit of a hack would be an understatement but hey. It works :) --- Kernel/init.cpp | 20 ++++++++++++++++++++ Kernel/linker.ld | 5 +++++ 2 files changed, 25 insertions(+) diff --git a/Kernel/init.cpp b/Kernel/init.cpp index 3510d126ee..1393d14be5 100644 --- a/Kernel/init.cpp +++ b/Kernel/init.cpp @@ -192,6 +192,22 @@ extern "C" { multiboot_info_t* multiboot_info_ptr; } +typedef void (*ctor_func_t)(); + +// Defined in the linker script +extern ctor_func_t start_ctors; +extern ctor_func_t end_ctors; + +// Define some Itanium C++ ABI methods to stop the linker from complaining +// If we actually call these something has gone horribly wrong +void* __dso_handle __attribute__((visibility ("hidden"))); + +extern "C" int __cxa_atexit ( void (*)(void *), void *, void *) +{ + ASSERT_NOT_REACHED(); + return 0; +} + extern "C" [[noreturn]] void init() { // this is only used one time, directly below here. we can't use this part @@ -234,6 +250,10 @@ extern "C" [[noreturn]] void init() gdt_init(); idt_init(); + // call global constructors after gtd and itd init + for (ctor_func_t* ctor = &start_ctors; ctor < &end_ctors; ctor++) + (*ctor)(); + keyboard = new KeyboardDevice; ps2mouse = new PS2MouseDevice; sb16 = new SB16; diff --git a/Kernel/linker.ld b/Kernel/linker.ld index e0880fd96e..986ede14ed 100644 --- a/Kernel/linker.ld +++ b/Kernel/linker.ld @@ -9,10 +9,15 @@ SECTIONS Arch/i386/Boot/boot.ao *(.multiboot) *(.text) + *(.text.startup) } .rodata BLOCK(4K) : ALIGN(4K) { + start_ctors = .; + *(.ctors) + end_ctors = .; + *(.rodata) }