Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save jessicah/db223791c03edeea9743bf3c3733a8af to your computer and use it in GitHub Desktop.

Select an option

Save jessicah/db223791c03edeea9743bf3c3733a8af to your computer and use it in GitHub Desktop.
Haiku GHC
From 0f9e9f6776649031b01cb7ad2536e86f69a4293f Mon Sep 17 00:00:00 2001
From: Jessica Hamilton <[email protected]>
Date: Fri, 8 May 2026 22:08:51 +1200
Subject: [PATCH] Haiku: enough to build cross-compiler.
Based on !15231.
---
compiler/GHC/Driver/DynFlags.hs | 1 +
m4/ghc_tables_next_to_code.m4 | 3 +++
rts/LinkerInternals.h | 3 ++-
rts/configure.ac | 2 +-
rts/linker/Elf.c | 3 ++-
rts/posix/OSMem.c | 4 ++++
6 files changed, 13 insertions(+), 3 deletions(-)
diff --git a/compiler/GHC/Driver/DynFlags.hs b/compiler/GHC/Driver/DynFlags.hs
index 71189b15d2..9980dd1955 100644
--- a/compiler/GHC/Driver/DynFlags.hs
+++ b/compiler/GHC/Driver/DynFlags.hs
@@ -1361,6 +1361,7 @@ default_PIC platform =
-- of that. Subsequently we expect all code on aarch64/linux (and macOS) to
-- be built with -fPIC.
(OSDarwin, ArchAArch64) -> [Opt_PIC]
+ (OSHaiku, _) -> [Opt_PIC, Opt_ExternalDynamicRefs, Opt_PIE]
(OSLinux, ArchAArch64) -> [Opt_PIC, Opt_ExternalDynamicRefs]
(OSLinux, ArchARM {}) -> [Opt_PIC, Opt_ExternalDynamicRefs]
(OSLinux, ArchRISCV64 {}) -> [Opt_PIC, Opt_ExternalDynamicRefs]
diff --git a/m4/ghc_tables_next_to_code.m4 b/m4/ghc_tables_next_to_code.m4
index 9bf473a712..fdac1377a6 100644
--- a/m4/ghc_tables_next_to_code.m4
+++ b/m4/ghc_tables_next_to_code.m4
@@ -25,6 +25,9 @@ AC_DEFUN([GHC_TABLES_NEXT_TO_CODE],
if test "$TargetOS" = "mingw32" && test "$TargetArch" = "aarch64"; then
TablesNextToCodeDefault=NO
AC_MSG_RESULT([no])
+ elif test "$TargetOS" = "haiku" && test "$TargetArch" = "x86_64"; then
+ TablesNextToCodeDefault=NO
+ AC_MSG_RESULT([no])
else
TablesNextToCodeDefault=YES
AC_MSG_RESULT([yes])
diff --git a/rts/LinkerInternals.h b/rts/LinkerInternals.h
index 8bc34f13e1..9a21aacdb1 100644
--- a/rts/LinkerInternals.h
+++ b/rts/LinkerInternals.h
@@ -25,7 +25,8 @@ void printLoadedObjects(void);
|| 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)
# define OBJFORMAT_ELF
#elif defined(mingw32_HOST_OS)
# define OBJFORMAT_PEi386
diff --git a/rts/configure.ac b/rts/configure.ac
index aba10311ed..7a4bc5e03a 100644
--- a/rts/configure.ac
+++ b/rts/configure.ac
@@ -286,7 +286,7 @@ dnl ** Use MMAP in the runtime linker?
dnl --------------------------------------------------------------
case ${HostOS} 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|ios|watchos|tvos)
diff --git a/rts/linker/Elf.c b/rts/linker/Elf.c
index ce4b519c61..9952e876b9 100644
--- a/rts/linker/Elf.c
+++ b/rts/linker/Elf.c
@@ -4,7 +4,8 @@
|| 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)
// It is essential that this is included before any <elf.h> is included. <elf.h>
// defines R_XXX relocations, which would interfere with the COMPAT_R_XXX
diff --git a/rts/posix/OSMem.c b/rts/posix/OSMem.c
index 9bc6c59cdc..a8d46b8311 100644
--- a/rts/posix/OSMem.c
+++ b/rts/posix/OSMem.c
@@ -246,6 +246,10 @@ my_mmap (void *addr, W_ size, int operation)
// See #7500.
ret = linux_retry_mmap(operation, size, ret, addr, prot, flags);
}
+# elif defined(haiku_HOST_OS)
+ // Retry without address constraint
+ if (ret == (void *)-1 && errno == EFAULT)
+ ret = mmap(0, size, prot, MAP_ANON | MAP_PRIVATE, -1, 0);
# endif
if (ret == MAP_FAILED) {
return NULL;
--
2.43.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment