Skip to content

Commit 87c2aa0

Browse files
authored
Use MUSL's weak* feature in bottom half (#306)
This change extracts the `weak*`-related parts of #303 as a separate PR. Note that this is slightly strange in that it uses some top-half MUSL headers in the bottom-half code, but discussion around this led me to believe that the advantages of, e.g., `LOCK` made this worthwhile. Beyond just changing uses of `weak` to `__weak__`, we also MUSL's `weak` and `weak_alias` macros in a few more places.
1 parent 60f221a commit 87c2aa0

8 files changed

Lines changed: 12 additions & 12 deletions

File tree

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,9 @@ $(DLMALLOC_OBJS): CFLAGS += \
428428
startup_files $(LIBC_BOTTOM_HALF_ALL_OBJS): CFLAGS += \
429429
-I$(LIBC_BOTTOM_HALF_HEADERS_PRIVATE) \
430430
-I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC_INC) \
431-
-I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC)
431+
-I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC) \
432+
-I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/include \
433+
-I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal
432434

433435
$(LIBC_TOP_HALF_ALL_OBJS) $(MUSL_PRINTSCAN_LONG_DOUBLE_OBJS) $(MUSL_PRINTSCAN_NO_FLOATING_POINT_OBJS) $(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS): CFLAGS += \
434436
-I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/include \

libc-bottom-half/cloudlibc/src/libc/time/clock_gettime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ int __clock_gettime(clockid_t clock_id, struct timespec *tp) {
1919
*tp = timestamp_to_timespec(ts);
2020
return 0;
2121
}
22-
extern __typeof(__clock_gettime) clock_gettime __attribute__((weak, alias("__clock_gettime")));
22+
weak_alias(__clock_gettime, clock_gettime);

libc-bottom-half/cloudlibc/src/libc/unistd/lseek.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ off_t __lseek(int fildes, off_t offset, int whence) {
2222
return new_offset;
2323
}
2424

25-
extern __typeof(__lseek) lseek __attribute__((weak, alias("__lseek")));
25+
weak_alias(__lseek, lseek);

libc-bottom-half/headers/public/wasi/libc-find-relpath.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ int __wasilibc_find_relpath_alloc(
7070
char **relative,
7171
size_t *relative_len,
7272
int can_realloc
73-
) __attribute__((weak));
73+
) __attribute__((__weak__));
7474

7575
#ifdef __cplusplus
7676
}

libc-bottom-half/sources/__main_void.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ int __main_argc_argv(int argc, char *argv[]);
88
// If the user's `main` function expects arguments, the compiler will rename
99
// it to `__main_argc_argv`, and this version will get linked in, which
1010
// initializes the argument data and calls `__main_argc_argv`.
11-
__attribute__((weak, nodebug))
11+
__attribute__((__weak__, nodebug))
1212
int __main_void(void) {
1313
__wasi_errno_t err;
1414

libc-bottom-half/sources/__wasilibc_initialize_environ.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/// Statically-initialize it to an invalid pointer value so that we can
1212
/// detect if it's been explicitly initialized (we can't use `NULL` because
1313
/// `clearenv` sets it to NULL.
14-
char **__wasilibc_environ __attribute__((weak)) = (char **)-1;
14+
char **__wasilibc_environ weak = (char **)-1;
1515

1616
// See the comments in libc-environ.h.
1717
void __wasilibc_ensure_environ(void) {
@@ -87,7 +87,7 @@ void __wasilibc_deinitialize_environ(void) {
8787
}
8888

8989
// See the comments in libc-environ.h.
90-
__attribute__((weak))
90+
weak
9191
void __wasilibc_maybe_reinitialize_environ_eagerly(void) {
9292
// This version does nothing. It may be overridden by a version which does
9393
// something if `environ` is used.

libc-bottom-half/sources/environ.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99
// `__wasilibc_environ`, which is initialized with a constructor function, so
1010
// that it's initialized whenever user code might want to access it.
1111
char **__wasilibc_environ;
12-
extern __typeof(__wasilibc_environ) _environ
13-
__attribute__((weak, alias("__wasilibc_environ")));
14-
extern __typeof(__wasilibc_environ) environ
15-
__attribute__((weak, alias("__wasilibc_environ")));
12+
weak_alias(__wasilibc_environ, _environ);
13+
weak_alias(__wasilibc_environ, environ);
1614

1715
// We define this function here in the same source file as
1816
// `__wasilibc_environ`, so that this function is called in iff environment

libc-bottom-half/sources/getentropy.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ int __getentropy(void *buffer, size_t len) {
2121

2222
return 0;
2323
}
24-
extern __typeof(__getentropy) getentropy __attribute__((weak, alias("__getentropy")));
24+
weak_alias(__getentropy, getentropy);

0 commit comments

Comments
 (0)