summaryrefslogtreecommitdiff
path: root/compiler/cbits
diff options
context:
space:
mode:
authorAndreas Klebinger <klebinger.andreas@gmx.at>2023-01-11 18:24:41 +0100
committerMarge Bot <ben+marge-bot@smart-cactus.org>2023-01-30 21:18:45 -0500
commit08ba87200ff068aa37cac082e61ee7e2d534daf5 (patch)
tree8247bc8d8a4357bfca9c43538417b512f45e7e9f /compiler/cbits
parentda468391872f6be286db37a0f016a37f9f362509 (diff)
downloadhaskell-08ba87200ff068aa37cac082e61ee7e2d534daf5.tar.gz
ghc-the-library: Retain cafs in both static in dynamic builds.
We use keepCAFsForGHCi.c to force -fkeep-cafs behaviour by using a __attribute__((constructor)) function. This broke for static builds where the linker discarded the object file since it was not reverenced from any exported code. We fix this by asserting that the flag is enabled using a function in the same module as the constructor. Which causes the object file to be retained by the linker, which in turn causes the constructor the be run in static builds. This changes nothing for dynamic builds using the ghc library. But causes static to also retain CAFs (as we expect them to). Fixes #22417. ------------------------- Metric Decrease: T21839r -------------------------
Diffstat (limited to 'compiler/cbits')
-rw-r--r--compiler/cbits/keepCAFsForGHCi.c26
1 files changed, 23 insertions, 3 deletions
diff --git a/compiler/cbits/keepCAFsForGHCi.c b/compiler/cbits/keepCAFsForGHCi.c
index ba635b0d95..9645f54c9b 100644
--- a/compiler/cbits/keepCAFsForGHCi.c
+++ b/compiler/cbits/keepCAFsForGHCi.c
@@ -1,15 +1,35 @@
#include <Rts.h>
+#include <ghcversion.h>
+// Note [keepCAFsForGHCi]
+// ~~~~~~~~~~~~~~~~~~~~~~
// This file is only included in the dynamic library.
// It contains an __attribute__((constructor)) function (run prior to main())
// which sets the keepCAFs flag in the RTS, before any Haskell code is run.
// This is required so that GHCi can use dynamic libraries instead of HSxyz.o
// files.
+//
+// For static builds we have to guarantee that the linker loads this object file
+// to ensure the constructor gets run and not discarded. If the object is part of
+// an archive and not otherwise referenced the linker would ignore the object.
+// To avoid this:
+// * When initializing a GHC session in initGhcMonad we assert keeping cafs has been
+// enabled by calling keepCAFsForGHCi.
+// * This causes the GHC module from the ghc package to carry a reference to this object
+// file.
+// * Which in turn ensures the linker doesn't discard this object file, causing
+// the constructor to be run, allowing the assertion to succeed in the first place
+// as keepCAFs will have been set already during initialization of constructors.
-static void keepCAFsForGHCi(void) __attribute__((constructor));
-static void keepCAFsForGHCi(void)
+
+bool keepCAFsForGHCi(void) __attribute__((constructor));
+
+bool keepCAFsForGHCi(void)
{
- keepCAFs = 1;
+ bool was_set = keepCAFs;
+ setKeepCAFs();
+ return was_set;
}
+