From 0631e2b9453b33c42e12a14c11e8257d470016c5 Mon Sep 17 00:00:00 2001
From: Ismael Luceno <ismael@sourcemage.org>
Date: Sun, 11 Dec 2022 23:55:56 +0100
Subject: [PATCH] libgo: Add support for libucontext

Test and link to libucontext if detected; additionally: undefine
SETCONTEXT_CLOBBERS_TLS in runtime/proc.c to prevent the use of
makecontext/swapcontext, not supported by libucontext.

[ismael@iodev.co.uk: Fixed to work with glibc too, tests for libucontext
 properly; improved commit message]

Based on a patch by Sören Tempel <soeren+git@soeren-tempel.net>.

Upstream-Status: Pending
Origin: Unknown
Signed-off-by: Ismael Luceno <ismael@sourcemage.org>
---
 Makefile.in          |    2 ++
 libgo/runtime/proc.c |    6 ++++++
 2 files changed, 8 insertions(+)

diff --git a/Makefile.in b/Makefile.in
index 593495e1650..c3e5d489bc0 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -52801,6 +52801,8 @@ configure-target-libgo:
 	esac; \
 	module_srcdir=libgo; \
 	rm -f no-such-file || : ; \
+	LIBS="`printf 'int main(){return 0;}' | $(CC) -x c -o /dev/null \
+               - -lucontext && printf -lucontext` $$LIBS" \
 	CONFIG_SITE=no-such-file $(SHELL) \
 	  $$s/$$module_srcdir/configure \
 	  --srcdir=$${topdir}/$$module_srcdir \
diff --git a/libgo/runtime/proc.c b/libgo/runtime/proc.c
index 3a30748d329..a987678fa84 100644
--- a/libgo/runtime/proc.c
+++ b/libgo/runtime/proc.c
@@ -66,6 +66,12 @@ static void gscanstack(G*);
 
 __thread G *g __asm__(GOSYM_PREFIX "runtime.g");
 
+/* libucontext does not seem to support tlsbase, undef the macro
+ * here to make sure we define initcontext and fixcontext as dummies. */
+#if !defined(__GLIBC__) && defined(__linux__)
+#undef SETCONTEXT_CLOBBERS_TLS
+#endif
+
 #ifndef SETCONTEXT_CLOBBERS_TLS
 
 static inline void
