From 24ea7955317878de5897fd2d7b1a9faac5668356 Mon Sep 17 00:00:00 2001 From: aliaspider Date: Tue, 29 Sep 2020 16:59:49 +0100 Subject: [PATCH] add TLS support. --- cafe/coreinit.def | 2 +- samples/cmake/CMakeLists.txt | 1 + samples/cmake/tls_test/CMakeLists.txt | 11 ++++++ samples/cmake/tls_test/main.cpp | 48 +++++++++++++++++++++++++++ share/wut.ld | 23 ++++++++++--- share/wut.mk | 2 +- share/wut.specs | 2 +- share/wut.toolchain.cmake | 2 +- 8 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 samples/cmake/tls_test/CMakeLists.txt create mode 100644 samples/cmake/tls_test/main.cpp diff --git a/cafe/coreinit.def b/cafe/coreinit.def index 32939ef46..9d3dcd12e 100644 --- a/cafe/coreinit.def +++ b/cafe/coreinit.def @@ -1200,7 +1200,7 @@ __PPCHalt //__ghs_mtx_lock //__ghs_mtx_unlock __os_snprintf -//__tls_get_addr +__tls_get_addr bspGetConsoleTypeRaw bspGetEntityVersion bspGetHardwareVersion diff --git a/samples/cmake/CMakeLists.txt b/samples/cmake/CMakeLists.txt index aefae08e3..febecf541 100644 --- a/samples/cmake/CMakeLists.txt +++ b/samples/cmake/CMakeLists.txt @@ -10,6 +10,7 @@ add_subdirectory(helloworld) add_subdirectory(helloworld_cpp) add_subdirectory(my_first_rpl) add_subdirectory(swkbd) +add_subdirectory(tls_test) install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/content/" DESTINATION "${CMAKE_INSTALL_PREFIX}/content") diff --git a/samples/cmake/tls_test/CMakeLists.txt b/samples/cmake/tls_test/CMakeLists.txt new file mode 100644 index 000000000..eaa4a6208 --- /dev/null +++ b/samples/cmake/tls_test/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.2) +project(tls_test CXX) +include("${DEVKITPRO}/wut/share/wut.cmake" REQUIRED) + +add_executable(tls_test + main.cpp) + +wut_create_rpx(tls_test) + +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/tls_test.rpx" + DESTINATION "${CMAKE_INSTALL_PREFIX}") diff --git a/samples/cmake/tls_test/main.cpp b/samples/cmake/tls_test/main.cpp new file mode 100644 index 000000000..9399cfbac --- /dev/null +++ b/samples/cmake/tls_test/main.cpp @@ -0,0 +1,48 @@ +#include +#include + +#include +#include +#include + +#include + +__thread +__attribute((tls_model("global-dynamic"))) +const char* thread_name = ""; + +int +thread1_entry(void) +{ + thread_name = "Thread1"; + WHBLogPrintf("Hello from %-20s (thread_name@0x%08X)\n", thread_name, &thread_name); + return 0; +} + +int +main(int argc, char **argv) +{ + WHBProcInit(); + WHBLogConsoleInit(); + + thread_name = "Main Thread"; + WHBLogPrintf("Hello from %-20s (thread_name@0x%08X)\n", thread_name, &thread_name); + + std::thread th1 = std::thread(thread1_entry); + th1.join(); + + WHBLogPrintf("Hello from %-20s (thread_name@0x%08X)\n", thread_name, &thread_name); + WHBLogConsoleDraw(); + + while(WHBProcIsRunning()) { + OSSleepTicks(OSMillisecondsToTicks(1000)); + } + + WHBLogPrintf("Exiting... good bye."); + WHBLogConsoleDraw(); + OSSleepTicks(OSMillisecondsToTicks(1000)); + + WHBLogConsoleFree(); + WHBProcShutdown(); + return 0; +} diff --git a/share/wut.ld b/share/wut.ld index d3107f655..ff8a7c108 100644 --- a/share/wut.ld +++ b/share/wut.ld @@ -25,6 +25,11 @@ SECTIONS *(.text.*) *(.gnu.warning) *(.gnu.linkonce.t.*) + *(.got) + *(.got1) + *(.got2) + *(.got.plt) + *(.plt) KEEP (*(.fini)) } > codemem @@ -56,11 +61,6 @@ SECTIONS *(.fixup) *(.gcc_except_table) *(.gcc_except_table.*) - *(.got) - *(.got1) - *(.got2) - *(.got.plt) - *(.plt) *(.tm_clone_table) } > datamem @@ -107,6 +107,19 @@ SECTIONS } > datamem __bss_end = .; + .thrdata : { + *(.tdata) + *(.tdata.*) + *(.gnu.linkonce.td.*) + } > datamem + + .thrbss : { + *(.tbss) + *(.tbss.*) + *(.gnu.linkonce.tb.*) + *(.tcommon) + } > datamem + . = ORIGIN(relmem); .rela.text ALIGN(4) : { *(.rela.text) diff --git a/share/wut.mk b/share/wut.mk index 62791cc55..2870bc000 100644 --- a/share/wut.mk +++ b/share/wut.mk @@ -46,7 +46,7 @@ DEPSEXT ?= d # There's some default wut flags, they need to be added # cpu type and hardware flags, keep all relocations (for elf2rpl), wut includes -WUT_CFLAGS := -mcpu=750 -meabi -mhard-float -isystem $(WUT_ROOT)/include +WUT_CFLAGS := -mcpu=750 -meabi -mhard-float -ftls-model=global-dynamic -isystem $(WUT_ROOT)/include # Defines to tell code about this environment WUT_CFLAGS += -D__WIIU__ -D__WUT__ -D__WUT_MAKEFILE__ # keep relocations, use wut linker script diff --git a/share/wut.specs b/share/wut.specs index 75905bcd5..8d707bc19 100644 --- a/share/wut.specs +++ b/share/wut.specs @@ -1,4 +1,4 @@ %rename link old_link *link: -%(old_link) -T %:getenv(DEVKITPRO /wut/share/wut.ld) --gc-sections --emit-relocs -z nocopyreloc %(wut_entry) +%(old_link) -T %:getenv(DEVKITPRO /wut/share/wut.ld) --gc-sections --emit-relocs -z nocopyreloc --no-tls-optimize %(wut_entry) diff --git a/share/wut.toolchain.cmake b/share/wut.toolchain.cmake index f2a9045c1..f241352c5 100644 --- a/share/wut.toolchain.cmake +++ b/share/wut.toolchain.cmake @@ -76,7 +76,7 @@ set(CMAKE_LINKER "${DEVKITPPC_LD}" CACHE PATH "") set(CMAKE_AR "${DEVKITPPC_AR}" CACHE PATH "") set(CMAKE_STRIP "${DEVKITPPC_STRIP}" CACHE PATH "") -set(WUT_C_FLAGS "-mcpu=750 -meabi -mhard-float -Wl,-q -D__WIIU__ -D__WUT__") +set(WUT_C_FLAGS "-mcpu=750 -meabi -mhard-float -ftls-model=global-dynamic -Wl,-q -D__WIIU__ -D__WUT__") 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 "")