Support b64_ntop(), b64_pton() (bsd-compat)
[uclibc-ng.git] / ldso / include / dl-syscall.h
blob2e8bb3630c71a7e03ee4377725c7b1b47ca92b43
1 /*
2 * Copyright (C) 2000-2005 by Erik Andersen <andersen@codepoet.org>
4 * GNU Lesser General Public License version 2.1 or later.
5 */
7 #ifndef _DL_SYSCALL_H
8 #define _DL_SYSCALL_H
10 /* We can't use the real errno in ldso, since it has not yet
11 * been dynamicly linked in yet. */
12 #include "sys/syscall.h"
13 extern int _dl_errno;
14 #undef __set_errno
15 #define __set_errno(X) {(_dl_errno) = (X);}
17 /* Pull in the arch specific syscall implementation */
18 #include <dl-syscalls.h>
19 /* For MAP_ANONYMOUS -- differs between platforms */
20 #define _SYS_MMAN_H 1
21 #include <bits/mman.h>
23 #ifdef __ARCH_HAS_DEPRECATED_SYSCALLS__
24 /* Pull in whatever this particular arch's kernel thinks the kernel version of
25 * struct stat should look like. It turns out that each arch has a different
26 * opinion on the subject, and different kernel revs use different names... */
27 #define kernel_stat stat
28 #include <bits/kernel_stat.h>
29 #include <bits/kernel_types.h>
31 /* Protection bits. */
32 #define S_ISUID 04000 /* Set user ID on execution. */
33 #define S_ISGID 02000 /* Set group ID on execution. */
35 #else
36 /* 1. common-generic ABI doesn't need kernel_stat translation
37 * 3. S_IS?ID already provided by stat.h
39 #include <fcntl.h>
40 #include <sys/stat.h>
41 #include <dl-string.h>
43 #include <bits/uClibc_arch_features.h>
44 #if defined __UCLIBC_HAVE_STATX__
45 static __always_inline void
46 __cp_stat_statx (struct stat *to, struct statx *from)
48 _dl_memset (to, 0, sizeof (struct stat));
49 to->st_dev = ((from->stx_dev_minor & 0xff) | (from->stx_dev_major << 8)
50 | ((from->stx_dev_minor & ~0xff) << 12));
51 to->st_rdev = ((from->stx_rdev_minor & 0xff) | (from->stx_rdev_major << 8)
52 | ((from->stx_rdev_minor & ~0xff) << 12));
53 to->st_ino = from->stx_ino;
54 to->st_mode = from->stx_mode;
55 to->st_nlink = from->stx_nlink;
56 to->st_uid = from->stx_uid;
57 to->st_gid = from->stx_gid;
58 to->st_atime = from->stx_atime.tv_sec;
59 to->st_atim.tv_nsec = from->stx_atime.tv_nsec;
60 to->st_mtime = from->stx_mtime.tv_sec;
61 to->st_mtim.tv_nsec = from->stx_mtime.tv_nsec;
62 to->st_ctime = from->stx_ctime.tv_sec;
63 to->st_ctim.tv_nsec = from->stx_ctime.tv_nsec;
64 to->st_size = from->stx_size;
65 to->st_blocks = from->stx_blocks;
66 to->st_blksize = from->stx_blksize;
68 #endif
69 #endif
71 #define AT_NO_AUTOMOUNT 0x800
72 #define AT_EMPTY_PATH 0x1000
74 /* Here are the definitions for some syscalls that are used
75 by the dynamic linker. The idea is that we want to be able
76 to call these before the errno symbol is dynamicly linked, so
77 we use our own version here. Note that we cannot assume any
78 dynamic linking at all, so we cannot return any error codes.
79 We just punt if there is an error. */
80 #define __NR__dl_exit __NR_exit
81 static __always_inline attribute_noreturn __cold void _dl_exit(int status)
83 INLINE_SYSCALL(_dl_exit, 1, status);
84 #if __GNUC_PREREQ(4, 5)
85 __builtin_unreachable(); /* shut up warning: 'noreturn' function does return*/
86 #else
87 while (1);
88 #endif
91 #define __NR__dl_close __NR_close
92 static __always_inline _syscall1(int, _dl_close, int, fd)
94 #if defined __NR_openat && !defined __NR_open
95 static __always_inline int _dl_open(const char *fn,
96 int flags, __kernel_mode_t mode)
98 return INLINE_SYSCALL(openat, 4, AT_FDCWD, fn, flags, mode);
101 #elif defined __NR_open
102 # define __NR__dl_open __NR_open
103 static __always_inline _syscall3(int, _dl_open, const char *, fn, int, flags,
104 __kernel_mode_t, mode)
105 #endif
107 #define __NR__dl_write __NR_write
108 static __always_inline _syscall3(unsigned long, _dl_write, int, fd,
109 const void *, buf, unsigned long, count)
111 #define __NR__dl_read __NR_read
112 static __always_inline _syscall3(unsigned long, _dl_read, int, fd,
113 const void *, buf, unsigned long, count)
115 #define __NR__dl_mprotect __NR_mprotect
116 static __always_inline _syscall3(int, _dl_mprotect, const void *, addr,
117 unsigned long, len, int, prot)
119 #if defined __NR_fstatat64 && !defined __NR_stat
120 # define __NR__dl_fstatat64 __NR_fstatat64
121 static __always_inline _syscall4(int, _dl_fstatat64, int, fd, const char *,
122 fn, struct stat *, stat, int, flags)
124 static __always_inline int _dl_stat(const char *file_name,
125 struct stat *buf)
127 return _dl_fstatat64(AT_FDCWD, file_name, buf, 0);
129 #elif defined __NR_newfstatat && !defined __NR_stat
130 # define __NR__dl_newfstatat __NR_newfstatat
131 static __always_inline _syscall4(int, _dl_newfstatat, int, fd, const char *,
132 fn, struct stat *, stat, int, flags)
134 static __always_inline int _dl_stat(const char *file_name,
135 struct stat *buf)
137 return _dl_newfstatat(AT_FDCWD, file_name, buf, 0);
139 #elif defined __NR_stat
140 # define __NR__dl_stat __NR_stat
141 static __always_inline _syscall2(int, _dl_stat, const char *, file_name,
142 struct stat *, buf)
144 #elif defined __NR_statx && defined __UCLIBC_HAVE_STATX__
145 # define __NR__dl_statx __NR_statx
146 # include <fcntl.h>
147 # include <statx_cp.h>
149 static __always_inline _syscall5(int, _dl_statx, int, fd, const char *, file_name, int, flags,
150 unsigned int, mask, struct statx *, buf);
152 static __always_inline int _dl_stat(const char *file_name,
153 struct stat *buf)
155 struct statx tmp;
156 int rc = _dl_statx(AT_FDCWD, file_name, AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &tmp);
157 if (rc == 0)
158 __cp_stat_statx ((struct stat *)buf, &tmp);
159 return rc;
161 #endif
163 #if defined __NR_fstat64 && !defined __NR_fstat
164 # define __NR__dl_fstat __NR_fstat64
165 static __always_inline _syscall2(int, _dl_fstat, int, fd, struct stat *, buf)
166 #elif defined __NR_fstat
167 # define __NR__dl_fstat __NR_fstat
168 static __always_inline _syscall2(int, _dl_fstat, int, fd, struct stat *, buf)
169 #elif defined __NR_statx && defined __UCLIBC_HAVE_STATX__
170 # define __NR__dl_fstatx __NR_statx
171 static __always_inline _syscall5(int, _dl_fstatx, int, fd, const char *, file_name, int, flags, unsigned int, mask, struct stat *, buf);
173 static __always_inline int _dl_fstat(int fd,
174 struct stat *buf)
176 struct statx tmp;
177 int rc = _dl_fstatx(fd, "", AT_EMPTY_PATH, STATX_BASIC_STATS, &tmp);
178 if (rc == 0)
179 __cp_stat_statx ((struct stat *)buf, &tmp);
180 return rc;
182 #else
183 static __always_inline _syscall2(int, _dl_fstat, int, fd, struct stat *, buf)
184 #endif
186 #define __NR__dl_munmap __NR_munmap
187 static __always_inline _syscall2(int, _dl_munmap, void *, start, unsigned long, length)
189 #ifdef __NR_getxuid
190 # define __NR_getuid __NR_getxuid
191 #endif
192 #define __NR__dl_getuid __NR_getuid
193 static __always_inline _syscall0(uid_t, _dl_getuid)
195 #ifndef __NR_geteuid
196 # define __NR_geteuid __NR_getuid
197 #endif
198 #define __NR__dl_geteuid __NR_geteuid
199 static __always_inline _syscall0(uid_t, _dl_geteuid)
201 #ifdef __NR_getxgid
202 # define __NR_getgid __NR_getxgid
203 #endif
204 #define __NR__dl_getgid __NR_getgid
205 static __always_inline _syscall0(gid_t, _dl_getgid)
207 #ifndef __NR_getegid
208 # define __NR_getegid __NR_getgid
209 #endif
210 #define __NR__dl_getegid __NR_getegid
211 static __always_inline _syscall0(gid_t, _dl_getegid)
213 #ifdef __NR_getxpid
214 # define __NR_getpid __NR_getxpid
215 #endif
216 #define __NR__dl_getpid __NR_getpid
217 static __always_inline _syscall0(gid_t, _dl_getpid)
219 #if defined __NR_readlinkat
220 # define __NR__dl_readlink __NR_readlinkat
221 static __always_inline _syscall4(int, _dl_readlink, int, id, const char *, path,
222 char *, buf, size_t, bufsiz)
223 #endif
225 #ifdef __NR_pread64
226 #define __NR___syscall_pread __NR_pread64
227 #ifdef __UCLIBC_SYSCALL_ALIGN_64BIT__
228 static __always_inline _syscall6(ssize_t, __syscall_pread, int, fd, void *, buf, size_t, dummy,
229 size_t, count, off_t, offset_hi, off_t, offset_lo)
231 static __always_inline ssize_t
232 _dl_pread(int fd, void *buf, size_t count, off_t offset)
234 return __syscall_pread(fd, buf, count, 0, __LONG_LONG_PAIR((offset >> 32), (offset & 0xffffffff)));
236 #else
237 static __always_inline _syscall5(ssize_t, __syscall_pread, int, fd, void *, buf,
238 size_t, count, off_t, offset_hi, off_t, offset_lo)
240 static __always_inline ssize_t
241 _dl_pread(int fd, void *buf, size_t count, off_t offset)
243 return __syscall_pread(fd, buf, count, __LONG_LONG_PAIR(offset >> 31, offset));
245 #endif
246 #elif defined __NR_pread
247 #define __NR___syscall_pread __NR_pread
248 static __always_inline _syscall5(ssize_t, __syscall_pread, int, fd, void *, buf,
249 size_t, count, off_t, offset_hi, off_t, offset_lo)
251 static __always_inline ssize_t
252 _dl_pread(int fd, void *buf, size_t count, off_t offset)
254 return __syscall_pread(fd, buf, count, __LONG_LONG_PAIR(offset >> 31, offset));
256 #endif
258 #ifdef __UCLIBC_HAS_SSP__
259 # include <sys/time.h>
260 # define __NR__dl_gettimeofday __NR_gettimeofday
261 static __always_inline _syscall2(int, _dl_gettimeofday, struct timeval *, tv,
262 # ifdef __USE_BSD
263 struct timezone *
264 # else
265 void *
266 # endif
267 , tz)
268 #endif
270 /* Some architectures always use 12 as page shift for mmap2() eventhough the
271 * real PAGE_SHIFT != 12. Other architectures use the same value as
272 * PAGE_SHIFT...
274 #ifndef MMAP2_PAGE_SHIFT
275 # define MMAP2_PAGE_SHIFT 12
276 #endif
278 #define MAP_FAILED ((void *) -1)
279 #define _dl_mmap_check_error(X) (((void *)X) == MAP_FAILED)
281 static __always_inline
282 void *_dl_mmap(void *addr, unsigned long size, int prot,
283 int flags, int fd, unsigned long offset)
285 #if defined(__UCLIBC_MMAP_HAS_6_ARGS__) && defined(__NR_mmap)
286 /* first try mmap(), syscall6() style */
287 return (void *)INLINE_SYSCALL(mmap, 6, addr, size, prot, flags, fd, offset);
289 #elif defined(__NR_mmap2) && !defined (__mcoldfire__)
290 /* then try mmap2() */
291 unsigned long shifted;
293 if (offset & ((1 << MMAP2_PAGE_SHIFT) - 1))
294 return MAP_FAILED;
296 /* gcc needs help with putting things onto the stack */
297 shifted = offset >> MMAP2_PAGE_SHIFT;
298 return (void *)INLINE_SYSCALL(mmap2, 6, addr, size, prot, flags, fd, shifted);
300 #elif defined(__NR_mmap)
301 /* finally, fall back to mmap(), syscall1() style */
302 unsigned long buffer[6];
303 buffer[0] = (unsigned long) addr;
304 buffer[1] = (unsigned long) size;
305 buffer[2] = (unsigned long) prot;
306 buffer[3] = (unsigned long) flags;
307 buffer[4] = (unsigned long) fd;
308 buffer[5] = (unsigned long) offset;
309 return (void *)INLINE_SYSCALL(mmap, 1, buffer);
310 #else
311 # error "Your architecture doesn't seem to provide mmap() !?"
312 #endif
315 #endif /* _DL_SYSCALL_H */