History

Merge tag 'nolibc-20260412-for-7.1-1' of git://git.kernel.org/pub/scm/linux/kernel/git/nolibc/linux-nolibc

Pull nolibc updates from Thomas Weißschuh: - Many new features and optimizations to printf() - Rename non-standard symbols to avoid collisions with application code - Support for byteswap.h, endian.h, err.h and asprintf() - 64-bit dev_t - Smaller cleanups and fixes to the code and build system * tag 'nolibc-20260412-for-7.1-1' of git://git.kernel.org/pub/scm/linux/kernel/git/nolibc/linux-nolibc: (61 commits) selftests/nolibc: use gcc 15 tools/nolibc: support UBSAN on gcc tools/nolibc: create __nolibc_no_sanitize_ubsan selftests/nolibc: don't skip tests for unimplemented syscalls anymore selftests/nolibc: explicitly handle ENOSYS from ptrace() tools/nolibc: add byteorder conversions tools/nolibc: add the _syscall() macro tools/nolibc: move the call to __sysret() into syscall() tools/nolibc: rename the internal macros used in syscall() selftests/nolibc: only use libgcc when really necessary selftests/nolibc: test the memory allocator tools/nolibc: check for overflow in calloc() without divisions tools/nolibc: add support for asprintf() tools/nolibc: use __builtin_offsetof() tools/nolibc: use makedev() in fstatat() tools/nolibc: handle all major and minor numbers in makedev() and friends tools/nolibc: make dev_t 64 bits wide tools/nolibc: move the logic of makedev() and friends into functions selftests/nolibc: add a test for stat().st_rdev selftests/nolibc: add some tests for makedev() and friends ...

Linus Torvalds committed 7 months ago

Commit ee60c512 parents 3203a08 b070dc355 files · +1453 −757Browse files

Unified · Split

55 files
  1. tools/include/nolibc/Makefile +5 −9
  2. tools/include/nolibc/arch-arm.h +7 −7
  3. tools/include/nolibc/arch-arm64.h +7 −7
  4. tools/include/nolibc/arch-loongarch.h +7 −7
  5. tools/include/nolibc/arch-m68k.h +7 −7
  6. tools/include/nolibc/arch-mips.h +21 −12
  7. tools/include/nolibc/arch-powerpc.h +7 −7
  8. tools/include/nolibc/arch-riscv.h +7 −7
  9. tools/include/nolibc/arch-s390.h +17 −17
  10. tools/include/nolibc/arch-sh.h +7 −7
  11. tools/include/nolibc/arch-sparc.h +13 −13
  12. tools/include/nolibc/arch-x86.h +34 −34
  13. tools/include/nolibc/byteswap.h +21 −0
  14. tools/include/nolibc/compiler.h +20 −1
  15. tools/include/nolibc/crt.h +27 −4
  16. tools/include/nolibc/dirent.h +2 −2
  17. tools/include/nolibc/endian.h +32 −0
  18. tools/include/nolibc/err.h +87 −0
  19. tools/include/nolibc/errno.h +4 −0
  20. tools/include/nolibc/fcntl.h +6 −6
  21. tools/include/nolibc/nolibc.h +8 −5
  22. tools/include/nolibc/poll.h +4 −4
  23. tools/include/nolibc/sched.h +6 −6
  24. tools/include/nolibc/signal.h +1 −1
  25. tools/include/nolibc/stackprotector.h +4 −4
  26. tools/include/nolibc/std.h +1 −1
  27. tools/include/nolibc/stddef.h +1 −1
  28. tools/include/nolibc/stdio.h +437 −128
  29. tools/include/nolibc/stdlib.h +85 −90
  30. tools/include/nolibc/sys.h +144 −144
  31. tools/include/nolibc/sys/ioctl.h +3 −3
  32. tools/include/nolibc/sys/mman.h +12 −12
  33. tools/include/nolibc/sys/mount.h +4 −4
  34. tools/include/nolibc/sys/prctl.h +4 −4
  35. tools/include/nolibc/sys/ptrace.h +3 −3
  36. tools/include/nolibc/sys/random.h +3 −3
  37. tools/include/nolibc/sys/reboot.h +3 −3
  38. tools/include/nolibc/sys/resource.h +5 −5
  39. tools/include/nolibc/sys/select.h +6 −4
  40. tools/include/nolibc/sys/stat.h +7 −10
  41. tools/include/nolibc/sys/syscall.h +6 −5
  42. tools/include/nolibc/sys/sysmacros.h +21 −4
  43. tools/include/nolibc/sys/time.h +4 −4
  44. tools/include/nolibc/sys/timerfd.h +12 −12
  45. tools/include/nolibc/sys/uio.h +6 −6
  46. tools/include/nolibc/sys/utsname.h +3 −3
  47. tools/include/nolibc/sys/wait.h +3 −3
  48. tools/include/nolibc/time.h +34 −34
  49. tools/include/nolibc/unistd.h +6 −6
  50. tools/testing/selftests/nolibc/Makefile +2 −2
  51. tools/testing/selftests/nolibc/Makefile.include +4 −1
  52. tools/testing/selftests/nolibc/Makefile.nolibc +7 −16
  53. tools/testing/selftests/nolibc/nolibc-test-ignore-errno.c +6 −0
  54. tools/testing/selftests/nolibc/nolibc-test.c +259 −78
  55. tools/testing/selftests/nolibc/run-tests.sh +1 −1
