Upgrade ACATS testsuite to latest ACATS 2.6
[gcc.git] / libgo / mksysinfo.sh
index 71c328627a6c7b56ee9ed8653a5161b8138e21dd..b32a0266b719f5856cc35961cfcffd36883d8938 100755 (executable)
 # Use of this source code is governed by a BSD-style
 # license that can be found in the LICENSE file.
 
-# Create sysinfo.go.
+# Create sysinfo.go from gen-sysinfo.go and errno.i.
 
 # This shell script creates the sysinfo.go file which holds types and
-# constants extracted from the system header files.  This relies on a
-# hook in gcc: the -fdump-go-spec option will generate debugging
-# information in Go syntax.
+# constants extracted from the system header files.  This reads the
+# raw data from gen-sysinfo.go which is generated using the
+# -fdump-go-spec option.
 
-# We currently #include all the files at once, which works, but leads
-# to exposing some names which ideally should not be exposed, as they
-# match grep patterns.  E.g., WCHAR_MIN gets exposed because it starts
-# with W, like the wait flags.
+# This currently exposes some names that ideally should not be
+# exposed, as they match grep patterns.  E.g., WCHAR_MIN gets exposed
+# because it starts with W, like the wait flags.
 
-CC=${CC:-gcc}
 OUT=tmp-sysinfo.go
 
 set -e
 
-rm -f sysinfo.c
-cat > sysinfo.c <<EOF
-#include "config.h"
-
-#include <sys/types.h>
-#include <dirent.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <netinet/in.h>
-/* <netinet/tcp.h> needs u_char/u_short, but <sys/bsd_types> is only
-   included by <netinet/in.h> if _SGIAPI (i.e. _SGI_SOURCE
-   && !_XOPEN_SOURCE.
-   <sys/termios.h> only defines TIOCNOTTY if !_XOPEN_SOURCE, while
-   <sys/ttold.h> does so unconditionally.  */
-#ifdef __sgi__
-#include <sys/bsd_types.h>
-#include <sys/ttold.h>
-#endif
-#include <netinet/tcp.h>
-#if defined(HAVE_NETINET_IN_SYSTM_H)
-#include <netinet/in_systm.h>
-#endif
-#if defined(HAVE_NETINET_IP_H)
-#include <netinet/ip.h>
-#endif
-#if defined(HAVE_NETINET_IP_MROUTE_H)
-#include <netinet/ip_mroute.h>
-#endif
-#if defined(HAVE_NETINET_IF_ETHER_H)
-#include <netinet/if_ether.h>
-#endif
-#include <signal.h>
-#include <sys/ioctl.h>
-#include <termios.h>
-#if defined(HAVE_SYSCALL_H)
-#include <syscall.h>
-#endif
-#if defined(HAVE_SYS_SYSCALL_H)
-#include <sys/syscall.h>
-#endif
-#if defined(HAVE_SYS_EPOLL_H)
-#include <sys/epoll.h>
-#endif
-#if defined(HAVE_SYS_FILE_H)
-#include <sys/file.h>
-#endif
-#if defined(HAVE_SYS_MMAN_H)
-#include <sys/mman.h>
-#endif
-#if defined(HAVE_SYS_PRCTL_H)
-#include <sys/prctl.h>
-#endif
-#if defined(HAVE_SYS_PTRACE_H)
-#include <sys/ptrace.h>
-#endif
-#include <sys/resource.h>
-#include <sys/uio.h>
-#include <sys/socket.h>
-#include <sys/stat.h>
-#include <sys/time.h>
-#include <sys/times.h>
-#include <sys/wait.h>
-#include <sys/un.h>
-#if defined(HAVE_SYS_USER_H)
-#include <sys/user.h>
-#endif
-#if defined(HAVE_SYS_UTSNAME_H)
-#include <sys/utsname.h>
-#endif
-#if defined(HAVE_SYS_SELECT_H)
-#include <sys/select.h>
-#endif
-#include <time.h>
-#include <unistd.h>
-#include <netdb.h>
-#include <pwd.h>
-#if defined(HAVE_LINUX_FILTER_H)
-#include <linux/filter.h>
-#endif
-#if defined(HAVE_LINUX_IF_ADDR_H)
-#include <linux/if_addr.h>
-#endif
-#if defined(HAVE_LINUX_IF_ETHER_H)
-#include <linux/if_ether.h>
-#endif
-#if defined(HAVE_LINUX_IF_TUN_H)
-#include <linux/if_tun.h>
-#endif
-#if defined(HAVE_LINUX_NETLINK_H)
-#include <linux/netlink.h>
-#endif
-#if defined(HAVE_LINUX_RTNETLINK_H)
-#include <linux/rtnetlink.h>
-#endif
-#if defined(HAVE_NET_IF_H)
-#include <net/if.h>
-#endif
-#if defined(HAVE_NET_IF_ARP_H)
-#include <net/if_arp.h>
-#endif
-#if defined(HAVE_NET_ROUTE_H)
-#include <net/route.h>
-#endif
-#if defined (HAVE_NETPACKET_PACKET_H)
-#include <netpacket/packet.h>
-#endif
-#if defined(HAVE_SYS_MOUNT_H)
-#include <sys/mount.h>
-#endif
-#if defined(HAVE_SYS_VFS_H)
-#include <sys/vfs.h>
-#endif
-#if defined(HAVE_STATFS_H)
-#include <sys/statfs.h>
-#endif
-#if defined(HAVE_SYS_TIMEX_H)
-#include <sys/timex.h>
-#endif
-#if defined(HAVE_SYS_SYSINFO_H)
-#include <sys/sysinfo.h>
-#endif
-#if defined(HAVE_USTAT_H)
-#include <ustat.h>
-#endif
-#if defined(HAVE_UTIME_H)
-#include <utime.h>
-#endif
-#if defined(HAVE_LINUX_ETHER_H)
-#include <linux/ether.h>
-#endif
-#if defined(HAVE_LINUX_FS_H)
-#include <linux/fs.h>
-#endif
-#if defined(HAVE_LINUX_REBOOT_H)
-#include <linux/reboot.h>
-#endif
-#if defined(HAVE_SYS_INOTIFY_H)
-#include <sys/inotify.h>
-#endif
-
-/* Constants that may only be defined as expressions on some systems,
-   expressions too complex for -fdump-go-spec to handle.  These are
-   handled specially below.  */
-enum {
-#ifdef TIOCGWINSZ
-  TIOCGWINSZ_val = TIOCGWINSZ,
-#endif
-#ifdef TIOCNOTTY
-  TIOCNOTTY_val = TIOCNOTTY,
-#endif
-#ifdef TIOCSCTTY
-  TIOCSCTTY_val = TIOCSCTTY,
-#endif
-};
-EOF
-
-${CC} -fdump-go-spec=gen-sysinfo.go -std=gnu99 -S -o sysinfo.s sysinfo.c
-
 echo 'package syscall' > ${OUT}
 echo 'import "unsafe"' >> ${OUT}
 echo 'type _ unsafe.Pointer' >> ${OUT}
@@ -189,11 +29,13 @@ echo 'type _ unsafe.Pointer' >> ${OUT}
 # will all have a leading underscore.
 grep -v '^// ' gen-sysinfo.go | \
   grep -v '^func' | \
