[PATCH] LoongArch: Support pointer guard on LoongArch32

mengqinggang <[email protected]>
Newsgroups gmane.comp.lib.glibc.alpha
Message-ID <[email protected]>
LoongArch32 Reduced has no rotri.d/rotri.w instructions.
Use slli.w/srli.w/or to synthesize the rotation on LoongArch32
and LoongArch32 Reduced.

Reported-by: Haiyong Sun <[email protected]>
---
 sysdeps/loongarch/__longjmp.S                 |  4 +-
 sysdeps/loongarch/setjmp.S                    |  4 +-
 .../sysv/linux/loongarch/pointer_guard-asm.h  | 44 +++++++++++++------
 3 files changed, 34 insertions(+), 18 deletions(-)

diff --git a/sysdeps/loongarch/__longjmp.S b/sysdeps/loongarch/__longjmp.S
index 191f03320a..302cb6894a 100644
--- a/sysdeps/loongarch/__longjmp.S
+++ b/sysdeps/loongarch/__longjmp.S
@@ -23,9 +23,9 @@
 ENTRY (__longjmp)
 #ifdef PTR_MANGLE
 	REG_L t0, a0, 0*SZREG
-	PTR_DEMANGLE (ra, t0, t1)
+	PTR_DEMANGLE (ra, t0, t1, t2)
 	REG_L t0, a0, 1*SZREG
-	PTR_DEMANGLE2 (sp, t0, t1)
+	PTR_DEMANGLE2 (sp, t0, t1, t2)
 #else
 	REG_L ra, a0, 0*SZREG
 	REG_L sp, a0, 1*SZREG
diff --git a/sysdeps/loongarch/setjmp.S b/sysdeps/loongarch/setjmp.S
index 591e0d2709..58c1ee02b2 100644
--- a/sysdeps/loongarch/setjmp.S
+++ b/sysdeps/loongarch/setjmp.S
@@ -31,9 +31,9 @@ END (setjmp)
 
 ENTRY (__sigsetjmp)
 #ifdef PTR_MANGLE
-	PTR_MANGLE (t0, ra, t1)
+	PTR_MANGLE (t0, ra, t1, t2)
 	REG_S t0, a0, 0*SZREG
-	PTR_MANGLE2 (t0, sp, t1)
+	PTR_MANGLE2 (t0, sp, t1, t2)
 	REG_S t0, a0, 1*SZREG
 #else
 	REG_S ra, a0, 0*SZREG
diff --git a/sysdeps/unix/sysv/linux/loongarch/pointer_guard-asm.h b/sysdeps/unix/sysv/linux/loongarch/pointer_guard-asm.h
index 24e27e4092..006b713f9c 100644
--- a/sysdeps/unix/sysv/linux/loongarch/pointer_guard-asm.h
+++ b/sysdeps/unix/sysv/linux/loongarch/pointer_guard-asm.h
@@ -24,24 +24,40 @@
       || (!defined SHARED && (IS_IN (libc) \
       || IS_IN (libpthread))))
 #  define PTR_MANGLE_LOAD(guard) \
-  LOAD_LOCAL (guard, __pointer_chk_guard_local);
+	LOAD_LOCAL (guard, __pointer_chk_guard_local);
 # else
 #  define PTR_MANGLE_LOAD(guard) \
-  LOAD_GLOBAL (guard, __pointer_chk_guard);
+	LOAD_GLOBAL (guard, __pointer_chk_guard);
 # endif
-# define PTR_MANGLE(dst, src, guard) \
-  PTR_MANGLE_LOAD (guard); \
-  PTR_MANGLE2 (dst, src, guard);
-# define PTR_DEMANGLE(dst, src, guard) \
-  PTR_MANGLE_LOAD (guard); \
-  PTR_DEMANGLE2 (dst, src, guard);
+
+# define PTR_MANGLE(dst, src, guard, tmp) \
+	PTR_MANGLE_LOAD (guard); \
+	PTR_MANGLE2 (dst, src, guard, tmp);
+# define PTR_DEMANGLE(dst, src, guard, tmp) \
+	PTR_MANGLE_LOAD (guard); \
+	PTR_DEMANGLE2 (dst, src, guard, tmp);
+
+# if __loongarch_grlen == 64
 /* Use PTR_MANGLE2 for efficiency if guard is already loaded.  */
-# define PTR_MANGLE2(dst, src, guard) \
-  xor  dst, src, guard; \
-  rotri.d  dst, dst, 47;
-# define PTR_DEMANGLE2(dst, src, guard) \
-  rotri.d  src, src, 17; \
-  xor  dst, src, guard;
+#  define PTR_MANGLE2(dst, src, guard, tmp) \
+	xor	  tmp, src, guard; \
+	rotri.d	  dst, tmp, 47;
+#  define PTR_DEMANGLE2(dst, src, guard, tmp) \
+	rotri.d	  tmp, src, 17; \
+	xor	  dst, tmp, guard;
+# elif __loongarch_grlen == 32 /* __loongarch_grlen == 64 */
+#  define PTR_MANGLE2(dst, src, guard, tmp) \
+	xor	  tmp, src, guard; \
+	slli.w	  dst, tmp, 9; \
+	srli.w	  tmp, tmp, 23; \
+	or	  dst, dst, tmp;
+#  define PTR_DEMANGLE2(dst, src, guard, tmp) \
+	srli.w	  tmp, src, 9; \
+	slli.w	  src, src, 23; \
+	or	  tmp, tmp, src; \
+	xor	  dst, tmp, guard;
+# endif /* __loongarch_grlen == 64 */
+
 #endif
 
 #endif /* POINTER_GUARD_ASM_H */
-- 
2.34.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.