libc_linux_arm64.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. // Copyright 2020 The Libc Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package libc // import "modernc.org/libc"
  5. import (
  6. "os"
  7. "strings"
  8. "time"
  9. "unsafe"
  10. "golang.org/x/sys/unix"
  11. "modernc.org/libc/errno"
  12. "modernc.org/libc/fcntl"
  13. "modernc.org/libc/signal"
  14. "modernc.org/libc/sys/stat"
  15. "modernc.org/libc/sys/types"
  16. )
  17. // int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
  18. func Xsigaction(t *TLS, signum int32, act, oldact uintptr) int32 {
  19. // musl/src/internal/ksigaction.h
  20. //
  21. // struct k_sigaction {
  22. // void (*handler)(int);
  23. // unsigned long flags;
  24. // void (*restorer)(void);
  25. // unsigned mask[2];
  26. // };
  27. type k_sigaction struct {
  28. handler uintptr
  29. flags ulong
  30. restorer uintptr
  31. mask [2]uint32
  32. }
  33. var kact, koldact uintptr
  34. if act != 0 {
  35. sz := int(unsafe.Sizeof(k_sigaction{}))
  36. kact = t.Alloc(sz)
  37. defer t.Free(sz)
  38. *(*k_sigaction)(unsafe.Pointer(kact)) = k_sigaction{
  39. handler: (*signal.Sigaction)(unsafe.Pointer(act)).F__sigaction_handler.Fsa_handler,
  40. flags: ulong((*signal.Sigaction)(unsafe.Pointer(act)).Fsa_flags),
  41. restorer: (*signal.Sigaction)(unsafe.Pointer(act)).Fsa_restorer,
  42. }
  43. Xmemcpy(t, kact+unsafe.Offsetof(k_sigaction{}.mask), act+unsafe.Offsetof(signal.Sigaction{}.Fsa_mask), types.Size_t(unsafe.Sizeof(k_sigaction{}.mask)))
  44. }
  45. if oldact != 0 {
  46. panic(todo(""))
  47. }
  48. if _, _, err := unix.Syscall6(unix.SYS_RT_SIGACTION, uintptr(signum), kact, koldact, unsafe.Sizeof(k_sigaction{}.mask), 0, 0); err != 0 {
  49. t.setErrno(err)
  50. return -1
  51. }
  52. if oldact != 0 {
  53. panic(todo(""))
  54. }
  55. return 0
  56. }
  57. // int lstat(const char *pathname, struct stat *statbuf);
  58. func Xlstat64(t *TLS, pathname, statbuf uintptr) int32 {
  59. if err := unix.Lstat(GoString(pathname), (*unix.Stat_t)(unsafe.Pointer(statbuf))); err != nil {
  60. if dmesgs {
  61. dmesg("%v: %q: %v", origin(1), GoString(pathname), err)
  62. }
  63. t.setErrno(err)
  64. return -1
  65. }
  66. if dmesgs {
  67. dmesg("%v: %q: ok", origin(1), GoString(pathname))
  68. }
  69. return 0
  70. }
  71. // int stat(const char *pathname, struct stat *statbuf);
  72. func Xstat64(t *TLS, pathname, statbuf uintptr) int32 {
  73. if err := unix.Fstatat(unix.AT_FDCWD, GoString(pathname), (*unix.Stat_t)(unsafe.Pointer(statbuf)), 0); err != nil {
  74. if dmesgs {
  75. dmesg("%v: %q: %v", origin(1), GoString(pathname), err)
  76. }
  77. t.setErrno(err)
  78. return -1
  79. }
  80. if dmesgs {
  81. dmesg("%v: %q: ok", origin(1), GoString(pathname))
  82. }
  83. return 0
  84. }
  85. // int unlink(const char *pathname);
  86. func Xunlink(t *TLS, pathname uintptr) int32 {
  87. if err := unix.Unlinkat(unix.AT_FDCWD, GoString(pathname), 0); err != nil {
  88. t.setErrno(err)
  89. return -1
  90. }
  91. if dmesgs {
  92. dmesg("%v: %q: ok", origin(1), GoString(pathname))
  93. }
  94. return 0
  95. }
  96. // int access(const char *pathname, int mode);
  97. func Xaccess(t *TLS, pathname uintptr, mode int32) int32 {
  98. if err := unix.Faccessat(unix.AT_FDCWD, GoString(pathname), uint32(mode), 0); err != nil {
  99. if dmesgs {
  100. dmesg("%v: %q: %v", origin(1), GoString(pathname), err)
  101. }
  102. t.setErrno(err)
  103. return -1
  104. }
  105. if dmesgs {
  106. dmesg("%v: %q %#o: ok", origin(1), GoString(pathname), mode)
  107. }
  108. return 0
  109. }
  110. // off64_t lseek64(int fd, off64_t offset, int whence);
  111. func Xlseek64(t *TLS, fd int32, offset types.Off_t, whence int32) types.Off_t {
  112. n, _, err := unix.Syscall(unix.SYS_LSEEK, uintptr(fd), uintptr(offset), uintptr(whence))
  113. if err != 0 {
  114. if dmesgs {
  115. dmesg("%v: fd %v, off %#x, whence %v: %v", origin(1), fd, offset, whenceStr(whence), err)
  116. }
  117. t.setErrno(err)
  118. return -1
  119. }
  120. if dmesgs {
  121. dmesg("%v: fd %v, off %#x, whence %v: %#x", origin(1), fd, offset, whenceStr(whence), n)
  122. }
  123. return types.Off_t(n)
  124. }
  125. // int fstat(int fd, struct stat *statbuf);
  126. func Xfstat64(t *TLS, fd int32, statbuf uintptr) int32 {
  127. if _, _, err := unix.Syscall(unix.SYS_FSTAT, uintptr(fd), statbuf, 0); err != 0 {
  128. if dmesgs {
  129. dmesg("%v: fd %d: %v", origin(1), fd, err)
  130. }
  131. t.setErrno(err)
  132. return -1
  133. }
  134. if dmesgs {
  135. dmesg("%v: %d size %#x: ok\n%+v", origin(1), fd, (*stat.Stat)(unsafe.Pointer(statbuf)).Fst_size, (*stat.Stat)(unsafe.Pointer(statbuf)))
  136. }
  137. return 0
  138. }
  139. // int ftruncate(int fd, off_t length);
  140. func Xftruncate64(t *TLS, fd int32, length types.Off_t) int32 {
  141. if _, _, err := unix.Syscall(unix.SYS_FTRUNCATE, uintptr(fd), uintptr(length), 0); err != 0 {
  142. if dmesgs {
  143. dmesg("%v: fd %d: %v", origin(1), fd, err)
  144. }
  145. t.setErrno(err)
  146. return -1
  147. }
  148. if dmesgs {
  149. dmesg("%v: %d %#x: ok", origin(1), fd, length)
  150. }
  151. return 0
  152. }
  153. // int fcntl(int fd, int cmd, ... /* arg */ );
  154. func Xfcntl64(t *TLS, fd, cmd int32, args uintptr) int32 {
  155. var arg uintptr
  156. if args != 0 {
  157. arg = *(*uintptr)(unsafe.Pointer(args))
  158. }
  159. if cmd == fcntl.F_SETFL {
  160. arg |= unix.O_LARGEFILE
  161. }
  162. n, _, err := unix.Syscall(unix.SYS_FCNTL, uintptr(fd), uintptr(cmd), arg)
  163. if err != 0 {
  164. if dmesgs {
  165. dmesg("%v: fd %v cmd %v", origin(1), fcntlCmdStr(fd), cmd)
  166. }
  167. t.setErrno(err)
  168. return -1
  169. }
  170. if dmesgs {
  171. dmesg("%v: %d %s %#x: %d", origin(1), fd, fcntlCmdStr(cmd), arg, n)
  172. }
  173. return int32(n)
  174. }
  175. // int rmdir(const char *pathname);
  176. func Xrmdir(t *TLS, pathname uintptr) int32 {
  177. if err := unix.Rmdir(GoString(pathname)); err != nil {
  178. t.setErrno(err)
  179. return -1
  180. }
  181. if dmesgs {
  182. dmesg("%v: %q: ok", origin(1), GoString(pathname))
  183. }
  184. return 0
  185. }
  186. // int rename(const char *oldpath, const char *newpath);
  187. func Xrename(t *TLS, oldpath, newpath uintptr) int32 {
  188. if err := unix.Rename(GoString(oldpath), GoString(newpath)); err != nil {
  189. t.setErrno(err)
  190. return -1
  191. }
  192. return 0
  193. }
  194. // int mknod(const char *pathname, mode_t mode, dev_t dev);
  195. func Xmknod(t *TLS, pathname uintptr, mode types.Mode_t, dev types.Dev_t) int32 {
  196. panic(todo(""))
  197. }
  198. // int chown(const char *pathname, uid_t owner, gid_t group);
  199. func Xchown(t *TLS, pathname uintptr, owner types.Uid_t, group types.Gid_t) int32 {
  200. if err := unix.Chown(GoString(pathname), int(owner), int(group)); err != nil {
  201. t.setErrno(err)
  202. return -1
  203. }
  204. return 0
  205. }
  206. // int link(const char *oldpath, const char *newpath);
  207. func Xlink(t *TLS, oldpath, newpath uintptr) int32 {
  208. panic(todo(""))
  209. }
  210. // int pipe(int pipefd[2]);
  211. func Xpipe(t *TLS, pipefd uintptr) int32 {
  212. if _, _, err := unix.Syscall(unix.SYS_PIPE2, pipefd, 0, 0); err != 0 {
  213. t.setErrno(err)
  214. return -1
  215. }
  216. return 0
  217. }
  218. // int dup2(int oldfd, int newfd);
  219. func Xdup2(t *TLS, oldfd, newfd int32) int32 {
  220. panic(todo(""))
  221. }
  222. // int getrlimit(int resource, struct rlimit *rlim);
  223. func Xgetrlimit64(t *TLS, resource int32, rlim uintptr) int32 {
  224. if _, _, err := unix.Syscall(unix.SYS_GETRLIMIT, uintptr(resource), uintptr(rlim), 0); err != 0 {
  225. t.setErrno(err)
  226. return -1
  227. }
  228. return 0
  229. }
  230. // ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize);
  231. func Xreadlink(t *TLS, path, buf uintptr, bufsize types.Size_t) types.Ssize_t {
  232. n, err := unix.Readlink(GoString(path), GoBytes(buf, int(bufsize)))
  233. if err != nil {
  234. t.setErrno(err)
  235. return -1
  236. }
  237. return types.Ssize_t(n)
  238. }
  239. // FILE *fopen64(const char *pathname, const char *mode);
  240. func Xfopen64(t *TLS, pathname, mode uintptr) uintptr {
  241. m := strings.ReplaceAll(GoString(mode), "b", "")
  242. var flags int
  243. switch m {
  244. case "r":
  245. flags = os.O_RDONLY
  246. case "r+":
  247. flags = os.O_RDWR
  248. case "w":
  249. flags = os.O_WRONLY | os.O_CREATE | os.O_TRUNC
  250. case "w+":
  251. flags = os.O_RDWR | os.O_CREATE | os.O_TRUNC
  252. case "a":
  253. flags = os.O_WRONLY | os.O_CREATE | os.O_APPEND
  254. case "a+":
  255. flags = os.O_RDWR | os.O_CREATE | os.O_APPEND
  256. default:
  257. panic(m)
  258. }
  259. //TODO- flags |= fcntl.O_LARGEFILE
  260. fd, err := unix.Openat(unix.AT_FDCWD, GoString(pathname), flags, 0666)
  261. if err != nil {
  262. t.setErrno(err)
  263. return 0
  264. }
  265. if p := newFile(t, int32(fd)); p != 0 {
  266. return p
  267. }
  268. Xclose(t, int32(fd))
  269. t.setErrno(errno.ENOMEM)
  270. return 0
  271. }
  272. // int mkdir(const char *path, mode_t mode);
  273. func Xmkdir(t *TLS, path uintptr, mode types.Mode_t) int32 {
  274. if err := unix.Mkdir(GoString(path), uint32(mode)); err != nil {
  275. t.setErrno(err)
  276. return -1
  277. }
  278. if dmesgs {
  279. dmesg("%v: %q: ok", origin(1), GoString(path))
  280. }
  281. return 0
  282. }
  283. // void *mremap(void *old_address, size_t old_size, size_t new_size, int flags, ... /* void *new_address */);
  284. func Xmremap(t *TLS, old_address uintptr, old_size, new_size types.Size_t, flags int32, args uintptr) uintptr {
  285. var arg uintptr
  286. if args != 0 {
  287. arg = *(*uintptr)(unsafe.Pointer(args))
  288. }
  289. data, _, err := unix.Syscall6(unix.SYS_MREMAP, old_address, uintptr(old_size), uintptr(new_size), uintptr(flags), arg, 0)
  290. if err != 0 {
  291. if dmesgs {
  292. dmesg("%v: %v", origin(1), err)
  293. }
  294. t.setErrno(err)
  295. return ^uintptr(0) // (void*)-1
  296. }
  297. if dmesgs {
  298. dmesg("%v: %#x", origin(1), data)
  299. }
  300. return data
  301. }
  302. func Xmmap(t *TLS, addr uintptr, length types.Size_t, prot, flags, fd int32, offset types.Off_t) uintptr {
  303. return Xmmap64(t, addr, length, prot, flags, fd, offset)
  304. }
  305. // void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
  306. func Xmmap64(t *TLS, addr uintptr, length types.Size_t, prot, flags, fd int32, offset types.Off_t) uintptr {
  307. data, _, err := unix.Syscall6(unix.SYS_MMAP, addr, uintptr(length), uintptr(prot), uintptr(flags), uintptr(fd), uintptr(offset))
  308. if err != 0 {
  309. if dmesgs {
  310. dmesg("%v: %v", origin(1), err)
  311. }
  312. t.setErrno(err)
  313. return ^uintptr(0) // (void*)-1
  314. }
  315. if dmesgs {
  316. dmesg("%v: %#x", origin(1), data)
  317. }
  318. return data
  319. }
  320. // int symlink(const char *target, const char *linkpath);
  321. func Xsymlink(t *TLS, target, linkpath uintptr) int32 {
  322. if err := unix.Symlink(GoString(target), GoString(linkpath)); err != nil {
  323. t.setErrno(err)
  324. return -1
  325. }
  326. if dmesgs {
  327. dmesg("%v: %q %q: ok", origin(1), GoString(target), GoString(linkpath))
  328. }
  329. return 0
  330. }
  331. // int chmod(const char *pathname, mode_t mode)
  332. func Xchmod(t *TLS, pathname uintptr, mode types.Mode_t) int32 {
  333. if err := unix.Chmod(GoString(pathname), uint32(mode)); err != nil {
  334. t.setErrno(err)
  335. return -1
  336. }
  337. if dmesgs {
  338. dmesg("%v: %q %#o: ok", origin(1), GoString(pathname), mode)
  339. }
  340. return 0
  341. }
  342. // time_t time(time_t *tloc);
  343. func Xtime(t *TLS, tloc uintptr) types.Time_t {
  344. n := time.Now().UTC().Unix()
  345. if tloc != 0 {
  346. *(*types.Time_t)(unsafe.Pointer(tloc)) = types.Time_t(n)
  347. }
  348. return types.Time_t(n)
  349. }
  350. // int utimes(const char *filename, const struct timeval times[2]);
  351. func Xutimes(t *TLS, filename, times uintptr) int32 {
  352. var tv []unix.Timeval
  353. if times != 0 {
  354. tv = make([]unix.Timeval, 2)
  355. *(*[2]unix.Timeval)(unsafe.Pointer(&tv[0])) = *(*[2]unix.Timeval)(unsafe.Pointer(times))
  356. }
  357. if err := unix.Utimes(GoString(filename), tv); err != nil {
  358. t.setErrno(err)
  359. return -1
  360. }
  361. if times != 0 {
  362. *(*[2]unix.Timeval)(unsafe.Pointer(times)) = *(*[2]unix.Timeval)(unsafe.Pointer(&tv[0]))
  363. }
  364. if dmesgs {
  365. dmesg("%v: %q: ok", origin(1), GoString(filename))
  366. }
  367. return 0
  368. }
  369. // int utime(const char *filename, const struct utimbuf *times);
  370. func Xutime(t *TLS, filename, times uintptr) int32 {
  371. if err := unix.Utime(GoString(filename), (*unix.Utimbuf)(unsafe.Pointer(times))); err != nil {
  372. t.setErrno(err)
  373. return -1
  374. }
  375. return 0
  376. }
  377. // unsigned int alarm(unsigned int seconds);
  378. func Xalarm(t *TLS, seconds uint32) uint32 {
  379. panic(todo(""))
  380. }