Skip to content

Commit

Permalink
Fixed some issue when build OS is _time64 aware (should help #987 #983
Browse files Browse the repository at this point in the history
…#976 #975, maybe some other)
  • Loading branch information
ptitSeb committed Aug 11, 2024
1 parent 9811f14 commit 664956a
Show file tree
Hide file tree
Showing 14 changed files with 306 additions and 64 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ set(ELFLOADER_SRC
"${BOX86_ROOT}/src/libtools/auxval.c"
"${BOX86_ROOT}/src/libtools/myalign.c"
"${BOX86_ROOT}/src/libtools/myalign64.c"
"${BOX86_ROOT}/src/libtools/myalignsys64.c"
"${BOX86_ROOT}/src/libtools/stat64_helper.c"
"${BOX86_ROOT}/src/libtools/myfts.c"
"${BOX86_ROOT}/src/libtools/sdl1rwops.c"
Expand Down
6 changes: 3 additions & 3 deletions src/emu/x86syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ void EXPORT x86Syscall(x86emu_t *emu)
{
struct stat64 st;
unsigned int r = syscall(__NR_stat64, R_EBX, &st);
UnalignStat64(&st, (void*)R_ECX);
UnalignSysStat64(&st, (void*)R_ECX);

R_EAX = r;
}
Expand All @@ -696,7 +696,7 @@ void EXPORT x86Syscall(x86emu_t *emu)
{
struct stat64 st;
unsigned int r = syscall(__NR_lstat64, R_EBX, &st);
UnalignStat64(&st, (void*)R_ECX);
UnalignSysStat64(&st, (void*)R_ECX);

R_EAX = r;
}
Expand All @@ -707,7 +707,7 @@ void EXPORT x86Syscall(x86emu_t *emu)
{
struct stat64 st;
unsigned int r = syscall(__NR_fstat64, R_EBX, &st);
UnalignStat64(&st, (void*)R_ECX);
UnalignSysStat64(&st, (void*)R_ECX);

R_EAX = r;
}
Expand Down
48 changes: 39 additions & 9 deletions src/include/myalign.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ void myStackAlignW(const char* fmt, uint32_t* st, uint32_t* mystack);

void AlignStat64(const void* source, void* dest);
void UnalignStat64(const void* source, void* dest);
void UnalignSysStat64(const void* source, void* dest);
void UnalignStat(const void* source, void* dest);

void UnalignStatFS64(const void* source, void* dest);
void UnalignStatFS(const void* source, void* dest);

void UnalignOggVorbis(void* dest, void* source); // Arm -> x86
void AlignOggVorbis(void* dest, void* source); // x86 -> Arm
Expand All @@ -43,26 +46,53 @@ void AlignEpollEvent(void* dest, void* source, int nbr); // x86 -> Arm
void UnalignSmpegInfo(void* dest, void* source); // Arm -> x86
void AlignSmpegInfo(void* dest, void* source); // x86 -> Arm

void Timespec2Timespec64(void* dest, const void* source);
void Timespec642Timespec(void* dest, const void* source);

// stat64 is packed on i386, not on arm (and possibly other structures)
#undef st_atime
#undef st_atime_nsec
#undef st_mtime
#undef st_mtime_nsec
#undef st_ctime
#undef st_ctime_nsec

struct i386_stat {
uint64_t st_dev;
uint32_t __pad1;
uint32_t st_ino;
uint32_t st_mode;
uint32_t st_nlink;
uint32_t st_uid;
uint32_t st_gid;
uint64_t st_rdev;
uint32_t __pad2;
int32_t st_size;
int32_t st_blksize;
int32_t st_blocks;
int32_t st_atime_sec;
uint32_t st_atime_nsec;
int32_t st_mtime_sec;
uint32_t st_mtime_nsec;
int32_t st_ctime_sec;
uint32_t st_ctime_nsec;
uint32_t __unused4;
uint32_t __unused5;
} __attribute__((packed));

struct i386_stat64 {
uint64_t st_dev;
uint8_t __pad0[4];
uint32_t __st_ino;
uint32_t st_mode;
uint32_t st_nlink;
uint32_t st_uid;
uint32_t st_gid;
uint32_t __pad0;
uint32_t __st_ino;
uint32_t st_mode;
uint32_t st_nlink;
uint32_t st_uid;
uint32_t st_gid;
uint64_t st_rdev;
uint8_t __pad3[4];
uint32_t __pad3;
int64_t st_size;
uint32_t st_blksize;
uint64_t st_blocks;
uint32_t st_blksize;
uint64_t st_blocks;
uint32_t st_atime;
uint32_t st_atime_nsec;
uint32_t st_mtime;
Expand Down
84 changes: 84 additions & 0 deletions src/libtools/myalign.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <wchar.h>
#include <sys/epoll.h>
#include <fts.h>
#include <sys/stat.h>
#include <sys/statfs.h>

#include "x86emu.h"
#include "emu/x86emu_private.h"
Expand Down Expand Up @@ -918,3 +920,85 @@ void unalignNGValue(void* value, my_GValue_t* v, int n)
--n;
}
}

void UnalignStat(const void* source, void* dest)
{
struct i386_stat *i386st = (struct i386_stat*)dest;
struct stat *st = (struct stat*) source;

i386st->__pad1 = 0;
i386st->__pad2 = 0;
i386st->st_dev = st->st_dev;
i386st->st_ino = st->st_ino;
i386st->st_mode = st->st_mode;
i386st->st_nlink = st->st_nlink;
i386st->st_uid = st->st_uid;
i386st->st_gid = st->st_gid;
i386st->st_rdev = st->st_rdev;
i386st->st_size = st->st_size;
i386st->st_blksize = st->st_blksize;
i386st->st_blocks = st->st_blocks;
# ifdef __USE_XOPEN2K8
i386st->st_atime_sec = st->st_atim.tv_sec;
i386st->st_atime_nsec = st->st_atim.tv_nsec;
i386st->st_mtime_sec = st->st_mtim.tv_sec;
i386st->st_mtime_nsec = st->st_mtim.tv_nsec;
i386st->st_ctime_sec = st->st_ctim.tv_sec;
i386st->st_ctime_nsec = st->st_ctim.tv_nsec;
#else
i386st->st_atime_sec = st->st_atime;
i386st->st_atime_nsec = st->st_atimensec;
i386st->st_mtime_sec = st->st_mtime;
i386st->st_mtime_nsec = st->st_mtimensec;
i386st->st_ctime_sec = st->st_ctime;
i386st->st_ctime_nsec = st->st_ctimensec;
#endif
}

void UnalignStatFS(const void* source, void* dest)
{
struct i386_statfs *i386st = (struct i386_statfs*)dest;
struct statfs *st = (struct statfs*) source;

i386st->f_type = st->f_type;
i386st->f_bsize = st->f_bsize;
i386st->f_blocks = st->f_blocks;
i386st->f_bfree = st->f_bfree;
i386st->f_bavail = st->f_bavail;
i386st->f_files = st->f_files;
i386st->f_ffree = st->f_ffree;
memcpy(&i386st->f_fsid, &st->f_fsid, sizeof(i386st->f_fsid));
i386st->f_namelen = st->f_namelen;
i386st->f_frsize = st->f_frsize;
i386st->f_flags = st->f_flags;
i386st->f_spare[0] = st->f_spare[0];
i386st->f_spare[1] = st->f_spare[1];
i386st->f_spare[2] = st->f_spare[2];
i386st->f_spare[3] = st->f_spare[3];
}

typedef struct my_timespec_s {
int tv_sec;
int tv_nsec;
} my_timespec_t;

typedef struct my_timespec64_s {
int64_t tv_sec;
int tv_nsec;
int :32;
} my_timespec64_t;

void Timespec2Timespec64(void* dest, const void* source)
{
my_timespec64_t* dst = (my_timespec64_t*)dest;
my_timespec_t* src = (my_timespec_t*)source;
dst->tv_sec = src->tv_sec;
dst->tv_nsec = src->tv_nsec;
}
void Timespec642Timespec(void* dest, const void* source)
{
my_timespec_t* dst = (my_timespec_t*)dest;
my_timespec64_t* src = (my_timespec64_t*)source;
dst->tv_sec = src->tv_sec;
dst->tv_nsec = src->tv_nsec;
}
41 changes: 31 additions & 10 deletions src/libtools/myalign64.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,31 @@
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <asm/stat.h>
#include <sys/stat.h>
#include <sys/vfs.h>
#include <signal.h>
#include <stddef.h>

#include "x86emu.h"
#include "emu/x86emu_private.h"
#include "myalign.h"
#include "debug.h"

void UnalignStat64(const void* source, void* dest)
{
struct i386_stat64 *i386st = (struct i386_stat64*)dest;
struct stat64 *st = (struct stat64*) source;

memset(i386st->__pad0, 0, sizeof(i386st->__pad0));
memset(i386st->__pad3, 0, sizeof(i386st->__pad3));
i386st->__pad0 = 0;
i386st->__pad3 = 0;
i386st->st_dev = st->st_dev;
#ifndef POWERPCLE
i386st->__st_ino = st->__st_ino;
#else
#ifdef __USE_TIME64_REDIRECTS
i386st->__st_ino = st->st_ino;
#elif defined(POWERPCLE)
i386st->__st_ino = st->st_ino; // Separate __st_ino doesn't
// exist on powerpc
#else
i386st->__st_ino = st->__st_ino;
#endif
i386st->st_mode = st->st_mode;
i386st->st_nlink = st->st_nlink;
Expand All @@ -35,12 +39,21 @@ void UnalignStat64(const void* source, void* dest)
i386st->st_size = st->st_size;
i386st->st_blksize = st->st_blksize;
i386st->st_blocks = st->st_blocks;
# ifdef __USE_XOPEN2K8
i386st->st_atime = st->st_atim.tv_sec;
i386st->st_atime_nsec = st->st_atim.tv_nsec;
i386st->st_mtime = st->st_mtim.tv_sec;
i386st->st_mtime_nsec = st->st_mtim.tv_nsec;
i386st->st_ctime = st->st_ctim.tv_sec;
i386st->st_ctime_nsec = st->st_ctim.tv_nsec;
#else
i386st->st_atime = st->st_atime;
i386st->st_atime_nsec = st->st_atime_nsec;
i386st->st_mtime = st->st_mtime;
i386st->st_mtime_nsec = st->st_mtime_nsec;
i386st->st_ctime = st->st_ctime;
i386st->st_ctime_nsec = st->st_ctime_nsec;
#endif
i386st->st_ino = st->st_ino;
}

Expand All @@ -50,11 +63,10 @@ void AlignStat64(const void* source, void* dest)
struct i386_stat64 *i386st = (struct i386_stat64*)source;

st->st_dev = i386st->st_dev;
#ifndef POWERPCLE
st->__st_ino = i386st->__st_ino;
#if defined(__USE_TIME64_REDIRECTS) || defined(POWERPCLE)
// Separate __st_ino doesn't exist
#else
st->st_ino = i386st->__st_ino; // Separate __st_ino doesn't
// exist on powerpc
st->__st_ino = i386st->__st_ino;
#endif
st->st_mode = i386st->st_mode;
st->st_nlink = i386st->st_nlink;
Expand All @@ -64,12 +76,21 @@ void AlignStat64(const void* source, void* dest)
st->st_size = i386st->st_size;
st->st_blksize = i386st->st_blksize;
st->st_blocks = i386st->st_blocks;
#ifdef __USE_XOPEN2K8
st->st_atim.tv_sec = i386st->st_atime;
st->st_atim.tv_nsec = i386st->st_atime_nsec;
st->st_mtim.tv_sec = i386st->st_mtime;
st->st_mtim.tv_nsec = i386st->st_mtime_nsec;
st->st_ctim.tv_sec = i386st->st_ctime;
st->st_ctim.tv_nsec = i386st->st_ctime_nsec;
#else
st->st_atime = i386st->st_atime;
st->st_atime_nsec = i386st->st_atime_nsec;
st->st_mtime = i386st->st_mtime;
st->st_mtime_nsec = i386st->st_mtime_nsec;
st->st_ctime = i386st->st_ctime;
st->st_ctime_nsec = i386st->st_ctime_nsec;
#endif
st->st_ino = i386st->st_ino;
}

