Commit 6f589c43 authored by jan.koester's avatar jan.koester
Browse files

libc: glibc compatibility symbols (LFS *64, gnu_get_libc_version, __res_init)

Rust's std built by rustc for *-linux-gnu calls the LFS "64" variants
through the libc crate -- linking hello world failed on mmap64/open64.
off_t and struct stat/dirent are 64-bit on every systempp target, so each
is the plain function, as in musl: stat64 fstat64 lstat64 fstatat64
open64 openat64 creat64 lseek64 mmap64 pread64 pwrite64 ftruncate64
truncate64 sendfile64 posix_fadvise64 posix_fallocate64 readdir64
fopen64 freopen64 fseeko64 ftello64 tmpfile64 getrlimit64 setrlimit64
statfs64 fstatfs64. The headers already mapped these names with macros
(enough for C), so the file #undefs them. Also gnu_get_libc_version
("2.41": std asks it to pick glibc workarounds, none apply),
gnu_get_libc_release and __res_init.

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent 427d6789
Loading
Loading
Loading
Loading
+1 −0
Changes for src/CMakeLists.txt: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -208,6 +208,7 @@ list(APPEND osSrcs
    "linux/sysprocess.cpp"
    "linux/terminate.cpp"
    "linux/unistd.cpp"
    "linux/glibc_compat.cpp"
    "linux/wchar.cpp"
    "linux/stdio.cpp"
    "linux/membarrier.cpp"
+124 −0
Changes for src/linux/glibc_compat.cpp: 124 added lines, 0 removed lines.
Original line number Diff line number Diff line
/* glibc-only names programs built for *-linux-gnu link against -- above
 * all Rust's std through the libc crate, which calls the LFS "64" variants
 * (open64, stat64, mmap64, ...) on that target. systempp's off_t and struct
 * stat/dirent are 64-bit on every target, so each is the plain function
 * (as in musl). gnu_get_libc_version: std asks for it to decide on glibc
 * bug workarounds; systempp reports a current glibc, none of them apply. */
/*******************************************************************************
Copyright (c) 2026, Jan Koester jan.koester@gmx.net
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
	* Redistributions of source code must retain the above copyright
	  notice, this list of conditions and the following disclaimer.
	* Redistributions in binary form must reproduce the above copyright
	  notice, this list of conditions and the following disclaimer in the
	  documentation and/or other materials provided with the distribution.
	* Neither the name of the <organization> nor the
	  names of its contributors may be used to endorse or promote products
	  derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/sendfile.h>
#include <sys/statfs.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <stdio.h>
#include <stdarg.h>
#include <resolv.h>

/* The headers map these names to the plain functions with macros (enough
 * for C sources); here they are defined as real symbols. */
#undef stat64
#undef fstat64
#undef lstat64
#undef fstatat64
#undef open64
#undef openat64
#undef creat64
#undef lseek64
#undef mmap64
#undef pread64
#undef pwrite64
#undef ftruncate64
#undef truncate64
#undef sendfile64
#undef posix_fadvise64
#undef posix_fallocate64
#undef readdir64
#undef fopen64
#undef freopen64
#undef fseeko64
#undef ftello64
#undef tmpfile64
#undef getrlimit64
#undef setrlimit64
#undef statfs64
#undef fstatfs64
#undef dirent64

static_assert(sizeof(off_t) == 8, "the *64 aliases assume a 64-bit off_t");

extern "C" {
    int stat64(const char* path, struct stat* st) { return stat(path, st); }
    int fstat64(int fd, struct stat* st) { return fstat(fd, st); }
    int lstat64(const char* path, struct stat* st) { return lstat(path, st); }
    int fstatat64(int dirfd, const char* path, struct stat* st, int flags) { return fstatat(dirfd, path, st, flags); }

    /* the mode argument is only there with O_CREAT/O_TMPFILE; reading it
     * anyway is what glibc does too (va_arg of an int not passed is
     * harmless on every ABI systempp has) */
    int open64(const char* path, int flags, ...) {
        va_list ap; va_start(ap, flags); mode_t mode = (mode_t)va_arg(ap, int); va_end(ap);
        return open(path, flags, mode);
    }
    int openat64(int dirfd, const char* path, int flags, ...) {
        va_list ap; va_start(ap, flags); mode_t mode = (mode_t)va_arg(ap, int); va_end(ap);
        return openat(dirfd, path, flags, mode);
    }
    int creat64(const char* path, mode_t mode) { return creat(path, mode); }

    off_t lseek64(int fd, off_t off, int whence) { return lseek(fd, off, whence); }
    void* mmap64(void* addr, size_t len, int prot, int flags, int fd, off_t off) {
        return mmap(addr, len, prot, flags, fd, off);
    }
    ssize_t pread64(int fd, void* buf, size_t n, off_t off) { return pread(fd, buf, n, off); }
    ssize_t pwrite64(int fd, const void* buf, size_t n, off_t off) { return pwrite(fd, buf, n, off); }
    int ftruncate64(int fd, off_t len) { return ftruncate(fd, len); }
    int truncate64(const char* path, off_t len) { return truncate(path, len); }
    ssize_t sendfile64(int out_fd, int in_fd, off_t* off, size_t count) { return sendfile(out_fd, in_fd, off, count); }
    int posix_fadvise64(int fd, off_t off, off_t len, int advice) { return posix_fadvise(fd, off, len, advice); }
    int posix_fallocate64(int fd, off_t off, off_t len) { return posix_fallocate(fd, off, len); }

    struct dirent* readdir64(DIR* d) { return readdir(d); }

    FILE* fopen64(const char* path, const char* mode) { return fopen(path, mode); }
    FILE* freopen64(const char* path, const char* mode, FILE* f) { return freopen(path, mode, f); }
    int fseeko64(FILE* f, off_t off, int whence) { return fseeko(f, off, whence); }
    off_t ftello64(FILE* f) { return ftello(f); }
    FILE* tmpfile64(void) { return tmpfile(); }

    int getrlimit64(int res, struct rlimit* rl) { return getrlimit(res, rl); }
    int setrlimit64(int res, const struct rlimit* rl) { return setrlimit(res, rl); }
    int statfs64(const char* path, struct statfs* buf) { return statfs(path, buf); }
    int fstatfs64(int fd, struct statfs* buf) { return fstatfs(fd, buf); }

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

    int __res_init(void) { return res_init(); }
}