Skip to content

Commit 4819428

Browse files
committed
Add static-external-plugin logic for cross compilers.
1 parent 3feec6e commit 4819428

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

overlays/bootstrap.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ in {
278278
# This one will lead to segv's on darwin, when calling `strlen` during lookupStrHashTable. `strlen` ends up being called with 0x0.
279279
# This patch will allow adding additional symbols to iserv, instead of having to patch them into GHC all the time.
280280
++ final.lib.optional (versionAtLeast "9.6.1" && versionLessThan"9.10" && (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
281+
# Allow loading static external plugins into cross compilers
282+
++ final.lib.optional (versionAtLeast "9.6.1" && versionLessThan"9.10" && (final.stdenv.targetPlatform != final.stdenv.hostPlatform)) ./patches/ghc/5c80a27488acfe3610ddfcb99a1e961002e386d0.patch
283+
++ final.lib.optional (versionAtLeast "9.6.1" && versionLessThan"9.10" && (final.stdenv.targetPlatform != final.stdenv.hostPlatform)) ./patches/ghc/f8beb54a1d5725bd0d8a4b0a909d1b41d742b50b.patch
281284
# These two patches are needed for libblst, which has now hidden symbols, which the linker doesn't know how to deal with.
282285
++ final.lib.optional (versionAtLeast "8.10" && versionLessThan "8.11") ./patches/ghc/ghc-8.10-0006-Adds-support-for-Hidden-symbols.patch
283286
++ final.lib.optional (versionAtLeast "8.10" && versionLessThan "8.11") ./patches/ghc/ghc-8.10-0006-Adds-support-for-Hidden-symbols-2.patch
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)