1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
3 * Copyright (C) 2017-2018 Willy Tarreau <w@1wt.eu>
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.
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. The MIPS
17 * implementation is limited to 5 arguments. All input arguments are cast
18 * to a long stored in a register. These expressions always return the
19 * syscall's return value as a signed long value which is often either a
20 * pointer or the negated errno value.
22 * - The second level is mostly architecture-independent. It is made of
23 * static functions called sys_<name>() which rely on my_syscallN()
24 * depending on the syscall definition. These functions are responsible
25 * for exposing the appropriate types for the syscall arguments (int,
26 * pointers, etc) and for setting the appropriate return type (often int).
27 * A few of them are architecture-specific because the syscalls are not all
28 * mapped exactly the same among architectures. For example, some archs do
29 * not implement select() and need pselect6() instead, so the sys_select()
30 * function will have to abstract this.
32 * - The third level is the libc call definition. It exposes the lower raw
33 * sys_<name>() calls in a way that looks like what a libc usually does,
34 * takes care of specific input values, and of setting errno upon error.
35 * There can be minor variations compared to standard libc calls. For
36 * example the open() call always takes 3 args here.
38 * The errno variable is declared static and unused. This way it can be
39 * optimized away if not used. However this means that a program made of
40 * multiple C files may observe different errno values (one per C file). For
41 * the type of programs this project targets it usually is not a problem. The
42 * resulting program may even be reduced by defining the NOLIBC_IGNORE_ERRNO
43 * macro, in which case the errno value will never be assigned.
45 * Some stdint-like integer types are defined. These are valid on all currently
46 * supported architectures, because signs are enforced, ints are assumed to be
47 * 32 bits, longs the size of a pointer and long long 64 bits. If more
48 * architectures have to be supported, this may need to be adapted.
50 * Some macro definitions like the O_* values passed to open(), and some
51 * structures like the sys_stat struct depend on the architecture.
53 * The definitions start with the architecture-specific parts, which are picked
54 * based on what the compiler knows about the target architecture, and are
55 * completed with the generic code. Since it is the compiler which sets the
56 * target architecture, cross-compiling normally works out of the box without
57 * having to specify anything.
59 * Finally some very common libc-level functions are provided. It is the case
60 * for a few functions usually found in string.h, ctype.h, or stdlib.h. Nothing
61 * is currently provided regarding stdio emulation.
63 * The macro NOLIBC is always defined, so that it is possible for a program to
64 * check this macro to know if it is being built against and decide to disable
65 * some features or simply not to include some standard libc files.
67 * Ideally this file should be split in multiple files for easier long term
68 * maintenance, but provided as a single file as it is now, it's quite
69 * convenient to use. Maybe some variations involving a set of includes at the
72 * A simple static executable may be built this way :
73 * $ gcc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \
74 * -static -include nolibc.h -lgcc -o hello hello.c
76 * A very useful calling convention table may be found here :
77 * http://man7.org/linux/man-pages/man2/syscall.2.html
79 * This doc is quite convenient though not necessarily up to date :
80 * https://w3challs.com/syscalls/
84 /* Some archs (at least aarch64) don't expose the regular syscalls anymore by
85 * default, either because they have an "_at" replacement, or because there are
86 * more modern alternatives. For now we'd rather still use them.
88 #define __ARCH_WANT_SYSCALL_NO_AT
89 #define __ARCH_WANT_SYSCALL_NO_FLAGS
90 #define __ARCH_WANT_SYSCALL_DEPRECATED
92 #include <asm/unistd.h>
93 #include <asm/ioctls.h>
94 #include <asm/errno.h>
96 #include <linux/loop.h>
100 /* this way it will be removed if unused */
103 #ifndef NOLIBC_IGNORE_ERRNO
104 #define SET_ERRNO(v) do { errno = (v); } while (0)
106 #define SET_ERRNO(v) do { } while (0)
109 /* errno codes all ensure that they will not conflict with a valid pointer
110 * because they all correspond to the highest addressable memry page.
112 #define MAX_ERRNO 4095
114 /* Declare a few quite common macros and types that usually are in stdlib.h,
115 * stdint.h, ctype.h, unistd.h and a few other common locations.
118 #define NULL ((void *)0)
121 typedef unsigned char uint8_t;
122 typedef signed char int8_t;
123 typedef unsigned short uint16_t;
124 typedef signed short int16_t;
125 typedef unsigned int uint32_t;
126 typedef signed int int32_t;
127 typedef unsigned long long uint64_t;
128 typedef signed long long int64_t;
129 typedef unsigned long size_t;
130 typedef signed long ssize_t;
131 typedef unsigned long uintptr_t;
132 typedef signed long intptr_t;
133 typedef signed long ptrdiff_t;
136 typedef unsigned int dev_t;
137 typedef unsigned long ino_t;
138 typedef unsigned int mode_t;
139 typedef signed int pid_t;
140 typedef unsigned int uid_t;
141 typedef unsigned int gid_t;
142 typedef unsigned long nlink_t;
143 typedef signed long off_t;
144 typedef signed long blksize_t;
145 typedef signed long blkcnt_t;
146 typedef signed long time_t;
167 /* for gettimeofday() */
173 /* for getdents64() */
174 struct linux_dirent64 {
177 unsigned short d_reclen;
178 unsigned char d_type;
182 /* commonly an fd_set represents 256 FDs */
183 #define FD_SETSIZE 256
184 typedef struct { uint32_t fd32[FD_SETSIZE/32]; } fd_set;
186 /* needed by wait4() */
188 struct timeval ru_utime;
189 struct timeval ru_stime;
206 /* stat flags (WARNING, octal here) */
207 #define S_IFDIR 0040000
208 #define S_IFCHR 0020000
209 #define S_IFBLK 0060000
210 #define S_IFREG 0100000
211 #define S_IFIFO 0010000
212 #define S_IFLNK 0120000
213 #define S_IFSOCK 0140000
214 #define S_IFMT 0170000
216 #define S_ISDIR(mode) (((mode) & S_IFDIR) == S_IFDIR)
217 #define S_ISCHR(mode) (((mode) & S_IFCHR) == S_IFCHR)
218 #define S_ISBLK(mode) (((mode) & S_IFBLK) == S_IFBLK)
219 #define S_ISREG(mode) (((mode) & S_IFREG) == S_IFREG)
220 #define S_ISFIFO(mode) (((mode) & S_IFIFO) == S_IFIFO)
221 #define S_ISLNK(mode) (((mode) & S_IFLNK) == S_IFLNK)
222 #define S_ISSOCK(mode) (((mode) & S_IFSOCK) == S_IFSOCK)
233 /* all the *at functions */
235 #define AT_FDCWD -100
244 #define LINUX_REBOOT_MAGIC1 0xfee1dead
245 #define LINUX_REBOOT_MAGIC2 0x28121969
246 #define LINUX_REBOOT_CMD_HALT 0xcdef0123
247 #define LINUX_REBOOT_CMD_POWER_OFF 0x4321fedc
248 #define LINUX_REBOOT_CMD_RESTART 0x01234567
249 #define LINUX_REBOOT_CMD_SW_SUSPEND 0xd000fce2
252 /* The format of the struct as returned by the libc to the application, which
253 * significantly differs from the format returned by the stat() syscall flavours.
256 dev_t st_dev; /* ID of device containing file */
257 ino_t st_ino; /* inode number */
258 mode_t st_mode; /* protection */
259 nlink_t st_nlink; /* number of hard links */
260 uid_t st_uid; /* user ID of owner */
261 gid_t st_gid; /* group ID of owner */
262 dev_t st_rdev; /* device ID (if special file) */
263 off_t st_size; /* total size, in bytes */
264 blksize_t st_blksize; /* blocksize for file system I/O */
265 blkcnt_t st_blocks; /* number of 512B blocks allocated */
266 time_t st_atime; /* time of last access */
267 time_t st_mtime; /* time of last modification */
268 time_t st_ctime; /* time of last status change */
271 #define WEXITSTATUS(status) (((status) & 0xff00) >> 8)
272 #define WIFEXITED(status) (((status) & 0x7f) == 0)
275 /* Below comes the architecture-specific code. For each architecture, we have
276 * the syscall declarations and the _start code definition. This is the only
277 * global part. On all architectures the kernel puts everything in the stack
278 * before jumping to _start just above us, without any return address (_start
279 * is not a function but an entry pint). So at the stack pointer we find argc.
280 * Then argv[] begins, and ends at the first NULL. Then we have envp which
281 * starts and ends with a NULL as well. So envp=argv+argc+1.
284 #if defined(__x86_64__)
285 /* Syscalls for x86_64 :
286 * - registers are 64-bit
287 * - syscall number is passed in rax
288 * - arguments are in rdi, rsi, rdx, r10, r8, r9 respectively
289 * - the system call is performed by calling the syscall instruction
290 * - syscall return comes in rax
291 * - rcx and r8..r11 may be clobbered, others are preserved.
292 * - the arguments are cast to long and assigned into the target registers
293 * which are then simply passed as registers to the asm code, so that we
294 * don't have to experience issues with register constraints.
295 * - the syscall number is always specified last in order to allow to force
296 * some registers before (gcc refuses a %-register at the last position).
299 #define my_syscall0(num) \
302 register long _num asm("rax") = (num); \
308 : "rcx", "r8", "r9", "r10", "r11", "memory", "cc" \
313 #define my_syscall1(num, arg1) \
316 register long _num asm("rax") = (num); \
317 register long _arg1 asm("rdi") = (long)(arg1); \
324 : "rcx", "r8", "r9", "r10", "r11", "memory", "cc" \
329 #define my_syscall2(num, arg1, arg2) \
332 register long _num asm("rax") = (num); \
333 register long _arg1 asm("rdi") = (long)(arg1); \
334 register long _arg2 asm("rsi") = (long)(arg2); \
339 : "r"(_arg1), "r"(_arg2), \
341 : "rcx", "r8", "r9", "r10", "r11", "memory", "cc" \
346 #define my_syscall3(num, arg1, arg2, arg3) \
349 register long _num asm("rax") = (num); \
350 register long _arg1 asm("rdi") = (long)(arg1); \
351 register long _arg2 asm("rsi") = (long)(arg2); \
352 register long _arg3 asm("rdx") = (long)(arg3); \
357 : "r"(_arg1), "r"(_arg2), "r"(_arg3), \
359 : "rcx", "r8", "r9", "r10", "r11", "memory", "cc" \
364 #define my_syscall4(num, arg1, arg2, arg3, arg4) \
367 register long _num asm("rax") = (num); \
368 register long _arg1 asm("rdi") = (long)(arg1); \
369 register long _arg2 asm("rsi") = (long)(arg2); \
370 register long _arg3 asm("rdx") = (long)(arg3); \
371 register long _arg4 asm("r10") = (long)(arg4); \
375 : "=a" (_ret), "=r"(_arg4) \
376 : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \
378 : "rcx", "r8", "r9", "r11", "memory", "cc" \
383 #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
386 register long _num asm("rax") = (num); \
387 register long _arg1 asm("rdi") = (long)(arg1); \
388 register long _arg2 asm("rsi") = (long)(arg2); \
389 register long _arg3 asm("rdx") = (long)(arg3); \
390 register long _arg4 asm("r10") = (long)(arg4); \
391 register long _arg5 asm("r8") = (long)(arg5); \
395 : "=a" (_ret), "=r"(_arg4), "=r"(_arg5) \
396 : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
398 : "rcx", "r9", "r11", "memory", "cc" \
403 #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
406 register long _num asm("rax") = (num); \
407 register long _arg1 asm("rdi") = (long)(arg1); \
408 register long _arg2 asm("rsi") = (long)(arg2); \
409 register long _arg3 asm("rdx") = (long)(arg3); \
410 register long _arg4 asm("r10") = (long)(arg4); \
411 register long _arg5 asm("r8") = (long)(arg5); \
412 register long _arg6 asm("r9") = (long)(arg6); \
416 : "=a" (_ret), "=r"(_arg4), "=r"(_arg5) \
417 : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
418 "r"(_arg6), "0"(_num) \
419 : "rcx", "r11", "memory", "cc" \
425 asm(".section .text\n"
428 "pop %rdi\n" // argc (first arg, %rdi)
429 "mov %rsp, %rsi\n" // argv[] (second arg, %rsi)
430 "lea 8(%rsi,%rdi,8),%rdx\n" // then a NULL then envp (third arg, %rdx)
431 "and $-16, %rsp\n" // x86 ABI : esp must be 16-byte aligned when
432 "sub $8, %rsp\n" // entering the callee
433 "call main\n" // main() returns the status code, we'll exit with it.
434 "movzb %al, %rdi\n" // retrieve exit code from 8 lower bits
435 "mov $60, %rax\n" // NR_exit == 60
436 "syscall\n" // really exit
437 "hlt\n" // ensure it does not return
446 #define O_NOCTTY 0x100
447 #define O_TRUNC 0x200
448 #define O_APPEND 0x400
449 #define O_NONBLOCK 0x800
450 #define O_DIRECTORY 0x10000
452 /* The struct returned by the stat() syscall, equivalent to stat64(). The
453 * syscall returns 116 bytes and stops in the middle of __unused.
455 struct sys_stat_struct {
456 unsigned long st_dev;
457 unsigned long st_ino;
458 unsigned long st_nlink;
459 unsigned int st_mode;
464 unsigned long st_rdev;
469 unsigned long st_atime;
470 unsigned long st_atime_nsec;
471 unsigned long st_mtime;
473 unsigned long st_mtime_nsec;
474 unsigned long st_ctime;
475 unsigned long st_ctime_nsec;
479 #elif defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__)
480 /* Syscalls for i386 :
481 * - mostly similar to x86_64
482 * - registers are 32-bit
483 * - syscall number is passed in eax
484 * - arguments are in ebx, ecx, edx, esi, edi, ebp respectively
485 * - all registers are preserved (except eax of course)
486 * - the system call is performed by calling int $0x80
487 * - syscall return comes in eax
488 * - the arguments are cast to long and assigned into the target registers
489 * which are then simply passed as registers to the asm code, so that we
490 * don't have to experience issues with register constraints.
491 * - the syscall number is always specified last in order to allow to force
492 * some registers before (gcc refuses a %-register at the last position).
494 * Also, i386 supports the old_select syscall if newselect is not available
496 #define __ARCH_WANT_SYS_OLD_SELECT
498 #define my_syscall0(num) \
501 register long _num asm("eax") = (num); \
512 #define my_syscall1(num, arg1) \
515 register long _num asm("eax") = (num); \
516 register long _arg1 asm("ebx") = (long)(arg1); \
528 #define my_syscall2(num, arg1, arg2) \
531 register long _num asm("eax") = (num); \
532 register long _arg1 asm("ebx") = (long)(arg1); \
533 register long _arg2 asm("ecx") = (long)(arg2); \
538 : "r"(_arg1), "r"(_arg2), \
545 #define my_syscall3(num, arg1, arg2, arg3) \
548 register long _num asm("eax") = (num); \
549 register long _arg1 asm("ebx") = (long)(arg1); \
550 register long _arg2 asm("ecx") = (long)(arg2); \
551 register long _arg3 asm("edx") = (long)(arg3); \
556 : "r"(_arg1), "r"(_arg2), "r"(_arg3), \
563 #define my_syscall4(num, arg1, arg2, arg3, arg4) \
566 register long _num asm("eax") = (num); \
567 register long _arg1 asm("ebx") = (long)(arg1); \
568 register long _arg2 asm("ecx") = (long)(arg2); \
569 register long _arg3 asm("edx") = (long)(arg3); \
570 register long _arg4 asm("esi") = (long)(arg4); \
575 : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \
582 #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
585 register long _num asm("eax") = (num); \
586 register long _arg1 asm("ebx") = (long)(arg1); \
587 register long _arg2 asm("ecx") = (long)(arg2); \
588 register long _arg3 asm("edx") = (long)(arg3); \
589 register long _arg4 asm("esi") = (long)(arg4); \
590 register long _arg5 asm("edi") = (long)(arg5); \
595 : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
603 asm(".section .text\n"
606 "pop %eax\n" // argc (first arg, %eax)
607 "mov %esp, %ebx\n" // argv[] (second arg, %ebx)
608 "lea 4(%ebx,%eax,4),%ecx\n" // then a NULL then envp (third arg, %ecx)
609 "and $-16, %esp\n" // x86 ABI : esp must be 16-byte aligned when
610 "push %ecx\n" // push all registers on the stack so that we
611 "push %ebx\n" // support both regparm and plain stack modes
613 "call main\n" // main() returns the status code in %eax
614 "movzbl %al, %ebx\n" // retrieve exit code from lower 8 bits
615 "movl $1, %eax\n" // NR_exit == 1
616 "int $0x80\n" // exit now
617 "hlt\n" // ensure it does not
626 #define O_NOCTTY 0x100
627 #define O_TRUNC 0x200
628 #define O_APPEND 0x400
629 #define O_NONBLOCK 0x800
630 #define O_DIRECTORY 0x10000
632 /* The struct returned by the stat() syscall, 32-bit only, the syscall returns
633 * exactly 56 bytes (stops before the unused array).
635 struct sys_stat_struct {
636 unsigned long st_dev;
637 unsigned long st_ino;
638 unsigned short st_mode;
639 unsigned short st_nlink;
640 unsigned short st_uid;
641 unsigned short st_gid;
643 unsigned long st_rdev;
644 unsigned long st_size;
645 unsigned long st_blksize;
646 unsigned long st_blocks;
648 unsigned long st_atime;
649 unsigned long st_atime_nsec;
650 unsigned long st_mtime;
651 unsigned long st_mtime_nsec;
653 unsigned long st_ctime;
654 unsigned long st_ctime_nsec;
655 unsigned long __unused[2];
658 #elif defined(__ARM_EABI__)
659 /* Syscalls for ARM in ARM or Thumb modes :
660 * - registers are 32-bit
661 * - stack is 8-byte aligned
662 * ( http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka4127.html)
663 * - syscall number is passed in r7
664 * - arguments are in r0, r1, r2, r3, r4, r5
665 * - the system call is performed by calling svc #0
666 * - syscall return comes in r0.
667 * - only lr is clobbered.
668 * - the arguments are cast to long and assigned into the target registers
669 * which are then simply passed as registers to the asm code, so that we
670 * don't have to experience issues with register constraints.
671 * - the syscall number is always specified last in order to allow to force
672 * some registers before (gcc refuses a %-register at the last position).
674 * Also, ARM supports the old_select syscall if newselect is not available
676 #define __ARCH_WANT_SYS_OLD_SELECT
678 #define my_syscall0(num) \
680 register long _num asm("r7") = (num); \
681 register long _arg1 asm("r0"); \
687 : "memory", "cc", "lr" \
692 #define my_syscall1(num, arg1) \
694 register long _num asm("r7") = (num); \
695 register long _arg1 asm("r0") = (long)(arg1); \
702 : "memory", "cc", "lr" \
707 #define my_syscall2(num, arg1, arg2) \
709 register long _num asm("r7") = (num); \
710 register long _arg1 asm("r0") = (long)(arg1); \
711 register long _arg2 asm("r1") = (long)(arg2); \
716 : "r"(_arg1), "r"(_arg2), \
718 : "memory", "cc", "lr" \
723 #define my_syscall3(num, arg1, arg2, arg3) \
725 register long _num asm("r7") = (num); \
726 register long _arg1 asm("r0") = (long)(arg1); \
727 register long _arg2 asm("r1") = (long)(arg2); \
728 register long _arg3 asm("r2") = (long)(arg3); \
733 : "r"(_arg1), "r"(_arg2), "r"(_arg3), \
735 : "memory", "cc", "lr" \
740 #define my_syscall4(num, arg1, arg2, arg3, arg4) \
742 register long _num asm("r7") = (num); \
743 register long _arg1 asm("r0") = (long)(arg1); \
744 register long _arg2 asm("r1") = (long)(arg2); \
745 register long _arg3 asm("r2") = (long)(arg3); \
746 register long _arg4 asm("r3") = (long)(arg4); \
751 : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \
753 : "memory", "cc", "lr" \
758 #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
760 register long _num asm("r7") = (num); \
761 register long _arg1 asm("r0") = (long)(arg1); \
762 register long _arg2 asm("r1") = (long)(arg2); \
763 register long _arg3 asm("r2") = (long)(arg3); \
764 register long _arg4 asm("r3") = (long)(arg4); \
765 register long _arg5 asm("r4") = (long)(arg5); \
770 : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
772 : "memory", "cc", "lr" \
778 asm(".section .text\n"
781 #if defined(__THUMBEB__) || defined(__THUMBEL__)
782 /* We enter here in 32-bit mode but if some previous functions were in
783 * 16-bit mode, the assembler cannot know, so we need to tell it we're in
784 * 32-bit now, then switch to 16-bit (is there a better way to do it than
785 * adding 1 by hand ?) and tell the asm we're now in 16-bit mode so that
786 * it generates correct instructions. Note that we do not support thumb1.
793 "pop {%r0}\n" // argc was in the stack
794 "mov %r1, %sp\n" // argv = sp
795 "add %r2, %r1, %r0, lsl #2\n" // envp = argv + 4*argc ...
796 "add %r2, %r2, $4\n" // ... + 4
797 "and %r3, %r1, $-8\n" // AAPCS : sp must be 8-byte aligned in the
798 "mov %sp, %r3\n" // callee, an bl doesn't push (lr=pc)
799 "bl main\n" // main() returns the status code, we'll exit with it.
800 "and %r0, %r0, $0xff\n" // limit exit code to 8 bits
801 "movs r7, $1\n" // NR_exit == 1
811 #define O_NOCTTY 0x100
812 #define O_TRUNC 0x200
813 #define O_APPEND 0x400
814 #define O_NONBLOCK 0x800
815 #define O_DIRECTORY 0x4000
817 /* The struct returned by the stat() syscall, 32-bit only, the syscall returns
818 * exactly 56 bytes (stops before the unused array). In big endian, the format
819 * differs as devices are returned as short only.
821 struct sys_stat_struct {
822 #if defined(__ARMEB__)
823 unsigned short st_dev;
824 unsigned short __pad1;
826 unsigned long st_dev;
828 unsigned long st_ino;
829 unsigned short st_mode;
830 unsigned short st_nlink;
831 unsigned short st_uid;
832 unsigned short st_gid;
833 #if defined(__ARMEB__)
834 unsigned short st_rdev;
835 unsigned short __pad2;
837 unsigned long st_rdev;
839 unsigned long st_size;
840 unsigned long st_blksize;
841 unsigned long st_blocks;
842 unsigned long st_atime;
843 unsigned long st_atime_nsec;
844 unsigned long st_mtime;
845 unsigned long st_mtime_nsec;
846 unsigned long st_ctime;
847 unsigned long st_ctime_nsec;
848 unsigned long __unused[2];
851 #elif defined(__aarch64__)
852 /* Syscalls for AARCH64 :
853 * - registers are 64-bit
854 * - stack is 16-byte aligned
855 * - syscall number is passed in x8
856 * - arguments are in x0, x1, x2, x3, x4, x5
857 * - the system call is performed by calling svc 0
858 * - syscall return comes in x0.
859 * - the arguments are cast to long and assigned into the target registers
860 * which are then simply passed as registers to the asm code, so that we
861 * don't have to experience issues with register constraints.
863 * On aarch64, select() is not implemented so we have to use pselect6().
865 #define __ARCH_WANT_SYS_PSELECT6
867 #define my_syscall0(num) \
869 register long _num asm("x8") = (num); \
870 register long _arg1 asm("x0"); \
881 #define my_syscall1(num, arg1) \
883 register long _num asm("x8") = (num); \
884 register long _arg1 asm("x0") = (long)(arg1); \
896 #define my_syscall2(num, arg1, arg2) \
898 register long _num asm("x8") = (num); \
899 register long _arg1 asm("x0") = (long)(arg1); \
900 register long _arg2 asm("x1") = (long)(arg2); \
905 : "r"(_arg1), "r"(_arg2), \
912 #define my_syscall3(num, arg1, arg2, arg3) \
914 register long _num asm("x8") = (num); \
915 register long _arg1 asm("x0") = (long)(arg1); \
916 register long _arg2 asm("x1") = (long)(arg2); \
917 register long _arg3 asm("x2") = (long)(arg3); \
922 : "r"(_arg1), "r"(_arg2), "r"(_arg3), \
929 #define my_syscall4(num, arg1, arg2, arg3, arg4) \
931 register long _num asm("x8") = (num); \
932 register long _arg1 asm("x0") = (long)(arg1); \
933 register long _arg2 asm("x1") = (long)(arg2); \
934 register long _arg3 asm("x2") = (long)(arg3); \
935 register long _arg4 asm("x3") = (long)(arg4); \
940 : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \
947 #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
949 register long _num asm("x8") = (num); \
950 register long _arg1 asm("x0") = (long)(arg1); \
951 register long _arg2 asm("x1") = (long)(arg2); \
952 register long _arg3 asm("x2") = (long)(arg3); \
953 register long _arg4 asm("x3") = (long)(arg4); \
954 register long _arg5 asm("x4") = (long)(arg5); \
959 : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
966 #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \
968 register long _num asm("x8") = (num); \
969 register long _arg1 asm("x0") = (long)(arg1); \
970 register long _arg2 asm("x1") = (long)(arg2); \
971 register long _arg3 asm("x2") = (long)(arg3); \
972 register long _arg4 asm("x3") = (long)(arg4); \
973 register long _arg5 asm("x4") = (long)(arg5); \
974 register long _arg6 asm("x5") = (long)(arg6); \
979 : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \
980 "r"(_arg6), "r"(_num) \
987 asm(".section .text\n"
990 "ldr x0, [sp]\n" // argc (x0) was in the stack
991 "add x1, sp, 8\n" // argv (x1) = sp
992 "lsl x2, x0, 3\n" // envp (x2) = 8*argc ...
993 "add x2, x2, 8\n" // + 8 (skip null)
994 "add x2, x2, x1\n" // + argv
995 "and sp, x1, -16\n" // sp must be 16-byte aligned in the callee
996 "bl main\n" // main() returns the status code, we'll exit with it.
997 "and x0, x0, 0xff\n" // limit exit code to 8 bits
998 "mov x8, 93\n" // NR_exit == 93
1006 #define O_CREAT 0x40
1008 #define O_NOCTTY 0x100
1009 #define O_TRUNC 0x200
1010 #define O_APPEND 0x400
1011 #define O_NONBLOCK 0x800
1012 #define O_DIRECTORY 0x4000
1014 /* The struct returned by the newfstatat() syscall. Differs slightly from the
1015 * x86_64's stat one by field ordering, so be careful.
1017 struct sys_stat_struct {
1018 unsigned long st_dev;
1019 unsigned long st_ino;
1020 unsigned int st_mode;
1021 unsigned int st_nlink;
1022 unsigned int st_uid;
1023 unsigned int st_gid;
1025 unsigned long st_rdev;
1026 unsigned long __pad1;
1033 unsigned long st_atime_nsec;
1036 unsigned long st_mtime_nsec;
1038 unsigned long st_ctime_nsec;
1039 unsigned int __unused[2];
1042 #elif defined(__mips__) && defined(_ABIO32)
1043 /* Syscalls for MIPS ABI O32 :
1044 * - WARNING! there's always a delayed slot!
1045 * - WARNING again, the syntax is different, registers take a '$' and numbers
1047 * - registers are 32-bit
1048 * - stack is 8-byte aligned
1049 * - syscall number is passed in v0 (starts at 0xfa0).
1050 * - arguments are in a0, a1, a2, a3, then the stack. The caller needs to
1051 * leave some room in the stack for the callee to save a0..a3 if needed.
1052 * - Many registers are clobbered, in fact only a0..a2 and s0..s8 are
1053 * preserved. See: https://www.linux-mips.org/wiki/Syscall as well as
1054 * scall32-o32.S in the kernel sources.
1055 * - the system call is performed by calling "syscall"
1056 * - syscall return comes in v0, and register a3 needs to be checked to know
1057 * if an error occured, in which case errno is in v0.
1058 * - the arguments are cast to long and assigned into the target registers
1059 * which are then simply passed as registers to the asm code, so that we
1060 * don't have to experience issues with register constraints.
1063 #define my_syscall0(num) \
1065 register long _num asm("v0") = (num); \
1066 register long _arg4 asm("a3"); \
1069 "addiu $sp, $sp, -32\n" \
1071 "addiu $sp, $sp, 32\n" \
1072 : "=r"(_num), "=r"(_arg4) \
1074 : "memory", "cc", "at", "v1", "hi", "lo", \
1075 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \
1077 _arg4 ? -_num : _num; \
1080 #define my_syscall1(num, arg1) \
1082 register long _num asm("v0") = (num); \
1083 register long _arg1 asm("a0") = (long)(arg1); \
1084 register long _arg4 asm("a3"); \
1087 "addiu $sp, $sp, -32\n" \
1089 "addiu $sp, $sp, 32\n" \
1090 : "=r"(_num), "=r"(_arg4) \
1093 : "memory", "cc", "at", "v1", "hi", "lo", \
1094 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \
1096 _arg4 ? -_num : _num; \
1099 #define my_syscall2(num, arg1, arg2) \
1101 register long _num asm("v0") = (num); \
1102 register long _arg1 asm("a0") = (long)(arg1); \
1103 register long _arg2 asm("a1") = (long)(arg2); \
1104 register long _arg4 asm("a3"); \
1107 "addiu $sp, $sp, -32\n" \
1109 "addiu $sp, $sp, 32\n" \
1110 : "=r"(_num), "=r"(_arg4) \
1112 "r"(_arg1), "r"(_arg2) \
1113 : "memory", "cc", "at", "v1", "hi", "lo", \
1114 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \
1116 _arg4 ? -_num : _num; \
1119 #define my_syscall3(num, arg1, arg2, arg3) \
1121 register long _num asm("v0") = (num); \
1122 register long _arg1 asm("a0") = (long)(arg1); \
1123 register long _arg2 asm("a1") = (long)(arg2); \
1124 register long _arg3 asm("a2") = (long)(arg3); \
1125 register long _arg4 asm("a3"); \
1128 "addiu $sp, $sp, -32\n" \
1130 "addiu $sp, $sp, 32\n" \
1131 : "=r"(_num), "=r"(_arg4) \
1133 "r"(_arg1), "r"(_arg2), "r"(_arg3) \
1134 : "memory", "cc", "at", "v1", "hi", "lo", \
1135 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \
1137 _arg4 ? -_num : _num; \
1140 #define my_syscall4(num, arg1, arg2, arg3, arg4) \
1142 register long _num asm("v0") = (num); \
1143 register long _arg1 asm("a0") = (long)(arg1); \
1144 register long _arg2 asm("a1") = (long)(arg2); \
1145 register long _arg3 asm("a2") = (long)(arg3); \
1146 register long _arg4 asm("a3") = (long)(arg4); \
1149 "addiu $sp, $sp, -32\n" \
1151 "addiu $sp, $sp, 32\n" \
1152 : "=r" (_num), "=r"(_arg4) \
1154 "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4) \
1155 : "memory", "cc", "at", "v1", "hi", "lo", \
1156 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \
1158 _arg4 ? -_num : _num; \
1161 #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \
1163 register long _num asm("v0") = (num); \
1164 register long _arg1 asm("a0") = (long)(arg1); \
1165 register long _arg2 asm("a1") = (long)(arg2); \
1166 register long _arg3 asm("a2") = (long)(arg3); \
1167 register long _arg4 asm("a3") = (long)(arg4); \
1168 register long _arg5 = (long)(arg5); \
1171 "addiu $sp, $sp, -32\n" \
1172 "sw %7, 16($sp)\n" \
1174 "addiu $sp, $sp, 32\n" \
1175 : "=r" (_num), "=r"(_arg4) \
1177 "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5) \
1178 : "memory", "cc", "at", "v1", "hi", "lo", \
1179 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \
1181 _arg4 ? -_num : _num; \
1184 /* startup code, note that it's called __start on MIPS */
1185 asm(".section .text\n"
1192 "lw $a0,($sp)\n" // argc was in the stack
1193 "addiu $a1, $sp, 4\n" // argv = sp + 4
1194 "sll $a2, $a0, 2\n" // a2 = argc * 4
1195 "add $a2, $a2, $a1\n" // envp = argv + 4*argc ...
1196 "addiu $a2, $a2, 4\n" // ... + 4
1198 "and $sp, $sp, $t0\n" // sp must be 8-byte aligned
1199 "addiu $sp,$sp,-16\n" // the callee expects to save a0..a3 there!
1200 "jal main\n" // main() returns the status code, we'll exit with it.
1201 "nop\n" // delayed slot
1202 "and $a0, $v0, 0xff\n" // limit exit code to 8 bits
1203 "li $v0, 4001\n" // NR_exit == 4001
1212 #define O_APPEND 0x0008
1213 #define O_NONBLOCK 0x0080
1214 #define O_CREAT 0x0100
1215 #define O_TRUNC 0x0200
1216 #define O_EXCL 0x0400
1217 #define O_NOCTTY 0x0800
1218 #define O_DIRECTORY 0x10000
1220 /* The struct returned by the stat() syscall. 88 bytes are returned by the
1223 struct sys_stat_struct {
1224 unsigned int st_dev;
1226 unsigned long st_ino;
1227 unsigned int st_mode;
1228 unsigned int st_nlink;
1229 unsigned int st_uid;
1230 unsigned int st_gid;
1231 unsigned int st_rdev;
1249 /* Below are the C functions used to declare the raw syscalls. They try to be
1250 * architecture-agnostic, and return either a success or -errno. Declaring them
1251 * static will lead to them being inlined in most cases, but it's still possible
1252 * to reference them by a pointer if needed.
1254 static __attribute__((unused))
1255 void *sys_brk(void *addr)
1257 return (void *)my_syscall1(__NR_brk, addr);
1260 static __attribute__((noreturn,unused))
1261 void sys_exit(int status)
1263 my_syscall1(__NR_exit, status & 255);
1264 while(1); // shut the "noreturn" warnings.
1267 static __attribute__((unused))
1268 int sys_chdir(const char *path)
1270 return my_syscall1(__NR_chdir, path);
1273 static __attribute__((unused))
1274 int sys_chmod(const char *path, mode_t mode)
1276 #ifdef __NR_fchmodat
1277 return my_syscall4(__NR_fchmodat, AT_FDCWD, path, mode, 0);
1279 return my_syscall2(__NR_chmod, path, mode);
1283 static __attribute__((unused))
1284 int sys_chown(const char *path, uid_t owner, gid_t group)
1286 #ifdef __NR_fchownat
1287 return my_syscall5(__NR_fchownat, AT_FDCWD, path, owner, group, 0);
1289 return my_syscall3(__NR_chown, path, owner, group);
1293 static __attribute__((unused))
1294 int sys_chroot(const char *path)
1296 return my_syscall1(__NR_chroot, path);
1299 static __attribute__((unused))
1300 int sys_close(int fd)
1302 return my_syscall1(__NR_close, fd);
1305 static __attribute__((unused))
1308 return my_syscall1(__NR_dup, fd);
1311 static __attribute__((unused))
1312 int sys_dup2(int old, int new)
1314 return my_syscall2(__NR_dup2, old, new);
1317 static __attribute__((unused))
1318 int sys_execve(const char *filename, char *const argv[], char *const envp[])
1320 return my_syscall3(__NR_execve, filename, argv, envp);
1323 static __attribute__((unused))
1324 pid_t sys_fork(void)
1326 return my_syscall0(__NR_fork);
1329 static __attribute__((unused))
1330 int sys_fsync(int fd)
1332 return my_syscall1(__NR_fsync, fd);
1335 static __attribute__((unused))
1336 int sys_getdents64(int fd, struct linux_dirent64 *dirp, int count)
1338 return my_syscall3(__NR_getdents64, fd, dirp, count);
1341 static __attribute__((unused))
1342 pid_t sys_getpgrp(void)
1344 return my_syscall0(__NR_getpgrp);
1347 static __attribute__((unused))
1348 pid_t sys_getpid(void)
1350 return my_syscall0(__NR_getpid);
1353 static __attribute__((unused))
1354 int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
1356 return my_syscall2(__NR_gettimeofday, tv, tz);
1359 static __attribute__((unused))
1360 int sys_ioctl(int fd, unsigned long req, void *value)
1362 return my_syscall3(__NR_ioctl, fd, req, value);
1365 static __attribute__((unused))
1366 int sys_kill(pid_t pid, int signal)
1368 return my_syscall2(__NR_kill, pid, signal);
1371 static __attribute__((unused))
1372 int sys_link(const char *old, const char *new)
1375 return my_syscall5(__NR_linkat, AT_FDCWD, old, AT_FDCWD, new, 0);
1377 return my_syscall2(__NR_link, old, new);
1381 static __attribute__((unused))
1382 off_t sys_lseek(int fd, off_t offset, int whence)
1384 return my_syscall3(__NR_lseek, fd, offset, whence);
1387 static __attribute__((unused))
1388 int sys_mkdir(const char *path, mode_t mode)
1391 return my_syscall3(__NR_mkdirat, AT_FDCWD, path, mode);
1393 return my_syscall2(__NR_mkdir, path, mode);
1397 static __attribute__((unused))
1398 long sys_mknod(const char *path, mode_t mode, dev_t dev)
1401 return my_syscall4(__NR_mknodat, AT_FDCWD, path, mode, dev);
1403 return my_syscall3(__NR_mknod, path, mode, dev);
1407 static __attribute__((unused))
1408 int sys_mount(const char *src, const char *tgt, const char *fst,
1409 unsigned long flags, const void *data)
1411 return my_syscall5(__NR_mount, src, tgt, fst, flags, data);
1414 static __attribute__((unused))
1415 int sys_open(const char *path, int flags, mode_t mode)
1418 return my_syscall4(__NR_openat, AT_FDCWD, path, flags, mode);
1420 return my_syscall3(__NR_open, path, flags, mode);
1424 static __attribute__((unused))
1425 int sys_pivot_root(const char *new, const char *old)
1427 return my_syscall2(__NR_pivot_root, new, old);
1430 static __attribute__((unused))
1431 int sys_poll(struct pollfd *fds, int nfds, int timeout)
1433 return my_syscall3(__NR_poll, fds, nfds, timeout);
1436 static __attribute__((unused))
1437 ssize_t sys_read(int fd, void *buf, size_t count)
1439 return my_syscall3(__NR_read, fd, buf, count);
1442 static __attribute__((unused))
1443 ssize_t sys_reboot(int magic1, int magic2, int cmd, void *arg)
1445 return my_syscall4(__NR_reboot, magic1, magic2, cmd, arg);
1448 static __attribute__((unused))
1449 int sys_sched_yield(void)
1451 return my_syscall0(__NR_sched_yield);
1454 static __attribute__((unused))
1455 int sys_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
1457 #if defined(__ARCH_WANT_SYS_OLD_SELECT) && !defined(__NR__newselect)
1458 struct sel_arg_struct {
1462 } arg = { .n = nfds, .r = rfds, .w = wfds, .e = efds, .t = timeout };
1463 return my_syscall1(__NR_select, &arg);
1464 #elif defined(__ARCH_WANT_SYS_PSELECT6) && defined(__NR_pselect6)
1468 t.tv_sec = timeout->tv_sec;
1469 t.tv_nsec = timeout->tv_usec * 1000;
1471 return my_syscall6(__NR_pselect6, nfds, rfds, wfds, efds, timeout ? &t : NULL, NULL);
1473 #ifndef __NR__newselect
1474 #define __NR__newselect __NR_select
1476 return my_syscall5(__NR__newselect, nfds, rfds, wfds, efds, timeout);
1480 static __attribute__((unused))
1481 int sys_setpgid(pid_t pid, pid_t pgid)
1483 return my_syscall2(__NR_setpgid, pid, pgid);
1486 static __attribute__((unused))
1487 pid_t sys_setsid(void)
1489 return my_syscall0(__NR_setsid);
1492 static __attribute__((unused))
1493 int sys_stat(const char *path, struct stat *buf)
1495 struct sys_stat_struct stat;
1498 #ifdef __NR_newfstatat
1499 /* only solution for arm64 */
1500 ret = my_syscall4(__NR_newfstatat, AT_FDCWD, path, &stat, 0);
1502 ret = my_syscall2(__NR_stat, path, &stat);
1504 buf->st_dev = stat.st_dev;
1505 buf->st_ino = stat.st_ino;
1506 buf->st_mode = stat.st_mode;
1507 buf->st_nlink = stat.st_nlink;
1508 buf->st_uid = stat.st_uid;
1509 buf->st_gid = stat.st_gid;
1510 buf->st_rdev = stat.st_rdev;
1511 buf->st_size = stat.st_size;
1512 buf->st_blksize = stat.st_blksize;
1513 buf->st_blocks = stat.st_blocks;
1514 buf->st_atime = stat.st_atime;
1515 buf->st_mtime = stat.st_mtime;
1516 buf->st_ctime = stat.st_ctime;
1521 static __attribute__((unused))
1522 int sys_symlink(const char *old, const char *new)
1524 #ifdef __NR_symlinkat
1525 return my_syscall3(__NR_symlinkat, old, AT_FDCWD, new);
1527 return my_syscall2(__NR_symlink, old, new);
1531 static __attribute__((unused))
1532 mode_t sys_umask(mode_t mode)
1534 return my_syscall1(__NR_umask, mode);
1537 static __attribute__((unused))
1538 int sys_umount2(const char *path, int flags)
1540 return my_syscall2(__NR_umount2, path, flags);
1543 static __attribute__((unused))
1544 int sys_unlink(const char *path)
1546 #ifdef __NR_unlinkat
1547 return my_syscall3(__NR_unlinkat, AT_FDCWD, path, 0);
1549 return my_syscall1(__NR_unlink, path);
1553 static __attribute__((unused))
1554 pid_t sys_wait4(pid_t pid, int *status, int options, struct rusage *rusage)
1556 return my_syscall4(__NR_wait4, pid, status, options, rusage);
1559 static __attribute__((unused))
1560 pid_t sys_waitpid(pid_t pid, int *status, int options)
1562 return sys_wait4(pid, status, options, 0);
1565 static __attribute__((unused))
1566 pid_t sys_wait(int *status)
1568 return sys_waitpid(-1, status, 0);
1571 static __attribute__((unused))
1572 ssize_t sys_write(int fd, const void *buf, size_t count)
1574 return my_syscall3(__NR_write, fd, buf, count);
1578 /* Below are the libc-compatible syscalls which return x or -1 and set errno.
1579 * They rely on the functions above. Similarly they're marked static so that it
1580 * is possible to assign pointers to them if needed.
1583 static __attribute__((unused))
1586 void *ret = sys_brk(addr);
1595 static __attribute__((noreturn,unused))
1596 void exit(int status)
1601 static __attribute__((unused))
1602 int chdir(const char *path)
1604 int ret = sys_chdir(path);
1613 static __attribute__((unused))
1614 int chmod(const char *path, mode_t mode)
1616 int ret = sys_chmod(path, mode);
1625 static __attribute__((unused))
1626 int chown(const char *path, uid_t owner, gid_t group)
1628 int ret = sys_chown(path, owner, group);
1637 static __attribute__((unused))
1638 int chroot(const char *path)
1640 int ret = sys_chroot(path);
1649 static __attribute__((unused))
1652 int ret = sys_close(fd);
1661 static __attribute__((unused))
1662 int dup2(int old, int new)
1664 int ret = sys_dup2(old, new);
1673 static __attribute__((unused))
1674 int execve(const char *filename, char *const argv[], char *const envp[])
1676 int ret = sys_execve(filename, argv, envp);
1685 static __attribute__((unused))
1688 pid_t ret = sys_fork();
1697 static __attribute__((unused))
1700 int ret = sys_fsync(fd);
1709 static __attribute__((unused))
1710 int getdents64(int fd, struct linux_dirent64 *dirp, int count)
1712 int ret = sys_getdents64(fd, dirp, count);
1721 static __attribute__((unused))
1724 pid_t ret = sys_getpgrp();
1733 static __attribute__((unused))
1736 pid_t ret = sys_getpid();
1745 static __attribute__((unused))
1746 int gettimeofday(struct timeval *tv, struct timezone *tz)
1748 int ret = sys_gettimeofday(tv, tz);
1757 static __attribute__((unused))
1758 int ioctl(int fd, unsigned long req, void *value)
1760 int ret = sys_ioctl(fd, req, value);
1769 static __attribute__((unused))
1770 int kill(pid_t pid, int signal)
1772 int ret = sys_kill(pid, signal);
1781 static __attribute__((unused))
1782 int link(const char *old, const char *new)
1784 int ret = sys_link(old, new);
1793 static __attribute__((unused))
1794 off_t lseek(int fd, off_t offset, int whence)
1796 off_t ret = sys_lseek(fd, offset, whence);
1805 static __attribute__((unused))
1806 int mkdir(const char *path, mode_t mode)
1808 int ret = sys_mkdir(path, mode);
1817 static __attribute__((unused))
1818 int mknod(const char *path, mode_t mode, dev_t dev)
1820 int ret = sys_mknod(path, mode, dev);
1829 static __attribute__((unused))
1830 int mount(const char *src, const char *tgt,
1831 const char *fst, unsigned long flags,
1834 int ret = sys_mount(src, tgt, fst, flags, data);
1843 static __attribute__((unused))
1844 int open(const char *path, int flags, mode_t mode)
1846 int ret = sys_open(path, flags, mode);
1855 static __attribute__((unused))
1856 int pivot_root(const char *new, const char *old)
1858 int ret = sys_pivot_root(new, old);
1867 static __attribute__((unused))
1868 int poll(struct pollfd *fds, int nfds, int timeout)
1870 int ret = sys_poll(fds, nfds, timeout);
1879 static __attribute__((unused))
1880 ssize_t read(int fd, void *buf, size_t count)
1882 ssize_t ret = sys_read(fd, buf, count);
1891 static __attribute__((unused))
1894 int ret = sys_reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, 0);
1903 static __attribute__((unused))
1904 void *sbrk(intptr_t inc)
1908 /* first call to find current end */
1909 if ((ret = sys_brk(0)) && (sys_brk(ret + inc) == ret + inc))
1916 static __attribute__((unused))
1917 int sched_yield(void)
1919 int ret = sys_sched_yield();
1928 static __attribute__((unused))
1929 int select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
1931 int ret = sys_select(nfds, rfds, wfds, efds, timeout);
1940 static __attribute__((unused))
1941 int setpgid(pid_t pid, pid_t pgid)
1943 int ret = sys_setpgid(pid, pgid);
1952 static __attribute__((unused))
1955 pid_t ret = sys_setsid();
1964 static __attribute__((unused))
1965 unsigned int sleep(unsigned int seconds)
1967 struct timeval my_timeval = { seconds, 0 };
1969 if (sys_select(0, 0, 0, 0, &my_timeval) < 0)
1970 return my_timeval.tv_sec + !!my_timeval.tv_usec;
1975 static __attribute__((unused))
1976 int stat(const char *path, struct stat *buf)
1978 int ret = sys_stat(path, buf);
1987 static __attribute__((unused))
1988 int symlink(const char *old, const char *new)
1990 int ret = sys_symlink(old, new);
1999 static __attribute__((unused))
2000 int tcsetpgrp(int fd, pid_t pid)
2002 return ioctl(fd, TIOCSPGRP, &pid);
2005 static __attribute__((unused))
2006 mode_t umask(mode_t mode)
2008 return sys_umask(mode);
2011 static __attribute__((unused))
2012 int umount2(const char *path, int flags)
2014 int ret = sys_umount2(path, flags);
2023 static __attribute__((unused))
2024 int unlink(const char *path)
2026 int ret = sys_unlink(path);
2035 static __attribute__((unused))
2036 pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage)
2038 pid_t ret = sys_wait4(pid, status, options, rusage);
2047 static __attribute__((unused))
2048 pid_t waitpid(pid_t pid, int *status, int options)
2050 pid_t ret = sys_waitpid(pid, status, options);
2059 static __attribute__((unused))
2060 pid_t wait(int *status)
2062 pid_t ret = sys_wait(status);
2071 static __attribute__((unused))
2072 ssize_t write(int fd, const void *buf, size_t count)
2074 ssize_t ret = sys_write(fd, buf, count);
2083 /* some size-optimized reimplementations of a few common str* and mem*
2084 * functions. They're marked static, except memcpy() and raise() which are used
2085 * by libgcc on ARM, so they are marked weak instead in order not to cause an
2086 * error when building a program made of multiple files (not recommended).
2089 static __attribute__((unused))
2090 void *memmove(void *dst, const void *src, size_t len)
2092 ssize_t pos = (dst <= src) ? -1 : (long)len;
2096 pos += (dst <= src) ? 1 : -1;
2097 ((char *)dst)[pos] = ((char *)src)[pos];
2102 static __attribute__((unused))
2103 void *memset(void *dst, int b, size_t len)
2112 static __attribute__((unused))
2113 int memcmp(const void *s1, const void *s2, size_t n)
2118 while (ofs < n && !(c1 = ((char *)s1)[ofs] - ((char *)s2)[ofs])) {
2124 static __attribute__((unused))
2125 char *strcpy(char *dst, const char *src)
2129 while ((*dst++ = *src++));
2133 static __attribute__((unused))
2134 char *strchr(const char *s, int c)
2144 static __attribute__((unused))
2145 char *strrchr(const char *s, int c)
2147 const char *ret = NULL;
2157 static __attribute__((unused))
2158 size_t nolibc_strlen(const char *str)
2162 for (len = 0; str[len]; len++);
2166 #define strlen(str) ({ \
2167 __builtin_constant_p((str)) ? \
2168 __builtin_strlen((str)) : \
2169 nolibc_strlen((str)); \
2172 static __attribute__((unused))
2175 return (unsigned int)(c - '0') <= 9;
2178 static __attribute__((unused))
2179 long atol(const char *s)
2181 unsigned long ret = 0;
2198 return neg ? -ret : ret;
2201 static __attribute__((unused))
2202 int atoi(const char *s)
2207 static __attribute__((unused))
2208 const char *ltoa(long in)
2210 /* large enough for -9223372036854775808 */
2211 static char buffer[21];
2212 char *pos = buffer + sizeof(buffer) - 1;
2214 unsigned long n = neg ? -in : in;
2218 *pos-- = '0' + n % 10;
2229 __attribute__((weak,unused))
2230 void *memcpy(void *dst, const void *src, size_t len)
2232 return memmove(dst, src, len);
2235 /* needed by libgcc for divide by zero */
2236 __attribute__((weak,unused))
2237 int raise(int signal)
2239 return kill(getpid(), signal);
2242 /* Here come a few helper functions */
2244 static __attribute__((unused))
2245 void FD_ZERO(fd_set *set)
2247 memset(set, 0, sizeof(*set));
2250 static __attribute__((unused))
2251 void FD_SET(int fd, fd_set *set)
2253 if (fd < 0 || fd >= FD_SETSIZE)
2255 set->fd32[fd / 32] |= 1 << (fd & 31);
2258 /* WARNING, it only deals with the 4096 first majors and 256 first minors */
2259 static __attribute__((unused))
2260 dev_t makedev(unsigned int major, unsigned int minor)
2262 return ((major & 0xfff) << 8) | (minor & 0xff);