From ea8e49c870b3a604982398a429231bf67412e989 Mon Sep 17 00:00:00 2001 From: fgsfds Date: Fri, 4 Aug 2023 23:44:20 +0200 Subject: [PATCH] port: linux support; horrible hack to avoid segmented address funny moments linux support only tested a little bit in a VM --- Makefile.port | 204 +++++++++++++----------- include/PR/os_libc.h | 6 +- include/PR/{sched.h => ultrasched.h} | 0 include/PR/ultratypes.h | 8 + port/fast3d/gfx_sdl2.cpp | 3 + port/src/libultra.c | 7 +- port/src/main.c | 2 +- port/src/pdmain.c | 2 +- port/src/pdsched.c | 2 +- src/include/lib/sched.h | 2 +- src/include/{stdarg.h => libc_stdarg.h} | 10 ++ src/include/{stdlib.h => libc_stdlib.h} | 0 src/include/{string.h => libc_string.h} | 1 + src/include/types.h | 2 +- 14 files changed, 141 insertions(+), 108 deletions(-) rename include/PR/{sched.h => ultrasched.h} (100%) rename src/include/{stdarg.h => libc_stdarg.h} (95%) mode change 100755 => 100644 rename src/include/{stdlib.h => libc_stdlib.h} (100%) mode change 100755 => 100644 rename src/include/{string.h => libc_string.h} (98%) mode change 100755 => 100644 diff --git a/Makefile.port b/Makefile.port index 8361f2673..da0190fad 100644 --- a/Makefile.port +++ b/Makefile.port @@ -56,38 +56,38 @@ DEBUG = 0 # Detect host platform if not specified. ifeq ($(HOST_PLATFORM),) - ifeq ($(OS),Windows_NT) - # assume msys for now - UNAME_OS := $(shell uname -s) - ifneq (,$(findstring MINGW64,$(UNAME_OS))) - HOST_PLATFORM := x86_64-windows - else - HOST_PLATFORM := i686-windows - endif - else # TODO - UNAME_ARCH := $(shell uname -p) - UNAME_OS := $(shell uname -s) - UNAME_OS := `echo $(UNAME_OS) | tr A-Z a-z` - HOST_PLATFORM := $(UNAME_ARCH)-$(UNAME_OS) - endif + ifeq ($(OS),Windows_NT) + # assume msys for now + UNAME_OS := $(shell uname -s) + ifneq (,$(findstring MINGW64,$(UNAME_OS))) + HOST_PLATFORM := x86_64-windows + else + HOST_PLATFORM := i686-windows + endif + else # TODO + UNAME_ARCH := $(shell uname -p) + UNAME_OS := $(shell uname -s) + UNAME_OS := $(shell echo $(UNAME_OS) | tr A-Z a-z) + HOST_PLATFORM := $(UNAME_ARCH)-$(UNAME_OS) + endif endif $(info Host platform: $(HOST_PLATFORM)) # Set target platform to host platform if not specified. ifeq ($(TARGET_PLATFORM),) - TARGET_PLATFORM := $(HOST_PLATFORM) + TARGET_PLATFORM := $(HOST_PLATFORM) endif $(info Target platform: $(TARGET_PLATFORM)) # Set whether the target arch is 64- or 32-bit. ifneq (,$(findstring 64,$(TARGET_PLATFORM))) - # TODO: we're not 64-bit compatible yet - $(error 64-bit target platforms are not supported yet.) - TARGET_64BIT = 1 + # TODO: we're not 64-bit compatible yet + $(error 64-bit target platforms are not supported yet) + TARGET_64BIT = 1 else # TODO - TARGET_64BIT = 0 + TARGET_64BIT = 0 endif # Set whether the target is big-endian or little-endian. @@ -95,21 +95,21 @@ TARGET_BIG_ENDIAN = 0 # TODO # The VERSION constant is used in the source to handle version-specific code. ifeq ($(ROMID),ntsc-final) - VERSION = 2 + VERSION = 2 else - $(error ROM version $(VERSION) is not supported.) + $(error ROM version $(VERSION) is not supported) endif DEFINES := \ - VERSION=$(VERSION) \ - MATCHING=0 \ - PAL=0 \ - PIRACYCHECKS=0 \ - ROM_SIZE=$(ROM_SIZE) \ - _LANGUAGE_C=1 + VERSION=$(VERSION) \ + MATCHING=0 \ + PAL=0 \ + PIRACYCHECKS=0 \ + ROM_SIZE=$(ROM_SIZE) \ + _LANGUAGE_C=1 ifeq ($(DEBUG),1) - DEFINES := $(DEFINES) DEBUG=1 + DEFINES := $(DEFINES) DEBUG=1 endif C_DEFINES := $(foreach d,$(DEFINES),-D$(d)) @@ -119,19 +119,28 @@ B_DIR := build/$(ROMID)-port E_DIR := extracted/$(ROMID) ifneq (,$(findstring x86_64,$(TARGET_PLATFORM))) - TOOLCHAIN_ARCH := x86_64 + TOOLCHAIN_ARCH := x86_64 else # TODO - TOOLCHAIN_ARCH := i686 + TOOLCHAIN_ARCH := i686 endif ifneq (,$(findstring windows,$(TARGET_PLATFORM))) - TOOLCHAIN := $(TOOLCHAIN_ARCH)-w64-mingw32 - TARGET_LDFLAGS := $(shell pkg-config sdl2 --libs) -lz -lopengl32 - TARGET_CFLAGS := $(shell pkg-config sdl2 --cflags-only-I) + TOOLCHAIN := $(TOOLCHAIN_ARCH)-w64-mingw32 + TARGET_CFLAGS := $(shell pkg-config sdl2 --cflags-only-I) + TARGET_LDFLAGS := $(shell pkg-config sdl2 --libs) -lz -lopengl32 + # HACKHACKHACK: big one, done to avoid possible collisions with segmented addresses + # by default windows places imagebase for 32-bit EXEs at 0x00400000, so it could easily overflow 0x01000000 + # proper solution required instead of this bullshit, like patching all segmented pointers to have the LSB set + TARGET_CFLAGS += -fno-pie + TARGET_LDFLAGS += -Wl,-no-pie -Wl,--image-base=0x12000000 else # TODO - TOOLCHAIN := $(TOOLCHAIN_ARCH)-linux-gnu - TARGET_LDFLAGS := $(shell pkg-config sdl2 --libs) -lGL -lz - TARGET_CFLAGS := $(shell pkg-config sdl2 --cflags-only-I) + TOOLCHAIN := $(TOOLCHAIN_ARCH)-linux-gnu + TARGET_CFLAGS := $(shell pkg-config sdl2 --cflags-only-I) + TARGET_LDFLAGS := $(shell pkg-config sdl2 --libs) -lGL -lz + # HACKHACKHACK: see above + # I think 32-bit Linux USUALLY places imagebase at 0x08000000-ish, so it's already in rsp seg territory + TARGET_CFLAGS += -fno-pie + TARGET_LDFLAGS += -Wl,-no-pie -Wl,--section-start=.text=0x10000000 endif # These are still used in the port, but only to generate headers. @@ -141,41 +150,41 @@ TILES_JSON_FILES := $(shell find $(A_DIR) -path '*/tiles/*.json') C_FILES := $(shell find src/game port -name '*.c') C_FILES := $(C_FILES) \ - $(wildcard src/lib/mp3/*.c) \ - $(wildcard src/lib/naudio/*.c) \ - $(wildcard src/lib/ultra/audio/*.c) \ - $(wildcard src/lib/ultra/gu/*.c) \ - src/lib/ultra/io/vimodentsclan1.c \ - src/lib/ultra/io/vimodepallan1.c \ - src/lib/ultra/io/vimodempallan1.c \ - src/lib/ultra/io/vitbl.c \ - $(wildcard src/lib/a*.c) \ - src/lib/base.c \ - src/lib/collision.c \ - src/lib/debughud.c \ - src/lib/dma.c \ - src/lib/fault.c \ - src/lib/joy.c \ - $(wildcard src/lib/lib_*.c) \ - src/lib/mema.c \ - src/lib/memp.c \ - src/lib/model.c \ - src/lib/modelasm_c.c \ - src/lib/mp3.c \ - src/lib/mtx_c.c \ - src/lib/mtx.c \ - src/lib/music.c \ - src/lib/path.c \ - src/lib/profile.c \ - src/lib/rdp.c \ - src/lib/rmon.c \ - src/lib/rng_c.c \ - src/lib/rzip_c.c \ - src/lib/snd.c \ - src/lib/speaker.c \ - src/lib/varsinit.c \ - src/lib/vi.c \ - src/textureconfig.c + $(wildcard src/lib/mp3/*.c) \ + $(wildcard src/lib/naudio/*.c) \ + $(wildcard src/lib/ultra/audio/*.c) \ + $(wildcard src/lib/ultra/gu/*.c) \ + src/lib/ultra/io/vimodentsclan1.c \ + src/lib/ultra/io/vimodepallan1.c \ + src/lib/ultra/io/vimodempallan1.c \ + src/lib/ultra/io/vitbl.c \ + $(wildcard src/lib/a*.c) \ + src/lib/base.c \ + src/lib/collision.c \ + src/lib/debughud.c \ + src/lib/dma.c \ + src/lib/fault.c \ + src/lib/joy.c \ + $(wildcard src/lib/lib_*.c) \ + src/lib/mema.c \ + src/lib/memp.c \ + src/lib/model.c \ + src/lib/modelasm_c.c \ + src/lib/mp3.c \ + src/lib/mtx_c.c \ + src/lib/mtx.c \ + src/lib/music.c \ + src/lib/path.c \ + src/lib/profile.c \ + src/lib/rdp.c \ + src/lib/rmon.c \ + src/lib/rng_c.c \ + src/lib/rzip_c.c \ + src/lib/snd.c \ + src/lib/speaker.c \ + src/lib/varsinit.c \ + src/lib/vi.c \ + src/textureconfig.c CXX_FILES :=$(shell find port -name '*.cpp') @@ -185,39 +194,39 @@ CXX_O_FILES := $(patsubst %.cpp, $(B_DIR)/%.o, $(CXX_FILES)) O_FILES := $(C_O_FILES) $(CXX_O_FILES) INCLUDES = \ - -I include \ - -I include/PR \ - -I src/include \ - -I src/generated/$(ROMID) \ - -I src/lib/ultra/audio \ - -I port/include + -I include \ + -I include/PR \ + -I src/include \ + -I src/generated/$(ROMID) \ + -I src/lib/ultra/audio \ + -I port/include -CC := $(TOOLCHAIN)-gcc -std=gnu11 -CXX := $(TOOLCHAIN)-g++ -std=gnu++20 +CC := $(TOOLCHAIN)-gcc -std=c11 +CXX := $(TOOLCHAIN)-g++ -std=c++20 ifneq (,$(CXX_O_FILES)) - LD := $(CXX) + LD := $(CXX) else - LD := $(CC) + LD := $(CC) endif $(C_O_FILES): OPT_LVL := $(GCC_OPT_LVL) COMMON_CFLAGS := $(C_DEFINES) -DAVOID_UB=1 $(INCLUDES) $(TARGET_CFLAGS) -g \ - -fno-inline-functions \ - -fno-strict-aliasing \ - -funsigned-char \ - -fwrapv \ - -Wall \ - -Wno-address \ - -Wno-aggressive-loop-optimizations \ - -Wno-int-in-bool-context \ - -Wno-misleading-indentation \ - -Wno-missing-braces \ - -Wno-multichar \ - -Wno-tautological-compare \ - -Wno-unused-but-set-variable \ - -Wno-unused-value \ - -Wno-unused-variable + -fno-inline-functions \ + -fno-strict-aliasing \ + -funsigned-char \ + -fwrapv \ + -Wall \ + -Wno-address \ + -Wno-aggressive-loop-optimizations \ + -Wno-int-in-bool-context \ + -Wno-misleading-indentation \ + -Wno-missing-braces \ + -Wno-multichar \ + -Wno-tautological-compare \ + -Wno-unused-but-set-variable \ + -Wno-unused-value \ + -Wno-unused-variable CFLAGS := $(COMMON_CFLAGS) -Wno-pointer-sign CXXFLAGS := $(COMMON_CFLAGS) -Wno-unused-function @@ -251,4 +260,5 @@ allclean: # rm -rf src/generated codeclean: - find $(B_DIR)/game $(B_DIR)/inflate $(B_DIR)/lib -name '*.o' -delete + find $(B_DIR)/game $(B_DIR)/inflate $(B_DIR)/lib $(B_DIR)/port -name '*.o' -delete + diff --git a/include/PR/os_libc.h b/include/PR/os_libc.h index e210f87e5..23a13d72e 100644 --- a/include/PR/os_libc.h +++ b/include/PR/os_libc.h @@ -80,9 +80,9 @@ extern "C" { /* byte string operations */ -extern void bcopy(const void *, void *, int); -extern int bcmp(const void *, const void *, int); -extern void bzero(void *, int); +extern void bcopy(const void *, void *, size_t); +extern int bcmp(const void *, const void *, size_t); +extern void bzero(void *, size_t); /* Printf */ diff --git a/include/PR/sched.h b/include/PR/ultrasched.h similarity index 100% rename from include/PR/sched.h rename to include/PR/ultrasched.h diff --git a/include/PR/ultratypes.h b/include/PR/ultratypes.h index b8b9b0843..a38522075 100644 --- a/include/PR/ultratypes.h +++ b/include/PR/ultratypes.h @@ -57,6 +57,8 @@ typedef volatile long long vs64; /* signed 64-bit */ typedef float f32; /* single prec floating point */ typedef double f64; /* double prec floating point */ +#ifdef PLATFORM_N64 + #if !defined(_SIZE_T) && !defined(_SIZE_T_) && !defined(_SIZE_T_DEF) #define _SIZE_T #define _SIZE_T_DEF /* exeGCC size_t define label */ @@ -68,6 +70,12 @@ typedef unsigned long size_t; #endif #endif +#else + +#include + +#endif + #endif /* _LANGUAGE_C */ diff --git a/port/fast3d/gfx_sdl2.cpp b/port/fast3d/gfx_sdl2.cpp index 4f14cbce4..268e2a858 100644 --- a/port/fast3d/gfx_sdl2.cpp +++ b/port/fast3d/gfx_sdl2.cpp @@ -3,6 +3,9 @@ #ifdef __MINGW32__ // mingw has its nanosleep implementation in pthread.h for some reson #include +#else +#include +#include #endif #include "gfx_window_manager_api.h" diff --git a/port/src/libultra.c b/port/src/libultra.c index 6e551c8df..00e31e58a 100644 --- a/port/src/libultra.c +++ b/port/src/libultra.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -425,21 +426,21 @@ OSIntMask osSetIntMask(OSIntMask mask) /* libc compatibility wrappers */ #ifndef HAVE_BZERO -void bzero(void *ptr, s32 size) +void bzero(void *ptr, size_t size) { memset(ptr, 0, size); } #endif #ifndef HAVE_BCOPY -void bcopy(const void *src, void *dst, s32 n) +void bcopy(const void *src, void *dst, size_t n) { memcpy(dst, src, n); } #endif #ifndef HAVE_BCMP -s32 bcmp(const void *s1, const void *s2, s32 n) +s32 bcmp(const void *s1, const void *s2, size_t n) { return memcmp(s1, s2, n); } diff --git a/port/src/main.c b/port/src/main.c index 01a55c9a8..da07579a8 100644 --- a/port/src/main.c +++ b/port/src/main.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include "lib/main.h" diff --git a/port/src/pdmain.c b/port/src/pdmain.c index 85960adc2..7554507d6 100644 --- a/port/src/pdmain.c +++ b/port/src/pdmain.c @@ -2,7 +2,7 @@ #include #include -#include +#include #include "lib/sched.h" #include "lib/vars.h" #include "constants.h" diff --git a/port/src/pdsched.c b/port/src/pdsched.c index d8a9e49f9..bfb8b42e4 100644 --- a/port/src/pdsched.c +++ b/port/src/pdsched.c @@ -1,5 +1,5 @@ #include -#include +#include #include "lib/boot.h" #include "lib/sched.h" #include "constants.h" diff --git a/src/include/lib/sched.h b/src/include/lib/sched.h index da4c54d95..042672bde 100644 --- a/src/include/lib/sched.h +++ b/src/include/lib/sched.h @@ -1,7 +1,7 @@ #ifndef _IN_BOOT_SCHED_H #define _IN_BOOT_SCHED_H #include -#include +#include #include "types.h" void schedSetCrashEnable2(s32 enable); diff --git a/src/include/stdarg.h b/src/include/libc_stdarg.h old mode 100755 new mode 100644 similarity index 95% rename from src/include/stdarg.h rename to src/include/libc_stdarg.h index fc651f9b4..7828a7a0c --- a/src/include/stdarg.h +++ b/src/include/libc_stdarg.h @@ -1,6 +1,8 @@ #ifndef N64_STDARG_H #define N64_STDARG_H +#ifdef TARGET_N64 + // When not building with IDO, use the builtin vaarg macros for portability. #ifndef __sgi #define va_list __builtin_va_list @@ -39,4 +41,12 @@ typedef char *va_list; #define va_end(__list) #endif + +#else // TARGET_N64 + +#include_next +#warning fuck + +#endif + #endif diff --git a/src/include/stdlib.h b/src/include/libc_stdlib.h old mode 100755 new mode 100644 similarity index 100% rename from src/include/stdlib.h rename to src/include/libc_stdlib.h diff --git a/src/include/string.h b/src/include/libc_string.h old mode 100755 new mode 100644 similarity index 98% rename from src/include/string.h rename to src/include/libc_string.h index c017dd372..f886b1beb --- a/src/include/string.h +++ b/src/include/libc_string.h @@ -58,6 +58,7 @@ extern size_t strlen(const char *); #else +#include #include_next #endif diff --git a/src/include/types.h b/src/include/types.h index 1fc6be6af..a63199dfd 100644 --- a/src/include/types.h +++ b/src/include/types.h @@ -1,7 +1,7 @@ #ifndef _IN_TYPES_H #define _IN_TYPES_H #include -#include +#include #include "n_libaudio.h" #include "constants.h" #include "lang.h"