+  grep -v '^var' | \
   grep -v '^type _timeval ' | \
   grep -v '^type _timespec_t ' | \
   grep -v '^type _timespec ' | \
   grep -v '^type _timestruc_t ' | \
   grep -v '^type _epoll_' | \
+  grep -v '^type _*locale[_ ]' | \
   grep -v 'in6_addr' | \
   grep -v 'sockaddr_in6' | \
   sed -e 's/\([^a-zA-Z0-9_]\)_timeval\([^a-zA-Z0-9_]\)/\1Timeval\2/g' \
@@ -202,11 +44,24 @@ grep -v '^// ' gen-sysinfo.go | \
       -e 's/\([^a-zA-Z0-9_]\)_timestruc_t\([^a-zA-Z0-9_]\)/\1Timestruc\2/g' \
     >> ${OUT}
 
+# On AIX, the _arpcom struct, is filtered by the above grep sequence, as it as
+# a field of type _in6_addr, but other types depend on _arpcom, so we need to
+# put it back.
+grep '^type _arpcom ' gen-sysinfo.go | \
+  sed -e 's/_in6_addr/[16]byte/' >> ${OUT}
+
+# Same on Solaris for _mld_hdr_t.
+grep '^type _mld_hdr_t ' gen-sysinfo.go | \
+  sed -e 's/_in6_addr/[16]byte/' >> ${OUT}
+
 # The errno constants.  These get type Errno.
-echo '#include <errno.h>' | ${CC} -x c - -E -dM | \
-  egrep '#define E[A-Z0-9_]+ ' | \
+egrep '#define E[A-Z0-9_]+ [0-9E]' errno.i | \
   sed -e 's/^#define \(E[A-Z0-9_]*\) .*$/const \1 = Errno(_\1)/' >> ${OUT}
 
+# Workaround for GNU/Hurd _EMIG_* errors having negative values
+egrep '#define E[A-Z0-9_]+ -[0-9]' errno.i | \
+  sed -e 's/^#define \(E[A-Z0-9_]*\) .*$/const \1 = Errno(-_\1)/' >> ${OUT}
+
 # The O_xxx flags.
 egrep '^const _(O|F|FD)_' gen-sysinfo.go | \
   sed -e 's/^\(const \)_\([^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
@@ -217,6 +72,25 @@ if ! grep '^const O_CLOEXEC' ${OUT} >/dev/null 2>&1; then
   echo "const O_CLOEXEC = 0" >> ${OUT}
 fi
 
+# The os package requires F_DUPFD_CLOEXEC to be defined.
+if ! grep '^const F_DUPFD_CLOEXEC' ${OUT} >/dev/null 2>&1; then
+  echo "const F_DUPFD_CLOEXEC = 0" >> ${OUT}
+fi
+
+# The internal/poll package requires F_GETPIPE_SZ to be defined.
+if ! grep '^const F_GETPIPE_SZ' ${OUT} >/dev/null 2>&1; then
+  echo "const F_GETPIPE_SZ = 0" >> ${OUT}
+fi
+
+# AIX 7.1 is a 64 bits value for _FCLOEXEC (referenced by O_CLOEXEC)
+# which leads to a constant overflow when using O_CLOEXEC in some
+# go code. Issue wan not present in 6.1 (no O_CLOEXEC) and is no
+# more present in 7.2 (_FCLOEXEC is a 32 bit value).
+if test "${GOOS}" = "aix" && `oslevel | grep -q "^7.1"`; then
+    sed -e 's/const __FCLOEXEC = .*/const __FCLOEXEC = 0/' ${OUT} > ${OUT}-2
+    mv ${OUT}-2 ${OUT}
+fi
+
 # These flags can be lost on i386 GNU/Linux when using
 # -D_FILE_OFFSET_BITS=64, because we see "#define F_SETLK F_SETLK64"
 # before we see the definition of F_SETLK64.
@@ -227,6 +101,16 @@ for flag in F_GETLK F_SETLK F_SETLKW; do
   fi
 done
 
+# The Flock_t struct for fcntl.
+grep '^type _flock ' gen-sysinfo.go | \
+    sed -e 's/type _flock/type Flock_t/' \
+      -e 's/l_type/Type/' \
+      -e 's/l_whence/Whence/' \
+      -e 's/l_start/Start/' \
+      -e 's/l_len/Len/' \
+      -e 's/l_pid/Pid/' \
+    >> ${OUT}
+
 # The signal numbers.
 grep '^const _SIG[^_]' gen-sysinfo.go | \
   grep -v '^const _SIGEV_' | \
@@ -250,6 +134,11 @@ grep '^const _SYS_' gen-sysinfo.go | \
     echo "const $sup = _$sys" >> ${OUT}
   done
 
+# Special treatment of SYS_IOCTL for GNU/Hurd.
+if ! grep '^const SYS_IOCTL' ${OUT} > /dev/null 2>&1; then
+  echo "const SYS_IOCTL = 0" >> ${OUT}
+fi
+
 # The GNU/Linux support wants to use SYS_GETDENTS64 if available.
 if ! grep '^const SYS_GETDENTS ' ${OUT} >/dev/null 2>&1; then
   echo "const SYS_GETDENTS = 0" >> ${OUT}
@@ -258,6 +147,12 @@ if ! grep '^const SYS_GETDENTS64 ' ${OUT} >/dev/null 2>&1; then
   echo "const SYS_GETDENTS64 = 0" >> ${OUT}
 fi
 
+# The syscall package wants the geteuid system call number.  It isn't
+# defined on Alpha, which only provides the getresuid system call.
+if ! grep '^const SYS_GETEUID ' ${OUT} >/dev/null 2>&1; then
+  echo "const SYS_GETEUID = 0" >> ${OUT}
+fi
+
 # Stat constants.
 grep '^const _S_' gen-sysinfo.go | \
   sed -e 's/^\(const \)_\(S_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
@@ -287,9 +182,19 @@ if grep '^const ___WALL = ' gen-sysinfo.go >/dev/null 2>&1 \
    && ! grep '^const _WALL = ' gen-sysinfo.go >/dev/null 2>&1; then
   echo 'const WALL = ___WALL' >> ${OUT}
 fi
+# On GNU/Linux the os package requires WEXITED and WNOWAIT.
+if test "${GOOS}" = "linux"; then
+  if ! grep '^const WEXITED = ' ${OUT} >/dev/null 2>&1; then
+    echo 'const WEXITED = 4' >> ${OUT}
+  fi
+  if ! grep '^const WNOWAIT = ' ${OUT} >/dev/null 2>&1; then
+    echo 'const WNOWAIT = 0x01000000' >> ${OUT}
+  fi
+fi
 
 # Networking constants.
 egrep '^const _(AF|ARPHRD|ETH|IN|SOCK|SOL|SO|IPPROTO|TCP|IP|IPV6)_' gen-sysinfo.go |
+  grep -v '_val =' |
   sed -e 's/^\(const \)_\([^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
 grep '^const _SOMAXCONN' gen-sysinfo.go |
   sed -e 's/^\(const \)_\(SOMAXCONN[^= ]*\)\(.*\)$/\1\2 = _\2/' \
@@ -309,6 +214,14 @@ for m in SOCK_CLOEXEC SOCK_NONBLOCK; do
   fi
 done
 
