From e2b60be8472ad5b7b6ef2b265b4a06d1b2b67c7d Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Sat, 18 Dec 2021 19:29:13 -0500
Subject: [PATCH] rts: Consolidate RtsSymbols from libc

Previously (9ebda74ec5331911881d734b21fbb31c00a0a22f) `environ` was
added to `RtsSymbols` to ensure that environment was correctly
propagated when statically linking. However, this introduced #20577
since platforms are inconsistent in whether they provide a prototype for
`environ`. I fixed this by providing a prototype but while doing so
dropped symbol-table entry, presumably thinking that it was redundant
due to the entry in the mingw-specific table.

Here I reintroduce the symbol table entry for `environ` and move libc
symbols shared by Windows and Linux into a new macro,
`RTS_LIBC_SYMBOLS`, avoiding this potential confusion.
---
 rts/RtsSymbols.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/rts/RtsSymbols.c b/rts/RtsSymbols.c
index f76573e3ae18..894e9ff30578 100644
--- a/rts/RtsSymbols.c
+++ b/rts/RtsSymbols.c
@@ -72,7 +72,6 @@ extern char **environ;
       SymI_HasProto(signal_handlers)            \
       SymI_HasProto(stg_sig_install)            \
       SymI_HasProto(rtsTimerSignal)             \
-      SymI_HasProto_redirect(atexit, atexit, STRENGTH_STRONG) /* See Note [Strong symbols] */ \
       SymI_NeedsDataProto(nocldstop)
 #endif
 
@@ -166,7 +165,6 @@ extern char **environ;
       SymI_HasProto(stg_asyncDoProczh)                   \
       SymI_HasProto(rts_InstallConsoleEvent)             \
       SymI_HasProto(rts_ConsoleHandlerDone)              \
-      SymI_HasProto(atexit)                              \
       RTS_WIN32_ONLY(SymI_NeedsProto(___chkstk_ms))      \
       RTS_WIN64_ONLY(SymI_NeedsProto(___chkstk_ms))      \
       RTS_WIN32_ONLY(SymI_HasProto(_imp___environ))      \
@@ -1071,6 +1069,11 @@ extern char **environ;
 #define RTS_LIBGCC_SYMBOLS
 #endif
 
+// Symbols defined by libc
+#define RTS_LIBC_SYMBOLS                               \
+      SymI_HasProto_redirect(atexit, atexit, STRENGTH_STRONG) /* See Note [Strong symbols] */ \
+      SymI_HasProto(environ)
+
 #if !defined(DYNAMIC) && defined(linux_HOST_OS)
 // we need these for static musl builds. However when
 // linking shared objects (DLLs) this will fail, hence
@@ -1108,6 +1111,7 @@ RTS_POSIX_ONLY_SYMBOLS
 RTS_MINGW_ONLY_SYMBOLS
 RTS_DARWIN_ONLY_SYMBOLS
 RTS_OPENBSD_ONLY_SYMBOLS
+RTS_LIBC_SYMBOLS
 RTS_LIBGCC_SYMBOLS
 RTS_FINI_ARRAY_SYMBOLS
 RTS_LIBFFI_SYMBOLS
-- 
GitLab