* sysdeps/unix/make-syscalls.sh: Insert $(make-target-directory) at
[platform/upstream/glibc.git] / sysdeps / unix / make-syscalls.sh
1 #! /bin/sh
2
3 # Usage: make-syscalls.sh ../sysdeps/unix/common
4 # Expects $sysdirs in environment.
5
6 ##############################################################################
7
8 # Syscall Signature Key Letters for BP Thunks:
9 #
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)
26
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
30
31 ##############################################################################
32
33 thisdir=$1; shift
34
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
43
44 # Get the list of system calls for this directory.
45 calls=`sed 's/#.*$//
46 /^[     ]*$/d' $thisdir/syscalls.list`
47
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`
65
66 # Any calls left?
67 test -n "$calls" || exit 0
68
69 # Emit rules to compile the syscalls remaining in $calls.
70 echo "$calls" |
71 while read file srcfile caller syscall args strong weak; do
72
73   case x"$syscall" in
74   x-) callnum=_ ;;
75   *)
76   # Figure out if $syscall is defined with a number in syscall.h.
77   callnum=-
78   eval `{ echo "#include <sysdep.h>";
79         echo "callnum=SYS_ify ($syscall)"; } |
80           $asm_CPP -D__OPTIMIZE__ - |
81           sed -n -e "/^callnum=.*$syscall/d" \
82                  -e "/^\(callnum=\)[    ]*\(.*\)/s//\1'\2'/p"`
83   ;;
84   esac
85
86   # Derive the number of arguments from the argument signature
87   case $args in
88   [0-9]) nargs=$args;;
89   ?:) nargs=0;;
90   ?:?) nargs=1;;
91   ?:??) nargs=2;;
92   ?:???) nargs=3;;
93   ?:????) nargs=4;;
94   ?:?????) nargs=5;;
95   ?:??????) nargs=6;;
96   ?:???????) nargs=7;;
97   ?:????????) nargs=8;;
98   ?:?????????) nargs=9;;
99   esac
100
101   # Make sure only the first syscall rule is used, if multiple dirs
102   # define the same syscall.
103   echo ''
104   echo "#### CALL=$file NUMBER=$callnum ARGS=$args SOURCE=$srcfile"
105
106  case x$srcfile"$callnum" in
107  x*-) ;; ### Do nothing for undefined callnum
108  x-*)
109   echo "ifeq (,\$(filter $file,\$(unix-syscalls)))"
110
111   case $weak in
112   *@*)
113     # The versioned symbols are only in the shared library.
114     echo "ifneq (,\$(filter .os,\$(object-suffixes)))"
115     ;;
116   esac
117   # Accumulate the list of syscall files for this directory.
118   echo "unix-syscalls += $file"
119   test x$caller = x- || echo "unix-extra-syscalls += $file"
120
121   # Emit a compilation rule for this syscall.
122   case $weak in
123   *@*)
124     # The versioned symbols are only in the shared library.
125     echo "\
126 shared-only-routines += $file
127 \$(objpfx)${file}.os: \\"
128     ;;
129   *)
130     echo "\
131 \$(foreach o,\$(object-suffixes),\$(objpfx)$file\$o) \
132 \$(objpfx)rtld-$file.os: \\"
133     ;;
134   esac
135
136   echo '                $(common-objpfx)s-proto.d'
137   case x"$callnum" in
138   x_)
139   echo "\
140         (echo '/* Dummy module requested by syscalls.list */'; \\"
141   ;;
142   x*)
143   echo "\
144         \$(make-target-directory)
145         (echo '#include <sysdep.h>'; \\
146          echo 'PSEUDO ($strong, $syscall, $nargs)'; \\
147          echo ' ret'; \\
148          echo 'PSEUDO_END($strong)'; \\
149          echo 'libc_hidden_def ($strong)'; \\"
150   ;;
151   esac
152
153   # Append any weak aliases or versions defined for this syscall function.
154
155   # A shortcoming in the current gas is that it will only allow one
156   # version-alias per symbol.  So we create new strong aliases as needed.
157   vcount=""
158
159   for name in $weak; do
160     case $name in
161       *@@*)
162         base=`echo $name | sed 's/@@.*//'`
163         ver=`echo $name | sed 's/.*@@//'`
164         if test -z "$vcount" ; then
165           source=$strong
166           vcount=1
167         else
168           source="${strong}_${vcount}"
169           vcount=`expr $vcount + 1`
170           echo "         echo 'strong_alias ($strong, $source)'; \\"
171         fi
172         echo "   echo 'default_symbol_version($source, $base, $ver)'; \\"
173         ;;
174       *@*)
175         base=`echo $name | sed 's/@.*//'`
176         ver=`echo $name | sed 's/.*@//'`
177         if test -z "$vcount" ; then
178           source=$strong
179           vcount=1
180         else
181           source="${strong}_${vcount}"
182           vcount=`expr $vcount + 1`
183           echo "         echo 'strong_alias ($strong, $source)'; \\"
184         fi
185         echo "   echo 'symbol_version($source, $base, $ver)'; \\"
186         ;;
187       *)
188         echo "   echo 'weak_alias ($strong, $name)'; \\"
189         echo "   echo 'libc_hidden_weak ($name)'; \\"
190         ;;
191     esac
192   done
193
194   # And finally, pipe this all into the compiler.
195   echo '        ) | $(COMPILE.S) -x assembler-with-cpp -o $@ -'
196
197   case $weak in
198   *@*)
199     # The versioned symbols are only in the shared library.
200     echo endif
201     ;;
202   esac
203
204   echo endif
205  ;;
206  esac
207
208   case x"$callnum",$srcfile,$args in
209   x[_-],-,* | x*,*.[sS],*V*) ;;
210   x*,-,*$ptr* | x*,*.[sS],*$ptr*)
211
212     nv_weak=`for name in $weak; do
213                 case $name in
214                 *@*) ;;
215                 *) echo $name;;
216                 esac; done`
217
218     # choose the name with the fewest leading underscores, preferably none
219     set `echo $strong $nv_weak |tr '@ \t' ' \n\n' |sort -r`
220     callname=$1
221
222     # convert signature string to individual numbered arg names
223     # e.g., i:ipbN -> i0 i1 p2 b3 N4
224     set `echo $args |
225         sed -e 's/^\(.\):\(.*\)/\2 <\10>/' \
226             -e 's/^\([^ ]\)\(.*\)/\2 <\11>/' \
227             -e 's/^\([^ ]\)\(.*\)/\2 <\12>/' \
228             -e 's/^\([^ ]\)\(.*\)/\2 <\13>/' \
229             -e 's/^\([^ ]\)\(.*\)/\2 <\14>/' \
230             -e 's/^\([^ ]\)\(.*\)/\2 <\15>/' \
231             -e 's/^\([^ ]\)\(.*\)/\2 <\16>/' \
232             -e 's/^\([^ ]\)\(.*\)/\2 <\17>/' \
233             -e 's/^\([^ ]\)\(.*\)/\2 <\18>/' \
234             -e 's/^\([^ ]\)\(.*\)/\2 <\19>/'`
235     rtn=$1; shift
236     args=$*
237     arglist=`echo $* |sed 's/ /, /g'`
238
239     # The best way to understand what's going on here is to examine
240     # the output in BUILDDIR/sysd-syscalls.
241
242     # generate makefile envelope & rule head
243     echo "ifeq (,\$(filter $file,\$(bp-thunks)))"
244     echo "bp-thunks += $file"
245     echo "\$(objpfx)\$(bppfx)$file.ob: \$(common-objpfx)s-proto.d"
246
247     # generate macro head
248     echo "      (echo '#define $callname(`echo $arglist | \
249             sed -e 's/[<>]//g'`) `echo $rtn | \
250             sed -e 's/<\('$typ'0\)>/\1v;/g' \
251                 -e 's/<\(b0\)>/x0; extern char \1v;/g'` \\'; \\"
252
253     # generate extern decls of dummy variables for each arg
254     echo "       echo '`echo $args | \
255             sed -e 's/<\('$typ'[1-9]\)>/extern \1, \1v;/g' \
256                 -e 's/<\([abBFIsSV][1-9]\)>/extern char \1v;/g' \
257                 -e 's/<\([Wv][1-9]\)>/extern int \1v;/g'` \\'; \\"
258
259     # generate bounded-pointer thunk declarator
260     echo "       echo '`echo $rtn | \
261             sed -e 's/<\('$ptr'0\)>/__typeof (\1v) *__bounded/g' \
262                 -e 's/<\('$int'0\)>/__typeof (\1v)/g'` BP_SYM ($strong) (`echo $arglist | \
263             sed -e 's/<\('$ptr'[1-9]\)>/__typeof (\1v) *__bounded \1a/g' \
264                 -e 's/<\('$int'[1-9]\)>/__typeof (\1v) \1a/g'`) { \\'; \\"
265
266     # generate extern primitive syscall declaration
267     echo "       echo '  extern `echo $rtn | \
268             sed -e 's/<\('$ptr'0\)>/__typeof (\1v) *__unbounded/g' \
269                 -e 's/<\('$int'0\)>/__typeof (\1v)/g'` ($callname) (`echo $arglist | \
270             sed -e 's/<\('$ptr'[1-9]\)>/__typeof (\1v) *__unbounded/g' \
271                 -e 's/<\('$int'[1-9]\)>/__typeof (\1v)/g'`); \\'; \\"
272
273     # generate call the primtive system call, optionally wrapping bounds
274     # around the result if the signature's return keyletter is `b'.
275     echo "       echo '  return `echo $rtn |
276             sed -e 's/<b0>/BOUNDED_N (/' \
277                 -e 's/<.0>//'`($callname) (`echo $arglist | \
278             sed -e 's/<\(a[1-9]\)>/__ptrvalue (\1a)/g' \
279                 -e 's/<\(n[1-9]\)>, <\(V[1-9]\)>/\1a, CHECK_N_PAGES (\2a, \1a)/g' \
280                 -e 's/<\(b[1-9]\)>, <\(n[1-9]\)>/CHECK_N (\1a, \2a), \2a/g' \
281                 -e 's/<\(b[1-9]\)>, <\(N[1-9]\)>/CHECK_N (\1a, *CHECK_1 (\2a)), __ptrvalue (\2a)/g' \
282                 -e 's/<\(B[1-9]\)>, <\(n[1-9]\)>/CHECK_N_NULL_OK (\1a, \2a), \2a/g' \
283                 -e 's/<\(B[1-9]\)>, <\(N[1-9]\)>/CHECK_N_NULL_OK (\1a, *CHECK_1 (\2a)), __ptrvalue (\2a)/g' \
284                 -e 's/<\(f[1-9]\)>/CHECK_N (\1a, 2)/g' \
285                 -e 's/<\(i[1-9]\)>, <\(F[1-9]\)>/\1a, CHECK_FCNTL (\2a, \1a)/g' \
286                 -e 's/<\(i[1-9]\)>, <\(I[1-9]\)>/\1a, CHECK_IOCTL (\2a, \1a)/g' \
287                 -e 's/<\(p[1-9]\)>/CHECK_1 (\1a)/g' \
288                 -e 's/<\([PW][1-9]\)>/CHECK_1_NULL_OK (\1a)/g' \
289                 -e 's/<\(s[1-9]\)>/CHECK_STRING (\1a)/g' \
290                 -e 's/<\(S[1-9]\)>/CHECK_STRING_NULL_OK (\1a)/g' \
291                 -e 's/<\([ivn][1-9]\)>/\1a/g'`)`echo $rtn $args |
292             sed -e 's/<b0>.*<\(n[1-9]\)>.*/, \1a)/' \
293                 -e 's/<.0>.*//'`; \\'; \\"
294
295     echo "       echo '} \\'; \\"
296
297     echo "       echo 'libc_hidden_def (BP_SYM ($strong)) \\'; \\"
298
299     # generate thunk aliases
300     for name in $nv_weak; do
301       echo "     echo 'weak_alias (BP_SYM ($strong), BP_SYM ($name)) \\'; \\"
302       echo "     echo 'libc_hidden_weak (BP_SYM ($name)) \\'; \\"
303     done
304
305     # wrap up
306     echo "\
307          echo ''; \\
308          echo '#include <bp-thunks.h>'; \\
309         ) | \$(COMPILE.c) -x c -o \$@ -"
310 ### Use this for debugging intermediate output:
311 ###     ) >\$(@:.ob=.c)
312 ###     \$(subst -c,-E,\$(COMPILE.c)) -o \$(@:.ob=.ib) \$(@:.ob=.c)
313 ###     \$(COMPILE.c) -x cpp-output -o \$@ \$(@:.ob=.ib)"
314     echo endif
315     ;;
316   esac
317
318 done