+# On 32-bit GNU/Linux the expression for SO_RCVTIMEO is too complicated
+# for -fdump-go-spec.
+if ! grep '^const SO_RCVTIMEO ' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _SO_RCVTIMEO_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const SO_RCVTIMEO = _SO_RCVTIMEO_val' >> ${OUT}
+  fi
+fi
+
 # The syscall package requires AF_LOCAL.
 if ! grep '^const AF_LOCAL ' ${OUT} >/dev/null 2>&1; then
   if grep '^const AF_UNIX ' ${OUT} >/dev/null 2>&1; then
@@ -316,6 +229,31 @@ if ! grep '^const AF_LOCAL ' ${OUT} >/dev/null 2>&1; then
   fi
 fi
 
+# The syscall package requires _AT_FDCWD, but doesn't export it.
+if ! grep '^const _AT_FDCWD = ' ${OUT} >/dev/null 2>&1; then
+  echo "const _AT_FDCWD = -100" >> ${OUT}
+fi
+
+# sysctl constants.
+grep '^const _CTL' gen-sysinfo.go |
+  sed -e 's/^\(const \)_\(CTL[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
+  grep '^const _SYSCTL' gen-sysinfo.go |
+  sed -e 's/^\(const \)_\(SYSCTL[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
+  grep '^const _NET_RT' gen-sysinfo.go |
+  sed -e 's/^\(const \)_\(NET_RT[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
+
+# The sysctlnode struct.
+grep '^type _sysctlnode ' gen-sysinfo.go | \
+    sed -e 's/_sysctlnode/Sysctlnode/' \
+               -e 's/sysctl_flags/Flags/' \
+    -e 's/sysctl_name/Name/' \
+    -e 's/sysctl_num/Num/' \
+               >> ${OUT}
+
+# sysconf constants.
+grep '^const __SC' gen-sysinfo.go |
+  sed -e 's/^\(const \)__\(SC[^= ]*\)\(.*\)$/\1\2 = __\2/' >> ${OUT}
+
 # pathconf constants.
 grep '^const __PC' gen-sysinfo.go |
   sed -e 's/^\(const \)__\(PC[^= ]*\)\(.*\)$/\1\2 = __\2/' >> ${OUT}
@@ -327,7 +265,15 @@ fi
 
 # epoll constants.
 grep '^const _EPOLL' gen-sysinfo.go |
+  grep -v EPOLLET |
   sed -e 's/^\(const \)_\(EPOLL[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
+# Make sure EPOLLET is positive.
+if grep '^const _EPOLLET = [0-9]' gen-sysinfo.go >/dev/null 2>&1; then
+  grep '^const _EPOLLET ' gen-sysinfo.go |
+    sed -e 's/^\(const \)_\(EPOLL[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
+else
+  echo "const EPOLLET = 0x80000000" >> ${OUT}
+fi
 # Make sure EPOLLRDHUP and EPOLL_CLOEXEC are defined.
 if ! grep '^const EPOLLRDHUP' ${OUT} >/dev/null 2>&1; then
   echo "const EPOLLRDHUP = 0x2000" >> ${OUT}
@@ -397,12 +343,45 @@ if ! grep '^const _PTRACE_TRACEME' ${OUT} > /dev/null 2>&1; then
   echo "const _PTRACE_TRACEME = 0" >> ${OUT}
 fi
 
