public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/3] LoongArch: Clean up after Binutils minimal version bump
@ 2023-08-26 16:36 Xi Ruoyao
  2023-08-26 16:36 ` [PATCH 1/3] LoongArch: Simplify the autoconf check for static PIE Xi Ruoyao
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Xi Ruoyao @ 2023-08-26 16:36 UTC (permalink / raw)
  To: libc-alpha; +Cc: Adhemerval Zanella Netto, caiyinyu, WANG Xuerui, Xi Ruoyao

Now we require Binutils >= 2.41, so we can take the advantage and make
some code simpler.

Xi Ruoyao (3):
  LoongArch: Simplify the autoconf check for static PIE
  LoongArch: Remove support code for old linker in start.S
  LoongArch: Micro-optimize LD_PCREL

 sysdeps/loongarch/configure                   | 36 ++++---------------
 sysdeps/loongarch/configure.ac                | 30 +++++-----------
 sysdeps/loongarch/start.S                     | 19 ++--------
 .../unix/sysv/linux/loongarch/pointer_guard.h | 10 +++---
 4 files changed, 23 insertions(+), 72 deletions(-)

-- 
2.42.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] LoongArch: Simplify the autoconf check for static PIE
  2023-08-26 16:36 [PATCH 0/3] LoongArch: Clean up after Binutils minimal version bump Xi Ruoyao
@ 2023-08-26 16:36 ` Xi Ruoyao
  2023-08-26 16:36 ` [PATCH 2/3] LoongArch: Remove support code for old linker in start.S Xi Ruoyao
  2023-08-26 16:36 ` [PATCH 3/3] LoongArch: Micro-optimize LD_PCREL Xi Ruoyao
  2 siblings, 0 replies; 4+ messages in thread
From: Xi Ruoyao @ 2023-08-26 16:36 UTC (permalink / raw)
  To: libc-alpha; +Cc: Adhemerval Zanella Netto, caiyinyu, WANG Xuerui, Xi Ruoyao

We are strictly requiring GAS >= 2.41 now, so we don't need to check
assembler capability anymore.

Signed-off-by: Xi Ruoyao <xry111@xry111.site>
---
 sysdeps/loongarch/configure    | 36 +++++++---------------------------
 sysdeps/loongarch/configure.ac | 30 +++++++++-------------------
 2 files changed, 16 insertions(+), 50 deletions(-)

diff --git a/sysdeps/loongarch/configure b/sysdeps/loongarch/configure
index 395ddc92ca..30b60d1983 100644
--- a/sysdeps/loongarch/configure
+++ b/sysdeps/loongarch/configure
@@ -4,21 +4,19 @@
 printf "%s\n" "#define HIDDEN_VAR_NEEDS_DYNAMIC_RELOC 1" >>confdefs.h
 
 
-{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking if the toolchain is sufficient to build static PIE on LoongArch" >&5
-printf %s "checking if the toolchain is sufficient to build static PIE on LoongArch... " >&6; }
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking if ${CC-cc} is sufficient to build static PIE on LoongArch" >&5
+printf %s "checking if ${CC-cc} is sufficient to build static PIE on LoongArch... " >&6; }
 if test ${libc_cv_static_pie_on_loongarch+y}
 then :
   printf %s "(cached) " >&6
 else $as_nop
 
-  cat > conftest1.S <<\EOF
+  cat > conftest.S <<\EOF
 .global _start
 .type _start, @function
 _start:
   li.w $a7, 93
-  /* This ensures the assembler supports explicit reloc.  */
-  pcalau12i $a0, %pc_hi20(x)
-  ld.w $a0, $a0, %pc_lo12(x)
+  li.w $a0, 0
   syscall 0
 
 .data
