![]() |
|
|||
0001 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */ 0002 /* nolibc.h 0003 * Copyright (C) 2017-2018 Willy Tarreau <w@1wt.eu> 0004 */ 0005 0006 /* 0007 * This file is designed to be used as a libc alternative for minimal programs 0008 * with very limited requirements. It consists of a small number of syscall and 0009 * type definitions, and the minimal startup code needed to call main(). 0010 * All syscalls are declared as static functions so that they can be optimized 0011 * away by the compiler when not used. 0012 * 0013 * Syscalls are split into 3 levels: 0014 * - The lower level is the arch-specific syscall() definition, consisting in 0015 * assembly code in compound expressions. These are called my_syscall0() to 0016 * my_syscall6() depending on the number of arguments. The MIPS 0017 * implementation is limited to 5 arguments. All input arguments are cast 0018 * to a long stored in a register. These expressions always return the 0019 * syscall's return value as a signed long value which is often either a 0020 * pointer or the negated errno value. 0021 * 0022 * - The second level is mostly architecture-independent. It is made of 0023 * static functions called sys_<name>() which rely on my_syscallN() 0024 * depending on the syscall definition. These functions are responsible 0025 * for exposing the appropriate types for the syscall arguments (int, 0026 * pointers, etc) and for setting the appropriate return type (often int). 0027 * A few of them are architecture-specific because the syscalls are not all 0028 * mapped exactly the same among architectures. For example, some archs do 0029 * not implement select() and need pselect6() instead, so the sys_select() 0030 * function will have to abstract this. 0031 * 0032 * - The third level is the libc call definition. It exposes the lower raw 0033 * sys_<name>() calls in a way that looks like what a libc usually does, 0034 * takes care of specific input values, and of setting errno upon error. 0035 * There can be minor variations compared to standard libc calls. For 0036 * example the open() call always takes 3 args here. 0037 * 0038 * The errno variable is declared static and unused. This way it can be 0039 * optimized away if not used. However this means that a program made of 0040 * multiple C files may observe different errno values (one per C file). For 0041 * the type of programs this project targets it usually is not a problem. The 0042 * resulting program may even be reduced by defining the NOLIBC_IGNORE_ERRNO 0043 * macro, in which case the errno value will never be assigned. 0044 * 0045 * Some stdint-like integer types are defined. These are valid on all currently 0046 * supported architectures, because signs are enforced, ints are assumed to be 0047 * 32 bits, longs the size of a pointer and long long 64 bits. If more 0048 * architectures have to be supported, this may need to be adapted. 0049 * 0050 * Some macro definitions like the O_* values passed to open(), and some 0051 * structures like the sys_stat struct depend on the architecture. 0052 * 0053 * The definitions start with the architecture-specific parts, which are picked 0054 * based on what the compiler knows about the target architecture, and are 0055 * completed with the generic code. Since it is the compiler which sets the 0056 * target architecture, cross-compiling normally works out of the box without 0057 * having to specify anything. 0058 * 0059 * Finally some very common libc-level functions are provided. It is the case 0060 * for a few functions usually found in string.h, ctype.h, or stdlib.h. 0061 * 0062 * The nolibc.h file is only a convenient entry point which includes all other 0063 * files. It also defines the NOLIBC macro, so that it is possible for a 0064 * program to check this macro to know if it is being built against and decide 0065 * to disable some features or simply not to include some standard libc files. 0066 * 0067 * A simple static executable may be built this way : 0068 * $ gcc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \ 0069 * -static -include nolibc.h -o hello hello.c -lgcc 0070 * 0071 * Simple programs meant to be reasonably portable to various libc and using 0072 * only a few common includes, may also be built by simply making the include 0073 * path point to the nolibc directory: 0074 * $ gcc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \ 0075 * -I../nolibc -o hello hello.c -lgcc 0076 * 0077 * The available standard (but limited) include files are: 0078 * ctype.h, errno.h, signal.h, stdio.h, stdlib.h, string.h, time.h 0079 * 0080 * In addition, the following ones are expected to be provided by the compiler: 0081 * float.h, stdarg.h, stddef.h 0082 * 0083 * The following ones which are part to the C standard are not provided: 0084 * assert.h, locale.h, math.h, setjmp.h, limits.h 0085 * 0086 * A very useful calling convention table may be found here : 0087 * http://man7.org/linux/man-pages/man2/syscall.2.html 0088 * 0089 * This doc is quite convenient though not necessarily up to date : 0090 * https://w3challs.com/syscalls/ 0091 * 0092 */ 0093 #ifndef _NOLIBC_H 0094 #define _NOLIBC_H 0095 0096 #include "std.h" 0097 #include "arch.h" 0098 #include "types.h" 0099 #include "sys.h" 0100 #include "ctype.h" 0101 #include "signal.h" 0102 #include "stdio.h" 0103 #include "stdlib.h" 0104 #include "string.h" 0105 #include "time.h" 0106 #include "unistd.h" 0107 0108 /* Used by programs to avoid std includes */ 0109 #define NOLIBC 0110 0111 #endif /* _NOLIBC_H */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |