mkrsysinfo.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/bin/sh
  2. # Copyright 2016 The Go Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style
  4. # license that can be found in the LICENSE file.
  5. # Create runtime_sysinfo.go from gen-sysinfo.go and errno.i.
  6. OUT=tmp-runtime_sysinfo.go
  7. set -e
  8. echo 'package runtime' > ${OUT}
  9. # Get all the consts and types, skipping ones which could not be
  10. # represented in Go and ones which we need to rewrite. We also skip
  11. # function declarations, as we don't need them here. All the symbols
  12. # will all have a leading underscore.
  13. grep -v '^// ' gen-sysinfo.go | \
  14. grep -v '^func' | \
  15. grep -v '^var ' | \
  16. grep -v '^type _timeval ' | \
  17. grep -v '^type _timespec_t ' | \
  18. grep -v '^type _timespec ' | \
  19. grep -v '^type _epoll_' | \
  20. grep -v '^type _*locale[_ ]' | \
  21. grep -v '^type _in6_addr' | \
  22. grep -v 'sockaddr_in6' | \
  23. egrep -v '^const _*FLT(64|128)_(NORM_)?MAX' | \
  24. sed -e 's/\([^a-zA-Z0-9_]\)_timeval\([^a-zA-Z0-9_]\)/\1timeval\2/g' \
  25. -e 's/\([^a-zA-Z0-9_]\)_timeval$/\1timeval/g' \
  26. -e 's/\([^a-zA-Z0-9_]\)_timespec_t\([^a-zA-Z0-9_]\)/\1timespec\2/g' \
  27. -e 's/\([^a-zA-Z0-9_]\)_timespec_t$/\1timespec_t/g' \
  28. -e 's/\([^a-zA-Z0-9_]\)_timespec\([^a-zA-Z0-9_]\)/\1timespec\2/g' \
  29. -e 's/\([^a-zA-Z0-9_]\)_timespec$/\1timespec/g' \
  30. -e 's/\([^a-zA-Z0-9_]\)_in6_addr\([^a-zA-Z0-9_]\)/\1[16]byte\2/g' \
  31. -e 's/\([^a-zA-Z0-9_]\)_in6_addr$/\1[16]byte/g' \
  32. -e 's/\([^a-zA-Z0-9_]\)_in6_addr_t\([^a-zA-Z0-9_]\)/\1[16]byte\2/g' \
  33. -e 's/\([^a-zA-Z0-9_]\)_in6_addr_t$/\1[16]byte/g' \
  34. >> ${OUT}
  35. # The C long type, needed because that is the type that ptrace returns.
  36. sizeof_long=`grep '^const ___SIZEOF_LONG__ = ' gen-sysinfo.go | sed -e 's/.*= //'`
  37. if test "$sizeof_long" = "4"; then
  38. echo "type _C_long int32" >> ${OUT}
  39. echo "type _C_ulong uint32" >> ${OUT}
  40. elif test "$sizeof_long" = "8"; then
  41. echo "type _C_long int64" >> ${OUT}
  42. echo "type _C_ulong uint64" >> ${OUT}
  43. else
  44. echo 1>&2 "mkrsysinfo.sh: could not determine size of long (got $sizeof_long)"
  45. exit 1
  46. fi
  47. # The time structures need special handling: we need to name the
  48. # types, so that we can cast integers to the right types when
  49. # assigning to the structures.
  50. timeval=`grep '^type _timeval ' gen-sysinfo.go`
  51. timeval_sec=`echo $timeval | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
  52. timeval_usec=`echo $timeval | sed -n -e 's/^.*tv_usec \([^ ]*\);.*$/\1/p'`
  53. echo "type timeval_sec_t $timeval_sec" >> ${OUT}
  54. echo "type timeval_usec_t $timeval_usec" >> ${OUT}
  55. echo $timeval | \
  56. sed -e 's/type _timeval /type timeval /' \
  57. -e 's/tv_sec *[a-zA-Z0-9_]*/tv_sec timeval_sec_t/' \
  58. -e 's/tv_usec *[a-zA-Z0-9_]*/tv_usec timeval_usec_t/' >> ${OUT}
  59. echo >> ${OUT}
  60. echo "func (tv *timeval) set_usec(x int32) {" >> ${OUT}
  61. echo " tv.tv_usec = timeval_usec_t(x)" >> ${OUT}
  62. echo "}" >> ${OUT}
  63. timespec=`grep '^type _timespec ' gen-sysinfo.go || true`
  64. if test "$timespec" = ""; then
  65. # IRIX 6.5 has __timespec instead.
  66. timespec=`grep '^type ___timespec ' gen-sysinfo.go || true`
  67. fi
  68. timespec_sec=`echo $timespec | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
  69. timespec_nsec=`echo $timespec | sed -n -e 's/^.*tv_nsec \([^ ]*\);.*$/\1/p'`
  70. echo "type timespec_sec_t $timespec_sec" >> ${OUT}
  71. echo "type timespec_nsec_t $timespec_nsec" >> ${OUT}
  72. echo $timespec | \
  73. sed -e 's/^type ___timespec /type timespec /' \
  74. -e 's/^type _timespec /type timespec /' \
  75. -e 's/tv_sec *[a-zA-Z0-9_]*/tv_sec timespec_sec_t/' \
  76. -e 's/tv_nsec *[a-zA-Z0-9_]*/tv_nsec timespec_nsec_t/' >> ${OUT}
  77. echo >> ${OUT}
  78. echo "func (ts *timespec) setNsec(ns int64) {" >> ${OUT}
  79. echo " ts.tv_sec = timespec_sec_t(ns / 1e9)" >> ${OUT}
  80. echo " ts.tv_nsec = timespec_nsec_t(ns % 1e9)" >> ${OUT}
  81. echo "}" >> ${OUT}
  82. echo >> ${OUT}
  83. # Define the epollevent struct. This needs special attention because
  84. # the C definition uses a union and is sometimes packed.
  85. if grep '^const _epoll_data_offset ' ${OUT} >/dev/null 2>&1; then
  86. val=`grep '^const _epoll_data_offset ' ${OUT} | sed -e 's/const _epoll_data_offset = \(.*\)$/\1/'`
  87. if test "$val" = "4"; then
  88. echo 'type epollevent struct { events uint32; data [8]byte }' >> ${OUT}
  89. elif test "$val" = "8"; then
  90. if test "$GOARCH" = "sparc64" -a "$GOOS" = "linux"; then
  91. echo 'type epollevent struct { events uint32; pad [4]byte; data [8]byte; _ [0]int64 }' >> ${OUT}
  92. else
  93. echo 'type epollevent struct { events uint32; pad [4]byte; data [8]byte }' >> ${OUT}
  94. fi
  95. else
  96. echo 1>&2 "unknown epoll data offset value ${val}"
  97. exit 1
  98. fi
  99. fi
  100. # Make sure EPOLLET is positive.
  101. if grep '^const _EPOLLET = [0-9]' gen-sysinfo.go > /dev/null 2>&1; then
  102. echo "const _EPOLLETpos = _EPOLLET" >> ${OUT}
  103. else
  104. echo "const _EPOLLETpos = 0x80000000" >> ${OUT}
  105. fi
  106. # Make sure EPOLLRDHUP and EPOLL_CLOEXEC are defined.
  107. if ! grep '^const _EPOLLRDHUP' ${OUT} >/dev/null 2>&1; then
  108. echo "const _EPOLLRDHUP = 0x2000" >> ${OUT}
  109. fi
  110. if ! grep '^const _EPOLL_CLOEXEC' ${OUT} >/dev/null 2>&1; then
  111. echo "const _EPOLL_CLOEXEC = 02000000" >> ${OUT}
  112. fi
  113. # AIX 7.1 is a 64 bits value for _FCLOEXEC (referenced by O_CLOEXEC)
  114. # which leads to a constant overflow when using O_CLOEXEC in some
  115. # go code. Issue wan not present in 6.1 (no O_CLOEXEC) and is no
  116. # more present in 7.2 (_FCLOEXEC is a 32 bit value).
  117. if test "${GOOS}" = "aix" && `oslevel | grep -q "^7.1"`; then
  118. sed -e 's/const __FCLOEXEC = .*/const __FCLOEXEC = 0/' ${OUT} > ${OUT}-2
  119. mv ${OUT}-2 ${OUT}
  120. fi
  121. # Make sure _MAP_FAILED is defined.
  122. if ! grep '^const _MAP_FAILED =' gen-sysinfo.go > /dev/null 2>&1; then
  123. echo "const _MAP_FAILED = ^uintptr(0)" >> ${OUT}
  124. fi
  125. # Make sure _MAP_ANON is defined.
  126. if ! grep '^const _MAP_ANON =' gen-sysinfo.go > /dev/null 2>&1; then
  127. if grep '^const _MAP_ANONYMOUS ' gen-sysinfo.go > /dev/null 2>&1; then
  128. echo "const _MAP_ANON = _MAP_ANONYMOUS" >> ${OUT}
  129. else
  130. echo "const _MAP_ANON = 0" >> ${OUT}
  131. fi
  132. fi
  133. # Make sure _MADV_DONTNEED is defined.
  134. if ! grep '^const _MADV_DONTNEED =' gen-sysinfo.go > /dev/null 2>&1; then
  135. echo "const _MADV_DONTNEED = 0" >> ${OUT}
  136. fi
  137. # Make sure _MADV_FREE is defined.
  138. if ! grep '^const _MADV_FREE =' gen-sysinfo.go > /dev/null 2>&1; then
  139. echo "const _MADV_FREE = 0" >> ${OUT}
  140. fi
  141. # Make sure _MADV_HUGEPAGE is defined.
  142. if ! grep '^const _MADV_HUGEPAGE =' gen-sysinfo.go > /dev/null 2>&1; then
  143. echo "const _MADV_HUGEPAGE = 0" >> ${OUT}
  144. fi
  145. # Make sure _MADV_NOHUGEPAGE is defined.
  146. if ! grep '^const _MADV_NOHUGEPAGE =' gen-sysinfo.go > /dev/null 2>&1; then
  147. echo "const _MADV_NOHUGEPAGE = 0" >> ${OUT}
  148. fi
  149. # The semt structure, for Solaris.
  150. grep '^type _sem_t ' gen-sysinfo.go | \
  151. sed -e 's/_sem_t/semt/' >> ${OUT}
  152. # The Solaris port_event_t struct.
  153. grep '^type _port_event_t ' gen-sysinfo.go | \
  154. sed -e s'/_port_event_t/portevent/' \
  155. >> ${OUT}
  156. # The *BSD kevent struct.
  157. grep '^type _kevent ' gen-sysinfo.go | \
  158. sed -e s'/_kevent/keventt/' \
  159. -e 's/ udata [^;}]*/ udata *byte/' \
  160. >> ${OUT}
  161. # Type 'uint128' is needed in a couple of type definitions on arm64,such
  162. # as _user_fpsimd_struct, _elf_fpregset_t, etc.
  163. if ! grep '^type uint128' ${OUT} > /dev/null 2>&1; then
  164. echo "type uint128 [16]byte" >> ${OUT}
  165. fi