Skip to content

Commit a1bff1e

Browse files
linusgnico
authored andcommitted
LibC: Improve symbol visibility and naming consistency
This is largely inspired by ziglang/zig#23879.
1 parent 6244c66 commit a1bff1e

File tree

12 files changed

+40
-41
lines changed

12 files changed

+40
-41
lines changed

AK/NoAllocationGuard.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class NoAllocationGuard {
4141
return Processor::current_thread()->get_allocation_enabled();
4242
#elif defined(AK_OS_SERENITY)
4343
// This extern thread-local lives in our LibC, which doesn't exist on other systems.
44-
return s_allocation_enabled;
44+
return __allocation_enabled;
4545
#else
4646
return true;
4747
#endif
@@ -52,7 +52,7 @@ class NoAllocationGuard {
5252
#if defined(KERNEL)
5353
Processor::current_thread()->set_allocation_enabled(value);
5454
#elif defined(AK_OS_SERENITY)
55-
s_allocation_enabled = value;
55+
__allocation_enabled = value;
5656
#else
5757
(void)value;
5858
#endif

Userland/Libraries/LibC/assert.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
extern "C" {
1818

19-
extern bool __stdio_is_initialized;
20-
2119
void __assertion_failed(char const* msg)
2220
{
2321
if (__heap_is_stable) {

Userland/Libraries/LibC/bits/pthread_cancel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ inline void __pthread_maybe_cancel(void)
1717
{
1818
}
1919
#else
20-
void __pthread_maybe_cancel(void);
20+
__attribute__((visibility("hidden"))) void __pthread_maybe_cancel(void);
2121
#endif
2222

2323
__END_DECLS

Userland/Libraries/LibC/bits/pthread_integration.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@
1111

1212
__BEGIN_DECLS
1313

14-
void __pthread_fork_prepare(void);
15-
void __pthread_fork_child(void);
16-
void __pthread_fork_parent(void);
17-
void __pthread_fork_atfork_register_prepare(void (*)(void));
18-
void __pthread_fork_atfork_register_parent(void (*)(void));
19-
void __pthread_fork_atfork_register_child(void (*)(void));
14+
__attribute__((visibility("hidden"))) void __pthread_fork_prepare(void);
15+
__attribute__((visibility("hidden"))) void __pthread_fork_child(void);
16+
__attribute__((visibility("hidden"))) void __pthread_fork_parent(void);
17+
__attribute__((visibility("hidden"))) void __pthread_fork_atfork_register_prepare(void (*)(void));
18+
__attribute__((visibility("hidden"))) void __pthread_fork_atfork_register_parent(void (*)(void));
19+
__attribute__((visibility("hidden"))) void __pthread_fork_atfork_register_child(void (*)(void));
2020

21-
int __pthread_mutex_lock_pessimistic_np(pthread_mutex_t*);
21+
__attribute__((visibility("hidden"))) int __pthread_mutex_lock_pessimistic_np(pthread_mutex_t*);
2222

2323
typedef void (*KeyDestructor)(void*);
2424

25-
void __pthread_key_destroy_for_current_thread(void);
25+
__attribute__((visibility("hidden"))) void __pthread_key_destroy_for_current_thread(void);
2626

2727
#define __PTHREAD_MUTEX_NORMAL 0
2828
#define __PTHREAD_MUTEX_RECURSIVE 1

Userland/Libraries/LibC/bits/utimens.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313

1414
__BEGIN_DECLS
1515

16-
int __utimens(int fd, char const* path, struct timespec const times[2], int flag);
16+
__attribute__((visibility("hidden"))) int __utimens(int fd, char const* path, struct timespec const times[2], int flag);
1717

1818
__END_DECLS

Userland/Libraries/LibC/dlfcn.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
[[gnu::weak]] Result<void, DlErrorMessage> __dladdr(void const*, Dl_info*) asm("__dladdr");
1818

1919
// FIXME: use thread_local and a String once TLS works
20-
__thread char* s_dlerror_text = NULL;
21-
__thread bool s_dlerror_retrieved = false;
20+
static __thread char* s_dlerror_text = NULL;
21+
static __thread bool s_dlerror_retrieved = false;
2222

2323
static void store_error(ByteString const& error)
2424
{

Userland/Libraries/LibC/libcinit.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
extern "C" {
1717

1818
#ifdef NO_TLS
19-
int errno_storage;
19+
static int s_errno_storage;
2020
#else
21-
__thread int errno_storage;
21+
static __thread int s_errno_storage;
2222
#endif
2323
bool __environ_is_malloced = false;
2424
bool __stdio_is_initialized = false;
@@ -31,7 +31,7 @@ uintptr_t __stack_chk_guard;
3131

3232
int* __errno_location()
3333
{
34-
return &errno_storage;
34+
return &s_errno_storage;
3535
}
3636

3737
void __libc_init()

Userland/Libraries/LibC/malloc.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,13 @@ enum class CallerWillInitializeMemory {
249249
};
250250

251251
#ifndef NO_TLS
252-
__thread bool s_allocation_enabled = true;
252+
__thread bool __allocation_enabled = true;
253253
#endif
254254

255255
static ErrorOr<void*> malloc_impl(size_t size, size_t align, CallerWillInitializeMemory caller_will_initialize_memory)
256256
{
257257
#ifndef NO_TLS
258-
VERIFY(s_allocation_enabled);
258+
VERIFY(__allocation_enabled);
259259
#endif
260260

261261
// Align must be a power of 2.
@@ -397,7 +397,7 @@ static ErrorOr<void*> malloc_impl(size_t size, size_t align, CallerWillInitializ
397397
static void free_impl(void* ptr)
398398
{
399399
#ifndef NO_TLS
400-
VERIFY(s_allocation_enabled);
400+
VERIFY(__allocation_enabled);
401401
#endif
402402

403403
ScopedValueRollback rollback(errno);

Userland/Libraries/LibC/mallocdefs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static constexpr size_t num_size_classes = (sizeof(size_classes) / sizeof(unsign
2121

2222
#ifndef NO_TLS
2323
extern "C" {
24-
extern __thread bool s_allocation_enabled;
24+
extern __thread bool __allocation_enabled;
2525
}
2626
#endif
2727

Userland/Libraries/LibC/pthread.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ using PthreadAttrImpl = Syscall::SC_create_thread_params;
3535
static constexpr size_t required_stack_alignment = 4 * MiB;
3636
static constexpr size_t highest_reasonable_guard_size = 32 * PAGE_SIZE;
3737

38-
__thread void* s_stack_location;
39-
__thread size_t s_stack_size;
38+
static __thread void* s_stack_location;
39+
static __thread size_t s_stack_size;
4040

41-
__thread int s_thread_cancel_state = PTHREAD_CANCEL_ENABLE;
42-
__thread int s_thread_cancel_type = PTHREAD_CANCEL_DEFERRED;
41+
static __thread int s_thread_cancel_state = PTHREAD_CANCEL_ENABLE;
42+
static __thread int s_thread_cancel_type = PTHREAD_CANCEL_DEFERRED;
4343

4444
#define __RETURN_PTHREAD_ERROR(rc) \
4545
return ((rc) < 0 ? -(rc) : 0)
@@ -49,9 +49,9 @@ struct CleanupHandler {
4949
void* argument;
5050
};
5151

52-
static thread_local SinglyLinkedList<CleanupHandler> cleanup_handlers;
52+
static thread_local SinglyLinkedList<CleanupHandler> s_cleanup_handlers;
5353

54-
static __thread bool pending_cancellation = false;
54+
static __thread bool s_pending_cancellation = false;
5555

5656
[[gnu::weak]] extern ErrorOr<FlatPtr> __create_new_tls_region() asm("__create_new_tls_region");
5757
[[gnu::weak]] extern ErrorOr<void> __free_tls_region(FlatPtr thread_pointer) asm("__free_tls_region");
@@ -166,8 +166,8 @@ int pthread_create(pthread_t* thread, pthread_attr_t const* attributes, void* (*
166166
// https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_exit.html
167167
void pthread_exit(void* value_ptr)
168168
{
169-
while (!cleanup_handlers.is_empty()) {
170-
auto handler = cleanup_handlers.take_first();
169+
while (!s_cleanup_handlers.is_empty()) {
170+
auto handler = s_cleanup_handlers.take_first();
171171
handler.routine(handler.argument);
172172
}
173173

@@ -182,7 +182,7 @@ void __pthread_maybe_cancel()
182182
return;
183183

184184
// Check if a cancellation request is pending.
185-
if (!pending_cancellation)
185+
if (!s_pending_cancellation)
186186
return;
187187

188188
// Exit the thread via `pthread_exit`. This handles passing the
@@ -194,15 +194,15 @@ void __pthread_maybe_cancel()
194194
// https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_cleanup_push.html
195195
void pthread_cleanup_push(void (*routine)(void*), void* arg)
196196
{
197-
cleanup_handlers.prepend({ routine, arg });
197+
s_cleanup_handlers.prepend({ routine, arg });
198198
}
199199

200200
// https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_cleanup_pop.html
201201
void pthread_cleanup_pop(int execute)
202202
{
203-
VERIFY(!cleanup_handlers.is_empty());
203+
VERIFY(!s_cleanup_handlers.is_empty());
204204

205-
auto handler = cleanup_handlers.take_first();
205+
auto handler = s_cleanup_handlers.take_first();
206206

207207
if (execute)
208208
handler.routine(handler.argument);
@@ -545,7 +545,7 @@ static void pthread_cancel_signal_handler(int signal)
545545
// Note: We don't handle PTHREAD_CANCEL_ASYNCHRONOUS any different from PTHREAD_CANCEL_DEFERRED,
546546
// since ASYNCHRONOUS just means that the thread can be cancelled at any time (instead of just
547547
// at the next cancellation point) and it seems to be generally discouraged to use it at all.
548-
pending_cancellation = true;
548+
s_pending_cancellation = true;
549549
}
550550

551551
// https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_cancel.html

0 commit comments

Comments
 (0)