diff --git a/system/lib/libc/README.md b/system/lib/libc/README.md index e970933eb771a..8b4b6f8125186 100644 --- a/system/lib/libc/README.md +++ b/system/lib/libc/README.md @@ -7,8 +7,7 @@ and use a script (`system/lib/update_musl.py`) to pull in updates. Some changes have been made to the version that was taken from upstream, including: - * Emscripten-specific changes (from before this readme existed). These should be marked with `XXX EMSCRIPTEN` in the source, or ifdefed with `#if __EMSCRIPTEN__`. They are mostly in pthreads code and hopefully temporary. - * Backporting an operator-precedence warning fix from 6e76e1540fc58a418494bf5eb832b556f9c5763e in the upstream version. + * Emscripten-specific changes (from before this readme existed). These should be marked with `XXX EMSCRIPTEN` in the source, or ifdefed with `#ifdef __EMSCRIPTEN__`. They are mostly in pthreads code and hopefully temporary. * Switch to using the wasi `fd_write` syscall instead of `writev`. * Simplify stdout stream handling: do not support seeking, terminal handling, etc., as it just increases code size and Emscripten doesn't have those features anyhow. * Setting `_POSIX_REALTIME_SIGNALS` and `_POSIX_SPAWN` macros to -1, to exclude unsupported functions. diff --git a/system/lib/libc/musl/arch/emscripten/bits/fenv.h b/system/lib/libc/musl/arch/emscripten/bits/fenv.h index d3512cb6f7f53..96b60ee219548 100644 --- a/system/lib/libc/musl/arch/emscripten/bits/fenv.h +++ b/system/lib/libc/musl/arch/emscripten/bits/fenv.h @@ -6,22 +6,6 @@ #define FE_TOWARDZERO 0xc00 typedef unsigned short fexcept_t; - -typedef struct { - unsigned short __control_word; - unsigned short __unused1; - unsigned short __status_word; - unsigned short __unused2; - unsigned short __tags; - unsigned short __unused3; - unsigned int __eip; - unsigned short __cs_selector; - unsigned int __opcode:11; - unsigned int __unused4:5; - unsigned int __data_offset; - unsigned short __data_selector; - unsigned short __unused5; - unsigned int __mxcsr; -} fenv_t; +typedef unsigned short fenv_t; #define FE_DFL_ENV ((const fenv_t *) -1) diff --git a/system/lib/libc/musl/arch/emscripten/bits/limits.h b/system/lib/libc/musl/arch/emscripten/bits/limits.h index 7a09fdc1d175d..b36e964152ea1 100644 --- a/system/lib/libc/musl/arch/emscripten/bits/limits.h +++ b/system/lib/libc/musl/arch/emscripten/bits/limits.h @@ -1 +1,3 @@ -#define PAGE_SIZE 65536 +// A value used historically in Emscripten, and which we don't have a strong +// reason to change so far. +#define PAGESIZE 65536 diff --git a/system/lib/libc/musl/arch/emscripten/bits/mman.h b/system/lib/libc/musl/arch/emscripten/bits/mman.h new file mode 100644 index 0000000000000..c52e718d0623f --- /dev/null +++ b/system/lib/libc/musl/arch/emscripten/bits/mman.h @@ -0,0 +1,4 @@ +// XXX Emscripten in sync with both: +// - musl/arch/x86_64/bits/mman.h +// - musl/arch/i386/bits/mman.h +#define MAP_32BIT 0x40 diff --git a/system/lib/libc/musl/arch/emscripten/bits/posix.h b/system/lib/libc/musl/arch/emscripten/bits/posix.h index 30a38714f36dd..0dd953c3223f5 100644 --- a/system/lib/libc/musl/arch/emscripten/bits/posix.h +++ b/system/lib/libc/musl/arch/emscripten/bits/posix.h @@ -1,2 +1,9 @@ +#ifdef __wasm64__ +// 64-bit wide pointers under wasm64 +#define _POSIX_V6_LP64_OFF64 1 +#define _POSIX_V7_LP64_OFF64 1 +#else +// 32-bit wide pointers under wasm32 #define _POSIX_V6_ILP32_OFFBIG 1 #define _POSIX_V7_ILP32_OFFBIG 1 +#endif diff --git a/system/lib/libc/musl/arch/emscripten/bits/setjmp.h b/system/lib/libc/musl/arch/emscripten/bits/setjmp.h index decd26dca07a0..1c9aa40ed60c4 100644 --- a/system/lib/libc/musl/arch/emscripten/bits/setjmp.h +++ b/system/lib/libc/musl/arch/emscripten/bits/setjmp.h @@ -1 +1,2 @@ +// XXX Emscripten in sync with musl/arch/i386/bits/setjmp.h typedef unsigned long __jmp_buf[6]; diff --git a/system/lib/libc/musl/arch/emscripten/bits/signal.h b/system/lib/libc/musl/arch/emscripten/bits/signal.h index 58bc5e2c3a780..0c989dfdb9ef9 100644 --- a/system/lib/libc/musl/arch/emscripten/bits/signal.h +++ b/system/lib/libc/musl/arch/emscripten/bits/signal.h @@ -1,3 +1,4 @@ +// XXX Emscripten in sync with musl/arch/i386/bits/signal.h #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \ || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE) @@ -7,25 +8,44 @@ #endif #ifdef _GNU_SOURCE -#define REG_GS 0 -#define REG_FS 1 -#define REG_ES 2 -#define REG_DS 3 -#define REG_EDI 4 -#define REG_ESI 5 -#define REG_EBP 6 -#define REG_ESP 7 -#define REG_EBX 8 -#define REG_EDX 9 -#define REG_ECX 10 -#define REG_EAX 11 -#define REG_TRAPNO 12 -#define REG_ERR 13 -#define REG_EIP 14 -#define REG_CS 15 -#define REG_EFL 16 -#define REG_UESP 17 -#define REG_SS 18 +enum { REG_GS = 0 }; +#define REG_GS REG_GS +enum { REG_FS = 1 }; +#define REG_FS REG_FS +enum { REG_ES = 2 }; +#define REG_ES REG_ES +enum { REG_DS = 3 }; +#define REG_DS REG_DS +enum { REG_EDI = 4 }; +#define REG_EDI REG_EDI +enum { REG_ESI = 5 }; +#define REG_ESI REG_ESI +enum { REG_EBP = 6 }; +#define REG_EBP REG_EBP +enum { REG_ESP = 7 }; +#define REG_ESP REG_ESP +enum { REG_EBX = 8 }; +#define REG_EBX REG_EBX +enum { REG_EDX = 9 }; +#define REG_EDX REG_EDX +enum { REG_ECX = 10 }; +#define REG_ECX REG_ECX +enum { REG_EAX = 11 }; +#define REG_EAX REG_EAX +enum { REG_TRAPNO = 12 }; +#define REG_TRAPNO REG_TRAPNO +enum { REG_ERR = 13 }; +#define REG_ERR REG_ERR +enum { REG_EIP = 14 }; +#define REG_EIP REG_EIP +enum { REG_CS = 15 }; +#define REG_CS REG_CS +enum { REG_EFL = 16 }; +#define REG_EFL REG_EFL +enum { REG_UESP = 17 }; +#define REG_UESP REG_UESP +enum { REG_SS = 18 }; +#define REG_SS REG_SS #endif struct sigaltstack { diff --git a/system/lib/libc/musl/arch/emscripten/bits/stat.h b/system/lib/libc/musl/arch/emscripten/bits/stat.h index bfa9a597bcf6d..d0259d1483089 100644 --- a/system/lib/libc/musl/arch/emscripten/bits/stat.h +++ b/system/lib/libc/musl/arch/emscripten/bits/stat.h @@ -1,8 +1,8 @@ +// XXX Emscripten in sync with musl/arch/i386/bits/stat.h except for the padding and 64-bit time_t redirections change. + /* copied from kernel definition, but with padding replaced * by the corresponding correctly-sized userspace types. */ - -struct stat -{ +struct stat { dev_t st_dev; #ifndef __EMSCRIPTEN__ int __st_dev_padding; @@ -19,6 +19,12 @@ struct stat off_t st_size; blksize_t st_blksize; blkcnt_t st_blocks; +#ifndef __EMSCRIPTEN__ // XXX Emscripten no need to activate the symbol redirections for 64-bit time_t. + struct { + long tv_sec; + long tv_nsec; + } __st_atim32, __st_mtim32, __st_ctim32; +#endif struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; diff --git a/system/lib/libc/musl/arch/emscripten/bits/user.h b/system/lib/libc/musl/arch/emscripten/bits/user.h index fa623621eeba3..e69de29bb2d1d 100644 --- a/system/lib/libc/musl/arch/emscripten/bits/user.h +++ b/system/lib/libc/musl/arch/emscripten/bits/user.h @@ -1,48 +0,0 @@ -#undef __WORDSIZE -#define __WORDSIZE 32 - -typedef struct user_fpregs_struct -{ - long cwd, swd, twd, fip, fcs, foo, fos, st_space[20]; -} elf_fpregset_t; - -typedef struct user_fpxregs_struct -{ - unsigned short cwd, swd, twd, fop; - long fip, fcs, foo, fos, mxcsr, reserved; - long st_space[32], xmm_space[32], padding[56]; -} elf_fpxregset_t; - -struct user_regs_struct -{ - long ebx, ecx, edx, esi, edi, ebp, eax, xds, xes, xfs, xgs; - long orig_eax, eip, xcs, eflags, esp, xss; -}; - -#define ELF_NGREG 17 -typedef unsigned long elf_greg_t, elf_gregset_t[ELF_NGREG]; - -struct user -{ - struct user_regs_struct regs; - int u_fpvalid; - struct user_fpregs_struct i387; - unsigned long u_tsize; - unsigned long u_dsize; - unsigned long u_ssize; - unsigned long start_code; - unsigned long start_stack; - long signal; - int reserved; - struct user_regs_struct *u_ar0; - struct user_fpregs_struct *u_fpstate; - unsigned long magic; - char u_comm[32]; - int u_debugreg[8]; -}; - -#define PAGE_MASK (~(PAGE_SIZE-1)) -#define NBPG PAGE_SIZE -#define UPAGES 1 -#define HOST_TEXT_START_ADDR (u.start_code) -#define HOST_STACK_END_ADDR (u.start_stack + u.u_ssize * NBPG) diff --git a/system/lib/libc/musl/arch/generic/bits/ipc.h b/system/lib/libc/musl/arch/generic/bits/ipc.h index 40d6f3a2587cf..779c42fd7bf52 100644 --- a/system/lib/libc/musl/arch/generic/bits/ipc.h +++ b/system/lib/libc/musl/arch/generic/bits/ipc.h @@ -9,3 +9,5 @@ struct ipc_perm { long __pad1; long __pad2; }; + +#define IPC_64 0x100 diff --git a/system/lib/libc/musl/include/setjmp.h b/system/lib/libc/musl/include/setjmp.h index af72c80cdbce0..5aabfa95c4a05 100644 --- a/system/lib/libc/musl/include/setjmp.h +++ b/system/lib/libc/musl/include/setjmp.h @@ -26,7 +26,7 @@ typedef struct __jmp_buf_tag { || defined(_BSD_SOURCE) typedef jmp_buf sigjmp_buf; /* XXX EMSCRIPTEN: No signals support, alias sigsetjmp and siglongjmp to their non-signals counterparts. */ -#if __EMSCRIPTEN__ && !defined(LLVM_LIBC) +#if defined(__EMSCRIPTEN__) && !defined(LLVM_LIBC) #define sigsetjmp(buf, x) setjmp((buf)) #define siglongjmp(buf, val) longjmp(buf, val) #else diff --git a/system/lib/libc/musl/include/unistd.h b/system/lib/libc/musl/include/unistd.h index d98a7460db4d3..d194bf1243c46 100644 --- a/system/lib/libc/musl/include/unistd.h +++ b/system/lib/libc/musl/include/unistd.h @@ -240,20 +240,20 @@ pid_t gettid(void); #define _POSIX_NO_TRUNC 1 #define _POSIX_RAW_SOCKETS _POSIX_VERSION -#ifndef __EMSCRIPTEN__ -#define _POSIX_REALTIME_SIGNALS _POSIX_VERSION -#else +#ifdef __EMSCRIPTEN__ #define _POSIX_REALTIME_SIGNALS -1 +#else +#define _POSIX_REALTIME_SIGNALS _POSIX_VERSION #endif #define _POSIX_REGEXP 1 #define _POSIX_SAVED_IDS 1 #define _POSIX_SHELL 1 -#ifndef __EMSCRIPTEN__ -#define _POSIX_SPAWN _POSIX_VERSION -#else +#ifdef __EMSCRIPTEN__ #define _POSIX_SPAWN -1 +#else +#define _POSIX_SPAWN _POSIX_VERSION #endif #define _POSIX_VDISABLE 0 @@ -263,10 +263,10 @@ pid_t gettid(void); #else #define _POSIX_THREADS _POSIX_VERSION #endif -#ifndef __EMSCRIPTEN__ -#define _POSIX_THREAD_PROCESS_SHARED _POSIX_VERSION -#else +#ifdef __EMSCRIPTEN__ #define _POSIX_THREAD_PROCESS_SHARED -1 +#else +#define _POSIX_THREAD_PROCESS_SHARED _POSIX_VERSION #endif #define _POSIX_THREAD_SAFE_FUNCTIONS _POSIX_VERSION #define _POSIX_THREAD_ATTR_STACKADDR _POSIX_VERSION diff --git a/system/lib/libc/musl/src/exit/abort.c b/system/lib/libc/musl/src/exit/abort.c index a6427d51d66e9..8ff8a8626ee07 100644 --- a/system/lib/libc/musl/src/exit/abort.c +++ b/system/lib/libc/musl/src/exit/abort.c @@ -6,13 +6,13 @@ #include "lock.h" #include "ksigaction.h" -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ #include "emscripten_internal.h" #endif _Noreturn void abort(void) { -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ /* In emscripten we call out to JS to perform the actual abort where it can * produce a nice error. * Note that the JS library function is not called `abort` to avoid conflict diff --git a/system/lib/libc/musl/src/internal/libm.h b/system/lib/libc/musl/src/internal/libm.h index a47208aa6ad3c..9d8b892d5b3e8 100644 --- a/system/lib/libc/musl/src/internal/libm.h +++ b/system/lib/libc/musl/src/internal/libm.h @@ -144,6 +144,16 @@ static inline long double fp_barrierl(long double x) } #endif +#ifdef __EMSCRIPTEN__ +/* + * wasm doesn't have user-accessible floating point exceptions, so there's + * no point in trying to force expression evaluations to produce them. + */ +#define fp_force_evalf(x) +#define fp_force_eval(x) +#define fp_force_evall(x) +#define FORCE_EVAL(x) +#else /* fp_force_eval ensures that the input value is computed when that's otherwise unused. To prevent the constant folding of the input expression, an additional fp_barrier may be needed or a compilation @@ -177,13 +187,6 @@ static inline void fp_force_evall(long double x) } #endif -#ifdef __EMSCRIPTEN__ -/* - * asm.js doesn't have user-accessible floating point exceptions, so there's - * no point in trying to force expression evaluations to produce them. - */ -#define FORCE_EVAL(x) -#else #define FORCE_EVAL(x) do { \ if (sizeof(x) == sizeof(float)) { \ fp_force_evalf(x); \ diff --git a/system/lib/libc/musl/src/internal/locale_impl.h b/system/lib/libc/musl/src/internal/locale_impl.h index 9a7b07c4aac0e..1f46f88fa7d74 100644 --- a/system/lib/libc/musl/src/internal/locale_impl.h +++ b/system/lib/libc/musl/src/internal/locale_impl.h @@ -31,7 +31,7 @@ hidden char *__gettextdomain(void); #define LOC_MAP_FAILED ((const struct __locale_map *)-1) -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ // Disable message translation completely under emscripten since we don't // support loading any actual locale data, and even looking up the current // local via CURRENT_LOCALE via TLS is not free. diff --git a/system/lib/libc/musl/src/internal/pthread_impl.h b/system/lib/libc/musl/src/internal/pthread_impl.h index a366b6cef8eb3..d7978ba8fccf7 100644 --- a/system/lib/libc/musl/src/internal/pthread_impl.h +++ b/system/lib/libc/musl/src/internal/pthread_impl.h @@ -237,7 +237,7 @@ static inline void __wake(volatile void *addr, int cnt, int priv) if (priv) priv = FUTEX_PRIVATE; if (cnt<0) cnt = INT_MAX; #ifdef __EMSCRIPTEN__ - emscripten_futex_wake(addr, (cnt)<0?INT_MAX:(cnt)); + emscripten_futex_wake(addr, cnt); #else __syscall(SYS_futex, addr, FUTEX_WAKE|priv, cnt) != -ENOSYS || __syscall(SYS_futex, addr, FUTEX_WAKE, cnt); diff --git a/system/lib/libc/musl/src/internal/syscall.h b/system/lib/libc/musl/src/internal/syscall.h index 1a6c4313852d7..b4152b6145eff 100644 --- a/system/lib/libc/musl/src/internal/syscall.h +++ b/system/lib/libc/musl/src/internal/syscall.h @@ -30,7 +30,7 @@ typedef long syscall_arg_t; #endif -#ifdef __cplusplus +#ifdef __cplusplus // XXX Emscripten we need C linkage for this extern "C" { #endif hidden long __syscall_ret(unsigned long), @@ -40,14 +40,7 @@ hidden long __syscall_ret(unsigned long), } #endif -#ifndef __EMSCRIPTEN__ -#define __syscall1(n,a) __syscall1(n,__scc(a)) -#define __syscall2(n,a,b) __syscall2(n,__scc(a),__scc(b)) -#define __syscall3(n,a,b,c) __syscall3(n,__scc(a),__scc(b),__scc(c)) -#define __syscall4(n,a,b,c,d) __syscall4(n,__scc(a),__scc(b),__scc(c),__scc(d)) -#define __syscall5(n,a,b,c,d,e) __syscall5(n,__scc(a),__scc(b),__scc(c),__scc(d),__scc(e)) -#define __syscall6(n,a,b,c,d,e,f) __syscall6(n,__scc(a),__scc(b),__scc(c),__scc(d),__scc(e),__scc(f)) -#else // __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ #define __syscall_emscripten(n, ...) n(__VA_ARGS__) #define __syscall_emscripten0(n) __syscall_emscripten(n) #define __syscall_emscripten1(n,a) __syscall_emscripten(n,__scc(a)) @@ -56,7 +49,14 @@ hidden long __syscall_ret(unsigned long), #define __syscall_emscripten4(n,a,b,c,d) __syscall_emscripten(n,__scc(a),__scc(b),__scc(c),__scc(d)) #define __syscall_emscripten5(n,a,b,c,d,e) __syscall_emscripten(n,__scc(a),__scc(b),__scc(c),__scc(d),__scc(e)) #define __syscall_emscripten6(n,a,b,c,d,e,f) __syscall_emscripten(n,__scc(a),__scc(b),__scc(c),__scc(d),__scc(e),__scc(f)) -#endif // __EMSCRIPTEN__ +#else // !defined(__EMSCRIPTEN__) +#define __syscall1(n,a) __syscall1(n,__scc(a)) +#define __syscall2(n,a,b) __syscall2(n,__scc(a),__scc(b)) +#define __syscall3(n,a,b,c) __syscall3(n,__scc(a),__scc(b),__scc(c)) +#define __syscall4(n,a,b,c,d) __syscall4(n,__scc(a),__scc(b),__scc(c),__scc(d)) +#define __syscall5(n,a,b,c,d,e) __syscall5(n,__scc(a),__scc(b),__scc(c),__scc(d),__scc(e)) +#define __syscall6(n,a,b,c,d,e,f) __syscall6(n,__scc(a),__scc(b),__scc(c),__scc(d),__scc(e),__scc(f)) +#endif // !defined(__EMSCRIPTEN__) #define __SYSCALL_NARGS_X(a,b,c,d,e,f,g,h,n,...) n #define __SYSCALL_NARGS(...) __SYSCALL_NARGS_X(__VA_ARGS__,7,6,5,4,3,2,1,0,) @@ -64,10 +64,10 @@ hidden long __syscall_ret(unsigned long), #define __SYSCALL_CONCAT(a,b) __SYSCALL_CONCAT_X(a,b) #define __SYSCALL_DISP(b,...) __SYSCALL_CONCAT(b,__SYSCALL_NARGS(__VA_ARGS__))(__VA_ARGS__) -#ifndef __EMSCRIPTEN__ -#define __syscall(...) __SYSCALL_DISP(__syscall,__VA_ARGS__) -#else +#ifdef __EMSCRIPTEN__ #define __syscall(...) __SYSCALL_DISP(__syscall_emscripten,__VA_ARGS__) +#else +#define __syscall(...) __SYSCALL_DISP(__syscall,__VA_ARGS__) #endif #define syscall(...) __syscall_ret(__syscall(__VA_ARGS__)) @@ -75,7 +75,9 @@ hidden long __syscall_ret(unsigned long), #define socketcall(nm,a,b,c,d,e,f) __syscall_ret(__socketcall(nm,a,b,c,d,e,f)) #define socketcall_cp(nm,a,b,c,d,e,f) __syscall_ret(__socketcall_cp(nm,a,b,c,d,e,f)) -#ifndef __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ +#define __syscall_cp(...) __syscall(__VA_ARGS__) +#else // !defined(__EMSCRIPTEN__) #define __syscall_cp0(n) (__syscall_cp)(n,0,0,0,0,0,0) #define __syscall_cp1(n,a) (__syscall_cp)(n,__scc(a),0,0,0,0,0) #define __syscall_cp2(n,a,b) (__syscall_cp)(n,__scc(a),__scc(b),0,0,0,0) @@ -85,9 +87,7 @@ hidden long __syscall_ret(unsigned long), #define __syscall_cp6(n,a,b,c,d,e,f) (__syscall_cp)(n,__scc(a),__scc(b),__scc(c),__scc(d),__scc(e),__scc(f)) #define __syscall_cp(...) __SYSCALL_DISP(__syscall_cp,__VA_ARGS__) -#else // __EMSCRIPTEN__ -#define __syscall_cp(...) __syscall(__VA_ARGS__) -#endif // __EMSCRIPTEN__ +#endif // !defined(__EMSCRIPTEN__) #define syscall_cp(...) __syscall_ret(__syscall_cp(__VA_ARGS__)) @@ -408,7 +408,12 @@ hidden long __syscall_ret(unsigned long), #define SIOCGSTAMPNS_OLD 0x8907 #endif -#ifndef __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ +#define __sys_open2(x,pn,fl) __syscall_openat(__scc(AT_FDCWD), __scc(pn), __scc((fl)|O_LARGEFILE)) +#define __sys_open3(x,pn,fl,mo) __syscall_openat(__scc(AT_FDCWD), __scc(pn), __scc((fl)|O_LARGEFILE), __scc(mo)) +#define __sys_open_cp2(x,pn,fl) __syscall_openat(__scc(AT_FDCWD), __scc(pn), __scc((fl)|O_LARGEFILE)) +#define __sys_open_cp3(x,pn,fl,mo) __syscall_openat(__scc(AT_FDCWD), __scc(pn), __scc((fl)|O_LARGEFILE), __scc(mo)) +#else // !defined(__EMSCRIPTEN__) #ifdef SYS_open #define __sys_open2(x,pn,fl) __syscall2(SYS_open, pn, (fl)|O_LARGEFILE) #define __sys_open3(x,pn,fl,mo) __syscall3(SYS_open, pn, (fl)|O_LARGEFILE, mo) @@ -420,13 +425,7 @@ hidden long __syscall_ret(unsigned long), #define __sys_open_cp2(x,pn,fl) __syscall_cp3(SYS_openat, AT_FDCWD, pn, (fl)|O_LARGEFILE) #define __sys_open_cp3(x,pn,fl,mo) __syscall_cp4(SYS_openat, AT_FDCWD, pn, (fl)|O_LARGEFILE, mo) #endif -#else // __EMSCRIPTEN__ -#define __sys_open2(x,pn,fl) __syscall_openat(__scc(AT_FDCWD), __scc(pn), __scc((fl)|O_LARGEFILE)) -#define __sys_open3(x,pn,fl,mo) __syscall_openat(__scc(AT_FDCWD), __scc(pn), __scc((fl)|O_LARGEFILE), __scc(mo)) -#define __sys_open_cp2(x,pn,fl) __syscall_openat(__scc(AT_FDCWD), __scc(pn), __scc((fl)|O_LARGEFILE)) -#define __sys_open_cp3(x,pn,fl,mo) __syscall_openat(__scc(AT_FDCWD), __scc(pn), __scc((fl)|O_LARGEFILE), __scc(mo)) -#endif - +#endif // !defined(__EMSCRIPTEN__) #define __sys_open(...) __SYSCALL_DISP(__sys_open,,__VA_ARGS__) #define sys_open(...) __syscall_ret(__sys_open(__VA_ARGS__)) @@ -445,7 +444,7 @@ hidden long __emulate_wait4(int, int *, int, void *, int); #define sys_wait4(a,b,c,d) __syscall_ret(__sys_wait4(a,b,c,d)) #define sys_wait4_cp(a,b,c,d) __syscall_ret(__sys_wait4_cp(a,b,c,d)) -#ifdef __cplusplus +#ifdef __cplusplus // XXX Emscripten static array size is a C99 feature, not permitted in C++ hidden void __procfdname(char __buf[], unsigned); #else hidden void __procfdname(char __buf[static 15+3*sizeof(int)], unsigned); diff --git a/system/lib/libc/musl/src/linux/sbrk.c b/system/lib/libc/musl/src/linux/sbrk.c index 8e3a9b6645d68..cf483a57fb329 100644 --- a/system/lib/libc/musl/src/linux/sbrk.c +++ b/system/lib/libc/musl/src/linux/sbrk.c @@ -1,4 +1,4 @@ -#if !__EMSCRIPTEN__ /* Emscripten controls sbrk itself */ +#ifndef __EMSCRIPTEN__ /* Emscripten controls sbrk itself */ #define _BSD_SOURCE #include #include @@ -11,4 +11,3 @@ void *sbrk(intptr_t inc) return (void *)__syscall(SYS_brk, 0); } #endif - diff --git a/system/lib/libc/musl/src/math/sqrt.c b/system/lib/libc/musl/src/math/sqrt.c index 6854c98381642..3b07abee8380a 100644 --- a/system/lib/libc/musl/src/math/sqrt.c +++ b/system/lib/libc/musl/src/math/sqrt.c @@ -27,7 +27,7 @@ double sqrt(double x) { // XXX EMSCRIPTEN: use the wasm instruction via clang builtin // See https://github.com/emscripten-core/emscripten/issues/9236 -#ifdef __wasm__ +#ifdef __EMSCRIPTEN__ return __builtin_sqrt(x); #else uint64_t ix, top, m; diff --git a/system/lib/libc/musl/src/math/sqrtf.c b/system/lib/libc/musl/src/math/sqrtf.c index fd74702691215..d1f3b9595ccd0 100644 --- a/system/lib/libc/musl/src/math/sqrtf.c +++ b/system/lib/libc/musl/src/math/sqrtf.c @@ -18,7 +18,7 @@ float sqrtf(float x) { // XXX EMSCRIPTEN: use the wasm instruction via clang builtin // See https://github.com/emscripten-core/emscripten/issues/9236 -#ifdef __wasm__ +#ifdef __EMSCRIPTEN__ return __builtin_sqrtf(x); #else uint32_t ix, m, m1, m0, even, ey; diff --git a/system/lib/libc/musl/src/network/freeaddrinfo.c b/system/lib/libc/musl/src/network/freeaddrinfo.c index c4016d9f7c246..68fdc09cb9e10 100644 --- a/system/lib/libc/musl/src/network/freeaddrinfo.c +++ b/system/lib/libc/musl/src/network/freeaddrinfo.c @@ -6,7 +6,7 @@ void freeaddrinfo(struct addrinfo *p) { -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ // Emscripten's usage of this structure is very simple: we always allocate // ai_addr, and do not use the linked list aspect at all. There is also no // aliasing with aibuf. diff --git a/system/lib/libc/musl/src/sched/sched_yield.c b/system/lib/libc/musl/src/sched/sched_yield.c index 7bd3846fc239e..82a14a3914e95 100644 --- a/system/lib/libc/musl/src/sched/sched_yield.c +++ b/system/lib/libc/musl/src/sched/sched_yield.c @@ -1,7 +1,7 @@ #include #include "syscall.h" -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ #include #include #include "threading_internal.h" @@ -9,7 +9,7 @@ int sched_yield() { -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ // SharedArrayBuffer and wasm threads do not support explicit yielding. // For now we at least call `emscripten_yield` which processes the event queue // (along with other essential tasks). diff --git a/system/lib/libc/musl/src/stat/fchmod.c b/system/lib/libc/musl/src/stat/fchmod.c index 58badd5c01bb8..ca2a6c783b2bd 100644 --- a/system/lib/libc/musl/src/stat/fchmod.c +++ b/system/lib/libc/musl/src/stat/fchmod.c @@ -9,7 +9,7 @@ int fchmod(int fd, mode_t mode) { int ret = __syscall(SYS_fchmod, fd, mode); -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ if (ret != -EBADF || !__wasi_fd_is_valid(fd)) return __syscall_ret(ret); #else diff --git a/system/lib/libc/musl/src/stat/fstatat.c b/system/lib/libc/musl/src/stat/fstatat.c index 631451268c590..6d499c44174a3 100644 --- a/system/lib/libc/musl/src/stat/fstatat.c +++ b/system/lib/libc/musl/src/stat/fstatat.c @@ -145,7 +145,7 @@ static int fstatat_kstat(int fd, const char *restrict path, struct stat *restric int __fstatat(int fd, const char *restrict path, struct stat *restrict st, int flag) { int ret; -#ifdef __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ // XXX Emscripten statx syscall unsupported // some logic here copied from fstatat_kstat above if (flag==AT_EMPTY_PATH && fd>=0 && !*path) ret = __syscall(SYS_fstat, fd, st); diff --git a/system/lib/libc/musl/src/stdio/__stdio_read.c b/system/lib/libc/musl/src/stdio/__stdio_read.c index 392a4858281f3..c7adefe079f01 100644 --- a/system/lib/libc/musl/src/stdio/__stdio_read.c +++ b/system/lib/libc/musl/src/stdio/__stdio_read.c @@ -8,8 +8,7 @@ size_t __stdio_read(FILE *f, unsigned char *buf, size_t len) { .iov_base = f->buf, .iov_len = f->buf_size } }; ssize_t cnt; - -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ size_t num; if (__wasi_syscall_ret(__wasi_fd_read(f->fd, (struct __wasi_iovec_t*)iov, 2, &num))) { num = -1; diff --git a/system/lib/libc/musl/src/stdio/__stdio_write.c b/system/lib/libc/musl/src/stdio/__stdio_write.c index cce9f6a16a558..097acd53b79cc 100644 --- a/system/lib/libc/musl/src/stdio/__stdio_write.c +++ b/system/lib/libc/musl/src/stdio/__stdio_write.c @@ -12,7 +12,7 @@ size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len) int iovcnt = 2; ssize_t cnt; for (;;) { -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ size_t num; if (__wasi_syscall_ret(__wasi_fd_write(f->fd, (struct __wasi_ciovec_t*)iov, iovcnt, &num))) { num = -1; diff --git a/system/lib/libc/musl/src/stdio/stdout.c b/system/lib/libc/musl/src/stdio/stdout.c index 62cf40220a5c4..5054e5b6ccc14 100644 --- a/system/lib/libc/musl/src/stdio/stdout.c +++ b/system/lib/libc/musl/src/stdio/stdout.c @@ -1,6 +1,6 @@ #include "stdio_impl.h" -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ // Emscripten doesn't support terminal seeking. static off_t __emscripten_stdout_seek(FILE *f, off_t off, int whence) { @@ -23,7 +23,7 @@ hidden FILE __stdout_FILE = { .fd = 1, .flags = F_PERM | F_NORD, .lbf = '\n', -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ // avoid stout_write which adds special terminal window size handling, which emscripten doesn't support anyhow .write = __stdio_write, .seek = __emscripten_stdout_seek, diff --git a/system/lib/libc/musl/src/stdio/vfprintf.c b/system/lib/libc/musl/src/stdio/vfprintf.c index c0542b599024c..ce1acf9140e48 100644 --- a/system/lib/libc/musl/src/stdio/vfprintf.c +++ b/system/lib/libc/musl/src/stdio/vfprintf.c @@ -147,7 +147,7 @@ typedef void (*pop_arg_long_double_t)(union arg *arg, va_list *ap); static void pop_arg_long_double(union arg *arg, va_list *ap) { - arg->f = va_arg(*ap, long double); + arg->f = va_arg(*ap, long double); } static void pop_arg(union arg *arg, int type, va_list *ap, pop_arg_long_double_t pop_arg_long_double) diff --git a/system/lib/libc/musl/src/string/memcmp.c b/system/lib/libc/musl/src/string/memcmp.c index c93cf8cc97330..ba9ab6d41d3c9 100644 --- a/system/lib/libc/musl/src/string/memcmp.c +++ b/system/lib/libc/musl/src/string/memcmp.c @@ -1,4 +1,4 @@ -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ #include #endif #include diff --git a/system/lib/libc/musl/src/thread/__timedwait.c b/system/lib/libc/musl/src/thread/__timedwait.c index 9e3ccc9849257..51afba0044942 100644 --- a/system/lib/libc/musl/src/thread/__timedwait.c +++ b/system/lib/libc/musl/src/thread/__timedwait.c @@ -32,10 +32,10 @@ static int __futex4_cp(volatile void *addr, int op, int val, const struct timesp if (r != -ENOSYS) return r; return __syscall_cp(SYS_futex, addr, op & ~FUTEX_PRIVATE, val, to); } -#endif static volatile int dummy = 0; weak_alias(dummy, __eintr_valid_flag); +#endif int __timedwait_cp(volatile int *addr, int val, clockid_t clk, const struct timespec *at, int priv) @@ -64,11 +64,13 @@ int __timedwait_cp(volatile int *addr, int val, r = -__futex4_cp(addr, FUTEX_WAIT|priv, val, top); #endif if (r != EINTR && r != ETIMEDOUT && r != ECANCELED) r = 0; +#ifndef __EMSCRIPTEN__ // XXX Emscripten revert musl commit a63c0104e496f7ba78b64be3cd299b41e8cd427f /* Mitigate bug in old kernels wrongly reporting EINTR for non- * interrupting (SA_RESTART) signal handlers. This is only practical * when NO interrupting signal handlers have been installed, and * works by sigaction tracking whether that's the case. */ if (r == EINTR && !__eintr_valid_flag) r = 0; +#endif return r; } diff --git a/system/lib/libc/musl/src/thread/pthread_mutex_timedlock.c b/system/lib/libc/musl/src/thread/pthread_mutex_timedlock.c index 232c9b321a8e2..95eefb45ea0a8 100644 --- a/system/lib/libc/musl/src/thread/pthread_mutex_timedlock.c +++ b/system/lib/libc/musl/src/thread/pthread_mutex_timedlock.c @@ -77,7 +77,7 @@ int __pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec #ifndef __EMSCRIPTEN__ if (type&8) return pthread_mutex_timedlock_pi(m, at); #endif - + int spins = 100; while (spins-- && m->_m_lock && !m->_m_waiters) a_spin(); diff --git a/system/lib/libc/musl/src/time/clock_gettime.c b/system/lib/libc/musl/src/time/clock_gettime.c index 18926de8ce8f3..36d901e85dff1 100644 --- a/system/lib/libc/musl/src/time/clock_gettime.c +++ b/system/lib/libc/musl/src/time/clock_gettime.c @@ -56,7 +56,7 @@ static void *volatile vdso_func = (void *)cgt_init; #endif -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ _Static_assert(CLOCK_REALTIME == __WASI_CLOCKID_REALTIME, "monotonic clock must match"); _Static_assert(CLOCK_MONOTONIC == __WASI_CLOCKID_MONOTONIC, "monotonic clock must match"); diff --git a/system/lib/libc/musl/src/time/clock_nanosleep.c b/system/lib/libc/musl/src/time/clock_nanosleep.c index 7ad2aba1a0b7a..3684e43aa14c6 100644 --- a/system/lib/libc/musl/src/time/clock_nanosleep.c +++ b/system/lib/libc/musl/src/time/clock_nanosleep.c @@ -1,7 +1,7 @@ #include #include #include "syscall.h" -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ #include #include #endif @@ -12,7 +12,7 @@ int __clock_nanosleep(clockid_t clk, int flags, const struct timespec *req, struct timespec *rem) { if (clk == CLOCK_THREAD_CPUTIME_ID) return EINVAL; -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ if (!req || req->tv_nsec < 0 || req->tv_nsec > 999999999L || req->tv_sec < 0) { return EINVAL; } diff --git a/system/lib/libc/musl/src/time/clock_settime.c b/system/lib/libc/musl/src/time/clock_settime.c index ed0182fff664c..686488414371a 100644 --- a/system/lib/libc/musl/src/time/clock_settime.c +++ b/system/lib/libc/musl/src/time/clock_settime.c @@ -1,9 +1,6 @@ #include #include #include "syscall.h" -#ifdef __EMSCRIPTEN__ -#include -#endif #define IS32BIT(x) !((x)+0x80000000ULL>>32) @@ -13,8 +10,7 @@ int clock_settime(clockid_t clk, const struct timespec *ts) // JS and wasm VMs do not allow setting the time. errno = EPERM; return -1; -#else -#ifdef SYS_clock_settime64 +#elif defined(SYS_clock_settime64) // XXX EMSCRIPTEN replace #ifdef SYS_clock_settime64 time_t s = ts->tv_sec; long ns = ts->tv_nsec; int r = -ENOSYS; @@ -29,5 +25,4 @@ int clock_settime(clockid_t clk, const struct timespec *ts) #else return syscall(SYS_clock_settime, clk, ts); #endif -#endif } diff --git a/system/lib/libc/musl/src/unistd/fchdir.c b/system/lib/libc/musl/src/unistd/fchdir.c index bcbd0fc9cc9cd..a22f5f4fed65d 100644 --- a/system/lib/libc/musl/src/unistd/fchdir.c +++ b/system/lib/libc/musl/src/unistd/fchdir.c @@ -9,7 +9,7 @@ int fchdir(int fd) { int ret = __syscall(SYS_fchdir, fd); -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ if (ret != -EBADF || !__wasi_fd_is_valid(fd)) return __syscall_ret(ret); #else diff --git a/system/lib/libc/musl/src/unistd/fchown.c b/system/lib/libc/musl/src/unistd/fchown.c index a328189505444..02facc410b5d1 100644 --- a/system/lib/libc/musl/src/unistd/fchown.c +++ b/system/lib/libc/musl/src/unistd/fchown.c @@ -9,7 +9,7 @@ int fchown(int fd, uid_t uid, gid_t gid) { int ret = __syscall(SYS_fchown, fd, uid, gid); -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ // We can't continue onwards to try the /proc/fd/NNN approach that musl does, // as we don't support that much of POSIX. return __syscall_ret(ret); @@ -24,5 +24,5 @@ int fchown(int fd, uid_t uid, gid_t gid) #else return syscall(SYS_fchownat, AT_FDCWD, buf, uid, gid, 0); #endif -#endif // EMSCRIPTEN +#endif // __EMSCRIPTEN__ } diff --git a/system/lib/libc/musl/src/unistd/fsync.c b/system/lib/libc/musl/src/unistd/fsync.c index 2b26be534e96c..8c6c1d13dcc5d 100644 --- a/system/lib/libc/musl/src/unistd/fsync.c +++ b/system/lib/libc/musl/src/unistd/fsync.c @@ -3,7 +3,7 @@ int fsync(int fd) { -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ return __wasi_syscall_ret(__wasi_fd_sync(fd)); #else return syscall_cp(SYS_fsync, fd); diff --git a/system/lib/libc/musl/src/unistd/pread.c b/system/lib/libc/musl/src/unistd/pread.c index a2361a0473b51..7b790d47d1abc 100644 --- a/system/lib/libc/musl/src/unistd/pread.c +++ b/system/lib/libc/musl/src/unistd/pread.c @@ -3,7 +3,7 @@ ssize_t pread(int fd, void *buf, size_t size, off_t ofs) { -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ __wasi_iovec_t iov = { .buf = buf, .buf_len = size diff --git a/system/lib/libc/musl/src/unistd/preadv.c b/system/lib/libc/musl/src/unistd/preadv.c index fa6b78c101b3f..5c86d09cf2c7d 100644 --- a/system/lib/libc/musl/src/unistd/preadv.c +++ b/system/lib/libc/musl/src/unistd/preadv.c @@ -5,7 +5,7 @@ ssize_t preadv(int fd, const struct iovec *iov, int count, off_t ofs) { -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ size_t num; if (__wasi_syscall_ret(__wasi_fd_pread(fd, (struct __wasi_iovec_t*)iov, count, ofs, &num))) { return -1; diff --git a/system/lib/libc/musl/src/unistd/pwrite.c b/system/lib/libc/musl/src/unistd/pwrite.c index 3573ab3902f82..671d7732abd1a 100644 --- a/system/lib/libc/musl/src/unistd/pwrite.c +++ b/system/lib/libc/musl/src/unistd/pwrite.c @@ -3,7 +3,7 @@ ssize_t pwrite(int fd, const void *buf, size_t size, off_t ofs) { -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ __wasi_ciovec_t iov = { .buf = buf, .buf_len = size diff --git a/system/lib/libc/musl/src/unistd/pwritev.c b/system/lib/libc/musl/src/unistd/pwritev.c index bb44236c51ef7..b2d9d7c541f23 100644 --- a/system/lib/libc/musl/src/unistd/pwritev.c +++ b/system/lib/libc/musl/src/unistd/pwritev.c @@ -5,7 +5,7 @@ ssize_t pwritev(int fd, const struct iovec *iov, int count, off_t ofs) { -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ size_t num; if (__wasi_syscall_ret(__wasi_fd_pwrite(fd, (struct __wasi_ciovec_t*)iov, count, ofs, &num))) { return -1; diff --git a/system/lib/libc/musl/src/unistd/read.c b/system/lib/libc/musl/src/unistd/read.c index f079439c9d302..f84e9436c7c8b 100644 --- a/system/lib/libc/musl/src/unistd/read.c +++ b/system/lib/libc/musl/src/unistd/read.c @@ -3,7 +3,7 @@ ssize_t read(int fd, void *buf, size_t count) { -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ __wasi_iovec_t iov = { .buf = buf, .buf_len = count diff --git a/system/lib/libc/musl/src/unistd/readv.c b/system/lib/libc/musl/src/unistd/readv.c index bb0760da6e147..3a40c1e9626f6 100644 --- a/system/lib/libc/musl/src/unistd/readv.c +++ b/system/lib/libc/musl/src/unistd/readv.c @@ -3,7 +3,7 @@ ssize_t readv(int fd, const struct iovec *iov, int count) { -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ size_t num; if (__wasi_syscall_ret(__wasi_fd_read(fd, (struct __wasi_iovec_t*)iov, count, &num))) { num = -1; diff --git a/system/lib/libc/musl/src/unistd/write.c b/system/lib/libc/musl/src/unistd/write.c index 53742cd4fd972..135229be89121 100644 --- a/system/lib/libc/musl/src/unistd/write.c +++ b/system/lib/libc/musl/src/unistd/write.c @@ -3,7 +3,7 @@ ssize_t write(int fd, const void *buf, size_t count) { -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ __wasi_ciovec_t iov = { .buf = buf, .buf_len = count diff --git a/system/lib/libc/musl/src/unistd/writev.c b/system/lib/libc/musl/src/unistd/writev.c index 443d14f35d1be..4e81f107f61fb 100644 --- a/system/lib/libc/musl/src/unistd/writev.c +++ b/system/lib/libc/musl/src/unistd/writev.c @@ -1,12 +1,12 @@ #include #include "syscall.h" -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ #include #endif ssize_t writev(int fd, const struct iovec *iov, int count) { -#if __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ size_t num; if (__wasi_syscall_ret(__wasi_fd_write(fd, (struct __wasi_ciovec_t*)iov, count, &num))) { return -1;