From: =?UTF-8?q?Radek=20Barto=C5=88?= <radek.barton@microsoft.com>
Date: Thu, 24 Jul 2025 12:36:02 +0200
Subject: [PATCH v2] newlib: libc: return back support for AArch64 ILP32

This patch is returning back support for AArch64 ILP32 ABI that was
removed in de479a54e22e8fcb6262639a8e67fe8b00a27c37 commit but is needed
to ensure source code compatibility with GCC 14.

The change in newlib/libc/machine/aarch64/asmdefs.h makes it
out-of-the-sync with the current upstream implementation in
https://github.com/ARM-software/optimized-routines repository.

Signed-off-by: Radek Bartoň <radek.barton@microsoft.com>
---
 newlib/libc/machine/aarch64/asmdefs.h   | 48 +++++++++++++++++++------
 newlib/libc/machine/aarch64/memchr.S    |  2 ++
 newlib/libc/machine/aarch64/memcmp.S    |  4 +++
 newlib/libc/machine/aarch64/memcpy.S    |  3 ++
 newlib/libc/machine/aarch64/memrchr.S   |  1 +
 newlib/libc/machine/aarch64/memset.S    |  3 ++
 newlib/libc/machine/aarch64/strchr.S    |  1 +
 newlib/libc/machine/aarch64/strchrnul.S |  1 +
 newlib/libc/machine/aarch64/strcmp.S    |  2 ++
 newlib/libc/machine/aarch64/strcpy.S    |  2 ++
 newlib/libc/machine/aarch64/strlen.S    |  1 +
 newlib/libc/machine/aarch64/strncmp.S   |  3 ++
 newlib/libc/machine/aarch64/strnlen.S   |  2 ++
 newlib/libc/machine/aarch64/strrchr.S   |  1 +
 14 files changed, 64 insertions(+), 10 deletions(-)

diff --git a/newlib/libc/machine/aarch64/asmdefs.h b/newlib/libc/machine/aarch64/asmdefs.h
index 6c99fa704..2aaa459ee 100644
--- a/newlib/libc/machine/aarch64/asmdefs.h
+++ b/newlib/libc/machine/aarch64/asmdefs.h
@@ -30,18 +30,32 @@
 #define FEATURE_1_PAC 2
 
 /* Add a NT_GNU_PROPERTY_TYPE_0 note.  */
+#ifdef __ILP32__
 #define GNU_PROPERTY(type, value)	\
-  .section .note.gnu.property, "a"  SEP \
-  .p2align 3			    SEP \
-  .word 4			    SEP \
-  .word 16			    SEP \
-  .word 5			    SEP \
-  .asciz "GNU"			    SEP \
-  .word type			    SEP \
-  .word 4			    SEP \
-  .word value			    SEP \
-  .word 0			    SEP \
+  .section .note.gnu.property, "a";	\
+  .p2align 2;				\
+  .word 4;				\
+  .word 12;				\
+  .word 5;				\
+  .asciz "GNU";				\
+  .word type;				\
+  .word 4;				\
+  .word value;				\
   .text
+#else
+#define GNU_PROPERTY(type, value)	\
+  .section .note.gnu.property, "a";	\
+  .p2align 3;				\
+  .word 4;				\
+  .word 16;				\
+  .word 5;				\
+  .asciz "GNU";				\
+  .word type;				\
+  .word 4;				\
+  .word value;				\
+  .word 0;				\
+  .text
+#endif
 
 /* If set then the GNU Property Note section will be added to
    mark objects to support BTI and PAC-RET.  */
@@ -88,4 +102,18 @@ GNU_PROPERTY (FEATURE_1_AND, FEATURE_1_BTI|FEATURE_1_PAC)
 
 #define L(l) .L ## l
 
+#ifdef __ILP32__
+  /* Sanitize padding bits of pointer arguments as per aapcs64 */
+#define PTR_ARG(n)  mov w##n, w##n
+#else
+#define PTR_ARG(n)
+#endif
+
+#ifdef __ILP32__
+  /* Sanitize padding bits of size arguments as per aapcs64 */
+#define SIZE_ARG(n)  mov w##n, w##n
+#else
+#define SIZE_ARG(n)
+#endif
+
 #endif
