Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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
...

+1469 -773
+5 -9
tools/include/nolibc/Makefile
··· 17 17 # it defaults to this nolibc directory. 18 18 OUTPUT ?= $(CURDIR)/ 19 19 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))) 27 22 all_files := \ 23 + byteswap.h \ 28 24 compiler.h \ 29 25 crt.h \ 30 26 ctype.h \ 31 27 dirent.h \ 32 28 elf.h \ 29 + endian.h \ 30 + err.h \ 33 31 errno.h \ 34 32 fcntl.h \ 35 33 getopt.h \ ··· 94 96 95 97 # installs headers for all archs at once. 96 98 headers: 97 - $(Q)mkdir -p "$(OUTPUT)sysroot" 98 99 $(Q)mkdir -p "$(OUTPUT)sysroot/include" 99 100 $(Q)cp --parents $(arch_files) $(all_files) "$(OUTPUT)sysroot/include/" 100 101 101 102 headers_standalone: headers 102 - $(Q)$(MAKE) -C $(srctree) headers 103 103 $(Q)$(MAKE) -C $(srctree) headers_install INSTALL_HDR_PATH=$(OUTPUT)sysroot 104 104 105 105 CFLAGS_s390 := -m64
+7 -7
tools/include/nolibc/arch-arm.h
··· 50 50 51 51 #endif /* end THUMB */ 52 52 53 - #define my_syscall0(num) \ 53 + #define __nolibc_syscall0(num) \ 54 54 ({ \ 55 55 register long _num __asm__(_NOLIBC_SYSCALL_REG) = (num); \ 56 56 register long _arg1 __asm__ ("r0"); \ ··· 67 67 _arg1; \ 68 68 }) 69 69 70 - #define my_syscall1(num, arg1) \ 70 + #define __nolibc_syscall1(num, arg1) \ 71 71 ({ \ 72 72 register long _num __asm__(_NOLIBC_SYSCALL_REG) = (num); \ 73 73 register long _arg1 __asm__ ("r0") = (long)(arg1); \ ··· 84 84 _arg1; \ 85 85 }) 86 86 87 - #define my_syscall2(num, arg1, arg2) \ 87 + #define __nolibc_syscall2(num, arg1, arg2) \ 88 88 ({ \ 89 89 register long _num __asm__(_NOLIBC_SYSCALL_REG) = (num); \ 90 90 register long _arg1 __asm__ ("r0") = (long)(arg1); \ ··· 102 102 _arg1; \ 103 103 }) 104 104 105 - #define my_syscall3(num, arg1, arg2, arg3) \ 105 + #define __nolibc_syscall3(num, arg1, arg2, arg3) \ 106 106 ({ \ 107 107 register long _num __asm__(_NOLIBC_SYSCALL_REG) = (num); \ 108 108 register long _arg1 __asm__ ("r0") = (long)(arg1); \ ··· 121 121 _arg1; \ 122 122 }) 123 123 124 - #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 124 + #define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \ 125 125 ({ \ 126 126 register long _num __asm__(_NOLIBC_SYSCALL_REG) = (num); \ 127 127 register long _arg1 __asm__ ("r0") = (long)(arg1); \ ··· 141 141 _arg1; \ 142 142 }) 143 143 144 - #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 144 + #define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 145 145 ({ \ 146 146 register long _num __asm__(_NOLIBC_SYSCALL_REG) = (num); \ 147 147 register long _arg1 __asm__ ("r0") = (long)(arg1); \ ··· 162 162 _arg1; \ 163 163 }) 164 164 165 - #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 165 + #define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 166 166 ({ \ 167 167 register long _num __asm__(_NOLIBC_SYSCALL_REG) = (num); \ 168 168 register long _arg1 __asm__ ("r0") = (long)(arg1); \
+7 -7
tools/include/nolibc/arch-arm64.h
··· 22 22 * don't have to experience issues with register constraints. 23 23 */ 24 24 25 - #define my_syscall0(num) \ 25 + #define __nolibc_syscall0(num) \ 26 26 ({ \ 27 27 register long _num __asm__ ("x8") = (num); \ 28 28 register long _arg1 __asm__ ("x0"); \ ··· 36 36 _arg1; \ 37 37 }) 38 38 39 - #define my_syscall1(num, arg1) \ 39 + #define __nolibc_syscall1(num, arg1) \ 40 40 ({ \ 41 41 register long _num __asm__ ("x8") = (num); \ 42 42 register long _arg1 __asm__ ("x0") = (long)(arg1); \ ··· 51 51 _arg1; \ 52 52 }) 53 53 54 - #define my_syscall2(num, arg1, arg2) \ 54 + #define __nolibc_syscall2(num, arg1, arg2) \ 55 55 ({ \ 56 56 register long _num __asm__ ("x8") = (num); \ 57 57 register long _arg1 __asm__ ("x0") = (long)(arg1); \ ··· 67 67 _arg1; \ 68 68 }) 69 69 70 - #define my_syscall3(num, arg1, arg2, arg3) \ 70 + #define __nolibc_syscall3(num, arg1, arg2, arg3) \ 71 71 ({ \ 72 72 register long _num __asm__ ("x8") = (num); \ 73 73 register long _arg1 __asm__ ("x0") = (long)(arg1); \ ··· 84 84 _arg1; \ 85 85 }) 86 86 87 - #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 87 + #define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \ 88 88 ({ \ 89 89 register long _num __asm__ ("x8") = (num); \ 90 90 register long _arg1 __asm__ ("x0") = (long)(arg1); \ ··· 102 102 _arg1; \ 103 103 }) 104 104 105 - #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 105 + #define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 106 106 ({ \ 107 107 register long _num __asm__ ("x8") = (num); \ 108 108 register long _arg1 __asm__ ("x0") = (long)(arg1); \ ··· 121 121 _arg1; \ 122 122 }) 123 123 124 - #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 124 + #define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 125 125 ({ \ 126 126 register long _num __asm__ ("x8") = (num); \ 127 127 register long _arg1 __asm__ ("x0") = (long)(arg1); \
+7 -7
tools/include/nolibc/arch-loongarch.h
··· 24 24 #define _NOLIBC_SYSCALL_CLOBBERLIST \ 25 25 "memory", "$t0", "$t1", "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t8" 26 26 27 - #define my_syscall0(num) \ 27 + #define __nolibc_syscall0(num) \ 28 28 ({ \ 29 29 register long _num __asm__ ("a7") = (num); \ 30 30 register long _arg1 __asm__ ("a0"); \ ··· 38 38 _arg1; \ 39 39 }) 40 40 41 - #define my_syscall1(num, arg1) \ 41 + #define __nolibc_syscall1(num, arg1) \ 42 42 ({ \ 43 43 register long _num __asm__ ("a7") = (num); \ 44 44 register long _arg1 __asm__ ("a0") = (long)(arg1); \ ··· 52 52 _arg1; \ 53 53 }) 54 54 55 - #define my_syscall2(num, arg1, arg2) \ 55 + #define __nolibc_syscall2(num, arg1, arg2) \ 56 56 ({ \ 57 57 register long _num __asm__ ("a7") = (num); \ 58 58 register long _arg1 __asm__ ("a0") = (long)(arg1); \ ··· 68 68 _arg1; \ 69 69 }) 70 70 71 - #define my_syscall3(num, arg1, arg2, arg3) \ 71 + #define __nolibc_syscall3(num, arg1, arg2, arg3) \ 72 72 ({ \ 73 73 register long _num __asm__ ("a7") = (num); \ 74 74 register long _arg1 __asm__ ("a0") = (long)(arg1); \ ··· 85 85 _arg1; \ 86 86 }) 87 87 88 - #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 88 + #define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \ 89 89 ({ \ 90 90 register long _num __asm__ ("a7") = (num); \ 91 91 register long _arg1 __asm__ ("a0") = (long)(arg1); \ ··· 103 103 _arg1; \ 104 104 }) 105 105 106 - #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 106 + #define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 107 107 ({ \ 108 108 register long _num __asm__ ("a7") = (num); \ 109 109 register long _arg1 __asm__ ("a0") = (long)(arg1); \ ··· 122 122 _arg1; \ 123 123 }) 124 124 125 - #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 125 + #define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 126 126 ({ \ 127 127 register long _num __asm__ ("a7") = (num); \ 128 128 register long _arg1 __asm__ ("a0") = (long)(arg1); \
+7 -7
tools/include/nolibc/arch-m68k.h
··· 15 15 16 16 #define _NOLIBC_SYSCALL_CLOBBERLIST "memory" 17 17 18 - #define my_syscall0(num) \ 18 + #define __nolibc_syscall0(num) \ 19 19 ({ \ 20 20 register long _num __asm__ ("d0") = (num); \ 21 21 \ ··· 28 28 _num; \ 29 29 }) 30 30 31 - #define my_syscall1(num, arg1) \ 31 + #define __nolibc_syscall1(num, arg1) \ 32 32 ({ \ 33 33 register long _num __asm__ ("d0") = (num); \ 34 34 register long _arg1 __asm__ ("d1") = (long)(arg1); \ ··· 42 42 _num; \ 43 43 }) 44 44 45 - #define my_syscall2(num, arg1, arg2) \ 45 + #define __nolibc_syscall2(num, arg1, arg2) \ 46 46 ({ \ 47 47 register long _num __asm__ ("d0") = (num); \ 48 48 register long _arg1 __asm__ ("d1") = (long)(arg1); \ ··· 57 57 _num; \ 58 58 }) 59 59 60 - #define my_syscall3(num, arg1, arg2, arg3) \ 60 + #define __nolibc_syscall3(num, arg1, arg2, arg3) \ 61 61 ({ \ 62 62 register long _num __asm__ ("d0") = (num); \ 63 63 register long _arg1 __asm__ ("d1") = (long)(arg1); \ ··· 73 73 _num; \ 74 74 }) 75 75 76 - #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 76 + #define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \ 77 77 ({ \ 78 78 register long _num __asm__ ("d0") = (num); \ 79 79 register long _arg1 __asm__ ("d1") = (long)(arg1); \ ··· 90 90 _num; \ 91 91 }) 92 92 93 - #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 93 + #define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 94 94 ({ \ 95 95 register long _num __asm__ ("d0") = (num); \ 96 96 register long _arg1 __asm__ ("d1") = (long)(arg1); \ ··· 108 108 _num; \ 109 109 }) 110 110 111 - #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 111 + #define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 112 112 ({ \ 113 113 register long _num __asm__ ("d0") = (num); \ 114 114 register long _arg1 __asm__ ("d1") = (long)(arg1); \
+21 -12
tools/include/nolibc/arch-mips.h
··· 39 39 * - stack is 16-byte aligned 40 40 */ 41 41 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 + 42 48 #if defined(_ABIO32) 43 49 44 50 #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 + 47 55 #define _NOLIBC_SYSCALL_STACK_RESERVE "addiu $sp, $sp, -32\n" 48 56 #define _NOLIBC_SYSCALL_STACK_UNRESERVE "addiu $sp, $sp, 32\n" 49 57 ··· 60 52 /* binutils, GCC and clang disagree about register aliases, use numbers instead. */ 61 53 #define _NOLIBC_SYSCALL_CLOBBERLIST \ 62 54 "memory", "cc", "at", "v1", \ 63 - "10", "11", "12", "13", "14", "15", "24", "25" 55 + "10", "11", "12", "13", "14", "15", "24", "25", \ 56 + _NOLIBC_SYSCALL_CLOBBER_HI_LO 64 57 65 58 #define _NOLIBC_SYSCALL_STACK_RESERVE 66 59 #define _NOLIBC_SYSCALL_STACK_UNRESERVE 67 60 68 61 #endif /* _ABIO32 */ 69 62 70 - #define my_syscall0(num) \ 63 + #define __nolibc_syscall0(num) \ 71 64 ({ \ 72 65 register long _num __asm__ ("v0") = (num); \ 73 66 register long _arg4 __asm__ ("a3"); \ ··· 84 75 _arg4 ? -_num : _num; \ 85 76 }) 86 77 87 - #define my_syscall1(num, arg1) \ 78 + #define __nolibc_syscall1(num, arg1) \ 88 79 ({ \ 89 80 register long _num __asm__ ("v0") = (num); \ 90 81 register long _arg1 __asm__ ("a0") = (long)(arg1); \ ··· 102 93 _arg4 ? -_num : _num; \ 103 94 }) 104 95 105 - #define my_syscall2(num, arg1, arg2) \ 96 + #define __nolibc_syscall2(num, arg1, arg2) \ 106 97 ({ \ 107 98 register long _num __asm__ ("v0") = (num); \ 108 99 register long _arg1 __asm__ ("a0") = (long)(arg1); \ ··· 121 112 _arg4 ? -_num : _num; \ 122 113 }) 123 114 124 - #define my_syscall3(num, arg1, arg2, arg3) \ 115 + #define __nolibc_syscall3(num, arg1, arg2, arg3) \ 125 116 ({ \ 126 117 register long _num __asm__ ("v0") = (num); \ 127 118 register long _arg1 __asm__ ("a0") = (long)(arg1); \ ··· 141 132 _arg4 ? -_num : _num; \ 142 133 }) 143 134 144 - #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 135 + #define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \ 145 136 ({ \ 146 137 register long _num __asm__ ("v0") = (num); \ 147 138 register long _arg1 __asm__ ("a0") = (long)(arg1); \ ··· 163 154 164 155 #if defined(_ABIO32) 165 156 166 - #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 157 + #define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 167 158 ({ \ 168 159 register long _num __asm__ ("v0") = (num); \ 169 160 register long _arg1 __asm__ ("a0") = (long)(arg1); \ ··· 185 176 _arg4 ? -_num : _num; \ 186 177 }) 187 178 188 - #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 179 + #define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 189 180 ({ \ 190 181 register long _num __asm__ ("v0") = (num); \ 191 182 register long _arg1 __asm__ ("a0") = (long)(arg1); \ ··· 212 203 213 204 #else /* _ABIN32 || _ABI64 */ 214 205 215 - #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 206 + #define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 216 207 ({ \ 217 208 register long _num __asm__ ("v0") = (num); \ 218 209 register long _arg1 __asm__ ("$4") = (long)(arg1); \ ··· 231 222 _arg4 ? -_num : _num; \ 232 223 }) 233 224 234 - #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 225 + #define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 235 226 ({ \ 236 227 register long _num __asm__ ("v0") = (num); \ 237 228 register long _arg1 __asm__ ("$4") = (long)(arg1); \
+7 -7
tools/include/nolibc/arch-powerpc.h
··· 25 25 #define _NOLIBC_SYSCALL_CLOBBERLIST \ 26 26 "memory", "cr0", "r12", "r11", "r10", "r9" 27 27 28 - #define my_syscall0(num) \ 28 + #define __nolibc_syscall0(num) \ 29 29 ({ \ 30 30 register long _ret __asm__ ("r3"); \ 31 31 register long _num __asm__ ("r0") = (num); \ ··· 42 42 _ret; \ 43 43 }) 44 44 45 - #define my_syscall1(num, arg1) \ 45 + #define __nolibc_syscall1(num, arg1) \ 46 46 ({ \ 47 47 register long _ret __asm__ ("r3"); \ 48 48 register long _num __asm__ ("r0") = (num); \ ··· 61 61 }) 62 62 63 63 64 - #define my_syscall2(num, arg1, arg2) \ 64 + #define __nolibc_syscall2(num, arg1, arg2) \ 65 65 ({ \ 66 66 register long _ret __asm__ ("r3"); \ 67 67 register long _num __asm__ ("r0") = (num); \ ··· 81 81 }) 82 82 83 83 84 - #define my_syscall3(num, arg1, arg2, arg3) \ 84 + #define __nolibc_syscall3(num, arg1, arg2, arg3) \ 85 85 ({ \ 86 86 register long _ret __asm__ ("r3"); \ 87 87 register long _num __asm__ ("r0") = (num); \ ··· 102 102 }) 103 103 104 104 105 - #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 105 + #define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \ 106 106 ({ \ 107 107 register long _ret __asm__ ("r3"); \ 108 108 register long _num __asm__ ("r0") = (num); \ ··· 125 125 }) 126 126 127 127 128 - #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 128 + #define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 129 129 ({ \ 130 130 register long _ret __asm__ ("r3"); \ 131 131 register long _num __asm__ ("r0") = (num); \ ··· 148 148 _ret; \ 149 149 }) 150 150 151 - #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 151 + #define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 152 152 ({ \ 153 153 register long _ret __asm__ ("r3"); \ 154 154 register long _num __asm__ ("r0") = (num); \
+7 -7
tools/include/nolibc/arch-riscv.h
··· 21 21 * so that we don't have to experience issues with register constraints. 22 22 */ 23 23 24 - #define my_syscall0(num) \ 24 + #define __nolibc_syscall0(num) \ 25 25 ({ \ 26 26 register long _num __asm__ ("a7") = (num); \ 27 27 register long _arg1 __asm__ ("a0"); \ ··· 35 35 _arg1; \ 36 36 }) 37 37 38 - #define my_syscall1(num, arg1) \ 38 + #define __nolibc_syscall1(num, arg1) \ 39 39 ({ \ 40 40 register long _num __asm__ ("a7") = (num); \ 41 41 register long _arg1 __asm__ ("a0") = (long)(arg1); \ ··· 49 49 _arg1; \ 50 50 }) 51 51 52 - #define my_syscall2(num, arg1, arg2) \ 52 + #define __nolibc_syscall2(num, arg1, arg2) \ 53 53 ({ \ 54 54 register long _num __asm__ ("a7") = (num); \ 55 55 register long _arg1 __asm__ ("a0") = (long)(arg1); \ ··· 65 65 _arg1; \ 66 66 }) 67 67 68 - #define my_syscall3(num, arg1, arg2, arg3) \ 68 + #define __nolibc_syscall3(num, arg1, arg2, arg3) \ 69 69 ({ \ 70 70 register long _num __asm__ ("a7") = (num); \ 71 71 register long _arg1 __asm__ ("a0") = (long)(arg1); \ ··· 82 82 _arg1; \ 83 83 }) 84 84 85 - #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 85 + #define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \ 86 86 ({ \ 87 87 register long _num __asm__ ("a7") = (num); \ 88 88 register long _arg1 __asm__ ("a0") = (long)(arg1); \ ··· 100 100 _arg1; \ 101 101 }) 102 102 103 - #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 103 + #define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 104 104 ({ \ 105 105 register long _num __asm__ ("a7") = (num); \ 106 106 register long _arg1 __asm__ ("a0") = (long)(arg1); \ ··· 119 119 _arg1; \ 120 120 }) 121 121 122 - #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 122 + #define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 123 123 ({ \ 124 124 register long _num __asm__ ("a7") = (num); \ 125 125 register long _arg1 __asm__ ("a0") = (long)(arg1); \
+17 -17
tools/include/nolibc/arch-s390.h
··· 28 28 * 29 29 */ 30 30 31 - #define my_syscall0(num) \ 31 + #define __nolibc_syscall0(num) \ 32 32 ({ \ 33 33 register long _num __asm__ ("1") = (num); \ 34 34 register long _rc __asm__ ("2"); \ ··· 42 42 _rc; \ 43 43 }) 44 44 45 - #define my_syscall1(num, arg1) \ 45 + #define __nolibc_syscall1(num, arg1) \ 46 46 ({ \ 47 47 register long _num __asm__ ("1") = (num); \ 48 48 register long _arg1 __asm__ ("2") = (long)(arg1); \ ··· 56 56 _arg1; \ 57 57 }) 58 58 59 - #define my_syscall2(num, arg1, arg2) \ 59 + #define __nolibc_syscall2(num, arg1, arg2) \ 60 60 ({ \ 61 61 register long _num __asm__ ("1") = (num); \ 62 62 register long _arg1 __asm__ ("2") = (long)(arg1); \ ··· 71 71 _arg1; \ 72 72 }) 73 73 74 - #define my_syscall3(num, arg1, arg2, arg3) \ 74 + #define __nolibc_syscall3(num, arg1, arg2, arg3) \ 75 75 ({ \ 76 76 register long _num __asm__ ("1") = (num); \ 77 77 register long _arg1 __asm__ ("2") = (long)(arg1); \ ··· 87 87 _arg1; \ 88 88 }) 89 89 90 - #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 90 + #define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \ 91 91 ({ \ 92 92 register long _num __asm__ ("1") = (num); \ 93 93 register long _arg1 __asm__ ("2") = (long)(arg1); \ ··· 104 104 _arg1; \ 105 105 }) 106 106 107 - #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 107 + #define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 108 108 ({ \ 109 109 register long _num __asm__ ("1") = (num); \ 110 110 register long _arg1 __asm__ ("2") = (long)(arg1); \ ··· 123 123 _arg1; \ 124 124 }) 125 125 126 - #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 126 + #define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 127 127 ({ \ 128 128 register long _num __asm__ ("1") = (num); \ 129 129 register long _arg1 __asm__ ("2") = (long)(arg1); \ ··· 167 167 }; 168 168 169 169 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) 172 172 { 173 173 struct s390_mmap_arg_struct args = { 174 174 .addr = (unsigned long)addr, ··· 179 179 .offset = (unsigned long)offset 180 180 }; 181 181 182 - return (void *)my_syscall1(__NR_mmap, &args); 182 + return (void *)__nolibc_syscall1(__NR_mmap, &args); 183 183 } 184 - #define sys_mmap sys_mmap 184 + #define _sys_mmap _sys_mmap 185 185 186 186 static __attribute__((unused)) 187 - pid_t sys_fork(void) 187 + pid_t _sys_fork(void) 188 188 { 189 - return my_syscall5(__NR_clone, 0, SIGCHLD, 0, 0, 0); 189 + return __nolibc_syscall5(__NR_clone, 0, SIGCHLD, 0, 0, 0); 190 190 } 191 - #define sys_fork sys_fork 191 + #define _sys_fork _sys_fork 192 192 193 193 static __attribute__((unused)) 194 - pid_t sys_vfork(void) 194 + pid_t _sys_vfork(void) 195 195 { 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); 197 197 } 198 - #define sys_vfork sys_vfork 198 + #define _sys_vfork _sys_vfork 199 199 200 200 #endif /* _NOLIBC_ARCH_S390_H */
+7 -7
tools/include/nolibc/arch-sh.h
··· 19 19 * - syscall return value is in r0 20 20 */ 21 21 22 - #define my_syscall0(num) \ 22 + #define __nolibc_syscall0(num) \ 23 23 ({ \ 24 24 register long _num __asm__ ("r3") = (num); \ 25 25 register long _ret __asm__ ("r0"); \ ··· 33 33 _ret; \ 34 34 }) 35 35 36 - #define my_syscall1(num, arg1) \ 36 + #define __nolibc_syscall1(num, arg1) \ 37 37 ({ \ 38 38 register long _num __asm__ ("r3") = (num); \ 39 39 register long _ret __asm__ ("r0"); \ ··· 48 48 _ret; \ 49 49 }) 50 50 51 - #define my_syscall2(num, arg1, arg2) \ 51 + #define __nolibc_syscall2(num, arg1, arg2) \ 52 52 ({ \ 53 53 register long _num __asm__ ("r3") = (num); \ 54 54 register long _ret __asm__ ("r0"); \ ··· 64 64 _ret; \ 65 65 }) 66 66 67 - #define my_syscall3(num, arg1, arg2, arg3) \ 67 + #define __nolibc_syscall3(num, arg1, arg2, arg3) \ 68 68 ({ \ 69 69 register long _num __asm__ ("r3") = (num); \ 70 70 register long _ret __asm__ ("r0"); \ ··· 81 81 _ret; \ 82 82 }) 83 83 84 - #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 84 + #define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \ 85 85 ({ \ 86 86 register long _num __asm__ ("r3") = (num); \ 87 87 register long _ret __asm__ ("r0"); \ ··· 99 99 _ret; \ 100 100 }) 101 101 102 - #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 102 + #define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 103 103 ({ \ 104 104 register long _num __asm__ ("r3") = (num); \ 105 105 register long _ret __asm__ ("r0"); \ ··· 119 119 _ret; \ 120 120 }) 121 121 122 - #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 122 + #define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 123 123 ({ \ 124 124 register long _num __asm__ ("r3") = (num); \ 125 125 register long _ret __asm__ ("r0"); \
+13 -13
tools/include/nolibc/arch-sparc.h
··· 38 38 39 39 #endif /* __arch64__ */ 40 40 41 - #define my_syscall0(num) \ 41 + #define __nolibc_syscall0(num) \ 42 42 ({ \ 43 43 register long _num __asm__ ("g1") = (num); \ 44 44 register long _arg1 __asm__ ("o0"); \ ··· 52 52 _arg1; \ 53 53 }) 54 54 55 - #define my_syscall1(num, arg1) \ 55 + #define __nolibc_syscall1(num, arg1) \ 56 56 ({ \ 57 57 register long _num __asm__ ("g1") = (num); \ 58 58 register long _arg1 __asm__ ("o0") = (long)(arg1); \ ··· 66 66 _arg1; \ 67 67 }) 68 68 69 - #define my_syscall2(num, arg1, arg2) \ 69 + #define __nolibc_syscall2(num, arg1, arg2) \ 70 70 ({ \ 71 71 register long _num __asm__ ("g1") = (num); \ 72 72 register long _arg1 __asm__ ("o0") = (long)(arg1); \ ··· 81 81 _arg1; \ 82 82 }) 83 83 84 - #define my_syscall3(num, arg1, arg2, arg3) \ 84 + #define __nolibc_syscall3(num, arg1, arg2, arg3) \ 85 85 ({ \ 86 86 register long _num __asm__ ("g1") = (num); \ 87 87 register long _arg1 __asm__ ("o0") = (long)(arg1); \ ··· 97 97 _arg1; \ 98 98 }) 99 99 100 - #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 100 + #define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \ 101 101 ({ \ 102 102 register long _num __asm__ ("g1") = (num); \ 103 103 register long _arg1 __asm__ ("o0") = (long)(arg1); \ ··· 114 114 _arg1; \ 115 115 }) 116 116 117 - #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 117 + #define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 118 118 ({ \ 119 119 register long _num __asm__ ("g1") = (num); \ 120 120 register long _arg1 __asm__ ("o0") = (long)(arg1); \ ··· 132 132 _arg1; \ 133 133 }) 134 134 135 - #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 135 + #define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 136 136 ({ \ 137 137 register long _num __asm__ ("g1") = (num); \ 138 138 register long _arg1 __asm__ ("o0") = (long)(arg1); \ ··· 175 175 static pid_t getpid(void); 176 176 177 177 static __attribute__((unused)) 178 - pid_t sys_fork(void) 178 + pid_t _sys_fork(void) 179 179 { 180 180 pid_t parent, ret; 181 181 182 182 parent = getpid(); 183 - ret = my_syscall0(__NR_fork); 183 + ret = __nolibc_syscall0(__NR_fork); 184 184 185 185 /* The syscall returns the parent pid in the child instead of 0 */ 186 186 if (ret == parent) ··· 188 188 else 189 189 return ret; 190 190 } 191 - #define sys_fork sys_fork 191 + #define _sys_fork _sys_fork 192 192 193 193 static __attribute__((unused)) 194 - pid_t sys_vfork(void) 194 + pid_t _sys_vfork(void) 195 195 { 196 196 pid_t parent, ret; 197 197 198 198 parent = getpid(); 199 - ret = my_syscall0(__NR_vfork); 199 + ret = __nolibc_syscall0(__NR_vfork); 200 200 201 201 /* The syscall returns the parent pid in the child instead of 0 */ 202 202 if (ret == parent) ··· 204 204 else 205 205 return ret; 206 206 } 207 - #define sys_vfork sys_vfork 207 + #define _sys_vfork _sys_vfork 208 208 209 209 #endif /* _NOLIBC_ARCH_SPARC_H */
+34 -34
tools/include/nolibc/arch-x86.h
··· 30 30 */ 31 31 #define __ARCH_WANT_SYS_OLD_SELECT 32 32 33 - #define my_syscall0(num) \ 33 + #define __nolibc_syscall0(num) \ 34 34 ({ \ 35 35 long _ret; \ 36 36 register long _num __asm__ ("eax") = (num); \ ··· 44 44 _ret; \ 45 45 }) 46 46 47 - #define my_syscall1(num, arg1) \ 47 + #define __nolibc_syscall1(num, arg1) \ 48 48 ({ \ 49 49 long _ret; \ 50 50 register long _num __asm__ ("eax") = (num); \ ··· 60 60 _ret; \ 61 61 }) 62 62 63 - #define my_syscall2(num, arg1, arg2) \ 63 + #define __nolibc_syscall2(num, arg1, arg2) \ 64 64 ({ \ 65 65 long _ret; \ 66 66 register long _num __asm__ ("eax") = (num); \ ··· 77 77 _ret; \ 78 78 }) 79 79 80 - #define my_syscall3(num, arg1, arg2, arg3) \ 80 + #define __nolibc_syscall3(num, arg1, arg2, arg3) \ 81 81 ({ \ 82 82 long _ret; \ 83 83 register long _num __asm__ ("eax") = (num); \ ··· 95 95 _ret; \ 96 96 }) 97 97 98 - #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 98 + #define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \ 99 99 ({ \ 100 100 long _ret; \ 101 101 register long _num __asm__ ("eax") = (num); \ ··· 114 114 _ret; \ 115 115 }) 116 116 117 - #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 117 + #define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 118 118 ({ \ 119 119 long _ret; \ 120 120 register long _num __asm__ ("eax") = (num); \ ··· 134 134 _ret; \ 135 135 }) 136 136 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; \ 158 158 }) 159 159 160 160 #ifndef NOLIBC_NO_RUNTIME ··· 200 200 * 201 201 */ 202 202 203 - #define my_syscall0(num) \ 203 + #define __nolibc_syscall0(num) \ 204 204 ({ \ 205 205 long _ret; \ 206 206 register long _num __asm__ ("rax") = (num); \ ··· 214 214 _ret; \ 215 215 }) 216 216 217 - #define my_syscall1(num, arg1) \ 217 + #define __nolibc_syscall1(num, arg1) \ 218 218 ({ \ 219 219 long _ret; \ 220 220 register long _num __asm__ ("rax") = (num); \ ··· 230 230 _ret; \ 231 231 }) 232 232 233 - #define my_syscall2(num, arg1, arg2) \ 233 + #define __nolibc_syscall2(num, arg1, arg2) \ 234 234 ({ \ 235 235 long _ret; \ 236 236 register long _num __asm__ ("rax") = (num); \ ··· 247 247 _ret; \ 248 248 }) 249 249 250 - #define my_syscall3(num, arg1, arg2, arg3) \ 250 + #define __nolibc_syscall3(num, arg1, arg2, arg3) \ 251 251 ({ \ 252 252 long _ret; \ 253 253 register long _num __asm__ ("rax") = (num); \ ··· 265 265 _ret; \ 266 266 }) 267 267 268 - #define my_syscall4(num, arg1, arg2, arg3, arg4) \ 268 + #define __nolibc_syscall4(num, arg1, arg2, arg3, arg4) \ 269 269 ({ \ 270 270 long _ret; \ 271 271 register long _num __asm__ ("rax") = (num); \ ··· 284 284 _ret; \ 285 285 }) 286 286 287 - #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 287 + #define __nolibc_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 288 288 ({ \ 289 289 long _ret; \ 290 290 register long _num __asm__ ("rax") = (num); \ ··· 304 304 _ret; \ 305 305 }) 306 306 307 - #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 307 + #define __nolibc_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 308 308 ({ \ 309 309 long _ret; \ 310 310 register long _num __asm__ ("rax") = (num); \
+21
tools/include/nolibc/byteswap.h
··· 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 */
+20 -1
tools/include/nolibc/compiler.h
··· 47 47 # define __nolibc_fallthrough do { } while (0) 48 48 #endif /* __nolibc_has_attribute(fallthrough) */ 49 49 50 + #if defined(__STDC_VERSION__) 51 + # define __nolibc_stdc_version __STDC_VERSION__ 52 + #else 53 + # define __nolibc_stdc_version 0 54 + #endif 55 + 50 56 #define __nolibc_version(_major, _minor, _patch) ((_major) * 10000 + (_minor) * 100 + (_patch)) 51 57 52 58 #ifdef __GNUC__ ··· 69 63 # define __nolibc_clang_version 0 70 64 #endif /* __clang__ */ 71 65 72 - #if __STDC_VERSION__ >= 201112L || \ 66 + #if __nolibc_stdc_version >= 201112L || \ 73 67 __nolibc_gnuc_version >= __nolibc_version(4, 6, 0) || \ 74 68 __nolibc_clang_version >= __nolibc_version(3, 0, 0) 75 69 # define __nolibc_static_assert(_t) _Static_assert(_t, "") 76 70 #else 77 71 # define __nolibc_static_assert(_t) 72 + #endif 73 + 74 + /* Make the optimizer believe the variable can be manipulated arbitrarily. */ 75 + #define _NOLIBC_OPTIMIZER_HIDE_VAR(var) __asm__ ("" : "+r" (var)) 76 + 77 + #if __nolibc_has_feature(undefined_behavior_sanitizer) 78 + # if defined(__clang__) 79 + # define __nolibc_no_sanitize_undefined __attribute__((no_sanitize("function"))) 80 + # else 81 + # define __nolibc_no_sanitize_undefined __attribute__((no_sanitize_undefined)) 82 + # endif 83 + #else 84 + # define __nolibc_no_sanitize_undefined 78 85 #endif 79 86 80 87 #endif /* _NOLIBC_COMPILER_H */
+27 -4
tools/include/nolibc/crt.h
··· 17 17 void _start(void); 18 18 static void __stack_chk_init(void); 19 19 static void exit(int); 20 + static char *strrchr(const char *s, int c); 20 21 21 22 extern void (*const __preinit_array_start[])(int, char **, char**) __attribute__((weak)); 22 23 extern void (*const __preinit_array_end[])(int, char **, char**) __attribute__((weak)); ··· 28 27 extern void (*const __fini_array_start[])(void) __attribute__((weak)); 29 28 extern void (*const __fini_array_end[])(void) __attribute__((weak)); 30 29 30 + #ifndef NOLIBC_IGNORE_ERRNO 31 + extern char *program_invocation_name __attribute__((weak)); 32 + extern char *program_invocation_short_name __attribute__((weak)); 33 + 34 + static __inline__ 35 + char *__nolibc_program_invocation_short_name(char *long_name) 36 + { 37 + 38 + char *short_name; 39 + 40 + short_name = strrchr(long_name, '/'); 41 + if (!short_name || !short_name[0]) 42 + return long_name; 43 + 44 + return short_name + 1; 45 + } 46 + #endif /* NOLIBC_IGNORE_ERRNO */ 47 + 31 48 void _start_c(long *sp); 32 - __attribute__((weak,used)) 33 - #if __nolibc_has_feature(undefined_behavior_sanitizer) 34 - __attribute__((no_sanitize("function"))) 35 - #endif 49 + __attribute__((weak,used)) __nolibc_no_sanitize_undefined 36 50 void _start_c(long *sp) 37 51 { 38 52 long argc; ··· 91 75 for (auxv = (void *)envp; *auxv++;) 92 76 ; 93 77 _auxv = auxv; 78 + 79 + #ifndef NOLIBC_IGNORE_ERRNO 80 + if (argc > 0 && argv[0]) { 81 + program_invocation_name = argv[0]; 82 + program_invocation_short_name = __nolibc_program_invocation_short_name(argv[0]); 83 + } 84 + #endif /* NOLIBC_IGNORE_ERRNO */ 94 85 95 86 for (ctor_func = __preinit_array_start; ctor_func < __preinit_array_end; ctor_func++) 96 87 (*ctor_func)(argc, argv, envp);
+2 -2
tools/include/nolibc/dirent.h
··· 73 73 74 74 fd = ~i; 75 75 76 - ret = sys_getdents64(fd, ldir, sizeof(buf)); 76 + ret = _sys_getdents64(fd, ldir, sizeof(buf)); 77 77 if (ret < 0) 78 78 return -ret; 79 79 if (ret == 0) { ··· 86 86 * readdir() can only return one entry at a time. 87 87 * Make sure the non-returned ones are not skipped. 88 88 */ 89 - ret = sys_lseek(fd, ldir->d_off, SEEK_SET); 89 + ret = _sys_lseek(fd, ldir->d_off, SEEK_SET); 90 90 if (ret < 0) 91 91 return -ret; 92 92
+32
tools/include/nolibc/endian.h
··· 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 */
+87
tools/include/nolibc/err.h
··· 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 */
+4
tools/include/nolibc/errno.h
··· 15 15 #ifndef NOLIBC_IGNORE_ERRNO 16 16 #define SET_ERRNO(v) do { errno = (v); } while (0) 17 17 int errno __attribute__((weak)); 18 + char *program_invocation_name __attribute__((weak)) = ""; 19 + char *program_invocation_short_name __attribute__((weak)) = ""; 18 20 #else 19 21 #define SET_ERRNO(v) do { } while (0) 22 + #define program_invocation_name "" 23 + #define program_invocation_short_name "" 20 24 #endif 21 25 22 26
+6 -6
tools/include/nolibc/fcntl.h
··· 19 19 */ 20 20 21 21 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) 23 23 { 24 - return my_syscall4(__NR_openat, dirfd, path, flags, mode); 24 + return __nolibc_syscall4(__NR_openat, dirfd, path, flags, mode); 25 25 } 26 26 27 27 static __attribute__((unused)) ··· 37 37 va_end(args); 38 38 } 39 39 40 - return __sysret(sys_openat(dirfd, path, flags, mode)); 40 + return __sysret(_sys_openat(dirfd, path, flags, mode)); 41 41 } 42 42 43 43 /* ··· 45 45 */ 46 46 47 47 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) 49 49 { 50 - return my_syscall4(__NR_openat, AT_FDCWD, path, flags, mode); 50 + return __nolibc_syscall4(__NR_openat, AT_FDCWD, path, flags, mode); 51 51 } 52 52 53 53 static __attribute__((unused)) ··· 63 63 va_end(args); 64 64 } 65 65 66 - return __sysret(sys_open(path, flags, mode)); 66 + return __sysret(_sys_open(path, flags, mode)); 67 67 } 68 68 69 69 #endif /* _NOLIBC_FCNTL_H */
+8 -5
tools/include/nolibc/nolibc.h
··· 12 12 * 13 13 * Syscalls are split into 3 levels: 14 14 * - 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 17 17 * are castto a long stored in a register. These expressions always return 18 18 * the syscall's return value as a signed long value which is often either 19 19 * a pointer or the negated errno value. 20 20 * 21 21 * - 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() 23 23 * depending on the syscall definition. These functions are responsible 24 24 * for exposing the appropriate types for the syscall arguments (int, 25 25 * pointers, etc) and for setting the appropriate return type (often int). 26 26 * A few of them are architecture-specific because the syscalls are not all 27 27 * 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() 29 29 * function will have to abstract this. 30 30 * 31 31 * - 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, 33 33 * takes care of specific input values, and of setting errno upon error. 34 34 * There can be minor variations compared to standard libc calls. 35 35 * ··· 130 130 #include "getopt.h" 131 131 #include "poll.h" 132 132 #include "math.h" 133 + #include "err.h" 134 + #include "byteswap.h" 135 + #include "endian.h" 133 136 134 137 /* Used by programs to avoid std includes */ 135 138 #define NOLIBC
+4 -4
tools/include/nolibc/poll.h
··· 21 21 */ 22 22 23 23 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) 25 25 { 26 26 #if defined(__NR_ppoll_time64) 27 27 struct __kernel_timespec t; ··· 30 30 t.tv_sec = timeout / 1000; 31 31 t.tv_nsec = (timeout % 1000) * 1000000; 32 32 } 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); 34 34 #else 35 35 struct __kernel_old_timespec t; 36 36 ··· 38 38 t.tv_sec = timeout / 1000; 39 39 t.tv_nsec = (timeout % 1000) * 1000000; 40 40 } 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); 42 42 #endif 43 43 } 44 44 45 45 static __attribute__((unused)) 46 46 int poll(struct pollfd *fds, int nfds, int timeout) 47 47 { 48 - return __sysret(sys_poll(fds, nfds, timeout)); 48 + return __sysret(_sys_poll(fds, nfds, timeout)); 49 49 } 50 50 51 51 #endif /* _NOLIBC_POLL_H */
+6 -6
tools/include/nolibc/sched.h
··· 19 19 */ 20 20 21 21 static __attribute__((unused)) 22 - int sys_setns(int fd, int nstype) 22 + int _sys_setns(int fd, int nstype) 23 23 { 24 - return my_syscall2(__NR_setns, fd, nstype); 24 + return __nolibc_syscall2(__NR_setns, fd, nstype); 25 25 } 26 26 27 27 static __attribute__((unused)) 28 28 int setns(int fd, int nstype) 29 29 { 30 - return __sysret(sys_setns(fd, nstype)); 30 + return __sysret(_sys_setns(fd, nstype)); 31 31 } 32 32 33 33 ··· 36 36 */ 37 37 38 38 static __attribute__((unused)) 39 - int sys_unshare(int flags) 39 + int _sys_unshare(int flags) 40 40 { 41 - return my_syscall1(__NR_unshare, flags); 41 + return __nolibc_syscall1(__NR_unshare, flags); 42 42 } 43 43 44 44 static __attribute__((unused)) 45 45 int unshare(int flags) 46 46 { 47 - return __sysret(sys_unshare(flags)); 47 + return __sysret(_sys_unshare(flags)); 48 48 } 49 49 50 50 #endif /* _NOLIBC_SCHED_H */
+1 -1
tools/include/nolibc/signal.h
··· 20 20 __attribute__((weak,unused,section(".text.nolibc_raise"))) 21 21 int raise(int signal) 22 22 { 23 - return sys_kill(sys_getpid(), signal); 23 + return _sys_kill(_sys_getpid(), signal); 24 24 } 25 25 26 26 #endif /* _NOLIBC_SIGNAL_H */
+4 -4
tools/include/nolibc/stackprotector.h
··· 24 24 void __stack_chk_fail(void) 25 25 { 26 26 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); 30 30 for (;;); 31 31 } 32 32 ··· 42 42 43 43 static __no_stack_protector void __stack_chk_init(void) 44 44 { 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); 46 46 /* a bit more randomness in case getrandom() fails, ensure the guard is never 0 */ 47 47 if (__stack_chk_guard != (uintptr_t) &__stack_chk_guard) 48 48 __stack_chk_guard ^= (uintptr_t) &__stack_chk_guard;
+1 -1
tools/include/nolibc/std.h
··· 19 19 #include <linux/types.h> 20 20 21 21 /* those are commonly provided by sys/types.h */ 22 - typedef unsigned int dev_t; 22 + typedef uint64_t dev_t; 23 23 typedef uint64_t ino_t; 24 24 typedef unsigned int mode_t; 25 25 typedef signed int pid_t;
+1 -1
tools/include/nolibc/stddef.h
··· 18 18 #endif 19 19 20 20 #ifndef offsetof 21 - #define offsetof(TYPE, FIELD) ((size_t) &((TYPE *)0)->FIELD) 21 + #define offsetof(TYPE, FIELD) __builtin_offsetof(TYPE, FIELD) 22 22 #endif 23 23 24 24 #endif /* _NOLIBC_STDDEF_H */
+451 -142
tools/include/nolibc/stdio.h
··· 291 291 } 292 292 293 293 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. 298 310 */ 299 - typedef int (*__nolibc_printf_cb)(intptr_t state, const char *buf, size_t size); 300 311 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) 303 340 { 304 - char escape, lpref, c; 341 + char ch; 305 342 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; 309 348 const char *outstr; 349 + unsigned int sign_prefix; 350 + int got_width; 310 351 311 - written = ofs = escape = lpref = 0; 352 + written = 0; 312 353 while (1) { 313 - c = fmt[ofs++]; 354 + outstr = fmt; 355 + ch = *fmt++; 356 + if (!ch) 357 + break; 358 + 314 359 width = 0; 315 - 316 - if (escape) { 317 - /* we're in an escape sequence, ofs == 1 */ 318 - escape = 0; 319 - 320 - /* width */ 321 - while (c >= '0' && c <= '9') { 322 - width *= 10; 323 - width += c - '0'; 324 - 325 - c = fmt[ofs++]; 326 - } 327 - 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; 347 - } 348 - 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; 367 - } 368 - outstr = tmpbuf; 369 - } 370 - else if (c == 's') { 371 - outstr = va_arg(args, char *); 372 - if (!outstr) 373 - outstr="(null)"; 374 - } 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 */ 381 - } 382 - else if (c == '%') { 383 - /* queue it verbatim */ 384 - continue; 385 - } 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; 393 - } 394 - escape = 1; 395 - goto do_escape; 396 - } 397 - len = strlen(outstr); 398 - goto flush_str; 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; 399 367 } 400 368 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 - } 369 + /* we're in a format sequence */ 420 370 421 - written += len; 422 - do_escape: 423 - if (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) 424 376 break; 425 - fmt += ofs; 426 - ofs = 0; 427 - continue; 377 + flags |= ch_flag; 428 378 } 429 379 430 - /* literal char, just queue it */ 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; 458 + } 459 + 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; 468 + } 469 + goto do_strlen_output; 470 + } 471 + 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 = ' '; 487 + } 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'; 496 + } 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); 539 + } 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 + } 559 + } 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'; 568 + } 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; 584 + } 585 + } 586 + outstr = out; 587 + goto do_output; 588 + } 589 + 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 + } 598 + 599 + if (ch != '%') { 600 + /* Invalid format: back up to output the format characters */ 601 + fmt = outstr + 1; 602 + /* and output a '%' now. */ 603 + } 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; 617 + 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; 431 639 } 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 + 432 647 return written; 433 648 } 434 649 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) 436 651 { 437 - return _fwrite(buf, size, (FILE *)state); 652 + return _fwrite(buf, size, stream); 438 653 } 439 654 440 655 static __attribute__((unused, format(printf, 2, 0))) 441 656 int vfprintf(FILE *stream, const char *fmt, va_list args) 442 657 { 443 - return __nolibc_printf(__nolibc_fprintf_cb, (intptr_t)stream, SIZE_MAX, fmt, args); 658 + return __nolibc_printf(__nolibc_fprintf_cb, stream, fmt, args); 444 659 } 445 660 446 661 static __attribute__((unused, format(printf, 1, 0))) ··· 713 498 return ret; 714 499 } 715 500 716 - static int __nolibc_sprintf_cb(intptr_t _state, const char *buf, size_t size) 717 - { 718 - char **state = (char **)_state; 501 + struct __nolibc_sprintf_cb_state { 502 + char *buf; 503 + size_t space; 504 + }; 719 505 720 - memcpy(*state, buf, size); 721 - *state += size; 506 + static int __nolibc_sprintf_cb(void *v_state, const char *buf, size_t size) 507 + { 508 + struct __nolibc_sprintf_cb_state *state = v_state; 509 + size_t space = state->space; 510 + char *tgt; 511 + 512 + /* Truncate the request to fit in the output buffer space. 513 + * The last byte is reserved for the terminating '\0'. 514 + * state->space can only be zero for snprintf(NULL, 0, fmt, args) 515 + * so this normally lets through calls with 'size == 0'. 516 + */ 517 + if (size >= space) { 518 + if (space <= 1) 519 + return 0; 520 + size = space - 1; 521 + } 522 + tgt = state->buf; 523 + 524 + /* __nolibc_printf() ends with cb(state, NULL, 0) to request the output 525 + * buffer be '\0' terminated. 526 + * That will be the only cb() call for, eg, snprintf(buf, sz, ""). 527 + * Zero lengths can occur at other times (eg "%s" for an empty string). 528 + * Unconditionally write the '\0' byte to reduce code size, it is 529 + * normally overwritten by the data being output. 530 + * There is no point adding a '\0' after copied data - there is always 531 + * another call. 532 + */ 533 + *tgt = '\0'; 534 + if (size) { 535 + state->space = space - size; 536 + state->buf = tgt + size; 537 + memcpy(tgt, buf, size); 538 + } 539 + 722 540 return 0; 723 541 } 724 542 725 543 static __attribute__((unused, format(printf, 3, 0))) 726 544 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 727 545 { 728 - char *state = buf; 729 - int ret; 546 + struct __nolibc_sprintf_cb_state state = { .buf = buf, .space = size }; 730 547 731 - ret = __nolibc_printf(__nolibc_sprintf_cb, (intptr_t)&state, size, fmt, args); 732 - if (ret < 0) 733 - return ret; 734 - buf[(size_t)ret < size ? (size_t)ret : size - 1] = '\0'; 735 - return ret; 548 + return __nolibc_printf(__nolibc_sprintf_cb, &state, fmt, args); 736 549 } 737 550 738 551 static __attribute__((unused, format(printf, 3, 4))) ··· 790 547 791 548 va_start(args, fmt); 792 549 ret = vsprintf(buf, fmt, args); 550 + va_end(args); 551 + 552 + return ret; 553 + } 554 + 555 + static __attribute__((unused, format(printf, 2, 0))) 556 + int __nolibc_vasprintf(char **strp, const char *fmt, va_list args1, va_list args2) 557 + { 558 + int len1, len2; 559 + char *buf; 560 + 561 + len1 = vsnprintf(NULL, 0, fmt, args1); 562 + if (len1 < 0) 563 + return -1; 564 + 565 + buf = malloc(len1 + 1); 566 + if (!buf) 567 + return -1; 568 + 569 + len2 = vsnprintf(buf, len1 + 1, fmt, args2); 570 + if (len2 < 0) { 571 + free(buf); 572 + return -1; 573 + } 574 + 575 + *strp = buf; 576 + return len1; 577 + } 578 + 579 + static __attribute__((unused, format(printf, 2, 0))) 580 + int vasprintf(char **strp, const char *fmt, va_list args) 581 + { 582 + va_list args2; 583 + int ret; 584 + 585 + va_copy(args2, args); 586 + ret = __nolibc_vasprintf(strp, fmt, args, args2); 587 + va_end(args2); 588 + 589 + return ret; 590 + } 591 + 592 + static __attribute__((unused, format(printf, 2, 3))) 593 + int asprintf(char **strp, const char *fmt, ...) 594 + { 595 + va_list args; 596 + int ret; 597 + 598 + va_start(args, fmt); 599 + ret = vasprintf(strp, fmt, args); 793 600 va_end(args); 794 601 795 602 return ret; ··· 976 683 } 977 684 978 685 static __attribute__((unused)) 979 - const char *strerror(int errno) 686 + int strerror_r(int errnum, char *buf, size_t buflen) 980 687 { 981 - static char buf[18] = "errno="; 688 + if (buflen < 18) 689 + return ERANGE; 982 690 983 - i64toa_r(errno, &buf[6]); 691 + __builtin_memcpy(buf, "errno=", 6); 692 + i64toa_r(errnum, buf + 6); 693 + return 0; 694 + } 984 695 985 - return buf; 696 + static __attribute__((unused)) 697 + const char *strerror(int errnum) 698 + { 699 + static char buf[18]; 700 + char *b = buf; 701 + 702 + /* Force gcc to use 'register offset' to access buf[]. */ 703 + _NOLIBC_OPTIMIZER_HIDE_VAR(b); 704 + 705 + /* Use strerror_r() to avoid having the only .data in small programs. */ 706 + strerror_r(errnum, b, sizeof(buf)); 707 + 708 + return b; 986 709 } 987 710 988 711 #endif /* _NOLIBC_STDIO_H */
+87 -92
tools/include/nolibc/stdlib.h
··· 55 55 __attribute__((weak,unused,noreturn,section(".text.nolibc_abort"))) 56 56 void abort(void) 57 57 { 58 - sys_kill(sys_getpid(), SIGABRT); 58 + _sys_kill(_sys_getpid(), SIGABRT); 59 59 for (;;); 60 60 } 61 61 ··· 145 145 static __attribute__((unused)) 146 146 void *calloc(size_t size, size_t nmemb) 147 147 { 148 - size_t x = size * nmemb; 148 + size_t x; 149 149 150 - if (__builtin_expect(size && ((x / size) != nmemb), 0)) { 150 + if (__builtin_expect(__builtin_mul_overflow(size, nmemb, &x), 0)) { 151 151 SET_ERRNO(ENOMEM); 152 152 return NULL; 153 153 } ··· 188 188 return ret; 189 189 } 190 190 191 + /* Converts the unsigned 64bit integer <in> to base <base> ascii into 192 + * buffer <buffer>, which must be long enough to store the number and the 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. 221 + */ 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) 225 + { 226 + unsigned int digits = 0; 227 + unsigned int dig; 228 + uint64_t q; 229 + char *p; 230 + 231 + /* Generate least significant digit first */ 232 + do { 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++; 246 + } 247 + if (dig > 9) 248 + dig += 'a' - '0' - 10; 249 + buffer[digits++] = '0' + dig; 250 + } while ((in = q)); 251 + 252 + 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 + 261 + return digits; 262 + } 263 + 191 264 /* Converts the unsigned long integer <in> to its hex representation into 192 265 * buffer <buffer>, which must be long enough to store the number and the 193 266 * trailing zero (17 bytes for "ffffffffffffffff" or 9 for "ffffffff"). The 194 267 * 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. 268 + * (not counting the trailing zero) is returned. 198 269 */ 199 - static __attribute__((unused)) 270 + static __inline__ __attribute__((unused)) 200 271 int utoh_r(unsigned long in, char *buffer) 201 272 { 202 - signed char pos = (~0UL > 0xfffffffful) ? 60 : 28; 203 - int digits = 0; 204 - int dig; 205 - 206 - 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; 214 - } 215 - } while (pos >= 0); 216 - 217 - buffer[digits] = 0; 218 - return digits; 273 + return _nolibc_u64toa_base(in, buffer, 16, _NOLIBC_U64TOA_RECIP(16)); 219 274 } 220 275 221 276 /* converts unsigned long <in> to an hex string using the static itoa_buffer ··· 288 233 * trailing zero (21 bytes for 18446744073709551615 in 64-bit, 11 for 289 234 * 4294967295 in 32-bit). The buffer is filled from the first byte, and the 290 235 * number of characters emitted (not counting the trailing zero) is returned. 291 - * The function is constructed in a way to optimize the code size and avoid 292 - * any divide that could add a dependency on large external functions. 293 236 */ 294 - static __attribute__((unused)) 237 + static __inline__ __attribute__((unused)) 295 238 int utoa_r(unsigned long in, char *buffer) 296 239 { 297 - unsigned long lim; 298 - int digits = 0; 299 - int pos = (~0UL > 0xfffffffful) ? 19 : 9; 300 - int dig; 301 - 302 - do { 303 - for (dig = 0, lim = 1; dig < pos; dig++) 304 - lim *= 10; 305 - 306 - if (digits || in >= lim || !pos) { 307 - for (dig = 0; in >= lim; dig++) 308 - in -= lim; 309 - buffer[digits++] = '0' + dig; 310 - } 311 - } while (pos--); 312 - 313 - buffer[digits] = 0; 314 - return digits; 240 + return _nolibc_u64toa_base(in, buffer, 10, _NOLIBC_U64TOA_RECIP(10)); 315 241 } 316 242 317 243 /* Converts the signed long integer <in> to its string representation into ··· 360 324 * buffer <buffer>, which must be long enough to store the number and the 361 325 * trailing zero (17 bytes for "ffffffffffffffff"). The buffer is filled from 362 326 * the first byte, and the number of characters emitted (not counting the 363 - * trailing zero) is returned. The function is constructed in a way to optimize 364 - * the code size and avoid any divide that could add a dependency on large 365 - * external functions. 327 + * trailing zero) is returned. 366 328 */ 367 - static __attribute__((unused)) 329 + static __inline__ __attribute__((unused)) 368 330 int u64toh_r(uint64_t in, char *buffer) 369 331 { 370 - signed char pos = 60; 371 - int digits = 0; 372 - int dig; 373 - 374 - do { 375 - if (sizeof(long) >= 8) { 376 - dig = (in >> pos) & 0xF; 377 - } else { 378 - /* 32-bit platforms: avoid a 64-bit shift */ 379 - uint32_t d = (pos >= 32) ? (in >> 32) : in; 380 - dig = (d >> (pos & 31)) & 0xF; 381 - } 382 - if (dig > 9) 383 - dig += 'a' - '0' - 10; 384 - pos -= 4; 385 - if (dig || digits || pos < 0) 386 - buffer[digits++] = '0' + dig; 387 - } while (pos >= 0); 388 - 389 - buffer[digits] = 0; 390 - return digits; 332 + return _nolibc_u64toa_base(in, buffer, 16, _NOLIBC_U64TOA_RECIP(16)); 391 333 } 392 334 393 335 /* converts uint64_t <in> to an hex string using the static itoa_buffer and ··· 382 368 * buffer <buffer>, which must be long enough to store the number and the 383 369 * trailing zero (21 bytes for 18446744073709551615). The buffer is filled from 384 370 * the first byte, and the number of characters emitted (not counting the 385 - * trailing zero) is returned. The function is constructed in a way to optimize 386 - * the code size and avoid any divide that could add a dependency on large 387 - * external functions. 371 + * trailing zero) is returned. 388 372 */ 389 - static __attribute__((unused)) 373 + static __inline__ __attribute__((unused)) 390 374 int u64toa_r(uint64_t in, char *buffer) 391 375 { 392 - unsigned long long lim; 393 - int digits = 0; 394 - int pos = 19; /* start with the highest possible digit */ 395 - int dig; 396 - 397 - do { 398 - for (dig = 0, lim = 1; dig < pos; dig++) 399 - lim *= 10; 400 - 401 - if (digits || in >= lim || !pos) { 402 - for (dig = 0; in >= lim; dig++) 403 - in -= lim; 404 - buffer[digits++] = '0' + dig; 405 - } 406 - } while (pos--); 407 - 408 - buffer[digits] = 0; 409 - return digits; 376 + return _nolibc_u64toa_base(in, buffer, 10, _NOLIBC_U64TOA_RECIP(10)); 410 377 } 411 378 412 379 /* Converts the signed 64-bit integer <in> to its string representation into
+144 -144
tools/include/nolibc/sys.h
··· 63 63 * - the "internal" ones, which matches the raw syscall interface at the 64 64 * kernel level, which may sometimes slightly differ from the documented 65 65 * 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 67 67 * by non-portable applications if desired. 68 68 * 69 69 * - the "exported" ones, whose interface must closely match the one ··· 85 85 */ 86 86 87 87 static __attribute__((unused)) 88 - void *sys_brk(void *addr) 88 + void *_sys_brk(void *addr) 89 89 { 90 - return (void *)my_syscall1(__NR_brk, addr); 90 + return (void *)__nolibc_syscall1(__NR_brk, addr); 91 91 } 92 92 93 93 static __attribute__((unused)) 94 94 int brk(void *addr) 95 95 { 96 - void *ret = sys_brk(addr); 96 + void *ret = _sys_brk(addr); 97 97 98 98 if (!ret) { 99 99 SET_ERRNO(ENOMEM); ··· 106 106 void *sbrk(intptr_t inc) 107 107 { 108 108 /* first call to find current end */ 109 - void *ret = sys_brk(NULL); 109 + void *ret = _sys_brk(NULL); 110 110 111 - if (ret && sys_brk(ret + inc) == ret + inc) 111 + if (ret && _sys_brk(ret + inc) == ret + inc) 112 112 return ret + inc; 113 113 114 114 SET_ERRNO(ENOMEM); ··· 122 122 */ 123 123 124 124 static __attribute__((unused)) 125 - int sys_chdir(const char *path) 125 + int _sys_chdir(const char *path) 126 126 { 127 - return my_syscall1(__NR_chdir, path); 127 + return __nolibc_syscall1(__NR_chdir, path); 128 128 } 129 129 130 130 static __attribute__((unused)) 131 131 int chdir(const char *path) 132 132 { 133 - return __sysret(sys_chdir(path)); 133 + return __sysret(_sys_chdir(path)); 134 134 } 135 135 136 136 static __attribute__((unused)) 137 - int sys_fchdir(int fildes) 137 + int _sys_fchdir(int fildes) 138 138 { 139 - return my_syscall1(__NR_fchdir, fildes); 139 + return __nolibc_syscall1(__NR_fchdir, fildes); 140 140 } 141 141 142 142 static __attribute__((unused)) 143 143 int fchdir(int fildes) 144 144 { 145 - return __sysret(sys_fchdir(fildes)); 145 + return __sysret(_sys_fchdir(fildes)); 146 146 } 147 147 148 148 ··· 151 151 */ 152 152 153 153 static __attribute__((unused)) 154 - int sys_chmod(const char *path, mode_t mode) 154 + int _sys_chmod(const char *path, mode_t mode) 155 155 { 156 156 #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); 158 158 #else 159 - return my_syscall2(__NR_chmod, path, mode); 159 + return __nolibc_syscall2(__NR_chmod, path, mode); 160 160 #endif 161 161 } 162 162 163 163 static __attribute__((unused)) 164 164 int chmod(const char *path, mode_t mode) 165 165 { 166 - return __sysret(sys_chmod(path, mode)); 166 + return __sysret(_sys_chmod(path, mode)); 167 167 } 168 168 169 169 ··· 172 172 */ 173 173 174 174 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) 176 176 { 177 177 #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); 179 179 #else 180 - return my_syscall3(__NR_chown, path, owner, group); 180 + return __nolibc_syscall3(__NR_chown, path, owner, group); 181 181 #endif 182 182 } 183 183 184 184 static __attribute__((unused)) 185 185 int chown(const char *path, uid_t owner, gid_t group) 186 186 { 187 - return __sysret(sys_chown(path, owner, group)); 187 + return __sysret(_sys_chown(path, owner, group)); 188 188 } 189 189 190 190 ··· 193 193 */ 194 194 195 195 static __attribute__((unused)) 196 - int sys_chroot(const char *path) 196 + int _sys_chroot(const char *path) 197 197 { 198 - return my_syscall1(__NR_chroot, path); 198 + return __nolibc_syscall1(__NR_chroot, path); 199 199 } 200 200 201 201 static __attribute__((unused)) 202 202 int chroot(const char *path) 203 203 { 204 - return __sysret(sys_chroot(path)); 204 + return __sysret(_sys_chroot(path)); 205 205 } 206 206 207 207 ··· 210 210 */ 211 211 212 212 static __attribute__((unused)) 213 - int sys_close(int fd) 213 + int _sys_close(int fd) 214 214 { 215 - return my_syscall1(__NR_close, fd); 215 + return __nolibc_syscall1(__NR_close, fd); 216 216 } 217 217 218 218 static __attribute__((unused)) 219 219 int close(int fd) 220 220 { 221 - return __sysret(sys_close(fd)); 221 + return __sysret(_sys_close(fd)); 222 222 } 223 223 224 224 ··· 227 227 */ 228 228 229 229 static __attribute__((unused)) 230 - int sys_dup(int fd) 230 + int _sys_dup(int fd) 231 231 { 232 - return my_syscall1(__NR_dup, fd); 232 + return __nolibc_syscall1(__NR_dup, fd); 233 233 } 234 234 235 235 static __attribute__((unused)) 236 236 int dup(int fd) 237 237 { 238 - return __sysret(sys_dup(fd)); 238 + return __sysret(_sys_dup(fd)); 239 239 } 240 240 241 241 ··· 244 244 */ 245 245 246 246 static __attribute__((unused)) 247 - int sys_dup2(int old, int new) 247 + int _sys_dup2(int old, int new) 248 248 { 249 249 #if defined(__NR_dup3) 250 250 int ret, nr_fcntl; ··· 256 256 #endif 257 257 258 258 if (old == new) { 259 - ret = my_syscall2(nr_fcntl, old, F_GETFD); 259 + ret = __nolibc_syscall2(nr_fcntl, old, F_GETFD); 260 260 return ret < 0 ? ret : old; 261 261 } 262 262 263 - return my_syscall3(__NR_dup3, old, new, 0); 263 + return __nolibc_syscall3(__NR_dup3, old, new, 0); 264 264 #else 265 - return my_syscall2(__NR_dup2, old, new); 265 + return __nolibc_syscall2(__NR_dup2, old, new); 266 266 #endif 267 267 } 268 268 269 269 static __attribute__((unused)) 270 270 int dup2(int old, int new) 271 271 { 272 - return __sysret(sys_dup2(old, new)); 272 + return __sysret(_sys_dup2(old, new)); 273 273 } 274 274 275 275 ··· 279 279 280 280 #if defined(__NR_dup3) 281 281 static __attribute__((unused)) 282 - int sys_dup3(int old, int new, int flags) 282 + int _sys_dup3(int old, int new, int flags) 283 283 { 284 - return my_syscall3(__NR_dup3, old, new, flags); 284 + return __nolibc_syscall3(__NR_dup3, old, new, flags); 285 285 } 286 286 287 287 static __attribute__((unused)) 288 288 int dup3(int old, int new, int flags) 289 289 { 290 - return __sysret(sys_dup3(old, new, flags)); 290 + return __sysret(_sys_dup3(old, new, flags)); 291 291 } 292 292 #endif 293 293 ··· 297 297 */ 298 298 299 299 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[]) 301 301 { 302 - return my_syscall3(__NR_execve, filename, argv, envp); 302 + return __nolibc_syscall3(__NR_execve, filename, argv, envp); 303 303 } 304 304 305 305 static __attribute__((unused)) 306 306 int execve(const char *filename, char *const argv[], char *const envp[]) 307 307 { 308 - return __sysret(sys_execve(filename, argv, envp)); 308 + return __sysret(_sys_execve(filename, argv, envp)); 309 309 } 310 310 311 311 ··· 314 314 */ 315 315 316 316 static __attribute__((noreturn,unused)) 317 - void sys_exit(int status) 317 + void _sys_exit(int status) 318 318 { 319 - my_syscall1(__NR_exit, status & 255); 319 + __nolibc_syscall1(__NR_exit, status & 255); 320 320 while(1); /* shut the "noreturn" warnings. */ 321 321 } 322 322 323 323 static __attribute__((noreturn,unused)) 324 324 void _exit(int status) 325 325 { 326 - sys_exit(status); 326 + _sys_exit(status); 327 327 } 328 328 329 329 static __attribute__((noreturn,unused)) ··· 337 337 * pid_t fork(void); 338 338 */ 339 339 340 - #ifndef sys_fork 340 + #ifndef _sys_fork 341 341 static __attribute__((unused)) 342 - pid_t sys_fork(void) 342 + pid_t _sys_fork(void) 343 343 { 344 344 #if defined(__NR_clone) 345 345 /* note: some archs only have clone() and not fork(). Different archs 346 346 * have a different API, but most archs have the flags on first arg and 347 347 * will not use the rest with no other flag. 348 348 */ 349 - return my_syscall5(__NR_clone, SIGCHLD, 0, 0, 0, 0); 349 + return __nolibc_syscall5(__NR_clone, SIGCHLD, 0, 0, 0, 0); 350 350 #else 351 - return my_syscall0(__NR_fork); 351 + return __nolibc_syscall0(__NR_fork); 352 352 #endif 353 353 } 354 354 #endif ··· 356 356 static __attribute__((unused)) 357 357 pid_t fork(void) 358 358 { 359 - return __sysret(sys_fork()); 359 + return __sysret(_sys_fork()); 360 360 } 361 361 362 - #ifndef sys_vfork 362 + #ifndef _sys_vfork 363 363 static __attribute__((unused)) 364 - pid_t sys_vfork(void) 364 + pid_t _sys_vfork(void) 365 365 { 366 366 #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); 369 369 #elif defined(__NR_vfork) 370 - return my_syscall0(__NR_vfork); 370 + return __nolibc_syscall0(__NR_vfork); 371 371 #endif 372 372 } 373 373 #endif ··· 375 375 static __attribute__((unused)) 376 376 pid_t vfork(void) 377 377 { 378 - return __sysret(sys_vfork()); 378 + return __sysret(_sys_vfork()); 379 379 } 380 380 381 381 /* ··· 383 383 */ 384 384 385 385 static __attribute__((unused)) 386 - int sys_fsync(int fd) 386 + int _sys_fsync(int fd) 387 387 { 388 - return my_syscall1(__NR_fsync, fd); 388 + return __nolibc_syscall1(__NR_fsync, fd); 389 389 } 390 390 391 391 static __attribute__((unused)) 392 392 int fsync(int fd) 393 393 { 394 - return __sysret(sys_fsync(fd)); 394 + return __sysret(_sys_fsync(fd)); 395 395 } 396 396 397 397 ··· 400 400 */ 401 401 402 402 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) 404 404 { 405 - return my_syscall3(__NR_getdents64, fd, dirp, count); 405 + return __nolibc_syscall3(__NR_getdents64, fd, dirp, count); 406 406 } 407 407 408 408 static __attribute__((unused)) 409 409 int getdents64(int fd, struct linux_dirent64 *dirp, int count) 410 410 { 411 - return __sysret(sys_getdents64(fd, dirp, count)); 411 + return __sysret(_sys_getdents64(fd, dirp, count)); 412 412 } 413 413 414 414 ··· 417 417 */ 418 418 419 419 static __attribute__((unused)) 420 - uid_t sys_geteuid(void) 420 + uid_t _sys_geteuid(void) 421 421 { 422 422 #if defined(__NR_geteuid32) 423 - return my_syscall0(__NR_geteuid32); 423 + return __nolibc_syscall0(__NR_geteuid32); 424 424 #else 425 - return my_syscall0(__NR_geteuid); 425 + return __nolibc_syscall0(__NR_geteuid); 426 426 #endif 427 427 } 428 428 429 429 static __attribute__((unused)) 430 430 uid_t geteuid(void) 431 431 { 432 - return sys_geteuid(); 432 + return _sys_geteuid(); 433 433 } 434 434 435 435 ··· 438 438 */ 439 439 440 440 static __attribute__((unused)) 441 - pid_t sys_getpgid(pid_t pid) 441 + pid_t _sys_getpgid(pid_t pid) 442 442 { 443 - return my_syscall1(__NR_getpgid, pid); 443 + return __nolibc_syscall1(__NR_getpgid, pid); 444 444 } 445 445 446 446 static __attribute__((unused)) 447 447 pid_t getpgid(pid_t pid) 448 448 { 449 - return __sysret(sys_getpgid(pid)); 449 + return __sysret(_sys_getpgid(pid)); 450 450 } 451 451 452 452 ··· 455 455 */ 456 456 457 457 static __attribute__((unused)) 458 - pid_t sys_getpgrp(void) 458 + pid_t _sys_getpgrp(void) 459 459 { 460 - return sys_getpgid(0); 460 + return _sys_getpgid(0); 461 461 } 462 462 463 463 static __attribute__((unused)) 464 464 pid_t getpgrp(void) 465 465 { 466 - return sys_getpgrp(); 466 + return _sys_getpgrp(); 467 467 } 468 468 469 469 ··· 472 472 */ 473 473 474 474 static __attribute__((unused)) 475 - pid_t sys_getpid(void) 475 + pid_t _sys_getpid(void) 476 476 { 477 - return my_syscall0(__NR_getpid); 477 + return __nolibc_syscall0(__NR_getpid); 478 478 } 479 479 480 480 static __attribute__((unused)) 481 481 pid_t getpid(void) 482 482 { 483 - return sys_getpid(); 483 + return _sys_getpid(); 484 484 } 485 485 486 486 ··· 489 489 */ 490 490 491 491 static __attribute__((unused)) 492 - pid_t sys_getppid(void) 492 + pid_t _sys_getppid(void) 493 493 { 494 - return my_syscall0(__NR_getppid); 494 + return __nolibc_syscall0(__NR_getppid); 495 495 } 496 496 497 497 static __attribute__((unused)) 498 498 pid_t getppid(void) 499 499 { 500 - return sys_getppid(); 500 + return _sys_getppid(); 501 501 } 502 502 503 503 ··· 506 506 */ 507 507 508 508 static __attribute__((unused)) 509 - pid_t sys_gettid(void) 509 + pid_t _sys_gettid(void) 510 510 { 511 - return my_syscall0(__NR_gettid); 511 + return __nolibc_syscall0(__NR_gettid); 512 512 } 513 513 514 514 static __attribute__((unused)) 515 515 pid_t gettid(void) 516 516 { 517 - return sys_gettid(); 517 + return _sys_gettid(); 518 518 } 519 519 520 520 #ifndef NOLIBC_NO_RUNTIME ··· 536 536 */ 537 537 538 538 static __attribute__((unused)) 539 - uid_t sys_getuid(void) 539 + uid_t _sys_getuid(void) 540 540 { 541 541 #if defined(__NR_getuid32) 542 - return my_syscall0(__NR_getuid32); 542 + return __nolibc_syscall0(__NR_getuid32); 543 543 #else 544 - return my_syscall0(__NR_getuid); 544 + return __nolibc_syscall0(__NR_getuid); 545 545 #endif 546 546 } 547 547 548 548 static __attribute__((unused)) 549 549 uid_t getuid(void) 550 550 { 551 - return sys_getuid(); 551 + return _sys_getuid(); 552 552 } 553 553 554 554 ··· 557 557 */ 558 558 559 559 static __attribute__((unused)) 560 - int sys_kill(pid_t pid, int signal) 560 + int _sys_kill(pid_t pid, int signal) 561 561 { 562 - return my_syscall2(__NR_kill, pid, signal); 562 + return __nolibc_syscall2(__NR_kill, pid, signal); 563 563 } 564 564 565 565 static __attribute__((unused)) 566 566 int kill(pid_t pid, int signal) 567 567 { 568 - return __sysret(sys_kill(pid, signal)); 568 + return __sysret(_sys_kill(pid, signal)); 569 569 } 570 570 571 571 ··· 574 574 */ 575 575 576 576 static __attribute__((unused)) 577 - int sys_link(const char *old, const char *new) 577 + int _sys_link(const char *old, const char *new) 578 578 { 579 579 #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); 581 581 #else 582 - return my_syscall2(__NR_link, old, new); 582 + return __nolibc_syscall2(__NR_link, old, new); 583 583 #endif 584 584 } 585 585 586 586 static __attribute__((unused)) 587 587 int link(const char *old, const char *new) 588 588 { 589 - return __sysret(sys_link(old, new)); 589 + return __sysret(_sys_link(old, new)); 590 590 } 591 591 592 592 ··· 595 595 */ 596 596 597 597 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) 599 599 { 600 600 #if defined(__NR_llseek) 601 601 __kernel_loff_t loff = 0; 602 602 off_t result; 603 603 int ret; 604 604 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); 606 606 if (ret < 0) 607 607 result = ret; 608 608 else ··· 610 610 611 611 return result; 612 612 #else 613 - return my_syscall3(__NR_lseek, fd, offset, whence); 613 + return __nolibc_syscall3(__NR_lseek, fd, offset, whence); 614 614 #endif 615 615 } 616 616 617 617 static __attribute__((unused)) 618 618 off_t lseek(int fd, off_t offset, int whence) 619 619 { 620 - return __sysret(sys_lseek(fd, offset, whence)); 620 + return __sysret(_sys_lseek(fd, offset, whence)); 621 621 } 622 622 623 623 ··· 626 626 */ 627 627 628 628 static __attribute__((unused)) 629 - int sys_mkdir(const char *path, mode_t mode) 629 + int _sys_mkdir(const char *path, mode_t mode) 630 630 { 631 631 #if defined(__NR_mkdirat) 632 - return my_syscall3(__NR_mkdirat, AT_FDCWD, path, mode); 632 + return __nolibc_syscall3(__NR_mkdirat, AT_FDCWD, path, mode); 633 633 #else 634 - return my_syscall2(__NR_mkdir, path, mode); 634 + return __nolibc_syscall2(__NR_mkdir, path, mode); 635 635 #endif 636 636 } 637 637 638 638 static __attribute__((unused)) 639 639 int mkdir(const char *path, mode_t mode) 640 640 { 641 - return __sysret(sys_mkdir(path, mode)); 641 + return __sysret(_sys_mkdir(path, mode)); 642 642 } 643 643 644 644 /* ··· 646 646 */ 647 647 648 648 static __attribute__((unused)) 649 - int sys_rmdir(const char *path) 649 + int _sys_rmdir(const char *path) 650 650 { 651 651 #if defined(__NR_rmdir) 652 - return my_syscall1(__NR_rmdir, path); 652 + return __nolibc_syscall1(__NR_rmdir, path); 653 653 #else 654 - return my_syscall3(__NR_unlinkat, AT_FDCWD, path, AT_REMOVEDIR); 654 + return __nolibc_syscall3(__NR_unlinkat, AT_FDCWD, path, AT_REMOVEDIR); 655 655 #endif 656 656 } 657 657 658 658 static __attribute__((unused)) 659 659 int rmdir(const char *path) 660 660 { 661 - return __sysret(sys_rmdir(path)); 661 + return __sysret(_sys_rmdir(path)); 662 662 } 663 663 664 664 ··· 667 667 */ 668 668 669 669 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) 671 671 { 672 672 #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); 674 674 #else 675 - return my_syscall3(__NR_mknod, path, mode, dev); 675 + return __nolibc_syscall3(__NR_mknod, path, mode, dev); 676 676 #endif 677 677 } 678 678 679 679 static __attribute__((unused)) 680 680 int mknod(const char *path, mode_t mode, dev_t dev) 681 681 { 682 - return __sysret(sys_mknod(path, mode, dev)); 682 + return __sysret(_sys_mknod(path, mode, dev)); 683 683 } 684 684 685 685 ··· 689 689 */ 690 690 691 691 static __attribute__((unused)) 692 - int sys_pipe2(int pipefd[2], int flags) 692 + int _sys_pipe2(int pipefd[2], int flags) 693 693 { 694 - return my_syscall2(__NR_pipe2, pipefd, flags); 694 + return __nolibc_syscall2(__NR_pipe2, pipefd, flags); 695 695 } 696 696 697 697 static __attribute__((unused)) 698 698 int pipe2(int pipefd[2], int flags) 699 699 { 700 - return __sysret(sys_pipe2(pipefd, flags)); 700 + return __sysret(_sys_pipe2(pipefd, flags)); 701 701 } 702 702 703 703 static __attribute__((unused)) ··· 712 712 */ 713 713 714 714 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) 716 716 { 717 - return my_syscall2(__NR_pivot_root, new, old); 717 + return __nolibc_syscall2(__NR_pivot_root, new, old); 718 718 } 719 719 720 720 static __attribute__((unused)) 721 721 int pivot_root(const char *new, const char *old) 722 722 { 723 - return __sysret(sys_pivot_root(new, old)); 723 + return __sysret(_sys_pivot_root(new, old)); 724 724 } 725 725 726 726 ··· 729 729 */ 730 730 731 731 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) 733 733 { 734 - return my_syscall3(__NR_read, fd, buf, count); 734 + return __nolibc_syscall3(__NR_read, fd, buf, count); 735 735 } 736 736 737 737 static __attribute__((unused)) 738 738 ssize_t read(int fd, void *buf, size_t count) 739 739 { 740 - return __sysret(sys_read(fd, buf, count)); 740 + return __sysret(_sys_read(fd, buf, count)); 741 741 } 742 742 743 743 ··· 746 746 */ 747 747 748 748 static __attribute__((unused)) 749 - int sys_sched_yield(void) 749 + int _sys_sched_yield(void) 750 750 { 751 - return my_syscall0(__NR_sched_yield); 751 + return __nolibc_syscall0(__NR_sched_yield); 752 752 } 753 753 754 754 static __attribute__((unused)) 755 755 int sched_yield(void) 756 756 { 757 - return __sysret(sys_sched_yield()); 757 + return __sysret(_sys_sched_yield()); 758 758 } 759 759 760 760 ··· 763 763 */ 764 764 765 765 static __attribute__((unused)) 766 - int sys_setpgid(pid_t pid, pid_t pgid) 766 + int _sys_setpgid(pid_t pid, pid_t pgid) 767 767 { 768 - return my_syscall2(__NR_setpgid, pid, pgid); 768 + return __nolibc_syscall2(__NR_setpgid, pid, pgid); 769 769 } 770 770 771 771 static __attribute__((unused)) 772 772 int setpgid(pid_t pid, pid_t pgid) 773 773 { 774 - return __sysret(sys_setpgid(pid, pgid)); 774 + return __sysret(_sys_setpgid(pid, pgid)); 775 775 } 776 776 777 777 /* ··· 790 790 */ 791 791 792 792 static __attribute__((unused)) 793 - pid_t sys_setsid(void) 793 + pid_t _sys_setsid(void) 794 794 { 795 - return my_syscall0(__NR_setsid); 795 + return __nolibc_syscall0(__NR_setsid); 796 796 } 797 797 798 798 static __attribute__((unused)) 799 799 pid_t setsid(void) 800 800 { 801 - return __sysret(sys_setsid()); 801 + return __sysret(_sys_setsid()); 802 802 } 803 803 804 804 ··· 807 807 */ 808 808 809 809 static __attribute__((unused)) 810 - int sys_symlink(const char *old, const char *new) 810 + int _sys_symlink(const char *old, const char *new) 811 811 { 812 812 #if defined(__NR_symlinkat) 813 - return my_syscall3(__NR_symlinkat, old, AT_FDCWD, new); 813 + return __nolibc_syscall3(__NR_symlinkat, old, AT_FDCWD, new); 814 814 #else 815 - return my_syscall2(__NR_symlink, old, new); 815 + return __nolibc_syscall2(__NR_symlink, old, new); 816 816 #endif 817 817 } 818 818 819 819 static __attribute__((unused)) 820 820 int symlink(const char *old, const char *new) 821 821 { 822 - return __sysret(sys_symlink(old, new)); 822 + return __sysret(_sys_symlink(old, new)); 823 823 } 824 824 825 825 ··· 828 828 */ 829 829 830 830 static __attribute__((unused)) 831 - mode_t sys_umask(mode_t mode) 831 + mode_t _sys_umask(mode_t mode) 832 832 { 833 - return my_syscall1(__NR_umask, mode); 833 + return __nolibc_syscall1(__NR_umask, mode); 834 834 } 835 835 836 836 static __attribute__((unused)) 837 837 mode_t umask(mode_t mode) 838 838 { 839 - return sys_umask(mode); 839 + return _sys_umask(mode); 840 840 } 841 841 842 842 ··· 845 845 */ 846 846 847 847 static __attribute__((unused)) 848 - int sys_umount2(const char *path, int flags) 848 + int _sys_umount2(const char *path, int flags) 849 849 { 850 - return my_syscall2(__NR_umount2, path, flags); 850 + return __nolibc_syscall2(__NR_umount2, path, flags); 851 851 } 852 852 853 853 static __attribute__((unused)) 854 854 int umount2(const char *path, int flags) 855 855 { 856 - return __sysret(sys_umount2(path, flags)); 856 + return __sysret(_sys_umount2(path, flags)); 857 857 } 858 858 859 859 ··· 862 862 */ 863 863 864 864 static __attribute__((unused)) 865 - int sys_unlink(const char *path) 865 + int _sys_unlink(const char *path) 866 866 { 867 867 #if defined(__NR_unlinkat) 868 - return my_syscall3(__NR_unlinkat, AT_FDCWD, path, 0); 868 + return __nolibc_syscall3(__NR_unlinkat, AT_FDCWD, path, 0); 869 869 #else 870 - return my_syscall1(__NR_unlink, path); 870 + return __nolibc_syscall1(__NR_unlink, path); 871 871 #endif 872 872 } 873 873 874 874 static __attribute__((unused)) 875 875 int unlink(const char *path) 876 876 { 877 - return __sysret(sys_unlink(path)); 877 + return __sysret(_sys_unlink(path)); 878 878 } 879 879 880 880 ··· 883 883 */ 884 884 885 885 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) 887 887 { 888 - return my_syscall3(__NR_write, fd, buf, count); 888 + return __nolibc_syscall3(__NR_write, fd, buf, count); 889 889 } 890 890 891 891 static __attribute__((unused)) 892 892 ssize_t write(int fd, const void *buf, size_t count) 893 893 { 894 - return __sysret(sys_write(fd, buf, count)); 894 + return __sysret(_sys_write(fd, buf, count)); 895 895 } 896 896 897 897 ··· 900 900 */ 901 901 902 902 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) 904 904 { 905 - return my_syscall2(__NR_memfd_create, name, flags); 905 + return __nolibc_syscall2(__NR_memfd_create, name, flags); 906 906 } 907 907 908 908 static __attribute__((unused)) 909 909 int memfd_create(const char *name, unsigned int flags) 910 910 { 911 - return __sysret(sys_memfd_create(name, flags)); 911 + return __sysret(_sys_memfd_create(name, flags)); 912 912 } 913 913 914 914 #endif /* _NOLIBC_SYS_H */
+3 -3
tools/include/nolibc/sys/ioctl.h
··· 19 19 */ 20 20 21 21 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) 23 23 { 24 - return my_syscall3(__NR_ioctl, fd, cmd, arg); 24 + return __nolibc_syscall3(__NR_ioctl, fd, cmd, arg); 25 25 } 26 26 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))) 28 28 29 29 #endif /* _NOLIBC_SYS_IOCTL_H */
+12 -12
tools/include/nolibc/sys/mman.h
··· 13 13 #include "../arch.h" 14 14 #include "../sys.h" 15 15 16 - #ifndef sys_mmap 16 + #ifndef _sys_mmap 17 17 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) 20 20 { 21 21 int n; 22 22 ··· 27 27 n = __NR_mmap; 28 28 #endif 29 29 30 - return (void *)my_syscall6(n, addr, length, prot, flags, fd, offset); 30 + return (void *)__nolibc_syscall6(n, addr, length, prot, flags, fd, offset); 31 31 } 32 32 #endif 33 33 34 34 static __attribute__((unused)) 35 35 void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) 36 36 { 37 - void *ret = sys_mmap(addr, length, prot, flags, fd, offset); 37 + void *ret = _sys_mmap(addr, length, prot, flags, fd, offset); 38 38 39 39 if ((unsigned long)ret >= -4095UL) { 40 40 SET_ERRNO(-(long)ret); ··· 44 44 } 45 45 46 46 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) 48 48 { 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); 51 51 } 52 52 53 53 static __attribute__((unused)) 54 54 void *mremap(void *old_address, size_t old_size, size_t new_size, int flags, void *new_address) 55 55 { 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); 57 57 58 58 if ((unsigned long)ret >= -4095UL) { 59 59 SET_ERRNO(-(long)ret); ··· 63 63 } 64 64 65 65 static __attribute__((unused)) 66 - int sys_munmap(void *addr, size_t length) 66 + int _sys_munmap(void *addr, size_t length) 67 67 { 68 - return my_syscall2(__NR_munmap, addr, length); 68 + return __nolibc_syscall2(__NR_munmap, addr, length); 69 69 } 70 70 71 71 static __attribute__((unused)) 72 72 int munmap(void *addr, size_t length) 73 73 { 74 - return __sysret(sys_munmap(addr, length)); 74 + return __sysret(_sys_munmap(addr, length)); 75 75 } 76 76 77 77 #endif /* _NOLIBC_SYS_MMAN_H */
+4 -4
tools/include/nolibc/sys/mount.h
··· 20 20 * const void *data); 21 21 */ 22 22 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) 25 25 { 26 - return my_syscall5(__NR_mount, src, tgt, fst, flags, data); 26 + return __nolibc_syscall5(__NR_mount, src, tgt, fst, flags, data); 27 27 } 28 28 29 29 static __attribute__((unused)) ··· 31 31 const char *fst, unsigned long flags, 32 32 const void *data) 33 33 { 34 - return __sysret(sys_mount(src, tgt, fst, flags, data)); 34 + return __sysret(_sys_mount(src, tgt, fst, flags, data)); 35 35 } 36 36 37 37 #endif /* _NOLIBC_SYS_MOUNT_H */
+4 -4
tools/include/nolibc/sys/prctl.h
··· 20 20 */ 21 21 22 22 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) 25 25 { 26 - return my_syscall5(__NR_prctl, option, arg2, arg3, arg4, arg5); 26 + return __nolibc_syscall5(__NR_prctl, option, arg2, arg3, arg4, arg5); 27 27 } 28 28 29 29 static __attribute__((unused)) 30 30 int prctl(int option, unsigned long arg2, unsigned long arg3, 31 31 unsigned long arg4, unsigned long arg5) 32 32 { 33 - return __sysret(sys_prctl(option, arg2, arg3, arg4, arg5)); 33 + return __sysret(_sys_prctl(option, arg2, arg3, arg4, arg5)); 34 34 } 35 35 36 36 #endif /* _NOLIBC_SYS_PRCTL_H */
+3 -3
tools/include/nolibc/sys/ptrace.h
··· 19 19 * long ptrace(int op, pid_t pid, void *addr, void *data); 20 20 */ 21 21 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) 23 23 { 24 - return my_syscall4(__NR_ptrace, op, pid, addr, data); 24 + return __nolibc_syscall4(__NR_ptrace, op, pid, addr, data); 25 25 } 26 26 27 27 static __attribute__((unused)) 28 28 ssize_t ptrace(int op, pid_t pid, void *addr, void *data) 29 29 { 30 - return __sysret(sys_ptrace(op, pid, addr, data)); 30 + return __sysret(_sys_ptrace(op, pid, addr, data)); 31 31 } 32 32 33 33 #endif /* _NOLIBC_SYS_PTRACE_H */
+3 -3
tools/include/nolibc/sys/random.h
··· 20 20 */ 21 21 22 22 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) 24 24 { 25 - return my_syscall3(__NR_getrandom, buf, buflen, flags); 25 + return __nolibc_syscall3(__NR_getrandom, buf, buflen, flags); 26 26 } 27 27 28 28 static __attribute__((unused)) 29 29 ssize_t getrandom(void *buf, size_t buflen, unsigned int flags) 30 30 { 31 - return __sysret(sys_getrandom(buf, buflen, flags)); 31 + return __sysret(_sys_getrandom(buf, buflen, flags)); 32 32 } 33 33 34 34 #endif /* _NOLIBC_SYS_RANDOM_H */
+3 -3
tools/include/nolibc/sys/reboot.h
··· 20 20 */ 21 21 22 22 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) 24 24 { 25 - return my_syscall4(__NR_reboot, magic1, magic2, cmd, arg); 25 + return __nolibc_syscall4(__NR_reboot, magic1, magic2, cmd, arg); 26 26 } 27 27 28 28 static __attribute__((unused)) 29 29 int reboot(int cmd) 30 30 { 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)); 32 32 } 33 33 34 34 #endif /* _NOLIBC_SYS_REBOOT_H */
+5 -5
tools/include/nolibc/sys/resource.h
··· 20 20 */ 21 21 22 22 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) 25 25 { 26 - return my_syscall4(__NR_prlimit64, pid, resource, new_limit, old_limit); 26 + return __nolibc_syscall4(__NR_prlimit64, pid, resource, new_limit, old_limit); 27 27 } 28 28 29 29 static __attribute__((unused)) ··· 32 32 struct rlimit64 rlim64; 33 33 int ret; 34 34 35 - ret = __sysret(sys_prlimit64(0, resource, NULL, &rlim64)); 35 + ret = __sysret(_sys_prlimit64(0, resource, NULL, &rlim64)); 36 36 rlim->rlim_cur = rlim64.rlim_cur; 37 37 rlim->rlim_max = rlim64.rlim_max; 38 38 ··· 47 47 .rlim_max = rlim->rlim_max, 48 48 }; 49 49 50 - return __sysret(sys_prlimit64(0, resource, &rlim64, NULL)); 50 + return __sysret(_sys_prlimit64(0, resource, &rlim64, NULL)); 51 51 } 52 52 53 53 #endif /* _NOLIBC_SYS_RESOURCE_H */
+6 -4
tools/include/nolibc/sys/select.h
··· 61 61 */ 62 62 63 63 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) 65 65 { 66 66 #if defined(__NR_pselect6_time64) 67 67 struct __kernel_timespec t; ··· 70 70 t.tv_sec = timeout->tv_sec; 71 71 t.tv_nsec = (uint32_t)timeout->tv_usec * 1000; 72 72 } 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); 74 75 #else 75 76 struct __kernel_old_timespec t; 76 77 ··· 79 78 t.tv_sec = timeout->tv_sec; 80 79 t.tv_nsec = (uint32_t)timeout->tv_usec * 1000; 81 80 } 82 - return my_syscall6(__NR_pselect6, nfds, rfds, wfds, efds, timeout ? &t : NULL, NULL); 81 + return __nolibc_syscall6(__NR_pselect6, nfds, rfds, wfds, efds, 82 + timeout ? &t : NULL, NULL); 83 83 #endif 84 84 } 85 85 86 86 static __attribute__((unused)) 87 87 int select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout) 88 88 { 89 - return __sysret(sys_select(nfds, rfds, wfds, efds, timeout)); 89 + return __sysret(_sys_select(nfds, rfds, wfds, efds, timeout)); 90 90 } 91 91 92 92
+7 -10
tools/include/nolibc/sys/stat.h
··· 13 13 #include "../arch.h" 14 14 #include "../types.h" 15 15 #include "../sys.h" 16 + #include "../sys/sysmacros.h" 16 17 17 18 /* 18 19 * int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf); ··· 24 23 */ 25 24 26 25 static __attribute__((unused)) 27 - int sys_statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf) 26 + int _sys_statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf) 28 27 { 29 28 #ifdef __NR_statx 30 - return my_syscall5(__NR_statx, fd, path, flags, mask, buf); 29 + return __nolibc_syscall5(__NR_statx, fd, path, flags, mask, buf); 31 30 #else 32 31 return __nolibc_enosys(__func__, fd, path, flags, mask, buf); 33 32 #endif ··· 36 35 static __attribute__((unused)) 37 36 int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf) 38 37 { 39 - return __sysret(sys_statx(fd, path, flags, mask, buf)); 38 + return __sysret(_sys_statx(fd, path, flags, mask, buf)); 40 39 } 41 40 42 41 ··· 46 45 struct statx statx; 47 46 long ret; 48 47 49 - ret = __sysret(sys_statx(fd, path, flag | AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &statx)); 48 + ret = __sysret(_sys_statx(fd, path, flag | AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &statx)); 50 49 if (ret == -1) 51 50 return ret; 52 51 53 - buf->st_dev = ((statx.stx_dev_minor & 0xff) 54 - | (statx.stx_dev_major << 8) 55 - | ((statx.stx_dev_minor & ~0xff) << 12)); 52 + buf->st_dev = makedev(statx.stx_dev_major, statx.stx_dev_minor); 56 53 buf->st_ino = statx.stx_ino; 57 54 buf->st_mode = statx.stx_mode; 58 55 buf->st_nlink = statx.stx_nlink; 59 56 buf->st_uid = statx.stx_uid; 60 57 buf->st_gid = statx.stx_gid; 61 - buf->st_rdev = ((statx.stx_rdev_minor & 0xff) 62 - | (statx.stx_rdev_major << 8) 63 - | ((statx.stx_rdev_minor & ~0xff) << 12)); 58 + buf->st_rdev = makedev(statx.stx_rdev_major, statx.stx_rdev_minor); 64 59 buf->st_size = statx.stx_size; 65 60 buf->st_blksize = statx.stx_blksize; 66 61 buf->st_blocks = statx.stx_blocks;
+6 -5
tools/include/nolibc/sys/syscall.h
··· 10 10 #ifndef _NOLIBC_SYS_SYSCALL_H 11 11 #define _NOLIBC_SYS_SYSCALL_H 12 12 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__)) 18 19 19 20 #endif /* _NOLIBC_SYS_SYSCALL_H */
+21 -4
tools/include/nolibc/sys/sysmacros.h
··· 12 12 13 13 #include "../std.h" 14 14 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) 19 36 20 37 #endif /* _NOLIBC_SYS_SYSMACROS_H */
+4 -4
tools/include/nolibc/sys/time.h
··· 13 13 #include "../arch.h" 14 14 #include "../sys.h" 15 15 16 - static int sys_clock_gettime(clockid_t clockid, struct timespec *tp); 16 + static int _sys_clock_gettime(clockid_t clockid, struct timespec *tp); 17 17 18 18 /* 19 19 * int gettimeofday(struct timeval *tv, struct timezone *tz); 20 20 */ 21 21 22 22 static __attribute__((unused)) 23 - int sys_gettimeofday(struct timeval *tv, struct timezone *tz) 23 + int _sys_gettimeofday(struct timeval *tv, struct timezone *tz) 24 24 { 25 25 (void) tz; /* Non-NULL tz is undefined behaviour */ 26 26 27 27 struct timespec tp; 28 28 int ret; 29 29 30 - ret = sys_clock_gettime(CLOCK_REALTIME, &tp); 30 + ret = _sys_clock_gettime(CLOCK_REALTIME, &tp); 31 31 if (!ret && tv) { 32 32 tv->tv_sec = tp.tv_sec; 33 33 tv->tv_usec = (uint32_t)tp.tv_nsec / 1000; ··· 39 39 static __attribute__((unused)) 40 40 int gettimeofday(struct timeval *tv, struct timezone *tz) 41 41 { 42 - return __sysret(sys_gettimeofday(tv, tz)); 42 + return __sysret(_sys_gettimeofday(tv, tz)); 43 43 } 44 44 45 45 #endif /* _NOLIBC_SYS_TIME_H */
+12 -12
tools/include/nolibc/sys/timerfd.h
··· 17 17 18 18 19 19 static __attribute__((unused)) 20 - int sys_timerfd_create(int clockid, int flags) 20 + int _sys_timerfd_create(int clockid, int flags) 21 21 { 22 - return my_syscall2(__NR_timerfd_create, clockid, flags); 22 + return __nolibc_syscall2(__NR_timerfd_create, clockid, flags); 23 23 } 24 24 25 25 static __attribute__((unused)) 26 26 int timerfd_create(int clockid, int flags) 27 27 { 28 - return __sysret(sys_timerfd_create(clockid, flags)); 28 + return __sysret(_sys_timerfd_create(clockid, flags)); 29 29 } 30 30 31 31 32 32 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) 34 34 { 35 35 #if defined(__NR_timerfd_gettime64) 36 36 __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); 38 38 #else 39 39 __nolibc_assert_native_time64(); 40 - return my_syscall2(__NR_timerfd_gettime, fd, curr_value); 40 + return __nolibc_syscall2(__NR_timerfd_gettime, fd, curr_value); 41 41 #endif 42 42 } 43 43 44 44 static __attribute__((unused)) 45 45 int timerfd_gettime(int fd, struct itimerspec *curr_value) 46 46 { 47 - return __sysret(sys_timerfd_gettime(fd, curr_value)); 47 + return __sysret(_sys_timerfd_gettime(fd, curr_value)); 48 48 } 49 49 50 50 51 51 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) 54 54 { 55 55 #if defined(__NR_timerfd_settime64) 56 56 __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); 58 58 #else 59 59 __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); 61 61 #endif 62 62 } 63 63 ··· 65 65 int timerfd_settime(int fd, int flags, 66 66 const struct itimerspec *new_value, struct itimerspec *old_value) 67 67 { 68 - return __sysret(sys_timerfd_settime(fd, flags, new_value, old_value)); 68 + return __sysret(_sys_timerfd_settime(fd, flags, new_value, old_value)); 69 69 } 70 70 71 71 #endif /* _NOLIBC_SYS_TIMERFD_H */
+6 -6
tools/include/nolibc/sys/uio.h
··· 19 19 * ssize_t readv(int fd, const struct iovec *iovec, int count); 20 20 */ 21 21 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) 23 23 { 24 - return my_syscall3(__NR_readv, fd, iovec, count); 24 + return __nolibc_syscall3(__NR_readv, fd, iovec, count); 25 25 } 26 26 27 27 static __attribute__((unused)) 28 28 ssize_t readv(int fd, const struct iovec *iovec, int count) 29 29 { 30 - return __sysret(sys_readv(fd, iovec, count)); 30 + return __sysret(_sys_readv(fd, iovec, count)); 31 31 } 32 32 33 33 /* 34 34 * ssize_t writev(int fd, const struct iovec *iovec, int count); 35 35 */ 36 36 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) 38 38 { 39 - return my_syscall3(__NR_writev, fd, iovec, count); 39 + return __nolibc_syscall3(__NR_writev, fd, iovec, count); 40 40 } 41 41 42 42 static __attribute__((unused)) 43 43 ssize_t writev(int fd, const struct iovec *iovec, int count) 44 44 { 45 - return __sysret(sys_writev(fd, iovec, count)); 45 + return __sysret(_sys_writev(fd, iovec, count)); 46 46 } 47 47 48 48
+3 -3
tools/include/nolibc/sys/utsname.h
··· 28 28 }; 29 29 30 30 static __attribute__((unused)) 31 - int sys_uname(struct utsname *buf) 31 + int _sys_uname(struct utsname *buf) 32 32 { 33 - return my_syscall1(__NR_uname, buf); 33 + return __nolibc_syscall1(__NR_uname, buf); 34 34 } 35 35 36 36 static __attribute__((unused)) 37 37 int uname(struct utsname *buf) 38 38 { 39 - return __sysret(sys_uname(buf)); 39 + return __sysret(_sys_uname(buf)); 40 40 } 41 41 42 42 #endif /* _NOLIBC_SYS_UTSNAME_H */
+3 -3
tools/include/nolibc/sys/wait.h
··· 21 21 */ 22 22 23 23 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) 25 25 { 26 - return my_syscall5(__NR_waitid, which, pid, infop, options, rusage); 26 + return __nolibc_syscall5(__NR_waitid, which, pid, infop, options, rusage); 27 27 } 28 28 29 29 static __attribute__((unused)) 30 30 int waitid(int which, pid_t pid, siginfo_t *infop, int options) 31 31 { 32 - return __sysret(sys_waitid(which, pid, infop, options, NULL)); 32 + return __sysret(_sys_waitid(which, pid, infop, options, NULL)); 33 33 } 34 34 35 35
+34 -34
tools/include/nolibc/time.h
··· 33 33 */ 34 34 35 35 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) 37 37 { 38 38 #if defined(__NR_clock_getres_time64) 39 39 __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); 41 41 #else 42 42 __nolibc_assert_native_time64(); 43 - return my_syscall2(__NR_clock_getres, clockid, res); 43 + return __nolibc_syscall2(__NR_clock_getres, clockid, res); 44 44 #endif 45 45 } 46 46 47 47 static __attribute__((unused)) 48 48 int clock_getres(clockid_t clockid, struct timespec *res) 49 49 { 50 - return __sysret(sys_clock_getres(clockid, res)); 50 + return __sysret(_sys_clock_getres(clockid, res)); 51 51 } 52 52 53 53 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) 55 55 { 56 56 #if defined(__NR_clock_gettime64) 57 57 __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); 59 59 #else 60 60 __nolibc_assert_native_time64(); 61 - return my_syscall2(__NR_clock_gettime, clockid, tp); 61 + return __nolibc_syscall2(__NR_clock_gettime, clockid, tp); 62 62 #endif 63 63 } 64 64 65 65 static __attribute__((unused)) 66 66 int clock_gettime(clockid_t clockid, struct timespec *tp) 67 67 { 68 - return __sysret(sys_clock_gettime(clockid, tp)); 68 + return __sysret(_sys_clock_gettime(clockid, tp)); 69 69 } 70 70 71 71 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) 73 73 { 74 74 #if defined(__NR_clock_settime64) 75 75 __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); 77 77 #else 78 78 __nolibc_assert_native_time64(); 79 - return my_syscall2(__NR_clock_settime, clockid, tp); 79 + return __nolibc_syscall2(__NR_clock_settime, clockid, tp); 80 80 #endif 81 81 } 82 82 83 83 static __attribute__((unused)) 84 84 int clock_settime(clockid_t clockid, struct timespec *tp) 85 85 { 86 - return __sysret(sys_clock_settime(clockid, tp)); 86 + return __sysret(_sys_clock_settime(clockid, tp)); 87 87 } 88 88 89 89 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) 92 92 { 93 93 #if defined(__NR_clock_nanosleep_time64) 94 94 __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); 96 96 #else 97 97 __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); 99 99 #endif 100 100 } 101 101 ··· 104 104 struct timespec *rmtp) 105 105 { 106 106 /* Directly return a positive error number */ 107 - return -sys_clock_nanosleep(clockid, flags, rqtp, rmtp); 107 + return -_sys_clock_nanosleep(clockid, flags, rqtp, rmtp); 108 108 } 109 109 110 110 static __inline__ ··· 116 116 static __inline__ 117 117 int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) 118 118 { 119 - return __sysret(sys_clock_nanosleep(CLOCK_REALTIME, 0, rqtp, rmtp)); 119 + return __sysret(_sys_clock_nanosleep(CLOCK_REALTIME, 0, rqtp, rmtp)); 120 120 } 121 121 122 122 ··· 126 126 struct timeval tv; 127 127 128 128 /* note, cannot fail here */ 129 - sys_gettimeofday(&tv, NULL); 129 + _sys_gettimeofday(&tv, NULL); 130 130 131 131 if (tptr) 132 132 *tptr = tv.tv_sec; ··· 141 141 */ 142 142 143 143 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) 145 145 { 146 - return my_syscall3(__NR_timer_create, clockid, evp, timerid); 146 + return __nolibc_syscall3(__NR_timer_create, clockid, evp, timerid); 147 147 } 148 148 149 149 static __attribute__((unused)) 150 150 int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid) 151 151 { 152 - return __sysret(sys_timer_create(clockid, evp, timerid)); 152 + return __sysret(_sys_timer_create(clockid, evp, timerid)); 153 153 } 154 154 155 155 static __attribute__((unused)) 156 - int sys_timer_delete(timer_t timerid) 156 + int _sys_timer_delete(timer_t timerid) 157 157 { 158 - return my_syscall1(__NR_timer_delete, timerid); 158 + return __nolibc_syscall1(__NR_timer_delete, timerid); 159 159 } 160 160 161 161 static __attribute__((unused)) 162 162 int timer_delete(timer_t timerid) 163 163 { 164 - return __sysret(sys_timer_delete(timerid)); 164 + return __sysret(_sys_timer_delete(timerid)); 165 165 } 166 166 167 167 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) 169 169 { 170 170 #if defined(__NR_timer_gettime64) 171 171 __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); 173 173 #else 174 174 __nolibc_assert_native_time64(); 175 - return my_syscall2(__NR_timer_gettime, timerid, curr_value); 175 + return __nolibc_syscall2(__NR_timer_gettime, timerid, curr_value); 176 176 #endif 177 177 } 178 178 179 179 static __attribute__((unused)) 180 180 int timer_gettime(timer_t timerid, struct itimerspec *curr_value) 181 181 { 182 - return __sysret(sys_timer_gettime(timerid, curr_value)); 182 + return __sysret(_sys_timer_gettime(timerid, curr_value)); 183 183 } 184 184 185 185 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) 188 188 { 189 189 #if defined(__NR_timer_settime64) 190 190 __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); 192 192 #else 193 193 __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); 195 195 #endif 196 196 } 197 197 ··· 199 199 int timer_settime(timer_t timerid, int flags, 200 200 const struct itimerspec *new_value, struct itimerspec *old_value) 201 201 { 202 - return __sysret(sys_timer_settime(timerid, flags, new_value, old_value)); 202 + return __sysret(_sys_timer_settime(timerid, flags, new_value, old_value)); 203 203 } 204 204 205 205 #endif /* _NOLIBC_TIME_H */
+6 -6
tools/include/nolibc/unistd.h
··· 31 31 */ 32 32 33 33 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) 35 35 { 36 - return my_syscall4(__NR_faccessat, fd, path, amode, flag); 36 + return __nolibc_syscall4(__NR_faccessat, fd, path, amode, flag); 37 37 } 38 38 39 39 static __attribute__((unused)) 40 40 int faccessat(int fd, const char *path, int amode, int flag) 41 41 { 42 - return __sysret(sys_faccessat(fd, path, amode, flag)); 42 + return __sysret(_sys_faccessat(fd, path, amode, flag)); 43 43 } 44 44 45 45 static __attribute__((unused)) ··· 54 54 { 55 55 struct timeval my_timeval = { msecs / 1000, (msecs % 1000) * 1000 }; 56 56 57 - if (sys_select(0, NULL, NULL, NULL, &my_timeval) < 0) 57 + if (_sys_select(0, NULL, NULL, NULL, &my_timeval) < 0) 58 58 return (my_timeval.tv_sec * 1000) + 59 59 (my_timeval.tv_usec / 1000) + 60 60 !!(my_timeval.tv_usec % 1000); ··· 67 67 { 68 68 struct timeval my_timeval = { seconds, 0 }; 69 69 70 - if (sys_select(0, NULL, NULL, NULL, &my_timeval) < 0) 70 + if (_sys_select(0, NULL, NULL, NULL, &my_timeval) < 0) 71 71 return my_timeval.tv_sec + !!my_timeval.tv_usec; 72 72 else 73 73 return 0; ··· 78 78 { 79 79 struct timeval my_timeval = { usecs / 1000000, usecs % 1000000 }; 80 80 81 - return sys_select(0, NULL, NULL, NULL, &my_timeval); 81 + return _sys_select(0, NULL, NULL, NULL, &my_timeval); 82 82 } 83 83 84 84 static __attribute__((unused))
+2 -2
tools/testing/selftests/nolibc/Makefile
··· 13 13 -isystem $(top_srcdir)/tools/include/nolibc -isystem $(top_srcdir)/usr/include \ 14 14 $(CFLAGS_NOLIBC_TEST) 15 15 $(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 17 17 18 - $(OUTPUT)/libc-test: nolibc-test.c nolibc-test-linkage.c 18 + $(OUTPUT)/libc-test: $(NOLIBC_TEST_SOURCES) 19 19 $(call msg,CC,,$@) 20 20 $(Q)$(LINK.c) $^ -o $@ 21 21
+4 -1
tools/testing/selftests/nolibc/Makefile.include
··· 5 5 echo 'void foo(void) {}' | $(CC) -x c - -o - -S $(CLANG_CROSS_FLAGS) $(__CFLAGS_STACKPROTECTOR) | grep -q __stack_chk_guard, \ 6 6 $(__CFLAGS_STACKPROTECTOR)) 7 7 _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 \ 9 10 $(call cc-option,-fno-stack-protector) $(call cc-option,-Wmissing-prototypes) \ 10 11 $(_CFLAGS_STACKPROTECTOR) $(_CFLAGS_SANITIZER) 12 + 13 + NOLIBC_TEST_SOURCES := nolibc-test.c nolibc-test-linkage.c nolibc-test-ignore-errno.c
+7 -16
tools/testing/selftests/nolibc/Makefile.nolibc
··· 232 232 CFLAGS_XARCH = $(CFLAGS_$(XARCH)) 233 233 endif 234 234 235 + LDLIBS_ppc = $(if $(LLVM),,-lgcc) 236 + LDLIBS = $(LDLIBS_$(XARCH)) 237 + 235 238 include Makefile.include 236 239 237 240 CFLAGS ?= $(CFLAGS_NOLIBC_TEST) $(CFLAGS_XARCH) $(CFLAGS_EXTRA) 238 241 LDFLAGS := 239 - 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 242 252 243 # Modify CFLAGS based on LLVM= 253 244 include $(srctree)/tools/scripts/Makefile.include ··· 293 302 $(Q)$(MAKE) -C $(srctree)/tools/include/nolibc ARCH=$(ARCH) OUTPUT=$(CURDIR)/sysroot/ headers_standalone headers_check 294 303 $(Q)mv sysroot/sysroot sysroot/$(ARCH) 295 304 296 - nolibc-test: nolibc-test.c nolibc-test-linkage.c sysroot/$(ARCH)/include 305 + nolibc-test: $(NOLIBC_TEST_SOURCES) sysroot/$(ARCH)/include 297 306 $(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \ 298 - -nostdlib -nostdinc -static -Isysroot/$(ARCH)/include nolibc-test.c nolibc-test-linkage.c $(LIBGCC) 307 + -nostdlib -nostdinc -static -Isysroot/$(ARCH)/include $(NOLIBC_TEST_SOURCES) $(LDLIBS) 299 308 300 - libc-test: nolibc-test.c nolibc-test-linkage.c 301 - $(QUIET_CC)$(HOSTCC) -o $@ nolibc-test.c nolibc-test-linkage.c 309 + libc-test: $(NOLIBC_TEST_SOURCES) 310 + $(QUIET_CC)$(HOSTCC) -o $@ $(NOLIBC_TEST_SOURCES) 302 311 303 312 # local libc-test 304 313 run-libc-test: libc-test
+6
tools/testing/selftests/nolibc/nolibc-test-ignore-errno.c
··· 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>
+259 -78
tools/testing/selftests/nolibc/nolibc-test.c
··· 42 42 #include <unistd.h> 43 43 #include <limits.h> 44 44 #include <ctype.h> 45 + #include <stdbool.h> 46 + #include <byteswap.h> 47 + #include <endian.h> 45 48 46 49 #pragma GCC diagnostic ignored "-Wmissing-prototypes" 47 50 ··· 69 66 /* will be used by constructor tests */ 70 67 static int constructor_test_value; 71 68 69 + static const int is_le = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__; 70 + 72 71 static const int is_nolibc = 73 72 #ifdef NOLIBC 74 73 1 ··· 78 73 0 79 74 #endif 80 75 ; 76 + 77 + static const int is_glibc = 78 + #ifdef __GLIBC__ 79 + 1 80 + #else 81 + 0 82 + #endif 83 + ; 84 + 85 + #if !defined(NOLIBC) 86 + /* Some disabled tests may not compile. */ 87 + 88 + /* strlcat() and strlcpy() may not be in the system headers. */ 89 + #undef strlcat 90 + #undef strlcpy 91 + #define strlcat(d, s, l) 0 92 + #define strlcpy(d, s, l) 0 93 + 94 + /* readdir_r() is likely to be marked deprecated */ 95 + #undef readdir_r 96 + #define readdir_r(dir, dirent, result) ((errno = EINVAL), -1) 97 + 98 + #define _syscall(...) 0 99 + #endif 81 100 82 101 /* definition of a series of tests */ 83 102 struct test { ··· 171 142 } 172 143 } 173 144 174 - static void align_result(size_t llen) 175 - { 176 - const size_t align = 64; 177 - char buf[align]; 178 - size_t n; 179 - 180 - if (llen >= align) 181 - return; 182 - 183 - n = align - llen; 184 - memset(buf, ' ', n); 185 - buf[n] = '\0'; 186 - fputs(buf, stdout); 187 - } 188 - 189 145 enum RESULT { 190 146 OK, 191 147 FAIL, ··· 188 174 else 189 175 msg = " [FAIL]"; 190 176 191 - align_result(llen); 192 - puts(msg); 177 + llen = 64 - llen; 178 + if (llen < 0) 179 + llen = 0; 180 + printf("%*s%s\n", llen, "", msg); 193 181 } 194 182 195 183 /* The tests below are intended to be used by the macroes, which evaluate ··· 320 304 { 321 305 int ret = 0; 322 306 323 - if (errno == ENOSYS) { 324 - llen += printf(" = ENOSYS"); 325 - result(llen, SKIPPED); 326 - } else if (expr) { 307 + if (expr) { 327 308 ret = 1; 328 309 llen += printf(" = %d %s ", expr, errorname(errno)); 329 310 result(llen, FAIL); ··· 360 347 { 361 348 int ret = 0; 362 349 363 - if (errno == ENOSYS) { 364 - llen += printf(" = ENOSYS"); 365 - result(llen, SKIPPED); 366 - } else if (expr == val) { 350 + if (expr == val) { 367 351 ret = 1; 368 352 llen += printf(" = %d %s ", expr, errorname(errno)); 369 353 result(llen, FAIL); ··· 385 375 int _errno = errno; 386 376 387 377 llen += printf(" = %d %s ", expr, errorname(_errno)); 388 - if (errno == ENOSYS) { 389 - result(llen, SKIPPED); 390 - } else if (expr != expret || (_errno != experr1 && _errno != experr2)) { 378 + if (expr != expret || (_errno != experr1 && _errno != experr2)) { 391 379 ret = 1; 392 380 if (experr2 == 0) 393 381 llen += printf(" != (%d %s) ", expret, errorname(experr1)); ··· 709 701 constructor_test_value |= 1 << 1; 710 702 } 711 703 704 + int test_program_invocation_name(void) 705 + { 706 + char buf[100]; 707 + char *dirsep; 708 + ssize_t r; 709 + int fd; 710 + 711 + fd = open("/proc/self/cmdline", O_RDONLY); 712 + if (fd == -1) 713 + return 1; 714 + 715 + r = read(fd, buf, sizeof(buf)); 716 + close(fd); 717 + if (r < 1 || r == sizeof(buf)) 718 + return 1; 719 + 720 + buf[r - 1] = '\0'; 721 + 722 + if (strcmp(program_invocation_name, buf) != 0) 723 + return 1; 724 + 725 + dirsep = strrchr(buf, '/'); 726 + if (!dirsep || dirsep[1] == '\0') 727 + return 1; 728 + 729 + if (strcmp(program_invocation_short_name, dirsep + 1) != 0) 730 + return 1; 731 + 732 + return 0; 733 + } 734 + 712 735 int run_startup(int min, int max) 713 736 { 714 737 int test; ··· 754 715 #ifdef NOLIBC 755 716 test_auxv = _auxv; 756 717 #endif 718 + bool proc = access("/proc", R_OK) == 0; 757 719 758 720 for (test = min; test >= 0 && test <= max; test++) { 759 721 int llen = 0; /* line length */ ··· 780 740 CASE_TEST(constructor); EXPECT_EQ(is_nolibc, constructor_test_value, 0x3); break; 781 741 CASE_TEST(linkage_errno); EXPECT_PTREQ(1, linkage_test_errno_addr(), &errno); break; 782 742 CASE_TEST(linkage_constr); EXPECT_EQ(1, linkage_test_constructor_test_value, 0x3); break; 743 + CASE_TEST(prog_name); EXPECT_ZR(proc, test_program_invocation_name()); break; 783 744 case __LINE__: 784 745 return ret; /* must be last */ 785 746 /* note: do not set any defaults so as to permit holes above */ ··· 907 866 908 867 errno = 0; 909 868 r = fwrite("foo", 1, 3, f); 910 - if (r != 0 || errno != EBADF) { 869 + if (r != 0 || ((is_nolibc || is_glibc) && errno != EBADF)) { 911 870 fclose(f); 912 871 return -1; 913 872 } ··· 1449 1408 CASE_TEST(fork); EXPECT_SYSZR(1, test_fork(FORK_STANDARD)); break; 1450 1409 CASE_TEST(getdents64_root); EXPECT_SYSNE(1, test_getdents64("/"), -1); break; 1451 1410 CASE_TEST(getdents64_null); EXPECT_SYSER(1, test_getdents64("/dev/null"), -1, ENOTDIR); break; 1452 - CASE_TEST(directories); EXPECT_SYSZR(proc, test_dirent()); break; 1411 + CASE_TEST(directories); EXPECT_SYSZR(is_nolibc && proc, test_dirent()); break; 1453 1412 CASE_TEST(getrandom); EXPECT_SYSZR(1, test_getrandom()); break; 1454 1413 CASE_TEST(gettimeofday_tv); EXPECT_SYSZR(1, gettimeofday(&tv, NULL)); break; 1455 1414 CASE_TEST(gettimeofday_tv_tz);EXPECT_SYSZR(1, gettimeofday(&tv, &tz)); break; ··· 1483 1442 CASE_TEST(select_fault); EXPECT_SYSER(1, select(1, (void *)1, NULL, NULL, 0), -1, EFAULT); break; 1484 1443 CASE_TEST(stat_blah); EXPECT_SYSER(1, stat("/proc/self/blah", &stat_buf), -1, ENOENT); break; 1485 1444 CASE_TEST(stat_fault); EXPECT_SYSER(1, stat(NULL, &stat_buf), -1, EFAULT); break; 1445 + CASE_TEST(stat_rdev); EXPECT_SYSZR(1, ({ int ret = stat("/dev/null", &stat_buf); ret ?: stat_buf.st_rdev != makedev(1, 3); })); break; 1486 1446 CASE_TEST(stat_timestamps); EXPECT_SYSZR(1, test_stat_timestamps()); break; 1487 1447 CASE_TEST(symlink_root); EXPECT_SYSER(1, symlink("/", "/"), -1, EEXIST); break; 1488 1448 CASE_TEST(timer); EXPECT_SYSZR(1, test_timer()); break; ··· 1502 1460 CASE_TEST(readv_zero); EXPECT_SYSZR(1, readv(0, NULL, 0)); break; 1503 1461 CASE_TEST(writev_badf); EXPECT_SYSER(1, writev(-1, &iov_one, 1), -1, EBADF); break; 1504 1462 CASE_TEST(writev_zero); EXPECT_SYSZR(1, writev(1, NULL, 0)); break; 1505 - CASE_TEST(ptrace); EXPECT_SYSER(1, ptrace(PTRACE_CONT, getpid(), NULL, NULL), -1, ESRCH); break; 1463 + CASE_TEST(ptrace); tmp = ptrace(PTRACE_CONT, getpid(), NULL, NULL); EXPECT_SYSER(tmp != -1 && errno != ENOSYS, tmp, -1, EFAULT); break; 1506 1464 CASE_TEST(syscall_noargs); EXPECT_SYSEQ(1, syscall(__NR_getpid), getpid()); break; 1507 1465 CASE_TEST(syscall_args); EXPECT_SYSER(1, syscall(__NR_statx, 0, NULL, 0, 0, NULL), -1, EFAULT); break; 1466 + CASE_TEST(_syscall_noargs); EXPECT_SYSEQ(is_nolibc, _syscall(__NR_getpid), getpid()); break; 1467 + CASE_TEST(_syscall_args); EXPECT_SYSEQ(is_nolibc, _syscall(__NR_statx, 0, NULL, 0, 0, NULL), -EFAULT); break; 1508 1468 CASE_TEST(namespace); EXPECT_SYSZR(euid0 && proc, test_namespace()); break; 1509 1469 case __LINE__: 1510 1470 return ret; /* must be last */ ··· 1552 1508 return 1; 1553 1509 #endif /* NOLIBC */ 1554 1510 1511 + return 0; 1512 + } 1513 + 1514 + int test_malloc(void) 1515 + { 1516 + size_t sz_array1, sz_array2, sz_array3; 1517 + int *array1, *array2, *array3; 1518 + int pagesize = getpagesize(); 1519 + size_t idx; 1520 + 1521 + if (pagesize < 0) 1522 + return 1; 1523 + 1524 + /* Dependent on the page size, as that is the granularity of our allocator. */ 1525 + sz_array1 = pagesize / 2; 1526 + array1 = malloc(sz_array1 * sizeof(*array1)); 1527 + if (!array1) 1528 + return 2; 1529 + 1530 + for (idx = 0; idx < sz_array1; idx++) 1531 + array1[idx] = idx; 1532 + 1533 + sz_array2 = pagesize * 2; 1534 + array2 = calloc(sz_array2, sizeof(*array2)); 1535 + if (!array2) { 1536 + free(array1); 1537 + return 3; 1538 + } 1539 + 1540 + for (idx = 0; idx < sz_array2; idx++) { 1541 + if (array2[idx] != 0) { 1542 + free(array2); 1543 + return 4; 1544 + } 1545 + array2[idx] = idx + sz_array1; 1546 + } 1547 + 1548 + /* Resize array1 into array3 and append array2 at the end. */ 1549 + sz_array3 = sz_array1 + sz_array2; 1550 + array3 = realloc(array1, sz_array3 * sizeof(*array3)); 1551 + if (!array3) { 1552 + free(array2); 1553 + free(array1); 1554 + return 5; 1555 + } 1556 + memcpy(array3 + sz_array1, array2, sizeof(*array2) * sz_array2); 1557 + free(array2); 1558 + 1559 + /* The contents must be contiguous now. */ 1560 + for (idx = 0; idx < sz_array3; idx++) 1561 + if (array3[idx] != (int)idx) 1562 + return 6; 1563 + 1564 + free(array3); 1555 1565 return 0; 1556 1566 } 1557 1567 ··· 1735 1637 CASE_TEST(memchr_foobar6_o); EXPECT_STREQ(1, memchr("foobar", 'o', 6), "oobar"); break; 1736 1638 CASE_TEST(memchr_foobar3_b); EXPECT_STRZR(1, memchr("foobar", 'b', 3)); break; 1737 1639 CASE_TEST(time_types); EXPECT_ZR(is_nolibc, test_time_types()); break; 1640 + CASE_TEST(makedev); EXPECT_EQ(1, makedev(0x12, 0x34), 0x1234); break; 1641 + CASE_TEST(major); EXPECT_EQ(1, major(0x1234), 0x12); break; 1642 + CASE_TEST(minor); EXPECT_EQ(1, minor(0x1234), 0x34); break; 1643 + CASE_TEST(makedev_big); EXPECT_EQ(1, makedev(0x11223344, 0x55667788), 0x1122355667734488); break; 1644 + CASE_TEST(major_big); EXPECT_EQ(1, major(0x1122355667734488), 0x11223344); break; 1645 + CASE_TEST(minor_big); EXPECT_EQ(1, minor(0x1122355667734488), 0x55667788); break; 1646 + CASE_TEST(malloc); EXPECT_ZR(1, test_malloc()); break; 1647 + CASE_TEST(bswap_16); EXPECT_EQ(1, bswap_16(0x0123), 0x2301); break; 1648 + CASE_TEST(bswap_32); EXPECT_EQ(1, bswap_32(0x01234567), 0x67452301); break; 1649 + CASE_TEST(bswap_64); EXPECT_EQ(1, bswap_64(0x0123456789abcdef), 0xefcdab8967452301); break; 1650 + CASE_TEST(htobe16); EXPECT_EQ(1, htobe16(is_le ? 0x0123 : 0x2301), 0x2301); break; 1651 + CASE_TEST(htole16); EXPECT_EQ(1, htole16(is_le ? 0x0123 : 0x2301), 0x0123); break; 1652 + CASE_TEST(htobe32); EXPECT_EQ(1, htobe32(is_le ? 0x01234567 : 0x67452301), 0x67452301); break; 1653 + CASE_TEST(htole32); EXPECT_EQ(1, htole32(is_le ? 0x01234567 : 0x67452301), 0x01234567); break; 1654 + CASE_TEST(htobe64); EXPECT_EQ(1, htobe64(is_le ? 0x0123456789000000 : 0x8967452301), 0x8967452301); break; 1655 + CASE_TEST(htole64); EXPECT_EQ(1, htole64(is_le ? 0x0123456789 : 0x8967452301000000), 0x0123456789); break; 1738 1656 1739 1657 case __LINE__: 1740 1658 return ret; /* must be last */ ··· 1760 1646 return ret; 1761 1647 } 1762 1648 1763 - #define EXPECT_VFPRINTF(c, expected, fmt, ...) \ 1764 - ret += expect_vfprintf(llen, c, expected, fmt, ##__VA_ARGS__) 1649 + #define EXPECT_VFPRINTF(cond, expected, fmt, ...) \ 1650 + do { if (!(cond)) result(llen, SKIPPED); else ret += expect_vfprintf(llen, expected, fmt, ##__VA_ARGS__); } while (0) 1765 1651 1766 - static int expect_vfprintf(int llen, int c, const char *expected, const char *fmt, ...) 1652 + #define VFPRINTF_LEN 25 1653 + static int expect_vfprintf(int llen, const char *expected, const char *fmt, ...) 1767 1654 { 1768 - char buf[100]; 1655 + char buf[VFPRINTF_LEN + 80]; 1656 + unsigned int cmp_len; 1769 1657 va_list args; 1770 - ssize_t w; 1771 - int ret; 1658 + ssize_t written, expected_len; 1772 1659 1660 + /* Fill and terminate buf[] to check for overlong/absent writes */ 1661 + memset(buf, 0xa5, sizeof(buf) - 1); 1662 + buf[sizeof(buf) - 1] = 0; 1773 1663 1774 1664 va_start(args, fmt); 1775 - /* Only allow writing 21 bytes, to test truncation */ 1776 - w = vsnprintf(buf, 21, fmt, args); 1665 + /* Limit buffer length to test truncation */ 1666 + written = vsnprintf(buf, VFPRINTF_LEN + 1, fmt, args); 1777 1667 va_end(args); 1778 1668 1779 - if (w != c) { 1780 - llen += printf(" written(%d) != %d", (int)w, c); 1669 + llen += printf(" \"%s\"", buf); 1670 + 1671 + expected_len = strlen(expected); 1672 + if (expected_len > VFPRINTF_LEN) { 1673 + /* Indicate truncated in test output */ 1674 + llen += printf("+"); 1675 + cmp_len = VFPRINTF_LEN; 1676 + } else { 1677 + cmp_len = expected_len; 1678 + } 1679 + 1680 + if (memcmp(expected, buf, cmp_len) || buf[cmp_len]) { 1681 + llen += printf(" should be \"%.*s\"", VFPRINTF_LEN, expected); 1781 1682 result(llen, FAIL); 1782 1683 return 1; 1783 1684 } 1784 1685 1785 - llen += printf(" \"%s\" = \"%s\"", expected, buf); 1786 - ret = strncmp(expected, buf, c); 1686 + if (written != expected_len) { 1687 + llen += printf(" written(%d) != %d", (int)written, (int)expected_len); 1688 + result(llen, FAIL); 1689 + return 1; 1690 + } 1787 1691 1788 - result(llen, ret ? FAIL : OK); 1789 - return ret; 1692 + /* Check for any overwrites after the actual data. */ 1693 + while (++cmp_len < sizeof(buf) - 1) { 1694 + if ((unsigned char)buf[cmp_len] != 0xa5) { 1695 + llen += printf(" overwrote buf[%d] with 0x%x", cmp_len, buf[cmp_len]); 1696 + result(llen, FAIL); 1697 + return 1; 1698 + } 1699 + } 1700 + 1701 + result(llen, OK); 1702 + return 0; 1790 1703 } 1791 1704 1792 1705 static int test_scanf(void) ··· 1883 1742 return 0; 1884 1743 } 1885 1744 1886 - int test_strerror(void) 1887 - { 1888 - char buf[100]; 1889 - ssize_t ret; 1890 - 1891 - memset(buf, 'A', sizeof(buf)); 1892 - 1893 - errno = EINVAL; 1894 - ret = snprintf(buf, sizeof(buf), "%m"); 1895 - if (is_nolibc) { 1896 - if (ret < 6 || memcmp(buf, "errno=", 6)) 1897 - return 1; 1898 - } 1899 - 1900 - return 0; 1901 - } 1902 - 1903 1745 static int test_printf_error(void) 1904 1746 { 1905 1747 int fd, ret, saved_errno; ··· 1905 1781 return 0; 1906 1782 } 1907 1783 1784 + int test_asprintf(void) 1785 + { 1786 + char *str; 1787 + int ret; 1788 + 1789 + ret = asprintf(&str, "foo%s", "bar"); 1790 + if (ret == -1) 1791 + return 1; 1792 + 1793 + if (ret != 6) { 1794 + free(str); 1795 + return 2; 1796 + } 1797 + 1798 + if (memcmp(str, "foobar", 6) != 0) { 1799 + free(str); 1800 + return 3; 1801 + } 1802 + 1803 + free(str); 1804 + return 0; 1805 + } 1806 + 1908 1807 static int run_printf(int min, int max) 1909 1808 { 1910 1809 int test; ··· 1940 1793 * test numbers. 1941 1794 */ 1942 1795 switch (test + __LINE__ + 1) { 1943 - CASE_TEST(empty); EXPECT_VFPRINTF(0, "", ""); break; 1944 - CASE_TEST(simple); EXPECT_VFPRINTF(3, "foo", "foo"); break; 1945 - CASE_TEST(string); EXPECT_VFPRINTF(3, "foo", "%s", "foo"); break; 1946 - CASE_TEST(number); EXPECT_VFPRINTF(4, "1234", "%d", 1234); break; 1947 - CASE_TEST(negnumber); EXPECT_VFPRINTF(5, "-1234", "%d", -1234); break; 1948 - CASE_TEST(unsigned); EXPECT_VFPRINTF(5, "12345", "%u", 12345); break; 1949 - CASE_TEST(char); EXPECT_VFPRINTF(1, "c", "%c", 'c'); break; 1950 - CASE_TEST(hex); EXPECT_VFPRINTF(1, "f", "%x", 0xf); break; 1951 - CASE_TEST(pointer); EXPECT_VFPRINTF(3, "0x1", "%p", (void *) 0x1); break; 1952 - CASE_TEST(uintmax_t); EXPECT_VFPRINTF(20, "18446744073709551615", "%ju", 0xffffffffffffffffULL); break; 1953 - CASE_TEST(intmax_t); EXPECT_VFPRINTF(20, "-9223372036854775807", "%jd", 0x8000000000000001LL); break; 1954 - CASE_TEST(truncation); EXPECT_VFPRINTF(25, "01234567890123456789", "%s", "0123456789012345678901234"); break; 1955 - CASE_TEST(string_width); EXPECT_VFPRINTF(10, " 1", "%10s", "1"); break; 1956 - CASE_TEST(number_width); EXPECT_VFPRINTF(10, " 1", "%10d", 1); break; 1957 - CASE_TEST(width_trunc); EXPECT_VFPRINTF(25, " ", "%25d", 1); break; 1796 + CASE_TEST(empty); EXPECT_VFPRINTF(1, "", ""); break; 1797 + CASE_TEST(simple); EXPECT_VFPRINTF(1, "foo", "foo"); break; 1798 + CASE_TEST(string); EXPECT_VFPRINTF(1, "foo", "%s", "foo"); break; 1799 + CASE_TEST(number); EXPECT_VFPRINTF(1, "1234", "%d", 1234); break; 1800 + CASE_TEST(negnumber); EXPECT_VFPRINTF(1, "-1234", "%d", -1234); break; 1801 + CASE_TEST(num_sign); EXPECT_VFPRINTF(1, "| 1|+2|+3|+4|5|", "|% d|%+d|% +d|%+ d|%#d|", 1, 2, 3, 4, 5); break; 1802 + CASE_TEST(unsigned); EXPECT_VFPRINTF(1, "12345", "%u", 12345); break; 1803 + CASE_TEST(signed_max); EXPECT_VFPRINTF(1, "2147483647", "%i", ~0u >> 1); break; 1804 + CASE_TEST(signed_min); EXPECT_VFPRINTF(1, "-2147483648", "%i", (~0u >> 1) + 1); break; 1805 + CASE_TEST(unsigned_max); EXPECT_VFPRINTF(1, "4294967295", "%u", ~0u); break; 1806 + CASE_TEST(char); EXPECT_VFPRINTF(1, "|c|d| e|", "|%c|%.0c|%4c|", 'c', 'd', 'e'); break; 1807 + CASE_TEST(octal); EXPECT_VFPRINTF(1, "|17| 0033||", "|%o|%6.4o|%.0o|", 017, 033, 0); break; 1808 + CASE_TEST(octal_max); EXPECT_VFPRINTF(1, "1777777777777777777777", "%llo", ~0ULL); break; 1809 + 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; 1810 + CASE_TEST(hex_nolibc); EXPECT_VFPRINTF(is_nolibc, "|f|d|", "|%x|%X|", 0xf, 0xd); break; 1811 + CASE_TEST(hex_libc); EXPECT_VFPRINTF(!is_nolibc, "|f|D|", "|%x|%X|", 0xf, 0xd); break; 1812 + CASE_TEST(hex_alt); EXPECT_VFPRINTF(1, "|0x1| 0x2| 0|", "|%#x|%#5x|%#5x|", 1, 2, 0); break; 1813 + CASE_TEST(hex_alt_prec); EXPECT_VFPRINTF(1, "| 0x02|0x03| 0x123|", "|%#5.2x|%#04x|%#6.2x|", 2, 3, 0x123); break; 1814 + CASE_TEST(hex_0_alt); EXPECT_VFPRINTF(1, "|0|0000| 00|", "|%#x|%#04x|%#5.2x|", 0, 0, 0); break; 1815 + CASE_TEST(pointer); EXPECT_VFPRINTF(1, "0x1", "%p", (void *) 0x1); break; 1816 + CASE_TEST(pointer_NULL); EXPECT_VFPRINTF(is_nolibc || is_glibc, "|(nil)|(nil)|", "|%p|%.4p|", (void *)0, (void *)0); break; 1817 + CASE_TEST(string_NULL); EXPECT_VFPRINTF(is_nolibc || is_glibc, "|(null)||(null)|", "|%s|%.5s|%.6s|", (void *)0, (void *)0, (void *)0); break; 1818 + CASE_TEST(percent); EXPECT_VFPRINTF(1, "a%d42%69%", "a%%d%d%%%d%%", 42, 69); break; 1819 + CASE_TEST(perc_qual); EXPECT_VFPRINTF(is_nolibc || is_glibc, "a%d2", "a%-14l%d%d", 2); break; 1820 + CASE_TEST(invalid); EXPECT_VFPRINTF(is_nolibc || is_glibc, "a%12yx3%y42%P", "a%12yx%d%y%d%P", 3, 42); break; 1821 + CASE_TEST(intmax_max); EXPECT_VFPRINTF(1, "9223372036854775807", "%lld", ~0ULL >> 1); break; 1822 + CASE_TEST(intmax_min); EXPECT_VFPRINTF(is_nolibc || is_glibc, "-9223372036854775808", "%Li", (~0ULL >> 1) + 1); break; 1823 + CASE_TEST(uintmax_max); EXPECT_VFPRINTF(1, "18446744073709551615", "%ju", ~0ULL); break; 1824 + CASE_TEST(truncation); EXPECT_VFPRINTF(1, "012345678901234567890123456789", "%s", "012345678901234567890123456789"); break; 1825 + CASE_TEST(string_width); EXPECT_VFPRINTF(1, " 1", "%10s", "1"); break; 1826 + CASE_TEST(string_trunc); EXPECT_VFPRINTF(1, " 12345", "%10.5s", "1234567890"); break; 1827 + 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; 1828 + CASE_TEST(number_width); EXPECT_VFPRINTF(1, " 1", "%10d", 1); break; 1829 + CASE_TEST(number_left); EXPECT_VFPRINTF(1, "|-5 |", "|%-8d|", -5); break; 1830 + CASE_TEST(string_align); EXPECT_VFPRINTF(1, "|foo |", "|%-8s|", "foo"); break; 1831 + CASE_TEST(width_trunc); EXPECT_VFPRINTF(1, " 1", "%30d", 1); break; 1832 + CASE_TEST(width_tr_lft); EXPECT_VFPRINTF(1, "1 ", "%-30d", 1); break; 1833 + CASE_TEST(number_pad); EXPECT_VFPRINTF(1, "0000000005", "%010d", 5); break; 1834 + CASE_TEST(number_pad); EXPECT_VFPRINTF(1, "|0000000005|0x1234|", "|%010d|%#01x|", 5, 0x1234); break; 1835 + CASE_TEST(num_pad_neg); EXPECT_VFPRINTF(1, "-000000005", "%010d", -5); break; 1836 + CASE_TEST(num_pad_hex); EXPECT_VFPRINTF(1, "00fffffffb", "%010x", -5); break; 1837 + CASE_TEST(num_pad_trunc);EXPECT_VFPRINTF(is_nolibc, " 0000000000000000000000000000005", "%035d", 5); break; /* max 31 '0' can be added */ 1838 + CASE_TEST(num_p_tr_libc);EXPECT_VFPRINTF(!is_nolibc, "00000000000000000000000000000000005", "%035d", 5); break; 1839 + CASE_TEST(number_prec); EXPECT_VFPRINTF(1, " 00005", "%10.5d", 5); break; 1840 + CASE_TEST(num_prec_neg); EXPECT_VFPRINTF(1, " -00005", "%10.5d", -5); break; 1841 + CASE_TEST(number_var); EXPECT_VFPRINTF(1, "| -00005|5 |", "|%*.*d|%*.*d|", 10, 5, -5, -2, -10, 5); break; 1842 + CASE_TEST(num_0_prec_0); EXPECT_VFPRINTF(1, "|| |+||||", "|%.0d|% .0d|%+.0d|%.0u|%.0x|%#.0x|", 0, 0, 0, 0, 0, 0); break; 1843 + CASE_TEST(errno); errno = 22; EXPECT_VFPRINTF(is_nolibc, "errno=22", "%m"); break; 1844 + CASE_TEST(errno-neg); errno = -22; EXPECT_VFPRINTF(is_nolibc, "errno=-22 ", "%-12m"); break; 1958 1845 CASE_TEST(scanf); EXPECT_ZR(1, test_scanf()); break; 1959 - CASE_TEST(strerror); EXPECT_ZR(1, test_strerror()); break; 1960 1846 CASE_TEST(printf_error); EXPECT_ZR(1, test_printf_error()); break; 1847 + CASE_TEST(asprintf); EXPECT_ZR(1, test_asprintf()); break; 1961 1848 case __LINE__: 1962 1849 return ret; /* must be last */ 1963 1850 /* note: do not set any defaults so as to permit holes above */
+1 -1
tools/testing/selftests/nolibc/run-tests.sh
··· 7 7 8 8 trap 'echo Aborting...' 'ERR' 9 9 10 - crosstool_version=13.2.0 10 + crosstool_version=15.2.0 11 11 hostarch=x86_64 12 12 nproc=$(( $(nproc) + 2)) 13 13 cache_dir="${XDG_CACHE_HOME:-"$HOME"/.cache}"