From 0abd9153ae5372da715e9646627018e46091b55f Mon Sep 17 00:00:00 2001 From: apio Date: Sun, 1 Sep 2024 12:39:55 +0200 Subject: [PATCH] tools+libluna: Make new and delete weak to avoid conflicts with libstdc++ Wasn't causing problems earlier, but when trying to rebuild the toolchain, it failed because of this. --- libluna/CMakeLists.txt | 3 ++ libluna/src/Heap.cpp | 66 ++++++++++++++++++++++-------------------- tools/setup-gcc.sh | 2 +- 3 files changed, 39 insertions(+), 32 deletions(-) diff --git a/libluna/CMakeLists.txt b/libluna/CMakeLists.txt index 7826cda1..7b49e9d1 100644 --- a/libluna/CMakeLists.txt +++ b/libluna/CMakeLists.txt @@ -51,6 +51,9 @@ add_library(luna ${SOURCES}) target_compile_options(luna PRIVATE ${COMMON_FLAGS}) target_include_directories(luna PUBLIC include/) target_include_directories(luna PUBLIC ${LUNA_BASE}/usr/include) +if(DEFINED LIBC_BOOTSTRAP) +target_compile_definitions(luna PUBLIC LIBC_BOOTSTRAP) +endif() if("${LUNA_ARCH}" MATCHES "x86_64") target_compile_options(luna-freestanding PRIVATE -mno-red-zone) diff --git a/libluna/src/Heap.cpp b/libluna/src/Heap.cpp index 44783998..8b9f9965 100644 --- a/libluna/src/Heap.cpp +++ b/libluna/src/Heap.cpp @@ -420,35 +420,39 @@ void dump_heap_usage() dbgln("-- Heap memory in use: %zu bytes", alloc_used); } -// Otherwise, this is defined in libstdc++. -#ifdef USE_FREESTANDING -void* operator new(usize size, const std::nothrow_t&) noexcept -{ - return malloc_impl(size, false, false).value_or(nullptr); -} - -void* operator new[](usize size, const std::nothrow_t&) noexcept -{ - return malloc_impl(size, false, false).value_or(nullptr); -} - -void operator delete(void* p) noexcept -{ - free_impl(p); -} - -void operator delete[](void* p) noexcept -{ - free_impl(p); -} - -void operator delete(void* p, usize) noexcept -{ - free_impl(p); -} - -void operator delete[](void* p, usize) noexcept -{ - free_impl(p); -} +#if defined(USE_FREESTANDING) || defined(LIBC_BOOTSTRAP) +#define WEAK +#else +#define WEAK __attribute__((weak)) #endif + +// If libstdc++ is linked, it should override these definitions. +void* WEAK operator new(usize size, const std::nothrow_t&) noexcept +{ + return malloc_impl(size, false, false).value_or(nullptr); +} + +void* WEAK operator new[](usize size, const std::nothrow_t&) noexcept +{ + return malloc_impl(size, false, false).value_or(nullptr); +} + +void WEAK operator delete(void* p) noexcept +{ + free_impl(p); +} + +void WEAK operator delete[](void* p) noexcept +{ + free_impl(p); +} + +void WEAK operator delete(void* p, usize) noexcept +{ + free_impl(p); +} + +void WEAK operator delete[](void* p, usize) noexcept +{ + free_impl(p); +} diff --git a/tools/setup-gcc.sh b/tools/setup-gcc.sh index 3e4618ad..1413f534 100755 --- a/tools/setup-gcc.sh +++ b/tools/setup-gcc.sh @@ -64,7 +64,7 @@ make install-gcc make install-target-libgcc mkdir -p $LUNA_BUILD_DIR -cmake -S $LUNA_ROOT -B $LUNA_BUILD_DIR -G "$LUNA_CMAKE_GENERATOR_NAME" +cmake -S $LUNA_ROOT -B $LUNA_BUILD_DIR -G "$LUNA_CMAKE_GENERATOR_NAME" -DLIBC_BOOTSTRAP=1 cmake --build $LUNA_BUILD_DIR --target libc make all-target-libstdc++-v3 CXXFLAGS_FOR_TARGET="-fno-exceptions" -j$(nproc)