Skip to content

Commit bdffdcf

Browse files
angermanhamishmack
andauthored
Fix more aarch64-linux stuff. Take 2 (#2116)
* Fix ghc:-dynamic-system-linker for android (cherry picked from commit 6c6c32b) * Set -dynamic-system-linker if musl and not x86_64 (cherry picked from commit 89b97d5) * add parens * Add iserv-syms.patch * Add iserv/proxy_args logic to linux as well (same as weindows) * Add keep cafs. * bump flakes * Let's try using it directly. * cleanup * bump iserv * ifdLevel 1 * ifdLevel 2 * ifdLevel 3 * Add static-external-plugin logic for cross compilers. * Fix musl copy * Fix x86_64-musl * initial iserv-proxy * Add deadbeef patches * Add pool allocator patch * Drop duplicates. * Fix alignment * ifdLevel 0 * ifdLevel 1 * ifdLevel 2 * ifdLevel 3 * Add more 9.6 patches 🤦 * Update patches * Add pool alignment and hidden support for 9.6 Pool alignment only for musl. * fixup! Set -dynamic-system-linker if musl and not x86_64 * Use gold with musl. Seems like the bug has been resolved. * Fixup patch * Extend patch range to cover 8.10 as well * apparently this doesn't apply to 9.8 :( * Sigh, ffs. gold still broken with the NCG. * Reduce allocation to 512MB not 1G * Fix up pool allocator stuff. * From 8.10, not 9.10 🤦 * rm -rf materialized/dummy-ghc * nix flake update iserv-proxy * Fix for non SECTION_POOL sections * Exclude pool related patches for non aarch64 target platforms * Avoid gold for llvm+musl builds * Use +dynamic-system-linker for aarch64 -> aarch64 musl * Include fixes for ghc 9.10 as well * Include fixes for ghc 9.10 as well --------- Co-authored-by: Hamish Mackenzie <[email protected]>
1 parent 8990a3f commit bdffdcf

24 files changed

+2456
-21
lines changed

compiler/ghc/default.nix

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,15 @@ let self =
6767
, useLdGold ?
6868
# might be better check to see if cc is clang/llvm?
6969
# use gold as the linker on linux to improve link times
70-
# do not use it on musl due to a ld.gold bug. See: <https://sourceware.org/bugzilla/show_bug.cgi?id=22266>.
71-
(stdenv.targetPlatform.isLinux && !stdenv.targetPlatform.isAndroid && !stdenv.targetPlatform.isMusl)
70+
# do not use ld.gold 2.3 with musl due to a ld.gold bug.
71+
# See: <https://sourceware.org/bugzilla/show_bug.cgi?id=22266>.
72+
# Note that this bug was resolved in 2017.
73+
( stdenv.targetPlatform.isLinux
74+
# don't use gold on android.
75+
&& !stdenv.targetPlatform.isAndroid
76+
# don't use gold with with musl. Still seems to be
77+
# affected by 22266.
78+
&& !stdenv.targetPlatform.isMusl)
7279

7380
, ghc-version ? src-spec.version
7481
, ghc-version-date ? null
@@ -314,6 +321,11 @@ let
314321
# `-fexternal-dynamic-refs` causes `undefined reference` errors when building GHC cross compiler for windows
315322
+ lib.optionalString (enableRelocatedStaticLibs && targetPlatform.isx86_64 && !targetPlatform.isWindows)
316323
" '*.*.ghc.*.opts += -fexternal-dynamic-refs'"
324+
# The fact that we need to set this here is pretty idiotic. GHC should figure this out on it's own.
325+
# Either have a runtime flag/setting to disable it or if dlopen fails, remember that it failed and
326+
# fall back to non-dynamic. We only have dynamic linker with musl if host and target arch match.
327+
+ lib.optionalString (targetPlatform.isAndroid || (targetPlatform.isMusl && haskell-nix.haskellLib.isCrossTarget))
328+
" '*.ghc.cabal.configure.opts += --flags=-dynamic-system-linker'"
317329
# The following is required if we build on aarch64-darwin for aarch64-iOS. Otherwise older
318330
# iPhones/iPads/... won't understand the compiled code, as the compiler will emit LDSETALH
319331
# + lib.optionalString (targetPlatform.???) "'*.rts.ghc.c.opts += -optc-mcpu=apple-a7 -optc-march=armv8-a+norcpc'"

flake.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

overlays/bootstrap.nix

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,34 @@ in {
269269
++ final.lib.optional (versionAtLeast "9.6" && versionLessThan "9.6.4" && final.stdenv.targetPlatform.isAarch64) ./patches/ghc/2adc050857a9c1b992040fbfd55fbe65b2851b19.patch
270270
++ final.lib.optional (versionAtLeast "8.10.7" && versionLessThan "9.0" && final.stdenv.targetPlatform.isAarch64 && final.stdenv.targetPlatform.isMusl && final.stdenv.targetPlatform != final.stdenv.hostPlatform) ./patches/ghc/ghc-8.10-aarch64-musl-gettimeofday.patch
271271

272+
# This one will lead to segv's on darwin, when calling `strlen` during lookupStrHashTable. `strlen` ends up being called with 0x0.
273+
# This patch will allow adding additional symbols to iserv, instead of having to patch them into GHC all the time.
274+
++ final.lib.optional (versionAtLeast "9.6.1" && versionLessThan"9.11" && (final.stdenv.targetPlatform != final.stdenv.hostPlatform) && (final.stdenv.targetPlatform.isAndroid || final.stdenv.targetPlatform.isLinux) && (final.stdenv.targetPlatform.isAarch64 || final.stdenv.targetPlatform.is32bit)) ./patches/ghc/iserv-syms.patch
275+
# Allow loading static external plugins into cross compilers
276+
++ final.lib.optional (versionAtLeast "9.6.1" && versionLessThan"9.11" && (final.stdenv.targetPlatform != final.stdenv.hostPlatform)) ./patches/ghc/5c80a27488acfe3610ddfcb99a1e961002e386d0.patch
277+
++ final.lib.optional (versionAtLeast "9.6.1" && versionLessThan"9.11" && (final.stdenv.targetPlatform != final.stdenv.hostPlatform)) ./patches/ghc/f8beb54a1d5725bd0d8a4b0a909d1b41d742b50b.patch
278+
++ final.lib.optional (versionAtLeast "8.10" && versionLessThan "9.11" && (final.stdenv.targetPlatform.isAndroid && final.stdenv.targetPlatform.is32bit || final.stdenv.targetPlatform.isMusl)) ./patches/ghc/ghc-9.6-missing-symbols-deadbeef.patch
279+
++ final.lib.optional (versionAtLeast "9.6" && versionLessThan "9.11" && final.stdenv.targetPlatform.isMusl && final.stdenv.targetPlatform.isAarch64) ./patches/ghc/ghc-9.6-linker-pool-allocator.patch
280+
++ final.lib.optional (versionAtLeast "9.6" && versionLessThan "9.11" && final.stdenv.targetPlatform.isMusl && final.stdenv.targetPlatform.isAarch64) ./patches/ghc/ghc-9.6-linker-pool-allocator-2.patch
281+
282+
++ final.lib.optional (versionAtLeast "9.6" && versionLessThan "9.8" && final.stdenv.targetPlatform.isMusl) ./patches/ghc/ghc-9.6-0001-Refactor-IServ.hs.patch
283+
++ final.lib.optional (versionAtLeast "9.6" && versionLessThan "9.11" && final.stdenv.targetPlatform.isMusl) ./patches/ghc/ghc-9.6-0002-Drop-spurious-8-byte-offset-from-elf_plt.patch
284+
++ final.lib.optional (versionAtLeast "9.6" && versionLessThan "9.11" && final.stdenv.targetPlatform.isMusl && final.stdenv.targetPlatform.isAarch64) ./patches/ghc/ghc-9.6-0003-Better-pool-alignment.-We-still-hardcode-section-ali.patch
285+
++ final.lib.optional (versionAtLeast "9.6" && versionLessThan "9.11" && final.stdenv.targetPlatform.isMusl && final.stdenv.targetPlatform.isAarch64) ./patches/ghc/ghc-9.6-0007-fixup-Better-pool-alignment.-We-still-hardcode-secti.patch
286+
++ final.lib.optional (versionAtLeast "9.6" && versionLessThan "9.11" && final.stdenv.targetPlatform.isMusl && final.stdenv.targetPlatform.isAarch64) ./patches/ghc/ghc-9.6-0008-pool-improvements.patch
287+
# these two are abit questionable. They are pretty rough, and assume static binary as well as posix.
288+
# ++ final.lib.optional (versionAtLeast "9.6" && versionLessThan "9.11" && final.stdenv.targetPlatform.isMusl) ./patches/ghc/ghc-9.6-0004-ghcidladdr.patch
289+
# ++ final.lib.optional (versionAtLeast "9.6" && versionLessThan "9.11" && final.stdenv.targetPlatform.isMusl) ./patches/ghc/ghc-9.6-0005-Better-interpreter-debugging.-Needs-ghcidladdr.patch
290+
272291
# Fix docs/users_guide/rtd-theme/layout.html to work with sphinx 7
273292
++ fromUntil "9.0" "9.8" ./patches/ghc/docs-sphinx-7.patch
274293
++ fromUntil "9.8" "9.9" ./patches/ghc/docs-sphinx-7-ghc98.patch
275294

276295
# These two patches are needed for libblst, which has now hidden symbols, which the linker doesn't know how to deal with.
277296
++ final.lib.optional (versionAtLeast "8.10" && versionLessThan "8.11") ./patches/ghc/ghc-8.10-0006-Adds-support-for-Hidden-symbols.patch
278297
++ final.lib.optional (versionAtLeast "8.10" && versionLessThan "8.11") ./patches/ghc/ghc-8.10-0006-Adds-support-for-Hidden-symbols-2.patch
279-
++ final.lib.optional (versionAtLeast "9.6" && versionLessThan "9.8" && (final.stdenv.targetPlatform.isWindows || final.stdenv.targetPlatform.isMusl)) ./patches/ghc/ghc-9.6-0006-Adds-support-for-Hidden-symbols.patch
280-
++ final.lib.optional (versionAtLeast "9.6" && versionLessThan "9.8" && (final.stdenv.targetPlatform.isWindows || final.stdenv.targetPlatform.isMusl)) ./patches/ghc/ghc-9.6-0006-Adds-support-for-Hidden-symbols-2.patch
298+
++ final.lib.optional (versionAtLeast "9.6" && versionLessThan "9.11" && (final.stdenv.targetPlatform.isWindows || final.stdenv.targetPlatform.isMusl)) ./patches/ghc/ghc-9.6-0006-Adds-support-for-Hidden-symbols.patch
299+
++ final.lib.optional (versionAtLeast "9.6" && versionLessThan "9.11" && (final.stdenv.targetPlatform.isWindows || final.stdenv.targetPlatform.isMusl)) ./patches/ghc/ghc-9.6-0006-Adds-support-for-Hidden-symbols-2.patch
281300
++ fromUntil "9.9" "9.12" ./patches/ghc/ghc-9.9-Cabal-3.11.patch
282301
++ fromUntil "9.8" "9.9" ./patches/ghc/ghc-9.8-text-upper-bound.patch
283302
++ fromUntil "9.10" "9.12" ./patches/ghc/ghc-9.10-containers-upper-bound.patch

overlays/linux-cross.nix

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,20 @@ let
2525
in
2626
writeShellScriptBin ("iserv-wrapper" + lib.optionalString enableProfiling "-prof") ''
2727
set -euo pipefail
28+
ISERV_ARGS=''${ISERV_ARGS:-}
29+
PROXY_ARGS=''${PROXY_ARGS:-}
2830
# Unset configure flags as configure should have run already
2931
unset configureFlags
3032
PORT=$((5000 + $RANDOM % 5000))
3133
(>&2 echo "---> Starting ${interpreter.exeName} on port $PORT")
32-
${qemu}/bin/qemu-${qemuSuffix} ${interpreter.override (lib.optionalAttrs hostPlatform.isAndroid { setupBuildFlags = ["--ghc-option=-optl-static" ];})}/bin/${interpreter.exeName} tmp $PORT &
34+
${qemu}/bin/qemu-${qemuSuffix} ${interpreter.override
35+
(lib.optionalAttrs hostPlatform.isAndroid {
36+
setupBuildFlags = ["--ghc-option=-optl-static" ] ++ lib.optional hostPlatform.isAarch32 "--ghc-option=-optl-no-pie";
37+
enableDebugRTS = true;
38+
})}/bin/${interpreter.exeName} tmp $PORT $ISERV_ARGS &
3339
(>&2 echo "---| ${interpreter.exeName} should have started on $PORT")
3440
RISERV_PID="$!"
35-
${iserv-proxy}/bin/iserv-proxy $@ 127.0.0.1 "$PORT"
41+
${iserv-proxy}/bin/iserv-proxy $@ 127.0.0.1 "$PORT" $PROXY_ARGS
3642
(>&2 echo "---> killing ${interpreter.exeName}...")
3743
kill $RISERV_PID
3844
'';
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
From 5c80a27488acfe3610ddfcb99a1e961002e386d0 Mon Sep 17 00:00:00 2001
2+
From: Luite Stegeman <[email protected]>
3+
Date: Fri, 8 Dec 2023 11:24:05 +0100
4+
Subject: [PATCH] Allow stage1 compilers to load plugins with -fplugin-library
5+
6+
This exposes the GHCi.ObjLink module even if no internal
7+
interpreter is available. This may be unsafe.
8+
---
9+
compiler/GHC/Driver/Plugins.hs | 9 +++------
10+
libraries/ghci/ghci.cabal.in | 2 +-
11+
2 files changed, 4 insertions(+), 7 deletions(-)
12+
13+
diff --git a/compiler/GHC/Driver/Plugins.hs b/compiler/GHC/Driver/Plugins.hs
14+
index ddf47c05ce4..2f3bf44b408 100644
15+
--- a/compiler/GHC/Driver/Plugins.hs
16+
+++ b/compiler/GHC/Driver/Plugins.hs
17+
@@ -1,7 +1,7 @@
18+
{-# LANGUAGE RankNTypes #-}
19+
{-# LANGUAGE CPP #-}
20+
21+
-#if defined(HAVE_INTERNAL_INTERPRETER) && defined(CAN_LOAD_DLL)
22+
+#if defined(CAN_LOAD_DLL)
23+
{-# LANGUAGE MagicHash #-}
24+
{-# LANGUAGE LambdaCase #-}
25+
{-# LANGUAGE UnboxedTuples #-}
26+
@@ -103,7 +103,7 @@ import qualified Data.Semigroup
27+
28+
import Control.Monad
29+
30+
-#if defined(HAVE_INTERNAL_INTERPRETER) && defined(CAN_LOAD_DLL)
31+
+#if defined(CAN_LOAD_DLL)
32+
import GHCi.ObjLink
33+
import GHC.Exts (addrToAny#, Ptr(..))
34+
import GHC.Utils.Encoding
35+
@@ -372,10 +372,7 @@ defaultFrontendPlugin = FrontendPlugin { frontend = \_ _ -> return () }
36+
-- | Load external plugins
37+
loadExternalPlugins :: [ExternalPluginSpec] -> IO [ExternalPlugin]
38+
loadExternalPlugins [] = return []
39+
-#if !defined(HAVE_INTERNAL_INTERPRETER)
40+
-loadExternalPlugins _ = do
41+
- panic "loadExternalPlugins: can't load external plugins with GHC built without internal interpreter"
42+
-#elif !defined(CAN_LOAD_DLL)
43+
+#if !defined(CAN_LOAD_DLL)
44+
loadExternalPlugins _ = do
45+
panic "loadExternalPlugins: loading shared libraries isn't supported by this compiler"
46+
#else
47+
diff --git a/libraries/ghci/ghci.cabal.in b/libraries/ghci/ghci.cabal.in
48+
index 565b4198dea..6854a6bc561 100644
49+
--- a/libraries/ghci/ghci.cabal.in
50+
+++ b/libraries/ghci/ghci.cabal.in
51+
@@ -52,7 +52,6 @@ library
52+
exposed-modules:
53+
GHCi.Run
54+
GHCi.CreateBCO
55+
- GHCi.ObjLink
56+
GHCi.Signals
57+
GHCi.StaticPtrTable
58+
GHCi.TH
59+
@@ -63,6 +62,7 @@ library
60+
GHCi.InfoTable
61+
62+
exposed-modules:
63+
+ GHCi.ObjLink
64+
GHCi.BreakArray
65+
GHCi.BinaryArray
66+
GHCi.Message
67+
--
68+
GitLab
69+
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
From f8beb54a1d5725bd0d8a4b0a909d1b41d742b50b Mon Sep 17 00:00:00 2001
2+
From: Luite Stegeman <[email protected]>
3+
Date: Fri, 8 Dec 2023 12:12:20 +0100
4+
Subject: [PATCH] External plugins: try loading archive if loading dynamic
5+
library fails
6+
7+
---
8+
compiler/GHC/Driver/Plugins.hs | 27 ++++++++++-----------------
9+
1 file changed, 10 insertions(+), 17 deletions(-)
10+
11+
diff --git a/compiler/GHC/Driver/Plugins.hs b/compiler/GHC/Driver/Plugins.hs
12+
index 2f3bf44b408..8b06e8b16d5 100644
13+
--- a/compiler/GHC/Driver/Plugins.hs
14+
+++ b/compiler/GHC/Driver/Plugins.hs
15+
@@ -1,12 +1,9 @@
16+
{-# LANGUAGE RankNTypes #-}
17+
{-# LANGUAGE CPP #-}
18+
19+
-#if defined(CAN_LOAD_DLL)
20+
{-# LANGUAGE MagicHash #-}
21+
{-# LANGUAGE LambdaCase #-}
22+
{-# LANGUAGE UnboxedTuples #-}
23+
-#endif
24+
-
25+
26+
-- | Definitions for writing /plugins/ for GHC. Plugins can hook into
27+
-- several areas of the compiler. See the 'Plugin' type. These plugins
28+
@@ -103,11 +100,9 @@ import qualified Data.Semigroup
29+
30+
import Control.Monad
31+
32+
-#if defined(CAN_LOAD_DLL)
33+
import GHCi.ObjLink
34+
import GHC.Exts (addrToAny#, Ptr(..))
35+
import GHC.Utils.Encoding
36+
-#endif
37+
38+
39+
-- | Command line options gathered from the -PModule.Name:stuff syntax
40+
@@ -372,10 +367,6 @@ defaultFrontendPlugin = FrontendPlugin { frontend = \_ _ -> return () }
41+
-- | Load external plugins
42+
loadExternalPlugins :: [ExternalPluginSpec] -> IO [ExternalPlugin]
43+
loadExternalPlugins [] = return []
44+
-#if !defined(CAN_LOAD_DLL)
45+
-loadExternalPlugins _ = do
46+
- panic "loadExternalPlugins: loading shared libraries isn't supported by this compiler"
47+
-#else
48+
loadExternalPlugins ps = do
49+
-- initialize the linker
50+
initObjLinker RetainCAFs
51+
@@ -400,17 +391,19 @@ loadExternalPlugins ps = do
52+
53+
loadExternalPluginLib :: FilePath -> IO ()
54+
loadExternalPluginLib path = do
55+
- -- load library
56+
+ -- XXX we should probably use the filename to determine whether
57+
+ -- the plugin is an archive or dynamic lib
58+
+
59+
+ -- try loading it as a dynamic library
60+
loadDLL path >>= \case
61+
- Just errmsg -> pprPanic "loadExternalPluginLib"
62+
- (vcat [ text "Can't load plugin library"
63+
- , text " Library path: " <> text path
64+
- , text " Error : " <> text errmsg
65+
- ])
66+
- Nothing -> do
67+
+ Just _errmsg ->
68+
+ -- if that fails, try loading it as an archive
69+
+ loadArchive path >> resolve
70+
+ Nothing -> resolve
71+
+ where
72+
+ resolve = do
73+
-- resolve objects
74+
resolveObjs >>= \case
75+
True -> return ()
76+
False -> pprPanic "loadExternalPluginLib" (text "Unable to resolve objects for library: " <> text path)
77+
78+
-#endif
79+
--
80+
GitLab
81+

0 commit comments

Comments
 (0)