• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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. 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.
21  *
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.
31  *
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.
37  *
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.
44  *
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.
49  *
50  * Some macro definitions like the O_* values passed to open(), and some
51  * structures like the sys_stat struct depend on the architecture.
52  *
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.
58  *
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.
62  *
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.
66  *
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
70  * top could work.
71  *
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 -o hello hello.c -lgcc
75  *
76  * A very useful calling convention table may be found here :
77  *      http://man7.org/linux/man-pages/man2/syscall.2.html
78  *
79  * This doc is quite convenient though not necessarily up to date :
80  *      https://w3challs.com/syscalls/
81  *
82  */
83 
84 /* standard type definitions */
85 #include "std.h"
86 
87 /* system includes */
88 #include <asm/unistd.h>
89 #include <asm/signal.h>  // for SIGCHLD
90 #include <asm/ioctls.h>
91 #include <asm/errno.h>
92 #include <linux/fs.h>
93 #include <linux/loop.h>
94 #include <linux/time.h>
95 #include "arch.h"
96 #include "types.h"
97 
98 /* Used by programs to avoid std includes */
99 #define NOLIBC
100 
101 /* this way it will be removed if unused */
102 static int errno;
103 
104 #ifndef NOLIBC_IGNORE_ERRNO
105 #define SET_ERRNO(v) do { errno = (v); } while (0)
106 #else
107 #define SET_ERRNO(v) do { } while (0)
108 #endif
109 
110 /* errno codes all ensure that they will not conflict with a valid pointer
111  * because they all correspond to the highest addressable memory page.
112  */
113 #define MAX_ERRNO 4095
114 
115 
116 /* Below are the C functions used to declare the raw syscalls. They try to be
117  * architecture-agnostic, and return either a success or -errno. Declaring them
118  * static will lead to them being inlined in most cases, but it's still possible
119  * to reference them by a pointer if needed.
120  */
121 static __attribute__((unused))
sys_brk(void * addr)122 void *sys_brk(void *addr)
123 {
124 	return (void *)my_syscall1(__NR_brk, addr);
125 }
126 
127 static __attribute__((noreturn,unused))
sys_exit(int status)128 void sys_exit(int status)
129 {
130 	my_syscall1(__NR_exit, status & 255);
131 	while(1); // shut the "noreturn" warnings.
132 }
133 
134 static __attribute__((unused))
sys_chdir(const char * path)135 int sys_chdir(const char *path)
136 {
137 	return my_syscall1(__NR_chdir, path);
138 }
139 
140 static __attribute__((unused))
sys_chmod(const char * path,mode_t mode)141 int sys_chmod(const char *path, mode_t mode)
142 {
143 #ifdef __NR_fchmodat
144 	return my_syscall4(__NR_fchmodat, AT_FDCWD, path, mode, 0);
145 #elif defined(__NR_chmod)
146 	return my_syscall2(__NR_chmod, path, mode);
147 #else
148 #error Neither __NR_fchmodat nor __NR_chmod defined, cannot implement sys_chmod()
149 #endif
150 }
151 
152 static __attribute__((unused))
sys_chown(const char * path,uid_t owner,gid_t group)153 int sys_chown(const char *path, uid_t owner, gid_t group)
154 {
155 #ifdef __NR_fchownat
156 	return my_syscall5(__NR_fchownat, AT_FDCWD, path, owner, group, 0);
157 #elif defined(__NR_chown)
158 	return my_syscall3(__NR_chown, path, owner, group);
159 #else
160 #error Neither __NR_fchownat nor __NR_chown defined, cannot implement sys_chown()
161 #endif
162 }
163 
164 static __attribute__((unused))
sys_chroot(const char * path)165 int sys_chroot(const char *path)
166 {
167 	return my_syscall1(__NR_chroot, path);
168 }
169 
170 static __attribute__((unused))
sys_close(int fd)171 int sys_close(int fd)
172 {
173 	return my_syscall1(__NR_close, fd);
174 }
175 
176 static __attribute__((unused))
sys_dup(int fd)177 int sys_dup(int fd)
178 {
179 	return my_syscall1(__NR_dup, fd);
180 }
181 
182 #ifdef __NR_dup3
183 static __attribute__((unused))
sys_dup3(int old,int new,int flags)184 int sys_dup3(int old, int new, int flags)
185 {
186 	return my_syscall3(__NR_dup3, old, new, flags);
187 }
188 #endif
189 
190 static __attribute__((unused))
sys_dup2(int old,int new)191 int sys_dup2(int old, int new)
192 {
193 #ifdef __NR_dup3
194 	return my_syscall3(__NR_dup3, old, new, 0);
195 #elif defined(__NR_dup2)
196 	return my_syscall2(__NR_dup2, old, new);
197 #else
198 #error Neither __NR_dup3 nor __NR_dup2 defined, cannot implement sys_dup2()
199 #endif
200 }
201 
202 static __attribute__((unused))
sys_execve(const char * filename,char * const argv[],char * const envp[])203 int sys_execve(const char *filename, char *const argv[], char *const envp[])
204 {
205 	return my_syscall3(__NR_execve, filename, argv, envp);
206 }
207 
208 static __attribute__((unused))
sys_fork(void)209 pid_t sys_fork(void)
210 {
211 #ifdef __NR_clone
212 	/* note: some archs only have clone() and not fork(). Different archs
213 	 * have a different API, but most archs have the flags on first arg and
214 	 * will not use the rest with no other flag.
215 	 */
216 	return my_syscall5(__NR_clone, SIGCHLD, 0, 0, 0, 0);
217 #elif defined(__NR_fork)
218 	return my_syscall0(__NR_fork);
219 #else
220 #error Neither __NR_clone nor __NR_fork defined, cannot implement sys_fork()
221 #endif
222 }
223 
224 static __attribute__((unused))
sys_fsync(int fd)225 int sys_fsync(int fd)
226 {
227 	return my_syscall1(__NR_fsync, fd);
228 }
229 
230 static __attribute__((unused))
sys_getdents64(int fd,struct linux_dirent64 * dirp,int count)231 int sys_getdents64(int fd, struct linux_dirent64 *dirp, int count)
232 {
233 	return my_syscall3(__NR_getdents64, fd, dirp, count);
234 }
235 
236 static __attribute__((unused))
sys_getpgid(pid_t pid)237 pid_t sys_getpgid(pid_t pid)
238 {
239 	return my_syscall1(__NR_getpgid, pid);
240 }
241 
242 static __attribute__((unused))
sys_getpgrp(void)243 pid_t sys_getpgrp(void)
244 {
245 	return sys_getpgid(0);
246 }
247 
248 static __attribute__((unused))
sys_getpid(void)249 pid_t sys_getpid(void)
250 {
251 	return my_syscall0(__NR_getpid);
252 }
253 
254 static __attribute__((unused))
sys_gettimeofday(struct timeval * tv,struct timezone * tz)255 int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
256 {
257 	return my_syscall2(__NR_gettimeofday, tv, tz);
258 }
259 
260 static __attribute__((unused))
sys_ioctl(int fd,unsigned long req,void * value)261 int sys_ioctl(int fd, unsigned long req, void *value)
262 {
263 	return my_syscall3(__NR_ioctl, fd, req, value);
264 }
265 
266 static __attribute__((unused))
sys_kill(pid_t pid,int signal)267 int sys_kill(pid_t pid, int signal)
268 {
269 	return my_syscall2(__NR_kill, pid, signal);
270 }
271 
272 static __attribute__((unused))
sys_link(const char * old,const char * new)273 int sys_link(const char *old, const char *new)
274 {
275 #ifdef __NR_linkat
276 	return my_syscall5(__NR_linkat, AT_FDCWD, old, AT_FDCWD, new, 0);
277 #elif defined(__NR_link)
278 	return my_syscall2(__NR_link, old, new);
279 #else
280 #error Neither __NR_linkat nor __NR_link defined, cannot implement sys_link()
281 #endif
282 }
283 
284 static __attribute__((unused))
sys_lseek(int fd,off_t offset,int whence)285 off_t sys_lseek(int fd, off_t offset, int whence)
286 {
287 	return my_syscall3(__NR_lseek, fd, offset, whence);
288 }
289 
290 static __attribute__((unused))
sys_mkdir(const char * path,mode_t mode)291 int sys_mkdir(const char *path, mode_t mode)
292 {
293 #ifdef __NR_mkdirat
294 	return my_syscall3(__NR_mkdirat, AT_FDCWD, path, mode);
295 #elif defined(__NR_mkdir)
296 	return my_syscall2(__NR_mkdir, path, mode);
297 #else
298 #error Neither __NR_mkdirat nor __NR_mkdir defined, cannot implement sys_mkdir()
299 #endif
300 }
301 
302 static __attribute__((unused))
sys_mknod(const char * path,mode_t mode,dev_t dev)303 long sys_mknod(const char *path, mode_t mode, dev_t dev)
304 {
305 #ifdef __NR_mknodat
306 	return my_syscall4(__NR_mknodat, AT_FDCWD, path, mode, dev);
307 #elif defined(__NR_mknod)
308 	return my_syscall3(__NR_mknod, path, mode, dev);
309 #else
310 #error Neither __NR_mknodat nor __NR_mknod defined, cannot implement sys_mknod()
311 #endif
312 }
313 
314 static __attribute__((unused))
sys_mount(const char * src,const char * tgt,const char * fst,unsigned long flags,const void * data)315 int sys_mount(const char *src, const char *tgt, const char *fst,
316 	      unsigned long flags, const void *data)
317 {
318 	return my_syscall5(__NR_mount, src, tgt, fst, flags, data);
319 }
320 
321 static __attribute__((unused))
sys_open(const char * path,int flags,mode_t mode)322 int sys_open(const char *path, int flags, mode_t mode)
323 {
324 #ifdef __NR_openat
325 	return my_syscall4(__NR_openat, AT_FDCWD, path, flags, mode);
326 #elif defined(__NR_open)
327 	return my_syscall3(__NR_open, path, flags, mode);
328 #else
329 #error Neither __NR_openat nor __NR_open defined, cannot implement sys_open()
330 #endif
331 }
332 
333 static __attribute__((unused))
sys_pivot_root(const char * new,const char * old)334 int sys_pivot_root(const char *new, const char *old)
335 {
336 	return my_syscall2(__NR_pivot_root, new, old);
337 }
338 
339 static __attribute__((unused))
sys_poll(struct pollfd * fds,int nfds,int timeout)340 int sys_poll(struct pollfd *fds, int nfds, int timeout)
341 {
342 #if defined(__NR_ppoll)
343 	struct timespec t;
344 
345 	if (timeout >= 0) {
346 		t.tv_sec  = timeout / 1000;
347 		t.tv_nsec = (timeout % 1000) * 1000000;
348 	}
349 	return my_syscall4(__NR_ppoll, fds, nfds, (timeout >= 0) ? &t : NULL, NULL);
350 #elif defined(__NR_poll)
351 	return my_syscall3(__NR_poll, fds, nfds, timeout);
352 #else
353 #error Neither __NR_ppoll nor __NR_poll defined, cannot implement sys_poll()
354 #endif
355 }
356 
357 static __attribute__((unused))
sys_read(int fd,void * buf,size_t count)358 ssize_t sys_read(int fd, void *buf, size_t count)
359 {
360 	return my_syscall3(__NR_read, fd, buf, count);
361 }
362 
363 static __attribute__((unused))
sys_reboot(int magic1,int magic2,int cmd,void * arg)364 ssize_t sys_reboot(int magic1, int magic2, int cmd, void *arg)
365 {
366 	return my_syscall4(__NR_reboot, magic1, magic2, cmd, arg);
367 }
368 
369 static __attribute__((unused))
sys_sched_yield(void)370 int sys_sched_yield(void)
371 {
372 	return my_syscall0(__NR_sched_yield);
373 }
374 
375 static __attribute__((unused))
sys_select(int nfds,fd_set * rfds,fd_set * wfds,fd_set * efds,struct timeval * timeout)376 int sys_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
377 {
378 #if defined(__ARCH_WANT_SYS_OLD_SELECT) && !defined(__NR__newselect)
379 	struct sel_arg_struct {
380 		unsigned long n;
381 		fd_set *r, *w, *e;
382 		struct timeval *t;
383 	} arg = { .n = nfds, .r = rfds, .w = wfds, .e = efds, .t = timeout };
384 	return my_syscall1(__NR_select, &arg);
385 #elif defined(__ARCH_WANT_SYS_PSELECT6) && defined(__NR_pselect6)
386 	struct timespec t;
387 
388 	if (timeout) {
389 		t.tv_sec  = timeout->tv_sec;
390 		t.tv_nsec = timeout->tv_usec * 1000;
391 	}
392 	return my_syscall6(__NR_pselect6, nfds, rfds, wfds, efds, timeout ? &t : NULL, NULL);
393 #elif defined(__NR__newselect) || defined(__NR_select)
394 #ifndef __NR__newselect
395 #define __NR__newselect __NR_select
396 #endif
397 	return my_syscall5(__NR__newselect, nfds, rfds, wfds, efds, timeout);
398 #else
399 #error None of __NR_select, __NR_pselect6, nor __NR__newselect defined, cannot implement sys_select()
400 #endif
401 }
402 
403 static __attribute__((unused))
sys_setpgid(pid_t pid,pid_t pgid)404 int sys_setpgid(pid_t pid, pid_t pgid)
405 {
406 	return my_syscall2(__NR_setpgid, pid, pgid);
407 }
408 
409 static __attribute__((unused))
sys_setsid(void)410 pid_t sys_setsid(void)
411 {
412 	return my_syscall0(__NR_setsid);
413 }
414 
415 static __attribute__((unused))
sys_stat(const char * path,struct stat * buf)416 int sys_stat(const char *path, struct stat *buf)
417 {
418 	struct sys_stat_struct stat;
419 	long ret;
420 
421 #ifdef __NR_newfstatat
422 	/* only solution for arm64 */
423 	ret = my_syscall4(__NR_newfstatat, AT_FDCWD, path, &stat, 0);
424 #elif defined(__NR_stat)
425 	ret = my_syscall2(__NR_stat, path, &stat);
426 #else
427 #error Neither __NR_newfstatat nor __NR_stat defined, cannot implement sys_stat()
428 #endif
429 	buf->st_dev     = stat.st_dev;
430 	buf->st_ino     = stat.st_ino;
431 	buf->st_mode    = stat.st_mode;
432 	buf->st_nlink   = stat.st_nlink;
433 	buf->st_uid     = stat.st_uid;
434 	buf->st_gid     = stat.st_gid;
435 	buf->st_rdev    = stat.st_rdev;
436 	buf->st_size    = stat.st_size;
437 	buf->st_blksize = stat.st_blksize;
438 	buf->st_blocks  = stat.st_blocks;
439 	buf->st_atime   = stat.st_atime;
440 	buf->st_mtime   = stat.st_mtime;
441 	buf->st_ctime   = stat.st_ctime;
442 	return ret;
443 }
444 
445 
446 static __attribute__((unused))
sys_symlink(const char * old,const char * new)447 int sys_symlink(const char *old, const char *new)
448 {
449 #ifdef __NR_symlinkat
450 	return my_syscall3(__NR_symlinkat, old, AT_FDCWD, new);
451 #elif defined(__NR_symlink)
452 	return my_syscall2(__NR_symlink, old, new);
453 #else
454 #error Neither __NR_symlinkat nor __NR_symlink defined, cannot implement sys_symlink()
455 #endif
456 }
457 
458 static __attribute__((unused))
sys_umask(mode_t mode)459 mode_t sys_umask(mode_t mode)
460 {
461 	return my_syscall1(__NR_umask, mode);
462 }
463 
464 static __attribute__((unused))
sys_umount2(const char * path,int flags)465 int sys_umount2(const char *path, int flags)
466 {
467 	return my_syscall2(__NR_umount2, path, flags);
468 }
469 
470 static __attribute__((unused))
sys_unlink(const char * path)471 int sys_unlink(const char *path)
472 {
473 #ifdef __NR_unlinkat
474 	return my_syscall3(__NR_unlinkat, AT_FDCWD, path, 0);
475 #elif defined(__NR_unlink)
476 	return my_syscall1(__NR_unlink, path);
477 #else
478 #error Neither __NR_unlinkat nor __NR_unlink defined, cannot implement sys_unlink()
479 #endif
480 }
481 
482 static __attribute__((unused))
sys_wait4(pid_t pid,int * status,int options,struct rusage * rusage)483 pid_t sys_wait4(pid_t pid, int *status, int options, struct rusage *rusage)
484 {
485 	return my_syscall4(__NR_wait4, pid, status, options, rusage);
486 }
487 
488 static __attribute__((unused))
sys_waitpid(pid_t pid,int * status,int options)489 pid_t sys_waitpid(pid_t pid, int *status, int options)
490 {
491 	return sys_wait4(pid, status, options, 0);
492 }
493 
494 static __attribute__((unused))
sys_wait(int * status)495 pid_t sys_wait(int *status)
496 {
497 	return sys_waitpid(-1, status, 0);
498 }
499 
500 static __attribute__((unused))
sys_write(int fd,const void * buf,size_t count)501 ssize_t sys_write(int fd, const void *buf, size_t count)
502 {
503 	return my_syscall3(__NR_write, fd, buf, count);
504 }
505 
506 
507 /* Below are the libc-compatible syscalls which return x or -1 and set errno.
508  * They rely on the functions above. Similarly they're marked static so that it
509  * is possible to assign pointers to them if needed.
510  */
511 
512 static __attribute__((unused))
brk(void * addr)513 int brk(void *addr)
514 {
515 	void *ret = sys_brk(addr);
516 
517 	if (!ret) {
518 		SET_ERRNO(ENOMEM);
519 		return -1;
520 	}
521 	return 0;
522 }
523 
524 static __attribute__((noreturn,unused))
exit(int status)525 void exit(int status)
526 {
527 	sys_exit(status);
528 }
529 
530 static __attribute__((unused))
chdir(const char * path)531 int chdir(const char *path)
532 {
533 	int ret = sys_chdir(path);
534 
535 	if (ret < 0) {
536 		SET_ERRNO(-ret);
537 		ret = -1;
538 	}
539 	return ret;
540 }
541 
542 static __attribute__((unused))
chmod(const char * path,mode_t mode)543 int chmod(const char *path, mode_t mode)
544 {
545 	int ret = sys_chmod(path, mode);
546 
547 	if (ret < 0) {
548 		SET_ERRNO(-ret);
549 		ret = -1;
550 	}
551 	return ret;
552 }
553 
554 static __attribute__((unused))
chown(const char * path,uid_t owner,gid_t group)555 int chown(const char *path, uid_t owner, gid_t group)
556 {
557 	int ret = sys_chown(path, owner, group);
558 
559 	if (ret < 0) {
560 		SET_ERRNO(-ret);
561 		ret = -1;
562 	}
563 	return ret;
564 }
565 
566 static __attribute__((unused))
chroot(const char * path)567 int chroot(const char *path)
568 {
569 	int ret = sys_chroot(path);
570 
571 	if (ret < 0) {
572 		SET_ERRNO(-ret);
573 		ret = -1;
574 	}
575 	return ret;
576 }
577 
578 static __attribute__((unused))
close(int fd)579 int close(int fd)
580 {
581 	int ret = sys_close(fd);
582 
583 	if (ret < 0) {
584 		SET_ERRNO(-ret);
585 		ret = -1;
586 	}
587 	return ret;
588 }
589 
590 static __attribute__((unused))
dup(int fd)591 int dup(int fd)
592 {
593 	int ret = sys_dup(fd);
594 
595 	if (ret < 0) {
596 		SET_ERRNO(-ret);
597 		ret = -1;
598 	}
599 	return ret;
600 }
601 
602 static __attribute__((unused))
dup2(int old,int new)603 int dup2(int old, int new)
604 {
605 	int ret = sys_dup2(old, new);
606 
607 	if (ret < 0) {
608 		SET_ERRNO(-ret);
609 		ret = -1;
610 	}
611 	return ret;
612 }
613 
614 #ifdef __NR_dup3
615 static __attribute__((unused))
dup3(int old,int new,int flags)616 int dup3(int old, int new, int flags)
617 {
618 	int ret = sys_dup3(old, new, flags);
619 
620 	if (ret < 0) {
621 		SET_ERRNO(-ret);
622 		ret = -1;
623 	}
624 	return ret;
625 }
626 #endif
627 
628 static __attribute__((unused))
execve(const char * filename,char * const argv[],char * const envp[])629 int execve(const char *filename, char *const argv[], char *const envp[])
630 {
631 	int ret = sys_execve(filename, argv, envp);
632 
633 	if (ret < 0) {
634 		SET_ERRNO(-ret);
635 		ret = -1;
636 	}
637 	return ret;
638 }
639 
640 static __attribute__((unused))
fork(void)641 pid_t fork(void)
642 {
643 	pid_t ret = sys_fork();
644 
645 	if (ret < 0) {
646 		SET_ERRNO(-ret);
647 		ret = -1;
648 	}
649 	return ret;
650 }
651 
652 static __attribute__((unused))
fsync(int fd)653 int fsync(int fd)
654 {
655 	int ret = sys_fsync(fd);
656 
657 	if (ret < 0) {
658 		SET_ERRNO(-ret);
659 		ret = -1;
660 	}
661 	return ret;
662 }
663 
664 static __attribute__((unused))
getdents64(int fd,struct linux_dirent64 * dirp,int count)665 int getdents64(int fd, struct linux_dirent64 *dirp, int count)
666 {
667 	int ret = sys_getdents64(fd, dirp, count);
668 
669 	if (ret < 0) {
670 		SET_ERRNO(-ret);
671 		ret = -1;
672 	}
673 	return ret;
674 }
675 
676 static __attribute__((unused))
getpgid(pid_t pid)677 pid_t getpgid(pid_t pid)
678 {
679 	pid_t ret = sys_getpgid(pid);
680 
681 	if (ret < 0) {
682 		SET_ERRNO(-ret);
683 		ret = -1;
684 	}
685 	return ret;
686 }
687 
688 static __attribute__((unused))
getpgrp(void)689 pid_t getpgrp(void)
690 {
691 	pid_t ret = sys_getpgrp();
692 
693 	if (ret < 0) {
694 		SET_ERRNO(-ret);
695 		ret = -1;
696 	}
697 	return ret;
698 }
699 
700 static __attribute__((unused))
getpid(void)701 pid_t getpid(void)
702 {
703 	pid_t ret = sys_getpid();
704 
705 	if (ret < 0) {
706 		SET_ERRNO(-ret);
707 		ret = -1;
708 	}
709 	return ret;
710 }
711 
712 static __attribute__((unused))
gettimeofday(struct timeval * tv,struct timezone * tz)713 int gettimeofday(struct timeval *tv, struct timezone *tz)
714 {
715 	int ret = sys_gettimeofday(tv, tz);
716 
717 	if (ret < 0) {
718 		SET_ERRNO(-ret);
719 		ret = -1;
720 	}
721 	return ret;
722 }
723 
724 static __attribute__((unused))
ioctl(int fd,unsigned long req,void * value)725 int ioctl(int fd, unsigned long req, void *value)
726 {
727 	int ret = sys_ioctl(fd, req, value);
728 
729 	if (ret < 0) {
730 		SET_ERRNO(-ret);
731 		ret = -1;
732 	}
733 	return ret;
734 }
735 
736 static __attribute__((unused))
kill(pid_t pid,int signal)737 int kill(pid_t pid, int signal)
738 {
739 	int ret = sys_kill(pid, signal);
740 
741 	if (ret < 0) {
742 		SET_ERRNO(-ret);
743 		ret = -1;
744 	}
745 	return ret;
746 }
747 
748 static __attribute__((unused))
link(const char * old,const char * new)749 int link(const char *old, const char *new)
750 {
751 	int ret = sys_link(old, new);
752 
753 	if (ret < 0) {
754 		SET_ERRNO(-ret);
755 		ret = -1;
756 	}
757 	return ret;
758 }
759 
760 static __attribute__((unused))
lseek(int fd,off_t offset,int whence)761 off_t lseek(int fd, off_t offset, int whence)
762 {
763 	off_t ret = sys_lseek(fd, offset, whence);
764 
765 	if (ret < 0) {
766 		SET_ERRNO(-ret);
767 		ret = -1;
768 	}
769 	return ret;
770 }
771 
772 static __attribute__((unused))
mkdir(const char * path,mode_t mode)773 int mkdir(const char *path, mode_t mode)
774 {
775 	int ret = sys_mkdir(path, mode);
776 
777 	if (ret < 0) {
778 		SET_ERRNO(-ret);
779 		ret = -1;
780 	}
781 	return ret;
782 }
783 
784 static __attribute__((unused))
mknod(const char * path,mode_t mode,dev_t dev)785 int mknod(const char *path, mode_t mode, dev_t dev)
786 {
787 	int ret = sys_mknod(path, mode, dev);
788 
789 	if (ret < 0) {
790 		SET_ERRNO(-ret);
791 		ret = -1;
792 	}
793 	return ret;
794 }
795 
796 static __attribute__((unused))
mount(const char * src,const char * tgt,const char * fst,unsigned long flags,const void * data)797 int mount(const char *src, const char *tgt,
798 	  const char *fst, unsigned long flags,
799 	  const void *data)
800 {
801 	int ret = sys_mount(src, tgt, fst, flags, data);
802 
803 	if (ret < 0) {
804 		SET_ERRNO(-ret);
805 		ret = -1;
806 	}
807 	return ret;
808 }
809 
810 static __attribute__((unused))
open(const char * path,int flags,mode_t mode)811 int open(const char *path, int flags, mode_t mode)
812 {
813 	int ret = sys_open(path, flags, mode);
814 
815 	if (ret < 0) {
816 		SET_ERRNO(-ret);
817 		ret = -1;
818 	}
819 	return ret;
820 }
821 
822 static __attribute__((unused))
pivot_root(const char * new,const char * old)823 int pivot_root(const char *new, const char *old)
824 {
825 	int ret = sys_pivot_root(new, old);
826 
827 	if (ret < 0) {
828 		SET_ERRNO(-ret);
829 		ret = -1;
830 	}
831 	return ret;
832 }
833 
834 static __attribute__((unused))
poll(struct pollfd * fds,int nfds,int timeout)835 int poll(struct pollfd *fds, int nfds, int timeout)
836 {
837 	int ret = sys_poll(fds, nfds, timeout);
838 
839 	if (ret < 0) {
840 		SET_ERRNO(-ret);
841 		ret = -1;
842 	}
843 	return ret;
844 }
845 
846 static __attribute__((unused))
read(int fd,void * buf,size_t count)847 ssize_t read(int fd, void *buf, size_t count)
848 {
849 	ssize_t ret = sys_read(fd, buf, count);
850 
851 	if (ret < 0) {
852 		SET_ERRNO(-ret);
853 		ret = -1;
854 	}
855 	return ret;
856 }
857 
858 static __attribute__((unused))
reboot(int cmd)859 int reboot(int cmd)
860 {
861 	int ret = sys_reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, 0);
862 
863 	if (ret < 0) {
864 		SET_ERRNO(-ret);
865 		ret = -1;
866 	}
867 	return ret;
868 }
869 
870 static __attribute__((unused))
sbrk(intptr_t inc)871 void *sbrk(intptr_t inc)
872 {
873 	void *ret;
874 
875 	/* first call to find current end */
876 	if ((ret = sys_brk(0)) && (sys_brk(ret + inc) == ret + inc))
877 		return ret + inc;
878 
879 	SET_ERRNO(ENOMEM);
880 	return (void *)-1;
881 }
882 
883 static __attribute__((unused))
sched_yield(void)884 int sched_yield(void)
885 {
886 	int ret = sys_sched_yield();
887 
888 	if (ret < 0) {
889 		SET_ERRNO(-ret);
890 		ret = -1;
891 	}
892 	return ret;
893 }
894 
895 static __attribute__((unused))
select(int nfds,fd_set * rfds,fd_set * wfds,fd_set * efds,struct timeval * timeout)896 int select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
897 {
898 	int ret = sys_select(nfds, rfds, wfds, efds, timeout);
899 
900 	if (ret < 0) {
901 		SET_ERRNO(-ret);
902 		ret = -1;
903 	}
904 	return ret;
905 }
906 
907 static __attribute__((unused))
setpgid(pid_t pid,pid_t pgid)908 int setpgid(pid_t pid, pid_t pgid)
909 {
910 	int ret = sys_setpgid(pid, pgid);
911 
912 	if (ret < 0) {
913 		SET_ERRNO(-ret);
914 		ret = -1;
915 	}
916 	return ret;
917 }
918 
919 static __attribute__((unused))
setsid(void)920 pid_t setsid(void)
921 {
922 	pid_t ret = sys_setsid();
923 
924 	if (ret < 0) {
925 		SET_ERRNO(-ret);
926 		ret = -1;
927 	}
928 	return ret;
929 }
930 
931 static __attribute__((unused))
sleep(unsigned int seconds)932 unsigned int sleep(unsigned int seconds)
933 {
934 	struct timeval my_timeval = { seconds, 0 };
935 
936 	if (sys_select(0, 0, 0, 0, &my_timeval) < 0)
937 		return my_timeval.tv_sec + !!my_timeval.tv_usec;
938 	else
939 		return 0;
940 }
941 
942 static __attribute__((unused))
msleep(unsigned int msecs)943 int msleep(unsigned int msecs)
944 {
945 	struct timeval my_timeval = { msecs / 1000, (msecs % 1000) * 1000 };
946 
947 	if (sys_select(0, 0, 0, 0, &my_timeval) < 0)
948 		return (my_timeval.tv_sec * 1000) +
949 			(my_timeval.tv_usec / 1000) +
950 			!!(my_timeval.tv_usec % 1000);
951 	else
952 		return 0;
953 }
954 
955 static __attribute__((unused))
stat(const char * path,struct stat * buf)956 int stat(const char *path, struct stat *buf)
957 {
958 	int ret = sys_stat(path, buf);
959 
960 	if (ret < 0) {
961 		SET_ERRNO(-ret);
962 		ret = -1;
963 	}
964 	return ret;
965 }
966 
967 static __attribute__((unused))
symlink(const char * old,const char * new)968 int symlink(const char *old, const char *new)
969 {
970 	int ret = sys_symlink(old, new);
971 
972 	if (ret < 0) {
973 		SET_ERRNO(-ret);
974 		ret = -1;
975 	}
976 	return ret;
977 }
978 
979 static __attribute__((unused))
tcsetpgrp(int fd,pid_t pid)980 int tcsetpgrp(int fd, pid_t pid)
981 {
982 	return ioctl(fd, TIOCSPGRP, &pid);
983 }
984 
985 static __attribute__((unused))
umask(mode_t mode)986 mode_t umask(mode_t mode)
987 {
988 	return sys_umask(mode);
989 }
990 
991 static __attribute__((unused))
umount2(const char * path,int flags)992 int umount2(const char *path, int flags)
993 {
994 	int ret = sys_umount2(path, flags);
995 
996 	if (ret < 0) {
997 		SET_ERRNO(-ret);
998 		ret = -1;
999 	}
1000 	return ret;
1001 }
1002 
1003 static __attribute__((unused))
unlink(const char * path)1004 int unlink(const char *path)
1005 {
1006 	int ret = sys_unlink(path);
1007 
1008 	if (ret < 0) {
1009 		SET_ERRNO(-ret);
1010 		ret = -1;
1011 	}
1012 	return ret;
1013 }
1014 
1015 static __attribute__((unused))
wait4(pid_t pid,int * status,int options,struct rusage * rusage)1016 pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage)
1017 {
1018 	pid_t ret = sys_wait4(pid, status, options, rusage);
1019 
1020 	if (ret < 0) {
1021 		SET_ERRNO(-ret);
1022 		ret = -1;
1023 	}
1024 	return ret;
1025 }
1026 
1027 static __attribute__((unused))
waitpid(pid_t pid,int * status,int options)1028 pid_t waitpid(pid_t pid, int *status, int options)
1029 {
1030 	pid_t ret = sys_waitpid(pid, status, options);
1031 
1032 	if (ret < 0) {
1033 		SET_ERRNO(-ret);
1034 		ret = -1;
1035 	}
1036 	return ret;
1037 }
1038 
1039 static __attribute__((unused))
wait(int * status)1040 pid_t wait(int *status)
1041 {
1042 	pid_t ret = sys_wait(status);
1043 
1044 	if (ret < 0) {
1045 		SET_ERRNO(-ret);
1046 		ret = -1;
1047 	}
1048 	return ret;
1049 }
1050 
1051 static __attribute__((unused))
write(int fd,const void * buf,size_t count)1052 ssize_t write(int fd, const void *buf, size_t count)
1053 {
1054 	ssize_t ret = sys_write(fd, buf, count);
1055 
1056 	if (ret < 0) {
1057 		SET_ERRNO(-ret);
1058 		ret = -1;
1059 	}
1060 	return ret;
1061 }
1062 
1063 /* some size-optimized reimplementations of a few common str* and mem*
1064  * functions. They're marked static, except memcpy() and raise() which are used
1065  * by libgcc on ARM, so they are marked weak instead in order not to cause an
1066  * error when building a program made of multiple files (not recommended).
1067  */
1068 
1069 static __attribute__((unused))
memmove(void * dst,const void * src,size_t len)1070 void *memmove(void *dst, const void *src, size_t len)
1071 {
1072 	ssize_t pos = (dst <= src) ? -1 : (long)len;
1073 	void *ret = dst;
1074 
1075 	while (len--) {
1076 		pos += (dst <= src) ? 1 : -1;
1077 		((char *)dst)[pos] = ((char *)src)[pos];
1078 	}
1079 	return ret;
1080 }
1081 
1082 static __attribute__((unused))
memset(void * dst,int b,size_t len)1083 void *memset(void *dst, int b, size_t len)
1084 {
1085 	char *p = dst;
1086 
1087 	while (len--)
1088 		*(p++) = b;
1089 	return dst;
1090 }
1091 
1092 static __attribute__((unused))
memcmp(const void * s1,const void * s2,size_t n)1093 int memcmp(const void *s1, const void *s2, size_t n)
1094 {
1095 	size_t ofs = 0;
1096 	int c1 = 0;
1097 
1098 	while (ofs < n && !(c1 = ((unsigned char *)s1)[ofs] - ((unsigned char *)s2)[ofs])) {
1099 		ofs++;
1100 	}
1101 	return c1;
1102 }
1103 
1104 static __attribute__((unused))
strcpy(char * dst,const char * src)1105 char *strcpy(char *dst, const char *src)
1106 {
1107 	char *ret = dst;
1108 
1109 	while ((*dst++ = *src++));
1110 	return ret;
1111 }
1112 
1113 static __attribute__((unused))
strchr(const char * s,int c)1114 char *strchr(const char *s, int c)
1115 {
1116 	while (*s) {
1117 		if (*s == (char)c)
1118 			return (char *)s;
1119 		s++;
1120 	}
1121 	return NULL;
1122 }
1123 
1124 static __attribute__((unused))
strrchr(const char * s,int c)1125 char *strrchr(const char *s, int c)
1126 {
1127 	const char *ret = NULL;
1128 
1129 	while (*s) {
1130 		if (*s == (char)c)
1131 			ret = s;
1132 		s++;
1133 	}
1134 	return (char *)ret;
1135 }
1136 
1137 static __attribute__((unused))
nolibc_strlen(const char * str)1138 size_t nolibc_strlen(const char *str)
1139 {
1140 	size_t len;
1141 
1142 	for (len = 0; str[len]; len++);
1143 	return len;
1144 }
1145 
1146 #define strlen(str) ({                          \
1147 	__builtin_constant_p((str)) ?           \
1148 		__builtin_strlen((str)) :       \
1149 		nolibc_strlen((str));           \
1150 })
1151 
1152 static __attribute__((unused))
isdigit(int c)1153 int isdigit(int c)
1154 {
1155 	return (unsigned int)(c - '0') <= 9;
1156 }
1157 
1158 static __attribute__((unused))
atol(const char * s)1159 long atol(const char *s)
1160 {
1161 	unsigned long ret = 0;
1162 	unsigned long d;
1163 	int neg = 0;
1164 
1165 	if (*s == '-') {
1166 		neg = 1;
1167 		s++;
1168 	}
1169 
1170 	while (1) {
1171 		d = (*s++) - '0';
1172 		if (d > 9)
1173 			break;
1174 		ret *= 10;
1175 		ret += d;
1176 	}
1177 
1178 	return neg ? -ret : ret;
1179 }
1180 
1181 static __attribute__((unused))
atoi(const char * s)1182 int atoi(const char *s)
1183 {
1184 	return atol(s);
1185 }
1186 
1187 static __attribute__((unused))
ltoa(long in)1188 const char *ltoa(long in)
1189 {
1190 	/* large enough for -9223372036854775808 */
1191 	static char buffer[21];
1192 	char       *pos = buffer + sizeof(buffer) - 1;
1193 	int         neg = in < 0;
1194 	unsigned long n = neg ? -in : in;
1195 
1196 	*pos-- = '\0';
1197 	do {
1198 		*pos-- = '0' + n % 10;
1199 		n /= 10;
1200 		if (pos < buffer)
1201 			return pos + 1;
1202 	} while (n);
1203 
1204 	if (neg)
1205 		*pos-- = '-';
1206 	return pos + 1;
1207 }
1208 
1209 __attribute__((weak,unused))
memcpy(void * dst,const void * src,size_t len)1210 void *memcpy(void *dst, const void *src, size_t len)
1211 {
1212 	return memmove(dst, src, len);
1213 }
1214 
1215 /* needed by libgcc for divide by zero */
1216 __attribute__((weak,unused))
raise(int signal)1217 int raise(int signal)
1218 {
1219 	return kill(getpid(), signal);
1220 }
1221 
1222 /* Here come a few helper functions */
1223 
1224 static __attribute__((unused))
FD_ZERO(fd_set * set)1225 void FD_ZERO(fd_set *set)
1226 {
1227 	memset(set, 0, sizeof(*set));
1228 }
1229 
1230 static __attribute__((unused))
FD_SET(int fd,fd_set * set)1231 void FD_SET(int fd, fd_set *set)
1232 {
1233 	if (fd < 0 || fd >= FD_SETSIZE)
1234 		return;
1235 	set->fd32[fd / 32] |= 1 << (fd & 31);
1236 }
1237 
1238 /* WARNING, it only deals with the 4096 first majors and 256 first minors */
1239 static __attribute__((unused))
makedev(unsigned int major,unsigned int minor)1240 dev_t makedev(unsigned int major, unsigned int minor)
1241 {
1242 	return ((major & 0xfff) << 8) | (minor & 0xff);
1243 }
1244