From d412c455642284288f66c26c52a71b05a68efb3f Mon Sep 17 00:00:00 2001
From: Adrien Destugues <pulkomandy@pulkomandy.tk>
Date: Tue, 29 Dec 2020 15:33:18 +0100
Subject: Rebase Haiku patches from ghc 7.10


diff --git a/compiler/main/DriverPipeline.hs b/compiler/main/DriverPipeline.hs
index 40e6a8d..71a1725 100644
--- a/compiler/main/DriverPipeline.hs
+++ b/compiler/main/DriverPipeline.hs
@@ -2227,6 +2227,9 @@ joinObjectFiles dflags o_files output_fn = do
                           then [SysTools.Option "-no-pie"]
                           else [])
 
+                     ++ (if osInfo == OSHaiku
+                          then [SysTools.Option "-r"]
+                          else [])
                      ++ (if any (cc ==) [Clang, AppleClang, AppleClang51]
                           then []
                           else [SysTools.Option "-nodefaultlibs"])
diff --git a/compiler/main/DynFlags.hs b/compiler/main/DynFlags.hs
index f5f5f00..0c03454 100644
--- a/compiler/main/DynFlags.hs
+++ b/compiler/main/DynFlags.hs
@@ -4020,6 +4020,7 @@ default_PIC :: Platform -> [GeneralFlag]
 default_PIC platform =
   case (platformOS platform, platformArch platform) of
     (OSDarwin, ArchX86_64) -> [Opt_PIC]
+    (OSHaiku, _)           -> [Opt_PIC]
     (OSOpenBSD, ArchX86_64) -> [Opt_PIC] -- Due to PIE support in
                                          -- OpenBSD since 5.3 release
                                          -- (1 May 2013) we need to
diff --git a/configure.ac b/configure.ac
index 8fc0dc6..6b87fc7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1091,7 +1091,7 @@ dnl ** Use MMAP in the runtime linker?
 dnl --------------------------------------------------------------
 
 case ${TargetOS} in
-    linux|linux-android|freebsd|dragonfly|netbsd|openbsd|kfreebsdgnu|gnu|solaris2)
+    linux|linux-android|freebsd|dragonfly|netbsd|openbsd|kfreebsdgnu|gnu|solaris2|haiku)
         RtsLinkerUseMmap=1
         ;;
     darwin)
diff --git a/libraries/Cabal/Cabal/Distribution/Simple/InstallDirs.hs b/libraries/Cabal/Cabal/Distribution/Simple/InstallDirs.hs
index 14a2bf8..3fcf1a9 100644
--- a/libraries/Cabal/Cabal/Distribution/Simple/InstallDirs.hs
+++ b/libraries/Cabal/Cabal/Distribution/Simple/InstallDirs.hs
@@ -183,6 +183,7 @@ defaultInstallDirs' False comp userInstall _hasLibs = do
       else case buildOS of
            Windows -> do windowsProgramFilesDir <- getWindowsProgramFilesDir
                          return (windowsProgramFilesDir </> "Haskell")
+           Haiku -> return "/boot/system/non-packaged"
            _       -> return "/usr/local"
   installLibDir <-
       case buildOS of
@@ -208,17 +209,22 @@ defaultInstallDirs' False comp userInstall _hasLibs = do
       flibdir      = "$libdir",
       libexecdir   = case buildOS of
         Windows   -> "$prefix" </> "$libname"
+        Haiku     -> "$libdir"
         _other    -> "$prefix" </> "libexec",
       includedir   = "$libdir" </> "$libsubdir" </> "include",
       datadir      = case buildOS of
         Windows   -> "$prefix"
         _other    -> "$prefix" </> "share",
       datasubdir   = "$abi" </> "$pkgid",
-      docdir       = "$datadir" </> "doc" </> "$abi" </> "$pkgid",
-      mandir       = "$datadir" </> "man",
+      docdir       = case buildOS of
+        Haiku -> "$prefix" </> "documentation" </> "ghc"
+        _ -> "$datadir" </> "doc" </> "$abi" </> "$pkgid",
+      mandir       = "$docdir" </> "man",
       htmldir      = "$docdir"  </> "html",
       haddockdir   = "$htmldir",
-      sysconfdir   = "$prefix" </> "etc"
+      sysconfdir   = case buildOS of
+        Haiku -> "boot" </> "home" </> "config" </> "settings"
+        _ -> "$prefix" </> "etc"
   }
 
 -- ---------------------------------------------------------------------------
