diff --git a/libraries/CMakeLists.txt b/libraries/CMakeLists.txt index f192320..fb32b55 100644 --- a/libraries/CMakeLists.txt +++ b/libraries/CMakeLists.txt @@ -4,6 +4,7 @@ project(libraries C) add_subdirectory(libdefaultheap) add_subdirectory(libgfd) add_subdirectory(libwhb) +add_subdirectory(wutcrt) add_subdirectory(wutdevoptab) add_subdirectory(wutnewlib) add_subdirectory(wutstdc++) diff --git a/libraries/wutcrt/CMakeLists.txt b/libraries/wutcrt/CMakeLists.txt new file mode 100644 index 0000000..468a3c9 --- /dev/null +++ b/libraries/wutcrt/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required(VERSION 3.2) +project(wutnewlib C) + +set_property(SOURCE crt0.s PROPERTY LANGUAGE C) + +add_library(wutcrt + crt0.s + wut_crt.c) +target_include_directories(wutcrt PRIVATE "${WUT_ROOT}/include") + +install(TARGETS wutcrt + ARCHIVE DESTINATION "${CMAKE_INSTALL_PREFIX}/lib") diff --git a/libraries/wutnewlib/crt0.s b/libraries/wutcrt/crt0.s similarity index 75% rename from libraries/wutnewlib/crt0.s rename to libraries/wutcrt/crt0.s index 01fd202..5952f2e 100644 --- a/libraries/wutnewlib/crt0.s +++ b/libraries/wutcrt/crt0.s @@ -1,15 +1,18 @@ .extern main .extern exit +.extern __init_wut +.extern __fini_wut .global _start _start: stwu 1, -0x8(1) stw 3, 0(1) stw 4, 4(1) - bl __init_wut_newlibc + bl __init_wut lwz 3, 0(1) lwz 4, 4(1) bl main + bl __fini_wut # Calling __fini is leading to crashes! # bl __fini addi 1, 1, 0x8 diff --git a/libraries/wutcrt/wut_crt.c b/libraries/wutcrt/wut_crt.c new file mode 100644 index 0000000..21b6fa4 --- /dev/null +++ b/libraries/wutcrt/wut_crt.c @@ -0,0 +1,39 @@ +extern void __init_wut_newlib() __attribute__((weak)); +extern void __init_wut_devoptab() __attribute__((weak)); +extern void __init_wut_stdcpp() __attribute__((weak)); + +extern void __fini_wut_devoptab() __attribute__((weak)); +extern void __fini_wut_newlib() __attribute__((weak)); +extern void __fini_wut_stdcpp() __attribute__((weak)); + +void +__init_wut() +{ + if (__init_wut_newlib) { + __init_wut_newlib(); + } + + if (__init_wut_devoptab) { + __init_wut_devoptab(); + } + + if (__init_wut_stdcpp) { + __init_wut_stdcpp(); + } +} + +void +__fini_wut() +{ + if (__fini_wut_stdcpp) { + __fini_wut_stdcpp(); + } + + if (__fini_wut_devoptab) { + __fini_wut_devoptab(); + } + + if (__fini_wut_newlib) { + __fini_wut_newlib(); + } +} diff --git a/libraries/wutnewlib/CMakeLists.txt b/libraries/wutnewlib/CMakeLists.txt index 0fd4391..3c4652d 100644 --- a/libraries/wutnewlib/CMakeLists.txt +++ b/libraries/wutnewlib/CMakeLists.txt @@ -1,10 +1,7 @@ cmake_minimum_required(VERSION 3.2) project(wutnewlib C) -set_property(SOURCE crt0.s PROPERTY LANGUAGE C) - add_library(wutnewlib - crt0.s sleep.c syscalls.c) target_include_directories(wutnewlib PRIVATE "${WUT_ROOT}/include") diff --git a/libraries/wutnewlib/syscalls.c b/libraries/wutnewlib/syscalls.c index b6755d2..8559d98 100644 --- a/libraries/wutnewlib/syscalls.c +++ b/libraries/wutnewlib/syscalls.c @@ -18,9 +18,6 @@ static uint8_t *sHeapBase = NULL; static uint32_t sHeapMaxSize = 0; static volatile uint32_t sHeapSize = 0; -void -__init_wut_newlibc(); - static void * __libwut_sbrk_r(struct _reent *r, ptrdiff_t incr) @@ -172,16 +169,16 @@ __init_syscall_array() __syscalls.gettod_r = __libwut_gettod_r; } -extern void __init_wut_gthread() __attribute__((weak)); - void -__init_wut_newlibc() +__init_wut_newlib() { __init_libc_heap(); __init_malloc_lock(); __init_syscall_array(); - - if (__init_wut_gthread) { - __init_wut_gthread(); - } +} + +void +__fini_wut_newlib() +{ + __free_libc_heap(); } diff --git a/libraries/wutstdc++/CMakeLists.txt b/libraries/wutstdc++/CMakeLists.txt index 9989ddf..7d16c48 100644 --- a/libraries/wutstdc++/CMakeLists.txt +++ b/libraries/wutstdc++/CMakeLists.txt @@ -8,7 +8,8 @@ add_library(wutstdc++ wut_gthread_mutex.cc wut_gthread_once.cc wut_gthread_recursive_mutex.cc - wut_gthread_thread.cc) + wut_gthread_thread.cc + wut_stdcpp.cc) target_compile_definitions(wutstdc++ PRIVATE _GLIBCXX_HAS_GTHREADS) diff --git a/libraries/wutstdc++/wut_gthread.cc b/libraries/wutstdc++/wut_gthread.cc index 176f9ad..9d08d64 100644 --- a/libraries/wutstdc++/wut_gthread.cc +++ b/libraries/wutstdc++/wut_gthread.cc @@ -37,9 +37,6 @@ __wut_active_p() return 1; } -extern "C" -{ - void __init_wut_gthread() { @@ -74,5 +71,3 @@ __init_wut_gthread() __gthread_impl.cond_wait_recursive = (__gthread_fn_cond_wait_recursive)__wut_cond_wait_recursive; __gthread_impl.cond_destroy = (__gthread_fn_cond_destroy)__wut_cond_destroy; } - -} // extern "c" diff --git a/libraries/wutstdc++/wut_gthread.h b/libraries/wutstdc++/wut_gthread.h index d4f53fc..5ac355a 100644 --- a/libraries/wutstdc++/wut_gthread.h +++ b/libraries/wutstdc++/wut_gthread.h @@ -20,6 +20,9 @@ typedef volatile uint32_t __wut_once_t; typedef uint32_t __wut_key_t; +void +__init_wut_gthread(); + int __wut_active_p(); diff --git a/libraries/wutstdc++/wut_stdcpp.cc b/libraries/wutstdc++/wut_stdcpp.cc new file mode 100644 index 0000000..f15f6df --- /dev/null +++ b/libraries/wutstdc++/wut_stdcpp.cc @@ -0,0 +1,12 @@ +#include "wut_gthread.h" + +extern "C" void +__init_wut_stdcpp() +{ + __init_wut_gthread(); +} + +extern "C" void +__fini_wut_stdcpp() +{ +} diff --git a/share/wut.cmake b/share/wut.cmake index 9237bd4..53445b0 100644 --- a/share/wut.cmake +++ b/share/wut.cmake @@ -1,24 +1,39 @@ cmake_minimum_required(VERSION 3.2) +macro(wut_enable_newlib target) + set_property(TARGET ${target} + APPEND_STRING PROPERTY + LINK_FLAGS " -Wl,--whole-archive -lwutnewlib -Wl,--no-whole-archive") +endmacro(wut_enable_newlib) + +macro(wut_enable_devoptab target) + set_property(TARGET ${target} + APPEND_STRING PROPERTY + LINK_FLAGS " -Wl,--whole-archive -lwutdevoptab -Wl,--no-whole-archive") +endmacro(wut_enable_devoptab) + macro(wut_enable_stdcpp target) target_link_libraries(${target} - wutnewlib - stdc++) + stdc++) - set_target_properties(${target} PROPERTIES - COMPILE_FLAGS "-std=c++17" - LINK_FLAGS "-Wl,--whole-archive -lwutstdc++ -Wl,--no-whole-archive") + set_property(TARGET ${target} + APPEND_STRING PROPERTY + COMPILE_FLAGS "-std=c++17") + + set_property(TARGET ${target} + APPEND_STRING PROPERTY + LINK_FLAGS " -Wl,--whole-archive -lwutstdc++ -Wl,--no-whole-archive") endmacro(wut_enable_stdcpp) macro(wut_create_rpx target source) target_link_libraries(${source} - wutnewlib - coreinit) + wutnewlib + coreinit) add_custom_target(${target} ALL - COMMAND ${WUT_ELF2RPL} ${source} ${target} - DEPENDS ${source} - COMMENT "Converting to RPX ${target}") + COMMAND ${WUT_ELF2RPL} ${source} ${target} + DEPENDS ${source} + COMMENT "Converting to RPX ${target}") add_dependencies(${target} ${source}) endmacro(wut_create_rpx) diff --git a/share/wut.toolchain.cmake b/share/wut.toolchain.cmake index 3325127..ccdd55a 100644 --- a/share/wut.toolchain.cmake +++ b/share/wut.toolchain.cmake @@ -30,7 +30,7 @@ set(WUT_C_FLAGS "-mcpu=750 -meabi -mhard-float -Wl,-q \"-I${WUT_ROOT} set(CMAKE_C_FLAGS "${WUT_C_FLAGS}" CACHE STRING "") set(CMAKE_CXX_FLAGS "${WUT_C_FLAGS}" CACHE STRING "") set(CMAKE_ASM_FLAGS "${WUT_C_FLAGS}" CACHE STRING "") -set(CMAKE_EXE_LINKER_FLAGS "-Wl,-z,nocopyreloc -T \"${WUT_ROOT}/share/wut.ld\" \"-L${WUT_ROOT}/lib\"" CACHE STRING "") +set(CMAKE_EXE_LINKER_FLAGS "-Wl,-z,nocopyreloc -T \"${WUT_ROOT}/share/wut.ld\" \"-L${WUT_ROOT}/lib\" -lcoreinit -lwutcrt" CACHE STRING "") # Setup root to exclude host system headers + libraries set(CMAKE_FIND_ROOT_PATH "${DEVKITPPC}" "${WUT_ROOT}/bin" "${CMAKE_INSTALL_PREFIX}" "${CMAKE_INSTALL_PREFIX}/share")