xref: /openbmc/linux/tools/include/nolibc/nolibc.h (revision 6d1970e1)
130ca2051SWilly Tarreau /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
230ca2051SWilly Tarreau /* nolibc.h
330ca2051SWilly Tarreau  * Copyright (C) 2017-2018 Willy Tarreau <w@1wt.eu>
430ca2051SWilly Tarreau  */
530ca2051SWilly Tarreau 
630ca2051SWilly Tarreau /*
730ca2051SWilly Tarreau  * This file is designed to be used as a libc alternative for minimal programs
830ca2051SWilly Tarreau  * with very limited requirements. It consists of a small number of syscall and
930ca2051SWilly Tarreau  * type definitions, and the minimal startup code needed to call main().
1030ca2051SWilly Tarreau  * All syscalls are declared as static functions so that they can be optimized
1130ca2051SWilly Tarreau  * away by the compiler when not used.
1230ca2051SWilly Tarreau  *
1330ca2051SWilly Tarreau  * Syscalls are split into 3 levels:
1430ca2051SWilly Tarreau  *   - The lower level is the arch-specific syscall() definition, consisting in
1530ca2051SWilly Tarreau  *     assembly code in compound expressions. These are called my_syscall0() to
16*6d1970e1SZhangjin Wu  *     my_syscall6() depending on the number of arguments. All input arguments
17*6d1970e1SZhangjin Wu  *     are castto a long stored in a register. These expressions always return
18*6d1970e1SZhangjin Wu  *     the syscall's return value as a signed long value which is often either
19*6d1970e1SZhangjin Wu  *     a pointer or the negated errno value.
2030ca2051SWilly Tarreau  *
2130ca2051SWilly Tarreau  *   - The second level is mostly architecture-independent. It is made of
2230ca2051SWilly Tarreau  *     static functions called sys_<name>() which rely on my_syscallN()
2330ca2051SWilly Tarreau  *     depending on the syscall definition. These functions are responsible
2430ca2051SWilly Tarreau  *     for exposing the appropriate types for the syscall arguments (int,
2530ca2051SWilly Tarreau  *     pointers, etc) and for setting the appropriate return type (often int).
2630ca2051SWilly Tarreau  *     A few of them are architecture-specific because the syscalls are not all
2730ca2051SWilly Tarreau  *     mapped exactly the same among architectures. For example, some archs do
2830ca2051SWilly Tarreau  *     not implement select() and need pselect6() instead, so the sys_select()
2930ca2051SWilly Tarreau  *     function will have to abstract this.
3030ca2051SWilly Tarreau  *
3130ca2051SWilly Tarreau  *   - The third level is the libc call definition. It exposes the lower raw
3230ca2051SWilly Tarreau  *     sys_<name>() calls in a way that looks like what a libc usually does,
3330ca2051SWilly Tarreau  *     takes care of specific input values, and of setting errno upon error.
3430ca2051SWilly Tarreau  *     There can be minor variations compared to standard libc calls. For
3530ca2051SWilly Tarreau  *     example the open() call always takes 3 args here.
3630ca2051SWilly Tarreau  *
3730ca2051SWilly Tarreau  * The errno variable is declared static and unused. This way it can be
3830ca2051SWilly Tarreau  * optimized away if not used. However this means that a program made of
3930ca2051SWilly Tarreau  * multiple C files may observe different errno values (one per C file). For
4030ca2051SWilly Tarreau  * the type of programs this project targets it usually is not a problem. The
4130ca2051SWilly Tarreau  * resulting program may even be reduced by defining the NOLIBC_IGNORE_ERRNO
4230ca2051SWilly Tarreau  * macro, in which case the errno value will never be assigned.
4330ca2051SWilly Tarreau  *
4430ca2051SWilly Tarreau  * Some stdint-like integer types are defined. These are valid on all currently
4530ca2051SWilly Tarreau  * supported architectures, because signs are enforced, ints are assumed to be
4630ca2051SWilly Tarreau  * 32 bits, longs the size of a pointer and long long 64 bits. If more
4730ca2051SWilly Tarreau  * architectures have to be supported, this may need to be adapted.
4830ca2051SWilly Tarreau  *
4930ca2051SWilly Tarreau  * Some macro definitions like the O_* values passed to open(), and some
5030ca2051SWilly Tarreau  * structures like the sys_stat struct depend on the architecture.
5130ca2051SWilly Tarreau  *
5230ca2051SWilly Tarreau  * The definitions start with the architecture-specific parts, which are picked
5330ca2051SWilly Tarreau  * based on what the compiler knows about the target architecture, and are
5430ca2051SWilly Tarreau  * completed with the generic code. Since it is the compiler which sets the
5530ca2051SWilly Tarreau  * target architecture, cross-compiling normally works out of the box without
5630ca2051SWilly Tarreau  * having to specify anything.
5730ca2051SWilly Tarreau  *
5830ca2051SWilly Tarreau  * Finally some very common libc-level functions are provided. It is the case
59c4486e97SWilly Tarreau  * for a few functions usually found in string.h, ctype.h, or stdlib.h.
6030ca2051SWilly Tarreau  *
61c4486e97SWilly Tarreau  * The nolibc.h file is only a convenient entry point which includes all other
62c4486e97SWilly Tarreau  * files. It also defines the NOLIBC macro, so that it is possible for a
63c4486e97SWilly Tarreau  * program to check this macro to know if it is being built against and decide
64c4486e97SWilly Tarreau  * to disable some features or simply not to include some standard libc files.
6530ca2051SWilly Tarreau  *
6630ca2051SWilly Tarreau  * A simple static executable may be built this way :
6730ca2051SWilly Tarreau  *      $ gcc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \
683c6ce7a5SWilly Tarreau  *            -static -include nolibc.h -o hello hello.c -lgcc
6930ca2051SWilly Tarreau  *
70c4486e97SWilly Tarreau  * Simple programs meant to be reasonably portable to various libc and using
71c4486e97SWilly Tarreau  * only a few common includes, may also be built by simply making the include
72c4486e97SWilly Tarreau  * path point to the nolibc directory:
73c4486e97SWilly Tarreau  *      $ gcc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \
74c4486e97SWilly Tarreau  *            -I../nolibc -o hello hello.c -lgcc
75c4486e97SWilly Tarreau  *
76c4486e97SWilly Tarreau  * The available standard (but limited) include files are:
77c4486e97SWilly Tarreau  *   ctype.h, errno.h, signal.h, stdio.h, stdlib.h, string.h, time.h
78c4486e97SWilly Tarreau  *
79c4486e97SWilly Tarreau  * In addition, the following ones are expected to be provided by the compiler:
80c4486e97SWilly Tarreau  *   float.h, stdarg.h, stddef.h
81c4486e97SWilly Tarreau  *
82c4486e97SWilly Tarreau  * The following ones which are part to the C standard are not provided:
83c4486e97SWilly Tarreau  *   assert.h, locale.h, math.h, setjmp.h, limits.h
84c4486e97SWilly Tarreau  *
8530ca2051SWilly Tarreau  * A very useful calling convention table may be found here :
8630ca2051SWilly Tarreau  *      http://man7.org/linux/man-pages/man2/syscall.2.html
8730ca2051SWilly Tarreau  *
8830ca2051SWilly Tarreau  * This doc is quite convenient though not necessarily up to date :
8930ca2051SWilly Tarreau  *      https://w3challs.com/syscalls/
9030ca2051SWilly Tarreau  *
9130ca2051SWilly Tarreau  */
92930c4accSWilly Tarreau #ifndef _NOLIBC_H
93930c4accSWilly Tarreau #define _NOLIBC_H
9430ca2051SWilly Tarreau 
95967cce19SWilly Tarreau #include "std.h"
96271661c1SWilly Tarreau #include "arch.h"
97cc7a492aSWilly Tarreau #include "types.h"
98bd8c8fbbSWilly Tarreau #include "sys.h"
9962a2af07SWilly Tarreau #include "ctype.h"
10099cb50abSWilly Tarreau #include "signal.h"
1013a8039e2SMark Brown #include "unistd.h"
1024e383a66SWilly Tarreau #include "stdio.h"
10306fdba53SWilly Tarreau #include "stdlib.h"
104c91eb033SWilly Tarreau #include "string.h"
105cec15053SWilly Tarreau #include "time.h"
1067188d463SThomas Weißschuh #include "stackprotector.h"
10730ca2051SWilly Tarreau 
108cc7a492aSWilly Tarreau /* Used by programs to avoid std includes */
10930ca2051SWilly Tarreau #define NOLIBC
11030ca2051SWilly Tarreau 
111930c4accSWilly Tarreau #endif /* _NOLIBC_H */
112