diff --git a/libraries/Cabal/Cabal/Distribution/Simple/PreProcess.hs b/libraries/Cabal/Cabal/Distribution/Simple/PreProcess.hs
index 6835cf5..f0f51aa 100644
--- a/libraries/Cabal/Cabal/Distribution/Simple/PreProcess.hs
+++ b/libraries/Cabal/Cabal/Distribution/Simple/PreProcess.hs
@@ -580,6 +580,7 @@ platformDefines lbi =
       OpenBSD   -> ["openbsd"]
       NetBSD    -> ["netbsd"]
       DragonFly -> ["dragonfly"]
+      Haiku     -> ["haiku"]
       Solaris   -> ["solaris2"]
       AIX       -> ["aix"]
       HPUX      -> ["hpux"]
diff --git a/libraries/Cabal/Cabal/Distribution/System.hs b/libraries/Cabal/Cabal/Distribution/System.hs
index aa25624..4e635de 100644
--- a/libraries/Cabal/Cabal/Distribution/System.hs
+++ b/libraries/Cabal/Cabal/Distribution/System.hs
@@ -74,7 +74,7 @@ data ClassificationStrictness = Permissive | Compat | Strict
 -- ------------------------------------------------------------
 
 -- | These are the known OS names: Linux, Windows, OSX
---  ,FreeBSD, OpenBSD, NetBSD, DragonFly
+--  ,FreeBSD, OpenBSD, NetBSD, DragonFly, Haiku
 --  ,Solaris, AIX, HPUX, IRIX
 --  ,HaLVM ,Hurd ,IOS, Android,Ghcjs
 --
@@ -87,7 +87,7 @@ data ClassificationStrictness = Permissive | Compat | Strict
 --
 data OS = Linux | Windows | OSX        -- tier 1 desktop OSs
         | FreeBSD | OpenBSD | NetBSD   -- other free Unix OSs
-        | DragonFly
+        | DragonFly | Haiku
         | Solaris | AIX | HPUX | IRIX  -- ageing Unix OSs
         | HaLVM                        -- bare metal / VMs / hypervisors
         | Hurd                         -- GNU's microkernel
