more progress

This commit is contained in:
Ethan Roseman 2023-10-27 17:40:14 +09:00
parent 34aec2324f
commit 02b7b717da
No known key found for this signature in database
GPG Key ID: 27F9FCEB8E4969BD

View File

@ -1,10 +1,19 @@
#include "common.h" #include "common.h"
#include <spawn.h>
// from https://codebrowser.dev/glibc/glibc/sysdeps/x86/fpu_control.h.html
#define _FPU_GETCW(cw) __asm__ __volatile__ ("fnstcw %0" : "=m" (*&cw))
#define _FPU_SETCW(cw) __asm__ __volatile__ ("fldcw %0" : : "m" (*&cw))
typedef void (WIN_ENTRY *_PVFV)(void); typedef void (WIN_ENTRY *_PVFV)(void);
namespace msvcrt { namespace msvcrt {
int _commode; int _commode;
int _fmode; int _fmode;
int fpcntrl;
char **___initenv; char **___initenv;
char **__pgmptr; char **__pgmptr;
@ -20,13 +29,30 @@ namespace msvcrt {
return &_commode; return &_commode;
} }
char*** WIN_ENTRY __p___initenv(void) { return &___initenv; } char*** WIN_ENTRY __p___initenv(void) {
char **WIN_ENTRY __p__pgmptr(void) { return __pgmptr; } return &___initenv;
void* WIN_ENTRY __p__iob(void) { return nullptr; } }
char **WIN_ENTRY __p__pgmptr(void) {
if (!__pgmptr) {
__pgmptr = (char**)malloc(1000000);
// TODO put something in here?
}
return __pgmptr;
}
unsigned int WIN_ENTRY _controlfp(unsigned int _new, unsigned int mask) { FILE* WIN_ENTRY __p__iob(void) {
return 0; return nullptr;
}
unsigned int WIN_ENTRY _controlfp(unsigned int new_value, unsigned int mask) {
DEBUG_LOG("_controlfp called with value: 0x%X, mask: 0x%X\n", new_value, mask);
_FPU_GETCW(fpcntrl);
fpcntrl = ((fpcntrl & ~mask) | (new_value & mask));
_FPU_SETCW(fpcntrl);
return fpcntrl;
} }
void WIN_ENTRY _initterm(_PVFV *start, _PVFV *end) { void WIN_ENTRY _initterm(_PVFV *start, _PVFV *end) {
@ -40,13 +66,33 @@ namespace msvcrt {
} }
} }
int WIN_ENTRY __getmainargs(int *argc, char ***argv, char ***envp, int expand_wildcards, int *new_mode) { int WIN_ENTRY __getmainargs(int* argc, char*** argv, char*** env, int doWildCard, void* startInfo) {
DEBUG_LOG("msvcrt::__getmainargs(%p, %p, %p, %d, %p)\n", argc, argv, envp, expand_wildcards, new_mode); DEBUG_LOG("__getmainargs: %p, %p, %p, %i, %p\n", argc, argv, env, doWildCard, startInfo);
return 0; *argc = wibo::argc;
} *argv = wibo::argv;
void WIN_ENTRY setbuf(FILE* file, char *buf) {}
}
return 0; // success
}
void WIN_ENTRY setbuf(FILE* file, char *buf) {
}
int WIN_ENTRY _spawnvp(int mode, char* cmdname, char** argv) {
DEBUG_LOG("_spawnvp: %s\n", cmdname);
setenv("WIBO_DEBUG_INDENT", std::to_string(wibo::debugIndent + 1).c_str(), true);
char *new_argv[] = {cmdname};
pid_t pid;
if (posix_spawn(&pid, wibo::executableName, NULL, NULL, new_argv, environ)) {
return 0;
};
return 1;
}
}
static void *resolveByName(const char *name) { static void *resolveByName(const char *name) {
if (strcmp(name, "__set_app_type") == 0) return (void *) msvcrt::__set_app_type; if (strcmp(name, "__set_app_type") == 0) return (void *) msvcrt::__set_app_type;
@ -59,7 +105,28 @@ static void *resolveByName(const char *name) {
if (strcmp(name, "_initterm") == 0) return (void *) msvcrt::_initterm; if (strcmp(name, "_initterm") == 0) return (void *) msvcrt::_initterm;
if (strcmp(name, "__getmainargs") == 0) return (void *) msvcrt::__getmainargs; if (strcmp(name, "__getmainargs") == 0) return (void *) msvcrt::__getmainargs;
if (strcmp(name, "setbuf") == 0) return (void *) msvcrt::setbuf; if (strcmp(name, "setbuf") == 0) return (void *) msvcrt::setbuf;
if (strcmp(name, "_spawnvp") == 0) return (void *) msvcrt::_spawnvp;
char* (*wibo_strchr)(char* str, int c) = strchr;
// 1:1 mappings with linux funcs
if (strcmp(name, "exit") == 0) return (void *) exit;
if (strcmp(name, "fopen") == 0) return (void *) fopen;
if (strcmp(name, "fclose") == 0) return (void *) fclose;
if (strcmp(name, "free") == 0) return (void *) free;
if (strcmp(name, "getenv") == 0) return (void *) getenv; if (strcmp(name, "getenv") == 0) return (void *) getenv;
if (strcmp(name, "malloc") == 0) return (void *) malloc;
if (strcmp(name, "memcpy") == 0) return (void *) memcpy;
if (strcmp(name, "memmove") == 0) return (void *) memmove;
if (strcmp(name, "memset") == 0) return (void *) memset;
if (strcmp(name, "strcat") == 0) return (void *) strcat;
if (strcmp(name, "strchr") == 0) return (void *) wibo_strchr;
if (strcmp(name, "strcmp") == 0) return (void *) strcmp;
if (strcmp(name, "strncmp") == 0) return (void *) strncmp;
if (strcmp(name, "strcpy") == 0) return (void *) strcpy;
if (strcmp(name, "strlen") == 0) return (void *) strlen;
if (strcmp(name, "strncpy") == 0) return (void *) strncpy;
if (strcmp(name, "strtoul") == 0) return (void *) strtoul;
return nullptr; return nullptr;
} }