Adjusted.
[glibc.git] / sysdeps / unix / make-syscalls.sh
blob8a9c75956e4a2506406abe02b78a61356f0b4af6
1 #! /bin/sh
3 # Usage: make-syscalls.sh ../sysdeps/unix/common
4 # Expects $sysdirs in environment.
6 ##############################################################################
8 # Syscall Signature Key Letters for BP Thunks:
10 # a: unchecked address (e.g., 1st arg to mmap)
11 # b: non-NULL buffer (e.g., 2nd arg to read; return value from mmap)
12 # B: optionally-NULL buffer (e.g., 4th arg to getsockopt)
13 # f: buffer of 2 ints (e.g., 4th arg to socketpair)
14 # F: 3rd arg to fcntl
15 # i: scalar (any signedness & size: int, long, long long, enum, whatever)
16 # I: 3rd arg to ioctl
17 # n: scalar buffer length (e.g., 3rd arg to read)
18 # N: pointer to value/return scalar buffer length (e.g., 6th arg to recvfrom)
19 # p: non-NULL pointer to typed object (e.g., any non-void* arg)
20 # P: optionally-NULL pointer to typed object (e.g., 2nd argument to gettimeofday)
21 # s: non-NULL string (e.g., 1st arg to open)
22 # S: optionally-NULL string (e.g., 1st arg to acct)
23 # v: vararg scalar (e.g., optional 3rd arg to open)
24 # V: byte-per-page vector (3rd arg to mincore)
25 # W: wait status, optionally-NULL pointer to int (e.g., 2nd arg of wait4)
27 ptr='[abBfFINpPsSWV]' # all pointer keyletters
28 int='[inv]' # all scalar keyletters
29 typ='[ifnNpP]' # typed-arg keyletters: we capture type for use in thunk
31 ##############################################################################
33 thisdir=$1; shift
35 echo ''
36 echo \#### DIRECTORY = $thisdir
37 # Check each sysdep dir with higher priority than this one,
38 # and remove from $calls all the functions found in other dirs.
39 # Punt when we reach the directory defining these syscalls.
40 sysdirs=`for dir in $sysdirs; do
41 test $dir = $thisdir && break; echo $dir; done`
42 echo \#### SYSDIRS = $sysdirs
44 # Get the list of system calls for this directory.
45 calls=`sed 's/#.*$//
46 /^[ ]*$/d' $thisdir/syscalls.list`
48 calls=`echo "$calls" |
49 while read file caller rest; do
50 # Remove each syscall that is implemented by a file in $dir.
51 # If a syscall specified a "caller", then only compile that syscall
52 # if the caller function is also implemented in this directory.
53 srcfile=-;
54 for dir in $sysdirs; do
55 { test -f $dir/$file.c && srcfile=$dir/$file.c; } ||
56 { test -f $dir/$file.S && srcfile=$dir/$file.S; } ||
57 { test -f $dir/$file.s && srcfile=$dir/$file.s; } ||
58 { test x$caller != x- &&
59 { { test -f $dir/$caller.c && srcfile=$dir/$caller.c; } ||
60 { test -f $dir/$caller.S && srcfile=$dir/$caller.S; } ||
61 { test -f $dir/$caller.s && srcfile=$dir/$caller.s; }; }; } && break;
62 done;
63 echo $file $srcfile $caller $rest;
64 done`
66 # Any calls left?
67 test -n "$calls" || exit 0
69 # Emit rules to compile the syscalls remaining in $calls.
70 echo "$calls" |
71 while read file srcfile caller syscall args strong weak; do
73 # Figure out if $syscall is defined with a number in syscall.h.
74 callnum=-
75 eval `{ echo "#include <sysdep.h>";
76 echo "callnum=SYS_ify ($syscall)"; } |
77 $asm_CPP - |sed -n -e "/^callnum=.*$syscall/d" \
78 -e "/^\(callnum=\)[ ]*\(.*\)/s//\1'\2'/p"`
80 # Derive the number of arguments from the argument signature
81 case $args in
82 [0-9]) nargs=$args;;
83 ?:) nargs=0;;
84 ?:?) nargs=1;;
85 ?:??) nargs=2;;
86 ?:???) nargs=3;;
87 ?:????) nargs=4;;
88 ?:?????) nargs=5;;
89 ?:??????) nargs=6;;
90 ?:???????) nargs=7;;
91 ?:????????) nargs=8;;
92 ?:?????????) nargs=9;;
93 esac
95 # Make sure only the first syscall rule is used, if multiple dirs
96 # define the same syscall.
97 echo ''
98 echo "#### CALL=$file NUMBER=$callnum ARGS=$args SOURCE=$srcfile"
100 case x$srcfile"$callnum" in
101 x*-) ;; ### Do nothing for undefined callnum
102 x-*)
103 echo "ifeq (,\$(filter $file,\$(unix-syscalls)))"
105 case $weak in
106 *@*)
107 # The versioned symbols are only in the shared library.
108 echo "ifneq (,\$(filter .os,\$(object-suffixes)))"
110 esac
111 # Accumulate the list of syscall files for this directory.
112 echo "unix-syscalls += $file"
113 test x$caller = x- || echo "unix-extra-syscalls += $file"
115 # Emit a compilation rule for this syscall.
116 case $weak in
117 *@*)
118 # The versioned symbols are only in the shared library.
119 echo "\
120 shared-only-routines += $file
121 \$(objpfx)${file}.os: \\"
124 echo "\
125 \$(foreach o,\$(object-suffixes),\$(objpfx)$file\$o): \\"
127 esac
128 echo " \$(common-objpfx)s-proto.d
129 (echo '#include <sysdep.h>'; \\
130 echo 'PSEUDO ($strong, $syscall, $nargs)'; \\
131 echo ' ret'; \\
132 echo 'PSEUDO_END($strong)'; \\"
134 # Append any weak aliases or versions defined for this syscall function.
136 # A shortcoming in the current gas is that it will only allow one
137 # version-alias per symbol. So we create new strong aliases as needed.
138 vcount=""
140 for name in $weak; do
141 case $name in
142 *@@*)
143 base=`echo $name | sed 's/@@.*//'`
144 ver=`echo $name | sed 's/.*@@//'`
145 if test -z "$vcount" ; then
146 source=$strong
147 vcount=1
148 else
149 source="${strong}_${vcount}"
150 vcount=`expr $vcount + 1`
151 echo " echo 'strong_alias ($strong, $source)'; \\"
153 echo " echo 'default_symbol_version($source, $base, $ver)'; \\"
155 *@*)
156 base=`echo $name | sed 's/@.*//'`
157 ver=`echo $name | sed 's/.*@//'`
158 if test -z "$vcount" ; then
159 source=$strong
160 vcount=1
161 else
162 source="${strong}_${vcount}"
163 vcount=`expr $vcount + 1`
164 echo " echo 'strong_alias ($strong, $source)'; \\"
166 echo " echo 'symbol_version($source, $base, $ver)'; \\"
169 echo " echo 'weak_alias ($strong, $name)'; \\"
171 esac
172 done
174 # And finally, pipe this all into the compiler.
175 echo ' ) | $(COMPILE.S) -x assembler-with-cpp -o $@ -'
177 case $weak in
178 *@*)
179 # The versioned symbols are only in the shared library.
180 echo endif
182 esac
184 echo endif
186 esac
188 case x"$callnum",$srcfile,$args in
189 x-,-,* | x*,*.[sS],*V*) ;;
190 x*,-,*$ptr* | x*,*.[sS],*$ptr*)
192 nv_weak=`for name in $weak; do
193 case $name in
194 *@*) ;;
195 *) echo $name;;
196 esac; done`
198 # choose the name with the fewest leading underscores, preferably none
199 set `echo $strong $nv_weak |tr '@ \t' ' \n\n' |sort -r`
200 callname=$1
202 # convert signature string to individual numbered arg names
203 # e.g., i:ipbN -> i0 i1 p2 b3 N4
204 set `echo $args |
205 sed -e 's/^\(.\):\(.*\)/\2 <\10>/' \
206 -e 's/^\([^ ]\)\(.*\)/\2 <\11>/' \
207 -e 's/^\([^ ]\)\(.*\)/\2 <\12>/' \
208 -e 's/^\([^ ]\)\(.*\)/\2 <\13>/' \
209 -e 's/^\([^ ]\)\(.*\)/\2 <\14>/' \
210 -e 's/^\([^ ]\)\(.*\)/\2 <\15>/' \
211 -e 's/^\([^ ]\)\(.*\)/\2 <\16>/' \
212 -e 's/^\([^ ]\)\(.*\)/\2 <\17>/' \
213 -e 's/^\([^ ]\)\(.*\)/\2 <\18>/' \
214 -e 's/^\([^ ]\)\(.*\)/\2 <\19>/'`
215 rtn=$1; shift
216 args=$*
217 arglist=`echo $* |sed 's/ /, /g'`
219 # The best way to understand what's going on here is to examine
220 # the output in BUILDDIR/sysd-syscalls.
222 # generate makefile envelope & rule head
223 echo "ifeq (,\$(filter $file,\$(bp-thunks)))"
224 echo "bp-thunks += $file"
225 echo "\$(objpfx)\$(bppfx)$file.ob: \$(common-objpfx)s-proto.d"
227 # generate macro head
228 echo " (echo '#define $callname(`echo $arglist | \
229 sed -e 's/[<>]//g'`) `echo $rtn | \
230 sed -e 's/<\('$typ'0\)>/\1v;/g' \
231 -e 's/<\(b0\)>/x0; extern char \1v;/g'` \\'; \\"
233 # generate extern decls of dummy variables for each arg
234 echo " echo '`echo $args | \
235 sed -e 's/<\('$typ'[1-9]\)>/extern \1, \1v;/g' \
236 -e 's/<\([abBFIsSV][1-9]\)>/extern char \1v;/g' \
237 -e 's/<\([Wv][1-9]\)>/extern int \1v;/g'` \\'; \\"
239 # generate bounded-pointer thunk declarator
240 echo " echo '`echo $rtn | \
241 sed -e 's/<\('$ptr'0\)>/__typeof (\1v) *__bounded/g' \
242 -e 's/<\('$int'0\)>/__typeof (\1v)/g'` BP_SYM ($strong) (`echo $arglist | \
243 sed -e 's/<\('$ptr'[1-9]\)>/__typeof (\1v) *__bounded \1a/g' \
244 -e 's/<\('$int'[1-9]\)>/__typeof (\1v) \1a/g'`) { \\'; \\"
246 # generate extern primitive syscall declaration
247 echo " echo ' extern `echo $rtn | \
248 sed -e 's/<\('$ptr'0\)>/__typeof (\1v) *__unbounded/g' \
249 -e 's/<\('$int'0\)>/__typeof (\1v)/g'` ($callname) (`echo $arglist | \
250 sed -e 's/<\('$ptr'[1-9]\)>/__typeof (\1v) *__unbounded/g' \
251 -e 's/<\('$int'[1-9]\)>/__typeof (\1v)/g'`); \\'; \\"
253 # generate call the primtive system call, optionally wrapping bounds
254 # around the result if the signature's return keyletter is `b'.
255 echo " echo ' return `echo $rtn |
256 sed -e 's/<b0>/BOUNDED_N (/' \
257 -e 's/<.0>//'`($callname) (`echo $arglist | \
258 sed -e 's/<\(a[1-9]\)>/__ptrvalue (\1a)/g' \
259 -e 's/<\(n[1-9]\)>, <\(V[1-9]\)>/\1a, CHECK_N_PAGES (\2a, \1a)/g' \
260 -e 's/<\(b[1-9]\)>, <\(n[1-9]\)>/CHECK_N (\1a, \2a), \2a/g' \
261 -e 's/<\(b[1-9]\)>, <\(N[1-9]\)>/CHECK_N (\1a, *CHECK_1 (\2a)), __ptrvalue (\2a)/g' \
262 -e 's/<\(B[1-9]\)>, <\(n[1-9]\)>/CHECK_N_NULL_OK (\1a, \2a), \2a/g' \
263 -e 's/<\(B[1-9]\)>, <\(N[1-9]\)>/CHECK_N_NULL_OK (\1a, *CHECK_1 (\2a)), __ptrvalue (\2a)/g' \
264 -e 's/<\(f[1-9]\)>/CHECK_N (\1a, 2)/g' \
265 -e 's/<\(i[1-9]\)>, <\(F[1-9]\)>/\1a, CHECK_FCNTL (\2a, \1a)/g' \
266 -e 's/<\(i[1-9]\)>, <\(I[1-9]\)>/\1a, CHECK_IOCTL (\2a, \1a)/g' \
267 -e 's/<\(p[1-9]\)>/CHECK_1 (\1a)/g' \
268 -e 's/<\([PW][1-9]\)>/CHECK_1_NULL_OK (\1a)/g' \
269 -e 's/<\(s[1-9]\)>/CHECK_STRING (\1a)/g' \
270 -e 's/<\(S[1-9]\)>/CHECK_STRING_NULL_OK (\1a)/g' \
271 -e 's/<\([ivn][1-9]\)>/\1a/g'`)`echo $rtn $args |
272 sed -e 's/<b0>.*<\(n[1-9]\)>.*/, \1a)/' \
273 -e 's/<.0>.*//'`; \\'; \\"
275 echo " echo '} \\'; \\"
277 # generate thunk aliases
278 for name in $nv_weak; do
279 echo " echo 'weak_alias (BP_SYM ($strong), BP_SYM ($name)) \\'; \\"
280 done
282 # wrap up
283 echo "\
284 echo ''; \\
285 echo '#include <bp-thunks.h>'; \\
286 ) | \$(COMPILE.c) -x c -o \$@ -"
287 ### Use this for debugging intermediate output:
288 ### ) >\$(@:.ob=.c)
289 ### \$(subst -c,-E,\$(COMPILE.c)) -o \$(@:.ob=.ib) \$(@:.ob=.c)
290 ### \$(COMPILE.c) -x cpp-output -o \$@ \$(@:.ob=.ib)"
291 echo endif
293 esac
295 done