Expand Down
47 changes: 47 additions & 0 deletions src/libtools/myalignsys64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#define _LARGEFILE_SOURCE 1
#define _FILE_OFFSET_BITS 64
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <asm/stat.h>
#include <sys/vfs.h>
#include <signal.h>
#include <stddef.h>

#include "x86emu.h"
#include "emu/x86emu_private.h"
#include "myalign.h"
#include "debug.h"

void UnalignSysStat64(const void* source, void* dest)
{
struct i386_stat64 *i386st = (struct i386_stat64*)dest;
struct stat64 *st = (struct stat64*) source;

i386st->__pad0 = 0;
i386st->__pad3 = 0;
i386st->st_dev = st->st_dev;
#ifndef POWERPCLE
i386st->__st_ino = st->__st_ino;
#else
i386st->__st_ino = st->st_ino; // Separate __st_ino doesn't
// exist on powerpc
#endif
i386st->st_mode = st->st_mode;
i386st->st_nlink = st->st_nlink;
i386st->st_uid = st->st_uid;
i386st->st_gid = st->st_gid;
i386st->st_rdev = st->st_rdev;
i386st->st_size = st->st_size;
i386st->st_blksize = st->st_blksize;
i386st->st_blocks = st->st_blocks;
i386st->st_atime = st->st_atime;
i386st->st_atime_nsec = st->st_atime_nsec;
i386st->st_mtime = st->st_mtime;
i386st->st_mtime_nsec = st->st_mtime_nsec;
i386st->st_ctime = st->st_ctime;
i386st->st_ctime_nsec = st->st_ctime_nsec;
i386st->st_ino = st->st_ino;
}
Loading

0 comments on commit 664956a

Please sign in to comment.