modernc.org/libc@v1.24.1/libc_linux_loong64.go (about)

     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  
     5  package libc // import "modernc.org/libc"
     6  
     7  import (
     8  	"unicode"
     9  	"unsafe"
    10  	"os"
    11  	"strings"
    12  
    13  	"golang.org/x/sys/unix"
    14  	"modernc.org/libc/errno"
    15  	"modernc.org/libc/fcntl"
    16  	"modernc.org/libc/signal"
    17  	"modernc.org/libc/sys/types"
    18  	"modernc.org/libc/wctype"
    19  )
    20  
    21  // int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
    22  func Xsigaction(t *TLS, signum int32, act, oldact uintptr) int32 {
    23  	// 	musl/arch/x86_64/ksigaction.h
    24  	//
    25  	//	struct k_sigaction {
    26  	//		void (*handler)(int);
    27  	//		unsigned long flags;
    28  	//		void (*restorer)(void);
    29  	//		unsigned mask[2];
    30  	//	};
    31  	type k_sigaction struct {
    32  		handler  uintptr
    33  		flags    ulong
    34  		restorer uintptr
    35  		mask     [2]uint32
    36  	}
    37  
    38  	var kact, koldact uintptr
    39  	if act != 0 {
    40  		sz := int(unsafe.Sizeof(k_sigaction{}))
    41  		kact = t.Alloc(sz)
    42  		defer t.Free(sz)
    43  		*(*k_sigaction)(unsafe.Pointer(kact)) = k_sigaction{
    44  			handler:  (*signal.Sigaction)(unsafe.Pointer(act)).F__sigaction_handler.Fsa_handler,
    45  			flags:    ulong((*signal.Sigaction)(unsafe.Pointer(act)).Fsa_flags),
    46  			restorer: (*signal.Sigaction)(unsafe.Pointer(act)).Fsa_restorer,
    47  		}
    48  		Xmemcpy(t, kact+unsafe.Offsetof(k_sigaction{}.mask), act+unsafe.Offsetof(signal.Sigaction{}.Fsa_mask), types.Size_t(unsafe.Sizeof(k_sigaction{}.mask)))
    49  	}
    50  	if oldact != 0 {
    51  		panic(todo(""))
    52  	}
    53  
    54  	if _, _, err := unix.Syscall6(unix.SYS_RT_SIGACTION, uintptr(signum), kact, koldact, unsafe.Sizeof(k_sigaction{}.mask), 0, 0); err != 0 {
    55  		t.setErrno(err)
    56  		return -1
    57  	}
    58  
    59  	if oldact != 0 {
    60  		panic(todo(""))
    61  	}
    62  
    63  	return 0
    64  }
    65  
    66  // int fcntl(int fd, int cmd, ... /* arg */ );
    67  func Xfcntl64(t *TLS, fd, cmd int32, args uintptr) int32 {
    68  	var arg uintptr
    69  	if args != 0 {
    70  		arg = *(*uintptr)(unsafe.Pointer(args))
    71  	}
    72  	if cmd == fcntl.F_SETFL {
    73  		arg |= unix.O_LARGEFILE
    74  	}
    75  	n, _, err := unix.Syscall(unix.SYS_FCNTL, uintptr(fd), uintptr(cmd), arg)
    76  	if err != 0 {
    77  		// if dmesgs {
    78  		// 	dmesg("%v: fd %v cmd %v", origin(1), fcntlCmdStr(fd), cmd)
    79  		// }
    80  		t.setErrno(err)
    81  		return -1
    82  	}
    83  
    84  	// if dmesgs {
    85  	// 	dmesg("%v: %d %s %#x: %d", origin(1), fd, fcntlCmdStr(cmd), arg, n)
    86  	// }
    87  	return int32(n)
    88  }
    89  
    90  // int lstat(const char *pathname, struct stat *statbuf);
    91  func Xlstat64(t *TLS, pathname, statbuf uintptr) int32 {
    92  	panic(todo(""))
    93  	// if _, _, err := unix.Syscall(unix.SYS_LSTAT, pathname, statbuf, 0); err != 0 {
    94  	// 	// if dmesgs {
    95  	// 	// 	dmesg("%v: %q: %v", origin(1), GoString(pathname), err)
    96  	// 	// }
    97  	// 	t.setErrno(err)
    98  	// 	return -1
    99  	// }
   100  
   101  	// // if dmesgs {
   102  	// // 	dmesg("%v: %q: ok", origin(1), GoString(pathname))
   103  	// // }
   104  	// return 0
   105  }
   106  
   107  // int stat(const char *pathname, struct stat *statbuf);
   108  func Xstat64(t *TLS, pathname, statbuf uintptr) int32 {
   109  	panic(todo(""))
   110  	// if _, _, err := unix.Syscall(unix.SYS_STAT, pathname, statbuf, 0); err != 0 {
   111  	// 	// if dmesgs {
   112  	// 	// 	dmesg("%v: %q: %v", origin(1), GoString(pathname), err)
   113  	// 	// }
   114  	// 	t.setErrno(err)
   115  	// 	return -1
   116  	// }
   117  
   118  	// // if dmesgs {
   119  	// // 	dmesg("%v: %q: ok", origin(1), GoString(pathname))
   120  	// // }
   121  	// return 0
   122  }
   123  
   124  // int fstat(int fd, struct stat *statbuf);
   125  func Xfstat64(t *TLS, fd int32, statbuf uintptr) int32 {
   126  	panic(todo(""))
   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  
   135  	// // if dmesgs {
   136  	// // 	dmesg("%v: %d size %#x: ok\n%+v", origin(1), fd, (*stat.Stat)(unsafe.Pointer(statbuf)).Fst_size, (*stat.Stat)(unsafe.Pointer(statbuf)))
   137  	// // }
   138  	// return 0
   139  }
   140  
   141  func Xmmap(t *TLS, addr uintptr, length types.Size_t, prot, flags, fd int32, offset types.Off_t) uintptr {
   142  	return Xmmap64(t, addr, length, prot, flags, fd, offset)
   143  }
   144  
   145  // void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
   146  func Xmmap64(t *TLS, addr uintptr, length types.Size_t, prot, flags, fd int32, offset types.Off_t) uintptr {
   147  	data, _, err := unix.Syscall6(unix.SYS_MMAP, addr, uintptr(length), uintptr(prot), uintptr(flags), uintptr(fd), uintptr(offset))
   148  	if err != 0 {
   149  		// if dmesgs {
   150  		// 	dmesg("%v: %v", origin(1), err)
   151  		// }
   152  		t.setErrno(err)
   153  		return ^uintptr(0) // (void*)-1
   154  	}
   155  
   156  	// if dmesgs {
   157  	// 	dmesg("%v: %#x", origin(1), data)
   158  	// }
   159  	return data
   160  }
   161  
   162  // void *mremap(void *old_address, size_t old_size, size_t new_size, int flags, ... /* void *new_address */);
   163  func Xmremap(t *TLS, old_address uintptr, old_size, new_size types.Size_t, flags int32, args uintptr) uintptr {
   164  	var arg uintptr
   165  	if args != 0 {
   166  		arg = *(*uintptr)(unsafe.Pointer(args))
   167  	}
   168  	data, _, err := unix.Syscall6(unix.SYS_MREMAP, old_address, uintptr(old_size), uintptr(new_size), uintptr(flags), arg, 0)
   169  	if err != 0 {
   170  		// if dmesgs {
   171  		// 	dmesg("%v: %v", origin(1), err)
   172  		// }
   173  		t.setErrno(err)
   174  		return ^uintptr(0) // (void*)-1
   175  	}
   176  
   177  	// if dmesgs {
   178  	// 	dmesg("%v: %#x", origin(1), data)
   179  	// }
   180  	return data
   181  }
   182  
   183  // int ftruncate(int fd, off_t length);
   184  func Xftruncate64(t *TLS, fd int32, length types.Off_t) int32 {
   185  	if _, _, err := unix.Syscall(unix.SYS_FTRUNCATE, uintptr(fd), uintptr(length), 0); err != 0 {
   186  		// if dmesgs {
   187  		// 	dmesg("%v: fd %d: %v", origin(1), fd, err)
   188  		// }
   189  		t.setErrno(err)
   190  		return -1
   191  	}
   192  
   193  	// if dmesgs {
   194  	// 	dmesg("%v: %d %#x: ok", origin(1), fd, length)
   195  	// }
   196  	return 0
   197  }
   198  
   199  // off64_t lseek64(int fd, off64_t offset, int whence);
   200  func Xlseek64(t *TLS, fd int32, offset types.Off_t, whence int32) types.Off_t {
   201  	n, _, err := unix.Syscall(unix.SYS_LSEEK, uintptr(fd), uintptr(offset), uintptr(whence))
   202  	if err != 0 {
   203  		// if dmesgs {
   204  		// 	dmesg("%v: fd %v, off %#x, whence %v: %v", origin(1), fd, offset, whenceStr(whence), err)
   205  		// }
   206  		t.setErrno(err)
   207  		return -1
   208  	}
   209  
   210  	// if dmesgs {
   211  	// 	dmesg("%v: fd %v, off %#x, whence %v: %#x", origin(1), fd, offset, whenceStr(whence), n)
   212  	// }
   213  	return types.Off_t(n)
   214  }
   215  
   216  // int utime(const char *filename, const struct utimbuf *times);
   217  func Xutime(t *TLS, filename, times uintptr) int32 {
   218  	panic(todo(""))
   219  	// if _, _, err := unix.Syscall(unix.SYS_UTIME, filename, times, 0); err != 0 {
   220  	// 	t.setErrno(err)
   221  	// 	return -1
   222  	// }
   223  
   224  	// return 0
   225  }
   226  
   227  // unsigned int alarm(unsigned int seconds);
   228  func Xalarm(t *TLS, seconds uint32) uint32 {
   229  	panic(todo(""))
   230  	// n, _, err := unix.Syscall(unix.SYS_ALARM, uintptr(seconds), 0, 0)
   231  	// if err != 0 {
   232  	// 	panic(todo(""))
   233  	// }
   234  
   235  	// return uint32(n)
   236  }
   237  
   238  // time_t time(time_t *tloc);
   239  func Xtime(t *TLS, tloc uintptr) types.Time_t {
   240  	panic(todo(""))
   241  	// n, _, err := unix.Syscall(unix.SYS_TIME, tloc, 0, 0)
   242  	// if err != 0 {
   243  	// 	t.setErrno(err)
   244  	// 	return types.Time_t(-1)
   245  	// }
   246  
   247  	// if tloc != 0 {
   248  	// 	*(*types.Time_t)(unsafe.Pointer(tloc)) = types.Time_t(n)
   249  	// }
   250  	// return types.Time_t(n)
   251  }
   252  
   253  // int getrlimit(int resource, struct rlimit *rlim);
   254  func Xgetrlimit64(t *TLS, resource int32, rlim uintptr) int32 {
   255  	panic(todo(""))
   256  	// if _, _, err := unix.Syscall(unix.SYS_GETRLIMIT, uintptr(resource), uintptr(rlim), 0); err != 0 {
   257  	// 	t.setErrno(err)
   258  	// 	return -1
   259  	// }
   260  
   261  	// return 0
   262  }
   263  
   264  // int mkdir(const char *path, mode_t mode);
   265  func Xmkdir(t *TLS, path uintptr, mode types.Mode_t) int32 {
   266  	panic(todo(""))
   267  	// if _, _, err := unix.Syscall(unix.SYS_MKDIR, path, uintptr(mode), 0); err != 0 {
   268  	// 	t.setErrno(err)
   269  	// 	return -1
   270  	// }
   271  
   272  	// // if dmesgs {
   273  	// // 	dmesg("%v: %q: ok", origin(1), GoString(path))
   274  	// // }
   275  	// return 0
   276  }
   277  
   278  // int symlink(const char *target, const char *linkpath);
   279  func Xsymlink(t *TLS, target, linkpath uintptr) int32 {
   280  	panic(todo(""))
   281  	// if _, _, err := unix.Syscall(unix.SYS_SYMLINK, target, linkpath, 0); err != 0 {
   282  	// 	t.setErrno(err)
   283  	// 	return -1
   284  	// }
   285  
   286  	// // if dmesgs {
   287  	// // 	dmesg("%v: %q %q: ok", origin(1), GoString(target), GoString(linkpath))
   288  	// // }
   289  	// return 0
   290  }
   291  
   292  // int chmod(const char *pathname, mode_t mode)
   293  func Xchmod(t *TLS, pathname uintptr, mode types.Mode_t) int32 {
   294  	panic(todo(""))
   295  	// if _, _, err := unix.Syscall(unix.SYS_CHMOD, pathname, uintptr(mode), 0); err != 0 {
   296  	// 	t.setErrno(err)
   297  	// 	return -1
   298  	// }
   299  
   300  	// // if dmesgs {
   301  	// // 	dmesg("%v: %q %#o: ok", origin(1), GoString(pathname), mode)
   302  	// // }
   303  	// return 0
   304  }
   305  
   306  // int utimes(const char *filename, const struct timeval times[2]);
   307  func Xutimes(t *TLS, filename, times uintptr) int32 {
   308  	panic(todo(""))
   309  	// if _, _, err := unix.Syscall(unix.SYS_UTIMES, filename, times, 0); err != 0 {
   310  	// 	t.setErrno(err)
   311  	// 	return -1
   312  	// }
   313  
   314  	// // if dmesgs {
   315  	// // 	dmesg("%v: %q: ok", origin(1), GoString(filename))
   316  	// // }
   317  	// return 0
   318  }
   319  
   320  // int unlink(const char *pathname);
   321  func Xunlink(t *TLS, pathname uintptr) int32 {
   322  	panic(todo(""))
   323  	// if _, _, err := unix.Syscall(unix.SYS_UNLINK, pathname, 0, 0); err != 0 {
   324  	// 	t.setErrno(err)
   325  	// 	return -1
   326  	// }
   327  
   328  	// // if dmesgs {
   329  	// // 	dmesg("%v: %q: ok", origin(1), GoString(pathname))
   330  	// // }
   331  	// return 0
   332  }
   333  
   334  // int access(const char *pathname, int mode);
   335  func Xaccess(t *TLS, pathname uintptr, mode int32) int32 {
   336  	panic(todo(""))
   337  	// if _, _, err := unix.Syscall(unix.SYS_ACCESS, pathname, uintptr(mode), 0); err != 0 {
   338  	// 	// if dmesgs {
   339  	// 	// 	dmesg("%v: %q: %v", origin(1), GoString(pathname), err)
   340  	// 	// }
   341  	// 	t.setErrno(err)
   342  	// 	return -1
   343  	// }
   344  
   345  	// // if dmesgs {
   346  	// // 	dmesg("%v: %q %#o: ok", origin(1), GoString(pathname), mode)
   347  	// // }
   348  	// return 0
   349  }
   350  
   351  // int rmdir(const char *pathname);
   352  func Xrmdir(t *TLS, pathname uintptr) int32 {
   353  	panic(todo(""))
   354  	// if _, _, err := unix.Syscall(unix.SYS_RMDIR, pathname, 0, 0); err != 0 {
   355  	// 	t.setErrno(err)
   356  	// 	return -1
   357  	// }
   358  
   359  	// // if dmesgs {
   360  	// // 	dmesg("%v: %q: ok", origin(1), GoString(pathname))
   361  	// // }
   362  	// return 0
   363  }
   364  
   365  // int rename(const char *oldpath, const char *newpath);
   366  func Xrename(t *TLS, oldpath, newpath uintptr) int32 {
   367  	panic(todo(""))
   368  	// if _, _, err := unix.Syscall(unix.SYS_RENAME, oldpath, newpath, 0); err != 0 {
   369  	// 	t.setErrno(err)
   370  	// 	return -1
   371  	// }
   372  
   373  	// return 0
   374  }
   375  
   376  // int mknod(const char *pathname, mode_t mode, dev_t dev);
   377  func Xmknod(t *TLS, pathname uintptr, mode types.Mode_t, dev types.Dev_t) int32 {
   378  	panic(todo(""))
   379  	// if _, _, err := unix.Syscall(unix.SYS_MKNOD, pathname, uintptr(mode), uintptr(dev)); err != 0 {
   380  	// 	t.setErrno(err)
   381  	// 	return -1
   382  	// }
   383  
   384  	// return 0
   385  }
   386  
   387  // int chown(const char *pathname, uid_t owner, gid_t group);
   388  func Xchown(t *TLS, pathname uintptr, owner types.Uid_t, group types.Gid_t) int32 {
   389  	panic(todo(""))
   390  	// // if _, _, err := unix.Syscall(unix.SYS_CHOWN, pathname, uintptr(owner), uintptr(group)); err != 0 {
   391  	// // 	t.setErrno(err)
   392  	// // 	return -1
   393  	// // }
   394  
   395  	// // return 0
   396  }
   397  
   398  // int link(const char *oldpath, const char *newpath);
   399  func Xlink(t *TLS, oldpath, newpath uintptr) int32 {
   400  	panic(todo(""))
   401  	// if _, _, err := unix.Syscall(unix.SYS_LINK, oldpath, newpath, 0); err != 0 {
   402  	// 	t.setErrno(err)
   403  	// 	return -1
   404  	// }
   405  
   406  	// return 0
   407  }
   408  
   409  // int pipe(int pipefd[2]);
   410  func Xpipe(t *TLS, pipefd uintptr) int32 {
   411  	panic(todo(""))
   412  	// if _, _, err := unix.Syscall(unix.SYS_PIPE, pipefd, 0, 0); err != 0 {
   413  	// 	t.setErrno(err)
   414  	// 	return -1
   415  	// }
   416  
   417  	// return 0
   418  }
   419  
   420  // int dup2(int oldfd, int newfd);
   421  func Xdup2(t *TLS, oldfd, newfd int32) int32 {
   422  	panic(todo(""))
   423  	// n, _, err := unix.Syscall(unix.SYS_DUP2, uintptr(oldfd), uintptr(newfd), 0)
   424  	// if err != 0 {
   425  	// 	t.setErrno(err)
   426  	// 	return -1
   427  	// }
   428  
   429  	// return int32(n)
   430  }
   431  
   432  // ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize);
   433  func Xreadlink(t *TLS, path, buf uintptr, bufsize types.Size_t) types.Ssize_t {
   434  	panic(todo(""))
   435  	// n, _, err := unix.Syscall(unix.SYS_READLINK, path, buf, uintptr(bufsize))
   436  	// if err != 0 {
   437  	// 	t.setErrno(err)
   438  	// 	return -1
   439  	// }
   440  
   441  	// return types.Ssize_t(n)
   442  }
   443  
   444  // FILE *fopen64(const char *pathname, const char *mode);
   445  func Xfopen64(t *TLS, pathname, mode uintptr) uintptr {
   446  	m := strings.ReplaceAll(GoString(mode), "b", "")
   447  	var flags int
   448  	switch m {
   449  	case "r":
   450  		flags = os.O_RDONLY
   451  	case "r+":
   452  		flags = os.O_RDWR
   453  	case "w":
   454  		flags = os.O_WRONLY | os.O_CREATE | os.O_TRUNC
   455  	case "w+":
   456  		flags = os.O_RDWR | os.O_CREATE | os.O_TRUNC
   457  	case "a":
   458  		flags = os.O_WRONLY | os.O_CREATE | os.O_APPEND
   459  	case "a+":
   460  		flags = os.O_RDWR | os.O_CREATE | os.O_APPEND
   461  	default:
   462  		panic(m)
   463  	}
   464  	fd, err := unix.Open(GoString(pathname), flags|unix.O_LARGEFILE, 0666)
   465  	if err != nil {
   466  		t.setErrno(err)
   467  		return 0
   468  	}
   469  
   470  	if p := newFile(t, int32(fd)); p != 0 {
   471  		return p
   472  	}
   473  
   474  	Xclose(t, int32(fd))
   475  	t.setErrno(errno.ENOMEM)
   476  	return 0
   477  }
   478  
   479  // int iswspace(wint_t wc);
   480  func Xiswspace(t *TLS, wc wctype.Wint_t) int32 {
   481  	return Bool32(unicode.IsSpace(rune(wc)))
   482  }
   483  
   484  // int iswalnum(wint_t wc);
   485  func Xiswalnum(t *TLS, wc wctype.Wint_t) int32 {
   486  	return Bool32(unicode.IsLetter(rune(wc)) || unicode.IsNumber(rune(wc)))
   487  }
   488  
   489  // int setrlimit(int resource, const struct rlimit *rlim);
   490  func Xsetrlimit64(t *TLS, resource int32, rlim uintptr) int32 {
   491  	panic(todo(""))
   492  	// if _, _, err := unix.Syscall(unix.SYS_SETRLIMIT, uintptr(resource), uintptr(rlim), 0); err != 0 {
   493  	// 	t.setErrno(err)
   494  	// 	return -1
   495  	// }
   496  
   497  	// return 0
   498  }