Skip to content

Commit

Permalink
add TLS support.
Browse files Browse the repository at this point in the history
  • Loading branch information
aliaspider committed Sep 29, 2020
1 parent 59b83da commit 24ea795
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cafe/coreinit.def
Original file line number Diff line number Diff line change
Expand Up @@ -1200,7 +1200,7 @@ __PPCHalt
//__ghs_mtx_lock
//__ghs_mtx_unlock
__os_snprintf
//__tls_get_addr
__tls_get_addr
bspGetConsoleTypeRaw
bspGetEntityVersion
bspGetHardwareVersion
Expand Down
1 change: 1 addition & 0 deletions samples/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
11 changes: 11 additions & 0 deletions samples/cmake/tls_test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}")
48 changes: 48 additions & 0 deletions samples/cmake/tls_test/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <coreinit/thread.h>
#include <coreinit/time.h>

#include <whb/proc.h>
#include <whb/log.h>
#include <whb/log_console.h>

#include <thread>

__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;
}
23 changes: 18 additions & 5 deletions share/wut.ld
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ SECTIONS
*(.text.*)
*(.gnu.warning)
*(.gnu.linkonce.t.*)
*(.got)
*(.got1)
*(.got2)
*(.got.plt)
*(.plt)

KEEP (*(.fini))
} > codemem
Expand Down Expand Up @@ -56,11 +61,6 @@ SECTIONS
*(.fixup)
*(.gcc_except_table)
*(.gcc_except_table.*)
*(.got)
*(.got1)
*(.got2)
*(.got.plt)
*(.plt)
*(.tm_clone_table)
} > datamem

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion share/wut.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion share/wut.specs
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion share/wut.toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 "")
Expand Down

0 comments on commit 24ea795

Please sign in to comment.