/* SPDX-License-Identifier: LGPL-2.1 OR MIT */1/* nolibc.h2* Copyright (C) 2017-2018 Willy Tarreau <[email protected]>3*/45/*6* This file is designed to be used as a libc alternative for minimal programs7* with very limited requirements. It consists of a small number of syscall and8* type definitions, and the minimal startup code needed to call main().9* All syscalls are declared as static functions so that they can be optimized10* away by the compiler when not used.11*12* Syscalls are split into 3 levels:13* - The lower level is the arch-specific syscall() definition, consisting in14* assembly code in compound expressions. These are called my_syscall0() to15* my_syscall6() depending on the number of arguments. All input arguments16* are castto a long stored in a register. These expressions always return17* the syscall's return value as a signed long value which is often either18* a pointer or the negated errno value.19*20* - The second level is mostly architecture-independent. It is made of21* static functions called sys_<name>() which rely on my_syscallN()22* depending on the syscall definition. These functions are responsible23* for exposing the appropriate types for the syscall arguments (int,24* pointers, etc) and for setting the appropriate return type (often int).25* A few of them are architecture-specific because the syscalls are not all26* mapped exactly the same among architectures. For example, some archs do27* not implement select() and need pselect6() instead, so the sys_select()28* function will have to abstract this.29*30* - The third level is the libc call definition. It exposes the lower raw31* sys_<name>() calls in a way that looks like what a libc usually does,32* takes care of specific input values, and of setting errno upon error.33* There can be minor variations compared to standard libc calls.34*35* The errno variable is declared static and unused. This way it can be36* optimized away if not used. However this means that a program made of37* multiple C files may observe different errno values (one per C file). For38* the type of programs this project targets it usually is not a problem. The39* resulting program may even be reduced by defining the NOLIBC_IGNORE_ERRNO40* macro, in which case the errno value will never be assigned.41*42* Some stdint-like integer types are defined. These are valid on all currently43* supported architectures, because signs are enforced, ints are assumed to be44* 32 bits, longs the size of a pointer and long long 64 bits. If more45* architectures have to be supported, this may need to be adapted.46*47* Some macro definitions like the O_* values passed to open(), and some48* structures like the sys_stat struct depend on the architecture.49*50* The definitions start with the architecture-specific parts, which are picked51* based on what the compiler knows about the target architecture, and are52* completed with the generic code. Since it is the compiler which sets the53* target architecture, cross-compiling normally works out of the box without54* having to specify anything.55*56* Finally some very common libc-level functions are provided. It is the case57* for a few functions usually found in string.h, ctype.h, or stdlib.h.58*59* The nolibc.h file is only a convenient entry point which includes all other60* files. It also defines the NOLIBC macro, so that it is possible for a61* program to check this macro to know if it is being built against and decide62* to disable some features or simply not to include some standard libc files.63*64* A simple static executable may be built this way :65* $ gcc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \66* -static -include nolibc.h -o hello hello.c -lgcc67*68* Simple programs meant to be reasonably portable to various libc and using69* only a few common includes, may also be built by simply making the include70* path point to the nolibc directory:71* $ gcc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \72* -I../nolibc -o hello hello.c -lgcc73*74* The available standard (but limited) include files are:75* ctype.h, errno.h, signal.h, stdarg.h, stdbool.h stdio.h, stdlib.h,76* string.h, time.h77*78* In addition, the following ones are expected to be provided by the compiler:79* float.h, stddef.h80*81* The following ones which are part to the C standard are not provided:82* assert.h, locale.h, math.h, setjmp.h, limits.h83*84* A very useful calling convention table may be found here :85* http://man7.org/linux/man-pages/man2/syscall.2.html86*87* This doc is quite convenient though not necessarily up to date :88* https://w3challs.com/syscalls/89*90*/91#ifndef _NOLIBC_H92#define _NOLIBC_H9394#include "std.h"95#include "arch.h"96#include "types.h"97#include "sys.h"98#include "sys/auxv.h"99#include "sys/ioctl.h"100#include "sys/mman.h"101#include "sys/mount.h"102#include "sys/prctl.h"103#include "sys/random.h"104#include "sys/reboot.h"105#include "sys/resource.h"106#include "sys/stat.h"107#include "sys/syscall.h"108#include "sys/sysmacros.h"109#include "sys/time.h"110#include "sys/timerfd.h"111#include "sys/utsname.h"112#include "sys/wait.h"113#include "ctype.h"114#include "elf.h"115#include "sched.h"116#include "signal.h"117#include "unistd.h"118#include "stdio.h"119#include "stdlib.h"120#include "string.h"121#include "time.h"122#include "stackprotector.h"123#include "dirent.h"124#include "fcntl.h"125#include "getopt.h"126#include "poll.h"127#include "math.h"128129/* Used by programs to avoid std includes */130#define NOLIBC131132#endif /* _NOLIBC_H */133134135