diff --git a/src/libc/epoll.c b/src/libc/epoll.c index 0845cf38a8..a7d4075f37 100644 --- a/src/libc/epoll.c +++ b/src/libc/epoll.c @@ -4,9 +4,23 @@ #include "libc-shim.h" +/* On 32-bit architectures built with _TIME_BITS=64, glibc renames epoll_pwait2() to + * __epoll_pwait2_time64 via __asm__() in so the linker picks the variant whose + * struct timespec ABI matches our 64-bit time_t. dlsym() can't see that header-level rename, so + * we have to spell out the right name here, otherwise we'd silently dispatch a 64-bit timespec to + * the legacy 32-bit-time_t entry point. */ +#ifdef __USE_TIME_BITS64 +DEFINE_LIBC_ERRNO_SHIM_NAMED(epoll_pwait2, "__epoll_pwait2_time64", int, + int, fd, + struct epoll_event *, events, + int, maxevents, + const struct timespec *, timeout, + const sigset_t *, sigmask) +#else DEFINE_LIBC_ERRNO_SHIM(epoll_pwait2, int, int, fd, struct epoll_event *, events, int, maxevents, const struct timespec *, timeout, const sigset_t *, sigmask) +#endif diff --git a/src/libc/libc-shim.h b/src/libc/libc-shim.h index 022ee5ad15..4377b713f0 100644 --- a/src/libc/libc-shim.h +++ b/src/libc/libc-shim.h @@ -102,3 +102,25 @@ errno = ENOSYS; \ return -1; \ } + +/* Like DEFINE_LIBC_ERRNO_SHIM but with an explicit string for the libc symbol name to dlsym. This is + * needed for functions whose libc symbol is renamed via __asm__() in the header (e.g. when glibc + * redirects time-related calls to their __*_time64 aliases on 32-bit systems built with + * _TIME_BITS=64) since dlsym() doesn't see those header-level renames, so the caller has to spell out + * the actual ABI symbol name that matches the struct layout the compiler picked. We can't forward to + * DEFINE_LIBC_ERRNO_SHIM since passing `func` as a regular argument would let the override-header + * #define rewrite the token (e.g. `epoll_pwait2` to `epoll_pwait2_shim`) before the inner macro + * could paste it, so we duplicate the body and keep every `func` reference behind `#` or `##`. */ +#define DEFINE_LIBC_ERRNO_SHIM_NAMED(func, sym_name, ret, ...) \ + static typeof(&func##_shim) func##_shim_cache; \ + __attribute__((constructor)) static void func##_shim_init(void) { \ + void *p = dlsym(RTLD_DEFAULT, sym_name); \ + __asm__ volatile("" ::: "memory"); \ + func##_shim_cache = (typeof(&func##_shim)) p; \ + } \ + ret func##_shim(_SHIM_DECL(__VA_ARGS__)) { \ + if (func##_shim_cache) \ + return func##_shim_cache(_SHIM_NAME(__VA_ARGS__)); \ + errno = ENOSYS; \ + return -1; \ + }