@@ -27,41 +25,21 @@ 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 conftest1 conftest1.S'
-  { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
-  (eval $ac_try) 2>&5
-  ac_status=$?
-  printf "%s\n" "$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=$?
-  printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
-  test $ac_status = 0; }; } \
-     && ! { ac_try='LC_ALL=C $READELF -Wl conftest1 | grep -q INTERP'
+  if { ac_try='${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS -static-pie -nostdlib -fPIE -o conftest conftest.S'
   { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
   (eval $ac_try) 2>&5
   ac_status=$?
   printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
   test $ac_status = 0; }; } \
-     && { ac_try='${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS -shared -nostdlib -fPIC -o conftest2.so conftest2.S'
+     && { ac_try='LC_ALL=C $READELF -Wr conftest | grep -q R_LARCH_RELATIVE'
   { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
   (eval $ac_try) 2>&5
   ac_status=$?
   printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
   test $ac_status = 0; }; } \
-     && { ac_try='LC_ALL=C $READELF -Wr conftest2.so | grep -q 'R_LARCH_JUMP_SLOT.*external_func''
+     && ! { ac_try='LC_ALL=C $READELF -Wl conftest | grep -q INTERP'
   { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
   (eval $ac_try) 2>&5
   ac_status=$?
diff --git a/sysdeps/loongarch/configure.ac b/sysdeps/loongarch/configure.ac
index 989287c6d2..28a8ae5486 100644
--- a/sysdeps/loongarch/configure.ac
+++ b/sysdeps/loongarch/configure.ac
@@ -8,19 +8,17 @@ AC_DEFINE(HIDDEN_VAR_NEEDS_DYNAMIC_RELOC)
 dnl Test if the toolchain is new enough for static PIE.
 dnl We need a GAS supporting explicit reloc (older GAS produces stack-based
 dnl reloc and triggers an internal error in the linker).  And, we need GCC to
-dnl pass the correct linker flags for static PIE.  GCC >= 13 and GAS >= 2.40
-dnl satisfy 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],
+dnl pass the correct linker flags for static PIE.  We strictly require GAS >=
+dnl 2.41 so we don't need to check the assembler capability, but we need to
+dnl check if GCC is doing the correct thing.
+AC_CACHE_CHECK([if ${CC-cc} is sufficient to build static PIE on LoongArch],
 libc_cv_static_pie_on_loongarch, [
-  cat > conftest1.S <<\EOF
+  cat > conftest.S <<\EOF
 .global _start
 .type _start, @function
 _start:
   li.w $a7, 93
-  /* This ensures the assembler supports explicit reloc.  */
-  pcalau12i $a0, %pc_hi20(x)
-  ld.w $a0, $a0, %pc_lo12(x)
+  li.w $a0, 0
   syscall 0
 
 .data
@@ -29,21 +27,11 @@ 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_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 -nostdlib -fPIC -o conftest2.so conftest2.S]) \
-     && AC_TRY_COMMAND([LC_ALL=C $READELF -Wr conftest2.so | grep -q 'R_LARCH_JUMP_SLOT.*external_func'])
+  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]) \
+     && ! AC_TRY_COMMAND([LC_ALL=C $READELF -Wl conftest | grep -q INTERP])
   then
     libc_cv_static_pie_on_loongarch=yes
   fi
-- 
2.42.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/3] LoongArch: Remove support code for old linker in start.S
  2023-08-26 16:36 [PATCH 0/3] LoongArch: Clean up after Binutils minimal version bump Xi Ruoyao
  2023-08-26 16:36 ` [PATCH 1/3] LoongArch: Simplify the autoconf check for static PIE Xi Ruoyao
@ 2023-08-26 16:36 ` Xi Ruoyao
  2023-08-26 16:36 ` [PATCH 3/3] LoongArch: Micro-optimize LD_PCREL Xi Ruoyao
  2 siblings, 0 replies; 4+ messages in thread
From: Xi Ruoyao @ 2023-08-26 16:36 UTC (permalink / raw)
  To: libc-alpha; +Cc: Adhemerval Zanella Netto, caiyinyu, WANG Xuerui, Xi Ruoyao

We are requiring Binutils >= 2.41, so la.pcrel always works here.

Signed-off-by: Xi Ruoyao <xry111@xry111.site>
---
 sysdeps/loongarch/start.S | 19 +++----------------
 1 file changed, 3 insertions(+), 16 deletions(-)

diff --git a/sysdeps/loongarch/start.S b/sysdeps/loongarch/start.S
index e9d82033b7..bf6bfc9ea4 100644
--- a/sysdeps/loongarch/start.S
+++ b/sysdeps/loongarch/start.S
@@ -60,20 +60,7 @@ ENTRY (ENTRY_POINT)
 	cfi_undefined (1)
 	or		a5, a0, zero /* rtld_fini */
 
-#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.
-   __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
-/* 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
-
-	LA		a0, t0, main
+	la.pcrel	a0, t0, main
 	REG_L		a1, sp, 0
 	ADDI		a2, sp, SZREG
 
@@ -84,9 +71,9 @@ ENTRY (ENTRY_POINT)
 	move		a4, zero /* used to be fini */
 	or		a6, sp, zero /* stack_end */
 
-	LA		ra, t0, __libc_start_main
+	la.pcrel	ra, t0, __libc_start_main
 	jirl		ra, ra, 0
 
-	LA		ra, t0, abort
+	la.pcrel	ra, t0, abort
 	jirl		ra, ra, 0
 END (ENTRY_POINT)
-- 
2.42.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 3/3] LoongArch: Micro-optimize LD_PCREL
  2023-08-26 16:36 [PATCH 0/3] LoongArch: Clean up after Binutils minimal version bump Xi Ruoyao
  2023-08-26 16:36 ` [PATCH 1/3] LoongArch: Simplify the autoconf check for static PIE Xi Ruoyao
  2023-08-26 16:36 ` [PATCH 2/3] LoongArch: Remove support code for old linker in start.S Xi Ruoyao
@ 2023-08-26 16:36 ` Xi Ruoyao
  2 siblings, 0 replies; 4+ messages in thread
From: Xi Ruoyao @ 2023-08-26 16:36 UTC (permalink / raw)
  To: libc-alpha; +Cc: Adhemerval Zanella Netto, caiyinyu, WANG Xuerui, Xi Ruoyao

We are requiring Binutils >= 2.41, so explicit relocation syntax is
always supported by the assembler.  Use it to reduce one instruction.

Signed-off-by: Xi Ruoyao <xry111@xry111.site>
---
 sysdeps/unix/sysv/linux/loongarch/pointer_guard.h | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/loongarch/pointer_guard.h b/sysdeps/unix/sysv/linux/loongarch/pointer_guard.h
index b25e353b37..d6c78687d4 100644
--- a/sysdeps/unix/sysv/linux/loongarch/pointer_guard.h
+++ b/sysdeps/unix/sysv/linux/loongarch/pointer_guard.h
@@ -19,17 +19,15 @@
 #ifndef POINTER_GUARD_H
 #define POINTER_GUARD_H
 
-/* Load a got-relative EXPR into G, using T.
-   Note G and T are register names.  */
+/* Load a got-relative EXPR into register G.  */
 #define LD_GLOBAL(G, EXPR) \
   la.global G,  EXPR; \
   REG_L     G,  G,  0;
 
-/* Load a pc-relative EXPR into G, using T.
-   Note G and T are register names.  */
+/* Load a pc-relative EXPR into register G.  */
 #define LD_PCREL(G, EXPR) \
-  la.pcrel  G,  EXPR; \
-  REG_L     G,  G,  0;
+  pcalau12i G, %pc_hi20(EXPR); \
+  REG_L     G, G, %pc_lo12(EXPR);
 
 #if (IS_IN (rtld) \
      || (!defined SHARED && (IS_IN (libc) \
-- 
2.42.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-08-26 16:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-26 16:36 [PATCH 0/3] LoongArch: Clean up after Binutils minimal version bump Xi Ruoyao
2023-08-26 16:36 ` [PATCH 1/3] LoongArch: Simplify the autoconf check for static PIE Xi Ruoyao
2023-08-26 16:36 ` [PATCH 2/3] LoongArch: Remove support code for old linker in start.S Xi Ruoyao
2023-08-26 16:36 ` [PATCH 3/3] LoongArch: Micro-optimize LD_PCREL Xi Ruoyao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).