dl-syscall.h 7.4 KB

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