From d1fe9ab6ad02d21dba81413609d027a5d37d7ca0 Mon Sep 17 00:00:00 2001
From: Cheng Shao <terrorjack@type.dance>
Date: Fri, 31 May 2024 19:35:44 +0000
Subject: [PATCH] rts: remove legacy i386 windows code paths

This commit removes some legacy i386 windows related code paths in the
RTS, given this target is no longer supported.
---
 rts/Interpreter.c    |  2 --
 rts/RtsStartup.c     |  2 +-
 rts/RtsSymbols.c     | 19 ++++----------
 rts/linker/PEi386.c  | 60 ++------------------------------------------
 rts/win32/veh_excn.c | 10 --------
 5 files changed, 8 insertions(+), 85 deletions(-)

diff --git a/rts/Interpreter.c b/rts/Interpreter.c
index 9b1dc5162189..2f9b1ad935cc 100644
--- a/rts/Interpreter.c
+++ b/rts/Interpreter.c
@@ -35,10 +35,8 @@
 //      want declspec(__dllimport__) on the front of function prototypes
 //      from libffi.
 #if defined(mingw32_HOST_OS)
-#if (defined(i386_HOST_ARCH) && !defined(__PIC__)) || defined(x86_64_HOST_ARCH)
 # define LIBFFI_NOT_DLL
 #endif
-#endif
 
 #include "rts/ghc_ffi.h"
 
diff --git a/rts/RtsStartup.c b/rts/RtsStartup.c
index 02e763d0c10d..344b88eee411 100644
--- a/rts/RtsStartup.c
+++ b/rts/RtsStartup.c
@@ -88,7 +88,7 @@ static void flushStdHandles(void);
 static void
 x86_init_fpu ( void )
 {
-#if defined(mingw32_HOST_OS) && !X86_INIT_FPU
+#if defined(mingw32_HOST_OS) && defined(x86_64_HOST_ARCH) && !X86_INIT_FPU
     /* Mingw-w64 does a stupid thing. They set the FPU precision to extended mode by default.
     The reasoning is that it's for compatibility with GNU Linux ported libraries. However the
     problem is this is incompatible with the standard Windows double precision mode.  In fact,
diff --git a/rts/RtsSymbols.c b/rts/RtsSymbols.c
index c4dbaaf80a6b..65c3298e2e08 100644
--- a/rts/RtsSymbols.c
+++ b/rts/RtsSymbols.c
@@ -84,12 +84,6 @@ extern char **environ;
 #if defined(mingw32_HOST_OS)
 #define RTS_POSIX_ONLY_SYMBOLS  /**/
 
-#if defined(i386_HOST_ARCH)
-#define RTS_WIN32_ONLY(X) X
-#else
-#define RTS_WIN32_ONLY(X) /**/
-#endif
-
 #if defined(x86_64_HOST_ARCH)
 #define RTS_WIN64_ONLY(X) X
 #else
@@ -170,19 +164,16 @@ extern char **environ;
       SymI_HasProto(rts_IOManagerIsWin32Native)          \
       SymI_HasProto(rts_ConsoleHandlerDone)              \
       SymI_NeedsProto(__mingw_module_is_dll)             \
-      RTS_WIN32_ONLY(SymI_NeedsProto(___chkstk_ms))      \
       RTS_WIN64_ONLY(SymI_NeedsProto(___chkstk_ms))      \
-      RTS_WIN64_ONLY(SymI_HasProto(__stdio_common_vswprintf_s)) \
-      RTS_WIN64_ONLY(SymI_HasProto(__stdio_common_vswprintf)) \
-      RTS_WIN64_ONLY(SymI_HasProto(_errno))  \
+      SymI_HasProto(__stdio_common_vswprintf_s)          \
+      SymI_HasProto(__stdio_common_vswprintf)            \
+      SymI_HasProto(_errno)                              \
       /* see Note [Symbols for MinGW's printf] */        \
       SymI_HasProto(_lock_file)                          \
       SymI_HasProto(_unlock_file)                        \
       /* See Note [_iob_func symbol] */                  \
-      RTS_WIN64_ONLY(SymI_HasProto_redirect(             \
-         __imp___acrt_iob_func, __rts_iob_func, STRENGTH_WEAK, SYM_TYPE_INDIRECT_DATA))   \
-      RTS_WIN32_ONLY(SymI_HasProto_redirect(             \
-         __imp____acrt_iob_func, __rts_iob_func, STRENGTH_WEAK, SYM_TYPE_INDIRECT_DATA))
+      SymI_HasProto_redirect(                            \
+         __imp___acrt_iob_func, __rts_iob_func, STRENGTH_WEAK, SYM_TYPE_INDIRECT_DATA)
 #else
 #define RTS_MINGW_ONLY_SYMBOLS /**/
 #endif
diff --git a/rts/linker/PEi386.c b/rts/linker/PEi386.c
index 0b205f9d6e73..9a0105b85b16 100644
--- a/rts/linker/PEi386.c
+++ b/rts/linker/PEi386.c
@@ -346,12 +346,6 @@
 
 #include "Rts.h"
 
-#if defined(x86_64_HOST_ARCH)
-#define USED_IF_x86_64_HOST_ARCH    /* Nothing */
-#else
-#define USED_IF_x86_64_HOST_ARCH    STG_UNUSED
-#endif
-
 #if defined(mingw32_HOST_OS)
 
 #include "RtsUtils.h"
@@ -1131,15 +1125,6 @@ get_name_string (uint8_t* name, ObjectCode* oc)
     }
 }
 
