blob: 547dad1c8965683346e153174f302dbd56ec9323 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* vi: set sw=4 ts=4: */
2/*
3 * Copyright (C) 2000-2005 by Erik Andersen <andersen@codepoet.org>
4 *
5 * GNU Lesser General Public License version 2.1 or later.
6 */
7
8#ifndef _LD_SYSCALL_H_
9#define _LD_SYSCALL_H_
10
11/* We can't use the real errno in ldso, since it has not yet
12 * been dynamicly linked in yet. */
13#include "sys/syscall.h"
14extern int _dl_errno;
15#undef __set_errno
16#define __set_errno(X) {(_dl_errno) = (X);}
17
18/* Pull in the arch specific syscall implementation */
19#include <dl-syscalls.h>
20/* For MAP_ANONYMOUS -- differs between platforms */
21#define _SYS_MMAN_H 1
22#include <bits/mman.h>
23/* Pull in whatever this particular arch's kernel thinks the kernel version of
24 * struct stat should look like. It turns out that each arch has a different
25 * opinion on the subject, and different kernel revs use different names... */
26#if defined(__sparc_v9__) && (__WORDSIZE == 64)
27#define kernel_stat64 stat
28#else
29#define kernel_stat stat
30#endif
31#include <bits/kernel_stat.h>
32#include <bits/kernel_types.h>
33
34/* Protection bits. */
35#define S_ISUID 04000 /* Set user ID on execution. */
36#define S_ISGID 02000 /* Set group ID on execution. */
37
38
39/* Here are the definitions for some syscalls that are used
40 by the dynamic linker. The idea is that we want to be able
41 to call these before the errno symbol is dynamicly linked, so
42 we use our own version here. Note that we cannot assume any
43 dynamic linking at all, so we cannot return any error codes.
44 We just punt if there is an error. */
45#define __NR__dl_exit __NR_exit
46static __always_inline _syscall1(void, _dl_exit, int, status)
47
48#define __NR__dl_close __NR_close
49static __always_inline _syscall1(int, _dl_close, int, fd)
50
51#define __NR__dl_open __NR_open
52static __always_inline _syscall3(int, _dl_open, const char *, fn, int, flags,
53 __kernel_mode_t, mode)
54
55#define __NR__dl_write __NR_write
56static __always_inline _syscall3(unsigned long, _dl_write, int, fd,
57 const void *, buf, unsigned long, count)
58
59#define __NR__dl_read __NR_read
60static __always_inline _syscall3(unsigned long, _dl_read, int, fd,
61 const void *, buf, unsigned long, count)
62
63#define __NR__dl_mprotect __NR_mprotect
64static __always_inline _syscall3(int, _dl_mprotect, const void *, addr,
65 unsigned long, len, int, prot)
66
67#define __NR__dl_stat __NR_stat
68static __always_inline _syscall2(int, _dl_stat, const char *, file_name,
69 struct stat *, buf)
70
71#define __NR__dl_fstat __NR_fstat
72static __always_inline _syscall2(int, _dl_fstat, int, fd, struct stat *, buf)
73
74#define __NR__dl_munmap __NR_munmap
75static __always_inline _syscall2(int, _dl_munmap, void *, start, unsigned long, length)
76
77#ifdef __NR_getxuid
78# define __NR_getuid __NR_getxuid
79#endif
80#define __NR__dl_getuid __NR_getuid
81static __always_inline _syscall0(uid_t, _dl_getuid)
82
83#ifndef __NR_geteuid
84# define __NR_geteuid __NR_getuid
85#endif
86#define __NR__dl_geteuid __NR_geteuid
87static __always_inline _syscall0(uid_t, _dl_geteuid)
88
89#ifdef __NR_getxgid
90# define __NR_getgid __NR_getxgid
91#endif
92#define __NR__dl_getgid __NR_getgid
93static __always_inline _syscall0(gid_t, _dl_getgid)
94
95#ifndef __NR_getegid
96# define __NR_getegid __NR_getgid
97#endif
98#define __NR__dl_getegid __NR_getegid
99static __always_inline _syscall0(gid_t, _dl_getegid)
100
101#ifdef __NR_getxpid
102# define __NR_getpid __NR_getxpid
103#endif
104#define __NR__dl_getpid __NR_getpid
105static __always_inline _syscall0(gid_t, _dl_getpid)
106
107#define __NR__dl_readlink __NR_readlink
108static __always_inline _syscall3(int, _dl_readlink, const char *, path, char *, buf,
109 size_t, bufsiz)
110
111#ifdef __NR_pread64
112#define __NR___syscall_pread __NR_pread64
113static __always_inline _syscall5(ssize_t, __syscall_pread, int, fd, void *, buf,
114 size_t, count, off_t, offset_hi, off_t, offset_lo)
115
116static __always_inline ssize_t
117_dl_pread(int fd, void *buf, size_t count, off_t offset)
118{
119 return __syscall_pread(fd, buf, count, offset, offset >> 31);
120}
121#elif defined __NR_pread
122#define __NR___syscall_pread __NR_pread
123static __always_inline _syscall5(ssize_t, __syscall_pread, int, fd, void *, buf,
124 size_t, count, off_t, offset_hi, off_t, offset_lo)
125
126static __always_inline ssize_t
127_dl_pread(int fd, void *buf, size_t count, off_t offset)
128{
129 return __syscall_pread(fd, buf, count, __LONG_LONG_PAIR(offset >> 31, offset));
130}
131#endif
132
133#ifdef __UCLIBC_HAS_SSP__
134# include <sys/time.h>
135# define __NR__dl_gettimeofday __NR_gettimeofday
136static __always_inline _syscall2(int, _dl_gettimeofday, struct timeval *, tv,
137# ifdef __USE_BSD
138 struct timezone *
139# else
140 void *
141# endif
142 , tz)
143#endif
144
145/* Some architectures always use 12 as page shift for mmap2() eventhough the
146 * real PAGE_SHIFT != 12. Other architectures use the same value as
147 * PAGE_SHIFT...
148 */
149#ifndef MMAP2_PAGE_SHIFT
150# define MMAP2_PAGE_SHIFT 12
151#endif
152
153#define MAP_FAILED ((void *) -1)
154#define _dl_mmap_check_error(X) (((void *)X) == MAP_FAILED)
155
156static __always_inline
157void *_dl_mmap(void *addr, unsigned long size, int prot,
158 int flags, int fd, unsigned long offset)
159{
160#if defined(__UCLIBC_MMAP_HAS_6_ARGS__) && defined(__NR_mmap)
161 /* first try mmap(), syscall6() style */
162 return (void *)INLINE_SYSCALL(mmap, 6, addr, size, prot, flags, fd, offset);
163
164#elif defined(__NR_mmap2) && !defined (__mcoldfire__)
165 /* then try mmap2() */
166 unsigned long shifted;
167
168 if (offset & ((1 << MMAP2_PAGE_SHIFT) - 1))
169 return MAP_FAILED;
170
171 /* gcc needs help with putting things onto the stack */
172 shifted = offset >> MMAP2_PAGE_SHIFT;
173 return (void *)INLINE_SYSCALL(mmap2, 6, addr, size, prot, flags, fd, shifted);
174
175#elif defined(__NR_mmap)
176 /* finally, fall back to mmap(), syscall1() style */
177 unsigned long buffer[6];
178 buffer[0] = (unsigned long) addr;
179 buffer[1] = (unsigned long) size;
180 buffer[2] = (unsigned long) prot;
181 buffer[3] = (unsigned long) flags;
182 buffer[4] = (unsigned long) fd;
183 buffer[5] = (unsigned long) offset;
184 return (void *)INLINE_SYSCALL(mmap, 1, buffer);
185#else
186# error "Your architecture doesn't seem to provide mmap() !?"
187#endif
188}
189
190#endif /* _LD_SYSCALL_H_ */