From 54cc70f9b1d9aea90ce2819b5970b2bb86568b56 Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Wed, 21 Jun 2023 14:18:01 +0200 Subject: [PATCH] base: remove Env arg from init_exception_handling By supplying only the parts of the Env that are actually relevant for initializing the cxx heap, we avoid the need for the 'Env' interface. The patch also moves the call of 'init_ldso_phdr' to the outside of the cxx library because it does not belong there. Issue #4784 --- .../base-linux/src/lib/lx_hybrid/lx_hybrid.cc | 20 +++++++++----- .../base/src/include/base/internal/globals.h | 4 +-- repos/base/src/lib/base/component.cc | 15 ++++++++++- repos/base/src/lib/cxx/exception.cc | 26 +++---------------- repos/base/src/lib/cxx/malloc_free.cc | 5 ++-- .../base/src/lib/startup/init_main_thread.cc | 2 -- 6 files changed, 34 insertions(+), 38 deletions(-) diff --git a/repos/base-linux/src/lib/lx_hybrid/lx_hybrid.cc b/repos/base-linux/src/lib/lx_hybrid/lx_hybrid.cc index dcb865d51e..6cd6b08d04 100644 --- a/repos/base-linux/src/lib/lx_hybrid/lx_hybrid.cc +++ b/repos/base-linux/src/lib/lx_hybrid/lx_hybrid.cc @@ -89,14 +89,20 @@ namespace Genode { /* - * This function is normally provided by the cxx library, which is not - * used for lx_hybrid programs. For lx_hybrid programs, the exception - * handling is initialized by the host system's regular startup code. - * - * However, we conveniently use this function to get hold of the - * component's environment and initialize the default log output. + * For lx_hybrid programs, C++ support is initialized by the startup code + * provided by the host toolchain. */ -void Genode::init_exception_handling(Env &env) +void Genode::init_exception_handling(Ram_allocator &, Region_map &) { } + + +/* + * This function is normally provided by the dynamic linker, which is not used + * for lx_hybrid programs. For lx_hybrid programs. + * + * However, we conveniently use this function to get hold of the component's + * environment and initialize the default log output. + */ +void Genode::init_ldso_phdr(Env &env) { _env_ptr = &env; diff --git a/repos/base/src/include/base/internal/globals.h b/repos/base/src/include/base/internal/globals.h index 045f486f6d..0b8580a47b 100644 --- a/repos/base/src/include/base/internal/globals.h +++ b/repos/base/src/include/base/internal/globals.h @@ -28,11 +28,11 @@ namespace Genode { void init_platform(); void init_stack_area(); - void init_exception_handling(Env &); + void init_exception_handling(Ram_allocator &, Region_map &); void init_signal_transmitter(Env &); void init_signal_receiver(Pd_session &, Parent &); void init_cap_slab(Pd_session &, Parent &); - void init_cxx_heap(Env &); + void init_cxx_heap(Ram_allocator &, Region_map &); void init_cxx_guard(); void init_ldso_phdr(Env &); void init_signal_thread(Env &); diff --git a/repos/base/src/lib/base/component.cc b/repos/base/src/lib/base/component.cc index 50c0cb7a9a..44beffc473 100644 --- a/repos/base/src/lib/base/component.cc +++ b/repos/base/src/lib/base/component.cc @@ -234,6 +234,18 @@ Genode::size_t Component::stack_size() __attribute__((weak)); Genode::size_t Component::stack_size() { return 64*1024; } +/** + * Init program headers of the dynamic linker + * + * The weak function is used for statically linked binaries. The + * dynamic linker provides an implementation that loads the program + * headers of the linker. This must be done before the first exception + * is thrown. + */ +void Genode::init_ldso_phdr(Env &) __attribute__((weak)); +void Genode::init_ldso_phdr(Env &) { } + + /* * We need to execute the constructor of the main entrypoint from a * class called 'Startup' as 'Startup' is a friend of 'Entrypoint'. @@ -242,7 +254,8 @@ struct Genode::Startup { ::Env env { ep }; - bool const exception_handling = (init_exception_handling(env), true); + bool const ldso_phdr = (init_ldso_phdr(env), true); + bool const exception_handling = (init_exception_handling(env.pd(), env.rm()), true); bool const signal_receiver = (init_signal_receiver(env.pd(), env.parent()), true); /* diff --git a/repos/base/src/lib/cxx/exception.cc b/repos/base/src/lib/cxx/exception.cc index 5adec5c989..20afd9bd78 100644 --- a/repos/base/src/lib/cxx/exception.cc +++ b/repos/base/src/lib/cxx/exception.cc @@ -51,11 +51,7 @@ extern "C" int dl_iterate_phdr(int (*) (void *, unsigned long, void *), void *) return -1; } -/* - * Terminate handler - */ - -void terminate_handler() +static void terminate_handler() { std::type_info *t = __cxxabiv1::__cxa_current_exception_type(); @@ -74,25 +70,9 @@ void terminate_handler() } -/** - * Init program headers of the dynamic linker - * - * The weak function is used for statically linked binaries. The - * dynamic linker provides an implementation that loads the program - * headers of the linker. This must be done before the first exception - * is thrown. - */ -void Genode::init_ldso_phdr(Env &) __attribute__((weak)); -void Genode::init_ldso_phdr(Env &) { } - - -/* - * Initialization - */ -void Genode::init_exception_handling(Env &env) +void Genode::init_exception_handling(Ram_allocator &ram, Region_map &rm) { - init_ldso_phdr(env); - init_cxx_heap(env); + init_cxx_heap(ram, rm); __register_frame(__eh_frame_start__); diff --git a/repos/base/src/lib/cxx/malloc_free.cc b/repos/base/src/lib/cxx/malloc_free.cc index a5191dc96e..44578744d1 100644 --- a/repos/base/src/lib/cxx/malloc_free.cc +++ b/repos/base/src/lib/cxx/malloc_free.cc @@ -15,7 +15,6 @@ */ /* Genode includes */ -#include #include #include #include @@ -47,7 +46,7 @@ Heap &cxx_heap() * '__cxa_allocate_exception', which, in turn, calls 'malloc'. The cxx library * uses a local implementation of 'malloc' using a dedicated heap instance. */ -void Genode::init_cxx_heap(Env &env) +void Genode::init_cxx_heap(Ram_allocator &ram, Region_map &rm) { /* * Exception frames are small. Hence, a small static backing store suffices @@ -56,7 +55,7 @@ void Genode::init_cxx_heap(Env &env) */ static char initial_block[1024*sizeof(long)]; - cxx_heap_ptr = unmanaged_singleton(&env.ram(), &env.rm(), Heap::UNLIMITED, + cxx_heap_ptr = unmanaged_singleton(&ram, &rm, Heap::UNLIMITED, initial_block, sizeof(initial_block)); } diff --git a/repos/base/src/lib/startup/init_main_thread.cc b/repos/base/src/lib/startup/init_main_thread.cc index 1fe40a3af2..0b95c89da4 100644 --- a/repos/base/src/lib/startup/init_main_thread.cc +++ b/repos/base/src/lib/startup/init_main_thread.cc @@ -26,8 +26,6 @@ using namespace Genode; addr_t init_main_thread_result; -extern void init_exception_handling(); - namespace Genode { extern Region_map * env_stack_area_region_map; } void prepare_init_main_thread();