@@ -100,7 +100,7 @@ instance Binary OS
 
 knownOSs :: [OS]
 knownOSs = [Linux, Windows, OSX
-           ,FreeBSD, OpenBSD, NetBSD, DragonFly
+           ,FreeBSD, OpenBSD, NetBSD, DragonFly, Haiku
            ,Solaris, AIX, HPUX, IRIX
            ,HaLVM
            ,Hurd
@@ -111,6 +111,7 @@ osAliases :: ClassificationStrictness -> OS -> [String]
 osAliases Permissive Windows = ["mingw32", "win32", "cygwin32"]
 osAliases Compat     Windows = ["mingw32", "win32"]
 osAliases _          OSX     = ["darwin"]
+osAliases _          Haiku   = ["haiku"]
 osAliases _          Hurd    = ["gnu"]
 osAliases Permissive FreeBSD = ["kfreebsdgnu"]
 osAliases Compat     FreeBSD = ["kfreebsdgnu"]
diff --git a/libraries/directory/System/Directory.hs b/libraries/directory/System/Directory.hs
index f4475c4..83bb3ab 100644
--- a/libraries/directory/System/Directory.hs
+++ b/libraries/directory/System/Directory.hs
@@ -1872,6 +1872,9 @@ getAppUserDataDirectory appName = do
 #if defined(mingw32_HOST_OS)
     s <- Win32.sHGetFolderPath nullPtr Win32.cSIDL_APPDATA nullPtr 0
     return (s++'\\':appName)
+#elif defined(haiku_HOST_OS)
+    path <- getEnv "HOME"
+    return (path ++ "/config/settings/" ++ appName)
 #else
     path <- getEnv "HOME"
     return (path++'/':'.':appName)
diff --git a/rts/LinkerInternals.h b/rts/LinkerInternals.h
index 76497df..b6c6d8c 100644
--- a/rts/LinkerInternals.h
+++ b/rts/LinkerInternals.h
@@ -299,7 +299,7 @@ char *cstring_from_section_name(
 #endif
 
 /* Which object file format are we targetting? */
-#if defined(linux_HOST_OS) || defined(solaris2_HOST_OS) || defined(freebsd_HOST_OS) || defined(kfreebsdgnu_HOST_OS) || defined(dragonfly_HOST_OS) || defined(netbsd_HOST_OS) || defined(openbsd_HOST_OS) || defined(gnu_HOST_OS)
+#if defined(linux_HOST_OS) || defined(solaris2_HOST_OS) || defined(freebsd_HOST_OS) || defined(kfreebsdgnu_HOST_OS) || defined(dragonfly_HOST_OS) || defined(netbsd_HOST_OS) || defined(openbsd_HOST_OS) || defined(gnu_HOST_OS) || defined(haiku_HOST_OS)
 #  define OBJFORMAT_ELF
 #elif defined (mingw32_HOST_OS)
 #  define OBJFORMAT_PEi386
diff --git a/rts/linker/Elf.c b/rts/linker/Elf.c
index df35604..1c70941 100644
--- a/rts/linker/Elf.c
+++ b/rts/linker/Elf.c
@@ -4,7 +4,7 @@
 || defined(linux_android_HOST_OS) \
 || defined(freebsd_HOST_OS) || defined(kfreebsdgnu_HOST_OS) \
 || defined(dragonfly_HOST_OS) || defined(netbsd_HOST_OS) \
-|| defined(openbsd_HOST_OS) || defined(gnu_HOST_OS)
+|| defined(openbsd_HOST_OS) || defined(gnu_HOST_OS) || defined(haiku_HOST_OS)
 
 #include "RtsUtils.h"
 #include "RtsSymbolInfo.h"
@@ -98,6 +98,26 @@
 #    define R_X86_64_PC64 24
 #  endif
 
+#  ifndef SHT_INIT_ARRAY
+#    define SHT_INIT_ARRAY 14
+#  endif
+
+#ifndef R_386_NONE
+#define R_386_NONE		0
+#define R_386_32		1	/* add symbol value */
+#define R_386_PC32		2	/* add PC relative symbol value */
+#define R_386_GOT32		3	/* add PC relative GOT offset */
+#define R_386_PLT32		4	/* add PC relative PLT offset */
+#define R_386_COPY		5	/* copy data from shared object */
+#define R_386_GLOB_DAT	6	/* set GOT entry to data address */
+#define R_386_JMP_SLOT	7	/* set GOT entry to code address */
+#define R_386_RELATIVE	8	/* add load address of shared object */
+#define R_386_GOTOFF	9	/* add GOT relative symbol address */
+#define R_386_GOTPC		10	/* add PC relative GOT table address */
+#define R_386_TLS_DTPMOD32	35
+#define R_386_TLS_DTPOFF32	36
+#endif
+
 /*
  * Workaround for libc implementations (e.g. eglibc) with incomplete
  * relocation lists
diff --git a/rts/posix/OSMem.c b/rts/posix/OSMem.c
index d25aaf8..f4590e1 100644
--- a/rts/posix/OSMem.c
+++ b/rts/posix/OSMem.c
@@ -191,6 +191,11 @@ my_mmap (void *addr, W_ size, int operation)
             errno = ENOMEM;
         }
     }
+#elif haiku_HOST_OS
+    ret = mmap(addr, size, prot, flags | MAP_ANON | MAP_PRIVATE, -1, 0);
+    // Retry without address constraint
+    if (ret == (void *)-1 && errno == EFAULT)
+        ret = mmap(0, size, prot, MAP_ANON | MAP_PRIVATE, -1, 0);
 #else
     ret = mmap(addr, size, prot, flags | MAP_ANON | MAP_PRIVATE, -1, 0);
 #endif
diff --git a/rules/library-path.mk b/rules/library-path.mk
index dbfd4be..49e04ae 100644
--- a/rules/library-path.mk
+++ b/rules/library-path.mk
@@ -15,6 +15,8 @@ ifeq "$(TargetOS_CPP)" "mingw32"
 prependLibraryPath = $(error Do not know how to prependLibraryPath on Windows)
 else ifeq "$(TargetOS_CPP)" "darwin"
 prependLibraryPath = export DYLD_LIBRARY_PATH="$1$${DYLD_LIBRARY_PATH:+:$$DYLD_LIBRARY_PATH}"
+else ifeq "$(TargetOS_CPP)" "haiku"
+prependLibraryPath = export LIBRARY_PATH="$1:$$LIBRARY_PATH"
 else
 prependLibraryPath = export LD_LIBRARY_PATH="$1$${LD_LIBRARY_PATH:+:$$LD_LIBRARY_PATH}"
 endif
-- 
2.28.0

