File Index
Files and directories
Sign in
FILE

nolibc.h

File preview with syntax highlighting.

linux-6.19.14/tools/include/nolibc/nolibc.h OBJECTIVE-C
  1/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
  2/* nolibc.h
  3 * 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 programs
  8 * with very limited requirements. It consists of a small number of syscall and
  9 * type definitions, and the minimal startup code needed to call main().
 10 * All syscalls are declared as static functions so that they can be optimized
 11 * 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 in
 15 *     assembly code in compound expressions. These are called my_syscall0() to
 16 *     my_syscall6() depending on the number of arguments. All input arguments
 17 *     are castto a long stored in a register. These expressions always return
 18 *     the syscall's return value as a signed long value which is often either
 19 *     a pointer or the negated errno value.
 20 *
 21 *   - The second level is mostly architecture-independent. It is made of
 22 *     static functions called sys_<name>() which rely on my_syscallN()
 23 *     depending on the syscall definition. These functions are responsible
 24 *     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 all
 27 *     mapped exactly the same among architectures. For example, some archs do
 28 *     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 raw
 32 *     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.
 35 *
 36 * The errno variable is declared static and unused. This way it can be
 37 * optimized away if not used. However this means that a program made of
 38 * multiple C files may observe different errno values (one per C file). For
 39 * the type of programs this project targets it usually is not a problem. The
 40 * resulting program may even be reduced by defining the NOLIBC_IGNORE_ERRNO
 41 * macro, in which case the errno value will never be assigned.
 42 *
 43 * Some stdint-like integer types are defined. These are valid on all currently
 44 * supported architectures, because signs are enforced, ints are assumed to be
 45 * 32 bits, longs the size of a pointer and long long 64 bits. If more
 46 * architectures have to be supported, this may need to be adapted.
 47 *
 48 * Some macro definitions like the O_* values passed to open(), and some
 49 * structures like the sys_stat struct depend on the architecture.
 50 *
 51 * The definitions start with the architecture-specific parts, which are picked
 52 * based on what the compiler knows about the target architecture, and are
 53 * completed with the generic code. Since it is the compiler which sets the
 54 * target architecture, cross-compiling normally works out of the box without
 55 * having to specify anything.
 56 *
 57 * Finally some very common libc-level functions are provided. It is the case
 58 * for a few functions usually found in string.h, ctype.h, or stdlib.h.
 59 *
 60 * The nolibc.h file is only a convenient entry point which includes all other
 61 * files. It also defines the NOLIBC macro, so that it is possible for a
 62 * program to check this macro to know if it is being built against and decide
 63 * to disable some features or simply not to include some standard libc files.
 64 *
 65 * A simple static executable may be built this way :
 66 *      $ gcc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \
 67 *            -static -include nolibc.h -o hello hello.c -lgcc
 68 *
 69 * Simple programs meant to be reasonably portable to various libc and using
 70 * only a few common includes, may also be built by simply making the include
 71 * path point to the nolibc directory:
 72 *      $ gcc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \
 73 *            -I../nolibc -o hello hello.c -lgcc
 74 *
 75 * The available standard (but limited) include files are:
 76 *   ctype.h, errno.h, signal.h, stdarg.h, stdbool.h stdio.h, stdlib.h,
 77 *   string.h, time.h
 78 *
 79 * In addition, the following ones are expected to be provided by the compiler:
 80 *   float.h, stddef.h
 81 *
 82 * The following ones which are part to the C standard are not provided:
 83 *   assert.h, locale.h, math.h, setjmp.h, limits.h
 84 *
 85 * A very useful calling convention table may be found here :
 86 *      http://man7.org/linux/man-pages/man2/syscall.2.html
 87 *
 88 * This doc is quite convenient though not necessarily up to date :
 89 *      https://w3challs.com/syscalls/
 90 *
 91 */
 92#ifndef _NOLIBC_H
 93#define _NOLIBC_H
 94
 95#include "std.h"
 96#include "arch.h"
 97#include "types.h"
 98#include "sys.h"
 99#include "sys/auxv.h"
100#include "sys/ioctl.h"
101#include "sys/mman.h"
102#include "sys/mount.h"
103#include "sys/prctl.h"
104#include "sys/random.h"
105#include "sys/reboot.h"
106#include "sys/resource.h"
107#include "sys/select.h"
108#include "sys/stat.h"
109#include "sys/syscall.h"
110#include "sys/sysmacros.h"
111#include "sys/time.h"
112#include "sys/timerfd.h"
113#include "sys/uio.h"
114#include "sys/utsname.h"
115#include "sys/wait.h"
116#include "ctype.h"
117#include "elf.h"
118#include "sched.h"
119#include "signal.h"
120#include "unistd.h"
121#include "stdbool.h"
122#include "stdio.h"
123#include "stdlib.h"
124#include "string.h"
125#include "time.h"
126#include "stackprotector.h"
127#include "dirent.h"
128#include "fcntl.h"
129#include "getopt.h"
130#include "poll.h"
131#include "math.h"
132
133/* Used by programs to avoid std includes */
134#define NOLIBC
135
136#endif /* _NOLIBC_H */
~/ Read-only file preview.