+# A helper function that prints a structure from gen-sysinfo.go with the first
+# letter of the field names in upper case.  $1 is the name of structure.  If $2
+# is not empty, the structure or type is renamed to $2.
+upcase_fields () {
+  name="$1"
+  def=`grep "^type $name " gen-sysinfo.go`
+  fields=`echo $def | sed -e 's/^[^{]*{\(.*\)}$/\1/'`
+  prefix=`echo $def | sed -e 's/{.*//'`
+  if test "$2" != ""; then
+    prefix=`echo $prefix | sed -e "s/$1/$2/"`
+  fi
+  if test "$fields" != ""; then
+    nfields=
+    while test -n "$fields"; do
+      field=`echo $fields | sed -e 's/^\([^;]*\);.*$/\1/'`
+      fields=`echo $fields | sed -e 's/^[^;]*; *\(.*\)$/\1/'`
+      # capitalize the next character.
+      f=`echo $field | sed -e 's/^\(.\).*$/\1/'`
+      r=`echo $field | sed -e 's/^.\(.*\)$/\1/'`
+      f=`echo $f | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
+      field="$f$r"
+      nfields="$nfields $field;"
+    done
+    echo "${prefix} {$nfields }"
+  fi
+}
+
 # The registers returned by PTRACE_GETREGS.  This is probably
 # GNU/Linux specific; it should do no harm if there is no
 # _user_regs_struct.
 regs=`grep '^type _user_regs_struct struct' gen-sysinfo.go || true`
+if test "$regs" = ""; then
+  # mips*
+  regs=`grep '^type _pt_regs struct' gen-sysinfo.go || true`
+fi
 if test "$regs" != ""; then
-  regs=`echo $regs | sed -e 's/type _user_regs_struct struct //' -e 's/[{}]//g'`
+  regs=`echo $regs | sed -e 's/type _pt_regs struct//'`
+  regs=`echo $regs |
+    sed -e 's/type __*user_regs_struct struct //' -e 's/[{}]//g'`
   regs=`echo $regs | sed -e s'/^ *//'`
   nregs=
   while test -n "$regs"; do
@@ -413,6 +392,10 @@ if test "$regs" != ""; then
     r=`echo $field | sed -e 's/^.\(.*\)$/\1/'`
     f=`echo $f | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
     field="$f$r"
+    field=`echo "$field" | sed \
+      -e 's/__user_psw_struct/PtracePsw/' \
+      -e 's/__user_fpregs_struct/PtraceFpregs/' \
+      -e 's/__user_per_struct/PtracePer/'`
     nregs="$nregs $field;"
   done
   echo "type PtraceRegs struct {$nregs }" >> ${OUT}
@@ -449,8 +432,10 @@ fi
 sizeof_long=`grep '^const ___SIZEOF_LONG__ = ' gen-sysinfo.go | sed -e 's/.*= //'`
 if test "$sizeof_long" = "4"; then
   echo "type _C_long int32" >> ${OUT}
+  echo "type _C_ulong uint32" >> ${OUT}
 elif test "$sizeof_long" = "8"; then
   echo "type _C_long int64" >> ${OUT}
+  echo "type _C_ulong uint64" >> ${OUT}
 else
   echo 1>&2 "mksysinfo.sh: could not determine size of long (got $sizeof_long)"
   exit 1
@@ -476,8 +461,8 @@ fi
 timeval=`grep '^type _timeval ' gen-sysinfo.go`
 timeval_sec=`echo $timeval | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
 timeval_usec=`echo $timeval | sed -n -e 's/^.*tv_usec \([^ ]*\);.*$/\1/p'`
-echo "type Timeval_sec_t $timeval_sec" >> ${OUT}
-echo "type Timeval_usec_t $timeval_usec" >> ${OUT}
+echo "type Timeval_sec_t $timeval_sec" >> ${OUT}
+echo "type Timeval_usec_t $timeval_usec" >> ${OUT}
 echo $timeval | \
   sed -e 's/type _timeval /type Timeval /' \
       -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timeval_sec_t/' \
@@ -489,8 +474,8 @@ if test "$timespec" = ""; then
 fi
 timespec_sec=`echo $timespec | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
 timespec_nsec=`echo $timespec | sed -n -e 's/^.*tv_nsec \([^ ]*\);.*$/\1/p'`
-echo "type Timespec_sec_t $timespec_sec" >> ${OUT}
-echo "type Timespec_nsec_t $timespec_nsec" >> ${OUT}
+echo "type Timespec_sec_t $timespec_sec" >> ${OUT}
+echo "type Timespec_nsec_t $timespec_nsec" >> ${OUT}
 echo $timespec | \
   sed -e 's/^type ___timespec /type Timespec /' \
       -e 's/^type _timespec /type Timespec /' \
@@ -501,8 +486,8 @@ timestruc=`grep '^type _timestruc_t ' gen-sysinfo.go || true`
 if test "$timestruc" != ""; then
   timestruc_sec=`echo $timestruc | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
   timestruc_nsec=`echo $timestruc | sed -n -e 's/^.*tv_nsec \([^ ]*\);.*$/\1/p'`
-  echo "type Timestruc_sec_t $timestruc_sec" >> ${OUT}
-  echo "type Timestruc_nsec_t $timestruc_nsec" >> ${OUT}
+  echo "type Timestruc_sec_t $timestruc_sec" >> ${OUT}
+  echo "type Timestruc_nsec_t $timestruc_nsec" >> ${OUT}
   echo $timestruc | \
     sed -e 's/^type _timestruc_t /type Timestruc /' \
         -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timestruc_sec_t/' \
@@ -518,6 +503,26 @@ grep '^type _tms ' gen-sysinfo.go | \
       -e 's/tms_cstime/Cstime/' \
     >> ${OUT}
 
+# AIX uses st_timespec struct for stat.
+grep '^type _st_timespec ' gen-sysinfo.go | \
+    sed -e 's/type _st_timespec /type StTimespec /' \
+      -e 's/tv_sec/Sec/' \
+      -e 's/tv_nsec/Nsec/' >> ${OUT}
+
+# Special treatment of struct stat st_dev for GNU/Hurd
+# /usr/include/i386-gnu/bits/stat.h: #define st_dev st_fsid
+st_dev='-e s/st_dev/Dev/'
+if grep 'define st_dev st_fsid' gen-sysinfo.go > /dev/null 2>&1; then
+  st_dev='-e s/st_fsid/Dev/'
+fi
+
+# For historical reasons Go uses the suffix "timespec" instead of "tim" for
+# stat_t's time fields on NetBSD.
+st_times='-e s/st_atim/Atim/ -e s/st_mtim/Mtim/ -e s/st_ctim/Ctim/'
+if test "${GOOS}" = "netbsd"; then
+    st_times='-e s/st_atim/Atimespec/ -e s/st_mtim/Mtimespec/ -e s/st_ctim/Ctimespec/'
+fi
+
 # The stat type.
 # Prefer largefile variant if available.
 stat=`grep '^type _stat64 ' gen-sysinfo.go || true`
@@ -527,7 +532,8 @@ else
   grep '^type _stat ' gen-sysinfo.go
 fi | sed -e 's/type _stat64/type Stat_t/' \
          -e 's/type _stat/type Stat_t/' \
-         -e 's/st_dev/Dev/' \
+         ${st_dev} \
+         ${st_times} \
          -e 's/st_ino/Ino/g' \
          -e 's/st_nlink/Nlink/' \
          -e 's/st_mode/Mode/' \
@@ -537,11 +543,9 @@ fi | sed -e 's/type _stat64/type Stat_t/' \
          -e 's/st_size/Size/' \
          -e 's/st_blksize/Blksize/' \
          -e 's/st_blocks/Blocks/' \
-         -e 's/st_atim/Atim/' \
-         -e 's/st_mtim/Mtim/' \
-         -e 's/st_ctim/Ctim/' \
          -e 's/\([^a-zA-Z0-9_]\)_timeval\([^a-zA-Z0-9_]\)/\1Timeval\2/g' \
          -e 's/\([^a-zA-Z0-9_]\)_timespec_t\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
+         -e 's/\([^a-zA-Z0-9_]\)_st_timespec_t\([^a-zA-Z0-9_]\)/\1StTimespec\2/g' \
          -e 's/\([^a-zA-Z0-9_]\)_timespec\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
          -e 's/\([^a-zA-Z0-9_]\)_timestruc_t\([^a-zA-Z0-9_]\)/\1Timestruc\2/g' \
          -e 's/Godump_[0-9] struct { \([^;]*;\) };/\1/g' \
@@ -559,7 +563,9 @@ fi | sed -e 's/type _dirent64/type Dirent/' \
          -e 's/d_name \[0+1\]/d_name [0+256]/' \
          -e 's/d_name/Name/' \
          -e 's/]int8/]byte/' \
+         -e 's/d_fileno/Fileno/' \
          -e 's/d_ino/Ino/' \
+         -e 's/d_namlen/Namlen/' \
          -e 's/d_off/Off/' \
          -e 's/d_reclen/Reclen/' \
          -e 's/d_type/Type/' \
@@ -574,7 +580,7 @@ grep '^const _DT_' gen-sysinfo.go |
 rusage=`grep '^type _rusage struct' gen-sysinfo.go`
 if test "$rusage" != ""; then
   # Remove anonymous unions from GNU/Linux <bits/resource.h>.
-  rusage=`echo $rusage | sed -e 's/Godump_[0-9]* struct {\([^}]*\)};/\1/g'`
+  rusage=`echo $rusage | sed -e 's/Godump_[0-9][0-9]* struct {\([^}]*\)};/\1/g'`
   rusage=`echo $rusage | sed -e 's/type _rusage struct //' -e 's/[{}]//g'`
   rusage=`echo $rusage | sed -e 's/^ *//'`
   nrusage=
@@ -662,12 +668,7 @@ grep '^const _SCM_' gen-sysinfo.go | \
   sed -e 's/^\(const \)_\(SCM_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
 
 # The ucred struct.
-grep '^type _ucred ' gen-sysinfo.go | \
-    sed -e 's/_ucred/Ucred/' \
-      -e 's/pid/Pid/' \
-      -e 's/uid/Uid/' \
-      -e 's/gid/Gid/' \
-    >> ${OUT}
+upcase_fields "_ucred" "Ucred" >> ${OUT} || true
 
 # The ip_mreq struct.
 grep '^type _ip_mreq ' gen-sysinfo.go | \
@@ -709,6 +710,34 @@ if ! grep 'type IPMreqn ' ${OUT} >/dev/null 2>&1; then
   echo 'type IPMreqn struct { Multiaddr [4]byte; Interface [4]byte; Ifindex int32 }' >> ${OUT}
 fi
 
+# The icmp6_filter struct.
+grep '^type _icmp6_filter ' gen-sysinfo.go | \
+    sed -e 's/_icmp6_filter/ICMPv6Filter/' \
+      -e 's/data/Data/' \
+      -e 's/filt/Filt/' \
+    >> ${OUT}
+
+# We need ICMPv6Filter to compile the syscall package.
+if ! grep 'type ICMPv6Filter ' ${OUT} > /dev/null 2>&1; then
+  echo 'type ICMPv6Filter struct { Data [8]uint32 }' >> ${OUT}
+fi
+
+# The ip6_mtuinfo struct.
+grep '^type _ip6_mtuinfo ' gen-sysinfo.go | \
+    sed -e 's/_ip6_mtuinfo/IPv6MTUInfo/' \
+      -e 's/ip6m_addr/Addr/' \
+      -e 's/_sockaddr_in6/RawSockaddrInet6/' \
+      -e 's/ip6m_mtu/Mtu/' \
+    >> ${OUT}
+
+# We need IPv6MTUInfo to compile the syscall package.
+if ! grep 'type IPv6MTUInfo ' ${OUT} >/dev/null 2>&1; then
+  echo 'type IPv6MTUInfo struct { Addr RawSockaddrInet6; Mtu uint32; }' >> ${OUT}
+fi
+if ! grep 'const _sizeof_ip6_mtuinfo = ' ${OUT} >/dev/null 2>&1; then
+  echo 'const SizeofIPv6MTUInfo = 32' >> ${OUT}
+fi
+
 # Try to guess the type to use for fd_set.
 fd_set=`grep '^type _fd_set ' gen-sysinfo.go || true`
 fds_bits_type="_C_long"
@@ -723,11 +752,18 @@ grep '^type _addrinfo ' gen-sysinfo.go | \
       -e 's/ ai_/ Ai_/g' \
     >> ${OUT}
 
-# The addrinfo flags and errors.
+# The addrinfo and nameinfo flags and errors.
 grep '^const _AI_' gen-sysinfo.go | \
   sed -e 's/^\(const \)_\(AI_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
 grep '^const _EAI_' gen-sysinfo.go | \
   sed -e 's/^\(const \)_\(EAI_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
+grep '^const _NI_' gen-sysinfo.go | \
+  sed -e 's/^\(const \)_\(NI_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
+
+# If nothing else defined EAI_OVERFLOW, make sure it has a value.
+if ! grep "const EAI_OVERFLOW " ${OUT} >/dev/null 2>&1; then
+  echo "const EAI_OVERFLOW = 0" >> ${OUT}
+fi
 
 # The passwd struct.
 grep '^type _passwd ' gen-sysinfo.go | \
@@ -735,16 +771,30 @@ grep '^type _passwd ' gen-sysinfo.go | \
       -e 's/ pw_/ Pw_/g' \
     >> ${OUT}
 
+# The group struct.
+grep '^type _group ' gen-sysinfo.go | \
+    sed -e 's/_group/Group/' \
+      -e 's/ gr_/ Gr_/g' \
+    >> ${OUT}
+
 # The ioctl flags for the controlling TTY.
 grep '^const _TIOC' gen-sysinfo.go | \
     grep -v '_val =' | \
     sed -e 's/^\(const \)_\(TIOC[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
+grep '^const _TUNSET' gen-sysinfo.go | \
+    grep -v '_val =' | \
+    sed -e 's/^\(const \)_\(TUNSET[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
 # We need TIOCGWINSZ.
 if ! grep '^const TIOCGWINSZ' ${OUT} >/dev/null 2>&1; then
   if grep '^const _TIOCGWINSZ_val' ${OUT} >/dev/null 2>&1; then
     echo 'const TIOCGWINSZ = _TIOCGWINSZ_val' >> ${OUT}
   fi
 fi
+if ! grep '^const TIOCSWINSZ' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TIOCSWINSZ_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TIOCSWINSZ = _TIOCSWINSZ_val' >> ${OUT}
+  fi
+fi
 if ! grep '^const TIOCNOTTY' ${OUT} >/dev/null 2>&1; then
   if grep '^const _TIOCNOTTY_val' ${OUT} >/dev/null 2>&1; then
     echo 'const TIOCNOTTY = _TIOCNOTTY_val' >> ${OUT}
@@ -755,10 +805,171 @@ if ! grep '^const TIOCSCTTY' ${OUT} >/dev/null 2>&1; then
     echo 'const TIOCSCTTY = _TIOCSCTTY_val' >> ${OUT}
   fi
 fi
+if ! grep '^const TIOCGPGRP' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TIOCGPGRP_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TIOCGPGRP = _TIOCGPGRP_val' >> ${OUT}
+  fi
+fi
+if ! grep '^const TIOCSPGRP' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TIOCSPGRP_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TIOCSPGRP = _TIOCSPGRP_val' >> ${OUT}
+  fi
+fi
+if ! grep '^const TIOCGPTN' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TIOCGPTN_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TIOCGPTN = _TIOCGPTN_val' >> ${OUT}
+  fi
+fi
+if ! grep '^const TIOCSPTLCK' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TIOCSPTLCK_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TIOCSPTLCK = _TIOCSPTLCK_val' >> ${OUT}
+  fi
+fi
+if ! grep '^const TIOCGDEV' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TIOCGDEV_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TIOCGDEV = _TIOCGDEV_val' >> ${OUT}
+  fi
+fi
+if ! grep '^const TIOCSIG' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TIOCSIG_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TIOCSIG = _TIOCSIG_val' >> ${OUT}
+  fi
+fi
+
+if ! grep '^const TUNSETNOCSUM' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TUNSETNOCSUM_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TUNSETNOCSUM = _TUNSETNOCSUM_val' >> ${OUT}
+  fi
+fi
+
+if ! grep '^const TUNSETDEBUG' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TUNSETDEBUG_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TUNSETDEBUG = _TUNSETDEBUG_val' >> ${OUT}
+  fi
+fi
+
+if ! grep '^const TUNSETIFF' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TUNSETIFF_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TUNSETIFF = _TUNSETIFF_val' >> ${OUT}
+  fi
+fi
+
+if ! grep '^const TUNSETPERSIST' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TUNSETPERSIST_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TUNSETPERSIST = _TUNSETPERSIST_val' >> ${OUT}
+  fi
+fi
+
+if ! grep '^const TUNSETOWNER' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TUNSETOWNER_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TUNSETOWNER = _TUNSETOWNER_val' >> ${OUT}
+  fi
+fi
+
+if ! grep '^const TUNSETLINK' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TUNSETLINK_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TUNSETLINK = _TUNSETLINK_val' >> ${OUT}
+  fi
+fi
+
+if ! grep '^const TUNSETGROUP' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TUNSETGROUP_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TUNSETGROUP = _TUNSETGROUP_val' >> ${OUT}
+  fi
+fi
+
+if ! grep '^const TUNGETFEATURES' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TUNGETFEATURES_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TUNGETFEATURES = _TUNGETFEATURES_val' >> ${OUT}
+  fi
+fi
+
+if ! grep '^const TUNSETOFFLOAD' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TUNSETOFFLOAD_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TUNSETOFFLOAD = _TUNSETOFFLOAD_val' >> ${OUT}
+  fi
+fi
+
+if ! grep '^const TUNSETTXFILTER' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TUNSETTXFILTER_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TUNSETTXFILTER = _TUNSETTXFILTER_val' >> ${OUT}
+  fi
+fi
+
+if ! grep '^const TUNGETIFF' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TUNGETIFF_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TUNGETIFF = _TUNGETIFF_val' >> ${OUT}
+  fi
+fi
+
+if ! grep '^const TUNGETSNDBUF' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TUNGETSNDBUF_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TUNGETSNDBUF = _TUNGETSNDBUF_val' >> ${OUT}
+  fi
+fi
+
+if ! grep '^const TUNSETSNDBUF' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TUNSETSNDBUF_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TUNSETSNDBUF = _TUNSETSNDBUF_val' >> ${OUT}
+  fi
+fi
+
+if ! grep '^const TUNATTACHFILTER' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TUNATTACHFILTER_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TUNATTACHFILTER = _TUNATTACHFILTER_val' >> ${OUT}
+  fi
+fi
+
+if ! grep '^const TUNDETACHFILTER' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TUNDETACHFILTER_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TUNDETACHFILTER = _TUNDETACHFILTER_val' >> ${OUT}
+  fi
+fi
+
+if ! grep '^const TUNGETVNETHDRSZ' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TUNGETVNETHDRSZ_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TUNGETVNETHDRSZ = _TUNGETVNETHDRSZ_val' >> ${OUT}
+  fi
+fi
+
+if ! grep '^const TUNSETVNETHDRSZ' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TUNSETVNETHDRSZ_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TUNSETVNETHDRSZ = _TUNSETVNETHDRSZ_val' >> ${OUT}
+  fi
+fi
+
+if ! grep '^const TUNSETQUEUE' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TUNSETQUEUE_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TUNSETQUEUE = _TUNSETQUEUE_val' >> ${OUT}
+  fi
+fi
+
+
+if ! grep '^const TUNSETIFINDEX' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TUNSETIFINDEX_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TUNSETIFINDEX = _TUNSETIFINDEX_val' >> ${OUT}
+  fi
+fi
+
+if ! grep '^const TUNGETFILTER' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TUNGETFILTER_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TUNGETFILTER = _TUNGETFILTER_val' >> ${OUT}
+  fi
+fi
 
 # The ioctl flags for terminal control
-grep '^const _TC[GS]ET' gen-sysinfo.go | \
+grep '^const _TC[GS]ET' gen-sysinfo.go | grep -v _val | \
     sed -e 's/^\(const \)_\(TC[GS]ET[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
+if ! grep '^const TCGETS' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TCGETS_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TCGETS = _TCGETS_val' >> ${OUT}
+  fi
+fi
+if ! grep '^const TCSETS' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _TCSETS_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const TCSETS = _TCSETS_val' >> ${OUT}
+  fi
+fi
 
 # ioctl constants.  Might fall back to 0 if TIOCNXCL is missing, too, but
 # needs handling in syscalls.exec.go.
@@ -768,6 +979,11 @@ if ! grep '^const _TIOCSCTTY ' gen-sysinfo.go >/dev/null 2>&1; then
   fi
 fi
 
+# If nothing else defined TIOCSCTTY, make sure it has a value.
+if ! grep "const TIOCSCTTY " ${OUT} >/dev/null 2>&1; then
+  echo "const TIOCSCTTY = 0" >> ${OUT}
+fi
+
 # The nlmsghdr struct.
 grep '^type _nlmsghdr ' gen-sysinfo.go | \
     sed -e 's/_nlmsghdr/NlMsghdr/' \
@@ -809,6 +1025,39 @@ grep '^type _rtgenmsg ' gen-sysinfo.go | \
       -e 's/rtgen_family/Family/' \
     >> ${OUT}
 
+# The rt_msghdr struct.
+grep '^type _rt_msghdr ' gen-sysinfo.go | \
+    sed -e 's/_rt_msghdr/RtMsghdr/g' \
+        -e 's/rtm_msglen/Msglen/' \
+        -e 's/rtm_version/Version/' \
+        -e 's/rtm_type/Type/' \
+        -e 's/rtm_index/Index/' \
+        -e 's/rtm_flags/Flags/' \
+        -e 's/rtm_addrs/Addrs/' \
+        -e 's/rtm_pid/Pid/' \
+        -e 's/rtm_seq/Seq/' \
+        -e 's/rtm_errno/Errno/' \
+        -e 's/rtm_use/Use/' \
+        -e 's/rtm_inits/Inits/' \
+        -e 's/rtm_rmx/Rmx/' \
+        -e 's/_rt_metrics/RtMetrics/' \
+      >> ${OUT}
+
+# The rt_metrics struct.
+grep '^type _rt_metrics ' gen-sysinfo.go | \
+    sed -e 's/_rt_metrics/RtMetrics/g' \
+        -e 's/rmx_locks/Locks/' \
+        -e 's/rmx_mtu/Mtu/' \
+        -e 's/rmx_hopcount/Hopcount/' \
+        -e 's/rmx_recvpipe/Recvpipe/' \
+        -e 's/rmx_sendpipe/Sendpipe/' \
+        -e 's/rmx_ssthresh/Ssthresh/' \
+        -e 's/rmx_rtt/Rtt/' \
+        -e 's/rmx_rttvar/Rttvar/' \
+        -e 's/rmx_expire/Expire/' \
+        -e 's/rmx_pksent/Pksent/' \
+      >> ${OUT}
+
 # The routing message flags.
 grep '^const _RT_' gen-sysinfo.go | \
     sed -e 's/^\(const \)_\(RT_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
@@ -820,6 +1069,13 @@ grep '^const _RTCF' gen-sysinfo.go | \
     sed -e 's/^\(const \)_\(RTCF[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
 grep '^const _RTM' gen-sysinfo.go | \
     sed -e 's/^\(const \)_\(RTM[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
+if test "${GOOS}" = "netbsd"; then
+  if ! grep "RTM_RESOLVE" ${OUT} >/dev/null 2>&1; then
+    # NetBSD 8.0 removed RTM_RESOLVE, but it is part of the syscall package's
+    # stable API, so add it manually.
+    echo "const RTM_RESOLVE = 0xb" >> ${OUT}
+  fi
+fi
 grep '^const _RTN' gen-sysinfo.go | \
     sed -e 's/^\(const \)_\(RTN[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
 grep '^const _RTPROT' gen-sysinfo.go | \
@@ -835,6 +1091,34 @@ grep '^type _ifinfomsg ' gen-sysinfo.go | \
       -e 's/ifi_change/Change/' \
     >> ${OUT}
 
+# The if_msghdr struct. Upstream uses inconsistent capitalization for this type
+# on AIX, so we do too.
+ifmsghdr_name=IfMsghdr
+if test "${GOOS}" = "aix"; then
+    ifmsghdr_name=IfMsgHdr
+fi
+grep '^type _if_msghdr ' gen-sysinfo.go | \
+    sed -e "s/_if_msghdr/${ifmsghdr_name}/" \
+               -e 's/ifm_msglen/Msglen/' \
+               -e 's/ifm_version/Version/' \
+               -e 's/ifm_type/Type/' \
+               -e 's/ifm_addrs/Addrs/' \
+               -e 's/ifm_flags/Flags/' \
+               -e 's/ifm_index/Index/' \
+               -e 's/ifm_addrlen/Addrlen/' \
+               >> ${OUT}
+
+# The if_announcemsghdr struct.
+grep '^type _if_announcemsghdr ' gen-sysinfo.go | \
+    sed -e 's/_if_announcemsghdr/IfAnnounceMsghdr/g' \
+        -e 's/ifan_msglen/Msglen/' \
+        -e 's/ifan_version/Version/' \
+        -e 's/ifan_type/Type/' \
+        -e 's/ifan_index/Index/' \
+        -e 's/ifan_name/Name/' \
+        -e 's/ifan_what/What/' \
+      >> ${OUT}
+
 # The interface information types and flags.
 grep '^const _IFA' gen-sysinfo.go | \
     sed -e 's/^\(const \)_\(IFA[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
@@ -844,9 +1128,16 @@ grep '^const _IFF' gen-sysinfo.go | \
     sed -e 's/^\(const \)_\(IFF[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
 grep '^const _IFNAMSIZ' gen-sysinfo.go | \
     sed -e 's/^\(const \)_\(IFNAMSIZ[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
-grep '^const _SIOC' gen-sysinfo.go |
+grep '^const _SIOC' gen-sysinfo.go | \
+    grep -v '_val =' | \
     sed -e 's/^\(const \)_\(SIOC[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
 
+if ! grep '^const SIOCGIFMTU' ${OUT} >/dev/null 2>&1; then
+       if grep '^const _SIOCGIFMTU_val' ${OUT} >/dev/null 2>&1; then
+               echo 'const SIOCGIFMTU = _SIOCGIFMTU_val' >> ${OUT}
+       fi
+fi
+
 # The ifaddrmsg struct.
 grep '^type _ifaddrmsg ' gen-sysinfo.go | \
     sed -e 's/_ifaddrmsg/IfAddrmsg/' \
@@ -857,6 +1148,18 @@ grep '^type _ifaddrmsg ' gen-sysinfo.go | \
       -e 's/ifa_index/Index/' \
     >> ${OUT}
 
+# The ifa_msghdr struct.
+grep '^type _ifa_msghdr ' gen-sysinfo.go | \
+    sed -e 's/_ifa_msghdr/IfaMsghdr/g' \
+        -e 's/ifam_msglen/Msglen/' \
+        -e 's/ifam_version/Version/' \
+        -e 's/ifam_type/Type/' \
+        -e 's/ifam_addrs/Addrs/' \
+        -e 's/ifam_flags/Flags/' \
+        -e 's/ifam_metric/Metric/' \
+        -e 's/ifam_index/Index/' \
+      >> ${OUT}
+
 # The rtattr struct.
 grep '^type _rtattr ' gen-sysinfo.go | \
     sed -e 's/_rtattr/RtAttr/' \
@@ -864,6 +1167,54 @@ grep '^type _rtattr ' gen-sysinfo.go | \
       -e 's/rta_type/Type/' \
     >> ${OUT}
 
+# The bpf_version struct.
+grep '^type _bpf_version ' gen-sysinfo.go | \
+    sed -e 's/_bpf_version/BpfVersion/g' \
+        -e 's/bv_major/Major/' \
+        -e 's/bv_minor/Minor/' \
+      >> ${OUT}
+
+# The bpf_stat struct.
+grep '^type _bpf_stat ' gen-sysinfo.go | \
+    sed -e 's/_bpf_stat/BpfStat/g' \
+        -e 's/bs_recv/Recv/' \
+        -e 's/bs_drop/Drop/' \
+        -e 's/bs_capt/Capt/' \
+        -e 's/bs_padding/Padding/' \
+      >> ${OUT}
+
+# The bpf_insn struct.
+grep '^type _bpf_insn ' gen-sysinfo.go | \
+    sed -e 's/_bpf_insn/BpfInsn/g' \
+        -e 's/code/Code/' \
+        -e 's/jt/Jt/' \
+        -e 's/jf/Jf/' \
+        -e 's/k/K/' \
+      >> ${OUT}
+
+# The bpf_program struct.
+grep '^type _bpf_program ' gen-sysinfo.go | \
+    sed -e 's/_bpf_program/BpfProgram/g' \
+        -e 's/bf_len/Len/' \
+        -e 's/bf_insns/Insns/' \
+        -e 's/_bpf_insn/BpfInsn/' \
+      >> ${OUT}
+
+# The BPF ioctl constants.
+grep '^const _BIOC' gen-sysinfo.go | \
+    grep -v '_val =' | \
+    sed -e 's/^\(const \)_\(BIOC[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
+for c in BIOCFLUSH BIOCGBLEN BIOCGDLT BIOCGETIF BIOCGHDRCMPLT BIOCGRTIMEOUT \
+         BIOCGSTATS BIOCIMMEDIATE BIOCPROMISC BIOCSBLEN BIOCSDLT BIOCSETF \
+         BIOCSETIF BIOCSHDRCMPLT BIOCSRTIMEOUT BIOCVERSION
+do
+  if ! grep "^const ${c}" ${OUT} >/dev/null 2>&1; then
+    if grep "^const _${c}_val" ${OUT} >/dev/null 2>&1; then
+      echo "const ${c} = _${c}_val" >> ${OUT}
+    fi
+  fi
+done
+
 # The in_pktinfo struct.
 grep '^type _in_pktinfo ' gen-sysinfo.go | \
     sed -e 's/_in_pktinfo/Inet4Pktinfo/' \
@@ -924,12 +1275,15 @@ grep '^const _FALLOC_' gen-sysinfo.go |
 
 # The statfs struct.
 # Prefer largefile variant if available.
+# CentOS 5 does not have f_flags, so pull from f_spare.
 statfs=`grep '^type _statfs64 ' gen-sysinfo.go || true`
-if test "$statfs" != ""; then
-  grep '^type _statfs64 ' gen-sysinfo.go
-else
-  grep '^type _statfs ' gen-sysinfo.go
-fi | sed -e 's/type _statfs64/type Statfs_t/' \
+if test "$statfs" = ""; then
+  statfs=`grep '^type _statfs ' gen-sysinfo.go || true`
+fi
+if ! echo "$statfs" | grep f_flags >/dev/null 2>&1; then
+  statfs=`echo "$statfs" | sed -e 's/f_spare \[4+1\]\([^ ;]*\)/f_flags \1; f_spare [3+1]\1/'`
+fi
+echo "$statfs" | sed -e 's/type _statfs64/type Statfs_t/' \
         -e 's/type _statfs/type Statfs_t/' \
         -e 's/f_type/Type/' \
         -e 's/f_bsize/Bsize/' \
@@ -981,8 +1335,16 @@ if test "$timex" != ""; then
 fi
 
 # The rlimit struct.
-grep '^type _rlimit ' gen-sysinfo.go | \
-    sed -e 's/_rlimit/Rlimit/' \
+# On systems that use syscall/libcall_posix_largefile.go, use rlimit64
+# if it exists.
+rlimit="_rlimit"
+if test "${GOOS}" = "aix" || test "${GOOS}" = "linux" || (test "${GOOS}" = "solaris" && (test "${GOARCH}" = "386" || test "${GOARCH}" = "sparc")); then
+  if grep '^type _rlimit64 ' gen-sysinfo.go > /dev/null 2>&1; then
+    rlimit="_rlimit64"
+  fi
+fi
+grep "^type ${rlimit} " gen-sysinfo.go | \
+    sed -e "s/${rlimit}/Rlimit/" \
       -e 's/rlim_cur/Cur/' \
       -e 's/rlim_max/Max/' \
     >> ${OUT}
@@ -991,7 +1353,20 @@ grep '^type _rlimit ' gen-sysinfo.go | \
 grep '^const _RLIMIT_' gen-sysinfo.go |
     sed -e 's/^\(const \)_\(RLIMIT_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
 grep '^const _RLIM_' gen-sysinfo.go |
+    grep -v '^const _RLIM_INFINITY ' |
     sed -e 's/^\(const \)_\(RLIM_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
+rliminf=""
+if test "${rlimit}" = "_rlimit64" && grep '^const _RLIM64_INFINITY ' gen-sysinfo.go > /dev/null 2>&1; then
+  rliminf=`grep '^const _RLIM64_INFINITY ' gen-sysinfo.go | sed -e 's/.* //'`
+else
+  rliminf=`grep '^const _RLIM_INFINITY ' gen-sysinfo.go | sed -e 's/.* //'`
+fi
+# For compatibility with the gc syscall package, treat 0xffffffffffffffff as -1.
+if test "$rliminf" = "0xffffffffffffffff"; then
+  echo "const RLIM_INFINITY = -1" >> ${OUT}
+elif test -n "$rliminf"; then
+  echo "const RLIM_INFINITY = $rliminf" >> ${OUT}
+fi
 
 # The sysinfo struct.
 grep '^type _sysinfo ' gen-sysinfo.go | \
@@ -1010,20 +1385,6 @@ grep '^type _sysinfo ' gen-sysinfo.go | \
       -e 's/mem_unit/Unit/' \
     >> ${OUT}
 
-# The ustat struct.
-grep '^type _ustat ' gen-sysinfo.go | \
-    sed -e 's/_ustat/Ustat_t/' \
-      -e 's/f_tfree/Tfree/' \
-      -e 's/f_tinode/Tinoe/' \
-      -e 's/f_fname/Fname/' \
-      -e 's/f_fpack/Fpack/' \
-    >> ${OUT}
-# Force it to be defined, as on some older GNU/Linux systems the
-# header file fails when using with <linux/filter.h>.
-if ! grep 'type _ustat ' gen-sysinfo.go >/dev/null 2>&1; then
-  echo 'type Ustat_t struct { Tfree int32; Tinoe uint64; Fname [5+1]int8; Fpack [5+1]int8; }' >> ${OUT}
-fi
-
 # The utimbuf struct.
 grep '^type _utimbuf ' gen-sysinfo.go | \
     sed -e 's/_utimbuf/Utimbuf/' \
@@ -1091,9 +1452,15 @@ grep '^type _rtnexthop ' gen-sysinfo.go | \
 # The GNU/Linux netlink flags.
 grep '^const _NETLINK_' gen-sysinfo.go | \
   sed -e 's/^\(const \)_\(NETLINK_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
-grep '^const _NLA_' gen-sysinfo.go | \
+grep '^const _NLA_' gen-sysinfo.go | grep -v '_val =' | \
   sed -e 's/^\(const \)_\(NLA_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
 
+if ! grep '^const NLA_HDRLEN' ${OUT} >/dev/null 2>&1; then
+  if grep '^const _NLA_HDRLEN_val' ${OUT} >/dev/null 2>&1; then
+    echo 'const NLA_HDRLEN = _NLA_HDRLEN_val' >> ${OUT}
+  fi
+fi
+
 # The GNU/Linux packet socket flags.
 grep '^const _PACKET_' gen-sysinfo.go | \
   sed -e 's/^\(const \)_\(PACKET_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
@@ -1110,18 +1477,27 @@ grep '^type _inotify_event ' gen-sysinfo.go | \
       -e 's/\[0\]byte/[0]int8/' \
     >> ${OUT}
 
-# The Solaris 11 Update 1 _zone_net_addr_t struct.
-grep '^type _zone_net_addr_t ' gen-sysinfo.go | \
-    sed -e 's/_in6_addr/[16]byte/' \
-    >> ${OUT}
+# The GNU/Linux CLONE flags.
+grep '^const _CLONE_' gen-sysinfo.go | \
+  sed -e 's/^\(const \)_\(CLONE_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
+# We need some CLONE constants that are not defined in older versions
+# of glibc.
+if ! grep '^const CLONE_NEWUSER ' ${OUT} > /dev/null 2>&1; then
+  echo "const CLONE_NEWUSER = 0x10000000" >> ${OUT}
+fi
+if ! grep '^const CLONE_NEWNET ' ${OUT} > /dev/null 2>&1; then
+  echo "const CLONE_NEWNET = 0x40000000" >> ${OUT}
+fi
 
 # Struct sizes.
 set cmsghdr Cmsghdr ip_mreq IPMreq ip_mreqn IPMreqn ipv6_mreq IPv6Mreq \
-    ifaddrmsg IfAddrmsg ifinfomsg IfInfomsg in_pktinfo Inet4Pktinfo \
-    in6_pktinfo Inet6Pktinfo inotify_event InotifyEvent linger Linger \
-    msghdr Msghdr nlattr NlAttr nlmsgerr NlMsgerr nlmsghdr NlMsghdr \
-    rtattr RtAttr rtgenmsg RtGenmsg rtmsg RtMsg rtnexthop RtNexthop \
-    sock_filter SockFilter sock_fprog SockFprog ucred Ucred
+    ifaddrmsg IfAddrmsg ifa_msghdr IfaMsghdr ifinfomsg IfInfomsg \
+    if_msghdr IfMsghdr in_pktinfo Inet4Pktinfo in6_pktinfo Inet6Pktinfo \
+    inotify_event InotifyEvent linger Linger msghdr Msghdr nlattr NlAttr \
+    nlmsgerr NlMsgerr nlmsghdr NlMsghdr rtattr RtAttr rt_msghdr RtMsghdr \
+    rtgenmsg RtGenmsg rtmsg RtMsg rtnexthop RtNexthop \
+    sock_filter SockFilter sock_fprog SockFprog ucred Ucred \
+    icmp6_filter ICMPv6Filter ip6_mtuinfo IPv6MTUInfo
 while test $# != 0; do
     nc=$1
     ngo=$2
@@ -1143,5 +1519,39 @@ fi
 if ! grep 'const SizeofIPMreqn ' ${OUT} >/dev/null 2>&1; then
     echo 'const SizeofIPMreqn = 12' >> ${OUT}
 fi
+if ! grep 'const SizeofICMPv6Filter ' ${OUT} >/dev/null 2>&1; then
+    echo 'const SizeofICMPv6Filter = 32' >> ${OUT}
+fi
+
+# The Solaris 11 Update 1 _zone_net_addr_t struct.
+grep '^type _zone_net_addr_t ' gen-sysinfo.go | \
+    sed -e 's/_in6_addr/[16]byte/' \
+    >> ${OUT}
+
+# The Solaris 11.4 _flow_arp_desc_t struct.
+grep '^type _flow_arp_desc_t ' gen-sysinfo.go | \
+    sed -e 's/_in6_addr_t/[16]byte/g' \
+    >> ${OUT}
+
+# The Solaris 11.4 _flow_l3_desc_t struct.
+grep '^type _flow_l3_desc_t ' gen-sysinfo.go | \
+    sed -e 's/_in6_addr_t/[16]byte/g' \
+    >> ${OUT}
+
+# The Solaris 11.3 _mac_ipaddr_t struct.
+grep '^type _mac_ipaddr_t ' gen-sysinfo.go | \
+    sed -e 's/_in6_addr_t/[16]byte/g' \
+    >> ${OUT}
+
+# The Solaris 11.3 _mactun_info_t struct.
+grep '^type _mactun_info_t ' gen-sysinfo.go | \
+    sed -e 's/_in6_addr_t/[16]byte/g' \
+    >> ${OUT}
+
+# Type 'uint128' is needed in a couple of type definitions on arm64,such
+# as _user_fpsimd_struct, _elf_fpregset_t, etc.
+if ! grep '^type uint128' ${OUT} > /dev/null 2>&1; then
+    echo "type uint128 [16]byte" >> ${OUT}
+fi
 
 exit $?