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 1630ca2051SWilly Tarreau * my_syscall6() depending on the number of arguments. The MIPS 1730ca2051SWilly Tarreau * implementation is limited to 5 arguments. All input arguments are cast 1830ca2051SWilly Tarreau * to a long stored in a register. These expressions always return the 1930ca2051SWilly Tarreau * syscall's return value as a signed long value which is often either a 2030ca2051SWilly Tarreau * pointer or the negated errno value. 2130ca2051SWilly Tarreau * 2230ca2051SWilly Tarreau * - The second level is mostly architecture-independent. It is made of 2330ca2051SWilly Tarreau * static functions called sys_<name>() which rely on my_syscallN() 2430ca2051SWilly Tarreau * depending on the syscall definition. These functions are responsible 2530ca2051SWilly Tarreau * for exposing the appropriate types for the syscall arguments (int, 2630ca2051SWilly Tarreau * pointers, etc) and for setting the appropriate return type (often int). 2730ca2051SWilly Tarreau * A few of them are architecture-specific because the syscalls are not all 2830ca2051SWilly Tarreau * mapped exactly the same among architectures. For example, some archs do 2930ca2051SWilly Tarreau * not implement select() and need pselect6() instead, so the sys_select() 3030ca2051SWilly Tarreau * function will have to abstract this. 3130ca2051SWilly Tarreau * 3230ca2051SWilly Tarreau * - The third level is the libc call definition. It exposes the lower raw 3330ca2051SWilly Tarreau * sys_<name>() calls in a way that looks like what a libc usually does, 3430ca2051SWilly Tarreau * takes care of specific input values, and of setting errno upon error. 3530ca2051SWilly Tarreau * There can be minor variations compared to standard libc calls. For 3630ca2051SWilly Tarreau * example the open() call always takes 3 args here. 3730ca2051SWilly Tarreau * 3830ca2051SWilly Tarreau * The errno variable is declared static and unused. This way it can be 3930ca2051SWilly Tarreau * optimized away if not used. However this means that a program made of 4030ca2051SWilly Tarreau * multiple C files may observe different errno values (one per C file). For 4130ca2051SWilly Tarreau * the type of programs this project targets it usually is not a problem. The 4230ca2051SWilly Tarreau * resulting program may even be reduced by defining the NOLIBC_IGNORE_ERRNO 4330ca2051SWilly Tarreau * macro, in which case the errno value will never be assigned. 4430ca2051SWilly Tarreau * 4530ca2051SWilly Tarreau * Some stdint-like integer types are defined. These are valid on all currently 4630ca2051SWilly Tarreau * supported architectures, because signs are enforced, ints are assumed to be 4730ca2051SWilly Tarreau * 32 bits, longs the size of a pointer and long long 64 bits. If more 4830ca2051SWilly Tarreau * architectures have to be supported, this may need to be adapted. 4930ca2051SWilly Tarreau * 5030ca2051SWilly Tarreau * Some macro definitions like the O_* values passed to open(), and some 5130ca2051SWilly Tarreau * structures like the sys_stat struct depend on the architecture. 5230ca2051SWilly Tarreau * 5330ca2051SWilly Tarreau * The definitions start with the architecture-specific parts, which are picked 5430ca2051SWilly Tarreau * based on what the compiler knows about the target architecture, and are 5530ca2051SWilly Tarreau * completed with the generic code. Since it is the compiler which sets the 5630ca2051SWilly Tarreau * target architecture, cross-compiling normally works out of the box without 5730ca2051SWilly Tarreau * having to specify anything. 5830ca2051SWilly Tarreau * 5930ca2051SWilly Tarreau * Finally some very common libc-level functions are provided. It is the case 60c4486e97SWilly Tarreau * for a few functions usually found in string.h, ctype.h, or stdlib.h. 6130ca2051SWilly Tarreau * 62c4486e97SWilly Tarreau * The nolibc.h file is only a convenient entry point which includes all other 63c4486e97SWilly Tarreau * files. It also defines the NOLIBC macro, so that it is possible for a 64c4486e97SWilly Tarreau * program to check this macro to know if it is being built against and decide 65c4486e97SWilly Tarreau * to disable some features or simply not to include some standard libc files. 6630ca2051SWilly Tarreau * 6730ca2051SWilly Tarreau * A simple static executable may be built this way : 6830ca2051SWilly Tarreau * $ gcc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \ 693c6ce7a5SWilly Tarreau * -static -include nolibc.h -o hello hello.c -lgcc 7030ca2051SWilly Tarreau * 71c4486e97SWilly Tarreau * Simple programs meant to be reasonably portable to various libc and using 72c4486e97SWilly Tarreau * only a few common includes, may also be built by simply making the include 73c4486e97SWilly Tarreau * path point to the nolibc directory: 74c4486e97SWilly Tarreau * $ gcc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \ 75c4486e97SWilly Tarreau * -I../nolibc -o hello hello.c -lgcc 76c4486e97SWilly Tarreau * 77c4486e97SWilly Tarreau * The available standard (but limited) include files are: 78c4486e97SWilly Tarreau * ctype.h, errno.h, signal.h, stdio.h, stdlib.h, string.h, time.h 79c4486e97SWilly Tarreau * 80c4486e97SWilly Tarreau * In addition, the following ones are expected to be provided by the compiler: 81c4486e97SWilly Tarreau * float.h, stdarg.h, stddef.h 82c4486e97SWilly Tarreau * 83c4486e97SWilly Tarreau * The following ones which are part to the C standard are not provided: 84c4486e97SWilly Tarreau * assert.h, locale.h, math.h, setjmp.h, limits.h 85c4486e97SWilly Tarreau * 8630ca2051SWilly Tarreau * A very useful calling convention table may be found here : 8730ca2051SWilly Tarreau * http://man7.org/linux/man-pages/man2/syscall.2.html 8830ca2051SWilly Tarreau * 8930ca2051SWilly Tarreau * This doc is quite convenient though not necessarily up to date : 9030ca2051SWilly Tarreau * https://w3challs.com/syscalls/ 9130ca2051SWilly Tarreau * 9230ca2051SWilly Tarreau */ 93930c4accSWilly Tarreau #ifndef _NOLIBC_H 94930c4accSWilly Tarreau #define _NOLIBC_H 9530ca2051SWilly Tarreau 96967cce19SWilly Tarreau #include "std.h" 97271661c1SWilly Tarreau #include "arch.h" 98cc7a492aSWilly Tarreau #include "types.h" 99bd8c8fbbSWilly Tarreau #include "sys.h" 10062a2af07SWilly Tarreau #include "ctype.h" 10199cb50abSWilly Tarreau #include "signal.h" 102*3a8039e2SMark Brown #include "unistd.h" 1034e383a66SWilly Tarreau #include "stdio.h" 10406fdba53SWilly Tarreau #include "stdlib.h" 105c91eb033SWilly Tarreau #include "string.h" 106cec15053SWilly Tarreau #include "time.h" 1077188d463SThomas Weißschuh #include "stackprotector.h" 10830ca2051SWilly Tarreau 109cc7a492aSWilly Tarreau /* Used by programs to avoid std includes */ 11030ca2051SWilly Tarreau #define NOLIBC 11130ca2051SWilly Tarreau 112930c4accSWilly Tarreau #endif /* _NOLIBC_H */ 113