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

tools/nolibc: include nolibc.h early from all header files

Inclusion of any nolibc header file should also bring all other headers.
On the other hand it should also be possible to include any nolibc header
files
in any order.

Currently this is implemented by including the catch-all nolibc.h after the
headers own definitions.
This is problematic if one nolibc header depends on another one.
The first header has to include the other one before defining any symbols.
That in turn will include the rest of nolibc while the current header has
not defined anything yet. If any other part of nolibc depends on
definitions from the current header, errors are encountered.
This is already the case today. Effectively nolibc can only be included in
the order of nolibc.h.

Restructure the way "nolibc.h" is included.
Move it to the beginning of the header files and before the include guards.
Now any header will behave exactly like "nolibc.h" while the include
guards prevent any duplicate definitions.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Acked-by: Willy Tarreau <w@1wt.eu>
Link: https://lore.kernel.org/r/20250424-nolibc-header-check-v1-2-011576b6ed6f@linutronix.de

authored by

Thomas Weißschuh and committed by
Thomas Weißschuh
3785289f a6a054c8

+60 -63
+3 -3
tools/include/nolibc/ctype.h
··· 4 4 * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu> 5 5 */ 6 6 7 + /* make sure to include all global symbols */ 8 + #include "nolibc.h" 9 + 7 10 #ifndef _NOLIBC_CTYPE_H 8 11 #define _NOLIBC_CTYPE_H 9 12 ··· 98 95 { 99 96 return isgraph(c) && !isalnum(c); 100 97 } 101 - 102 - /* make sure to include all global symbols */ 103 - #include "nolibc.h" 104 98 105 99 #endif /* _NOLIBC_CTYPE_H */
+3 -3
tools/include/nolibc/dirent.h
··· 4 4 * Copyright (C) 2025 Thomas Weißschuh <linux@weissschuh.net> 5 5 */ 6 6 7 + /* make sure to include all global symbols */ 8 + #include "nolibc.h" 9 + 7 10 #ifndef _NOLIBC_DIRENT_H 8 11 #define _NOLIBC_DIRENT_H 9 12 ··· 96 93 *result = entry; 97 94 return 0; 98 95 } 99 - 100 - /* make sure to include all global symbols */ 101 - #include "nolibc.h" 102 96 103 97 #endif /* _NOLIBC_DIRENT_H */
+3 -3
tools/include/nolibc/elf.h
··· 4 4 * Copyright (C) 2025 Thomas Weißschuh <thomas.weissschuh@linutronix.de> 5 5 */ 6 6 7 + /* make sure to include all global symbols */ 8 + #include "nolibc.h" 9 + 7 10 #ifndef _NOLIBC_SYS_ELF_H 8 11 #define _NOLIBC_SYS_ELF_H 9 12 10 13 #include <linux/elf.h> 11 - 12 - /* make sure to include all global symbols */ 13 - #include "nolibc.h" 14 14 15 15 #endif /* _NOLIBC_SYS_ELF_H */
+3 -3
tools/include/nolibc/errno.h
··· 4 4 * Copyright (C) 2017-2022 Willy Tarreau <w@1wt.eu> 5 5 */ 6 6 7 + /* make sure to include all global symbols */ 8 + #include "nolibc.h" 9 + 7 10 #ifndef _NOLIBC_ERRNO_H 8 11 #define _NOLIBC_ERRNO_H 9 12 ··· 24 21 * because they all correspond to the highest addressable memory page. 25 22 */ 26 23 #define MAX_ERRNO 4095 27 - 28 - /* make sure to include all global symbols */ 29 - #include "nolibc.h" 30 24 31 25 #endif /* _NOLIBC_ERRNO_H */
+3 -3
tools/include/nolibc/fcntl.h
··· 4 4 * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu> 5 5 */ 6 6 7 + /* make sure to include all global symbols */ 8 + #include "nolibc.h" 9 + 7 10 #ifndef _NOLIBC_FCNTL_H 8 11 #define _NOLIBC_FCNTL_H 9 12 ··· 65 62 66 63 return __sysret(sys_open(path, flags, mode)); 67 64 } 68 - 69 - /* make sure to include all global symbols */ 70 - #include "nolibc.h" 71 65 72 66 #endif /* _NOLIBC_FCNTL_H */
+3 -3
tools/include/nolibc/getopt.h
··· 5 5 * Copyright (C) 2025 Thomas Weißschuh <linux@weissschuh.net> 6 6 */ 7 7 8 + /* make sure to include all global symbols */ 9 + #include "nolibc.h" 10 + 8 11 #ifndef _NOLIBC_GETOPT_H 9 12 #define _NOLIBC_GETOPT_H 10 13 ··· 97 94 } 98 95 return c; 99 96 } 100 - 101 - /* make sure to include all global symbols */ 102 - #include "nolibc.h" 103 97 104 98 #endif /* _NOLIBC_GETOPT_H */
+3 -3
tools/include/nolibc/signal.h
··· 4 4 * Copyright (C) 2017-2022 Willy Tarreau <w@1wt.eu> 5 5 */ 6 6 7 + /* make sure to include all global symbols */ 8 + #include "nolibc.h" 9 + 7 10 #ifndef _NOLIBC_SIGNAL_H 8 11 #define _NOLIBC_SIGNAL_H 9 12 ··· 22 19 { 23 20 return sys_kill(sys_getpid(), signal); 24 21 } 25 - 26 - /* make sure to include all global symbols */ 27 - #include "nolibc.h" 28 22 29 23 #endif /* _NOLIBC_SIGNAL_H */
+3 -3
tools/include/nolibc/stdio.h
··· 4 4 * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu> 5 5 */ 6 6 7 + /* make sure to include all global symbols */ 8 + #include "nolibc.h" 9 + 7 10 #ifndef _NOLIBC_STDIO_H 8 11 #define _NOLIBC_STDIO_H 9 12 ··· 600 597 601 598 return buf; 602 599 } 603 - 604 - /* make sure to include all global symbols */ 605 - #include "nolibc.h" 606 600 607 601 #endif /* _NOLIBC_STDIO_H */
+3 -3
tools/include/nolibc/stdlib.h
··· 4 4 * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu> 5 5 */ 6 6 7 + /* make sure to include all global symbols */ 8 + #include "nolibc.h" 9 + 7 10 #ifndef _NOLIBC_STDLIB_H 8 11 #define _NOLIBC_STDLIB_H 9 12 ··· 524 521 { 525 522 return __strtox(nptr, endptr, base, 0, UINTMAX_MAX); 526 523 } 527 - 528 - /* make sure to include all global symbols */ 529 - #include "nolibc.h" 530 524 531 525 #endif /* _NOLIBC_STDLIB_H */
+3 -4
tools/include/nolibc/string.h
··· 4 4 * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu> 5 5 */ 6 6 7 + /* make sure to include all global symbols */ 8 + #include "nolibc.h" 9 + 7 10 #ifndef _NOLIBC_STRING_H 8 11 #define _NOLIBC_STRING_H 9 12 ··· 307 304 return c - 'a' + 'A'; 308 305 return c; 309 306 } 310 - 311 - 312 - /* make sure to include all global symbols */ 313 - #include "nolibc.h" 314 307 315 308 #endif /* _NOLIBC_STRING_H */
+3 -3
tools/include/nolibc/sys.h
··· 4 4 * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu> 5 5 */ 6 6 7 + /* make sure to include all global symbols */ 8 + #include "nolibc.h" 9 + 7 10 #ifndef _NOLIBC_SYS_H 8 11 #define _NOLIBC_SYS_H 9 12 ··· 1121 1118 { 1122 1119 return __sysret(sys_memfd_create(name, flags)); 1123 1120 } 1124 - 1125 - /* make sure to include all global symbols */ 1126 - #include "nolibc.h" 1127 1121 1128 1122 #endif /* _NOLIBC_SYS_H */
+3 -3
tools/include/nolibc/sys/auxv.h
··· 4 4 * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu> 5 5 */ 6 6 7 + /* make sure to include all global symbols */ 8 + #include "../nolibc.h" 9 + 7 10 #ifndef _NOLIBC_SYS_AUXV_H 8 11 #define _NOLIBC_SYS_AUXV_H 9 12 ··· 37 34 38 35 return ret; 39 36 } 40 - 41 - /* make sure to include all global symbols */ 42 - #include "../nolibc.h" 43 37 44 38 #endif /* _NOLIBC_SYS_AUXV_H */
+3 -3
tools/include/nolibc/sys/mman.h
··· 4 4 * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu> 5 5 */ 6 6 7 + /* make sure to include all global symbols */ 8 + #include "../nolibc.h" 9 + 7 10 #ifndef _NOLIBC_SYS_MMAN_H 8 11 #define _NOLIBC_SYS_MMAN_H 9 12 ··· 59 56 { 60 57 return __sysret(sys_munmap(addr, length)); 61 58 } 62 - 63 - /* make sure to include all global symbols */ 64 - #include "../nolibc.h" 65 59 66 60 #endif /* _NOLIBC_SYS_MMAN_H */
+3 -4
tools/include/nolibc/sys/stat.h
··· 4 4 * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu> 5 5 */ 6 6 7 + /* make sure to include all global symbols */ 8 + #include "../nolibc.h" 9 + 7 10 #ifndef _NOLIBC_SYS_STAT_H 8 11 #define _NOLIBC_SYS_STAT_H 9 12 ··· 69 66 70 67 return 0; 71 68 } 72 - 73 - 74 - /* make sure to include all global symbols */ 75 - #include "../nolibc.h" 76 69 77 70 #endif /* _NOLIBC_SYS_STAT_H */
+3 -3
tools/include/nolibc/sys/syscall.h
··· 4 4 * Copyright (C) 2024 Thomas Weißschuh <linux@weissschuh.net> 5 5 */ 6 6 7 + /* make sure to include all global symbols */ 8 + #include "../nolibc.h" 9 + 7 10 #ifndef _NOLIBC_SYS_SYSCALL_H 8 11 #define _NOLIBC_SYS_SYSCALL_H 9 12 ··· 15 12 #define _syscall(N, ...) __sysret(my_syscall##N(__VA_ARGS__)) 16 13 #define _syscall_n(N, ...) _syscall(N, __VA_ARGS__) 17 14 #define syscall(...) _syscall_n(_syscall_narg(__VA_ARGS__), ##__VA_ARGS__) 18 - 19 - /* make sure to include all global symbols */ 20 - #include "../nolibc.h" 21 15 22 16 #endif /* _NOLIBC_SYS_SYSCALL_H */
+3 -3
tools/include/nolibc/sys/time.h
··· 4 4 * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu> 5 5 */ 6 6 7 + /* make sure to include all global symbols */ 8 + #include "../nolibc.h" 9 + 7 10 #ifndef _NOLIBC_SYS_TIME_H 8 11 #define _NOLIBC_SYS_TIME_H 9 12 ··· 32 29 { 33 30 return __sysret(sys_gettimeofday(tv, tz)); 34 31 } 35 - 36 - /* make sure to include all global symbols */ 37 - #include "../nolibc.h" 38 32 39 33 #endif /* _NOLIBC_SYS_TIME_H */
+3 -4
tools/include/nolibc/sys/wait.h
··· 4 4 * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu> 5 5 */ 6 6 7 + /* make sure to include all global symbols */ 8 + #include "../nolibc.h" 9 + 7 10 #ifndef _NOLIBC_SYS_WAIT_H 8 11 #define _NOLIBC_SYS_WAIT_H 9 12 ··· 112 109 113 110 return info.si_pid; 114 111 } 115 - 116 - 117 - /* make sure to include all global symbols */ 118 - #include "../nolibc.h" 119 112 120 113 #endif /* _NOLIBC_SYS_WAIT_H */
+3 -3
tools/include/nolibc/time.h
··· 4 4 * Copyright (C) 2017-2022 Willy Tarreau <w@1wt.eu> 5 5 */ 6 6 7 + /* make sure to include all global symbols */ 8 + #include "nolibc.h" 9 + 7 10 #ifndef _NOLIBC_TIME_H 8 11 #define _NOLIBC_TIME_H 9 12 ··· 27 24 *tptr = tv.tv_sec; 28 25 return tv.tv_sec; 29 26 } 30 - 31 - /* make sure to include all global symbols */ 32 - #include "nolibc.h" 33 27 34 28 #endif /* _NOLIBC_TIME_H */
+3 -3
tools/include/nolibc/types.h
··· 4 4 * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu> 5 5 */ 6 6 7 + /* make sure to include all global symbols */ 8 + #include "nolibc.h" 9 + 7 10 #ifndef _NOLIBC_TYPES_H 8 11 #define _NOLIBC_TYPES_H 9 12 ··· 216 213 (TYPE *)((char *) __FIELD_PTR - offsetof(TYPE, FIELD)); \ 217 214 }) 218 215 #endif 219 - 220 - /* make sure to include all global symbols */ 221 - #include "nolibc.h" 222 216 223 217 #endif /* _NOLIBC_TYPES_H */
+3 -3
tools/include/nolibc/unistd.h
··· 4 4 * Copyright (C) 2017-2022 Willy Tarreau <w@1wt.eu> 5 5 */ 6 6 7 + /* make sure to include all global symbols */ 8 + #include "nolibc.h" 9 + 7 10 #ifndef _NOLIBC_UNISTD_H 8 11 #define _NOLIBC_UNISTD_H 9 12 ··· 58 55 { 59 56 return ioctl(fd, TIOCSPGRP, &pid); 60 57 } 61 - 62 - /* make sure to include all global symbols */ 63 - #include "nolibc.h" 64 58 65 59 #endif /* _NOLIBC_UNISTD_H */