diff --git a/newlib/libc/machine/aarch64/memchr.S b/newlib/libc/machine/aarch64/memchr.S
index 074a004cf..a0f305e0f 100644
--- a/newlib/libc/machine/aarch64/memchr.S
+++ b/newlib/libc/machine/aarch64/memchr.S
@@ -50,6 +50,8 @@
  */
 
 ENTRY (memchr)
+	PTR_ARG (0)
+	SIZE_ARG (2)
 	/* Do not dereference srcin if no bytes to compare.  */
 	cbz	cntin, L(zero_length)
 	/*
diff --git a/newlib/libc/machine/aarch64/memcmp.S b/newlib/libc/machine/aarch64/memcmp.S
index 2a9c446bb..18874d321 100644
--- a/newlib/libc/machine/aarch64/memcmp.S
+++ b/newlib/libc/machine/aarch64/memcmp.S
@@ -34,6 +34,10 @@
 
 
 ENTRY (memcmp)
+	PTR_ARG (0)
+	PTR_ARG (1)
+	SIZE_ARG (2)
+
 	cmp	limit, 16
 	b.lo	L(less16)
 	ldp	data1, data3, [src1]
diff --git a/newlib/libc/machine/aarch64/memcpy.S b/newlib/libc/machine/aarch64/memcpy.S
index 252d2452a..248e7843a 100644
--- a/newlib/libc/machine/aarch64/memcpy.S
+++ b/newlib/libc/machine/aarch64/memcpy.S
@@ -58,6 +58,9 @@
 
 ENTRY_ALIAS (memmove)
 ENTRY (memcpy)
+	PTR_ARG (0)
+	PTR_ARG (1)
+	SIZE_ARG (2)
 	add	srcend, src, count
 	add	dstend, dstin, count
 	cmp	count, 128
diff --git a/newlib/libc/machine/aarch64/memrchr.S b/newlib/libc/machine/aarch64/memrchr.S
index 2525cf190..ba9915cc3 100644
--- a/newlib/libc/machine/aarch64/memrchr.S
+++ b/newlib/libc/machine/aarch64/memrchr.S
@@ -45,6 +45,7 @@
    exactly which byte matched.  */
 
 ENTRY (memrchr)
+	PTR_ARG (0)
 	add	end, srcin, cntin
 	sub	endm1, end, 1
 	bic	src, endm1, 15
diff --git a/newlib/libc/machine/aarch64/memset.S b/newlib/libc/machine/aarch64/memset.S
index 5bf4851f3..ca76439a9 100644
--- a/newlib/libc/machine/aarch64/memset.S
+++ b/newlib/libc/machine/aarch64/memset.S
@@ -25,6 +25,9 @@
 #define zva_val	x5
 
 ENTRY (memset)
+	PTR_ARG (0)
+	SIZE_ARG (2)
+
 	dup	v0.16B, valw
 	add	dstend, dstin, count
 
diff --git a/newlib/libc/machine/aarch64/strchr.S b/newlib/libc/machine/aarch64/strchr.S
index a4e089b99..500d9aff2 100644
--- a/newlib/libc/machine/aarch64/strchr.S
+++ b/newlib/libc/machine/aarch64/strchr.S
@@ -54,6 +54,7 @@
 /* Locals and temporaries.  */
 
 ENTRY (strchr)
+	PTR_ARG (0)
 	/* Magic constant 0xc0300c03 to allow us to identify which lane
 	   matches the requested byte.  Even bits are set if the character
 	   matches, odd bits if either the char is NUL or matches.  */
diff --git a/newlib/libc/machine/aarch64/strchrnul.S b/newlib/libc/machine/aarch64/strchrnul.S
index 4d6093be7..ceaf4dca1 100644
--- a/newlib/libc/machine/aarch64/strchrnul.S
+++ b/newlib/libc/machine/aarch64/strchrnul.S
@@ -50,6 +50,7 @@
 /* Locals and temporaries.  */
 
 ENTRY (strchrnul)
