From 5ae2554c104adedacc40129aa66aad8245df4c56 Mon Sep 17 00:00:00 2001
From: Justine Tunney <jtunney@gmail.com>
Date: Sun, 7 Jan 2024 11:37:18 -0800
Subject: [PATCH] Have cosmo_dlopen() request `-z execstack` on Linux

The AMD HIP SDK for Linux ships prebuilt DSOs with an RWX PT_GNU_STACK
since old versions of GCC made it nearly impossible to build artifacts
where that wasn't the case, however modern glibc systems will flat out
refuse to link RWX DSOs from an execuatble that uses PT_GNU_STACK = RW
---
 libc/dlopen/dlopen.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/libc/dlopen/dlopen.c b/libc/dlopen/dlopen.c
index 4263e6e55..bd00f696e 100644
--- a/libc/dlopen/dlopen.c
+++ b/libc/dlopen/dlopen.c
@@ -618,7 +618,15 @@ static dontinline bool foreign_compile(char exe[hasatleast PATH_MAX]) {
   }
   int pid, ws;
   char *args[] = {
-      "cc", "-pie", "-fPIC", src, "-o", tmp, IsNetbsd() ? 0 : "-ldl", 0,
+      "cc",
+      "-pie",
+      "-fPIC",
+      src,
+      "-o",
+      tmp,
+      IsLinux() ? "-Wl,-z,execstack" : "-DIGNORE",
+      IsNetbsd() ? 0 : "-ldl",
+      0,
   };
   errno_t err = posix_spawnp(&pid, args[0], NULL, NULL, args, environ);
   if (err) {