162306a36Sopenharmony_ci/* SPDX-License-Identifier: LGPL-2.1 OR MIT */ 262306a36Sopenharmony_ci/* nolibc.h 362306a36Sopenharmony_ci * Copyright (C) 2017-2018 Willy Tarreau <w@1wt.eu> 462306a36Sopenharmony_ci */ 562306a36Sopenharmony_ci 662306a36Sopenharmony_ci/* 762306a36Sopenharmony_ci * This file is designed to be used as a libc alternative for minimal programs 862306a36Sopenharmony_ci * with very limited requirements. It consists of a small number of syscall and 962306a36Sopenharmony_ci * type definitions, and the minimal startup code needed to call main(). 1062306a36Sopenharmony_ci * All syscalls are declared as static functions so that they can be optimized 1162306a36Sopenharmony_ci * away by the compiler when not used. 1262306a36Sopenharmony_ci * 1362306a36Sopenharmony_ci * Syscalls are split into 3 levels: 1462306a36Sopenharmony_ci * - The lower level is the arch-specific syscall() definition, consisting in 1562306a36Sopenharmony_ci * assembly code in compound expressions. These are called my_syscall0() to 1662306a36Sopenharmony_ci * my_syscall6() depending on the number of arguments. All input arguments 1762306a36Sopenharmony_ci * are castto a long stored in a register. These expressions always return 1862306a36Sopenharmony_ci * the syscall's return value as a signed long value which is often either 1962306a36Sopenharmony_ci * a pointer or the negated errno value. 2062306a36Sopenharmony_ci * 2162306a36Sopenharmony_ci * - The second level is mostly architecture-independent. It is made of 2262306a36Sopenharmony_ci * static functions called sys_<name>() which rely on my_syscallN() 2362306a36Sopenharmony_ci * depending on the syscall definition. These functions are responsible 2462306a36Sopenharmony_ci * for exposing the appropriate types for the syscall arguments (int, 2562306a36Sopenharmony_ci * pointers, etc) and for setting the appropriate return type (often int). 2662306a36Sopenharmony_ci * A few of them are architecture-specific because the syscalls are not all 2762306a36Sopenharmony_ci * mapped exactly the same among architectures. For example, some archs do 2862306a36Sopenharmony_ci * not implement select() and need pselect6() instead, so the sys_select() 2962306a36Sopenharmony_ci * function will have to abstract this. 3062306a36Sopenharmony_ci * 3162306a36Sopenharmony_ci * - The third level is the libc call definition. It exposes the lower raw 3262306a36Sopenharmony_ci * sys_<name>() calls in a way that looks like what a libc usually does, 3362306a36Sopenharmony_ci * takes care of specific input values, and of setting errno upon error. 3462306a36Sopenharmony_ci * There can be minor variations compared to standard libc calls. For 3562306a36Sopenharmony_ci * example the open() call always takes 3 args here. 3662306a36Sopenharmony_ci * 3762306a36Sopenharmony_ci * The errno variable is declared static and unused. This way it can be 3862306a36Sopenharmony_ci * optimized away if not used. However this means that a program made of 3962306a36Sopenharmony_ci * multiple C files may observe different errno values (one per C file). For 4062306a36Sopenharmony_ci * the type of programs this project targets it usually is not a problem. The 4162306a36Sopenharmony_ci * resulting program may even be reduced by defining the NOLIBC_IGNORE_ERRNO 4262306a36Sopenharmony_ci * macro, in which case the errno value will never be assigned. 4362306a36Sopenharmony_ci * 4462306a36Sopenharmony_ci * Some stdint-like integer types are defined. These are valid on all currently 4562306a36Sopenharmony_ci * supported architectures, because signs are enforced, ints are assumed to be 4662306a36Sopenharmony_ci * 32 bits, longs the size of a pointer and long long 64 bits. If more 4762306a36Sopenharmony_ci * architectures have to be supported, this may need to be adapted. 4862306a36Sopenharmony_ci * 4962306a36Sopenharmony_ci * Some macro definitions like the O_* values passed to open(), and some 5062306a36Sopenharmony_ci * structures like the sys_stat struct depend on the architecture. 5162306a36Sopenharmony_ci * 5262306a36Sopenharmony_ci * The definitions start with the architecture-specific parts, which are picked 5362306a36Sopenharmony_ci * based on what the compiler knows about the target architecture, and are 5462306a36Sopenharmony_ci * completed with the generic code. Since it is the compiler which sets the 5562306a36Sopenharmony_ci * target architecture, cross-compiling normally works out of the box without 5662306a36Sopenharmony_ci * having to specify anything. 5762306a36Sopenharmony_ci * 5862306a36Sopenharmony_ci * Finally some very common libc-level functions are provided. It is the case 5962306a36Sopenharmony_ci * for a few functions usually found in string.h, ctype.h, or stdlib.h. 6062306a36Sopenharmony_ci * 6162306a36Sopenharmony_ci * The nolibc.h file is only a convenient entry point which includes all other 6262306a36Sopenharmony_ci * files. It also defines the NOLIBC macro, so that it is possible for a 6362306a36Sopenharmony_ci * program to check this macro to know if it is being built against and decide 6462306a36Sopenharmony_ci * to disable some features or simply not to include some standard libc files. 6562306a36Sopenharmony_ci * 6662306a36Sopenharmony_ci * A simple static executable may be built this way : 6762306a36Sopenharmony_ci * $ gcc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \ 6862306a36Sopenharmony_ci * -static -include nolibc.h -o hello hello.c -lgcc 6962306a36Sopenharmony_ci * 7062306a36Sopenharmony_ci * Simple programs meant to be reasonably portable to various libc and using 7162306a36Sopenharmony_ci * only a few common includes, may also be built by simply making the include 7262306a36Sopenharmony_ci * path point to the nolibc directory: 7362306a36Sopenharmony_ci * $ gcc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \ 7462306a36Sopenharmony_ci * -I../nolibc -o hello hello.c -lgcc 7562306a36Sopenharmony_ci * 7662306a36Sopenharmony_ci * The available standard (but limited) include files are: 7762306a36Sopenharmony_ci * ctype.h, errno.h, signal.h, stdio.h, stdlib.h, string.h, time.h 7862306a36Sopenharmony_ci * 7962306a36Sopenharmony_ci * In addition, the following ones are expected to be provided by the compiler: 8062306a36Sopenharmony_ci * float.h, stdarg.h, stddef.h 8162306a36Sopenharmony_ci * 8262306a36Sopenharmony_ci * The following ones which are part to the C standard are not provided: 8362306a36Sopenharmony_ci * assert.h, locale.h, math.h, setjmp.h, limits.h 8462306a36Sopenharmony_ci * 8562306a36Sopenharmony_ci * A very useful calling convention table may be found here : 8662306a36Sopenharmony_ci * http://man7.org/linux/man-pages/man2/syscall.2.html 8762306a36Sopenharmony_ci * 8862306a36Sopenharmony_ci * This doc is quite convenient though not necessarily up to date : 8962306a36Sopenharmony_ci * https://w3challs.com/syscalls/ 9062306a36Sopenharmony_ci * 9162306a36Sopenharmony_ci */ 9262306a36Sopenharmony_ci#ifndef _NOLIBC_H 9362306a36Sopenharmony_ci#define _NOLIBC_H 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci#include "std.h" 9662306a36Sopenharmony_ci#include "arch.h" 9762306a36Sopenharmony_ci#include "types.h" 9862306a36Sopenharmony_ci#include "sys.h" 9962306a36Sopenharmony_ci#include "ctype.h" 10062306a36Sopenharmony_ci#include "signal.h" 10162306a36Sopenharmony_ci#include "unistd.h" 10262306a36Sopenharmony_ci#include "stdio.h" 10362306a36Sopenharmony_ci#include "stdlib.h" 10462306a36Sopenharmony_ci#include "string.h" 10562306a36Sopenharmony_ci#include "time.h" 10662306a36Sopenharmony_ci#include "stackprotector.h" 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ci/* Used by programs to avoid std includes */ 10962306a36Sopenharmony_ci#define NOLIBC 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci#endif /* _NOLIBC_H */ 112