+	PTR_ARG (0)
 	/* Magic constant 0x40100401 to allow us to identify which lane
 	   matches the termination condition.  */
 	mov	wtmp2, #0x0401
diff --git a/newlib/libc/machine/aarch64/strcmp.S b/newlib/libc/machine/aarch64/strcmp.S
index 1d85da432..691a1760e 100644
--- a/newlib/libc/machine/aarch64/strcmp.S
+++ b/newlib/libc/machine/aarch64/strcmp.S
@@ -54,6 +54,8 @@
 
 
 ENTRY (strcmp)
+	PTR_ARG (0)
+	PTR_ARG (1)
 	sub	off2, src2, src1
 	mov	zeroones, REP8_01
 	and	tmp, src1, 7
diff --git a/newlib/libc/machine/aarch64/strcpy.S b/newlib/libc/machine/aarch64/strcpy.S
index 1f5f70792..57c46f390 100644
--- a/newlib/libc/machine/aarch64/strcpy.S
+++ b/newlib/libc/machine/aarch64/strcpy.S
@@ -55,6 +55,8 @@
    exactly which byte matched.  */
 
 ENTRY (STRCPY)
+	PTR_ARG (0)
+	PTR_ARG (1)
 	bic	src, srcin, 15
 	ld1	{vdata.16b}, [src]
 	cmeq	vhas_nul.16b, vdata.16b, 0
diff --git a/newlib/libc/machine/aarch64/strlen.S b/newlib/libc/machine/aarch64/strlen.S
index a3345a3ab..68a6f357c 100644
--- a/newlib/libc/machine/aarch64/strlen.S
+++ b/newlib/libc/machine/aarch64/strlen.S
@@ -78,6 +78,7 @@
    character, return the length, if not, continue in the main loop.  */
 
 ENTRY (strlen)
+	PTR_ARG (0)
 	and	tmp1, srcin, MIN_PAGE_SIZE - 1
 	cmp	tmp1, MIN_PAGE_SIZE - 32
 	b.hi	L(page_cross)
diff --git a/newlib/libc/machine/aarch64/strncmp.S b/newlib/libc/machine/aarch64/strncmp.S
index 6b1994ef5..373695503 100644
--- a/newlib/libc/machine/aarch64/strncmp.S
+++ b/newlib/libc/machine/aarch64/strncmp.S
@@ -58,6 +58,9 @@
 #endif
 
 ENTRY (strncmp)
+	PTR_ARG (0)
+	PTR_ARG (1)
+	SIZE_ARG (2)
 	cbz	limit, L(ret0)
 	eor	tmp1, src1, src2
 	mov	zeroones, #REP8_01
diff --git a/newlib/libc/machine/aarch64/strnlen.S b/newlib/libc/machine/aarch64/strnlen.S
index dc9fcb2fd..091002e0b 100644
--- a/newlib/libc/machine/aarch64/strnlen.S
+++ b/newlib/libc/machine/aarch64/strnlen.S
@@ -39,6 +39,8 @@
    identifies the first zero byte.  */
 
 ENTRY (strnlen)
+	PTR_ARG (0)
+	SIZE_ARG (1)
 	bic	src, srcin, 15
 	cbz	cntin, L(nomatch)
 	ld1	{vdata.16b}, [src]
diff --git a/newlib/libc/machine/aarch64/strrchr.S b/newlib/libc/machine/aarch64/strrchr.S
index 67013e39d..b0574228b 100644
--- a/newlib/libc/machine/aarch64/strrchr.S
+++ b/newlib/libc/machine/aarch64/strrchr.S
@@ -58,6 +58,7 @@
    identify exactly which byte is causing the termination, and why.  */
 
 ENTRY (strrchr)
+	PTR_ARG (0)
 	/* Magic constant 0x40100401 to allow us to identify which lane
 	   matches the requested byte.  Magic constant 0x80200802 used
 	   similarly for NUL termination.  */
-- 
2.50.1.vfs.0.0
