re PR target/47986 (gcc.c-torture/execute/20040709-1.c fails with non-delegitimized...
[gcc.git] / libgo / mksysinfo.sh
1 #!/bin/sh
2
3 # Copyright 2009 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 # Create sysinfo.go.
8
9 # This shell script creates the sysinfo.go file which holds types and
10 # constants extracted from the system header files. This relies on a
11 # hook in gcc: the -fdump-go-spec option will generate debugging
12 # information in Go syntax.
13
14 # We currently #include all the files at once, which works, but leads
15 # to exposing some names which ideally should not be exposed, as they
16 # match grep patterns. E.g., WCHAR_MIN gets exposed because it starts
17 # with W, like the wait flags.
18
19 CC=${CC:-gcc}
20 OUT=tmp-sysinfo.go
21
22 set -e
23
24 rm -f sysinfo.c
25 cat > sysinfo.c <<EOF
26 #include "config.h"
27
28 #define _GNU_SOURCE
29 #define _LARGEFILE_SOURCE
30 #define _FILE_OFFSET_BITS 64
31
32 #if defined(__sun__) && defined(__svr4__)
33 /* Needed by Solaris header files. */
34 #define _XOPEN_SOURCE 600
35 #define __EXTENSIONS__
36 #endif
37
38 #include <sys/types.h>
39 #include <dirent.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <netinet/in.h>
43 #include <netinet/tcp.h>
44 #include <signal.h>
45 #if defined(HAVE_SYSCALL_H)
46 #include <syscall.h>
47 #endif
48 #if defined(HAVE_SYS_SYSCALL_H)
49 #include <sys/syscall.h>
50 #endif
51 #if defined(HAVE_SYS_EPOLL_H)
52 #include <sys/epoll.h>
53 #endif
54 #if defined(HAVE_SYS_PTRACE_H)
55 #include <sys/ptrace.h>
56 #endif
57 #include <sys/resource.h>
58 #include <sys/uio.h>
59 #include <sys/socket.h>
60 #include <sys/stat.h>
61 #include <sys/time.h>
62 #include <sys/wait.h>
63 #include <sys/un.h>
64 #if defined(HAVE_SYS_USER_H)
65 #include <sys/user.h>
66 #endif
67 #if defined(HAVE_SYS_UTSNAME_H)
68 #include <sys/utsname.h>
69 #endif
70 #include <unistd.h>
71 EOF
72
73 ${CC} -fdump-go-spec=gen-sysinfo.go -std=gnu99 -S -o sysinfo.s sysinfo.c
74
75 echo 'package syscall' > ${OUT}
76
77 # Get all the consts and types, skipping ones which could not be
78 # represented in Go and ones which we need to rewrite. We also skip
79 # function declarations, as we don't need them here. All the symbols
80 # will all have a leading underscore.
81 grep -v '^// ' gen-sysinfo.go | \
82 grep -v '^func' | \
83 grep -v '^type _timeval ' | \
84 grep -v '^type _timespec ' | \
85 grep -v '^type _timestruc_t ' | \
86 grep -v '^type _epoll_' | \
87 grep -v 'in6_addr' | \
88 grep -v 'sockaddr_in6' | \
89 sed -e 's/\([^a-zA-Z0-9_]\)_timeval\([^a-zA-Z0-9_]\)/\1Timeval\2/g' \
90 -e 's/\([^a-zA-Z0-9_]\)_timespec\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
91 -e 's/\([^a-zA-Z0-9_]\)_timestruc_t\([^a-zA-Z0-9_]\)/\1Timestruc\2/g' \
92 >> ${OUT}
93
94 # The errno constants.
95 grep '^const _E' gen-sysinfo.go | \
96 sed -e 's/^\(const \)_\(E[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
97
98 # The O_xxx flags.
99 grep '^const _\(O\|F\|FD\)_' gen-sysinfo.go | \
100 sed -e 's/^\(const \)_\([^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
101 if ! grep '^const O_ASYNC' ${OUT} >/dev/null 2>&1; then
102 echo "const O_ASYNC = 0" >> ${OUT}
103 fi
104 if ! grep '^const O_CLOEXEC' ${OUT} >/dev/null 2>&1; then
105 echo "const O_CLOEXEC = 0" >> ${OUT}
106 fi
107
108 # The signal numbers.
109 grep '^const _SIG[^_]' gen-sysinfo.go | \
110 grep -v '^const _SIGEV_' | \
111 sed -e 's/^\(const \)_\(SIG[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
112
113 # The syscall numbers. We force the names to upper case.
114 grep '^const _SYS_' gen-sysinfo.go | \
115 sed -e 's/const _\(SYS_[^= ]*\).*$/\1/' | \
116 while read sys; do
117 sup=`echo $sys | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
118 echo "const $sup = _$sys" >> ${OUT}
119 done
120
121 # Stat constants.
122 grep '^const _S_' gen-sysinfo.go | \
123 sed -e 's/^\(const \)_\(S_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
124
125 # Process status constants.
126 grep '^const _W' gen-sysinfo.go |
127 sed -e 's/^\(const \)_\(W[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
128 # WSTOPPED was introduced in glibc 2.3.4.
129 if ! grep '^const _WSTOPPED = ' gen-sysinfo.go >/dev/null 2>&1; then
130 if grep '^const _WUNTRACED = ' gen-sysinfo.go > /dev/null 2>&1; then
131 echo 'const WSTOPPED = _WUNTRACED' >> ${OUT}
132 else
133 echo 'const WSTOPPED = 2' >> ${OUT}
134 fi
135 fi
136 if grep '^const ___WALL = ' gen-sysinfo.go >/dev/null 2>&1 \
137 && ! grep '^const _WALL = ' gen-sysinfo.go >/dev/null 2>&1; then
138 echo 'const WALL = ___WALL' >> ${OUT}
139 fi
140
141 # Networking constants.
142 grep '^const _\(AF\|SOCK\|SOL\|SO\|IPPROTO\|TCP\|IP\|IPV6\)_' gen-sysinfo.go |
143 sed -e 's/^\(const \)_\([^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
144 grep '^const _SOMAXCONN' gen-sysinfo.go |
145 sed -e 's/^\(const \)_\(SOMAXCONN[^= ]*\)\(.*\)$/\1\2 = _\2/' \
146 >> ${OUT}
147 grep '^const _SHUT_' gen-sysinfo.go |
148 sed -e 's/^\(const \)_\(SHUT[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
149
150 # The net package requires a definition for IPV6ONLY.
151 if ! grep '^const IPV6_V6ONLY ' ${OUT} >/dev/null 2>&1; then
152 echo "const IPV6_V6ONLY = 0" >> ${OUT}
153 fi
154
155 # pathconf constants.
156 grep '^const __PC' gen-sysinfo.go |
157 sed -e 's/^\(const \)__\(PC[^= ]*\)\(.*\)$/\1\2 = __\2/' >> ${OUT}
158
159 # The epoll constants were picked up by the errno constants, but we
160 # need to be sure the EPOLLRDHUP is defined.
161 if ! grep '^const EPOLLRDHUP' ${OUT} >/dev/null 2>&1; then
162 echo "const EPOLLRDHUP = 0x2000" >> ${OUT}
163 fi
164
165 # Ptrace constants. We don't expose all the PTRACE flags, just the
166 # PTRACE_O_xxx and PTRACE_EVENT_xxx ones.
167 grep '^const _PTRACE_O' gen-sysinfo.go |
168 sed -e 's/^\(const \)_\(PTRACE_O[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
169 grep '^const _PTRACE_EVENT' gen-sysinfo.go |
170 sed -e 's/^\(const \)_\(PTRACE_EVENT[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
171 # We need PTRACE_SETOPTIONS and PTRACE_GETEVENTMSG, but they are not
172 # defined in older versions of glibc.
173 if ! grep '^const _PTRACE_SETOPTIONS' ${OUT} > /dev/null 2>&1; then
174 echo "const _PTRACE_SETOPTIONS = 0x4200" >> ${OUT}
175 fi
176 if ! grep '^const PTRACE_O_TRACESYSGOOD' ${OUT} > /dev/null 2>&1; then
177 echo "const PTRACE_O_TRACESYSGOOD = 0x1" >> ${OUT}
178 fi
179 if ! grep '^const PTRACE_O_TRACEFORK' ${OUT} > /dev/null 2>&1; then
180 echo "const PTRACE_O_TRACEFORK = 0x2" >> ${OUT}
181 fi
182 if ! grep '^const PTRACE_O_TRACEVFORK' ${OUT} > /dev/null 2>&1; then
183 echo "const PTRACE_O_TRACEVFORK = 0x4" >> ${OUT}
184 fi
185 if ! grep '^const PTRACE_O_TRACECLONE' ${OUT} > /dev/null 2>&1; then
186 echo "const PTRACE_O_TRACECLONE = 0x8" >> ${OUT}
187 fi
188 if ! grep '^const PTRACE_O_TRACEEXEC' ${OUT} > /dev/null 2>&1; then
189 echo "const PTRACE_O_TRACEEXEC = 0x10" >> ${OUT}
190 fi
191 if ! grep '^const PTRACE_O_TRACEVFORKDONE' ${OUT} > /dev/null 2>&1; then
192 echo "const PTRACE_O_TRACEVFORKDONE = 0x20" >> ${OUT}
193 fi
194 if ! grep '^const PTRACE_O_TRACEEXIT' ${OUT} > /dev/null 2>&1; then
195 echo "const PTRACE_O_TRACEEXIT = 0x40" >> ${OUT}
196 fi
197 if ! grep '^const PTRACE_O_MASK' ${OUT} > /dev/null 2>&1; then
198 echo "const PTRACE_O_MASK = 0x7f" >> ${OUT}
199 fi
200 if ! grep '^const _PTRACE_GETEVENTMSG' ${OUT} > /dev/null 2>&1; then
201 echo "const _PTRACE_GETEVENTMSG = 0x4201" >> ${OUT}
202 fi
203 if ! grep '^const PTRACE_EVENT_FORK' ${OUT} > /dev/null 2>&1; then
204 echo "const PTRACE_EVENT_FORK = 1" >> ${OUT}
205 fi
206 if ! grep '^const PTRACE_EVENT_VFORK' ${OUT} > /dev/null 2>&1; then
207 echo "const PTRACE_EVENT_VFORK = 2" >> ${OUT}
208 fi
209 if ! grep '^const PTRACE_EVENT_CLONE' ${OUT} > /dev/null 2>&1; then
210 echo "const PTRACE_EVENT_CLONE = 3" >> ${OUT}
211 fi
212 if ! grep '^const PTRACE_EVENT_EXEC' ${OUT} > /dev/null 2>&1; then
213 echo "const PTRACE_EVENT_EXEC = 4" >> ${OUT}
214 fi
215 if ! grep '^const PTRACE_EVENT_VFORK_DONE' ${OUT} > /dev/null 2>&1; then
216 echo "const PTRACE_EVENT_VFORK_DONE = 5" >> ${OUT}
217 fi
218 if ! grep '^const PTRACE_EVENT_EXIT' ${OUT} > /dev/null 2>&1; then
219 echo "const PTRACE_EVENT_EXIT = 6" >> ${OUT}
220 fi
221 if ! grep '^const _PTRACE_TRACEME' ${OUT} > /dev/null 2>&1; then
222 echo "const _PTRACE_TRACEME = 0" >> ${OUT}
223 fi
224
225 # The registers returned by PTRACE_GETREGS. This is probably
226 # GNU/Linux specific; it should do no harm if there is no
227 # _user_regs_struct.
228 regs=`grep '^type _user_regs_struct struct' gen-sysinfo.go || true`
229 if test "$regs" != ""; then
230 regs=`echo $regs | sed -e 's/type _user_regs_struct struct //' -e 's/[{}]//g'`
231 regs=`echo $regs | sed -e s'/^ *//'`
232 nregs=
233 while test -n "$regs"; do
234 field=`echo $regs | sed -e 's/^\([^;]*\);.*$/\1/'`
235 regs=`echo $regs | sed -e 's/^[^;]*; *\(.*\)$/\1/'`
236 # Capitalize the first character of the field.
237 f=`echo $field | sed -e 's/^\(.\).*$/\1/'`
238 r=`echo $field | sed -e 's/^.\(.*\)$/\1/'`
239 f=`echo $f | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
240 field="$f$r"
241 nregs="$nregs $field;"
242 done
243 echo "type PtraceRegs struct {$nregs }" >> ${OUT}
244 fi
245
246 # Some basic types.
247 echo 'type Size_t _size_t' >> ${OUT}
248 echo "type Ssize_t _ssize_t" >> ${OUT}
249 if grep '^const _HAVE_OFF64_T = ' gen-sysinfo.go > /dev/null 2>&1; then
250 echo "type Offset_t _off64_t" >> ${OUT}
251 else
252 echo "type Offset_t _off_t" >> ${OUT}
253 fi
254 echo "type Mode_t _mode_t" >> ${OUT}
255 echo "type Pid_t _pid_t" >> ${OUT}
256 echo "type Uid_t _uid_t" >> ${OUT}
257 echo "type Gid_t _gid_t" >> ${OUT}
258 echo "type Socklen_t _socklen_t" >> ${OUT}
259
260 # The long type, needed because that is the type that ptrace returns.
261 sizeof_long=`grep '^const ___SIZEOF_LONG__ = ' gen-sysinfo.go | sed -e 's/.*= //'`
262 if test "$sizeof_long" = "4"; then
263 echo "type _C_long int32" >> ${OUT}
264 elif test "$sizeof_long" = "8"; then
265 echo "type _C_long int64" >> ${OUT}
266 else
267 echo 1>&2 "mksysinfo.sh: could not determine size of long (got $sizeof_long)"
268 exit 1
269 fi
270
271 # Solaris 2 needs _u?pad128_t, but its default definition in terms of long
272 # double is commented by -fdump-go-spec.
273 if grep "^// type _pad128_t" gen-sysinfo.go > /dev/null 2>&1; then
274 echo "type _pad128_t struct { _l [4]int32; }" >> ${OUT}
275 fi
276 if grep "^// type _upad128_t" gen-sysinfo.go > /dev/null 2>&1; then
277 echo "type _upad128_t struct { _l [4]uint32; }" >> ${OUT}
278 fi
279
280 # The time structures need special handling: we need to name the
281 # types, so that we can cast integers to the right types when
282 # assigning to the structures.
283 timeval=`grep '^type _timeval ' gen-sysinfo.go`
284 timeval_sec=`echo $timeval | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
285 timeval_usec=`echo $timeval | sed -n -e 's/^.*tv_usec \([^ ]*\);.*$/\1/p'`
286 echo "type Timeval_sec_t $timeval_sec" >> ${OUT}
287 echo "type Timeval_usec_t $timeval_usec" >> ${OUT}
288 echo $timeval | \
289 sed -e 's/type _timeval /type Timeval /' \
290 -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timeval_sec_t/' \
291 -e 's/tv_usec *[a-zA-Z0-9_]*/Usec Timeval_usec_t/' >> ${OUT}
292 timespec=`grep '^type _timespec ' gen-sysinfo.go`
293 timespec_sec=`echo $timespec | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
294 timespec_nsec=`echo $timespec | sed -n -e 's/^.*tv_nsec \([^ ]*\);.*$/\1/p'`
295 echo "type Timespec_sec_t $timespec_sec" >> ${OUT}
296 echo "type Timespec_nsec_t $timespec_nsec" >> ${OUT}
297 echo $timespec | \
298 sed -e 's/^type _timespec /type Timespec /' \
299 -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timespec_sec_t/' \
300 -e 's/tv_nsec *[a-zA-Z0-9_]*/Nsec Timespec_nsec_t/' >> ${OUT}
301
302 timestruc=`grep '^type _timestruc_t ' gen-sysinfo.go || true`
303 if test "$timestruc" != ""; then
304 timestruc_sec=`echo $timestruc | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
305 timestruc_nsec=`echo $timestruc | sed -n -e 's/^.*tv_nsec \([^ ]*\);.*$/\1/p'`
306 echo "type Timestruc_sec_t $timestruc_sec" >> ${OUT}
307 echo "type Timestruc_nsec_t $timestruc_nsec" >> ${OUT}
308 echo $timestruc | \
309 sed -e 's/^type _timestruc_t /type Timestruc /' \
310 -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timestruc_sec_t/' \
311 -e 's/tv_nsec *[a-zA-Z0-9_]*/Nsec Timestruc_nsec_t/' >> ${OUT}
312 fi
313
314 # The stat type.
315 # Prefer largefile variant if available.
316 stat=`grep '^type _stat64 ' gen-sysinfo.go || true`
317 if test "$stat" != ""; then
318 grep '^type _stat64 ' gen-sysinfo.go
319 else
320 grep '^type _stat ' gen-sysinfo.go
321 fi | sed -e 's/type _stat\(64\)\?/type Stat_t/' \
322 -e 's/st_dev/Dev/' \
323 -e 's/st_ino/Ino/g' \
324 -e 's/st_nlink/Nlink/' \
325 -e 's/st_mode/Mode/' \
326 -e 's/st_uid/Uid/' \
327 -e 's/st_gid/Gid/' \
328 -e 's/st_rdev/Rdev/' \
329 -e 's/st_size/Size/' \
330 -e 's/st_blksize/Blksize/' \
331 -e 's/st_blocks/Blocks/' \
332 -e 's/st_atim/Atime/' \
333 -e 's/st_mtim/Mtime/' \
334 -e 's/st_ctim/Ctime/' \
335 -e 's/\([^a-zA-Z0-9_]\)_timeval\([^a-zA-Z0-9_]\)/\1Timeval\2/g' \
336 -e 's/\([^a-zA-Z0-9_]\)_timespec\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
337 -e 's/\([^a-zA-Z0-9_]\)_timestruc_t\([^a-zA-Z0-9_]\)/\1Timestruc\2/g' \
338 >> ${OUT}
339
340 # The directory searching types.
341 # Prefer largefile variant if available.
342 dirent=`grep '^type _dirent64 ' gen-sysinfo.go || true`
343 if test "$dirent" != ""; then
344 grep '^type _dirent64 ' gen-sysinfo.go
345 else
346 grep '^type _dirent ' gen-sysinfo.go
347 fi | sed -e 's/type _dirent\(64\)\?/type Dirent/' \
348 -e 's/d_name \[0+1\]/d_name [0+256]/' \
349 -e 's/d_name/Name/' \
350 -e 's/]int8/]byte/' \
351 -e 's/d_ino/Ino/' \
352 -e 's/d_off/Off/' \
353 -e 's/d_reclen/Reclen/' \
354 -e 's/d_type/Type/' \
355 >> ${OUT}
356 echo "type DIR _DIR" >> ${OUT}
357
358 # The rusage struct.
359 rusage=`grep '^type _rusage struct' gen-sysinfo.go`
360 if test "$rusage" != ""; then
361 rusage=`echo $rusage | sed -e 's/type _rusage struct //' -e 's/[{}]//g'`
362 rusage=`echo $rusage | sed -e 's/^ *//'`
363 nrusage=
364 while test -n "$rusage"; do
365 field=`echo $rusage | sed -e 's/^\([^;]*\);.*$/\1/'`
366 rusage=`echo $rusage | sed -e 's/^[^;]*; *\(.*\)$/\1/'`
367 # Drop the leading ru_, capitalize the next character.
368 field=`echo $field | sed -e 's/^ru_//'`
369 f=`echo $field | sed -e 's/^\(.\).*$/\1/'`
370 r=`echo $field | sed -e 's/^.\(.*\)$/\1/'`
371 f=`echo $f | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
372 # Fix _timeval _timespec, and _timestruc_t.
373 r=`echo $r | sed -e s'/ _timeval$/ Timeval/'`
374 r=`echo $r | sed -e s'/ _timespec$/ Timespec/'`
375 r=`echo $r | sed -e s'/ _timestruc_t$/ Timestruc/'`
376 field="$f$r"
377 nrusage="$nrusage $field;"
378 done
379 echo "type Rusage struct {$nrusage }" >> ${OUT}
380 fi
381
382 # The utsname struct.
383 grep '^type _utsname ' gen-sysinfo.go | \
384 sed -e 's/_utsname/Utsname/' \
385 -e 's/sysname/Sysname/' \
386 -e 's/nodename/Nodename/' \
387 -e 's/release/Release/' \
388 -e 's/version/Version/' \
389 -e 's/machine/Machine/' \
390 -e 's/domainname/Domainname/' \
391 >> ${OUT}
392
393 # The iovec struct.
394 iovec=`grep '^type _iovec ' gen-sysinfo.go`
395 iovec_len=`echo $iovec | sed -n -e 's/^.*iov_len \([^ ]*\);.*$/\1/p'`
396 echo "type Iovec_len_t $iovec_len" >> ${OUT}
397 echo $iovec | \
398 sed -e 's/_iovec/Iovec/' \
399 -e 's/iov_base/Base/' \
400 -e 's/iov_len *[a-zA-Z0-9_]*/Len Iovec_len_t/' \
401 >> ${OUT}
402
403 # The msghdr struct.
404 msghdr=`grep '^type _msghdr ' gen-sysinfo.go`
405 msghdr_controllen=`echo $msghdr | sed -n -e 's/^.*msg_controllen \([^ ]*\);.*$/\1/p'`
406 echo "type Msghdr_controllen_t $msghdr_controllen" >> ${OUT}
407 echo $msghdr | \
408 sed -e 's/_msghdr/Msghdr/' \
409 -e 's/msg_name/Name/' \
410 -e 's/msg_namelen/Namelen/' \
411 -e 's/msg_iov/Iov/' \
412 -e 's/msg_iovlen/Iovlen/' \
413 -e 's/_iovec/Iovec/' \
414 -e 's/msg_control/Control/' \
415 -e 's/msg_controllen *[a-zA-Z0-9_]*/Controllen Msghdr_controllen_t/' \
416 -e 's/msg_flags/Flags/' \
417 >> ${OUT}
418
419 exit $?