113 lines · c
1/* SPDX-License-Identifier: LGPL-2.1 OR MIT */2/* nolibc.h3 * Copyright (C) 2017-2018 Willy Tarreau <w@1wt.eu>4 */5 6/*7 * This file is designed to be used as a libc alternative for minimal programs8 * with very limited requirements. It consists of a small number of syscall and9 * type definitions, and the minimal startup code needed to call main().10 * All syscalls are declared as static functions so that they can be optimized11 * away by the compiler when not used.12 *13 * Syscalls are split into 3 levels:14 * - The lower level is the arch-specific syscall() definition, consisting in15 * assembly code in compound expressions. These are called my_syscall0() to16 * my_syscall6() depending on the number of arguments. All input arguments17 * are castto a long stored in a register. These expressions always return18 * the syscall's return value as a signed long value which is often either19 * a pointer or the negated errno value.20 *21 * - The second level is mostly architecture-independent. It is made of22 * static functions called sys_<name>() which rely on my_syscallN()23 * depending on the syscall definition. These functions are responsible24 * for exposing the appropriate types for the syscall arguments (int,25 * pointers, etc) and for setting the appropriate return type (often int).26 * A few of them are architecture-specific because the syscalls are not all27 * mapped exactly the same among architectures. For example, some archs do28 * not implement select() and need pselect6() instead, so the sys_select()29 * function will have to abstract this.30 *31 * - The third level is the libc call definition. It exposes the lower raw32 * sys_<name>() calls in a way that looks like what a libc usually does,33 * takes care of specific input values, and of setting errno upon error.34 * There can be minor variations compared to standard libc calls. For35 * example the open() call always takes 3 args here.36 *37 * The errno variable is declared static and unused. This way it can be38 * optimized away if not used. However this means that a program made of39 * multiple C files may observe different errno values (one per C file). For40 * the type of programs this project targets it usually is not a problem. The41 * resulting program may even be reduced by defining the NOLIBC_IGNORE_ERRNO42 * macro, in which case the errno value will never be assigned.43 *44 * Some stdint-like integer types are defined. These are valid on all currently45 * supported architectures, because signs are enforced, ints are assumed to be46 * 32 bits, longs the size of a pointer and long long 64 bits. If more47 * architectures have to be supported, this may need to be adapted.48 *49 * Some macro definitions like the O_* values passed to open(), and some50 * structures like the sys_stat struct depend on the architecture.51 *52 * The definitions start with the architecture-specific parts, which are picked53 * based on what the compiler knows about the target architecture, and are54 * completed with the generic code. Since it is the compiler which sets the55 * target architecture, cross-compiling normally works out of the box without56 * having to specify anything.57 *58 * Finally some very common libc-level functions are provided. It is the case59 * for a few functions usually found in string.h, ctype.h, or stdlib.h.60 *61 * The nolibc.h file is only a convenient entry point which includes all other62 * files. It also defines the NOLIBC macro, so that it is possible for a63 * program to check this macro to know if it is being built against and decide64 * to disable some features or simply not to include some standard libc files.65 *66 * A simple static executable may be built this way :67 * $ gcc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \68 * -static -include nolibc.h -o hello hello.c -lgcc69 *70 * Simple programs meant to be reasonably portable to various libc and using71 * only a few common includes, may also be built by simply making the include72 * path point to the nolibc directory:73 * $ gcc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \74 * -I../nolibc -o hello hello.c -lgcc75 *76 * The available standard (but limited) include files are:77 * ctype.h, errno.h, signal.h, stdarg.h, stdbool.h stdio.h, stdlib.h,78 * string.h, time.h79 *80 * In addition, the following ones are expected to be provided by the compiler:81 * float.h, stddef.h82 *83 * The following ones which are part to the C standard are not provided:84 * assert.h, locale.h, math.h, setjmp.h, limits.h85 *86 * A very useful calling convention table may be found here :87 * http://man7.org/linux/man-pages/man2/syscall.2.html88 *89 * This doc is quite convenient though not necessarily up to date :90 * https://w3challs.com/syscalls/91 *92 */93#ifndef _NOLIBC_H94#define _NOLIBC_H95 96#include "std.h"97#include "arch.h"98#include "types.h"99#include "sys.h"100#include "ctype.h"101#include "signal.h"102#include "unistd.h"103#include "stdio.h"104#include "stdlib.h"105#include "string.h"106#include "time.h"107#include "stackprotector.h"108 109/* Used by programs to avoid std includes */110#define NOLIBC111 112#endif /* _NOLIBC_H */113