Skip to content

Commit a76467d

Browse files
committed
Move reent logic into WUMSLoader
1 parent 5ca1144 commit a76467d

9 files changed

Lines changed: 137 additions & 240 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ include $(TOPDIR)/share/wums_rules
33

44
export WUMS_MAJOR := 0
55
export WUMS_MINOR := 3
6-
export WUMS_PATCH := 5
6+
export WUMS_PATCH := 6
77

88
VERSION := $(WUMS_MAJOR).$(WUMS_MINOR).$(WUMS_PATCH)
99

include/wums/hooks.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include "common.h"
2929
#include "defines/module_defines.h"
30+
#include "reent_internal.h"
3031

3132
#ifdef __cplusplus
3233
extern "C" {
@@ -68,7 +69,9 @@ typedef enum wums_hook_type_t {
6869
WUMS_HOOK_GET_CUSTOM_RPL_ALLOCATOR, // for internal usage only
6970
WUMS_HOOK_CLEAR_ALLOCATED_RPL_MEMORY, // for internal usage only
7071
// Introduced in 0.3.5
71-
WUMS_HOOK_INIT_WUT_THREAD // for internal usage only
72+
WUMS_HOOK_INIT_WUT_THREAD, // for internal usage only
73+
// Introduced in 0.3.6
74+
WUMS_HOOK_INIT_REENT_FUNCTIONS // for internal usage only
7275
} wums_hook_type_t;
7376

7477
typedef uint32_t (*WUMSRPLAllocatorAllocFn)(int32_t size, int32_t align, void **outAddr);
@@ -196,6 +199,14 @@ typedef struct wums_relocs_done_args_t {
196199
} \
197200
WUMS_HOOK_EX(WUMS_HOOK_INIT_WUT_THREAD, on_init_wut_thread);
198201

202+
#define WUMS_INIT_REENT_FUNCTIONS() \
203+
__EXTERN_C_MACRO void WUMSReentAPI_InitInternal(wums_loader_init_reent_args_t_); \
204+
void wums_init_reent_functions(wums_loader_init_reent_args_t_ args); \
205+
WUMS_HOOK_EX(WUMS_HOOK_INIT_REENT_FUNCTIONS, wums_init_reent_functions); \
206+
void wums_init_reent_functions(wums_loader_init_reent_args_t_ args) { \
207+
return WUMSReentAPI_InitInternal(args); \
208+
}
209+
199210
#define WUMS_USE_WUT_SOCKETS() \
200211
__EXTERN_C_MACRO void __init_wut_socket(); \
201212
WUMS_HOOK_EX(WUMS_HOOK_INIT_WUT_SOCKETS, __init_wut_socket); \

include/wums/meta.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
extern "C" {
4040
#endif
4141

42-
#define WUMS_VERSION "0.3.5"
42+
#define WUMS_VERSION "0.3.6"
4343

4444
#define WUMS_MODULE_EXPORT_NAME(__module_name) \
4545
WUMS_META(export_name, __module_name); \
@@ -50,6 +50,7 @@ extern "C" {
5050
WUMS_USE_WUT_THREAD(); \
5151
WUMS___INIT_WRAPPER(); \
5252
WUMS___FINI_WRAPPER(); \
53+
WUMS_INIT_REENT_FUNCTIONS(); \
5354
WUMS_META(buildtimestamp, __DATE__ " " __TIME__); \
5455
extern const char wums_meta_module_name[] WUMS_SECTION("meta"); \
5556
const char wums_meta_module_name[] = __module_name; \

include/wums/reent_internal.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
#include <stdint.h>
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
typedef enum wums_loader_init_reent_errors_t_ {
9+
WUMSReent_ERROR_NONE = 0,
10+
WUMSReent_ERROR_GLOBAL_REENT_REQUESTED = 1,
11+
WUMSReent_ERROR_NO_THREAD = 2,
12+
} wums_loader_init_reent_errors_t_;
13+
14+
typedef void *(*WUMSReent_GetReentContext)(const void *moduleId, wums_loader_init_reent_errors_t_ *outError);
15+
typedef void *(*WUMSReent_SetSentinel)();
16+
typedef void (*WUMSReent_RestoreHead)(void *oldHead);
17+
typedef bool (*WUMSReent_AddReentContext)(const void *moduleId, void *reentPtr, void (*cleanupFn)(void *), void *oldHead);
18+
19+
typedef uint32_t WUMS_REENT_API_VERSION;
20+
21+
#define WUMS_REENT_CUR_API_VERSION 0x01
22+
23+
typedef struct wums_loader_init_reent_args_t_ {
24+
WUMS_REENT_API_VERSION version;
25+
WUMSReent_GetReentContext get_context_ptr;
26+
WUMSReent_SetSentinel set_sentinel_ptr;
27+
WUMSReent_RestoreHead restore_head_ptr;
28+
WUMSReent_AddReentContext add_reent_context_ptr;
29+
} wums_loader_init_reent_args_t_;
30+
31+
void WUMSReentAPI_InitInternal(wums_loader_init_reent_args_t_ args);
32+
33+
#ifdef __cplusplus
34+
}
35+
#endif

include/wums/wums_debug.h

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,10 @@
11
#pragma once
22

3-
#if defined(__GNUC__) || defined(__clang__)
4-
5-
#define WUMS_FORMAT_PRINTF(fmt, args) __attribute__((__format__(__printf__, fmt, args)))
6-
7-
#else // not __GNUC__ and not __clang__
8-
9-
#define WUMS_FORMAT_PRINTF(fmt, args)
10-
11-
#endif //__GNUC__ or __clang__
12-
3+
extern "C" void OSReport(const char *fmt, ...);
134

145
#ifdef __cplusplus
156
extern "C" {
167
#endif
17-
18-
19-
#ifdef DEBUG
20-
extern void OSReport(const char *fmt, ...) WUMS_FORMAT_PRINTF(1, 2);
21-
#endif
22-
238
extern const char wums_meta_info_dump[];
249
#ifdef __cplusplus
2510
}
@@ -31,4 +16,4 @@ extern const char wums_meta_info_dump[];
3116
#define WUMS_DEBUG_REPORT(fmt, ...)
3217
#endif
3318

34-
#define WUMS_DEBUG_WARN(fmt, ...) OSReport("[%s] " fmt, wums_meta_info_dump, ##__VA_ARGS__)
19+
#define WUMS_DEBUG_WARN(fmt, ...) OSReport("\033[33m[%s] " fmt "\033[0m", wums_meta_info_dump, ##__VA_ARGS__)

libraries/libwums/crt.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
#include "wums/wums_debug.h"
12
#include "wums_reent.h"
2-
#include "wums_thread_specific.h"
3+
34
#include <cstdio>
45
#include <cstring>
56

7+
extern "C" void OSFatal(const char *);
68
extern "C" void OSFatal(const char *);
79

810
int main(int argc, char **argv) {
@@ -43,16 +45,22 @@ struct _reent *__getreent(void) {
4345
return __wums_getreent();
4446
}
4547

46-
extern "C" void __attribute__((weak)) wut_set_thread_specific(__wut_thread_specific_id id, void *value);
47-
48-
void wut_set_thread_specific(__wut_thread_specific_id id, void *value) {
49-
return wums_set_thread_specific(id, value);
50-
}
48+
typedef enum __wut_thread_specific_id {
49+
WUT_THREAD_SPECIFIC_0 = 0,
50+
WUT_THREAD_SPECIFIC_1 = 1,
51+
} __wut_thread_specific_id;
5152

5253
extern "C" void *__attribute__((weak)) wut_get_thread_specific(__wut_thread_specific_id id);
5354

5455
void *wut_get_thread_specific(__wut_thread_specific_id id) {
55-
return wums_get_thread_specific(id);
56+
if ((uint32_t) id == 0x13371337) { // Mechanism to detect if the function was overridden properly
57+
return (void *) 0x42424242;
58+
}
59+
60+
WUMS_DEBUG_WARN("wums_get_thread_specific: NOT SUPPORTED\n");
61+
OSFatal("wums_get_thread_specific: NOT SUPPORTED\n");
62+
63+
return nullptr;
5664
}
5765

5866
extern "C" const char wums_meta_module_name[];
@@ -100,6 +108,8 @@ __assert_func(const char *file,
100108
}
101109

102110
OSFatal(buffer);
111+
while (true)
112+
;
103113
/* NOTREACHED */
104114
}
105115

0 commit comments

Comments
 (0)