diff options
| author | David Laight <david.laight.linux@gmail.com> | 2026-03-08 14:37:28 +0300 |
|---|---|---|
| committer | Thomas Weißschuh <linux@weissschuh.net> | 2026-03-20 19:46:07 +0300 |
| commit | 2177dd375d087012907e389f787b21ac38bb1785 (patch) | |
| tree | 365fa498a048ab5398a4fd6c9ee2b9ed08e22b97 /tools/include | |
| parent | ab7cd329a837711b88600e5d776836f16a0e8de8 (diff) | |
| download | linux-2177dd375d087012907e389f787b21ac38bb1785.tar.xz | |
tools/nolibc: Implement strerror() in terms of strerror_r()
strerror() can be the only part of a program that has a .data section.
This requires 4k in the program file.
Add a simple implementation of strerror_r() and use that in strerror()
so that the "errno=" string is copied at run-time.
Use __builtin_memcpy() because that optimises away the input string
and just writes the required constants to the target buffer.
Code size change largely depends on whether the inlining decision for
strerror() changes.
Change the tests to use the normal EXPECT_VFPRINTF() when testing %m.
Skip the tests when !is_nolibc.
Signed-off-by: David Laight <david.laight.linux@gmail.com>
Acked-by: Willy Tarreau <w@1wt.eu>
Link: https://patch.msgid.link/20260308113742.12649-4-david.laight.linux@gmail.com
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Diffstat (limited to 'tools/include')
| -rw-r--r-- | tools/include/nolibc/stdio.h | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index a4df72d9a2d3..b324fc438ea0 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -723,13 +723,29 @@ int setvbuf(FILE *stream __attribute__((unused)), } static __attribute__((unused)) +int strerror_r(int errnum, char *buf, size_t buflen) +{ + if (buflen < 18) + return ERANGE; + + __builtin_memcpy(buf, "errno=", 6); + i64toa_r(errnum, buf + 6); + return 0; +} + +static __attribute__((unused)) const char *strerror(int errno) { - static char buf[18] = "errno="; + static char buf[18]; + char *b = buf; + + /* Force gcc to use 'register offset' to access buf[]. */ + _NOLIBC_OPTIMIZER_HIDE_VAR(b); - i64toa_r(errno, &buf[6]); + /* Use strerror_r() to avoid having the only .data in small programs. */ + strerror_r(errno, b, sizeof(buf)); - return buf; + return b; } #endif /* _NOLIBC_STDIO_H */ |
