Skip to content

Commit

Permalink
abis/linux: Add 64 bit counterparts of structs
Browse files Browse the repository at this point in the history
options/posix: Add 64 bit counterparts of structs and functions
  • Loading branch information
Dennisbonke authored and no92 committed Feb 29, 2024
1 parent d2e207f commit d7f69aa
Show file tree
Hide file tree
Showing 27 changed files with 155 additions and 82 deletions.
20 changes: 13 additions & 7 deletions abis/linux/msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@
extern "C" {
#endif

#if defined(__i386__)
typedef __mlibc_uint64 msglen_t;
typedef __mlibc_uint64 msgqnum_t;
#else
typedef unsigned long msglen_t;
typedef unsigned long msgqnum_t;
#endif

struct msqid_ds {
struct ipc_perm msg_perm;
struct msqid64_ds {
struct ipc64_perm msg_perm;
#if (UINTPTR_MAX == UINT64_MAX) /* || x32 ABI */
time_t msg_stime;
time_t msg_rtime;
time_t msg_ctime;
#else
unsigned long msg_stime;
unsigned long msg_stime_high;
unsigned long msg_rtime;
unsigned long msg_rtime_high;
unsigned long msg_ctime;
unsigned long msg_ctime_high;
#endif
unsigned long msg_cbytes;
msgqnum_t msg_qnum;
msglen_t msg_qbytes;
Expand All @@ -31,6 +35,8 @@ struct msqid_ds {
unsigned long __unused[2];
};

#define msqid_ds msqid64_ds

#ifdef __cplusplus
}
#endif
Expand Down
9 changes: 6 additions & 3 deletions abis/linux/signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ typedef void (*__sighandler) (int);
#define SIGRTMIN 35
#define SIGRTMAX 64

typedef uint64_t sigset_t;
typedef struct {
unsigned long sig[1024 / (8 * sizeof(long))];
} sigset_t;

// constants for sigprocmask()
#define SIG_BLOCK 0
Expand Down Expand Up @@ -266,10 +268,11 @@ struct sigaction {
void (*sa_handler)(int);
void (*sa_sigaction)(int, siginfo_t *, void *);
} __sa_handler;
sigset_t sa_mask;
int sa_flags;
unsigned long sa_flags;
void (*sa_restorer)(void);
sigset_t sa_mask;
};

#define sa_handler __sa_handler.sa_handler
#define sa_sigaction __sa_handler.sa_sigaction

Expand Down
3 changes: 2 additions & 1 deletion abis/linux/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ struct msghdr {

struct sockaddr_storage {
sa_family_t ss_family;
char __padding[128 - sizeof(sa_family_t)];
char __padding[128 - sizeof(sa_family_t) - sizeof(long)];
long __force_alignment;
};

struct mmsghdr {
Expand Down
9 changes: 5 additions & 4 deletions abis/linux/stat.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,21 @@ struct stat {

struct stat {
dev_t st_dev;
ino_t st_ino;
ino64_t st_ino;
mode_t st_mode;
nlink_t st_nlink;
uid_t st_uid;
gid_t st_gid;
dev_t st_rdev;
unsigned long __pad1;
dev_t __pad1;
off_t st_size;
blksize_t st_blksize;
int __pad2;
blkcnt_t st_blocks;
struct timespec st_atim;
struct timespec st_mtim;
struct timespec st_ctim;
unsigned int __unused4;
unsigned int __unused5;
int __pad3[2];
};

#elif defined(__i386__)
Expand Down Expand Up @@ -116,6 +115,8 @@ struct stat {

#endif

#define stat64 stat

#ifdef __cplusplus
}
#endif
Expand Down
33 changes: 20 additions & 13 deletions abis/linux/statfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,28 @@ typedef struct __mlibc_fsid {
int __val[2];
} fsid_t;

struct statfs {
unsigned long f_type;
unsigned long f_bsize;
fsblkcnt_t f_blocks;
fsblkcnt_t f_bfree;
fsblkcnt_t f_bavail;

fsfilcnt_t f_files;
fsfilcnt_t f_ffree;
fsid_t f_fsid;
unsigned long f_namelen;
unsigned long f_frsize;
unsigned long f_flags;
#define __MLIBC_STATFS_BODY unsigned long f_type; \
unsigned long f_bsize; \
fsblkcnt_t f_blocks; \
fsblkcnt_t f_bfree; \
fsblkcnt_t f_bavail; \
fsfilcnt_t f_files; \
fsfilcnt_t f_ffree; \
fsid_t f_fsid; \
unsigned long f_namelen; \
unsigned long f_frsize; \
unsigned long f_flags; \
unsigned long __f_spare[4];

struct statfs {
__MLIBC_STATFS_BODY
};

struct statfs64 {
__MLIBC_STATFS_BODY
};

#undef __MLIBC_STATFS_BODY

#endif /* _ABIBITS_STATFS_H */

32 changes: 19 additions & 13 deletions abis/linux/statvfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,28 @@
#define ST_NOSUID 2
#define ST_MANDLOCK 64

#define __MLIBC_STATVFS_BODY unsigned long f_bsize; \
unsigned long f_frsize; \
fsblkcnt_t f_blocks; \
fsblkcnt_t f_bfree; \
fsblkcnt_t f_bavail; \
fsfilcnt_t f_files; \
fsfilcnt_t f_ffree; \
fsfilcnt_t f_favail; \
unsigned long f_fsid; \
unsigned long f_flag; \
unsigned long f_namemax;

// On Linux, this struct is not directly used by the kernel.
struct statvfs {
unsigned long f_bsize;
unsigned long f_frsize;
fsblkcnt_t f_blocks;
fsblkcnt_t f_bfree;
fsblkcnt_t f_bavail;

fsfilcnt_t f_files;
fsfilcnt_t f_ffree;
fsfilcnt_t f_favail;

unsigned long f_fsid;
unsigned long f_flag;
unsigned long f_namemax;
__MLIBC_STATVFS_BODY
};

struct statvfs64 {
__MLIBC_STATVFS_BODY
};

#undef __MLIBC_STATVFS_BODY

#endif /* _ABIBITS_STATVFS_H */

2 changes: 1 addition & 1 deletion options/ansi/generic/signal-stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ __sighandler signal(int sn, __sighandler handler) {
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_flags = 0;
sa.sa_mask = 0;
sigemptyset(&sa.sa_mask);
struct sigaction old;
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_sigaction, SIG_ERR);
if(int e = mlibc::sys_sigaction(sn, &sa, &old)){
Expand Down
38 changes: 24 additions & 14 deletions options/internal/generic/sigset.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
#include <bits/sigset_t.h>
#include <bits/ensure.h>
#include <errno.h>
#include <limits.h>
#include <string.h>

int sigemptyset(sigset_t *sigset) {
*sigset = 0;
memset(sigset, 0, sizeof(*sigset));
return 0;
}

int sigfillset(sigset_t *sigset) {
*sigset = ~sigset_t(0);
memset(sigset, ~0, sizeof(*sigset));
return 0;
}

// TODO: Return EINVAL instead of __ensure()ing.

int sigaddset(sigset_t *sigset, int sig) {
int signo = sig - 1;
// TODO: do not hard code CHAR_BITS
__ensure((unsigned int)signo < sizeof(sigset_t) * 8);
*sigset |= sigset_t(1) << signo;
if(signo < 0 || static_cast<unsigned int>(signo) >= (sizeof(sigset_t) * CHAR_BIT)) {
errno = EINVAL;
return -1;
}
auto ptr = reinterpret_cast<char *>(sigset);
ptr[signo / CHAR_BIT] |= (1 << (signo % CHAR_BIT));
return 0;
}

int sigdelset(sigset_t *sigset, int sig) {
int signo = sig - 1;
// TODO: do not hard code CHAR_BITS
__ensure((unsigned int)signo < sizeof(sigset_t) * 8);
*sigset &= ~(sigset_t(1) << signo);
if(signo < 0 || static_cast<unsigned int>(signo) >= (sizeof(sigset_t) * CHAR_BIT)) {
errno = EINVAL;
return -1;
}
auto ptr = reinterpret_cast<char *>(sigset);
ptr[signo / CHAR_BIT] &= ~(1 << (signo % CHAR_BIT));
return 0;
}

int sigismember(const sigset_t *set, int sig) {
int sigismember(const sigset_t *sigset, int sig) {
int signo = sig - 1;
// TODO: do not hard code CHAR_BITS
__ensure((unsigned int)signo < sizeof(sigset_t) * 8);
return (*set) & (sigset_t(1) << signo);
if(signo < 0 || static_cast<unsigned int>(signo) >= (sizeof(sigset_t) * CHAR_BIT)) {
errno = EINVAL;
return -1;
}
auto ptr = reinterpret_cast<const char *>(sigset);
return !!(ptr[signo / CHAR_BIT] & (1 << (signo % CHAR_BIT)));
}
4 changes: 4 additions & 0 deletions options/posix/generic/dirent-stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ DIR *opendir(const char *path) {
return dir;
}
}

struct dirent *readdir(DIR *dir) {
__ensure(dir->__ent_next <= dir->__ent_limit);
if(dir->__ent_next == dir->__ent_limit) {
Expand All @@ -81,6 +82,9 @@ struct dirent *readdir(DIR *dir) {
dir->__ent_next += entp->d_reclen;
return &dir->__current;
}

struct dirent64 *readdir64(DIR *dir) __attribute__((alias("readdir")));

int readdir_r(DIR *dir, struct dirent *entry, struct dirent **result) {
if(!mlibc::sys_read_entries) {
MLIBC_MISSING_SYSDEP();
Expand Down
1 change: 1 addition & 0 deletions options/posix/generic/fcntl-stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,4 @@ int open(const char *pathname, int flags, ...) {
return fd;
}

int open64(const char *pathname, int flags, ...) __attribute__((alias("open")));
8 changes: 7 additions & 1 deletion options/posix/generic/posix_signal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,13 @@ int sigaltstack(const stack_t *__restrict ss, stack_t *__restrict oss) {

#if __MLIBC_GLIBC_OPTION
int sigisemptyset(const sigset_t *set) {
return !(*set);
auto ptr = reinterpret_cast<const char *>(set);
for(size_t i = 0; i < sizeof(sigset_t); i++) {
if(ptr[i]) {
return 0;
}
}
return 1;
}
#endif // __MLIBC_GLIBC_OPTION

Expand Down
4 changes: 4 additions & 0 deletions options/posix/generic/posix_stdio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ int fseeko(FILE *file_base, off_t offset, int whence) {
return 0;
}

int fseeko64(FILE *file_base, off64_t offset, int whence) __attribute__((alias("fseeko")));

off_t ftello(FILE *file_base) {
auto file = static_cast<mlibc::abstract_file *>(file_base);
off_t current_offset;
Expand All @@ -181,6 +183,8 @@ off_t ftello(FILE *file_base) {
return current_offset;
}

off64_t ftello64(FILE *file_base) __attribute__((alias("ftello")));

int dprintf(int fd, const char *format, ...) {
va_list args;
va_start(args, format);
Expand Down
2 changes: 2 additions & 0 deletions options/posix/generic/sys-mman-stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ void *mmap(void *hint, size_t size, int prot, int flags, int fd, off_t offset) {
return window;
}

void *mmap64(void *hint, size_t size, int prot, int flags, int fd, off_t offset) __attribute__((alias("mmap")));

int munmap(void *pointer, size_t size) {
if(int e = mlibc::sys_vm_unmap(pointer, size); e) {
errno = e;
Expand Down
3 changes: 3 additions & 0 deletions options/posix/generic/sys-stat-stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ int lstat(const char *path, struct stat *result) {
return 0;
}

int lstat64(const char *path, struct stat64 *result) __attribute__((alias("lstat")));

int fstat(int fd, struct stat *result) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_stat, -1);
if(int e = mlibc::sys_stat(mlibc::fsfd_target::fd, fd, "", 0, result); e) {
Expand All @@ -153,3 +155,4 @@ int fstat(int fd, struct stat *result) {
return 0;
}

int fstat64(int fd, struct stat64 *result) __attribute__((alias("fstat")));
2 changes: 2 additions & 0 deletions options/posix/generic/sys-statvfs-stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ int statvfs(const char *path, struct statvfs *out) {
return 0;
}

int statvfs64(const char *path, struct statvfs *out) __attribute__((alias("statvfs")));

int fstatvfs(int fd, struct statvfs *out) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_fstatvfs, -1);
if(int e = mlibc::sys_fstatvfs(fd, out); e) {
Expand Down
4 changes: 4 additions & 0 deletions options/posix/generic/unistd-stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ int ftruncate(int fd, off_t size) {
return 0;
}

int ftruncate64(int fd, off64_t size) __attribute__((alias("ftruncate")));

char *getcwd(char *buffer, size_t size) {
if (buffer) {
if (size == 0) {
Expand Down Expand Up @@ -783,6 +785,8 @@ int truncate(const char *, off_t) {
__builtin_unreachable();
}

int truncate64(const char *, off64_t) __attribute__((alias("truncate")));

char *ttyname(int fd) {
const size_t size = 128;
static thread_local char buf[size];
Expand Down
2 changes: 2 additions & 0 deletions options/posix/include/bits/posix/posix_stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ FILE *popen(const char*, const char *);
FILE *open_memstream(char **, size_t *);

int fseeko(FILE *stream, off_t offset, int whence);
int fseeko64(FILE *stream, off64_t offset, int whence);
off_t ftello(FILE *stream);
off64_t ftello64(FILE *stream);

int dprintf(int fd, const char *format, ...);
int vdprintf(int fd, const char *format, __builtin_va_list args);
Expand Down
1 change: 1 addition & 0 deletions options/posix/include/dirent.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ int dirfd(DIR *);
DIR *fdopendir(int);
DIR *opendir(const char *);
struct dirent *readdir(DIR *);
struct dirent64 *readdir64(DIR *);
int readdir_r(DIR *__restrict, struct dirent *__restrict, struct dirent **__restrict);
void rewinddir(DIR *);
int scandir(const char *, struct dirent ***, int (*)(const struct dirent *),
Expand Down
Loading

0 comments on commit d7f69aa

Please sign in to comment.