tools/include/nolibc/Makefile+5−9
@@ -17,19 +17,17 @@ endif · 16 unmodified lines
1717 # it defaults to this nolibc directory.
1818 OUTPUT ?= $(CURDIR)/
1919
20−ifeq ($(V),1)
21−Q=
22−else
23−Q=@
24−endif
25−
26−arch_files := arch.h $(wildcard arch-*.h)
20+architectures := arm arm64 loongarch m68k mips powerpc riscv s390 sh sparc x86
21+arch_files := arch.h $(addsuffix .h, $(addprefix arch-, $(architectures)))
2722 all_files := \
23+ byteswap.h \
2824 compiler.h \
2925 crt.h \
3026 ctype.h \
3127 dirent.h \
3228 elf.h \
29+ endian.h \
30+ err.h \
3331 errno.h \
3432 fcntl.h \
3533 getopt.h \
@@ -96,12 +94,10 @@ help: · 60 unmodified lines
9694
9795 # installs headers for all archs at once.
9896 headers:
99− $(Q)mkdir -p "$(OUTPUT)sysroot"
10097 $(Q)mkdir -p "$(OUTPUT)sysroot/include"
10198 $(Q)cp --parents $(arch_files) $(all_files) "$(OUTPUT)sysroot/include/"
10299
103100 headers_standalone: headers
104− $(Q)$(MAKE) -C $(srctree) headers
105101 $(Q)$(MAKE) -C $(srctree) headers_install INSTALL_HDR_PATH=$(OUTPUT)sysroot
106102
107103 CFLAGS_s390 := -m64
tools/include/nolibc/arch-arm.h+7−7
@@ -50,7 +50,7 @@ · 49 unmodified lines
5050
5151 #endif /* end THUMB */
5252
53−#define my_syscall0(num) \
53+#define __nolibc_syscall0(num) \
5454 ({ \
5555 register long _num __asm__(_NOLIBC_SYSCALL_REG) = (num); \
5656 register long _arg1 __asm__ ("r0"); \
@@ -67,7 +67,7 @@ · 10 unmodified lines
6767 _arg1; \
6868 })
6969
70−#define my_syscall1(num, arg1) \
70+#define __nolibc_syscall1(num, arg1) \
7171 ({ \
7272 register long _num __asm__(_NOLIBC_SYSCALL_REG) = (num); \
7373 register long _arg1 __asm__ ("r0") = (long)(arg1); \
@@ -84,7 +84,7 @@ · 10 unmodified lines
8484 _arg1; \
8585 })
8686
87−#define my_syscall2(num, arg1, arg2) \
87+#define __nolibc_syscall2(num, arg1, arg2) \
8888 ({ \
8989 register long _num __asm__(_NOLIBC_SYSCALL_REG) = (num); \
9090 register long _arg1 __asm__ ("r0") = (long)(arg1); \
@@ -102,7 +102,7 @@ · 11 unmodified lines
102102 _arg1; \
103103 })
104104
105−#define my_syscall3(num, arg1, arg2, arg3) \
105+#define __nolibc_syscall3(num, arg1, arg2, arg3) \
106106 ({ \
107107 register long _num __asm__(_NOLIBC_SYSCALL_REG) = (num); \
108108 register long _arg1 __asm__ ("r0") = (long)(arg1); \
@@ -121,7 +121,7 @@ · 12 unmodified lines
121121 _arg1; \
122122 })
123123
124−#define my_syscall4(num, arg1, arg2, arg3, arg4) \
124+#define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \
125125 ({ \
126126 register long _num __asm__(_NOLIBC_SYSCALL_REG) = (num); \
127127 register long _arg1 __asm__ ("r0") = (long)(arg1); \
@@ -141,7 +141,7 @@ · 13 unmodified lines
141141 _arg1; \
142142 })
143143
144−#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
144+#define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
145145 ({ \
146146 register long _num __asm__(_NOLIBC_SYSCALL_REG) = (num); \
147147 register long _arg1 __asm__ ("r0") = (long)(arg1); \
@@ -162,7 +162,7 @@ · 14 unmodified lines
162162 _arg1; \
163163 })
164164
165−#define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
165+#define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
166166 ({ \
167167 register long _num __asm__(_NOLIBC_SYSCALL_REG) = (num); \
168168 register long _arg1 __asm__ ("r0") = (long)(arg1); \
tools/include/nolibc/arch-arm64.h+7−7
@@ -22,7 +22,7 @@ · 21 unmodified lines
2222 * don't have to experience issues with register constraints.
2323 */
2424
25−#define my_syscall0(num) \
25+#define __nolibc_syscall0(num) \
2626 ({ \
2727 register long _num __asm__ ("x8") = (num); \
2828 register long _arg1 __asm__ ("x0"); \
@@ -36,7 +36,7 @@ · 7 unmodified lines
3636 _arg1; \
3737 })
3838
39−#define my_syscall1(num, arg1) \
39+#define __nolibc_syscall1(num, arg1) \
4040 ({ \
4141 register long _num __asm__ ("x8") = (num); \
4242 register long _arg1 __asm__ ("x0") = (long)(arg1); \
@@ -51,7 +51,7 @@ · 8 unmodified lines
5151 _arg1; \
5252 })
5353
54−#define my_syscall2(num, arg1, arg2) \
54+#define __nolibc_syscall2(num, arg1, arg2) \
5555 ({ \
5656 register long _num __asm__ ("x8") = (num); \
5757 register long _arg1 __asm__ ("x0") = (long)(arg1); \
@@ -67,7 +67,7 @@ · 9 unmodified lines
6767 _arg1; \
6868 })
6969
70−#define my_syscall3(num, arg1, arg2, arg3) \
70+#define __nolibc_syscall3(num, arg1, arg2, arg3) \
7171 ({ \
7272 register long _num __asm__ ("x8") = (num); \
7373 register long _arg1 __asm__ ("x0") = (long)(arg1); \
@@ -84,7 +84,7 @@ · 10 unmodified lines
8484 _arg1; \
8585 })
8686
87−#define my_syscall4(num, arg1, arg2, arg3, arg4) \
87+#define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \
8888 ({ \
8989 register long _num __asm__ ("x8") = (num); \
9090 register long _arg1 __asm__ ("x0") = (long)(arg1); \
@@ -102,7 +102,7 @@ · 11 unmodified lines
102102 _arg1; \
103103 })
104104
105−#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
105+#define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
106106 ({ \
107107 register long _num __asm__ ("x8") = (num); \
108108 register long _arg1 __asm__ ("x0") = (long)(arg1); \
@@ -121,7 +121,7 @@ · 12 unmodified lines
121121 _arg1; \
122122 })
123123
124−#define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
124+#define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
125125 ({ \
126126 register long _num __asm__ ("x8") = (num); \
127127 register long _arg1 __asm__ ("x0") = (long)(arg1); \
tools/include/nolibc/arch-loongarch.h+7−7
@@ -24,7 +24,7 @@ · 23 unmodified lines
2424 #define _NOLIBC_SYSCALL_CLOBBERLIST \
2525 "memory", "$t0", "$t1", "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t8"
2626
27−#define my_syscall0(num) \
27+#define __nolibc_syscall0(num) \
2828 ({ \
2929 register long _num __asm__ ("a7") = (num); \
3030 register long _arg1 __asm__ ("a0"); \
@@ -38,7 +38,7 @@ · 7 unmodified lines
3838 _arg1; \
3939 })
4040
41−#define my_syscall1(num, arg1) \
41+#define __nolibc_syscall1(num, arg1) \
4242 ({ \
4343 register long _num __asm__ ("a7") = (num); \
4444 register long _arg1 __asm__ ("a0") = (long)(arg1); \
@@ -52,7 +52,7 @@ · 7 unmodified lines
5252 _arg1; \
5353 })
5454
55−#define my_syscall2(num, arg1, arg2) \
55+#define __nolibc_syscall2(num, arg1, arg2) \
5656 ({ \
5757 register long _num __asm__ ("a7") = (num); \
5858 register long _arg1 __asm__ ("a0") = (long)(arg1); \
@@ -68,7 +68,7 @@ · 9 unmodified lines
6868 _arg1; \
6969 })
7070
71−#define my_syscall3(num, arg1, arg2, arg3) \
71+#define __nolibc_syscall3(num, arg1, arg2, arg3) \
7272 ({ \
7373 register long _num __asm__ ("a7") = (num); \
7474 register long _arg1 __asm__ ("a0") = (long)(arg1); \
@@ -85,7 +85,7 @@ · 10 unmodified lines
8585 _arg1; \
8686 })
8787
88−#define my_syscall4(num, arg1, arg2, arg3, arg4) \
88+#define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \
8989 ({ \
9090 register long _num __asm__ ("a7") = (num); \
9191 register long _arg1 __asm__ ("a0") = (long)(arg1); \
@@ -103,7 +103,7 @@ · 11 unmodified lines
103103 _arg1; \
104104 })
105105
106−#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
106+#define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
107107 ({ \
108108 register long _num __asm__ ("a7") = (num); \
109109 register long _arg1 __asm__ ("a0") = (long)(arg1); \
@@ -122,7 +122,7 @@ · 12 unmodified lines
122122 _arg1; \
123123 })
124124
125−#define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
125+#define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
126126 ({ \
127127 register long _num __asm__ ("a7") = (num); \
128128 register long _arg1 __asm__ ("a0") = (long)(arg1); \
tools/include/nolibc/arch-m68k.h+7−7
@@ -15,7 +15,7 @@ · 14 unmodified lines
1515
1616 #define _NOLIBC_SYSCALL_CLOBBERLIST "memory"
1717
18−#define my_syscall0(num) \
18+#define __nolibc_syscall0(num) \
1919 ({ \
2020 register long _num __asm__ ("d0") = (num); \
2121 \
@@ -28,7 +28,7 @@ · 6 unmodified lines
2828 _num; \
2929 })
3030
31−#define my_syscall1(num, arg1) \
31+#define __nolibc_syscall1(num, arg1) \
3232 ({ \
3333 register long _num __asm__ ("d0") = (num); \
3434 register long _arg1 __asm__ ("d1") = (long)(arg1); \
@@ -42,7 +42,7 @@ · 7 unmodified lines
4242 _num; \
4343 })
4444
45−#define my_syscall2(num, arg1, arg2) \
45+#define __nolibc_syscall2(num, arg1, arg2) \
4646 ({ \
4747 register long _num __asm__ ("d0") = (num); \
4848 register long _arg1 __asm__ ("d1") = (long)(arg1); \
@@ -57,7 +57,7 @@ · 8 unmodified lines
5757 _num; \
5858 })
5959
60−#define my_syscall3(num, arg1, arg2, arg3) \
60+#define __nolibc_syscall3(num, arg1, arg2, arg3) \
6161 ({ \
6262 register long _num __asm__ ("d0") = (num); \
6363 register long _arg1 __asm__ ("d1") = (long)(arg1); \
@@ -73,7 +73,7 @@ · 9 unmodified lines
7373 _num; \
7474 })
7575
76−#define my_syscall4(num, arg1, arg2, arg3, arg4) \
76+#define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \
7777 ({ \
7878 register long _num __asm__ ("d0") = (num); \
7979 register long _arg1 __asm__ ("d1") = (long)(arg1); \
@@ -90,7 +90,7 @@ · 10 unmodified lines
9090 _num; \
9191 })
9292
93−#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
93+#define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
9494 ({ \
9595 register long _num __asm__ ("d0") = (num); \
9696 register long _arg1 __asm__ ("d1") = (long)(arg1); \
@@ -108,7 +108,7 @@ · 11 unmodified lines
108108 _num; \
109109 })
110110
111−#define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
111+#define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
112112 ({ \
113113 register long _num __asm__ ("d0") = (num); \
114114 register long _arg1 __asm__ ("d1") = (long)(arg1); \
tools/include/nolibc/arch-mips.h+21−12
@@ -39,11 +39,19 @@ · 38 unmodified lines
3939 * - stack is 16-byte aligned
4040 */
4141
42+#if !defined(__mips_isa_rev) || __mips_isa_rev < 6
43+#define _NOLIBC_SYSCALL_CLOBBER_HI_LO "hi", "lo"
44+#else
45+#define _NOLIBC_SYSCALL_CLOBBER_HI_LO "$0"
46+#endif
47+
4248 #if defined(_ABIO32)
4349
4450 #define _NOLIBC_SYSCALL_CLOBBERLIST \
45− "memory", "cc", "at", "v1", "hi", "lo", \
46− "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9"
51+ "memory", "cc", "at", "v1", \
52+ "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9", \
53+ _NOLIBC_SYSCALL_CLOBBER_HI_LO
54+
4755 #define _NOLIBC_SYSCALL_STACK_RESERVE "addiu $sp, $sp, -32\n"
4856 #define _NOLIBC_SYSCALL_STACK_UNRESERVE "addiu $sp, $sp, 32\n"
4957
@@ -52,14 +60,15 @@ · 2 unmodified lines
5260 /* binutils, GCC and clang disagree about register aliases, use numbers instead. */
5361 #define _NOLIBC_SYSCALL_CLOBBERLIST \
5462 "memory", "cc", "at", "v1", \
55− "10", "11", "12", "13", "14", "15", "24", "25"
63+ "10", "11", "12", "13", "14", "15", "24", "25", \
64+ _NOLIBC_SYSCALL_CLOBBER_HI_LO
5665
5766 #define _NOLIBC_SYSCALL_STACK_RESERVE
5867 #define _NOLIBC_SYSCALL_STACK_UNRESERVE
5968
6069 #endif /* _ABIO32 */
6170
62−#define my_syscall0(num) \
71+#define __nolibc_syscall0(num) \
6372 ({ \
6473 register long _num __asm__ ("v0") = (num); \
6574 register long _arg4 __asm__ ("a3"); \
@@ -75,7 +84,7 @@ · 9 unmodified lines
7584 _arg4 ? -_num : _num; \
7685 })
7786
78−#define my_syscall1(num, arg1) \
87+#define __nolibc_syscall1(num, arg1) \
7988 ({ \
8089 register long _num __asm__ ("v0") = (num); \
8190 register long _arg1 __asm__ ("a0") = (long)(arg1); \
@@ -93,7 +102,7 @@ · 11 unmodified lines
93102 _arg4 ? -_num : _num; \
94103 })
95104
96−#define my_syscall2(num, arg1, arg2) \
105+#define __nolibc_syscall2(num, arg1, arg2) \
97106 ({ \
98107 register long _num __asm__ ("v0") = (num); \
99108 register long _arg1 __asm__ ("a0") = (long)(arg1); \
@@ -112,7 +121,7 @@ · 12 unmodified lines
112121 _arg4 ? -_num : _num; \
113122 })
114123
115−#define my_syscall3(num, arg1, arg2, arg3) \
124+#define __nolibc_syscall3(num, arg1, arg2, arg3) \
116125 ({ \
117126 register long _num __asm__ ("v0") = (num); \
118127 register long _arg1 __asm__ ("a0") = (long)(arg1); \
@@ -132,7 +141,7 @@ · 13 unmodified lines
132141 _arg4 ? -_num : _num; \
133142 })
134143
135−#define my_syscall4(num, arg1, arg2, arg3, arg4) \
144+#define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \
136145 ({ \
137146 register long _num __asm__ ("v0") = (num); \
138147 register long _arg1 __asm__ ("a0") = (long)(arg1); \
@@ -154,7 +163,7 @@ · 15 unmodified lines
154163
155164 #if defined(_ABIO32)
156165
157−#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
166+#define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
158167 ({ \
159168 register long _num __asm__ ("v0") = (num); \
160169 register long _arg1 __asm__ ("a0") = (long)(arg1); \
@@ -176,7 +185,7 @@ · 15 unmodified lines
176185 _arg4 ? -_num : _num; \
177186 })
178187
179−#define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
188+#define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
180189 ({ \
181190 register long _num __asm__ ("v0") = (num); \
182191 register long _arg1 __asm__ ("a0") = (long)(arg1); \
@@ -203,7 +212,7 @@ · 20 unmodified lines
203212
204213 #else /* _ABIN32 || _ABI64 */
205214
206−#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
215+#define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
207216 ({ \
208217 register long _num __asm__ ("v0") = (num); \
209218 register long _arg1 __asm__ ("$4") = (long)(arg1); \
@@ -222,7 +231,7 @@ · 12 unmodified lines
222231 _arg4 ? -_num : _num; \
223232 })
224233
225−#define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
234+#define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
226235 ({ \
227236 register long _num __asm__ ("v0") = (num); \
228237 register long _arg1 __asm__ ("$4") = (long)(arg1); \
tools/include/nolibc/arch-powerpc.h+7−7
@@ -25,7 +25,7 @@ · 24 unmodified lines
2525 #define _NOLIBC_SYSCALL_CLOBBERLIST \
2626 "memory", "cr0", "r12", "r11", "r10", "r9"
2727
28−#define my_syscall0(num) \
28+#define __nolibc_syscall0(num) \
2929 ({ \
3030 register long _ret __asm__ ("r3"); \
3131 register long _num __asm__ ("r0") = (num); \
@@ -42,7 +42,7 @@ · 10 unmodified lines
4242 _ret; \
4343 })
4444
45−#define my_syscall1(num, arg1) \
45+#define __nolibc_syscall1(num, arg1) \
4646 ({ \
4747 register long _ret __asm__ ("r3"); \
4848 register long _num __asm__ ("r0") = (num); \
@@ -61,7 +61,7 @@ · 12 unmodified lines
6161 })
6262
6363
64−#define my_syscall2(num, arg1, arg2) \
64+#define __nolibc_syscall2(num, arg1, arg2) \
6565 ({ \
6666 register long _ret __asm__ ("r3"); \
6767 register long _num __asm__ ("r0") = (num); \
@@ -81,7 +81,7 @@ · 13 unmodified lines
8181 })
8282
8383
84−#define my_syscall3(num, arg1, arg2, arg3) \
84+#define __nolibc_syscall3(num, arg1, arg2, arg3) \
8585 ({ \
8686 register long _ret __asm__ ("r3"); \
8787 register long _num __asm__ ("r0") = (num); \
@@ -102,7 +102,7 @@ · 14 unmodified lines
102102 })
103103
104104
105−#define my_syscall4(num, arg1, arg2, arg3, arg4) \
105+#define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \
106106 ({ \
107107 register long _ret __asm__ ("r3"); \
108108 register long _num __asm__ ("r0") = (num); \
@@ -125,7 +125,7 @@ · 16 unmodified lines
125125 })
126126
127127
128−#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
128+#define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
129129 ({ \
130130 register long _ret __asm__ ("r3"); \
131131 register long _num __asm__ ("r0") = (num); \
@@ -148,7 +148,7 @@ · 16 unmodified lines
148148 _ret; \
149149 })
150150
151−#define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
151+#define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
152152 ({ \
153153 register long _ret __asm__ ("r3"); \
154154 register long _num __asm__ ("r0") = (num); \
tools/include/nolibc/arch-riscv.h+7−7
@@ -21,7 +21,7 @@ · 20 unmodified lines
2121 * so that we don't have to experience issues with register constraints.
2222 */
2323
24−#define my_syscall0(num) \
24+#define __nolibc_syscall0(num) \
2525 ({ \
2626 register long _num __asm__ ("a7") = (num); \
2727 register long _arg1 __asm__ ("a0"); \
@@ -35,7 +35,7 @@ · 7 unmodified lines
3535 _arg1; \
3636 })
3737
38−#define my_syscall1(num, arg1) \
38+#define __nolibc_syscall1(num, arg1) \
3939 ({ \
4040 register long _num __asm__ ("a7") = (num); \
4141 register long _arg1 __asm__ ("a0") = (long)(arg1); \
@@ -49,7 +49,7 @@ · 7 unmodified lines
4949 _arg1; \
5050 })
5151
52−#define my_syscall2(num, arg1, arg2) \
52+#define __nolibc_syscall2(num, arg1, arg2) \
5353 ({ \
5454 register long _num __asm__ ("a7") = (num); \
5555 register long _arg1 __asm__ ("a0") = (long)(arg1); \
@@ -65,7 +65,7 @@ · 9 unmodified lines
6565 _arg1; \
6666 })
6767
68−#define my_syscall3(num, arg1, arg2, arg3) \
68+#define __nolibc_syscall3(num, arg1, arg2, arg3) \
6969 ({ \
7070 register long _num __asm__ ("a7") = (num); \
7171 register long _arg1 __asm__ ("a0") = (long)(arg1); \
@@ -82,7 +82,7 @@ · 10 unmodified lines
8282 _arg1; \
8383 })
8484
85−#define my_syscall4(num, arg1, arg2, arg3, arg4) \
85+#define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \
8686 ({ \
8787 register long _num __asm__ ("a7") = (num); \
8888 register long _arg1 __asm__ ("a0") = (long)(arg1); \
@@ -100,7 +100,7 @@ · 11 unmodified lines
100100 _arg1; \
101101 })
102102
103−#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
103+#define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
104104 ({ \
105105 register long _num __asm__ ("a7") = (num); \
106106 register long _arg1 __asm__ ("a0") = (long)(arg1); \
@@ -119,7 +119,7 @@ · 12 unmodified lines
119119 _arg1; \
120120 })
121121
122−#define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
122+#define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
123123 ({ \
124124 register long _num __asm__ ("a7") = (num); \
125125 register long _arg1 __asm__ ("a0") = (long)(arg1); \
tools/include/nolibc/arch-s390.h+17−17
@@ -28,7 +28,7 @@ · 27 unmodified lines
2828 *
2929 */
3030
31−#define my_syscall0(num) \
31+#define __nolibc_syscall0(num) \
3232 ({ \
3333 register long _num __asm__ ("1") = (num); \
3434 register long _rc __asm__ ("2"); \
@@ -42,7 +42,7 @@ · 7 unmodified lines
4242 _rc; \
4343 })
4444
45−#define my_syscall1(num, arg1) \
45+#define __nolibc_syscall1(num, arg1) \
4646 ({ \
4747 register long _num __asm__ ("1") = (num); \
4848 register long _arg1 __asm__ ("2") = (long)(arg1); \
@@ -56,7 +56,7 @@ · 7 unmodified lines
5656 _arg1; \
5757 })
5858
59−#define my_syscall2(num, arg1, arg2) \
59+#define __nolibc_syscall2(num, arg1, arg2) \
6060 ({ \
6161 register long _num __asm__ ("1") = (num); \
6262 register long _arg1 __asm__ ("2") = (long)(arg1); \
@@ -71,7 +71,7 @@ · 8 unmodified lines
7171 _arg1; \
7272 })
7373
74−#define my_syscall3(num, arg1, arg2, arg3) \
74+#define __nolibc_syscall3(num, arg1, arg2, arg3) \
7575 ({ \
7676 register long _num __asm__ ("1") = (num); \
7777 register long _arg1 __asm__ ("2") = (long)(arg1); \
@@ -87,7 +87,7 @@ · 9 unmodified lines
8787 _arg1; \
8888 })
8989
90−#define my_syscall4(num, arg1, arg2, arg3, arg4) \
90+#define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \
9191 ({ \
9292 register long _num __asm__ ("1") = (num); \
9393 register long _arg1 __asm__ ("2") = (long)(arg1); \
@@ -104,7 +104,7 @@ · 10 unmodified lines
104104 _arg1; \
105105 })
106106
107−#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
107+#define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
108108 ({ \
109109 register long _num __asm__ ("1") = (num); \
110110 register long _arg1 __asm__ ("2") = (long)(arg1); \
@@ -123,7 +123,7 @@ · 12 unmodified lines
123123 _arg1; \
124124 })
125125
126−#define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
126+#define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
127127 ({ \
128128 register long _num __asm__ ("1") = (num); \
129129 register long _arg1 __asm__ ("2") = (long)(arg1); \
@@ -167,8 +167,8 @@ struct s390_mmap_arg_struct { · 37 unmodified lines
167167 };
168168
169169 static __attribute__((unused))
170−void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd,
171− off_t offset)
170+void *_sys_mmap(void *addr, size_t length, int prot, int flags, int fd,
171+ off_t offset)
172172 {
173173 struct s390_mmap_arg_struct args = {
174174 .addr = (unsigned long)addr,
@@ -179,22 +179,22 @@ void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd, · 4 unmodified lines
179179 .offset = (unsigned long)offset
180180 };
181181
182− return (void *)my_syscall1(__NR_mmap, &args);
182+ return (void *)__nolibc_syscall1(__NR_mmap, &args);
183183 }
184−#define sys_mmap sys_mmap
184+#define _sys_mmap _sys_mmap
185185
186186 static __attribute__((unused))
187−pid_t sys_fork(void)
187+pid_t _sys_fork(void)
188188 {
189− return my_syscall5(__NR_clone, 0, SIGCHLD, 0, 0, 0);
189+ return __nolibc_syscall5(__NR_clone, 0, SIGCHLD, 0, 0, 0);
190190 }
191−#define sys_fork sys_fork
191+#define _sys_fork _sys_fork
192192
193193 static __attribute__((unused))
194−pid_t sys_vfork(void)
194+pid_t _sys_vfork(void)
195195 {
196− return my_syscall5(__NR_clone, 0, CLONE_VM | CLONE_VFORK | SIGCHLD, 0, 0, 0);
196+ return __nolibc_syscall5(__NR_clone, 0, CLONE_VM | CLONE_VFORK | SIGCHLD, 0, 0, 0);
197197 }
198−#define sys_vfork sys_vfork
198+#define _sys_vfork _sys_vfork
199199
200200 #endif /* _NOLIBC_ARCH_S390_H */
tools/include/nolibc/arch-sh.h+7−7
@@ -19,7 +19,7 @@ · 18 unmodified lines
1919 * - syscall return value is in r0
2020 */
2121
22−#define my_syscall0(num) \
22+#define __nolibc_syscall0(num) \
2323 ({ \
2424 register long _num __asm__ ("r3") = (num); \
2525 register long _ret __asm__ ("r0"); \
@@ -33,7 +33,7 @@ · 7 unmodified lines
3333 _ret; \
3434 })
3535
36−#define my_syscall1(num, arg1) \
36+#define __nolibc_syscall1(num, arg1) \
3737 ({ \
3838 register long _num __asm__ ("r3") = (num); \
3939 register long _ret __asm__ ("r0"); \
@@ -48,7 +48,7 @@ · 8 unmodified lines
4848 _ret; \
4949 })
5050
51−#define my_syscall2(num, arg1, arg2) \
51+#define __nolibc_syscall2(num, arg1, arg2) \
5252 ({ \
5353 register long _num __asm__ ("r3") = (num); \
5454 register long _ret __asm__ ("r0"); \
@@ -64,7 +64,7 @@ · 9 unmodified lines
6464 _ret; \
6565 })
6666
67−#define my_syscall3(num, arg1, arg2, arg3) \
67+#define __nolibc_syscall3(num, arg1, arg2, arg3) \
6868 ({ \
6969 register long _num __asm__ ("r3") = (num); \
7070 register long _ret __asm__ ("r0"); \
@@ -81,7 +81,7 @@ · 10 unmodified lines
8181 _ret; \
8282 })
8383
84−#define my_syscall4(num, arg1, arg2, arg3, arg4) \
84+#define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \
8585 ({ \
8686 register long _num __asm__ ("r3") = (num); \
8787 register long _ret __asm__ ("r0"); \
@@ -99,7 +99,7 @@ · 11 unmodified lines
9999 _ret; \
100100 })
101101
102−#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
102+#define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
103103 ({ \
104104 register long _num __asm__ ("r3") = (num); \
105105 register long _ret __asm__ ("r0"); \
@@ -119,7 +119,7 @@ · 13 unmodified lines
119119 _ret; \
120120 })
121121
122−#define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
122+#define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
123123 ({ \
124124 register long _num __asm__ ("r3") = (num); \
125125 register long _ret __asm__ ("r0"); \
tools/include/nolibc/arch-sparc.h+13−13
@@ -38,7 +38,7 @@ · 37 unmodified lines
3838
3939 #endif /* __arch64__ */
4040
41−#define my_syscall0(num) \
41+#define __nolibc_syscall0(num) \
4242 ({ \
4343 register long _num __asm__ ("g1") = (num); \
4444 register long _arg1 __asm__ ("o0"); \
@@ -52,7 +52,7 @@ · 7 unmodified lines
5252 _arg1; \
5353 })
5454
55−#define my_syscall1(num, arg1) \
55+#define __nolibc_syscall1(num, arg1) \
5656 ({ \
5757 register long _num __asm__ ("g1") = (num); \
5858 register long _arg1 __asm__ ("o0") = (long)(arg1); \
@@ -66,7 +66,7 @@ · 7 unmodified lines
6666 _arg1; \
6767 })
6868
69−#define my_syscall2(num, arg1, arg2) \
69+#define __nolibc_syscall2(num, arg1, arg2) \
7070 ({ \
7171 register long _num __asm__ ("g1") = (num); \
7272 register long _arg1 __asm__ ("o0") = (long)(arg1); \
@@ -81,7 +81,7 @@ · 8 unmodified lines
8181 _arg1; \
8282 })
8383
84−#define my_syscall3(num, arg1, arg2, arg3) \
84+#define __nolibc_syscall3(num, arg1, arg2, arg3) \
8585 ({ \
8686 register long _num __asm__ ("g1") = (num); \
8787 register long _arg1 __asm__ ("o0") = (long)(arg1); \
@@ -97,7 +97,7 @@ · 9 unmodified lines
9797 _arg1; \
9898 })
9999
100−#define my_syscall4(num, arg1, arg2, arg3, arg4) \
100+#define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \
101101 ({ \
102102 register long _num __asm__ ("g1") = (num); \
103103 register long _arg1 __asm__ ("o0") = (long)(arg1); \
@@ -114,7 +114,7 @@ · 10 unmodified lines
114114 _arg1; \
115115 })
116116
117−#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
117+#define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
118118 ({ \
119119 register long _num __asm__ ("g1") = (num); \
120120 register long _arg1 __asm__ ("o0") = (long)(arg1); \
@@ -132,7 +132,7 @@ · 11 unmodified lines
132132 _arg1; \
133133 })
134134
135−#define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
135+#define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
136136 ({ \
137137 register long _num __asm__ ("g1") = (num); \
138138 register long _arg1 __asm__ ("o0") = (long)(arg1); \
@@ -175,12 +175,12 @@ void __attribute__((weak, noreturn)) __nolibc_entrypoint __no_stack_protector _s · 36 unmodified lines
175175 static pid_t getpid(void);
176176
177177 static __attribute__((unused))
178−pid_t sys_fork(void)
178+pid_t _sys_fork(void)
179179 {
180180 pid_t parent, ret;
181181
182182 parent = getpid();
183− ret = my_syscall0(__NR_fork);
183+ ret = __nolibc_syscall0(__NR_fork);
184184
185185 /* The syscall returns the parent pid in the child instead of 0 */
186186 if (ret == parent)
@@ -188,15 +188,15 @@ pid_t sys_fork(void) · 1 unmodified line
188188 else
189189 return ret;
190190 }
191−#define sys_fork sys_fork
191+#define _sys_fork _sys_fork
192192
193193 static __attribute__((unused))
194−pid_t sys_vfork(void)
194+pid_t _sys_vfork(void)
195195 {
196196 pid_t parent, ret;
197197
198198 parent = getpid();
199− ret = my_syscall0(__NR_vfork);
199+ ret = __nolibc_syscall0(__NR_vfork);
200200
201201 /* The syscall returns the parent pid in the child instead of 0 */
202202 if (ret == parent)
@@ -204,6 +204,6 @@ pid_t sys_vfork(void) · 1 unmodified line
204204 else
205205 return ret;
206206 }
207−#define sys_vfork sys_vfork
207+#define _sys_vfork _sys_vfork
208208
209209 #endif /* _NOLIBC_ARCH_SPARC_H */
tools/include/nolibc/arch-x86.h+34−34
@@ -30,7 +30,7 @@ · 29 unmodified lines
3030 */
3131 #define __ARCH_WANT_SYS_OLD_SELECT
3232
33−#define my_syscall0(num) \
33+#define __nolibc_syscall0(num) \
3434 ({ \
3535 long _ret; \
3636 register long _num __asm__ ("eax") = (num); \
@@ -44,7 +44,7 @@ · 7 unmodified lines
4444 _ret; \
4545 })
4646
47−#define my_syscall1(num, arg1) \
47+#define __nolibc_syscall1(num, arg1) \
4848 ({ \
4949 long _ret; \
5050 register long _num __asm__ ("eax") = (num); \
@@ -60,7 +60,7 @@ · 9 unmodified lines
6060 _ret; \
6161 })
6262
63−#define my_syscall2(num, arg1, arg2) \
63+#define __nolibc_syscall2(num, arg1, arg2) \
6464 ({ \
6565 long _ret; \
6666 register long _num __asm__ ("eax") = (num); \
@@ -77,7 +77,7 @@ · 10 unmodified lines
7777 _ret; \
7878 })
7979
80−#define my_syscall3(num, arg1, arg2, arg3) \
80+#define __nolibc_syscall3(num, arg1, arg2, arg3) \
8181 ({ \
8282 long _ret; \
8383 register long _num __asm__ ("eax") = (num); \
@@ -95,7 +95,7 @@ · 11 unmodified lines
9595 _ret; \
9696 })
9797
98−#define my_syscall4(num, arg1, arg2, arg3, arg4) \
98+#define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \
9999 ({ \
100100 long _ret; \
101101 register long _num __asm__ ("eax") = (num); \
@@ -114,7 +114,7 @@ · 12 unmodified lines
114114 _ret; \
115115 })
116116
117−#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
117+#define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
118118 ({ \
119119 long _ret; \
120120 register long _num __asm__ ("eax") = (num); \
@@ -134,27 +134,27 @@ · 13 unmodified lines
134134 _ret; \
135135 })
136136
137−#define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
138−({ \
139− long _eax = (long)(num); \
140− long _arg6 = (long)(arg6); /* Always in memory */ \
141− __asm__ volatile ( \
142− "pushl %[_arg6]\n\t" \
143− "pushl %%ebp\n\t" \
144− "movl 4(%%esp),%%ebp\n\t" \
145− "int $0x80\n\t" \
146− "popl %%ebp\n\t" \
147− "addl $4,%%esp\n\t" \
148− : "+a"(_eax) /* %eax */ \
149− : "b"(arg1), /* %ebx */ \
150− "c"(arg2), /* %ecx */ \
151− "d"(arg3), /* %edx */ \
152− "S"(arg4), /* %esi */ \
153− "D"(arg5), /* %edi */ \
154− [_arg6]"m"(_arg6) /* memory */ \
155− : "memory", "cc" \
156− ); \
157− _eax; \
137+#define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
138+({ \
139+ long _eax = (long)(num); \
140+ long _arg6 = (long)(arg6); /* Always in memory */ \
141+ __asm__ volatile ( \
142+ "pushl %[_arg6]\n\t" \
143+ "pushl %%ebp\n\t" \
144+ "movl 4(%%esp),%%ebp\n\t" \
145+ "int $0x80\n\t" \
146+ "popl %%ebp\n\t" \
147+ "addl $4,%%esp\n\t" \
148+ : "+a"(_eax) /* %eax */ \
149+ : "b"(arg1), /* %ebx */ \
150+ "c"(arg2), /* %ecx */ \
151+ "d"(arg3), /* %edx */ \
152+ "S"(arg4), /* %esi */ \
153+ "D"(arg5), /* %edi */ \
154+ [_arg6]"m"(_arg6) /* memory */ \
155+ : "memory", "cc" \
156+ ); \
157+ _eax; \
158158 })
159159
160160 #ifndef NOLIBC_NO_RUNTIME
@@ -200,7 +200,7 @@ void __attribute__((weak, noreturn)) __nolibc_entrypoint __no_stack_protector _s · 39 unmodified lines
200200 *
201201 */
202202
203−#define my_syscall0(num) \
203+#define __nolibc_syscall0(num) \
204204 ({ \
205205 long _ret; \
206206 register long _num __asm__ ("rax") = (num); \
@@ -214,7 +214,7 @@ void __attribute__((weak, noreturn)) __nolibc_entrypoint __no_stack_protector _s · 7 unmodified lines
214214 _ret; \
215215 })
216216
217−#define my_syscall1(num, arg1) \
217+#define __nolibc_syscall1(num, arg1) \
218218 ({ \
219219 long _ret; \
220220 register long _num __asm__ ("rax") = (num); \
@@ -230,7 +230,7 @@ void __attribute__((weak, noreturn)) __nolibc_entrypoint __no_stack_protector _s · 9 unmodified lines
230230 _ret; \
231231 })
232232
233−#define my_syscall2(num, arg1, arg2) \
233+#define __nolibc_syscall2(num, arg1, arg2) \
234234 ({ \
235235 long _ret; \
236236 register long _num __asm__ ("rax") = (num); \
@@ -247,7 +247,7 @@ void __attribute__((weak, noreturn)) __nolibc_entrypoint __no_stack_protector _s · 10 unmodified lines
247247 _ret; \
248248 })
249249
250−#define my_syscall3(num, arg1, arg2, arg3) \
250+#define __nolibc_syscall3(num, arg1, arg2, arg3) \
251251 ({ \
252252 long _ret; \
253253 register long _num __asm__ ("rax") = (num); \
@@ -265,7 +265,7 @@ void __attribute__((weak, noreturn)) __nolibc_entrypoint __no_stack_protector _s · 11 unmodified lines
265265 _ret; \
266266 })
267267
268−#define my_syscall4(num, arg1, arg2, arg3, arg4) \
268+#define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \
269269 ({ \
270270 long _ret; \
271271 register long _num __asm__ ("rax") = (num); \
@@ -284,7 +284,7 @@ void __attribute__((weak, noreturn)) __nolibc_entrypoint __no_stack_protector _s · 12 unmodified lines
284284 _ret; \
285285 })
286286
287−#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
287+#define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
288288 ({ \
289289 long _ret; \
290290 register long _num __asm__ ("rax") = (num); \
@@ -304,7 +304,7 @@ void __attribute__((weak, noreturn)) __nolibc_entrypoint __no_stack_protector _s · 13 unmodified lines
304304 _ret; \
305305 })
306306
307−#define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
307+#define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
308308 ({ \
309309 long _ret; \
310310 register long _num __asm__ ("rax") = (num); \
tools/include/nolibc/byteswap.h+21−0
@@ -0,0 +1,21 @@
1+/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
2+/*
3+ * Byte swapping for NOLIBC
4+ * Copyright (C) 2026 Thomas Weißschuh <linux@weissschuh.net>
5+ */
6+
7+/* make sure to include all global symbols */
8+#include "nolibc.h"
9+
10+#ifndef _NOLIBC_BYTESWAP_H
11+#define _NOLIBC_BYTESWAP_H
12+
13+#include "stdint.h"
14+
15+#include <linux/swab.h>
16+
17+#define bswap_16(_x) __swab16(_x)
18+#define bswap_32(_x) __swab32(_x)
19+#define bswap_64(_x) __swab64(_x)
20+
21+#endif /* _NOLIBC_BYTESWAP_H */
tools/include/nolibc/compiler.h+20−1
@@ -47,6 +47,12 @@ · 46 unmodified lines
4747 # define __nolibc_fallthrough do { } while (0)
4848 #endif /* __nolibc_has_attribute(fallthrough) */
4949
50+#if defined(__STDC_VERSION__)
51+# define __nolibc_stdc_version __STDC_VERSION__
52+#else
53+# define __nolibc_stdc_version 0
54+#endif
55+
5056 #define __nolibc_version(_major, _minor, _patch) ((_major) * 10000 + (_minor) * 100 + (_patch))
5157
5258 #ifdef __GNUC__
@@ -63,7 +69,7 @@ · 10 unmodified lines
6369 # define __nolibc_clang_version 0
6470 #endif /* __clang__ */
6571
66−#if __STDC_VERSION__ >= 201112L || \
72+#if __nolibc_stdc_version >= 201112L || \
6773 __nolibc_gnuc_version >= __nolibc_version(4, 6, 0) || \
6874 __nolibc_clang_version >= __nolibc_version(3, 0, 0)
6975 # define __nolibc_static_assert(_t) _Static_assert(_t, "")
@@ -71,4 +77,17 @@ · 1 unmodified line
7177 # define __nolibc_static_assert(_t)
7278 #endif
7379
80+/* Make the optimizer believe the variable can be manipulated arbitrarily. */
81+#define _NOLIBC_OPTIMIZER_HIDE_VAR(var) __asm__ ("" : "+r" (var))
82+
83+#if __nolibc_has_feature(undefined_behavior_sanitizer)
84+# if defined(__clang__)
85+# define __nolibc_no_sanitize_undefined __attribute__((no_sanitize("function")))
86+# else
87+# define __nolibc_no_sanitize_undefined __attribute__((no_sanitize_undefined))
88+# endif
89+#else
90+# define __nolibc_no_sanitize_undefined
91+#endif
92+
7493 #endif /* _NOLIBC_COMPILER_H */
tools/include/nolibc/crt.h+27−4
@@ -17,6 +17,7 @@ const unsigned long *_auxv __attribute__((weak)); · 16 unmodified lines
1717 void _start(void);
1818 static void __stack_chk_init(void);
1919 static void exit(int);
20+static char *strrchr(const char *s, int c);
2021
2122 extern void (*const __preinit_array_start[])(int, char **, char**) __attribute__((weak));
2223 extern void (*const __preinit_array_end[])(int, char **, char**) __attribute__((weak));
@@ -27,11 +28,26 @@ extern void (*const __init_array_end[])(int, char **, char**) __attribute__((wea · 4 unmodified lines
2728 extern void (*const __fini_array_start[])(void) __attribute__((weak));
2829 extern void (*const __fini_array_end[])(void) __attribute__((weak));
2930
31+#ifndef NOLIBC_IGNORE_ERRNO
32+extern char *program_invocation_name __attribute__((weak));
33+extern char *program_invocation_short_name __attribute__((weak));
34+
35+static __inline__
36+char *__nolibc_program_invocation_short_name(char *long_name)
37+{
38+
39+ char *short_name;
40+
41+ short_name = strrchr(long_name, '/');
42+ if (!short_name || !short_name[0])
43+ return long_name;
44+
45+ return short_name + 1;
46+}
47+#endif /* NOLIBC_IGNORE_ERRNO */
48+
3049 void _start_c(long *sp);
31−__attribute__((weak,used))
32−#if __nolibc_has_feature(undefined_behavior_sanitizer)
33− __attribute__((no_sanitize("function")))
34−#endif
50+__attribute__((weak,used)) __nolibc_no_sanitize_undefined
3551 void _start_c(long *sp)
3652 {
3753 long argc;
@@ -76,6 +92,13 @@ void _start_c(long *sp) · 38 unmodified lines
7692 ;
7793 _auxv = auxv;
7894
95+#ifndef NOLIBC_IGNORE_ERRNO
96+ if (argc > 0 && argv[0]) {
97+ program_invocation_name = argv[0];
98+ program_invocation_short_name = __nolibc_program_invocation_short_name(argv[0]);
99+ }
100+#endif /* NOLIBC_IGNORE_ERRNO */
101+
79102 for (ctor_func = __preinit_array_start; ctor_func < __preinit_array_end; ctor_func++)
80103 (*ctor_func)(argc, argv, envp);
81104 for (ctor_func = __init_array_start; ctor_func < __init_array_end; ctor_func++)
tools/include/nolibc/dirent.h+2−2
@@ -73,7 +73,7 @@ int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result) · 72 unmodified lines
7373
7474 fd = ~i;
7575
76− ret = sys_getdents64(fd, ldir, sizeof(buf));
76+ ret = _sys_getdents64(fd, ldir, sizeof(buf));
7777 if (ret < 0)
7878 return -ret;
7979 if (ret == 0) {
@@ -86,7 +86,7 @@ int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result) · 6 unmodified lines
8686 * readdir() can only return one entry at a time.
8787 * Make sure the non-returned ones are not skipped.
8888 */
89− ret = sys_lseek(fd, ldir->d_off, SEEK_SET);
89+ ret = _sys_lseek(fd, ldir->d_off, SEEK_SET);
9090 if (ret < 0)
9191 return -ret;
9292
tools/include/nolibc/endian.h+32−0
@@ -0,0 +1,32 @@
1+/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
2+/*
3+ * Byte order conversion for NOLIBC
4+ * Copyright (C) 2026 Thomas Weißschuh <linux@weissschuh.net>
5+ */
6+
7+/* make sure to include all global symbols */
8+#include "nolibc.h"
9+
10+#ifndef _NOLIBC_ENDIAN_H
11+#define _NOLIBC_ENDIAN_H
12+
13+#include "stdint.h"
14+
15+#include <asm/byteorder.h>
16+
17+#define htobe16(_x) __cpu_to_be16(_x)
18+#define htole16(_x) __cpu_to_le16(_x)
19+#define be16toh(_x) __be16_to_cpu(_x)
20+#define le16toh(_x) __le16_to_cpu(_x)
21+
22+#define htobe32(_x) __cpu_to_be32(_x)
23+#define htole32(_x) __cpu_to_le32(_x)
24+#define be32toh(_x) __be32_to_cpu(_x)
25+#define le32toh(_x) __le32_to_cpu(_x)
26+
27+#define htobe64(_x) __cpu_to_be64(_x)
28+#define htole64(_x) __cpu_to_le64(_x)
29+#define be64toh(_x) __be64_to_cpu(_x)
30+#define le64toh(_x) __le64_to_cpu(_x)
31+
32+#endif /* _NOLIBC_ENDIAN_H */
tools/include/nolibc/err.h+87−0
@@ -0,0 +1,87 @@
1+/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
2+/*
3+ * formatted error message for NOLIBC
4+ * Copyright (C) 2026 Thomas Weißschuh <linux@weissschuh.net>
5+ */
6+
7+/* make sure to include all global symbols */
8+#include "nolibc.h"
9+
10+#ifndef _NOLIBC_ERR_H
11+#define _NOLIBC_ERR_H
12+
13+#include "errno.h"
14+#include "stdarg.h"
15+#include "sys.h"
16+
17+static __attribute__((unused))
18+void vwarn(const char *fmt, va_list args)
19+{
20+ fprintf(stderr, "%s: ", program_invocation_short_name);
21+ vfprintf(stderr, fmt, args);
22+ fprintf(stderr, ": %m\n");
23+}
24+
25+static __attribute__((unused))
26+void vwarnx(const char *fmt, va_list args)
27+{
28+ fprintf(stderr, "%s: ", program_invocation_short_name);
29+ vfprintf(stderr, fmt, args);
30+ fprintf(stderr, "\n");
31+}
32+
33+static __attribute__((unused))
34+void warn(const char *fmt, ...)
35+{
36+ va_list args;
37+
38+ va_start(args, fmt);
39+ vwarn(fmt, args);
40+ va_end(args);
41+}
42+
43+static __attribute__((unused))
44+void warnx(const char *fmt, ...)
45+{
46+ va_list args;
47+
48+ va_start(args, fmt);
49+ vwarnx(fmt, args);
50+ va_end(args);
51+}
52+
53+static __attribute__((noreturn, unused))
54+void verr(int eval, const char *fmt, va_list args)
55+{
56+ vwarn(fmt, args);
57+ exit(eval);
58+}
59+
60+static __attribute__((noreturn, unused))
61+void verrx(int eval, const char *fmt, va_list args)
62+{
63+ warnx(fmt, args);
64+ exit(eval);
65+}
66+
67+static __attribute__((noreturn, unused))
68+void err(int eval, const char *fmt, ...)
69+{
70+ va_list args;
71+
72+ va_start(args, fmt);
73+ verr(eval, fmt, args);
74+ va_end(args);
75+}
76+
77+static __attribute__((noreturn, unused))
78+void errx(int eval, const char *fmt, ...)
79+{
80+ va_list args;
81+
82+ va_start(args, fmt);
83+ verrx(eval, fmt, args);
84+ va_end(args);
85+}
86+
87+#endif /* _NOLIBC_ERR_H */
tools/include/nolibc/errno.h+4−0
@@ -15,8 +15,12 @@ · 14 unmodified lines
1515 #ifndef NOLIBC_IGNORE_ERRNO
1616 #define SET_ERRNO(v) do { errno = (v); } while (0)
1717 int errno __attribute__((weak));
18+char *program_invocation_name __attribute__((weak)) = "";
19+char *program_invocation_short_name __attribute__((weak)) = "";
1820 #else
1921 #define SET_ERRNO(v) do { } while (0)
22+#define program_invocation_name ""
23+#define program_invocation_short_name ""
2024 #endif
2125
2226
tools/include/nolibc/fcntl.h+6−6
@@ -19,9 +19,9 @@ · 18 unmodified lines
1919 */
2020
2121 static __attribute__((unused))
22−int sys_openat(int dirfd, const char *path, int flags, mode_t mode)
22+int _sys_openat(int dirfd, const char *path, int flags, mode_t mode)
2323 {
24− return my_syscall4(__NR_openat, dirfd, path, flags, mode);
24+ return __nolibc_syscall4(__NR_openat, dirfd, path, flags, mode);
2525 }
2626
2727 static __attribute__((unused))
@@ -37,7 +37,7 @@ int openat(int dirfd, const char *path, int flags, ...) · 9 unmodified lines
3737 va_end(args);
3838 }
3939
40− return __sysret(sys_openat(dirfd, path, flags, mode));
40+ return __sysret(_sys_openat(dirfd, path, flags, mode));
4141 }
4242
4343 /*
@@ -45,9 +45,9 @@ int openat(int dirfd, const char *path, int flags, ...) · 1 unmodified line
4545 */
4646
4747 static __attribute__((unused))
48−int sys_open(const char *path, int flags, mode_t mode)
48+int _sys_open(const char *path, int flags, mode_t mode)
4949 {
50− return my_syscall4(__NR_openat, AT_FDCWD, path, flags, mode);
50+ return __nolibc_syscall4(__NR_openat, AT_FDCWD, path, flags, mode);
5151 }
5252
5353 static __attribute__((unused))
@@ -63,7 +63,7 @@ int open(const char *path, int flags, ...) · 9 unmodified lines
6363 va_end(args);
6464 }
6565
66− return __sysret(sys_open(path, flags, mode));
66+ return __sysret(_sys_open(path, flags, mode));
6767 }
6868
6969 #endif /* _NOLIBC_FCNTL_H */
tools/include/nolibc/nolibc.h+8−5
@@ -12,24 +12,24 @@ · 11 unmodified lines
1212 *
1313 * Syscalls are split into 3 levels:
1414 * - The lower level is the arch-specific syscall() definition, consisting in
15− * assembly code in compound expressions. These are called my_syscall0() to
16− * my_syscall6() depending on the number of arguments. All input arguments
15+ * assembly code in compound expressions. These are called __nolibc_syscall0() to
16+ * __nolibc_syscall6() depending on the number of arguments. All input arguments
1717 * are castto a long stored in a register. These expressions always return
1818 * the syscall's return value as a signed long value which is often either
1919 * a pointer or the negated errno value.
2020 *
2121 * - The second level is mostly architecture-independent. It is made of
22− * static functions called sys_<name>() which rely on my_syscallN()
22+ * static functions called _sys_<name>() which rely on __nolibc_syscallN()
2323 * depending on the syscall definition. These functions are responsible
2424 * for exposing the appropriate types for the syscall arguments (int,
2525 * pointers, etc) and for setting the appropriate return type (often int).
2626 * A few of them are architecture-specific because the syscalls are not all
2727 * mapped exactly the same among architectures. For example, some archs do
28− * not implement select() and need pselect6() instead, so the sys_select()
28+ * not implement select() and need pselect6() instead, so the _sys_select()
2929 * function will have to abstract this.
3030 *
3131 * - The third level is the libc call definition. It exposes the lower raw
32− * sys_<name>() calls in a way that looks like what a libc usually does,
32+ * _sys_<name>() calls in a way that looks like what a libc usually does,
3333 * takes care of specific input values, and of setting errno upon error.
3434 * There can be minor variations compared to standard libc calls.
3535 *
@@ -130,6 +130,9 @@ · 94 unmodified lines
130130 #include "getopt.h"
131131 #include "poll.h"
132132 #include "math.h"
133+#include "err.h"
134+#include "byteswap.h"
135+#include "endian.h"
133136
134137 /* Used by programs to avoid std includes */
135138 #define NOLIBC
tools/include/nolibc/poll.h+4−4
@@ -21,7 +21,7 @@ · 20 unmodified lines
2121 */
2222
2323 static __attribute__((unused))
24−int sys_poll(struct pollfd *fds, int nfds, int timeout)
24+int _sys_poll(struct pollfd *fds, int nfds, int timeout)
2525 {
2626 #if defined(__NR_ppoll_time64)
2727 struct __kernel_timespec t;
@@ -30,7 +30,7 @@ int sys_poll(struct pollfd *fds, int nfds, int timeout) · 2 unmodified lines
3030 t.tv_sec = timeout / 1000;
3131 t.tv_nsec = (timeout % 1000) * 1000000;
3232 }
33− return my_syscall5(__NR_ppoll_time64, fds, nfds, (timeout >= 0) ? &t : NULL, NULL, 0);
33+ return __nolibc_syscall5(__NR_ppoll_time64, fds, nfds, (timeout >= 0) ? &t : NULL, NULL, 0);
3434 #else
3535 struct __kernel_old_timespec t;
3636
@@ -38,14 +38,14 @@ int sys_poll(struct pollfd *fds, int nfds, int timeout) · 1 unmodified line
3838 t.tv_sec = timeout / 1000;
3939 t.tv_nsec = (timeout % 1000) * 1000000;
4040 }
41− return my_syscall5(__NR_ppoll, fds, nfds, (timeout >= 0) ? &t : NULL, NULL, 0);
41+ return __nolibc_syscall5(__NR_ppoll, fds, nfds, (timeout >= 0) ? &t : NULL, NULL, 0);
4242 #endif
4343 }
4444
4545 static __attribute__((unused))
4646 int poll(struct pollfd *fds, int nfds, int timeout)
4747 {
48− return __sysret(sys_poll(fds, nfds, timeout));
48+ return __sysret(_sys_poll(fds, nfds, timeout));
4949 }
5050
5151 #endif /* _NOLIBC_POLL_H */
tools/include/nolibc/sched.h+6−6
@@ -19,15 +19,15 @@ · 18 unmodified lines
1919 */
2020
2121 static __attribute__((unused))
22−int sys_setns(int fd, int nstype)
22+int _sys_setns(int fd, int nstype)
2323 {
24− return my_syscall2(__NR_setns, fd, nstype);
24+ return __nolibc_syscall2(__NR_setns, fd, nstype);
2525 }
2626
2727 static __attribute__((unused))
2828 int setns(int fd, int nstype)
2929 {
30− return __sysret(sys_setns(fd, nstype));
30+ return __sysret(_sys_setns(fd, nstype));
3131 }
3232
3333
@@ -36,15 +36,15 @@ int setns(int fd, int nstype) · 2 unmodified lines
3636 */
3737
3838 static __attribute__((unused))
39−int sys_unshare(int flags)
39+int _sys_unshare(int flags)
4040 {
41− return my_syscall1(__NR_unshare, flags);
41+ return __nolibc_syscall1(__NR_unshare, flags);
4242 }
4343
4444 static __attribute__((unused))
4545 int unshare(int flags)
4646 {
47− return __sysret(sys_unshare(flags));
47+ return __sysret(_sys_unshare(flags));
4848 }
4949
5050 #endif /* _NOLIBC_SCHED_H */
tools/include/nolibc/signal.h+1−1
@@ -20,7 +20,7 @@ int raise(int signal); · 19 unmodified lines
2020 __attribute__((weak,unused,section(".text.nolibc_raise")))
2121 int raise(int signal)
2222 {
23− return sys_kill(sys_getpid(), signal);
23+ return _sys_kill(_sys_getpid(), signal);
2424 }
2525
2626 #endif /* _NOLIBC_SIGNAL_H */
tools/include/nolibc/stackprotector.h+4−4
@@ -24,9 +24,9 @@ __attribute__((weak,used,noreturn,section(".text.nolibc_stack_chk"))) · 23 unmodified lines
2424 void __stack_chk_fail(void)
2525 {
2626 pid_t pid;
27− my_syscall3(__NR_write, STDERR_FILENO, "!!Stack smashing detected!!\n", 28);
28− pid = my_syscall0(__NR_getpid);
29− my_syscall2(__NR_kill, pid, SIGABRT);
27+ __nolibc_syscall3(__NR_write, STDERR_FILENO, "!!Stack smashing detected!!\n", 28);
28+ pid = __nolibc_syscall0(__NR_getpid);
29+ __nolibc_syscall2(__NR_kill, pid, SIGABRT);
3030 for (;;);
3131 }
3232
@@ -42,7 +42,7 @@ uintptr_t __stack_chk_guard; · 9 unmodified lines
4242
4343 static __no_stack_protector void __stack_chk_init(void)
4444 {
45− my_syscall3(__NR_getrandom, &__stack_chk_guard, sizeof(__stack_chk_guard), 0);
45+ __nolibc_syscall3(__NR_getrandom, &__stack_chk_guard, sizeof(__stack_chk_guard), 0);
4646 /* a bit more randomness in case getrandom() fails, ensure the guard is never 0 */
4747 if (__stack_chk_guard != (uintptr_t) &__stack_chk_guard)
4848 __stack_chk_guard ^= (uintptr_t) &__stack_chk_guard;
tools/include/nolibc/std.h+1−1
@@ -19,7 +19,7 @@ · 18 unmodified lines
1919 #include <linux/types.h>
2020
2121 /* those are commonly provided by sys/types.h */
22−typedef unsigned int dev_t;
22+typedef uint64_t dev_t;
2323 typedef uint64_t ino_t;
2424 typedef unsigned int mode_t;
2525 typedef signed int pid_t;
tools/include/nolibc/stddef.h+1−1
@@ -18,7 +18,7 @@ · 17 unmodified lines
1818 #endif
1919
2020 #ifndef offsetof
21−#define offsetof(TYPE, FIELD) ((size_t) &((TYPE *)0)->FIELD)
21+#define offsetof(TYPE, FIELD) __builtin_offsetof(TYPE, FIELD)
2222 #endif
2323
2424 #endif /* _NOLIBC_STDDEF_H */
tools/include/nolibc/stdio.h+437−128
@@ -291,156 +291,371 @@ int fseek(FILE *stream, long offset, int whence) · 290 unmodified lines
291291 }
292292
293293
294−/* minimal printf(). It supports the following formats:
295− * - %[l*]{d,u,c,x,p}
296− * - %s
297− * - unknown modifiers are ignored.
294+/* printf(). Supports most of the normal integer and string formats.
295+ * - %[#0-+ ][width|*[.precision|*}][{l,t,z,ll,L,j,q}]{c,d,i,u,o,x,X,p,s,m,%}
296+ * - %% generates a single %
297+ * - %m outputs strerror(errno).
298+ * - %X outputs a..f the same as %x.
299+ * - No support for floating point or wide characters.
300+ * - Invalid formats are copied to the output buffer.
301+ *
302+ * Called by vfprintf() and snprintf() to do the actual formatting.
303+ * The callers provide a callback function to save the formatted data.
304+ * The callback function is called multiple times:
305+ * - for each group of literal characters in the format string.
306+ * - for field padding.
307+ * - for each conversion specifier.
308+ * - with (NULL, 0) at the end of the __nolibc_printf.
309+ * If the callback returns non-zero __nolibc_printf() immediately returns -1.
298310 */
299−typedef int (*__nolibc_printf_cb)(intptr_t state, const char *buf, size_t size);
300311
301−static __attribute__((unused, format(printf, 4, 0)))
302−int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char *fmt, va_list args)
312+typedef int (*__nolibc_printf_cb)(void *state, const char *buf, size_t size);
313+
314+/* This code uses 'flag' variables that are indexed by the low 6 bits
315+ * of characters to optimise checks for multiple characters.
316+ *
317+ * _NOLIBC_PF_FLAGS_CONTAIN(flags, 'a', 'b'. ...)
318+ * returns non-zero if the bit for any of the specified characters is set.
319+ *
320+ * _NOLIBC_PF_CHAR_IS_ONE_OF(ch, 'a', 'b'. ...)
321+ * returns the flag bit for ch if it is one of the specified characters.
322+ * All the characters must be in the same 32 character block (non-alphabetic,
323+ * upper case, or lower case) of the ASCII character set.
324+ */
325+#define _NOLIBC_PF_FLAG(ch) (1u << ((ch) & 0x1f))
326+#define _NOLIBC_PF_FLAG_NZ(ch) ((ch) ? _NOLIBC_PF_FLAG(ch) : 0)
327+#define _NOLIBC_PF_FLAG8(cmp_1, cmp_2, cmp_3, cmp_4, cmp_5, cmp_6, cmp_7, cmp_8, ...) \
328+ (_NOLIBC_PF_FLAG_NZ(cmp_1) | _NOLIBC_PF_FLAG_NZ(cmp_2) | \
329+ _NOLIBC_PF_FLAG_NZ(cmp_3) | _NOLIBC_PF_FLAG_NZ(cmp_4) | \
330+ _NOLIBC_PF_FLAG_NZ(cmp_5) | _NOLIBC_PF_FLAG_NZ(cmp_6) | \
331+ _NOLIBC_PF_FLAG_NZ(cmp_7) | _NOLIBC_PF_FLAG_NZ(cmp_8))
332+#define _NOLIBC_PF_FLAGS_CONTAIN(flags, ...) \
333+ ((flags) & _NOLIBC_PF_FLAG8(__VA_ARGS__, 0, 0, 0, 0, 0, 0, 0))
334+#define _NOLIBC_PF_CHAR_IS_ONE_OF(ch, cmp_1, ...) \
335+ ((unsigned int)(ch) - (cmp_1 & 0xe0) > 0x1f ? 0 : \
336+ _NOLIBC_PF_FLAGS_CONTAIN(_NOLIBC_PF_FLAG(ch), cmp_1, __VA_ARGS__))
337+
338+static __attribute__((unused, format(printf, 3, 0)))
339+int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list args)
303340 {
304− char escape, lpref, c;
341+ char ch;
305342 unsigned long long v;
306− unsigned int written, width;
307− size_t len, ofs, w;
308− char tmpbuf[21];
343+ long long signed_v;
344+ int written, width, precision, len;
345+ unsigned int flags, ch_flag;
346+ char outbuf[2 + 31 + 22 + 1];
347+ char *out;
309348 const char *outstr;
349+ unsigned int sign_prefix;
350+ int got_width;
310351
311− written = ofs = escape = lpref = 0;
352+ written = 0;
312353 while (1) {
313− c = fmt[ofs++];
354+ outstr = fmt;
355+ ch = *fmt++;
356+ if (!ch)
357+ break;
358+
314359 width = 0;
360+ flags = 0;
361+ if (ch != '%') {
362+ while (*fmt && *fmt != '%')
363+ fmt++;
364+ /* Output characters from the format string. */
365+ len = fmt - outstr;
366+ goto do_output;
367+ }
315368
316− if (escape) {
317− /* we're in an escape sequence, ofs == 1 */
318− escape = 0;
369+ /* we're in a format sequence */
319370
320− /* width */
321− while (c >= '0' && c <= '9') {
322− width *= 10;
323− width += c - '0';
371+ /* Conversion flag characters */
372+ while (1) {
373+ ch = *fmt++;
374+ ch_flag = _NOLIBC_PF_CHAR_IS_ONE_OF(ch, ' ', '#', '+', '-', '0');
375+ if (!ch_flag)
376+ break;
377+ flags |= ch_flag;
378+ }
324379
325− c = fmt[ofs++];
380+ /* Width and precision */
381+ for (got_width = 0;; ch = *fmt++) {
382+ if (ch == '*') {
383+ precision = va_arg(args, int);
384+ ch = *fmt++;
385+ } else {
386+ for (precision = 0; ch >= '0' && ch <= '9'; ch = *fmt++)
387+ precision = precision * 10 + (ch - '0');
388+ }
389+ if (got_width)
390+ break;
391+ width = precision;
392+ if (ch != '.') {
393+ /* Default precision for strings */
394+ precision = -1;
395+ break;
396+ }
397+ got_width = 1;
398+ }
399+ /* A negative width (e.g. from "%*s") requests left justify. */
400+ if (width < 0) {
401+ width = -width;
402+ flags |= _NOLIBC_PF_FLAG('-');
403+ }
404+
405+ /* Length modifier.
406+ * They miss the conversion flags characters " #+-0" so can go into flags.
407+ * Change both L and ll to j (all always 64bit).
408+ */
409+ if (ch == 'L')
410+ ch = 'j';
411+ ch_flag = _NOLIBC_PF_CHAR_IS_ONE_OF(ch, 'l', 't', 'z', 'j', 'q');
412+ if (ch_flag != 0) {
413+ if (ch == 'l' && fmt[0] == 'l') {
414+ fmt++;
415+ ch_flag = _NOLIBC_PF_FLAG('j');
416+ }
417+ flags |= ch_flag;
418+ ch = *fmt++;
419+ }
420+
421+ /* Conversion specifiers. */
422+
423+ /* Numeric and pointer conversion specifiers.
424+ *
425+ * Use an explicit bound check (rather than _NOLIBC_PF_CHAR_IS_ONE_OF())
426+ * so that 'X' can be allowed through.
427+ * 'X' gets treated and 'x' because _NOLIBC_PF_FLAG() returns the same
428+ * value for both.
429+ *
430+ * We need to check for "%p" or "%#x" later, merging here gives better code.
431+ * But '#' collides with 'c' so shift right.
432+ */
433+ ch_flag = _NOLIBC_PF_FLAG(ch) | (flags & _NOLIBC_PF_FLAG('#')) >> 1;
434+ if (((ch >= 'a' && ch <= 'z') || ch == 'X') &&
435+ _NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'c', 'd', 'i', 'u', 'o', 'x', 'p', 's')) {
436+ /* 'long' is needed for pointer/string conversions and ltz lengths.
437+ * A single test can be used provided 'p' (the same bit as '0')
438+ * is masked from flags.
439+ */
440+ if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag | (flags & ~_NOLIBC_PF_FLAG('p')),
441+ 'p', 's', 'l', 't', 'z')) {
442+ v = va_arg(args, unsigned long);
443+ signed_v = (long)v;
444+ } else if (_NOLIBC_PF_FLAGS_CONTAIN(flags, 'j', 'q')) {
445+ v = va_arg(args, unsigned long long);
446+ signed_v = v;
447+ } else {
448+ v = va_arg(args, unsigned int);
449+ signed_v = (int)v;
450+ }
451+
452+ if (ch == 'c') {
453+ /* "%c" - single character. */
454+ outbuf[0] = v;
455+ len = 1;
456+ outstr = outbuf;
457+ goto do_output;
326458 }
327459
328− if (c == 'c' || c == 'd' || c == 'u' || c == 'x' || c == 'p') {
329− char *out = tmpbuf;
330−
331− if (c == 'p')
332− v = va_arg(args, unsigned long);
333− else if (lpref) {
334− if (lpref > 1)
335− v = va_arg(args, unsigned long long);
336− else
337− v = va_arg(args, unsigned long);
338− } else
339− v = va_arg(args, unsigned int);
340−
341− if (c == 'd') {
342− /* sign-extend the value */
343− if (lpref == 0)
344− v = (long long)(int)v;
345− else if (lpref == 1)
346− v = (long long)(long)v;
460+ if (ch == 's') {
461+ /* "%s" - character string. */
462+ outstr = (const char *)(uintptr_t)v;
463+ if (!outstr) {
464+ outstr = "(null)";
465+ /* Match glibc, nothing output if precision too small */
466+ len = precision < 0 || precision >= 6 ? 6 : 0;
467+ goto do_output;
347468 }
469+ goto do_strlen_output;
470+ }
348471
349− switch (c) {
350− case 'c':
351− out[0] = v;
352− out[1] = 0;
353− break;
354− case 'd':
355− i64toa_r(v, out);
356− break;
357− case 'u':
358− u64toa_r(v, out);
359− break;
360− case 'p':
361− *(out++) = '0';
362− *(out++) = 'x';
363− __nolibc_fallthrough;
364− default: /* 'x' and 'p' above */
365− u64toh_r(v, out);
366− break;
472+ /* The 'sign_prefix' can be zero, one or two ("0x") characters.
473+ * Prepended least significant byte first stopping on a zero byte.
474+ */
475+ sign_prefix = 0;
476+
477+ if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'd', 'i')) {
478+ /* "%d" and "%i" - signed decimal numbers. */
479+ if (signed_v < 0) {
480+ sign_prefix = '-';
481+ v = -(signed_v + 1);
482+ v++;
483+ } else if (_NOLIBC_PF_FLAGS_CONTAIN(flags, '+')) {
484+ sign_prefix = '+';
485+ } else if (_NOLIBC_PF_FLAGS_CONTAIN(flags, ' ')) {
486+ sign_prefix = ' ';
367487 }
368− outstr = tmpbuf;
488+ } else {
489+ /* "#o" requires that the output always starts with a '0'.
490+ * This needs another check after any zero padding to avoid
491+ * adding an extra leading '0'.
492+ */
493+ if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'o') &&
494+ _NOLIBC_PF_FLAGS_CONTAIN(ch_flag, '#' - 1))
495+ sign_prefix = '0';
369496 }
370− else if (c == 's') {
371− outstr = va_arg(args, char *);
372− if (!outstr)
373− outstr="(null)";
497+
498+ /* The value is converted offset into the buffer so that
499+ * 31 zero pad characters and the sign/prefix can be added in front.
500+ * The longest digit string is 22 + 1 for octal conversions.
501+ */
502+ out = outbuf + 2 + 31;
503+
504+ if (v == 0) {
505+ /* There are special rules for zero. */
506+ if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'p')) {
507+ /* "%p" match glibc, precision is ignored */
508+ outstr = "(nil)";
509+ len = 5;
510+ goto do_output;
511+ }
512+ if (!precision) {
513+ /* Explicit %nn.0d, no digits output (except for %#.0o) */
514+ len = 0;
515+ goto prepend_sign;
516+ }
517+ /* All other formats (including "%#x") just output "0". */
518+ out[0] = '0';
519+ len = 1;
520+ } else {
521+ /* Convert the number to ascii in the required base. */
522+ unsigned long long recip;
523+ unsigned int base;
524+ if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'd', 'i', 'u')) {
525+ base = 10;
526+ recip = _NOLIBC_U64TOA_RECIP(10);
527+ } else if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'o')) {
528+ base = 8;
529+ recip = _NOLIBC_U64TOA_RECIP(8);
530+ } else {
531+ base = 16;
532+ recip = _NOLIBC_U64TOA_RECIP(16);
533+ if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'p', '#' - 1)) {
534+ /* "%p" and "%#x" need "0x" prepending. */
535+ sign_prefix = '0' << 8 | 'x';
536+ }
537+ }
538+ len = _nolibc_u64toa_base(v, out, base, recip);
374539 }
375− else if (c == 'm') {
376−#ifdef NOLIBC_IGNORE_ERRNO
377− outstr = "unknown error";
378−#else
379− outstr = strerror(errno);
380−#endif /* NOLIBC_IGNORE_ERRNO */
540+
541+ /* Add zero padding */
542+ if (precision < 0) {
543+ /* No explicit precision (or negative from "%.*s"). */
544+ if (!_NOLIBC_PF_FLAGS_CONTAIN(flags, '0'))
545+ goto no_zero_padding;
546+ if (_NOLIBC_PF_FLAGS_CONTAIN(flags, '-'))
547+ /* Left justify overrides zero pad */
548+ goto no_zero_padding;
549+ /* eg "%05d", Zero pad to field width less sign.
550+ * Note that precision can end up negative so all
551+ * the variables have to be 'signed int'.
552+ */
553+ precision = width;
554+ if (sign_prefix) {
555+ precision--;
556+ if (sign_prefix >= 256)
557+ precision--;
558+ }
381559 }
382− else if (c == '%') {
383− /* queue it verbatim */
384− continue;
560+ if (precision > 31)
561+ /* Don't run off the start of outbuf[], arbitrary limit
562+ * longer than the longest number field. */
563+ precision = 31;
564+ for (; len < precision; len++) {
565+ /* Stop gcc generating horrid code and memset(). */
566+ _NOLIBC_OPTIMIZER_HIDE_VAR(len);
567+ *--out = '0';
385568 }
386− else {
387− /* modifiers or final 0 */
388− if (c == 'l') {
389− /* long format prefix, maintain the escape */
390− lpref++;
391− } else if (c == 'j') {
392− lpref = 2;
569+no_zero_padding:
570+
571+ /* %#o has set sign_prefix to '0', but we don't want so add an extra
572+ * leading zero here.
573+ * Since the only other byte values of sign_prefix are ' ', '+' and '-'
574+ * it is enough to check that out[] doesn't already start with sign_prefix.
575+ */
576+ if (sign_prefix - *out) {
577+prepend_sign:
578+ /* Add the 0, 1 or 2 ("0x") sign/prefix characters at the front. */
579+ for (; sign_prefix; sign_prefix >>= 8) {
580+ /* Force gcc to increment len inside the loop. */
581+ _NOLIBC_OPTIMIZER_HIDE_VAR(len);
582+ len++;
583+ *--out = sign_prefix;
393584 }
394− escape = 1;
395− goto do_escape;
396585 }
397− len = strlen(outstr);
398− goto flush_str;
586+ outstr = out;
587+ goto do_output;
399588 }
400589
401− /* not an escape sequence */
402− if (c == 0 || c == '%') {
403− /* flush pending data on escape or end */
404− escape = 1;
405− lpref = 0;
406− outstr = fmt;
407− len = ofs - 1;
408− flush_str:
409− if (n) {
410− w = len < n ? len : n;
411− n -= w;
412− while (width-- > w) {
413− if (cb(state, " ", 1) != 0)
414− return -1;
415− written += 1;
416− }
417− if (cb(state, outstr, w) != 0)
418− return -1;
419− }
590+ if (ch == 'm') {
591+#ifdef NOLIBC_IGNORE_ERRNO
592+ outstr = "unknown error";
593+#else
594+ outstr = strerror(errno);
595+#endif /* NOLIBC_IGNORE_ERRNO */
596+ goto do_strlen_output;
597+ }
420598
421− written += len;
422− do_escape:
423− if (c == 0)
424− break;
425− fmt += ofs;
426− ofs = 0;
427− continue;
599+ if (ch != '%') {
600+ /* Invalid format: back up to output the format characters */
601+ fmt = outstr + 1;
602+ /* and output a '%' now. */
428603 }
604+ /* %% is documented as a 'conversion specifier'.
605+ * Any flags, precision or length modifier are ignored.
606+ */
607+ len = 1;
608+ width = 0;
609+ outstr = fmt - 1;
610+ goto do_output;
611+
612+do_strlen_output:
613+ /* Open coded strnlen() (slightly smaller). */
614+ for (len = 0; precision < 0 || len < precision; len++)
615+ if (!outstr[len])
616+ break;
429617
430− /* literal char, just queue it */
618+do_output:
619+ written += len;
620+
621+ /* Stop gcc back-merging this code into one of the conditionals above. */
622+ _NOLIBC_OPTIMIZER_HIDE_VAR(len);
623+
624+ /* Output the characters on the required side of any padding. */
625+ width -= len;
626+ flags = _NOLIBC_PF_FLAGS_CONTAIN(flags, '-');
627+ if (flags && cb(state, outstr, len) != 0)
628+ return -1;
629+ while (width > 0) {
630+ /* Output pad in 16 byte blocks with the small block first. */
631+ int pad_len = ((width - 1) & 15) + 1;
632+ width -= pad_len;
633+ written += pad_len;
634+ if (cb(state, " ", pad_len) != 0)
635+ return -1;
636+ }
637+ if (!flags && cb(state, outstr, len) != 0)
638+ return -1;
431639 }
640+
641+ /* Request a final '\0' be added to the snprintf() output.
642+ * This may be the only call of the cb() function.
643+ */
644+ if (cb(state, NULL, 0) != 0)
645+ return -1;
646+
432647 return written;
433648 }
434649
435−static int __nolibc_fprintf_cb(intptr_t state, const char *buf, size_t size)
650+static int __nolibc_fprintf_cb(void *stream, const char *buf, size_t size)
436651 {
437− return _fwrite(buf, size, (FILE *)state);
652+ return _fwrite(buf, size, stream);
438653 }
439654
440655 static __attribute__((unused, format(printf, 2, 0)))
441656 int vfprintf(FILE *stream, const char *fmt, va_list args)
442657 {
443− return __nolibc_printf(__nolibc_fprintf_cb, (intptr_t)stream, SIZE_MAX, fmt, args);
658+ return __nolibc_printf(__nolibc_fprintf_cb, stream, fmt, args);
444659 }
445660
446661 static __attribute__((unused, format(printf, 1, 0)))
@@ -498,26 +713,54 @@ int dprintf(int fd, const char *fmt, ...) · 51 unmodified lines
498713 return ret;
499714 }
500715
501−static int __nolibc_sprintf_cb(intptr_t _state, const char *buf, size_t size)
716+struct __nolibc_sprintf_cb_state {
717+ char *buf;
718+ size_t space;
719+};
720+
721+static int __nolibc_sprintf_cb(void *v_state, const char *buf, size_t size)
502722 {
503− char **state = (char **)_state;
723+ struct __nolibc_sprintf_cb_state *state = v_state;
724+ size_t space = state->space;
725+ char *tgt;
726+
727+ /* Truncate the request to fit in the output buffer space.
728+ * The last byte is reserved for the terminating '\0'.
729+ * state->space can only be zero for snprintf(NULL, 0, fmt, args)
730+ * so this normally lets through calls with 'size == 0'.
731+ */
732+ if (size >= space) {
733+ if (space <= 1)
734+ return 0;
735+ size = space - 1;
736+ }
737+ tgt = state->buf;
738+
739+ /* __nolibc_printf() ends with cb(state, NULL, 0) to request the output
740+ * buffer be '\0' terminated.
741+ * That will be the only cb() call for, eg, snprintf(buf, sz, "").
742+ * Zero lengths can occur at other times (eg "%s" for an empty string).
743+ * Unconditionally write the '\0' byte to reduce code size, it is
744+ * normally overwritten by the data being output.
745+ * There is no point adding a '\0' after copied data - there is always
746+ * another call.
747+ */
748+ *tgt = '\0';
749+ if (size) {
750+ state->space = space - size;
751+ state->buf = tgt + size;
752+ memcpy(tgt, buf, size);
753+ }
504754
505− memcpy(*state, buf, size);
506− *state += size;
507755 return 0;
508756 }
509757
510758 static __attribute__((unused, format(printf, 3, 0)))
511759 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
512760 {
513− char *state = buf;
514− int ret;
761+ struct __nolibc_sprintf_cb_state state = { .buf = buf, .space = size };
515762
516− ret = __nolibc_printf(__nolibc_sprintf_cb, (intptr_t)&state, size, fmt, args);
517− if (ret < 0)
518− return ret;
519− buf[(size_t)ret < size ? (size_t)ret : size - 1] = '\0';
520− return ret;
763+ return __nolibc_printf(__nolibc_sprintf_cb, &state, fmt, args);
521764 }
522765
523766 static __attribute__((unused, format(printf, 3, 4)))
@@ -552,6 +795,56 @@ int sprintf(char *buf, const char *fmt, ...) · 28 unmodified lines
552795 return ret;
553796 }
554797
798+static __attribute__((unused, format(printf, 2, 0)))
799+int __nolibc_vasprintf(char **strp, const char *fmt, va_list args1, va_list args2)
800+{
801+ int len1, len2;
802+ char *buf;
803+
804+ len1 = vsnprintf(NULL, 0, fmt, args1);
805+ if (len1 < 0)
806+ return -1;
807+
808+ buf = malloc(len1 + 1);
809+ if (!buf)
810+ return -1;
811+
812+ len2 = vsnprintf(buf, len1 + 1, fmt, args2);
813+ if (len2 < 0) {
814+ free(buf);
815+ return -1;
816+ }
817+
818+ *strp = buf;
819+ return len1;
820+}
821+
822+static __attribute__((unused, format(printf, 2, 0)))
823+int vasprintf(char **strp, const char *fmt, va_list args)
824+{
825+ va_list args2;
826+ int ret;
827+
828+ va_copy(args2, args);
829+ ret = __nolibc_vasprintf(strp, fmt, args, args2);
830+ va_end(args2);
831+
832+ return ret;
833+}
834+
835+static __attribute__((unused, format(printf, 2, 3)))
836+int asprintf(char **strp, const char *fmt, ...)
837+{
838+ va_list args;
839+ int ret;
840+
841+ va_start(args, fmt);
842+ ret = vasprintf(strp, fmt, args);
843+ va_end(args);
844+
845+ return ret;
846+}
847+
555848 static __attribute__((unused))
556849 int vsscanf(const char *str, const char *format, va_list args)
557850 {
@@ -683,13 +976,29 @@ int setvbuf(FILE *stream __attribute__((unused)), · 125 unmodified lines
683976 }
684977
685978 static __attribute__((unused))
686−const char *strerror(int errno)
979+int strerror_r(int errnum, char *buf, size_t buflen)
980+{
981+ if (buflen < 18)
982+ return ERANGE;
983+
984+ __builtin_memcpy(buf, "errno=", 6);
985+ i64toa_r(errnum, buf + 6);
986+ return 0;
987+}
988+
989+static __attribute__((unused))
990+const char *strerror(int errnum)
687991 {
688− static char buf[18] = "errno=";
992+ static char buf[18];
993+ char *b = buf;
994+
995+ /* Force gcc to use 'register offset' to access buf[]. */
996+ _NOLIBC_OPTIMIZER_HIDE_VAR(b);
689997
690− i64toa_r(errno, &buf[6]);
998+ /* Use strerror_r() to avoid having the only .data in small programs. */
999+ strerror_r(errnum, b, sizeof(buf));
6911000
692− return buf;
1001+ return b;
6931002 }
6941003
6951004 #endif /* _NOLIBC_STDIO_H */
tools/include/nolibc/stdlib.h+85−90
@@ -55,7 +55,7 @@ void abort(void); · 54 unmodified lines
5555 __attribute__((weak,unused,noreturn,section(".text.nolibc_abort")))
5656 void abort(void)
5757 {
58− sys_kill(sys_getpid(), SIGABRT);
58+ _sys_kill(_sys_getpid(), SIGABRT);
5959 for (;;);
6060 }
6161
@@ -145,9 +145,9 @@ void *malloc(size_t len) · 83 unmodified lines
145145 static __attribute__((unused))
146146 void *calloc(size_t size, size_t nmemb)
147147 {
148− size_t x = size * nmemb;
148+ size_t x;
149149
150− if (__builtin_expect(size && ((x / size) != nmemb), 0)) {
150+ if (__builtin_expect(__builtin_mul_overflow(size, nmemb, &x), 0)) {
151151 SET_ERRNO(ENOMEM);
152152 return NULL;
153153 }
@@ -188,36 +188,91 @@ void *realloc(void *old_ptr, size_t new_size) · 34 unmodified lines
188188 return ret;
189189 }
190190
191−/* Converts the unsigned long integer <in> to its hex representation into
191+/* Converts the unsigned 64bit integer <in> to base <base> ascii into
192192 * buffer <buffer>, which must be long enough to store the number and the
193− * trailing zero (17 bytes for "ffffffffffffffff" or 9 for "ffffffff"). The
194− * buffer is filled from the first byte, and the number of characters emitted
195− * (not counting the trailing zero) is returned. The function is constructed
196− * in a way to optimize the code size and avoid any divide that could add a
197− * dependency on large external functions.
193+ * trailing zero. The buffer is filled from the first byte, and the number
194+ * of characters emitted (not counting the trailing zero) is returned.
195+ * The function uses 'multiply by reciprocal' for the divisions and
196+ * requires the caller pass the correct reciprocal.
197+ *
198+ * Note that unlike __div64_const32() in asm-generic/div64.h there isn't
199+ * an extra shift done (by ___p), the reciprocal has to be lower resulting
200+ * in a slightly low quotient.
201+ * Keep things simple by correcting for the error.
202+ * This also saves calculating the 'low * low' product (e2 below) which is
203+ * very unlikely to be significant.
204+ *
205+ * Some maths:
206+ * recip = p2 / base - e1; // With e1 < base.
207+ * q = (recip * in - e2) / p2; // With e2 < p2.
208+ * = base / in - (e1 * in + e2) / p2;
209+ * > base / in - (e1 * p2 + p2) / p2;
210+ * = base / in - ((e1 + 1) * p2) / p2;
211+ * > base / in - base;
212+ * So the maximum error is less than 'base'.
213+ * Hence the largest possible digit is '2 * base - 1'.
214+ * For base 10 e1 is 6 and you can get digits of 15 (eg from 2**64-1).
215+ * Error e1 is largest for a base that is a factor of 2**64+1, the smallest is 274177
216+ * and converting 2**42-1 in base 274177 does generate a digit of 274177+274175.
217+ * This all means only a single correction is needed rather than a loop.
218+ *
219+ * __int128 isn't used for mips because gcc prior to 10.0 will call
220+ * __multi3 for MIPS64r6. The same also happens for SPARC and clang.
198221 */
199−static __attribute__((unused))
200−int utoh_r(unsigned long in, char *buffer)
222+#define _NOLIBC_U64TOA_RECIP(base) ((base) & 1 ? ~0ull / (base) : (1ull << 63) / ((base) / 2))
223+static __attribute__((unused, noinline))
224+int _nolibc_u64toa_base(uint64_t in, char *buffer, unsigned int base, uint64_t recip)
201225 {
202− signed char pos = (~0UL > 0xfffffffful) ? 60 : 28;
203− int digits = 0;
204− int dig;
226+ unsigned int digits = 0;
227+ unsigned int dig;
228+ uint64_t q;
229+ char *p;
205230
231+ /* Generate least significant digit first */
206232 do {
207− dig = in >> pos;
208− in -= (uint64_t)dig << pos;
209− pos -= 4;
210− if (dig || digits || pos < 0) {
211− if (dig > 9)
212− dig += 'a' - '0' - 10;
213− buffer[digits++] = '0' + dig;
233+#if defined(__SIZEOF_INT128__) && !defined(__mips__) && !defined(__sparc__)
234+ q = ((unsigned __int128)in * recip) >> 64;
235+#else
236+ uint64_t p = (uint32_t)in * (recip >> 32);
237+ q = (in >> 32) * (recip >> 32) + (p >> 32);
238+ p = (uint32_t)p + (in >> 32) * (uint32_t)recip;
239+ q += p >> 32;
240+#endif
241+ dig = in - q * base;
242+ /* Correct for any rounding errors */
243+ if (dig >= base) {
244+ dig -= base;
245+ q++;
214246 }
215− } while (pos >= 0);
247+ if (dig > 9)
248+ dig += 'a' - '0' - 10;
249+ buffer[digits++] = '0' + dig;
250+ } while ((in = q));
216251
217252 buffer[digits] = 0;
253+
254+ /* Order reverse to result */
255+ for (p = buffer + digits - 1; p > buffer; buffer++, p--) {
256+ dig = *buffer;
257+ *buffer = *p;
258+ *p = dig;
259+ }
260+
218261 return digits;
219262 }
220263
264+/* Converts the unsigned long integer <in> to its hex representation into
265+ * buffer <buffer>, which must be long enough to store the number and the
266+ * trailing zero (17 bytes for "ffffffffffffffff" or 9 for "ffffffff"). The
267+ * buffer is filled from the first byte, and the number of characters emitted
268+ * (not counting the trailing zero) is returned.
269+ */
270+static __inline__ __attribute__((unused))
271+int utoh_r(unsigned long in, char *buffer)
272+{
273+ return _nolibc_u64toa_base(in, buffer, 16, _NOLIBC_U64TOA_RECIP(16));
274+}
275+
221276 /* converts unsigned long <in> to an hex string using the static itoa_buffer
222277 * and returns the pointer to that string.
223278 */
@@ -233,30 +288,11 @@ char *utoh(unsigned long in) · 9 unmodified lines
233288 * trailing zero (21 bytes for 18446744073709551615 in 64-bit, 11 for
234289 * 4294967295 in 32-bit). The buffer is filled from the first byte, and the
235290 * number of characters emitted (not counting the trailing zero) is returned.
236− * The function is constructed in a way to optimize the code size and avoid
237− * any divide that could add a dependency on large external functions.
238291 */
239−static __attribute__((unused))
292+static __inline__ __attribute__((unused))
240293 int utoa_r(unsigned long in, char *buffer)
241294 {
242− unsigned long lim;
243− int digits = 0;
244− int pos = (~0UL > 0xfffffffful) ? 19 : 9;
245− int dig;
246−
247− do {
248− for (dig = 0, lim = 1; dig < pos; dig++)
249− lim *= 10;
250−
251− if (digits || in >= lim || !pos) {
252− for (dig = 0; in >= lim; dig++)
253− in -= lim;
254− buffer[digits++] = '0' + dig;
255− }
256− } while (pos--);
257−
258− buffer[digits] = 0;
259− return digits;
295+ return _nolibc_u64toa_base(in, buffer, 10, _NOLIBC_U64TOA_RECIP(10));
260296 }
261297
262298 /* Converts the signed long integer <in> to its string representation into
@@ -324,34 +360,12 @@ char *utoa(unsigned long in) · 61 unmodified lines
324360 * buffer <buffer>, which must be long enough to store the number and the
325361 * trailing zero (17 bytes for "ffffffffffffffff"). The buffer is filled from
326362 * the first byte, and the number of characters emitted (not counting the
327− * trailing zero) is returned. The function is constructed in a way to optimize
328− * the code size and avoid any divide that could add a dependency on large
329− * external functions.
363+ * trailing zero) is returned.
330364 */
331−static __attribute__((unused))
365+static __inline__ __attribute__((unused))
332366 int u64toh_r(uint64_t in, char *buffer)
333367 {
334− signed char pos = 60;
335− int digits = 0;
336− int dig;
337−
338− do {
339− if (sizeof(long) >= 8) {
340− dig = (in >> pos) & 0xF;
341− } else {
342− /* 32-bit platforms: avoid a 64-bit shift */
343− uint32_t d = (pos >= 32) ? (in >> 32) : in;
344− dig = (d >> (pos & 31)) & 0xF;
345− }
346− if (dig > 9)
347− dig += 'a' - '0' - 10;
348− pos -= 4;
349− if (dig || digits || pos < 0)
350− buffer[digits++] = '0' + dig;
351− } while (pos >= 0);
352−
353− buffer[digits] = 0;
354− return digits;
368+ return _nolibc_u64toa_base(in, buffer, 16, _NOLIBC_U64TOA_RECIP(16));
355369 }
356370
357371 /* converts uint64_t <in> to an hex string using the static itoa_buffer and
@@ -368,31 +382,12 @@ char *u64toh(uint64_t in) · 10 unmodified lines
368382 * buffer <buffer>, which must be long enough to store the number and the
369383 * trailing zero (21 bytes for 18446744073709551615). The buffer is filled from
370384 * the first byte, and the number of characters emitted (not counting the
371− * trailing zero) is returned. The function is constructed in a way to optimize
372− * the code size and avoid any divide that could add a dependency on large
373− * external functions.
385+ * trailing zero) is returned.
374386 */
375−static __attribute__((unused))
387+static __inline__ __attribute__((unused))
376388 int u64toa_r(uint64_t in, char *buffer)
377389 {
378− unsigned long long lim;
379− int digits = 0;
380− int pos = 19; /* start with the highest possible digit */
381− int dig;
382−
383− do {
384− for (dig = 0, lim = 1; dig < pos; dig++)
385− lim *= 10;
386−
387− if (digits || in >= lim || !pos) {
388− for (dig = 0; in >= lim; dig++)
389− in -= lim;
390− buffer[digits++] = '0' + dig;
391− }
392− } while (pos--);
393−
394− buffer[digits] = 0;
395− return digits;
390+ return _nolibc_u64toa_base(in, buffer, 10, _NOLIBC_U64TOA_RECIP(10));
396391 }
397392
398393 /* Converts the signed 64-bit integer <in> to its string representation into
tools/include/nolibc/sys.h+144−144
@@ -63,7 +63,7 @@ static __inline__ int __nolibc_enosys(const char *syscall, ...) · 62 unmodified lines
6363 * - the "internal" ones, which matches the raw syscall interface at the
6464 * kernel level, which may sometimes slightly differ from the documented
6565 * libc-level ones. For example most of them return either a valid value
66− * or -errno. All of these are prefixed with "sys_". They may be called
66+ * or -errno. All of these are prefixed with "_sys_". They may be called
6767 * by non-portable applications if desired.
6868 *
6969 * - the "exported" ones, whose interface must closely match the one
@@ -85,15 +85,15 @@ static __inline__ int __nolibc_enosys(const char *syscall, ...) · 15 unmodified lines
8585 */
8686
8787 static __attribute__((unused))
88−void *sys_brk(void *addr)
88+void *_sys_brk(void *addr)
8989 {
90− return (void *)my_syscall1(__NR_brk, addr);
90+ return (void *)__nolibc_syscall1(__NR_brk, addr);
9191 }
9292
9393 static __attribute__((unused))
9494 int brk(void *addr)
9595 {
96− void *ret = sys_brk(addr);
96+ void *ret = _sys_brk(addr);
9797
9898 if (!ret) {
9999 SET_ERRNO(ENOMEM);
@@ -106,9 +106,9 @@ static __attribute__((unused)) · 6 unmodified lines
106106 void *sbrk(intptr_t inc)
107107 {
108108 /* first call to find current end */
109− void *ret = sys_brk(NULL);
109+ void *ret = _sys_brk(NULL);
110110
111− if (ret && sys_brk(ret + inc) == ret + inc)
111+ if (ret && _sys_brk(ret + inc) == ret + inc)
112112 return ret + inc;
113113
114114 SET_ERRNO(ENOMEM);
@@ -122,27 +122,27 @@ void *sbrk(intptr_t inc) · 7 unmodified lines
122122 */
123123
124124 static __attribute__((unused))
125−int sys_chdir(const char *path)
125+int _sys_chdir(const char *path)
126126 {
127− return my_syscall1(__NR_chdir, path);
127+ return __nolibc_syscall1(__NR_chdir, path);
128128 }
129129
130130 static __attribute__((unused))
131131 int chdir(const char *path)
132132 {
133− return __sysret(sys_chdir(path));
133+ return __sysret(_sys_chdir(path));
134134 }
135135
136136 static __attribute__((unused))
137−int sys_fchdir(int fildes)
137+int _sys_fchdir(int fildes)
138138 {
139− return my_syscall1(__NR_fchdir, fildes);
139+ return __nolibc_syscall1(__NR_fchdir, fildes);
140140 }
141141
142142 static __attribute__((unused))
143143 int fchdir(int fildes)
144144 {
145− return __sysret(sys_fchdir(fildes));
145+ return __sysret(_sys_fchdir(fildes));
146146 }
147147
148148
@@ -151,19 +151,19 @@ int fchdir(int fildes) · 2 unmodified lines
151151 */
152152
153153 static __attribute__((unused))
154−int sys_chmod(const char *path, mode_t mode)
154+int _sys_chmod(const char *path, mode_t mode)
155155 {
156156 #if defined(__NR_fchmodat)
157− return my_syscall4(__NR_fchmodat, AT_FDCWD, path, mode, 0);
157+ return __nolibc_syscall4(__NR_fchmodat, AT_FDCWD, path, mode, 0);
158158 #else
159− return my_syscall2(__NR_chmod, path, mode);
159+ return __nolibc_syscall2(__NR_chmod, path, mode);
160160 #endif
161161 }
162162
163163 static __attribute__((unused))
164164 int chmod(const char *path, mode_t mode)
165165 {
166− return __sysret(sys_chmod(path, mode));
166+ return __sysret(_sys_chmod(path, mode));
167167 }
168168
169169
@@ -172,19 +172,19 @@ int chmod(const char *path, mode_t mode) · 2 unmodified lines
172172 */
173173
174174 static __attribute__((unused))
175−int sys_chown(const char *path, uid_t owner, gid_t group)
175+int _sys_chown(const char *path, uid_t owner, gid_t group)
176176 {
177177 #if defined(__NR_fchownat)
178− return my_syscall5(__NR_fchownat, AT_FDCWD, path, owner, group, 0);
178+ return __nolibc_syscall5(__NR_fchownat, AT_FDCWD, path, owner, group, 0);
179179 #else
180− return my_syscall3(__NR_chown, path, owner, group);
180+ return __nolibc_syscall3(__NR_chown, path, owner, group);
181181 #endif
182182 }
183183
184184 static __attribute__((unused))
185185 int chown(const char *path, uid_t owner, gid_t group)
186186 {
187− return __sysret(sys_chown(path, owner, group));
187+ return __sysret(_sys_chown(path, owner, group));
188188 }
189189
190190
@@ -193,15 +193,15 @@ int chown(const char *path, uid_t owner, gid_t group) · 2 unmodified lines
193193 */
194194
195195 static __attribute__((unused))
196−int sys_chroot(const char *path)
196+int _sys_chroot(const char *path)
197197 {
198− return my_syscall1(__NR_chroot, path);
198+ return __nolibc_syscall1(__NR_chroot, path);
199199 }
200200
201201 static __attribute__((unused))
202202 int chroot(const char *path)
203203 {
204− return __sysret(sys_chroot(path));
204+ return __sysret(_sys_chroot(path));
205205 }
206206
207207
@@ -210,15 +210,15 @@ int chroot(const char *path) · 2 unmodified lines
210210 */
211211
212212 static __attribute__((unused))
213−int sys_close(int fd)
213+int _sys_close(int fd)
214214 {
215− return my_syscall1(__NR_close, fd);
215+ return __nolibc_syscall1(__NR_close, fd);
216216 }
217217
218218 static __attribute__((unused))
219219 int close(int fd)
220220 {
221− return __sysret(sys_close(fd));
221+ return __sysret(_sys_close(fd));
222222 }
223223
224224
@@ -227,15 +227,15 @@ int close(int fd) · 2 unmodified lines
227227 */
228228
229229 static __attribute__((unused))
230−int sys_dup(int fd)
230+int _sys_dup(int fd)
231231 {
232− return my_syscall1(__NR_dup, fd);
232+ return __nolibc_syscall1(__NR_dup, fd);
233233 }
234234
235235 static __attribute__((unused))
236236 int dup(int fd)
237237 {
238− return __sysret(sys_dup(fd));
238+ return __sysret(_sys_dup(fd));
239239 }
240240
241241
@@ -244,7 +244,7 @@ int dup(int fd) · 2 unmodified lines
244244 */
245245
246246 static __attribute__((unused))
247−int sys_dup2(int old, int new)
247+int _sys_dup2(int old, int new)
248248 {
249249 #if defined(__NR_dup3)
250250 int ret, nr_fcntl;
@@ -256,20 +256,20 @@ int sys_dup2(int old, int new) · 5 unmodified lines
256256 #endif
257257
258258 if (old == new) {
259− ret = my_syscall2(nr_fcntl, old, F_GETFD);
259+ ret = __nolibc_syscall2(nr_fcntl, old, F_GETFD);
260260 return ret < 0 ? ret : old;
261261 }
262262
263− return my_syscall3(__NR_dup3, old, new, 0);
263+ return __nolibc_syscall3(__NR_dup3, old, new, 0);
264264 #else
265− return my_syscall2(__NR_dup2, old, new);
265+ return __nolibc_syscall2(__NR_dup2, old, new);
266266 #endif
267267 }
268268
269269 static __attribute__((unused))
270270 int dup2(int old, int new)
271271 {
272− return __sysret(sys_dup2(old, new));
272+ return __sysret(_sys_dup2(old, new));
273273 }
274274
275275
@@ -279,15 +279,15 @@ int dup2(int old, int new) · 3 unmodified lines
279279
280280 #if defined(__NR_dup3)
281281 static __attribute__((unused))
282−int sys_dup3(int old, int new, int flags)
282+int _sys_dup3(int old, int new, int flags)
283283 {
284− return my_syscall3(__NR_dup3, old, new, flags);
284+ return __nolibc_syscall3(__NR_dup3, old, new, flags);
285285 }
286286
287287 static __attribute__((unused))
288288 int dup3(int old, int new, int flags)
289289 {
290− return __sysret(sys_dup3(old, new, flags));
290+ return __sysret(_sys_dup3(old, new, flags));
291291 }
292292 #endif
293293
@@ -297,15 +297,15 @@ int dup3(int old, int new, int flags) · 3 unmodified lines
297297 */
298298
299299 static __attribute__((unused))
300−int sys_execve(const char *filename, char *const argv[], char *const envp[])
300+int _sys_execve(const char *filename, char *const argv[], char *const envp[])
301301 {
302− return my_syscall3(__NR_execve, filename, argv, envp);
302+ return __nolibc_syscall3(__NR_execve, filename, argv, envp);
303303 }
304304
305305 static __attribute__((unused))
306306 int execve(const char *filename, char *const argv[], char *const envp[])
307307 {
308− return __sysret(sys_execve(filename, argv, envp));
308+ return __sysret(_sys_execve(filename, argv, envp));
309309 }
310310
311311
@@ -314,16 +314,16 @@ int execve(const char *filename, char *const argv[], char *const envp[]) · 2 unmodified lines
314314 */
315315
316316 static __attribute__((noreturn,unused))
317−void sys_exit(int status)
317+void _sys_exit(int status)
318318 {
319− my_syscall1(__NR_exit, status & 255);
319+ __nolibc_syscall1(__NR_exit, status & 255);
320320 while(1); /* shut the "noreturn" warnings. */
321321 }
322322
323323 static __attribute__((noreturn,unused))
324324 void _exit(int status)
325325 {
326− sys_exit(status);
326+ _sys_exit(status);
327327 }
328328
329329 static __attribute__((noreturn,unused))
@@ -337,18 +337,18 @@ void exit(int status) · 7 unmodified lines
337337 * pid_t fork(void);
338338 */
339339
340−#ifndef sys_fork
340+#ifndef _sys_fork
341341 static __attribute__((unused))
342−pid_t sys_fork(void)
342+pid_t _sys_fork(void)
343343 {
344344 #if defined(__NR_clone)
345345 /* note: some archs only have clone() and not fork(). Different archs
346346 * have a different API, but most archs have the flags on first arg and
347347 * will not use the rest with no other flag.
348348 */
349− return my_syscall5(__NR_clone, SIGCHLD, 0, 0, 0, 0);
349+ return __nolibc_syscall5(__NR_clone, SIGCHLD, 0, 0, 0, 0);
350350 #else
351− return my_syscall0(__NR_fork);
351+ return __nolibc_syscall0(__NR_fork);
352352 #endif
353353 }
354354 #endif
@@ -356,18 +356,18 @@ pid_t sys_fork(void) · 1 unmodified line
356356 static __attribute__((unused))
357357 pid_t fork(void)
358358 {
359− return __sysret(sys_fork());
359+ return __sysret(_sys_fork());
360360 }
361361
362−#ifndef sys_vfork
362+#ifndef _sys_vfork
363363 static __attribute__((unused))
364−pid_t sys_vfork(void)
364+pid_t _sys_vfork(void)
365365 {
366366 #if defined(__NR_clone)
367− /* See the note in sys_fork(). */
368− return my_syscall5(__NR_clone, CLONE_VM | CLONE_VFORK | SIGCHLD, 0, 0, 0, 0);
367+ /* See the note in _sys_fork(). */
368+ return __nolibc_syscall5(__NR_clone, CLONE_VM | CLONE_VFORK | SIGCHLD, 0, 0, 0, 0);
369369 #elif defined(__NR_vfork)
370− return my_syscall0(__NR_vfork);
370+ return __nolibc_syscall0(__NR_vfork);
371371 #endif
372372 }
373373 #endif
@@ -375,7 +375,7 @@ pid_t sys_vfork(void) · 1 unmodified line
375375 static __attribute__((unused))
376376 pid_t vfork(void)
377377 {
378− return __sysret(sys_vfork());
378+ return __sysret(_sys_vfork());
379379 }
380380
381381 /*
@@ -383,15 +383,15 @@ pid_t vfork(void) · 1 unmodified line
383383 */
384384
385385 static __attribute__((unused))
386−int sys_fsync(int fd)
386+int _sys_fsync(int fd)
387387 {
388− return my_syscall1(__NR_fsync, fd);
388+ return __nolibc_syscall1(__NR_fsync, fd);
389389 }
390390
391391 static __attribute__((unused))
392392 int fsync(int fd)
393393 {
394− return __sysret(sys_fsync(fd));
394+ return __sysret(_sys_fsync(fd));
395395 }
396396
397397
@@ -400,15 +400,15 @@ int fsync(int fd) · 2 unmodified lines
400400 */
401401
402402 static __attribute__((unused))
403−int sys_getdents64(int fd, struct linux_dirent64 *dirp, int count)
403+int _sys_getdents64(int fd, struct linux_dirent64 *dirp, int count)
404404 {
405− return my_syscall3(__NR_getdents64, fd, dirp, count);
405+ return __nolibc_syscall3(__NR_getdents64, fd, dirp, count);
406406 }
407407
408408 static __attribute__((unused))
409409 int getdents64(int fd, struct linux_dirent64 *dirp, int count)
410410 {
411− return __sysret(sys_getdents64(fd, dirp, count));
411+ return __sysret(_sys_getdents64(fd, dirp, count));
412412 }
413413
414414
@@ -417,19 +417,19 @@ int getdents64(int fd, struct linux_dirent64 *dirp, int count) · 2 unmodified lines
417417 */
418418
419419 static __attribute__((unused))
420−uid_t sys_geteuid(void)
420+uid_t _sys_geteuid(void)
421421 {
422422 #if defined(__NR_geteuid32)
423− return my_syscall0(__NR_geteuid32);
423+ return __nolibc_syscall0(__NR_geteuid32);
424424 #else
425− return my_syscall0(__NR_geteuid);
425+ return __nolibc_syscall0(__NR_geteuid);
426426 #endif
427427 }
428428
429429 static __attribute__((unused))
430430 uid_t geteuid(void)
431431 {
432− return sys_geteuid();
432+ return _sys_geteuid();
433433 }
434434
435435
@@ -438,15 +438,15 @@ uid_t geteuid(void) · 2 unmodified lines
438438 */
439439
440440 static __attribute__((unused))
441−pid_t sys_getpgid(pid_t pid)
441+pid_t _sys_getpgid(pid_t pid)
442442 {
443− return my_syscall1(__NR_getpgid, pid);
443+ return __nolibc_syscall1(__NR_getpgid, pid);
444444 }
445445
446446 static __attribute__((unused))
447447 pid_t getpgid(pid_t pid)
448448 {
449− return __sysret(sys_getpgid(pid));
449+ return __sysret(_sys_getpgid(pid));
450450 }
451451
452452
@@ -455,15 +455,15 @@ pid_t getpgid(pid_t pid) · 2 unmodified lines
455455 */
456456
457457 static __attribute__((unused))
458−pid_t sys_getpgrp(void)
458+pid_t _sys_getpgrp(void)
459459 {
460− return sys_getpgid(0);
460+ return _sys_getpgid(0);
461461 }
462462
463463 static __attribute__((unused))
464464 pid_t getpgrp(void)
465465 {
466− return sys_getpgrp();
466+ return _sys_getpgrp();
467467 }
468468
469469
@@ -472,15 +472,15 @@ pid_t getpgrp(void) · 2 unmodified lines
472472 */
473473
474474 static __attribute__((unused))
475−pid_t sys_getpid(void)
475+pid_t _sys_getpid(void)
476476 {
477− return my_syscall0(__NR_getpid);
477+ return __nolibc_syscall0(__NR_getpid);
478478 }
479479
480480 static __attribute__((unused))
481481 pid_t getpid(void)
482482 {
483− return sys_getpid();
483+ return _sys_getpid();
484484 }
485485
486486
@@ -489,15 +489,15 @@ pid_t getpid(void) · 2 unmodified lines
489489 */
490490
491491 static __attribute__((unused))
492−pid_t sys_getppid(void)
492+pid_t _sys_getppid(void)
493493 {
494− return my_syscall0(__NR_getppid);
494+ return __nolibc_syscall0(__NR_getppid);
495495 }
496496
497497 static __attribute__((unused))
498498 pid_t getppid(void)
499499 {
500− return sys_getppid();
500+ return _sys_getppid();
501501 }
502502
503503
@@ -506,15 +506,15 @@ pid_t getppid(void) · 2 unmodified lines
506506 */
507507
508508 static __attribute__((unused))
509−pid_t sys_gettid(void)
509+pid_t _sys_gettid(void)
510510 {
511− return my_syscall0(__NR_gettid);
511+ return __nolibc_syscall0(__NR_gettid);
512512 }
513513
514514 static __attribute__((unused))
515515 pid_t gettid(void)
516516 {
517− return sys_gettid();
517+ return _sys_gettid();
518518 }
519519
520520 #ifndef NOLIBC_NO_RUNTIME
@@ -536,19 +536,19 @@ int getpagesize(void) · 15 unmodified lines
536536 */
537537
538538 static __attribute__((unused))
539−uid_t sys_getuid(void)
539+uid_t _sys_getuid(void)
540540 {
541541 #if defined(__NR_getuid32)
542− return my_syscall0(__NR_getuid32);
542+ return __nolibc_syscall0(__NR_getuid32);
543543 #else
544− return my_syscall0(__NR_getuid);
544+ return __nolibc_syscall0(__NR_getuid);
545545 #endif
546546 }
547547
548548 static __attribute__((unused))
549549 uid_t getuid(void)
550550 {
551− return sys_getuid();
551+ return _sys_getuid();
552552 }
553553
554554
@@ -557,15 +557,15 @@ uid_t getuid(void) · 2 unmodified lines
557557 */
558558
559559 static __attribute__((unused))
560−int sys_kill(pid_t pid, int signal)
560+int _sys_kill(pid_t pid, int signal)
561561 {
562− return my_syscall2(__NR_kill, pid, signal);
562+ return __nolibc_syscall2(__NR_kill, pid, signal);
563563 }
564564
565565 static __attribute__((unused))
566566 int kill(pid_t pid, int signal)
567567 {
568− return __sysret(sys_kill(pid, signal));
568+ return __sysret(_sys_kill(pid, signal));
569569 }
570570
571571
@@ -574,19 +574,19 @@ int kill(pid_t pid, int signal) · 2 unmodified lines
574574 */
575575
576576 static __attribute__((unused))
577−int sys_link(const char *old, const char *new)
577+int _sys_link(const char *old, const char *new)
578578 {
579579 #if defined(__NR_linkat)
580− return my_syscall5(__NR_linkat, AT_FDCWD, old, AT_FDCWD, new, 0);
580+ return __nolibc_syscall5(__NR_linkat, AT_FDCWD, old, AT_FDCWD, new, 0);
581581 #else
582− return my_syscall2(__NR_link, old, new);
582+ return __nolibc_syscall2(__NR_link, old, new);
583583 #endif
584584 }
585585
586586 static __attribute__((unused))
587587 int link(const char *old, const char *new)
588588 {
589− return __sysret(sys_link(old, new));
589+ return __sysret(_sys_link(old, new));
590590 }
591591
592592
@@ -595,14 +595,14 @@ int link(const char *old, const char *new) · 2 unmodified lines
595595 */
596596
597597 static __attribute__((unused))
598−off_t sys_lseek(int fd, off_t offset, int whence)
598+off_t _sys_lseek(int fd, off_t offset, int whence)
599599 {
600600 #if defined(__NR_llseek)
601601 __kernel_loff_t loff = 0;
602602 off_t result;
603603 int ret;
604604
605− ret = my_syscall5(__NR_llseek, fd, offset >> 32, (uint32_t)offset, &loff, whence);
605+ ret = __nolibc_syscall5(__NR_llseek, fd, offset >> 32, (uint32_t)offset, &loff, whence);
606606 if (ret < 0)
607607 result = ret;
608608 else
@@ -610,14 +610,14 @@ off_t sys_lseek(int fd, off_t offset, int whence) · 1 unmodified line
610610
611611 return result;
612612 #else
613− return my_syscall3(__NR_lseek, fd, offset, whence);
613+ return __nolibc_syscall3(__NR_lseek, fd, offset, whence);
614614 #endif
615615 }
616616
617617 static __attribute__((unused))
618618 off_t lseek(int fd, off_t offset, int whence)
619619 {
620− return __sysret(sys_lseek(fd, offset, whence));
620+ return __sysret(_sys_lseek(fd, offset, whence));
621621 }
622622
623623
@@ -626,19 +626,19 @@ off_t lseek(int fd, off_t offset, int whence) · 2 unmodified lines
626626 */
627627
628628 static __attribute__((unused))
629−int sys_mkdir(const char *path, mode_t mode)
629+int _sys_mkdir(const char *path, mode_t mode)
630630 {
631631 #if defined(__NR_mkdirat)
632− return my_syscall3(__NR_mkdirat, AT_FDCWD, path, mode);
632+ return __nolibc_syscall3(__NR_mkdirat, AT_FDCWD, path, mode);
633633 #else
634− return my_syscall2(__NR_mkdir, path, mode);
634+ return __nolibc_syscall2(__NR_mkdir, path, mode);
635635 #endif
636636 }
637637
638638 static __attribute__((unused))
639639 int mkdir(const char *path, mode_t mode)
640640 {
641− return __sysret(sys_mkdir(path, mode));
641+ return __sysret(_sys_mkdir(path, mode));
642642 }
643643
644644 /*
@@ -646,19 +646,19 @@ int mkdir(const char *path, mode_t mode) · 1 unmodified line
646646 */
647647
648648 static __attribute__((unused))
649−int sys_rmdir(const char *path)
649+int _sys_rmdir(const char *path)
650650 {
651651 #if defined(__NR_rmdir)
652− return my_syscall1(__NR_rmdir, path);
652+ return __nolibc_syscall1(__NR_rmdir, path);
653653 #else
654− return my_syscall3(__NR_unlinkat, AT_FDCWD, path, AT_REMOVEDIR);
654+ return __nolibc_syscall3(__NR_unlinkat, AT_FDCWD, path, AT_REMOVEDIR);
655655 #endif
656656 }
657657
658658 static __attribute__((unused))
659659 int rmdir(const char *path)
660660 {
661− return __sysret(sys_rmdir(path));
661+ return __sysret(_sys_rmdir(path));
662662 }
663663
664664
@@ -667,19 +667,19 @@ int rmdir(const char *path) · 2 unmodified lines
667667 */
668668
669669 static __attribute__((unused))
670−long sys_mknod(const char *path, mode_t mode, dev_t dev)
670+long _sys_mknod(const char *path, mode_t mode, dev_t dev)
671671 {
672672 #if defined(__NR_mknodat)
673− return my_syscall4(__NR_mknodat, AT_FDCWD, path, mode, dev);
673+ return __nolibc_syscall4(__NR_mknodat, AT_FDCWD, path, mode, dev);
674674 #else
675− return my_syscall3(__NR_mknod, path, mode, dev);
675+ return __nolibc_syscall3(__NR_mknod, path, mode, dev);
676676 #endif
677677 }
678678
679679 static __attribute__((unused))
680680 int mknod(const char *path, mode_t mode, dev_t dev)
681681 {
682− return __sysret(sys_mknod(path, mode, dev));
682+ return __sysret(_sys_mknod(path, mode, dev));
683683 }
684684
685685
@@ -689,15 +689,15 @@ int mknod(const char *path, mode_t mode, dev_t dev) · 3 unmodified lines
689689 */
690690
691691 static __attribute__((unused))
692−int sys_pipe2(int pipefd[2], int flags)
692+int _sys_pipe2(int pipefd[2], int flags)
693693 {
694− return my_syscall2(__NR_pipe2, pipefd, flags);
694+ return __nolibc_syscall2(__NR_pipe2, pipefd, flags);
695695 }
696696
697697 static __attribute__((unused))
698698 int pipe2(int pipefd[2], int flags)
699699 {
700− return __sysret(sys_pipe2(pipefd, flags));
700+ return __sysret(_sys_pipe2(pipefd, flags));
701701 }
702702
703703 static __attribute__((unused))
@@ -712,15 +712,15 @@ int pipe(int pipefd[2]) · 8 unmodified lines
712712 */
713713
714714 static __attribute__((unused))
715−int sys_pivot_root(const char *new, const char *old)
715+int _sys_pivot_root(const char *new, const char *old)
716716 {
717− return my_syscall2(__NR_pivot_root, new, old);
717+ return __nolibc_syscall2(__NR_pivot_root, new, old);
718718 }
719719
720720 static __attribute__((unused))
721721 int pivot_root(const char *new, const char *old)
722722 {
723− return __sysret(sys_pivot_root(new, old));
723+ return __sysret(_sys_pivot_root(new, old));
724724 }
725725
726726
@@ -729,15 +729,15 @@ int pivot_root(const char *new, const char *old) · 2 unmodified lines
729729 */
730730
731731 static __attribute__((unused))
732−ssize_t sys_read(int fd, void *buf, size_t count)
732+ssize_t _sys_read(int fd, void *buf, size_t count)
733733 {
734− return my_syscall3(__NR_read, fd, buf, count);
734+ return __nolibc_syscall3(__NR_read, fd, buf, count);
735735 }
736736
737737 static __attribute__((unused))
738738 ssize_t read(int fd, void *buf, size_t count)
739739 {
740− return __sysret(sys_read(fd, buf, count));
740+ return __sysret(_sys_read(fd, buf, count));
741741 }
742742
743743
@@ -746,15 +746,15 @@ ssize_t read(int fd, void *buf, size_t count) · 2 unmodified lines
746746 */
747747
748748 static __attribute__((unused))
749−int sys_sched_yield(void)
749+int _sys_sched_yield(void)
750750 {
751− return my_syscall0(__NR_sched_yield);
751+ return __nolibc_syscall0(__NR_sched_yield);
752752 }
753753
754754 static __attribute__((unused))
755755 int sched_yield(void)
756756 {
757− return __sysret(sys_sched_yield());
757+ return __sysret(_sys_sched_yield());
758758 }
759759
760760
@@ -763,15 +763,15 @@ int sched_yield(void) · 2 unmodified lines
763763 */
764764
765765 static __attribute__((unused))
766−int sys_setpgid(pid_t pid, pid_t pgid)
766+int _sys_setpgid(pid_t pid, pid_t pgid)
767767 {
768− return my_syscall2(__NR_setpgid, pid, pgid);
768+ return __nolibc_syscall2(__NR_setpgid, pid, pgid);
769769 }
770770
771771 static __attribute__((unused))
772772 int setpgid(pid_t pid, pid_t pgid)
773773 {
774− return __sysret(sys_setpgid(pid, pgid));
774+ return __sysret(_sys_setpgid(pid, pgid));
775775 }
776776
777777 /*
@@ -790,15 +790,15 @@ pid_t setpgrp(void) · 12 unmodified lines
790790 */
791791
792792 static __attribute__((unused))
793−pid_t sys_setsid(void)
793+pid_t _sys_setsid(void)
794794 {
795− return my_syscall0(__NR_setsid);
795+ return __nolibc_syscall0(__NR_setsid);
796796 }
797797
798798 static __attribute__((unused))
799799 pid_t setsid(void)
800800 {
801− return __sysret(sys_setsid());
801+ return __sysret(_sys_setsid());
802802 }
803803
804804
@@ -807,19 +807,19 @@ pid_t setsid(void) · 2 unmodified lines
807807 */
808808
809809 static __attribute__((unused))
810−int sys_symlink(const char *old, const char *new)
810+int _sys_symlink(const char *old, const char *new)
811811 {
812812 #if defined(__NR_symlinkat)
813− return my_syscall3(__NR_symlinkat, old, AT_FDCWD, new);
813+ return __nolibc_syscall3(__NR_symlinkat, old, AT_FDCWD, new);
814814 #else
815− return my_syscall2(__NR_symlink, old, new);
815+ return __nolibc_syscall2(__NR_symlink, old, new);
816816 #endif
817817 }
818818
819819 static __attribute__((unused))
820820 int symlink(const char *old, const char *new)
821821 {
822− return __sysret(sys_symlink(old, new));
822+ return __sysret(_sys_symlink(old, new));
823823 }
824824
825825
@@ -828,15 +828,15 @@ int symlink(const char *old, const char *new) · 2 unmodified lines
828828 */
829829
830830 static __attribute__((unused))
831−mode_t sys_umask(mode_t mode)
831+mode_t _sys_umask(mode_t mode)
832832 {
833− return my_syscall1(__NR_umask, mode);
833+ return __nolibc_syscall1(__NR_umask, mode);
834834 }
835835
836836 static __attribute__((unused))
837837 mode_t umask(mode_t mode)
838838 {
839− return sys_umask(mode);
839+ return _sys_umask(mode);
840840 }
841841
842842
@@ -845,15 +845,15 @@ mode_t umask(mode_t mode) · 2 unmodified lines
845845 */
846846
847847 static __attribute__((unused))
848−int sys_umount2(const char *path, int flags)
848+int _sys_umount2(const char *path, int flags)
849849 {
850− return my_syscall2(__NR_umount2, path, flags);
850+ return __nolibc_syscall2(__NR_umount2, path, flags);
851851 }
852852
853853 static __attribute__((unused))
854854 int umount2(const char *path, int flags)
855855 {
856− return __sysret(sys_umount2(path, flags));
856+ return __sysret(_sys_umount2(path, flags));
857857 }
858858
859859
@@ -862,19 +862,19 @@ int umount2(const char *path, int flags) · 2 unmodified lines
862862 */
863863
864864 static __attribute__((unused))
865−int sys_unlink(const char *path)
865+int _sys_unlink(const char *path)
866866 {
867867 #if defined(__NR_unlinkat)
868− return my_syscall3(__NR_unlinkat, AT_FDCWD, path, 0);
868+ return __nolibc_syscall3(__NR_unlinkat, AT_FDCWD, path, 0);
869869 #else
870− return my_syscall1(__NR_unlink, path);
870+ return __nolibc_syscall1(__NR_unlink, path);
871871 #endif
872872 }
873873
874874 static __attribute__((unused))
875875 int unlink(const char *path)
876876 {
877− return __sysret(sys_unlink(path));
877+ return __sysret(_sys_unlink(path));
878878 }
879879
880880
@@ -883,15 +883,15 @@ int unlink(const char *path) · 2 unmodified lines
883883 */
884884
885885 static __attribute__((unused))
886−ssize_t sys_write(int fd, const void *buf, size_t count)
886+ssize_t _sys_write(int fd, const void *buf, size_t count)
887887 {
888− return my_syscall3(__NR_write, fd, buf, count);
888+ return __nolibc_syscall3(__NR_write, fd, buf, count);
889889 }
890890
891891 static __attribute__((unused))
892892 ssize_t write(int fd, const void *buf, size_t count)
893893 {
894− return __sysret(sys_write(fd, buf, count));
894+ return __sysret(_sys_write(fd, buf, count));
895895 }
896896
897897
@@ -900,15 +900,15 @@ ssize_t write(int fd, const void *buf, size_t count) · 2 unmodified lines
900900 */
901901
902902 static __attribute__((unused))
903−int sys_memfd_create(const char *name, unsigned int flags)
903+int _sys_memfd_create(const char *name, unsigned int flags)
904904 {
905− return my_syscall2(__NR_memfd_create, name, flags);
905+ return __nolibc_syscall2(__NR_memfd_create, name, flags);
906906 }
907907
908908 static __attribute__((unused))
909909 int memfd_create(const char *name, unsigned int flags)
910910 {
911− return __sysret(sys_memfd_create(name, flags));
911+ return __sysret(_sys_memfd_create(name, flags));
912912 }
913913
914914 #endif /* _NOLIBC_SYS_H */
tools/include/nolibc/sys/ioctl.h+3−3
@@ -19,11 +19,11 @@ · 18 unmodified lines
1919 */
2020
2121 static __attribute__((unused))
22−long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
22+long _sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
2323 {
24− return my_syscall3(__NR_ioctl, fd, cmd, arg);
24+ return __nolibc_syscall3(__NR_ioctl, fd, cmd, arg);
2525 }
2626
27−#define ioctl(fd, cmd, arg) __sysret(sys_ioctl(fd, cmd, (unsigned long)(arg)))
27+#define ioctl(fd, cmd, arg) __sysret(_sys_ioctl(fd, cmd, (unsigned long)(arg)))
2828
2929 #endif /* _NOLIBC_SYS_IOCTL_H */
tools/include/nolibc/sys/mman.h+12−12
@@ -13,10 +13,10 @@ · 12 unmodified lines
1313 #include "../arch.h"
1414 #include "../sys.h"
1515
16−#ifndef sys_mmap
16+#ifndef _sys_mmap
1717 static __attribute__((unused))
18−void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd,
19− off_t offset)
18+void *_sys_mmap(void *addr, size_t length, int prot, int flags, int fd,
19+ off_t offset)
2020 {
2121 int n;
2222
@@ -27,14 +27,14 @@ void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd, · 4 unmodified lines
2727 n = __NR_mmap;
2828 #endif
2929
30− return (void *)my_syscall6(n, addr, length, prot, flags, fd, offset);
30+ return (void *)__nolibc_syscall6(n, addr, length, prot, flags, fd, offset);
3131 }
3232 #endif
3333
3434 static __attribute__((unused))
3535 void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
3636 {
37− void *ret = sys_mmap(addr, length, prot, flags, fd, offset);
37+ void *ret = _sys_mmap(addr, length, prot, flags, fd, offset);
3838
3939 if ((unsigned long)ret >= -4095UL) {
4040 SET_ERRNO(-(long)ret);
@@ -44,16 +44,16 @@ void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) · 3 unmodified lines
4444 }
4545
4646 static __attribute__((unused))
47−void *sys_mremap(void *old_address, size_t old_size, size_t new_size, int flags, void *new_address)
47+void *_sys_mremap(void *old_address, size_t old_size, size_t new_size, int flags, void *new_address)
4848 {
49− return (void *)my_syscall5(__NR_mremap, old_address, old_size,
50− new_size, flags, new_address);
49+ return (void *)__nolibc_syscall5(__NR_mremap, old_address, old_size,
50+ new_size, flags, new_address);
5151 }
5252
5353 static __attribute__((unused))
5454 void *mremap(void *old_address, size_t old_size, size_t new_size, int flags, void *new_address)
5555 {
56− void *ret = sys_mremap(old_address, old_size, new_size, flags, new_address);
56+ void *ret = _sys_mremap(old_address, old_size, new_size, flags, new_address);
5757
5858 if ((unsigned long)ret >= -4095UL) {
5959 SET_ERRNO(-(long)ret);
@@ -63,15 +63,15 @@ void *mremap(void *old_address, size_t old_size, size_t new_size, int flags, voi · 3 unmodified lines
6363 }
6464
6565 static __attribute__((unused))
66−int sys_munmap(void *addr, size_t length)
66+int _sys_munmap(void *addr, size_t length)
6767 {
68− return my_syscall2(__NR_munmap, addr, length);
68+ return __nolibc_syscall2(__NR_munmap, addr, length);
6969 }
7070
7171 static __attribute__((unused))
7272 int munmap(void *addr, size_t length)
7373 {
74− return __sysret(sys_munmap(addr, length));
74+ return __sysret(_sys_munmap(addr, length));
7575 }
7676
7777 #endif /* _NOLIBC_SYS_MMAN_H */
tools/include/nolibc/sys/mount.h+4−4
@@ -20,10 +20,10 @@ · 19 unmodified lines
2020 * const void *data);
2121 */
2222 static __attribute__((unused))
23−int sys_mount(const char *src, const char *tgt, const char *fst,
24− unsigned long flags, const void *data)
23+int _sys_mount(const char *src, const char *tgt, const char *fst,
24+ unsigned long flags, const void *data)
2525 {
26− return my_syscall5(__NR_mount, src, tgt, fst, flags, data);
26+ return __nolibc_syscall5(__NR_mount, src, tgt, fst, flags, data);
2727 }
2828
2929 static __attribute__((unused))
@@ -31,7 +31,7 @@ int mount(const char *src, const char *tgt, · 1 unmodified line
3131 const char *fst, unsigned long flags,
3232 const void *data)
3333 {
34− return __sysret(sys_mount(src, tgt, fst, flags, data));
34+ return __sysret(_sys_mount(src, tgt, fst, flags, data));
3535 }
3636
3737 #endif /* _NOLIBC_SYS_MOUNT_H */
tools/include/nolibc/sys/prctl.h+4−4
@@ -20,17 +20,17 @@ · 19 unmodified lines
2020 */
2121
2222 static __attribute__((unused))
23−int sys_prctl(int option, unsigned long arg2, unsigned long arg3,
24− unsigned long arg4, unsigned long arg5)
23+int _sys_prctl(int option, unsigned long arg2, unsigned long arg3,
24+ unsigned long arg4, unsigned long arg5)
2525 {
26− return my_syscall5(__NR_prctl, option, arg2, arg3, arg4, arg5);
26+ return __nolibc_syscall5(__NR_prctl, option, arg2, arg3, arg4, arg5);
2727 }
2828
2929 static __attribute__((unused))
3030 int prctl(int option, unsigned long arg2, unsigned long arg3,
3131 unsigned long arg4, unsigned long arg5)
3232 {
33− return __sysret(sys_prctl(option, arg2, arg3, arg4, arg5));
33+ return __sysret(_sys_prctl(option, arg2, arg3, arg4, arg5));
3434 }
3535
3636 #endif /* _NOLIBC_SYS_PRCTL_H */
tools/include/nolibc/sys/ptrace.h+3−3
@@ -19,15 +19,15 @@ · 18 unmodified lines
1919 * long ptrace(int op, pid_t pid, void *addr, void *data);
2020 */
2121 static __attribute__((unused))
22−long sys_ptrace(int op, pid_t pid, void *addr, void *data)
22+long _sys_ptrace(int op, pid_t pid, void *addr, void *data)
2323 {
24− return my_syscall4(__NR_ptrace, op, pid, addr, data);
24+ return __nolibc_syscall4(__NR_ptrace, op, pid, addr, data);
2525 }
2626
2727 static __attribute__((unused))
2828 ssize_t ptrace(int op, pid_t pid, void *addr, void *data)
2929 {
30− return __sysret(sys_ptrace(op, pid, addr, data));
30+ return __sysret(_sys_ptrace(op, pid, addr, data));
3131 }
3232
3333 #endif /* _NOLIBC_SYS_PTRACE_H */
tools/include/nolibc/sys/random.h+3−3
@@ -20,15 +20,15 @@ · 19 unmodified lines
2020 */
2121
2222 static __attribute__((unused))
23−ssize_t sys_getrandom(void *buf, size_t buflen, unsigned int flags)
23+ssize_t _sys_getrandom(void *buf, size_t buflen, unsigned int flags)
2424 {
25− return my_syscall3(__NR_getrandom, buf, buflen, flags);
25+ return __nolibc_syscall3(__NR_getrandom, buf, buflen, flags);
2626 }
2727
2828 static __attribute__((unused))
2929 ssize_t getrandom(void *buf, size_t buflen, unsigned int flags)
3030 {
31− return __sysret(sys_getrandom(buf, buflen, flags));
31+ return __sysret(_sys_getrandom(buf, buflen, flags));
3232 }
3333
3434 #endif /* _NOLIBC_SYS_RANDOM_H */
tools/include/nolibc/sys/reboot.h+3−3
@@ -20,15 +20,15 @@ · 19 unmodified lines
2020 */
2121
2222 static __attribute__((unused))
23−ssize_t sys_reboot(int magic1, int magic2, int cmd, void *arg)
23+ssize_t _sys_reboot(int magic1, int magic2, int cmd, void *arg)
2424 {
25− return my_syscall4(__NR_reboot, magic1, magic2, cmd, arg);
25+ return __nolibc_syscall4(__NR_reboot, magic1, magic2, cmd, arg);
2626 }
2727
2828 static __attribute__((unused))
2929 int reboot(int cmd)
3030 {
31− return __sysret(sys_reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, NULL));
31+ return __sysret(_sys_reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, NULL));
3232 }
3333
3434 #endif /* _NOLIBC_SYS_REBOOT_H */
tools/include/nolibc/sys/resource.h+5−5
@@ -20,10 +20,10 @@ · 19 unmodified lines
2020 */
2121
2222 static __attribute__((unused))
23−int sys_prlimit64(pid_t pid, int resource,
24− const struct rlimit64 *new_limit, struct rlimit64 *old_limit)
23+int _sys_prlimit64(pid_t pid, int resource,
24+ const struct rlimit64 *new_limit, struct rlimit64 *old_limit)
2525 {
26− return my_syscall4(__NR_prlimit64, pid, resource, new_limit, old_limit);
26+ return __nolibc_syscall4(__NR_prlimit64, pid, resource, new_limit, old_limit);
2727 }
2828
2929 static __attribute__((unused))
@@ -32,7 +32,7 @@ int getrlimit(int resource, struct rlimit *rlim) · 2 unmodified lines
3232 struct rlimit64 rlim64;
3333 int ret;
3434
35− ret = __sysret(sys_prlimit64(0, resource, NULL, &rlim64));
35+ ret = __sysret(_sys_prlimit64(0, resource, NULL, &rlim64));
3636 rlim->rlim_cur = rlim64.rlim_cur;
3737 rlim->rlim_max = rlim64.rlim_max;
3838
@@ -47,7 +47,7 @@ int setrlimit(int resource, const struct rlimit *rlim) · 8 unmodified lines
4747 .rlim_max = rlim->rlim_max,
4848 };
4949
50− return __sysret(sys_prlimit64(0, resource, &rlim64, NULL));
50+ return __sysret(_sys_prlimit64(0, resource, &rlim64, NULL));
5151 }
5252
5353 #endif /* _NOLIBC_SYS_RESOURCE_H */
tools/include/nolibc/sys/select.h+6−4
@@ -61,7 +61,7 @@ typedef struct { · 60 unmodified lines
6161 */
6262
6363 static __attribute__((unused))
64−int sys_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
64+int _sys_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
6565 {
6666 #if defined(__NR_pselect6_time64)
6767 struct __kernel_timespec t;
@@ -70,7 +70,8 @@ int sys_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeva · 2 unmodified lines
7070 t.tv_sec = timeout->tv_sec;
7171 t.tv_nsec = (uint32_t)timeout->tv_usec * 1000;
7272 }
73− return my_syscall6(__NR_pselect6_time64, nfds, rfds, wfds, efds, timeout ? &t : NULL, NULL);
73+ return __nolibc_syscall6(__NR_pselect6_time64, nfds, rfds, wfds, efds,
74+ timeout ? &t : NULL, NULL);
7475 #else
7576 struct __kernel_old_timespec t;
7677
@@ -78,14 +79,15 @@ int sys_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeva · 1 unmodified line
7879 t.tv_sec = timeout->tv_sec;
7980 t.tv_nsec = (uint32_t)timeout->tv_usec * 1000;
8081 }
81− return my_syscall6(__NR_pselect6, nfds, rfds, wfds, efds, timeout ? &t : NULL, NULL);
82+ return __nolibc_syscall6(__NR_pselect6, nfds, rfds, wfds, efds,
83+ timeout ? &t : NULL, NULL);
8284 #endif
8385 }
8486
8587 static __attribute__((unused))
8688 int select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
8789 {
88− return __sysret(sys_select(nfds, rfds, wfds, efds, timeout));
90+ return __sysret(_sys_select(nfds, rfds, wfds, efds, timeout));
8991 }
9092
9193
tools/include/nolibc/sys/stat.h+7−10
@@ -13,6 +13,7 @@ · 12 unmodified lines
1313 #include "../arch.h"
1414 #include "../types.h"
1515 #include "../sys.h"
16+#include "../sys/sysmacros.h"
1617
1718 /*
1819 * int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf);
@@ -23,10 +24,10 @@ · 4 unmodified lines
2324 */
2425
2526 static __attribute__((unused))
26−int sys_statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf)
27+int _sys_statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf)
2728 {
2829 #ifdef __NR_statx
29− return my_syscall5(__NR_statx, fd, path, flags, mask, buf);
30+ return __nolibc_syscall5(__NR_statx, fd, path, flags, mask, buf);
3031 #else
3132 return __nolibc_enosys(__func__, fd, path, flags, mask, buf);
3233 #endif
@@ -35,7 +36,7 @@ int sys_statx(int fd, const char *path, int flags, unsigned int mask, struct sta · 2 unmodified lines
3536 static __attribute__((unused))
3637 int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf)
3738 {
38− return __sysret(sys_statx(fd, path, flags, mask, buf));
39+ return __sysret(_sys_statx(fd, path, flags, mask, buf));
3940 }
4041
4142
@@ -45,21 +46,17 @@ int fstatat(int fd, const char *path, struct stat *buf, int flag) · 3 unmodified lines
4546 struct statx statx;
4647 long ret;
4748
48− ret = __sysret(sys_statx(fd, path, flag | AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &statx));
49+ ret = __sysret(_sys_statx(fd, path, flag | AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &statx));
4950 if (ret == -1)
5051 return ret;
5152
52− buf->st_dev = ((statx.stx_dev_minor & 0xff)
53− | (statx.stx_dev_major << 8)
54− | ((statx.stx_dev_minor & ~0xff) << 12));
53+ buf->st_dev = makedev(statx.stx_dev_major, statx.stx_dev_minor);
5554 buf->st_ino = statx.stx_ino;
5655 buf->st_mode = statx.stx_mode;
5756 buf->st_nlink = statx.stx_nlink;
5857 buf->st_uid = statx.stx_uid;
5958 buf->st_gid = statx.stx_gid;
60− buf->st_rdev = ((statx.stx_rdev_minor & 0xff)
61− | (statx.stx_rdev_major << 8)
62− | ((statx.stx_rdev_minor & ~0xff) << 12));
59+ buf->st_rdev = makedev(statx.stx_rdev_major, statx.stx_rdev_minor);
6360 buf->st_size = statx.stx_size;
6461 buf->st_blksize = statx.stx_blksize;
6562 buf->st_blocks = statx.stx_blocks;
tools/include/nolibc/sys/syscall.h+6−5
@@ -10,10 +10,11 @@ · 9 unmodified lines
1010 #ifndef _NOLIBC_SYS_SYSCALL_H
1111 #define _NOLIBC_SYS_SYSCALL_H
1212
13−#define __syscall_narg(_0, _1, _2, _3, _4, _5, _6, N, ...) N
14−#define _syscall_narg(...) __syscall_narg(__VA_ARGS__, 6, 5, 4, 3, 2, 1, 0)
15−#define _syscall(N, ...) __sysret(my_syscall##N(__VA_ARGS__))
16−#define _syscall_n(N, ...) _syscall(N, __VA_ARGS__)
17−#define syscall(...) _syscall_n(_syscall_narg(__VA_ARGS__), ##__VA_ARGS__)
13+#define ___nolibc_syscall_narg(_0, _1, _2, _3, _4, _5, _6, N, ...) N
14+#define __nolibc_syscall_narg(...) ___nolibc_syscall_narg(__VA_ARGS__, 6, 5, 4, 3, 2, 1, 0)
15+#define __nolibc_syscall(N, ...) __nolibc_syscall##N(__VA_ARGS__)
16+#define __nolibc_syscall_n(N, ...) __nolibc_syscall(N, __VA_ARGS__)
17+#define _syscall(...) __nolibc_syscall_n(__nolibc_syscall_narg(__VA_ARGS__), ##__VA_ARGS__)
18+#define syscall(...) __sysret(_syscall(__VA_ARGS__))
1819
1920 #endif /* _NOLIBC_SYS_SYSCALL_H */
tools/include/nolibc/sys/sysmacros.h+21−4
@@ -12,9 +12,26 @@ · 11 unmodified lines
1212
1313 #include "../std.h"
1414
15−/* WARNING, it only deals with the 4096 first majors and 256 first minors */
16−#define makedev(major, minor) ((dev_t)((((major) & 0xfff) << 8) | ((minor) & 0xff)))
17−#define major(dev) ((unsigned int)(((dev) >> 8) & 0xfff))
18−#define minor(dev) ((unsigned int)((dev) & 0xff))
15+static __inline__ dev_t __nolibc_makedev(unsigned int maj, unsigned int min)
16+{
17+ return (((dev_t)maj & ~0xfff) << 32) | ((maj & 0xfff) << 8) |
18+ (((dev_t)min & ~0xff) << 12) | (min & 0xff);
19+}
20+
21+#define makedev(maj, min) __nolibc_makedev(maj, min)
22+
23+static __inline__ unsigned int __nolibc_major(dev_t dev)
24+{
25+ return ((dev >> 32) & ~0xfff) | ((dev >> 8) & 0xfff);
26+}
27+
28+#define major(dev) __nolibc_major(dev)
29+
30+static __inline__ unsigned int __nolibc_minor(dev_t dev)
31+{
32+ return ((dev >> 12) & ~0xff) | (dev & 0xff);
33+}
34+
35+#define minor(dev) __nolibc_minor(dev)
1936
2037 #endif /* _NOLIBC_SYS_SYSMACROS_H */
tools/include/nolibc/sys/time.h+4−4
@@ -13,21 +13,21 @@ · 12 unmodified lines
1313 #include "../arch.h"
1414 #include "../sys.h"
1515
16−static int sys_clock_gettime(clockid_t clockid, struct timespec *tp);
16+static int _sys_clock_gettime(clockid_t clockid, struct timespec *tp);
1717
1818 /*
1919 * int gettimeofday(struct timeval *tv, struct timezone *tz);
2020 */
2121
2222 static __attribute__((unused))
23−int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
23+int _sys_gettimeofday(struct timeval *tv, struct timezone *tz)
2424 {
2525 (void) tz; /* Non-NULL tz is undefined behaviour */
2626
2727 struct timespec tp;
2828 int ret;
2929
30− ret = sys_clock_gettime(CLOCK_REALTIME, &tp);
30+ ret = _sys_clock_gettime(CLOCK_REALTIME, &tp);
3131 if (!ret && tv) {
3232 tv->tv_sec = tp.tv_sec;
3333 tv->tv_usec = (uint32_t)tp.tv_nsec / 1000;
@@ -39,7 +39,7 @@ int sys_gettimeofday(struct timeval *tv, struct timezone *tz) · 5 unmodified lines
3939 static __attribute__((unused))
4040 int gettimeofday(struct timeval *tv, struct timezone *tz)
4141 {
42− return __sysret(sys_gettimeofday(tv, tz));
42+ return __sysret(_sys_gettimeofday(tv, tz));
4343 }
4444
4545 #endif /* _NOLIBC_SYS_TIME_H */
tools/include/nolibc/sys/timerfd.h+12−12
@@ -17,47 +17,47 @@ · 16 unmodified lines
1717
1818
1919 static __attribute__((unused))
20−int sys_timerfd_create(int clockid, int flags)
20+int _sys_timerfd_create(int clockid, int flags)
2121 {
22− return my_syscall2(__NR_timerfd_create, clockid, flags);
22+ return __nolibc_syscall2(__NR_timerfd_create, clockid, flags);
2323 }
2424
2525 static __attribute__((unused))
2626 int timerfd_create(int clockid, int flags)
2727 {
28− return __sysret(sys_timerfd_create(clockid, flags));
28+ return __sysret(_sys_timerfd_create(clockid, flags));
2929 }
3030
3131
3232 static __attribute__((unused))
33−int sys_timerfd_gettime(int fd, struct itimerspec *curr_value)
33+int _sys_timerfd_gettime(int fd, struct itimerspec *curr_value)
3434 {
3535 #if defined(__NR_timerfd_gettime64)
3636 __nolibc_assert_time64_type(curr_value->it_value.tv_sec);
37− return my_syscall2(__NR_timerfd_gettime64, fd, curr_value);
37+ return __nolibc_syscall2(__NR_timerfd_gettime64, fd, curr_value);
3838 #else
3939 __nolibc_assert_native_time64();
40− return my_syscall2(__NR_timerfd_gettime, fd, curr_value);
40+ return __nolibc_syscall2(__NR_timerfd_gettime, fd, curr_value);
4141 #endif
4242 }
4343
4444 static __attribute__((unused))
4545 int timerfd_gettime(int fd, struct itimerspec *curr_value)
4646 {
47− return __sysret(sys_timerfd_gettime(fd, curr_value));
47+ return __sysret(_sys_timerfd_gettime(fd, curr_value));
4848 }
4949
5050
5151 static __attribute__((unused))
52−int sys_timerfd_settime(int fd, int flags,
53− const struct itimerspec *new_value, struct itimerspec *old_value)
52+int _sys_timerfd_settime(int fd, int flags,
53+ const struct itimerspec *new_value, struct itimerspec *old_value)
5454 {
5555 #if defined(__NR_timerfd_settime64)
5656 __nolibc_assert_time64_type(new_value->it_value.tv_sec);
57− return my_syscall4(__NR_timerfd_settime64, fd, flags, new_value, old_value);
57+ return __nolibc_syscall4(__NR_timerfd_settime64, fd, flags, new_value, old_value);
5858 #else
5959 __nolibc_assert_native_time64();
60− return my_syscall4(__NR_timerfd_settime, fd, flags, new_value, old_value);
60+ return __nolibc_syscall4(__NR_timerfd_settime, fd, flags, new_value, old_value);
6161 #endif
6262 }
6363
@@ -65,7 +65,7 @@ static __attribute__((unused)) · 1 unmodified line
6565 int timerfd_settime(int fd, int flags,
6666 const struct itimerspec *new_value, struct itimerspec *old_value)
6767 {
68− return __sysret(sys_timerfd_settime(fd, flags, new_value, old_value));
68+ return __sysret(_sys_timerfd_settime(fd, flags, new_value, old_value));
6969 }
7070
7171 #endif /* _NOLIBC_SYS_TIMERFD_H */
tools/include/nolibc/sys/uio.h+6−6
@@ -19,30 +19,30 @@ · 18 unmodified lines
1919 * ssize_t readv(int fd, const struct iovec *iovec, int count);
2020 */
2121 static __attribute__((unused))
22−ssize_t sys_readv(int fd, const struct iovec *iovec, int count)
22+ssize_t _sys_readv(int fd, const struct iovec *iovec, int count)
2323 {
24− return my_syscall3(__NR_readv, fd, iovec, count);
24+ return __nolibc_syscall3(__NR_readv, fd, iovec, count);
2525 }
2626
2727 static __attribute__((unused))
2828 ssize_t readv(int fd, const struct iovec *iovec, int count)
2929 {
30− return __sysret(sys_readv(fd, iovec, count));
30+ return __sysret(_sys_readv(fd, iovec, count));
3131 }
3232
3333 /*
3434 * ssize_t writev(int fd, const struct iovec *iovec, int count);
3535 */
3636 static __attribute__((unused))
37−ssize_t sys_writev(int fd, const struct iovec *iovec, int count)
37+ssize_t _sys_writev(int fd, const struct iovec *iovec, int count)
3838 {
39− return my_syscall3(__NR_writev, fd, iovec, count);
39+ return __nolibc_syscall3(__NR_writev, fd, iovec, count);
4040 }
4141
4242 static __attribute__((unused))
4343 ssize_t writev(int fd, const struct iovec *iovec, int count)
4444 {
45− return __sysret(sys_writev(fd, iovec, count));
45+ return __sysret(_sys_writev(fd, iovec, count));
4646 }
4747
4848
tools/include/nolibc/sys/utsname.h+3−3
@@ -28,15 +28,15 @@ struct utsname { · 27 unmodified lines
2828 };
2929
3030 static __attribute__((unused))
31−int sys_uname(struct utsname *buf)
31+int _sys_uname(struct utsname *buf)
3232 {
33− return my_syscall1(__NR_uname, buf);
33+ return __nolibc_syscall1(__NR_uname, buf);
3434 }
3535
3636 static __attribute__((unused))
3737 int uname(struct utsname *buf)
3838 {
39− return __sysret(sys_uname(buf));
39+ return __sysret(_sys_uname(buf));
4040 }
4141
4242 #endif /* _NOLIBC_SYS_UTSNAME_H */
tools/include/nolibc/sys/wait.h+3−3
@@ -21,15 +21,15 @@ · 20 unmodified lines
2121 */
2222
2323 static __attribute__((unused))
24−int sys_waitid(int which, pid_t pid, siginfo_t *infop, int options, struct rusage *rusage)
24+int _sys_waitid(int which, pid_t pid, siginfo_t *infop, int options, struct rusage *rusage)
2525 {
26− return my_syscall5(__NR_waitid, which, pid, infop, options, rusage);
26+ return __nolibc_syscall5(__NR_waitid, which, pid, infop, options, rusage);
2727 }
2828
2929 static __attribute__((unused))
3030 int waitid(int which, pid_t pid, siginfo_t *infop, int options)
3131 {
32− return __sysret(sys_waitid(which, pid, infop, options, NULL));
32+ return __sysret(_sys_waitid(which, pid, infop, options, NULL));
3333 }
3434
3535
tools/include/nolibc/time.h+34−34
@@ -33,69 +33,69 @@ · 32 unmodified lines
3333 */
3434
3535 static __attribute__((unused))
36−int sys_clock_getres(clockid_t clockid, struct timespec *res)
36+int _sys_clock_getres(clockid_t clockid, struct timespec *res)
3737 {
3838 #if defined(__NR_clock_getres_time64)
3939 __nolibc_assert_time64_type(res->tv_sec);
40− return my_syscall2(__NR_clock_getres_time64, clockid, res);
40+ return __nolibc_syscall2(__NR_clock_getres_time64, clockid, res);
4141 #else
4242 __nolibc_assert_native_time64();
43− return my_syscall2(__NR_clock_getres, clockid, res);
43+ return __nolibc_syscall2(__NR_clock_getres, clockid, res);
4444 #endif
4545 }
4646
4747 static __attribute__((unused))
4848 int clock_getres(clockid_t clockid, struct timespec *res)
4949 {
50− return __sysret(sys_clock_getres(clockid, res));
50+ return __sysret(_sys_clock_getres(clockid, res));
5151 }
5252
5353 static __attribute__((unused))
54−int sys_clock_gettime(clockid_t clockid, struct timespec *tp)
54+int _sys_clock_gettime(clockid_t clockid, struct timespec *tp)
5555 {
5656 #if defined(__NR_clock_gettime64)
5757 __nolibc_assert_time64_type(tp->tv_sec);
58− return my_syscall2(__NR_clock_gettime64, clockid, tp);
58+ return __nolibc_syscall2(__NR_clock_gettime64, clockid, tp);
5959 #else
6060 __nolibc_assert_native_time64();
61− return my_syscall2(__NR_clock_gettime, clockid, tp);
61+ return __nolibc_syscall2(__NR_clock_gettime, clockid, tp);
6262 #endif
6363 }
6464
6565 static __attribute__((unused))
6666 int clock_gettime(clockid_t clockid, struct timespec *tp)
6767 {
68− return __sysret(sys_clock_gettime(clockid, tp));
68+ return __sysret(_sys_clock_gettime(clockid, tp));
6969 }
7070
7171 static __attribute__((unused))
72−int sys_clock_settime(clockid_t clockid, struct timespec *tp)
72+int _sys_clock_settime(clockid_t clockid, struct timespec *tp)
7373 {
7474 #if defined(__NR_clock_settime64)
7575 __nolibc_assert_time64_type(tp->tv_sec);
76− return my_syscall2(__NR_clock_settime64, clockid, tp);
76+ return __nolibc_syscall2(__NR_clock_settime64, clockid, tp);
7777 #else
7878 __nolibc_assert_native_time64();
79− return my_syscall2(__NR_clock_settime, clockid, tp);
79+ return __nolibc_syscall2(__NR_clock_settime, clockid, tp);
8080 #endif
8181 }
8282
8383 static __attribute__((unused))
8484 int clock_settime(clockid_t clockid, struct timespec *tp)
8585 {
86− return __sysret(sys_clock_settime(clockid, tp));
86+ return __sysret(_sys_clock_settime(clockid, tp));
8787 }
8888
8989 static __attribute__((unused))
90−int sys_clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp,
91− struct timespec *rmtp)
90+int _sys_clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp,
91+ struct timespec *rmtp)
9292 {
9393 #if defined(__NR_clock_nanosleep_time64)
9494 __nolibc_assert_time64_type(rqtp->tv_sec);
95− return my_syscall4(__NR_clock_nanosleep_time64, clockid, flags, rqtp, rmtp);
95+ return __nolibc_syscall4(__NR_clock_nanosleep_time64, clockid, flags, rqtp, rmtp);
9696 #else
9797 __nolibc_assert_native_time64();
98− return my_syscall4(__NR_clock_nanosleep, clockid, flags, rqtp, rmtp);
98+ return __nolibc_syscall4(__NR_clock_nanosleep, clockid, flags, rqtp, rmtp);
9999 #endif
100100 }
101101
@@ -104,7 +104,7 @@ int clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp, · 2 unmodified lines
104104 struct timespec *rmtp)
105105 {
106106 /* Directly return a positive error number */
107− return -sys_clock_nanosleep(clockid, flags, rqtp, rmtp);
107+ return -_sys_clock_nanosleep(clockid, flags, rqtp, rmtp);
108108 }
109109
110110 static __inline__
@@ -116,7 +116,7 @@ double difftime(time_t time1, time_t time2) · 5 unmodified lines
116116 static __inline__
117117 int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
118118 {
119− return __sysret(sys_clock_nanosleep(CLOCK_REALTIME, 0, rqtp, rmtp));
119+ return __sysret(_sys_clock_nanosleep(CLOCK_REALTIME, 0, rqtp, rmtp));
120120 }
121121
122122
@@ -126,7 +126,7 @@ time_t time(time_t *tptr) · 3 unmodified lines
126126 struct timeval tv;
127127
128128 /* note, cannot fail here */
129− sys_gettimeofday(&tv, NULL);
129+ _sys_gettimeofday(&tv, NULL);
130130
131131 if (tptr)
132132 *tptr = tv.tv_sec;
@@ -141,57 +141,57 @@ time_t time(time_t *tptr) · 8 unmodified lines
141141 */
142142
143143 static __attribute__((unused))
144−int sys_timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid)
144+int _sys_timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid)
145145 {
146− return my_syscall3(__NR_timer_create, clockid, evp, timerid);
146+ return __nolibc_syscall3(__NR_timer_create, clockid, evp, timerid);
147147 }
148148
149149 static __attribute__((unused))
150150 int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid)
151151 {
152− return __sysret(sys_timer_create(clockid, evp, timerid));
152+ return __sysret(_sys_timer_create(clockid, evp, timerid));
153153 }
154154
155155 static __attribute__((unused))
156−int sys_timer_delete(timer_t timerid)
156+int _sys_timer_delete(timer_t timerid)
157157 {
158− return my_syscall1(__NR_timer_delete, timerid);
158+ return __nolibc_syscall1(__NR_timer_delete, timerid);
159159 }
160160
161161 static __attribute__((unused))
162162 int timer_delete(timer_t timerid)
163163 {
164− return __sysret(sys_timer_delete(timerid));
164+ return __sysret(_sys_timer_delete(timerid));
165165 }
166166
167167 static __attribute__((unused))
168−int sys_timer_gettime(timer_t timerid, struct itimerspec *curr_value)
168+int _sys_timer_gettime(timer_t timerid, struct itimerspec *curr_value)
169169 {
170170 #if defined(__NR_timer_gettime64)
171171 __nolibc_assert_time64_type(curr_value->it_value.tv_sec);
172− return my_syscall2(__NR_timer_gettime64, timerid, curr_value);
172+ return __nolibc_syscall2(__NR_timer_gettime64, timerid, curr_value);
173173 #else
174174 __nolibc_assert_native_time64();
175− return my_syscall2(__NR_timer_gettime, timerid, curr_value);
175+ return __nolibc_syscall2(__NR_timer_gettime, timerid, curr_value);
176176 #endif
177177 }
178178
179179 static __attribute__((unused))
180180 int timer_gettime(timer_t timerid, struct itimerspec *curr_value)
181181 {
182− return __sysret(sys_timer_gettime(timerid, curr_value));
182+ return __sysret(_sys_timer_gettime(timerid, curr_value));
183183 }
184184
185185 static __attribute__((unused))
186−int sys_timer_settime(timer_t timerid, int flags,
187− const struct itimerspec *new_value, struct itimerspec *old_value)
186+int _sys_timer_settime(timer_t timerid, int flags,
187+ const struct itimerspec *new_value, struct itimerspec *old_value)
188188 {
189189 #if defined(__NR_timer_settime64)
190190 __nolibc_assert_time64_type(new_value->it_value.tv_sec);
191− return my_syscall4(__NR_timer_settime64, timerid, flags, new_value, old_value);
191+ return __nolibc_syscall4(__NR_timer_settime64, timerid, flags, new_value, old_value);
192192 #else
193193 __nolibc_assert_native_time64();
194− return my_syscall4(__NR_timer_settime, timerid, flags, new_value, old_value);
194+ return __nolibc_syscall4(__NR_timer_settime, timerid, flags, new_value, old_value);
195195 #endif
196196 }
197197
@@ -199,7 +199,7 @@ static __attribute__((unused)) · 1 unmodified line
199199 int timer_settime(timer_t timerid, int flags,
200200 const struct itimerspec *new_value, struct itimerspec *old_value)
201201 {
202− return __sysret(sys_timer_settime(timerid, flags, new_value, old_value));
202+ return __sysret(_sys_timer_settime(timerid, flags, new_value, old_value));
203203 }
204204
205205 #endif /* _NOLIBC_TIME_H */
tools/include/nolibc/unistd.h+6−6
@@ -31,15 +31,15 @@ · 30 unmodified lines
3131 */
3232
3333 static __attribute__((unused))
34−int sys_faccessat(int fd, const char *path, int amode, int flag)
34+int _sys_faccessat(int fd, const char *path, int amode, int flag)
3535 {
36− return my_syscall4(__NR_faccessat, fd, path, amode, flag);
36+ return __nolibc_syscall4(__NR_faccessat, fd, path, amode, flag);
3737 }
3838
3939 static __attribute__((unused))
4040 int faccessat(int fd, const char *path, int amode, int flag)
4141 {
42− return __sysret(sys_faccessat(fd, path, amode, flag));
42+ return __sysret(_sys_faccessat(fd, path, amode, flag));
4343 }
4444
4545 static __attribute__((unused))
@@ -54,7 +54,7 @@ int msleep(unsigned int msecs) · 8 unmodified lines
5454 {
5555 struct timeval my_timeval = { msecs / 1000, (msecs % 1000) * 1000 };
5656
57− if (sys_select(0, NULL, NULL, NULL, &my_timeval) < 0)
57+ if (_sys_select(0, NULL, NULL, NULL, &my_timeval) < 0)
5858 return (my_timeval.tv_sec * 1000) +
5959 (my_timeval.tv_usec / 1000) +
6060 !!(my_timeval.tv_usec % 1000);
@@ -67,7 +67,7 @@ unsigned int sleep(unsigned int seconds) · 6 unmodified lines
6767 {
6868 struct timeval my_timeval = { seconds, 0 };
6969
70− if (sys_select(0, NULL, NULL, NULL, &my_timeval) < 0)
70+ if (_sys_select(0, NULL, NULL, NULL, &my_timeval) < 0)
7171 return my_timeval.tv_sec + !!my_timeval.tv_usec;
7272 else
7373 return 0;
@@ -78,7 +78,7 @@ int usleep(unsigned int usecs) · 4 unmodified lines
7878 {
7979 struct timeval my_timeval = { usecs / 1000000, usecs % 1000000 };
8080
81− return sys_select(0, NULL, NULL, NULL, &my_timeval);
81+ return _sys_select(0, NULL, NULL, NULL, &my_timeval);
8282 }
8383
8484 static __attribute__((unused))
tools/testing/selftests/nolibc/Makefile+2−2
@@ -13,9 +13,9 @@ $(OUTPUT)/nolibc-test: CFLAGS = -nostdlib -nostdinc -static \ · 12 unmodified lines
1313 -isystem $(top_srcdir)/tools/include/nolibc -isystem $(top_srcdir)/usr/include \
1414 $(CFLAGS_NOLIBC_TEST)
1515 $(OUTPUT)/nolibc-test: LDLIBS = $(if $(LLVM),,-lgcc)
16−$(OUTPUT)/nolibc-test: nolibc-test.c nolibc-test-linkage.c | headers
16+$(OUTPUT)/nolibc-test: $(NOLIBC_TEST_SOURCES) | headers
1717
18−$(OUTPUT)/libc-test: nolibc-test.c nolibc-test-linkage.c
18+$(OUTPUT)/libc-test: $(NOLIBC_TEST_SOURCES)
1919 $(call msg,CC,,$@)
2020 $(Q)$(LINK.c) $^ -o $@
2121
tools/testing/selftests/nolibc/Makefile.include+4−1
@@ -5,6 +5,9 @@ _CFLAGS_STACKPROTECTOR ?= $(call try-run, \ · 4 unmodified lines
55 echo 'void foo(void) {}' | $(CC) -x c - -o - -S $(CLANG_CROSS_FLAGS) $(__CFLAGS_STACKPROTECTOR) | grep -q __stack_chk_guard, \
66 $(__CFLAGS_STACKPROTECTOR))
77 _CFLAGS_SANITIZER ?= $(call cc-option,-fsanitize=undefined -fsanitize-trap=all)
8−CFLAGS_NOLIBC_TEST ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 -W -Wall -Wextra \
8+CFLAGS_NOLIBC_TEST ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 \
9+ -W -Wall -Wextra -Wundef \
910 $(call cc-option,-fno-stack-protector) $(call cc-option,-Wmissing-prototypes) \
1011 $(_CFLAGS_STACKPROTECTOR) $(_CFLAGS_SANITIZER)
12+
13+NOLIBC_TEST_SOURCES := nolibc-test.c nolibc-test-linkage.c nolibc-test-ignore-errno.c
tools/testing/selftests/nolibc/Makefile.nolibc+7−16
@@ -232,23 +232,14 @@ ifeq ($(origin XARCH),command line) · 231 unmodified lines
232232 CFLAGS_XARCH = $(CFLAGS_$(XARCH))
233233 endif
234234
235+LDLIBS_ppc = $(if $(LLVM),,-lgcc)
236+LDLIBS = $(LDLIBS_$(XARCH))
237+
235238 include Makefile.include
236239
237240 CFLAGS ?= $(CFLAGS_NOLIBC_TEST) $(CFLAGS_XARCH) $(CFLAGS_EXTRA)
238241 LDFLAGS :=
239242
240−LIBGCC := -lgcc
241−
242−ifeq ($(ARCH),x86)
243−# Not needed on x86, probably not present for x32
244−LIBGCC :=
245−endif
246−
247−ifneq ($(LLVM),)
248−# Not needed for clang
249−LIBGCC :=
250−endif
251−
252243 # Modify CFLAGS based on LLVM=
253244 include $(srctree)/tools/scripts/Makefile.include
254245
@@ -302,12 +293,12 @@ sysroot/$(ARCH)/include: · 47 unmodified lines
302293 $(Q)$(MAKE) -C $(srctree)/tools/include/nolibc ARCH=$(ARCH) OUTPUT=$(CURDIR)/sysroot/ headers_standalone headers_check
303294 $(Q)mv sysroot/sysroot sysroot/$(ARCH)
304295
305−nolibc-test: nolibc-test.c nolibc-test-linkage.c sysroot/$(ARCH)/include
296+nolibc-test: $(NOLIBC_TEST_SOURCES) sysroot/$(ARCH)/include
306297 $(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \
307− -nostdlib -nostdinc -static -Isysroot/$(ARCH)/include nolibc-test.c nolibc-test-linkage.c $(LIBGCC)
298+ -nostdlib -nostdinc -static -Isysroot/$(ARCH)/include $(NOLIBC_TEST_SOURCES) $(LDLIBS)
308299
309−libc-test: nolibc-test.c nolibc-test-linkage.c
310− $(QUIET_CC)$(HOSTCC) -o $@ nolibc-test.c nolibc-test-linkage.c
300+libc-test: $(NOLIBC_TEST_SOURCES)
301+ $(QUIET_CC)$(HOSTCC) -o $@ $(NOLIBC_TEST_SOURCES)
311302
312303 # local libc-test
313304 run-libc-test: libc-test
tools/testing/selftests/nolibc/nolibc-test-ignore-errno.c+6−0
@@ -0,0 +1,6 @@
1+/* SPDX-License-Identifier: GPL-2.0 */
2+
3+#define NOLIBC_IGNORE_ERRNO
4+
5+/* Include all of nolibc and make sure everything compiles */
6+#include <stdlib.h>
tools/testing/selftests/nolibc/nolibc-test.c+259−78
@@ -42,6 +42,9 @@ · 41 unmodified lines
4242 #include <unistd.h>
4343 #include <limits.h>
4444 #include <ctype.h>
45+#include <stdbool.h>
46+#include <byteswap.h>
47+#include <endian.h>
4548
4649 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
4750
@@ -66,6 +69,8 @@ static const char *argv0; · 18 unmodified lines
6669 /* will be used by constructor tests */
6770 static int constructor_test_value;
6871
72+static const int is_le = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
73+
6974 static const int is_nolibc =
7075 #ifdef NOLIBC
7176 1
@@ -74,6 +79,30 @@ static const int is_nolibc = · 2 unmodified lines
7479 #endif
7580 ;
7681
82+static const int is_glibc =
83+#ifdef __GLIBC__
84+ 1
85+#else
86+ 0
87+#endif
88+;
89+
90+#if !defined(NOLIBC)
91+/* Some disabled tests may not compile. */
92+
93+/* strlcat() and strlcpy() may not be in the system headers. */
94+#undef strlcat
95+#undef strlcpy
96+#define strlcat(d, s, l) 0
97+#define strlcpy(d, s, l) 0
98+
99+/* readdir_r() is likely to be marked deprecated */
100+#undef readdir_r
101+#define readdir_r(dir, dirent, result) ((errno = EINVAL), -1)
102+
103+#define _syscall(...) 0
104+#endif
105+
77106 /* definition of a series of tests */
78107 struct test {
79108 const char *name; /* test name */
@@ -142,21 +171,6 @@ static const char *errorname(int err) · 62 unmodified lines
142171 }
143172 }
144173
145−static void align_result(size_t llen)
146−{
147− const size_t align = 64;
148− char buf[align];
149− size_t n;
150−
151− if (llen >= align)
152− return;
153−
154− n = align - llen;
155− memset(buf, ' ', n);
156− buf[n] = '\0';
157− fputs(buf, stdout);
158−}
159−
160174 enum RESULT {
161175 OK,
162176 FAIL,
@@ -174,8 +188,10 @@ static void result(int llen, enum RESULT r) · 11 unmodified lines
174188 else
175189 msg = " [FAIL]";
176190
177− align_result(llen);
178− puts(msg);
191+ llen = 64 - llen;
192+ if (llen < 0)
193+ llen = 0;
194+ printf("%*s%s\n", llen, "", msg);
179195 }
180196
181197 /* The tests below are intended to be used by the macroes, which evaluate
@@ -304,10 +320,7 @@ int expect_syszr(int expr, int llen) · 122 unmodified lines
304320 {
305321 int ret = 0;
306322
307− if (errno == ENOSYS) {
308− llen += printf(" = ENOSYS");
309− result(llen, SKIPPED);
310− } else if (expr) {
323+ if (expr) {
311324 ret = 1;
312325 llen += printf(" = %d %s ", expr, errorname(errno));
313326 result(llen, FAIL);
@@ -347,10 +360,7 @@ int expect_sysne(int expr, int llen, int val) · 33 unmodified lines
347360 {
348361 int ret = 0;
349362
350− if (errno == ENOSYS) {
351− llen += printf(" = ENOSYS");
352− result(llen, SKIPPED);
353− } else if (expr == val) {
363+ if (expr == val) {
354364 ret = 1;
355365 llen += printf(" = %d %s ", expr, errorname(errno));
356366 result(llen, FAIL);
@@ -375,9 +385,7 @@ int expect_syserr2(int expr, int expret, int experr1, int experr2, int llen) · 18 unmodified lines
375385 int _errno = errno;
376386
377387 llen += printf(" = %d %s ", expr, errorname(_errno));
378− if (errno == ENOSYS) {
379− result(llen, SKIPPED);
380− } else if (expr != expret || (_errno != experr1 && _errno != experr2)) {
388+ if (expr != expret || (_errno != experr1 && _errno != experr2)) {
381389 ret = 1;
382390 if (experr2 == 0)
383391 llen += printf(" != (%d %s) ", expret, errorname(experr1));
@@ -701,6 +709,37 @@ static void constructor2(int argc, char **argv, char **envp) · 317 unmodified lines
701709 constructor_test_value |= 1 << 1;
702710 }
703711
712+int test_program_invocation_name(void)
713+{
714+ char buf[100];
715+ char *dirsep;
716+ ssize_t r;
717+ int fd;
718+
719+ fd = open("/proc/self/cmdline", O_RDONLY);
720+ if (fd == -1)
721+ return 1;
722+
723+ r = read(fd, buf, sizeof(buf));
724+ close(fd);
725+ if (r < 1 || r == sizeof(buf))
726+ return 1;
727+
728+ buf[r - 1] = '\0';
729+
730+ if (strcmp(program_invocation_name, buf) != 0)
731+ return 1;
732+
733+ dirsep = strrchr(buf, '/');
734+ if (!dirsep || dirsep[1] == '\0')
735+ return 1;
736+
737+ if (strcmp(program_invocation_short_name, dirsep + 1) != 0)
738+ return 1;
739+
740+ return 0;
741+}
742+
704743 int run_startup(int min, int max)
705744 {
706745 int test;
@@ -715,6 +754,7 @@ int run_startup(int min, int max) · 8 unmodified lines
715754 #ifdef NOLIBC
716755 test_auxv = _auxv;
717756 #endif
757+ bool proc = access("/proc", R_OK) == 0;
718758
719759 for (test = min; test >= 0 && test <= max; test++) {
720760 int llen = 0; /* line length */
@@ -740,6 +780,7 @@ int run_startup(int min, int max) · 19 unmodified lines
740780 CASE_TEST(constructor); EXPECT_EQ(is_nolibc, constructor_test_value, 0x3); break;
741781 CASE_TEST(linkage_errno); EXPECT_PTREQ(1, linkage_test_errno_addr(), &errno); break;
742782 CASE_TEST(linkage_constr); EXPECT_EQ(1, linkage_test_constructor_test_value, 0x3); break;
783+ CASE_TEST(prog_name); EXPECT_ZR(proc, test_program_invocation_name()); break;
743784 case __LINE__:
744785 return ret; /* must be last */
745786 /* note: do not set any defaults so as to permit holes above */
@@ -866,7 +907,7 @@ int test_file_stream(void) · 120 unmodified lines
866907
867908 errno = 0;
868909 r = fwrite("foo", 1, 3, f);
869− if (r != 0 || errno != EBADF) {
910+ if (r != 0 || ((is_nolibc || is_glibc) && errno != EBADF)) {
870911 fclose(f);
871912 return -1;
872913 }
@@ -1408,7 +1449,7 @@ int run_syscall(int min, int max) · 535 unmodified lines
14081449 CASE_TEST(fork); EXPECT_SYSZR(1, test_fork(FORK_STANDARD)); break;
14091450 CASE_TEST(getdents64_root); EXPECT_SYSNE(1, test_getdents64("/"), -1); break;
14101451 CASE_TEST(getdents64_null); EXPECT_SYSER(1, test_getdents64("/dev/null"), -1, ENOTDIR); break;
1411− CASE_TEST(directories); EXPECT_SYSZR(proc, test_dirent()); break;
1452+ CASE_TEST(directories); EXPECT_SYSZR(is_nolibc && proc, test_dirent()); break;
14121453 CASE_TEST(getrandom); EXPECT_SYSZR(1, test_getrandom()); break;
14131454 CASE_TEST(gettimeofday_tv); EXPECT_SYSZR(1, gettimeofday(&tv, NULL)); break;
14141455 CASE_TEST(gettimeofday_tv_tz);EXPECT_SYSZR(1, gettimeofday(&tv, &tz)); break;
@@ -1442,6 +1483,7 @@ int run_syscall(int min, int max) · 27 unmodified lines
14421483 CASE_TEST(select_fault); EXPECT_SYSER(1, select(1, (void *)1, NULL, NULL, 0), -1, EFAULT); break;
14431484 CASE_TEST(stat_blah); EXPECT_SYSER(1, stat("/proc/self/blah", &stat_buf), -1, ENOENT); break;
14441485 CASE_TEST(stat_fault); EXPECT_SYSER(1, stat(NULL, &stat_buf), -1, EFAULT); break;
1486+ CASE_TEST(stat_rdev); EXPECT_SYSZR(1, ({ int ret = stat("/dev/null", &stat_buf); ret ?: stat_buf.st_rdev != makedev(1, 3); })); break;
14451487 CASE_TEST(stat_timestamps); EXPECT_SYSZR(1, test_stat_timestamps()); break;
14461488 CASE_TEST(symlink_root); EXPECT_SYSER(1, symlink("/", "/"), -1, EEXIST); break;
14471489 CASE_TEST(timer); EXPECT_SYSZR(1, test_timer()); break;
@@ -1460,9 +1502,11 @@ int run_syscall(int min, int max) · 12 unmodified lines
14601502 CASE_TEST(readv_zero); EXPECT_SYSZR(1, readv(0, NULL, 0)); break;
14611503 CASE_TEST(writev_badf); EXPECT_SYSER(1, writev(-1, &iov_one, 1), -1, EBADF); break;
14621504 CASE_TEST(writev_zero); EXPECT_SYSZR(1, writev(1, NULL, 0)); break;
1463− CASE_TEST(ptrace); EXPECT_SYSER(1, ptrace(PTRACE_CONT, getpid(), NULL, NULL), -1, ESRCH); break;
1505+ CASE_TEST(ptrace); tmp = ptrace(PTRACE_CONT, getpid(), NULL, NULL); EXPECT_SYSER(tmp != -1 && errno != ENOSYS, tmp, -1, EFAULT); break;
14641506 CASE_TEST(syscall_noargs); EXPECT_SYSEQ(1, syscall(__NR_getpid), getpid()); break;
14651507 CASE_TEST(syscall_args); EXPECT_SYSER(1, syscall(__NR_statx, 0, NULL, 0, 0, NULL), -1, EFAULT); break;
1508+ CASE_TEST(_syscall_noargs); EXPECT_SYSEQ(is_nolibc, _syscall(__NR_getpid), getpid()); break;
1509+ CASE_TEST(_syscall_args); EXPECT_SYSEQ(is_nolibc, _syscall(__NR_statx, 0, NULL, 0, 0, NULL), -EFAULT); break;
14661510 CASE_TEST(namespace); EXPECT_SYSZR(euid0 && proc, test_namespace()); break;
14671511 case __LINE__:
14681512 return ret; /* must be last */
@@ -1511,6 +1555,60 @@ int test_time_types(void) · 42 unmodified lines
15111555 return 0;
15121556 }
15131557
1558+int test_malloc(void)
1559+{
1560+ size_t sz_array1, sz_array2, sz_array3;
1561+ int *array1, *array2, *array3;
1562+ int pagesize = getpagesize();
1563+ size_t idx;
1564+
1565+ if (pagesize < 0)
1566+ return 1;
1567+
1568+ /* Dependent on the page size, as that is the granularity of our allocator. */
1569+ sz_array1 = pagesize / 2;
1570+ array1 = malloc(sz_array1 * sizeof(*array1));
1571+ if (!array1)
1572+ return 2;
1573+
1574+ for (idx = 0; idx < sz_array1; idx++)
1575+ array1[idx] = idx;
1576+
1577+ sz_array2 = pagesize * 2;
1578+ array2 = calloc(sz_array2, sizeof(*array2));
1579+ if (!array2) {
1580+ free(array1);
1581+ return 3;
1582+ }
1583+
1584+ for (idx = 0; idx < sz_array2; idx++) {
1585+ if (array2[idx] != 0) {
1586+ free(array2);
1587+ return 4;
1588+ }
1589+ array2[idx] = idx + sz_array1;
1590+ }
1591+
1592+ /* Resize array1 into array3 and append array2 at the end. */
1593+ sz_array3 = sz_array1 + sz_array2;
1594+ array3 = realloc(array1, sz_array3 * sizeof(*array3));
1595+ if (!array3) {
1596+ free(array2);
1597+ free(array1);
1598+ return 5;
1599+ }
1600+ memcpy(array3 + sz_array1, array2, sizeof(*array2) * sz_array2);
1601+ free(array2);
1602+
1603+ /* The contents must be contiguous now. */
1604+ for (idx = 0; idx < sz_array3; idx++)
1605+ if (array3[idx] != (int)idx)
1606+ return 6;
1607+
1608+ free(array3);
1609+ return 0;
1610+}
1611+
15141612 int run_stdlib(int min, int max)
15151613 {
15161614 int test;
@@ -1637,6 +1735,22 @@ int run_stdlib(int min, int max) · 120 unmodified lines
16371735 CASE_TEST(memchr_foobar6_o); EXPECT_STREQ(1, memchr("foobar", 'o', 6), "oobar"); break;
16381736 CASE_TEST(memchr_foobar3_b); EXPECT_STRZR(1, memchr("foobar", 'b', 3)); break;
16391737 CASE_TEST(time_types); EXPECT_ZR(is_nolibc, test_time_types()); break;
1738+ CASE_TEST(makedev); EXPECT_EQ(1, makedev(0x12, 0x34), 0x1234); break;
1739+ CASE_TEST(major); EXPECT_EQ(1, major(0x1234), 0x12); break;
1740+ CASE_TEST(minor); EXPECT_EQ(1, minor(0x1234), 0x34); break;
1741+ CASE_TEST(makedev_big); EXPECT_EQ(1, makedev(0x11223344, 0x55667788), 0x1122355667734488); break;
1742+ CASE_TEST(major_big); EXPECT_EQ(1, major(0x1122355667734488), 0x11223344); break;
1743+ CASE_TEST(minor_big); EXPECT_EQ(1, minor(0x1122355667734488), 0x55667788); break;
1744+ CASE_TEST(malloc); EXPECT_ZR(1, test_malloc()); break;
1745+ CASE_TEST(bswap_16); EXPECT_EQ(1, bswap_16(0x0123), 0x2301); break;
1746+ CASE_TEST(bswap_32); EXPECT_EQ(1, bswap_32(0x01234567), 0x67452301); break;
1747+ CASE_TEST(bswap_64); EXPECT_EQ(1, bswap_64(0x0123456789abcdef), 0xefcdab8967452301); break;
1748+ CASE_TEST(htobe16); EXPECT_EQ(1, htobe16(is_le ? 0x0123 : 0x2301), 0x2301); break;
1749+ CASE_TEST(htole16); EXPECT_EQ(1, htole16(is_le ? 0x0123 : 0x2301), 0x0123); break;
1750+ CASE_TEST(htobe32); EXPECT_EQ(1, htobe32(is_le ? 0x01234567 : 0x67452301), 0x67452301); break;
1751+ CASE_TEST(htole32); EXPECT_EQ(1, htole32(is_le ? 0x01234567 : 0x67452301), 0x01234567); break;
1752+ CASE_TEST(htobe64); EXPECT_EQ(1, htobe64(is_le ? 0x0123456789000000 : 0x8967452301), 0x8967452301); break;
1753+ CASE_TEST(htole64); EXPECT_EQ(1, htole64(is_le ? 0x0123456789 : 0x8967452301000000), 0x0123456789); break;
16401754
16411755 case __LINE__:
16421756 return ret; /* must be last */
@@ -1646,33 +1760,60 @@ int run_stdlib(int min, int max) · 3 unmodified lines
16461760 return ret;
16471761 }
16481762
1649−#define EXPECT_VFPRINTF(c, expected, fmt, ...) \
1650− ret += expect_vfprintf(llen, c, expected, fmt, ##__VA_ARGS__)
1763+#define EXPECT_VFPRINTF(cond, expected, fmt, ...) \
1764+ do { if (!(cond)) result(llen, SKIPPED); else ret += expect_vfprintf(llen, expected, fmt, ##__VA_ARGS__); } while (0)
16511765
1652−static int expect_vfprintf(int llen, int c, const char *expected, const char *fmt, ...)
1766+#define VFPRINTF_LEN 25
1767+static int expect_vfprintf(int llen, const char *expected, const char *fmt, ...)
16531768 {
1654− char buf[100];
1769+ char buf[VFPRINTF_LEN + 80];
1770+ unsigned int cmp_len;
16551771 va_list args;
1656− ssize_t w;
1657− int ret;
1772+ ssize_t written, expected_len;
16581773
1774+ /* Fill and terminate buf[] to check for overlong/absent writes */
1775+ memset(buf, 0xa5, sizeof(buf) - 1);
1776+ buf[sizeof(buf) - 1] = 0;
16591777
16601778 va_start(args, fmt);
1661− /* Only allow writing 21 bytes, to test truncation */
1662− w = vsnprintf(buf, 21, fmt, args);
1779+ /* Limit buffer length to test truncation */
1780+ written = vsnprintf(buf, VFPRINTF_LEN + 1, fmt, args);
16631781 va_end(args);
16641782
1665− if (w != c) {
1666− llen += printf(" written(%d) != %d", (int)w, c);
1783+ llen += printf(" \"%s\"", buf);
1784+
1785+ expected_len = strlen(expected);
1786+ if (expected_len > VFPRINTF_LEN) {
1787+ /* Indicate truncated in test output */
1788+ llen += printf("+");
1789+ cmp_len = VFPRINTF_LEN;
1790+ } else {
1791+ cmp_len = expected_len;
1792+ }
1793+
1794+ if (memcmp(expected, buf, cmp_len) || buf[cmp_len]) {
1795+ llen += printf(" should be \"%.*s\"", VFPRINTF_LEN, expected);
16671796 result(llen, FAIL);
16681797 return 1;
16691798 }
16701799
1671− llen += printf(" \"%s\" = \"%s\"", expected, buf);
1672− ret = strncmp(expected, buf, c);
1800+ if (written != expected_len) {
1801+ llen += printf(" written(%d) != %d", (int)written, (int)expected_len);
1802+ result(llen, FAIL);
1803+ return 1;
1804+ }
16731805
1674− result(llen, ret ? FAIL : OK);
1675− return ret;
1806+ /* Check for any overwrites after the actual data. */
1807+ while (++cmp_len < sizeof(buf) - 1) {
1808+ if ((unsigned char)buf[cmp_len] != 0xa5) {
1809+ llen += printf(" overwrote buf[%d] with 0x%x", cmp_len, buf[cmp_len]);
1810+ result(llen, FAIL);
1811+ return 1;
1812+ }
1813+ }
1814+
1815+ result(llen, OK);
1816+ return 0;
16761817 }
16771818
16781819 static int test_scanf(void)
@@ -1742,23 +1883,6 @@ static int test_scanf(void) · 63 unmodified lines
17421883 return 0;
17431884 }
17441885
1745−int test_strerror(void)
1746−{
1747− char buf[100];
1748− ssize_t ret;
1749−
1750− memset(buf, 'A', sizeof(buf));
1751−
1752− errno = EINVAL;
1753− ret = snprintf(buf, sizeof(buf), "%m");
1754− if (is_nolibc) {
1755− if (ret < 6 || memcmp(buf, "errno=", 6))
1756− return 1;
1757− }
1758−
1759− return 0;
1760−}
1761−
17621886 static int test_printf_error(void)
17631887 {
17641888 int fd, ret, saved_errno;
@@ -1781,6 +1905,29 @@ static int test_printf_error(void) · 16 unmodified lines
17811905 return 0;
17821906 }
17831907
1908+int test_asprintf(void)
1909+{
1910+ char *str;
1911+ int ret;
1912+
1913+ ret = asprintf(&str, "foo%s", "bar");
1914+ if (ret == -1)
1915+ return 1;
1916+
1917+ if (ret != 6) {
1918+ free(str);
1919+ return 2;
1920+ }
1921+
1922+ if (memcmp(str, "foobar", 6) != 0) {
1923+ free(str);
1924+ return 3;
1925+ }
1926+
1927+ free(str);
1928+ return 0;
1929+}
1930+
17841931 static int run_printf(int min, int max)
17851932 {
17861933 int test;
@@ -1793,24 +1940,58 @@ static int run_printf(int min, int max) · 6 unmodified lines
17931940 * test numbers.
17941941 */
17951942 switch (test + __LINE__ + 1) {
1796− CASE_TEST(empty); EXPECT_VFPRINTF(0, "", ""); break;
1797− CASE_TEST(simple); EXPECT_VFPRINTF(3, "foo", "foo"); break;
1798− CASE_TEST(string); EXPECT_VFPRINTF(3, "foo", "%s", "foo"); break;
1799− CASE_TEST(number); EXPECT_VFPRINTF(4, "1234", "%d", 1234); break;
1800− CASE_TEST(negnumber); EXPECT_VFPRINTF(5, "-1234", "%d", -1234); break;
1801− CASE_TEST(unsigned); EXPECT_VFPRINTF(5, "12345", "%u", 12345); break;
1802− CASE_TEST(char); EXPECT_VFPRINTF(1, "c", "%c", 'c'); break;
1803− CASE_TEST(hex); EXPECT_VFPRINTF(1, "f", "%x", 0xf); break;
1804− CASE_TEST(pointer); EXPECT_VFPRINTF(3, "0x1", "%p", (void *) 0x1); break;
1805− CASE_TEST(uintmax_t); EXPECT_VFPRINTF(20, "18446744073709551615", "%ju", 0xffffffffffffffffULL); break;
1806− CASE_TEST(intmax_t); EXPECT_VFPRINTF(20, "-9223372036854775807", "%jd", 0x8000000000000001LL); break;
1807− CASE_TEST(truncation); EXPECT_VFPRINTF(25, "01234567890123456789", "%s", "0123456789012345678901234"); break;
1808− CASE_TEST(string_width); EXPECT_VFPRINTF(10, " 1", "%10s", "1"); break;
1809− CASE_TEST(number_width); EXPECT_VFPRINTF(10, " 1", "%10d", 1); break;
1810− CASE_TEST(width_trunc); EXPECT_VFPRINTF(25, " ", "%25d", 1); break;
1943+ CASE_TEST(empty); EXPECT_VFPRINTF(1, "", ""); break;
1944+ CASE_TEST(simple); EXPECT_VFPRINTF(1, "foo", "foo"); break;
1945+ CASE_TEST(string); EXPECT_VFPRINTF(1, "foo", "%s", "foo"); break;
1946+ CASE_TEST(number); EXPECT_VFPRINTF(1, "1234", "%d", 1234); break;
1947+ CASE_TEST(negnumber); EXPECT_VFPRINTF(1, "-1234", "%d", -1234); break;
1948+ CASE_TEST(num_sign); EXPECT_VFPRINTF(1, "| 1|+2|+3|+4|5|", "|% d|%+d|% +d|%+ d|%#d|", 1, 2, 3, 4, 5); break;
1949+ CASE_TEST(unsigned); EXPECT_VFPRINTF(1, "12345", "%u", 12345); break;
1950+ CASE_TEST(signed_max); EXPECT_VFPRINTF(1, "2147483647", "%i", ~0u >> 1); break;
1951+ CASE_TEST(signed_min); EXPECT_VFPRINTF(1, "-2147483648", "%i", (~0u >> 1) + 1); break;
1952+ CASE_TEST(unsigned_max); EXPECT_VFPRINTF(1, "4294967295", "%u", ~0u); break;
1953+ CASE_TEST(char); EXPECT_VFPRINTF(1, "|c|d| e|", "|%c|%.0c|%4c|", 'c', 'd', 'e'); break;
1954+ CASE_TEST(octal); EXPECT_VFPRINTF(1, "|17| 0033||", "|%o|%6.4o|%.0o|", 017, 033, 0); break;
1955+ CASE_TEST(octal_max); EXPECT_VFPRINTF(1, "1777777777777777777777", "%llo", ~0ULL); break;
1956+ CASE_TEST(octal_alt); EXPECT_VFPRINTF(1, "|0|01|02|034|0|0|", "|%#o|%#o|%#02o|%#02o|%#.0o|%0-#o|", 0, 1, 2, 034, 0, 0); break;
1957+ CASE_TEST(hex_nolibc); EXPECT_VFPRINTF(is_nolibc, "|f|d|", "|%x|%X|", 0xf, 0xd); break;
1958+ CASE_TEST(hex_libc); EXPECT_VFPRINTF(!is_nolibc, "|f|D|", "|%x|%X|", 0xf, 0xd); break;
1959+ CASE_TEST(hex_alt); EXPECT_VFPRINTF(1, "|0x1| 0x2| 0|", "|%#x|%#5x|%#5x|", 1, 2, 0); break;
1960+ CASE_TEST(hex_alt_prec); EXPECT_VFPRINTF(1, "| 0x02|0x03| 0x123|", "|%#5.2x|%#04x|%#6.2x|", 2, 3, 0x123); break;
1961+ CASE_TEST(hex_0_alt); EXPECT_VFPRINTF(1, "|0|0000| 00|", "|%#x|%#04x|%#5.2x|", 0, 0, 0); break;
1962+ CASE_TEST(pointer); EXPECT_VFPRINTF(1, "0x1", "%p", (void *) 0x1); break;
1963+ CASE_TEST(pointer_NULL); EXPECT_VFPRINTF(is_nolibc || is_glibc, "|(nil)|(nil)|", "|%p|%.4p|", (void *)0, (void *)0); break;
1964+ CASE_TEST(string_NULL); EXPECT_VFPRINTF(is_nolibc || is_glibc, "|(null)||(null)|", "|%s|%.5s|%.6s|", (void *)0, (void *)0, (void *)0); break;
1965+ CASE_TEST(percent); EXPECT_VFPRINTF(1, "a%d42%69%", "a%%d%d%%%d%%", 42, 69); break;
1966+ CASE_TEST(perc_qual); EXPECT_VFPRINTF(is_nolibc || is_glibc, "a%d2", "a%-14l%d%d", 2); break;
1967+ CASE_TEST(invalid); EXPECT_VFPRINTF(is_nolibc || is_glibc, "a%12yx3%y42%P", "a%12yx%d%y%d%P", 3, 42); break;
1968+ CASE_TEST(intmax_max); EXPECT_VFPRINTF(1, "9223372036854775807", "%lld", ~0ULL >> 1); break;
1969+ CASE_TEST(intmax_min); EXPECT_VFPRINTF(is_nolibc || is_glibc, "-9223372036854775808", "%Li", (~0ULL >> 1) + 1); break;
1970+ CASE_TEST(uintmax_max); EXPECT_VFPRINTF(1, "18446744073709551615", "%ju", ~0ULL); break;
1971+ CASE_TEST(truncation); EXPECT_VFPRINTF(1, "012345678901234567890123456789", "%s", "012345678901234567890123456789"); break;
1972+ CASE_TEST(string_width); EXPECT_VFPRINTF(1, " 1", "%10s", "1"); break;
1973+ CASE_TEST(string_trunc); EXPECT_VFPRINTF(1, " 12345", "%10.5s", "1234567890"); break;
1974+ CASE_TEST(string_var); EXPECT_VFPRINTF(1, "| ab|ef | ij|kl |", "|%*.*s|%*.*s|%*.*s|%*.*s|", 3, 2, "abcd", -3, 2, "efgh", 3, -1, "ij", -3, -1, "kl"); break;
1975+ CASE_TEST(number_width); EXPECT_VFPRINTF(1, " 1", "%10d", 1); break;
1976+ CASE_TEST(number_left); EXPECT_VFPRINTF(1, "|-5 |", "|%-8d|", -5); break;
1977+ CASE_TEST(string_align); EXPECT_VFPRINTF(1, "|foo |", "|%-8s|", "foo"); break;
1978+ CASE_TEST(width_trunc); EXPECT_VFPRINTF(1, " 1", "%30d", 1); break;
1979+ CASE_TEST(width_tr_lft); EXPECT_VFPRINTF(1, "1 ", "%-30d", 1); break;
1980+ CASE_TEST(number_pad); EXPECT_VFPRINTF(1, "0000000005", "%010d", 5); break;
1981+ CASE_TEST(number_pad); EXPECT_VFPRINTF(1, "|0000000005|0x1234|", "|%010d|%#01x|", 5, 0x1234); break;
1982+ CASE_TEST(num_pad_neg); EXPECT_VFPRINTF(1, "-000000005", "%010d", -5); break;
1983+ CASE_TEST(num_pad_hex); EXPECT_VFPRINTF(1, "00fffffffb", "%010x", -5); break;
1984+ CASE_TEST(num_pad_trunc);EXPECT_VFPRINTF(is_nolibc, " 0000000000000000000000000000005", "%035d", 5); break; /* max 31 '0' can be added */
1985+ CASE_TEST(num_p_tr_libc);EXPECT_VFPRINTF(!is_nolibc, "00000000000000000000000000000000005", "%035d", 5); break;
1986+ CASE_TEST(number_prec); EXPECT_VFPRINTF(1, " 00005", "%10.5d", 5); break;
1987+ CASE_TEST(num_prec_neg); EXPECT_VFPRINTF(1, " -00005", "%10.5d", -5); break;
1988+ CASE_TEST(number_var); EXPECT_VFPRINTF(1, "| -00005|5 |", "|%*.*d|%*.*d|", 10, 5, -5, -2, -10, 5); break;
1989+ CASE_TEST(num_0_prec_0); EXPECT_VFPRINTF(1, "|| |+||||", "|%.0d|% .0d|%+.0d|%.0u|%.0x|%#.0x|", 0, 0, 0, 0, 0, 0); break;
1990+ CASE_TEST(errno); errno = 22; EXPECT_VFPRINTF(is_nolibc, "errno=22", "%m"); break;
1991+ CASE_TEST(errno-neg); errno = -22; EXPECT_VFPRINTF(is_nolibc, "errno=-22 ", "%-12m"); break;
18111992 CASE_TEST(scanf); EXPECT_ZR(1, test_scanf()); break;
1812− CASE_TEST(strerror); EXPECT_ZR(1, test_strerror()); break;
18131993 CASE_TEST(printf_error); EXPECT_ZR(1, test_printf_error()); break;
1994+ CASE_TEST(asprintf); EXPECT_ZR(1, test_asprintf()); break;
18141995 case __LINE__:
18151996 return ret; /* must be last */
18161997 /* note: do not set any defaults so as to permit holes above */
tools/testing/selftests/nolibc/run-tests.sh+1−1
@@ -7,7 +7,7 @@ set -e · 6 unmodified lines
77
88 trap 'echo Aborting...' 'ERR'
99
10−crosstool_version=13.2.0
10+crosstool_version=15.2.0
1111 hostarch=x86_64
1212 nproc=$(( $(nproc) + 2))
1313 cache_dir="${XDG_CACHE_HOME:-"$HOME"/.cache}"

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t