|
| 1 | +From: Nathan Chancellor < [email protected]> |
| 2 | +Subject: [PATCH v3 2/2] lib/string.c: Add wcslen() |
| 3 | +Date: Fri, 28 Mar 2025 12:26:32 -0700 |
| 4 | +Content-Type: text/plain; charset="utf-8" |
| 5 | + |
| 6 | +A recent optimization change in LLVM [1] aims to transform certain loop |
| 7 | +idioms into calls to strlen() or wcslen(). This change transforms the |
| 8 | +first while loop in UniStrcat() into a call to wcslen(), breaking the |
| 9 | +build when UniStrcat() gets inlined into alloc_path_with_tree_prefix(): |
| 10 | + |
| 11 | + ld.lld: error: undefined symbol: wcslen |
| 12 | + >>> referenced by nls_ucs2_utils.h:54 (fs/smb/client/../../nls/nls_ucs2_utils.h:54) |
| 13 | + >>> vmlinux.o:(alloc_path_with_tree_prefix) |
| 14 | + >>> referenced by nls_ucs2_utils.h:54 (fs/smb/client/../../nls/nls_ucs2_utils.h:54) |
| 15 | + >>> vmlinux.o:(alloc_path_with_tree_prefix) |
| 16 | + |
| 17 | +The kernel does not build with '-ffreestanding' (which would avoid this |
| 18 | +transformation) because it does want libcall optimizations in general |
| 19 | +and turning on '-ffreestanding' disables the majority of them. While |
| 20 | +'-fno-builtin-wcslen' would be more targeted at the problem, it does not |
| 21 | +work with LTO. |
| 22 | + |
| 23 | +Add a basic wcslen() to avoid this linkage failure. While no |
| 24 | +architecture or FORTIFY_SOURCE overrides this, add it to string.c |
| 25 | +instead of string_helpers.c so that it is built with '-ffreestanding', |
| 26 | +otherwise the compiler might transform it into a call to itself. |
| 27 | + |
| 28 | + |
| 29 | +Link: https://github.com/llvm/llvm-project/commit/9694844d7e36fd5e01011ab56b64f27b867aa72d [1] |
| 30 | +Signed-off-by: Nathan Chancellor < [email protected]> |
| 31 | +--- |
| 32 | + include/linux/string.h | 2 ++ |
| 33 | + lib/string.c | 11 +++++++++++ |
| 34 | + 2 files changed, 13 insertions(+) |
| 35 | + |
| 36 | +diff --git a/include/linux/string.h b/include/linux/string.h |
| 37 | +index 0403a4ca4c11..b000f445a2c7 100644 |
| 38 | +--- a/include/linux/string.h |
| 39 | ++++ b/include/linux/string.h |
| 40 | +@@ -10,6 +10,7 @@ |
| 41 | + #include <linux/stddef.h> /* for NULL */ |
| 42 | + #include <linux/err.h> /* for ERR_PTR() */ |
| 43 | + #include <linux/errno.h> /* for E2BIG */ |
| 44 | ++#include <linux/nls_types.h> /* for wchar_t */ |
| 45 | + #include <linux/overflow.h> /* for check_mul_overflow() */ |
| 46 | + #include <linux/stdarg.h> |
| 47 | + #include <uapi/linux/string.h> |
| 48 | +@@ -203,6 +204,7 @@ extern __kernel_size_t strlen(const char *); |
| 49 | + #ifndef __HAVE_ARCH_STRNLEN |
| 50 | + extern __kernel_size_t strnlen(const char *,__kernel_size_t); |
| 51 | + #endif |
| 52 | ++__kernel_size_t wcslen(const wchar_t *s); |
| 53 | + #ifndef __HAVE_ARCH_STRPBRK |
| 54 | + extern char * strpbrk(const char *,const char *); |
| 55 | + #endif |
| 56 | +diff --git a/lib/string.c b/lib/string.c |
| 57 | +index eb4486ed40d2..2c6f8c8f4159 100644 |
| 58 | +--- a/lib/string.c |
| 59 | ++++ b/lib/string.c |
| 60 | +@@ -21,6 +21,7 @@ |
| 61 | + #include <linux/errno.h> |
| 62 | + #include <linux/limits.h> |
| 63 | + #include <linux/linkage.h> |
| 64 | ++#include <linux/nls_types.h> |
| 65 | + #include <linux/stddef.h> |
| 66 | + #include <linux/string.h> |
| 67 | + #include <linux/types.h> |
| 68 | +@@ -429,6 +430,16 @@ size_t strnlen(const char *s, size_t count) |
| 69 | + EXPORT_SYMBOL(strnlen); |
| 70 | + #endif |
| 71 | + |
| 72 | ++size_t wcslen(const wchar_t *s) |
| 73 | ++{ |
| 74 | ++ const wchar_t *sc; |
| 75 | ++ |
| 76 | ++ for (sc = s; *sc != '\0'; ++sc) |
| 77 | ++ /* nothing */; |
| 78 | ++ return sc - s; |
| 79 | ++} |
| 80 | ++EXPORT_SYMBOL(wcslen); |
| 81 | ++ |
| 82 | + #ifndef __HAVE_ARCH_STRSPN |
| 83 | + /** |
| 84 | + * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept |
| 85 | +-- |
| 86 | +2.49.0 |
| 87 | + |
0 commit comments