From d29ac2028078612c6b4c25fb5be857ca35b37a3b Mon Sep 17 00:00:00 2001
From: XingLi <lixing@loongson.cn>
Date: Thu, 27 Jul 2023 09:32:21 +0800
Subject: [PATCH 2/3] LoongArch: Fix the condition to use PC-relative
 addressing in start.S

A start.o compiled from start.S with -DPIC and no -DSHARED is used by
both crt1.o and rcrt1.o.  So the LoongArch static PIE patch
unintentionally introduced PC-relative addressing for main and
__libc_start_main into crt1.o.

While the latest Binutils (trunk, which will be released as 2.40)
supports the PC-relative relocs against an external function by creating
a PLT entry, the 2.39 release branch doesn't (and won't) support this.
An error is raised:

    "PLT stub does not represent and symbol not defined."

So, we need the following changes:

1. Check if ld supports the PC-relative relocs against an external
   function.  If it's not supported, we deem static PIE unsupported.
2. Change start.S.  If static PIE is supported, use PC-relative
   addressing for main and __libc_start_main and rely on the linker to
   create PLT entries.  Otherwise, restore the old behavior (using GOT
   to address these functions).

An alternative would be adding a new "static-pie-start.S", and some
custom logic into Makefile to build rcrt1.o with it.  And, restore
start.S to the state before static PIE change so crt1.o won't contain
PC-relative relocs against external symbols.  But I can't see any
benefit of this alternative, so I'd just keep it simple.

Tested by building glibc with the following configurations:

1. Binutils trunk + GCC trunk.  Static PIE enabled.  All tests
   passed.
2. Binutils 2.39 branch + GCC trunk.  Static PIE disabled.  Tests
   related to ifunc failed (it's a known issue).  All other tests
   passed.
3. Binutils 2.39 branch + GCC 12 branch, cross compilation with
   build-many-glibcs.py from x86_64-linux-gnu.  Static PIE disabled.
   Build succeeded.

From: Xi Ruoyao <xry111@xry111.site>
Date: Sun, 2 Oct 2022 22:23:09 +0800
---
 sysdeps/loongarch/configure    | 29 +++++++++++++++++++++++++----
 sysdeps/loongarch/configure.ac | 18 ++++++++++++++----
 sysdeps/loongarch/start.S      | 11 +++++++----
 3 files changed, 46 insertions(+), 12 deletions(-)

diff --git a/sysdeps/loongarch/configure b/sysdeps/loongarch/configure
index db6341e8..4f0ac252 100644
--- a/sysdeps/loongarch/configure
+++ b/sysdeps/loongarch/configure
@@ -11,7 +11,7 @@ if ${libc_cv_static_pie_on_loongarch+:} false; then :
   $as_echo_n "(cached) " >&6
 else
 
-  cat > conftest.S << EOF
+  cat > conftest1.S << EOF
 .global _start
 .type _start, @function
 _start:
@@ -27,14 +27,35 @@ x:
   /* This should produce an R_LARCH_RELATIVE in the static PIE.  */
   .dword _start
 EOF
+  cat > conftest2.S << EOF
+.global f
+.type f, @function
+f:
+  /* The linker should be able to handle this and produce a PLT entry.  */
+  la.pcrel \$t0, \$t0, external_func
+  jirl \$zero, \$t0, 0
+EOF
+
   libc_cv_static_pie_on_loongarch=no
-  if { ac_try='${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS -static-pie -nostdlib -fPIE -o conftest conftest.S'
+  if { ac_try='${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS -static-pie -nostdlib -fPIE -o conftest1 conftest1.S'
+  { { 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='LC_ALL=C $READELF -Wr conftest1 | grep -q R_LARCH_RELATIVE'
+  { { 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
   (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 -Wr conftest | grep -q R_LARCH_RELATIVE'
+     && { ac_try='LC_ALL=C $READELF -Wr conftest2.so | grep -q 'R_LARCH_JUMP_SLOT.*external_func''
   { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
   (eval $ac_try) 2>&5
   ac_status=$?
@@ -43,7 +64,7 @@ EOF
   then
     libc_cv_static_pie_on_loongarch=yes
   fi
-  rm -rf conftest.*
+  rm -rf conftest*
 fi
 { $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_cv_static_pie_on_loongarch" >&5
 $as_echo "$libc_cv_static_pie_on_loongarch" >&6; }
diff --git a/sysdeps/loongarch/configure.ac b/sysdeps/loongarch/configure.ac
index 603085cd..bfe4e0f2 100644
--- a/sysdeps/loongarch/configure.ac
+++ b/sysdeps/loongarch/configure.ac
@@ -13,7 +13,7 @@ dnl satisify the requirement, but a distro may backport static PIE support into
 dnl earlier GCC or Binutils releases as well.
 AC_CACHE_CHECK([if the toolchain is sufficient to build static PIE on LoongArch],
 libc_cv_static_pie_on_loongarch, [
-  cat > conftest.S << EOF
+  cat > conftest1.S << EOF
 .global _start
 .type _start, @function
 _start:
@@ -28,14 +28,24 @@ x:
   .word 0
   /* This should produce an R_LARCH_RELATIVE in the static PIE.  */
   .dword _start
+EOF
+  cat > conftest2.S << EOF
+.global f
+.type f, @function
+f:
+  /* The linker should be able to handle this and produce a PLT entry.  */
+  la.pcrel \$t0, \$t0, external_func
+  jirl \$zero, \$t0, 0
 EOF
   libc_cv_static_pie_on_loongarch=no
-  if AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS -static-pie -nostdlib -fPIE -o conftest conftest.S]) \
-     && AC_TRY_COMMAND([LC_ALL=C $READELF -Wr conftest | grep -q R_LARCH_RELATIVE])
+  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([${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
     libc_cv_static_pie_on_loongarch=yes
   fi
-  rm -rf conftest.*])
+  rm -rf conftest* ])
 
 if test "$libc_cv_static_pie_on_loongarch" = yes; then
   AC_DEFINE(SUPPORT_STATIC_PIE)
diff --git a/sysdeps/loongarch/start.S b/sysdeps/loongarch/start.S
index cb2b6c5b..11e4cede 100644
--- a/sysdeps/loongarch/start.S
+++ b/sysdeps/loongarch/start.S
@@ -44,13 +44,16 @@ ENTRY (ENTRY_POINT)
 	cfi_undefined (1)
 	or		a5, a0, zero /* rtld_fini */
 
-#if defined(PIC) && !defined(SHARED)
+#if ENABLE_STATIC_PIE
 /* For static PIE, the GOT cannot be used in _start because the GOT entries are
-   offsets instead of real addresses before __libc_start_main.  */
+   offsets instead of real addresses before __libc_start_main.
+   __libc_start_main and/or main may be not local, so we rely on the linker to
+   produce PLT entries for them.  GNU ld >= 2.40 supports this.  */
 # define LA la.pcrel
 #else
-/* We must get symbol main through GOT table, since main may not be local.
-   For instance: googletest defines main in dynamic library.  */
+/* Old GNU ld (< 2.40) cannot handle PC relative address against a non-local
+   function correctly.  We deem these old linkers failing to support static PIE
+   and load the addresses from GOT.  */
 # define LA la.got
 #endif
 
-- 
2.27.0

