[PATCH 1/2] Redirect mempcpy and stpcpy only in libc.a

H.J. Lu hjl.tools@gmail.com
Wed Jan 1 23:22:35 GMT 2025


Within libc, __mempcpy and __stpcpy are defined as macros which call
__builtin_mempcpy and __builtin_stpcpy which may end up calling the C
functions mempcpy and __stpcpy.  In libc.so, libc_hidden_builtin_proto
ensures that calls to those C functions are in turn mapped to call
__GI_mempcpy and __GI_stpcpy.  In libc.a, mempcpy and stpcpy may be
called which pollutes the ISO C namespace.  include/string.h contains

extern __typeof (mempcpy) mempcpy __asm__ ("__mempcpy");
extern __typeof (stpcpy) stpcpy __asm__ ("__stpcpy");

to redirect mempcpy and stpcpy to __mempcpy and __stpcpy.  Since mempcpy
and stpcpy macros are only defined and used within libc, limit these
redirections to libc.a to avoid Clang error:

In file included from tst-iconv-sticky-input-error.c:22:
In file included from ./gconv_int.h:24:
../include/string.h:182:44: error: attribute declaration must precede definition [-Werror,-Wignored-attributes]
  182 | extern __typeof (mempcpy) mempcpy __asm__ ("__mempcpy");
      |                                            ^
../string/bits/string_fortified.h:42:8: note: previous definition is here
   42 | __NTH (mempcpy (void *__restrict __dest, const void *__restrict __src,
      |        ^

when testing with Clang for fortify build.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 include/string.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/string.h b/include/string.h
index 3b4c6007d7..1f6aef9f93 100644
--- a/include/string.h
+++ b/include/string.h
@@ -175,7 +175,7 @@ extern __typeof (strnlen) strnlen attribute_hidden;
 extern __typeof (strsep) strsep attribute_hidden;
 #endif
 
-#if (!IS_IN (libc) || !defined SHARED) \
+#if IS_IN (libc) && !defined SHARED \
   && !defined NO_MEMPCPY_STPCPY_REDIRECT
 /* Redirect calls to __builtin_mempcpy and __builtin_stpcpy to call
    __mempcpy and __stpcpy if not inlined.  */
-- 
2.47.1



More information about the Libc-alpha mailing list