Commit f250c1c0 authored by jan.koester's avatar jan.koester
Browse files

libc: glibc's 64-bit-time ABI on 32-bit targets (struct stat, __*64 names)

For Rust on powerpc (the libc crate in its glibc _TIME_BITS=64 mode),
compared field by field against what rustc computes for
powerpc-unknown-linux-gnu: every struct matched -- timespec with its
big-endian padding before tv_nsec, timeval, dirent64, rusage, msghdr,
sigaction -- except struct stat. systempp used x86_64's layout on every
target (st_nlink before st_mode, a pad before st_rdev, 136 bytes on
ppc32); 32-bit targets now have glibc's generic __stat64_t64 one (112
bytes). stat() fills it by field name from statx, so only the header
changes; x86_64 is unchanged.

glibc_compat.cpp gets the time64 symbol names (__clock_gettime64,
__stat64_time64, __nanosleep64, __ppoll64, __recvmsg64, ... 47 of them,
those whose function systempp has) on 32-bit targets, each the plain
function since time_t is 64-bit there already.

Checked on ppc under qemu: stat/fstat/__stat64_time64 agree with the
host, __clock_gettime64 works. x86_64 ctest: 170/171, e2e_uid_r_src
fails only because the host now runs a uid_r kernel and the test
expects to run as root.

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent 3545e990
Loading
Loading
Loading
Loading
+18 −0
Changes for include/sys/stat.h: 18 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -92,9 +92,14 @@ extern "C" {
#define __st_uid_t unsigned int
#define __st_gid_t unsigned int
#endif
    /* stat() fills it field by field from statx(2), so the layout is ours to
     * choose: glibc's, which programs built for *-linux-gnu hardcode (Rust's
     * libc crate) -- x86_64's on 64-bit targets, the generic 64-bit-time
     * one (glibc's struct __stat64_t64, 112 bytes) on 32-bit ones. */
    struct stat {
        dev_t     st_dev;
        ino_t     st_ino;
#if defined(__LP64__) || defined(__x86_64__)
        nlink_t   st_nlink;
        mode_t    st_mode;
        __st_uid_t st_uid;    /* user ID of owner */
@@ -104,6 +109,17 @@ extern "C" {
        off_t     st_size;    /* total size, in bytes */
        blksize_t st_blksize; /* blocksize for file system I/O */
        blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
#else
        mode_t    st_mode;
        nlink_t   st_nlink;
        __st_uid_t st_uid;
        __st_gid_t st_gid;
        dev_t     st_rdev;
        off_t     st_size;
        blksize_t st_blksize;
        int       __pad1;
        blkcnt_t  st_blocks;
#endif
        /* POSIX.1-2008 timestamps (same bytes as the kernel's
         * st_atime/st_atime_nsec pairs): struct timespec members, with
         * the classic names as macros, like glibc and musl. Real gap:
@@ -113,7 +129,9 @@ extern "C" {
        struct timespec st_atim;  /* time of last access */
        struct timespec st_mtim;  /* time of last modification */
        struct timespec st_ctim;  /* time of last status change */
#if defined(__LP64__) || defined(__x86_64__)
        long      __unused[3];
#endif
    };
#undef __st_uid_t
#undef __st_gid_t
+88 −0
Changes for src/linux/glibc_compat.cpp: 88 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -40,6 +40,24 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <stdio.h>
#include <stdarg.h>
#include <resolv.h>
#if !defined(__LP64__)
#include <time.h>
#include <utime.h>
#include <poll.h>
#include <glob.h>
#include <pthread.h>
#include <semaphore.h>
#include <sched.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/timex.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <sys/prctl.h>
#include <sys/shm.h>
#endif

/* The headers map these names to the plain functions with macros (enough
 * for C sources); here they are defined as real symbols. */
@@ -117,6 +135,76 @@ extern "C" {
    int statfs64(const char* path, struct statfs* buf) { return statfs(path, buf); }
    int fstatfs64(int fd, struct statfs* buf) { return fstatfs(fd, buf); }

#if !defined(__LP64__)
    /* glibc's 64-bit-time names on 32-bit targets (_TIME_BITS=64; Rust's
     * libc crate with RUST_LIBC_UNSTABLE_GNU_TIME_BITS=64): systempp's time_t
     * is 64-bit there already, so each is the plain function. The structs
     * they take match glibc's time64 layouts (struct stat: see sys/stat.h). */
    int __clock_gettime64(clockid_t c, struct timespec* t) { return clock_gettime(c, t); }
    int __clock_getres64(clockid_t c, struct timespec* t) { return clock_getres(c, t); }
    int __clock_settime64(clockid_t c, const struct timespec* t) { return clock_settime(c, t); }
    int __clock_nanosleep_time64(clockid_t c, int f, const struct timespec* r, struct timespec* rem) { return clock_nanosleep(c, f, r, rem); }
    int __clock_adjtime64(clockid_t c, struct timex* t) { return clock_adjtime(c, t); }
    int ___adjtimex64(struct timex* t) { return adjtimex(t); }
    double __difftime64(time_t a, time_t b) { return difftime(a, b); }
    int __nanosleep64(const struct timespec* r, struct timespec* rem) { return nanosleep(r, rem); }
    time_t __time64(time_t* t) { return time(t); }
    int __gettimeofday64(struct timeval* tv, void* tz) { return gettimeofday(tv, (struct timezone*)tz); }
    int __settimeofday64(const struct timeval* tv, const void* tz) { return settimeofday(tv, (const struct timezone*)tz); }
    struct tm* __gmtime64(const time_t* t) { return gmtime(t); }
    struct tm* __localtime64(const time_t* t) { return localtime(t); }
    time_t __mktime64(struct tm* tm) { return mktime(tm); }
    time_t __timegm64(struct tm* tm) { return timegm(tm); }

    int __stat64_time64(const char* p, struct stat* st) { return stat(p, st); }
    int __fstat64_time64(int fd, struct stat* st) { return fstat(fd, st); }
    int __lstat64_time64(const char* p, struct stat* st) { return lstat(p, st); }
    int __fstatat64_time64(int d, const char* p, struct stat* st, int fl) { return fstatat(d, p, st, fl); }
    int __futimens64(int fd, const struct timespec t[2]) { return futimens(fd, t); }
    int __utimensat64(int d, const char* p, const struct timespec t[2], int fl) { return utimensat(d, p, t, fl); }
    int __futimes64(int fd, const struct timeval t[2]) { return futimes(fd, t); }
    int __lutimes64(const char* p, const struct timeval t[2]) { return lutimes(p, t); }
    int __utimes64(const char* p, const struct timeval t[2]) { return utimes(p, t); }
    int __utime64(const char* p, const struct utimbuf* t) { return utime(p, t); }
    int __getrusage64(int who, struct rusage* r) { return getrusage(who, r); }
    pid_t __wait4_time64(pid_t pid, int* st, int opt, struct rusage* r) { return wait4(pid, st, opt, r); }

    int __fcntl_time64(int fd, int cmd, ...) {
        va_list ap; va_start(ap, cmd); long arg = va_arg(ap, long); va_end(ap);
        return fcntl(fd, cmd, arg);
    }
    int __ioctl_time64(int fd, unsigned long req, ...) {
        va_list ap; va_start(ap, req); void* arg = va_arg(ap, void*); va_end(ap);
        return ioctl(fd, req, arg);
    }
    int __prctl_time64(int op, ...) {
        va_list ap; va_start(ap, op);
        unsigned long a2 = va_arg(ap, unsigned long), a3 = va_arg(ap, unsigned long);
        unsigned long a4 = va_arg(ap, unsigned long), a5 = va_arg(ap, unsigned long);
        va_end(ap);
        return prctl(op, a2, a3, a4, a5);
    }

    int __getsockopt64(int fd, int lvl, int name, void* v, socklen_t* l) { return getsockopt(fd, lvl, name, v, l); }
    int __setsockopt64(int fd, int lvl, int name, const void* v, socklen_t l) { return setsockopt(fd, lvl, name, v, l); }
    ssize_t __recvmsg64(int fd, struct msghdr* m, int fl) { return recvmsg(fd, m, fl); }
    ssize_t __sendmsg64(int fd, const struct msghdr* m, int fl) { return sendmsg(fd, m, fl); }
    int __recvmmsg64(int fd, struct mmsghdr* v, unsigned int n, int fl, struct timespec* t) { return recvmmsg(fd, v, n, fl, t); }
    int __sendmmsg64(int fd, struct mmsghdr* v, unsigned int n, int fl) { return sendmmsg(fd, v, n, fl); }
    int __ppoll64(struct pollfd* f, nfds_t n, const struct timespec* t, const sigset_t* m) { return ppoll(f, n, t, m); }
    int __pselect64(int n, fd_set* r, fd_set* w, fd_set* e, const struct timespec* t, const sigset_t* m) { return pselect(n, r, w, e, t, m); }
    int __select64(int n, fd_set* r, fd_set* w, fd_set* e, struct timeval* t) { return select(n, r, w, e, t); }

    int __pthread_cond_timedwait64(pthread_cond_t* c, pthread_mutex_t* m, const struct timespec* t) { return pthread_cond_timedwait(c, m, t); }
    int __sem_timedwait64(sem_t* s, const struct timespec* t) { return sem_timedwait(s, t); }
    int __sched_rr_get_interval64(pid_t pid, struct timespec* t) { return sched_rr_get_interval(pid, t); }
    int __sigtimedwait64(const sigset_t* set, siginfo_t* info, const struct timespec* t) { return sigtimedwait(set, info, t); }
    int __shmctl64(int id, int cmd, struct shmid_ds* b) { return shmctl(id, cmd, b); }

    int __glob64_time64(const char* pat, int fl, int (*err)(const char*, int), glob_t* g) { return glob(pat, fl, err, g); }
    void __globfree64_time64(glob_t* g) { globfree(g); }
#endif

    const char* gnu_get_libc_version(void) { return "2.41"; }
    const char* gnu_get_libc_release(void) { return "stable"; }