From 8e39b3ab56422fa2f5e65aa67fabd928f40c5e32 Mon Sep 17 00:00:00 2001
From: XingLi <lixing@loongson.cn>
Date: Thu, 27 Jul 2023 09:36:23 +0800
Subject: [PATCH 3/3] LoongArch: Further refine the condition to enable static
 PIE

Before GCC r13-2728, it would produce a normal dynamic-linked executable
with -static-pie.  I mistakely believed it would produce a static-linked
executable, so failed to detect the breakage.  Then with Binutils 2.40
and (vanilla) GCC 12, libc_cv_static_pie_on_loongarch is mistakenly
enabled and cause a building failure with "undefined reference to
_DYNAMIC".

Fix the issue by disabling static PIE if -static-pie creates something
with a INTERP header.

From: Xi Ruoyao <xry111@xry111.site>
Date: Mon, 27 Feb 2023 19:08:09 +0800
---
 sysdeps/loongarch/configure    | 6 ++++++
 sysdeps/loongarch/configure.ac | 1 +
 2 files changed, 7 insertions(+)

diff --git a/sysdeps/loongarch/configure b/sysdeps/loongarch/configure
index 4f0ac252..9fec0a73 100644
--- a/sysdeps/loongarch/configure
+++ b/sysdeps/loongarch/configure
@@ -48,6 +48,12 @@ EOF
   (eval $ac_try) 2>&5
   ac_status=$?
   $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; } \
+     && ! { ac_try='LC_ALL=C $READELF -Wl conftest1 | grep -q INTERP'
+  { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
   test $ac_status = 0; }; } \
      && { ac_try='${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS -shared -fPIC -o conftest2.so conftest2.S'
   { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
diff --git a/sysdeps/loongarch/configure.ac b/sysdeps/loongarch/configure.ac
index bfe4e0f2..6d0b4d0d 100644
--- a/sysdeps/loongarch/configure.ac
+++ b/sysdeps/loongarch/configure.ac
@@ -40,6 +40,7 @@ EOF
   libc_cv_static_pie_on_loongarch=no
   if AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS -static-pie -nostdlib -fPIE -o conftest1 conftest1.S]) \
      && AC_TRY_COMMAND([LC_ALL=C $READELF -Wr conftest1 | grep -q R_LARCH_RELATIVE]) \
+     && ! AC_TRY_COMMAND([LC_ALL=C $READELF -Wl conftest1 | grep -q INTERP]) \
      && AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS -shared -fPIC -o conftest2.so conftest2.S]) \
      && AC_TRY_COMMAND([LC_ALL=C $READELF -Wr conftest2.so | grep -q 'R_LARCH_JUMP_SLOT.*external_func'])
   then
-- 
2.27.0