-#if !defined(x86_64_HOST_ARCH)
-static void
-zapTrailingAtSign ( SymbolName* sym )
-{
-  char* lst = strrchr (sym, '@');
-  if (lst) lst[0]='\0';
-}
-#endif
-
 SymbolAddr*
 lookupSymbolInDLLs ( const SymbolName* lbl, ObjectCode *dependent )
 {
@@ -1202,12 +1187,7 @@ static bool
 verifyCOFFHeader ( uint16_t machine, IMAGE_FILE_HEADER *hdr,
                    pathchar *fileName )
 {
-#if defined(i386_HOST_ARCH)
-   if (machine != IMAGE_FILE_MACHINE_I386) {
-      errorBelch("%" PATH_FMT ": Not a x86 PE file.", fileName);
-      return false;
-   }
-#elif defined(x86_64_HOST_ARCH)
+#if defined(x86_64_HOST_ARCH)
    if (machine != IMAGE_FILE_MACHINE_AMD64) {
       errorBelch("%" PATH_FMT ": Not a x86_64 PE+ file.", fileName);
       return false;
@@ -2075,40 +2055,7 @@ ocResolve_PEi386 ( ObjectCode* oc )
          /* All supported relocations write at least 4 bytes */
          checkProddableBlock(oc, pP, 4);
          switch (reloc->Type) {
-#if defined(i386_HOST_ARCH)
-            case IMAGE_REL_I386_DIR32:
-            case IMAGE_REL_I386_DIR32NB:
-               *(uint32_t *)pP = S + A;
-               break;
-            case IMAGE_REL_I386_REL32:
-               /* Tricky.  We have to insert a displacement at
-                  pP which, when added to the PC for the _next_
-                  insn, gives the address of the target (S).
-                  Problem is to know the address of the next insn
-                  when we only know pP.  We assume that this
-                  literal field is always the last in the insn,
-                  so that the address of the next insn is pP+4
-                  -- hence the constant 4.
-                  Also I don't know if A should be added, but so
-                  far it has always been zero.
-
-                  SOF 05/2005: 'A' (old contents of *pP) have been observed
-                  to contain values other than zero (the 'wx' object file
-                  that came with wxhaskell-0.9.4; dunno how it was compiled..).
-                  So, add displacement to old value instead of asserting
-                  A to be zero. Fixes wxhaskell-related crashes, and no other
-                  ill effects have been observed.
-
-                  Update: the reason why we're seeing these more elaborate
-                  relocations is due to a switch in how the NCG compiles SRTs
-                  and offsets to them from info tables. SRTs live in .(ro)data,
-                  while info tables live in .text, causing GAS to emit REL32/DISP32
-                  relocations with non-zero values. Adding the displacement is
-                  the right thing to do.
-               */
-               *(uint32_t *)pP = ((uint32_t)S) + A - ((uint32_t)(size_t)pP) - 4;
-               break;
-#elif defined(x86_64_HOST_ARCH)
+#if defined(x86_64_HOST_ARCH)
             case 1: /* R_X86_64_64 (ELF constant 1) - IMAGE_REL_AMD64_ADDR64 (PE constant 1) */
                {
                    uint64_t A;
@@ -2325,9 +2272,6 @@ SymbolAddr *lookupSymbol_PEi386(SymbolName *lbl, ObjectCode *dependent, SymType
 
         SymbolAddr* sym;
 
-#if !defined(x86_64_HOST_ARCH)
-        zapTrailingAtSign ( lbl );
-#endif
         if (type) {
             // Unfortunately we can only assume that this is the case. Ideally
             // the user would have given us an import library, which would allow
diff --git a/rts/win32/veh_excn.c b/rts/win32/veh_excn.c
index ec992bb211bc..999580c7ef31 100644
--- a/rts/win32/veh_excn.c
+++ b/rts/win32/veh_excn.c
@@ -287,16 +287,6 @@ void generateStack (EXCEPTION_POINTERS* pExceptionPointers)
 
     stackFrame.AddrStack.Offset = context->Rsp;
     stackFrame.AddrStack.Mode = AddrModeFlat;
-#else
-    machineType = IMAGE_FILE_MACHINE_I386;
-    stackFrame.AddrPC.Offset = context->Eip;
-    stackFrame.AddrPC.Mode = AddrModeFlat;
-
-    stackFrame.AddrFrame.Offset = context->Ebp;
-    stackFrame.AddrFrame.Mode = AddrModeFlat;
-
-    stackFrame.AddrStack.Offset = context->Esp;
-    stackFrame.AddrStack.Mode = AddrModeFlat;
 #endif
     fprintf (stderr, "\n Attempting to reconstruct a stack trace...\n\n");
     if (!SymInitialize (GetCurrentProcess (), NULL, true))
-- 
GitLab