[PATCH v1 1/3] riscv: Add multiarch support for strcpy

Yao Zihong zihong.plct@isrc.iscas.ac.cn
Fri Sep 19 00:50:46 GMT 2025


This patch introduces the riscv multiarch/IFUNC support for strcpy on RISC-V
without changing behavior.

- Add sysdeps/riscv/multiarch/strcpy-generic.c to re-export the default
  string/strcpy.c as __strcpy_generic.
- Add sysdeps/unix/sysv/linux/riscv/multiarch/strcpy.c that provides the
  IFUNC wrapper and currently resolves to the generic implementation.
- Register strcpy and strcpy-generic in the multiarch Makefile.
- List the generic strcpy in ifunc-impl-list.c.

Signed-off-by: Yao Zihong <zihong.plct@isrc.iscas.ac.cn>
---
 sysdeps/riscv/multiarch/strcpy-generic.c      | 26 ++++++++++
 .../unix/sysv/linux/riscv/multiarch/Makefile  |  2 +
 .../linux/riscv/multiarch/ifunc-impl-list.c   |  4 +-
 .../unix/sysv/linux/riscv/multiarch/strcpy.c  | 52 +++++++++++++++++++
 4 files changed, 83 insertions(+), 1 deletion(-)
 create mode 100644 sysdeps/riscv/multiarch/strcpy-generic.c
 create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/strcpy.c

diff --git a/sysdeps/riscv/multiarch/strcpy-generic.c b/sysdeps/riscv/multiarch/strcpy-generic.c
new file mode 100644
index 0000000000..72901d49d6
--- /dev/null
+++ b/sysdeps/riscv/multiarch/strcpy-generic.c
@@ -0,0 +1,26 @@
+/* Re-include the default strcpy implementation.
+   Copyright (C) 2025 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <string.h>
+
+#if IS_IN(libc)
+# define STRCPY __strcpy_generic
+# undef libc_hidden_builtin_def
+# define libc_hidden_builtin_def(x)
+#endif
+#include <string/strcpy.c>
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
index fcef5659d4..b1c6d702c2 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile
@@ -3,6 +3,8 @@ sysdep_routines += \
   memcpy \
   memcpy-generic \
   memcpy_noalignment \
+  strcpy \
+  strcpy-generic \
   # sysdep_routines
 
 CFLAGS-memcpy_noalignment.c += -mno-strict-align
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
index 1c1deca8f6..8e14560b65 100644
--- a/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/ifunc-impl-list.c
@@ -38,6 +38,8 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
 	      IFUNC_IMPL_ADD (array, i, memcpy, fast_unaligned,
 			      __memcpy_noalignment)
 	      IFUNC_IMPL_ADD (array, i, memcpy, 1, __memcpy_generic))
-
+  
+  IFUNC_IMPL (i, name, strcpy,
+	      IFUNC_IMPL_ADD (array, i, strcpy, 1, __strcpy_generic))
   return 0;
 }
diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/strcpy.c b/sysdeps/unix/sysv/linux/riscv/multiarch/strcpy.c
new file mode 100644
index 0000000000..473fa7768d
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/riscv/multiarch/strcpy.c
@@ -0,0 +1,52 @@
+/* Multiple versions of strcpy.
+   All versions must be listed in ifunc-impl-list.c.
+   Copyright (C) 2025 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#if IS_IN (libc)
+/* Redefine strcpy so that the compiler won't complain about the type
+   mismatch with the IFUNC selector in strong_alias, below.  */
+# undef strcpy
+# define strcpy __redirect_strcpy
+# include <stdint.h>
+# include <string.h>
+# include <ifunc-init.h>
+# include <riscv-ifunc.h>
+# include <sys/hwprobe.h>
+# include <asm/hwcap.h>
+
+extern __typeof (__redirect_strcpy) __libc_strcpy;
+
+extern __typeof (__redirect_strcpy) __strcpy_generic attribute_hidden;
+
+static inline __typeof (__redirect_strcpy) *
+select_strcpy_ifunc (uint64_t dl_hwcap, __riscv_hwprobe_t hwprobe_func)
+{
+  return __strcpy_generic;
+}
+
+riscv_libc_ifunc (__libc_strcpy, select_strcpy_ifunc);
+
+# undef strcpy
+strong_alias (__libc_strcpy, strcpy);
+# ifdef SHARED
+__hidden_ver1 (strcpy, __GI_strcpy, __redirect_strcpy)
+  __attribute__ ((visibility ("hidden"))) __attribute_copy__ (strcpy);
+# endif
+#else
+# include <string/strcpy.c>
+#endif
-- 
2.47.2



More information about the Libc-alpha mailing list