github.com/alexanius/gollvm12@v0.0.0-20230419200121-b152358b84f3/gofrontend/libgo/runtime/go-varargs.c (about)

     1  /* go-varargs.c -- functions for calling C varargs functions.
     2  
     3     Copyright 2013 The Go Authors. All rights reserved.
     4     Use of this source code is governed by a BSD-style
     5     license that can be found in the LICENSE file.  */
     6  
     7  #include "config.h"
     8  
     9  #include <errno.h>
    10  #include <stdint.h>
    11  #include <unistd.h>
    12  #include <sys/types.h>
    13  #include <fcntl.h>
    14  #include <sys/ioctl.h>
    15  #ifdef HAVE_SYSCALL_H
    16  #include <syscall.h>
    17  #endif
    18  #ifdef HAVE_SYS_SYSCALL_H
    19  #include <sys/syscall.h>
    20  #endif
    21  #ifdef HAVE_SYS_PTRACE_H
    22  #include <sys/ptrace.h>
    23  #endif
    24  
    25  /* The syscall package calls C functions.  The Go compiler can not
    26     represent a C varargs functions.  On some systems it's important
    27     that the declaration of a function match the call.  This function
    28     holds non-varargs C functions that the Go code can call.  */
    29  
    30  int
    31  __go_open (char *path, int mode, mode_t perm)
    32  {
    33    return open (path, mode, perm);
    34  }
    35  
    36  int
    37  __go_fcntl (int fd, int cmd, int arg)
    38  {
    39    return fcntl (fd, cmd, arg);
    40  }
    41  
    42  int
    43  __go_fcntl_flock (int fd, int cmd, struct flock *arg)
    44  {
    45    return fcntl (fd, cmd, arg);
    46  }
    47  
    48  // This is for the net package.  We use uintptr_t to make sure that
    49  // the types match, since the Go and C "int" types are not the same.
    50  struct go_fcntl_ret {
    51    uintptr_t r;
    52    uintptr_t err;
    53  };
    54  
    55  struct go_fcntl_ret
    56  __go_fcntl_uintptr (uintptr_t fd, uintptr_t cmd, uintptr_t arg)
    57  {
    58    int r;
    59    struct go_fcntl_ret ret;
    60  
    61    r = fcntl ((int) fd, (int) cmd, (int) arg);
    62    ret.r = (uintptr_t) r;
    63    if (r < 0)
    64      ret.err = (uintptr_t) errno;
    65    else
    66      ret.err = 0;
    67    return ret;
    68  }
    69  
    70  int
    71  __go_ioctl (int d, int request, int arg)
    72  {
    73    return ioctl (d, request, arg);
    74  }
    75  
    76  int
    77  __go_ioctl_ptr (int d, int request, void *arg)
    78  {
    79    return ioctl (d, request, arg);
    80  }
    81  
    82  #ifdef HAVE_OPEN64
    83  
    84  int
    85  __go_open64 (char *path, int mode, mode_t perm)
    86  {
    87    return open64 (path, mode, perm);
    88  }
    89  
    90  #endif
    91  
    92  #ifdef HAVE_OPENAT
    93  
    94  int
    95  __go_openat (int fd, char *path, int flags, mode_t mode)
    96  {
    97    return openat (fd, path, flags, mode);
    98  }
    99  
   100  #endif
   101  
   102  #ifdef HAVE_SYSCALL
   103  
   104  // __go_syscall6 is called by both the runtime and syscall packages.
   105  // We use uintptr_t to make sure that the types match, since the Go
   106  // and C "int" types are not the same.
   107  
   108  uintptr_t
   109  __go_syscall6(uintptr_t flag, uintptr_t a1, uintptr_t a2, uintptr_t a3,
   110  	      uintptr_t a4, uintptr_t a5, uintptr_t a6)
   111  {
   112    return syscall (flag, a1, a2, a3, a4, a5, a6);
   113  }
   114  
   115  #endif
   116  
   117  
   118  #if defined(HAVE_SYS_PTRACE_H) && defined(__linux__)
   119  
   120  // Despite documented appearances, this is actually implemented as
   121  // a variadic function within glibc on Linux.
   122  
   123  long
   124  __go_ptrace(int request, pid_t pid, void *addr, void *data)
   125  {
   126    return ptrace (request, pid, addr, data);
   127  }
   128  
   129  #endif