Blame sysdeps/unix/make-syscalls.sh

Packit 6c4009
#!/bin/sh
Packit 6c4009
Packit 6c4009
# Usage: make-syscalls.sh ../sysdeps/unix/common
Packit 6c4009
# Expects $sysdirs in environment.
Packit 6c4009
Packit 6c4009
##############################################################################
Packit 6c4009
#
Packit 6c4009
# This script is used to process the syscall data encoded in the various
Packit 6c4009
# syscalls.list files to produce thin assembly syscall wrappers around the
Packit 6c4009
# appropriate OS syscall. See syscall-template.s for more details on the
Packit 6c4009
# actual wrapper.
Packit 6c4009
#
Packit 6c4009
# Syscall Signature Prefixes:
Packit 6c4009
#
Packit 6c4009
# E: errno and return value are not set by the call
Packit 6c4009
# V: errno is not set, but errno or zero (success) is returned from the call
Packit 6c4009
#
Packit 6c4009
# Syscall Signature Key Letters:
Packit 6c4009
#
Packit 6c4009
# a: unchecked address (e.g., 1st arg to mmap)
Packit 6c4009
# b: non-NULL buffer (e.g., 2nd arg to read; return value from mmap)
Packit 6c4009
# B: optionally-NULL buffer (e.g., 4th arg to getsockopt)
Packit 6c4009
# f: buffer of 2 ints (e.g., 4th arg to socketpair)
Packit 6c4009
# F: 3rd arg to fcntl
Packit 6c4009
# i: scalar (any signedness & size: int, long, long long, enum, whatever)
Packit 6c4009
# I: 3rd arg to ioctl
Packit 6c4009
# n: scalar buffer length (e.g., 3rd arg to read)
Packit 6c4009
# N: pointer to value/return scalar buffer length (e.g., 6th arg to recvfrom)
Packit 6c4009
# p: non-NULL pointer to typed object (e.g., any non-void* arg)
Packit 6c4009
# P: optionally-NULL pointer to typed object (e.g., 2nd argument to gettimeofday)
Packit 6c4009
# s: non-NULL string (e.g., 1st arg to open)
Packit 6c4009
# S: optionally-NULL string (e.g., 1st arg to acct)
Packit 6c4009
# v: vararg scalar (e.g., optional 3rd arg to open)
Packit 6c4009
# V: byte-per-page vector (3rd arg to mincore)
Packit 6c4009
# W: wait status, optionally-NULL pointer to int (e.g., 2nd arg of wait4)
Packit 6c4009
#
Packit 6c4009
Packit 6c4009
##############################################################################
Packit 6c4009
Packit 6c4009
thisdir=$1; shift
Packit 6c4009
Packit 6c4009
echo ''
Packit 6c4009
echo \#### DIRECTORY = $thisdir
Packit 6c4009
# Check each sysdep dir with higher priority than this one,
Packit 6c4009
# and remove from $calls all the functions found in other dirs.
Packit 6c4009
# Punt when we reach the directory defining these syscalls.
Packit 6c4009
sysdirs=`for dir in $sysdirs; do
Packit 6c4009
	 test $dir = $thisdir && break; echo $dir; done`
Packit 6c4009
echo \#### SYSDIRS = $sysdirs
Packit 6c4009
Packit 6c4009
# Get the list of system calls for this directory.
Packit 6c4009
calls=`sed 's/#.*$//
Packit 6c4009
/^[ 	]*$/d' $thisdir/syscalls.list`
Packit 6c4009
Packit 6c4009
calls=`echo "$calls" |
Packit 6c4009
while read file caller rest; do
Packit 6c4009
  # Remove each syscall that is implemented by a file in $dir.
Packit 6c4009
  # If a syscall specified a "caller", then only compile that syscall
Packit 6c4009
  # if the caller function is also implemented in this directory.
Packit 6c4009
  srcfile=-;
Packit 6c4009
  for dir in $sysdirs; do
Packit 6c4009
     { test -f $dir/$file.c && srcfile=$dir/$file.c; } ||
Packit 6c4009
     { test -f $dir/$file.S && srcfile=$dir/$file.S; } ||
Packit 6c4009
     { test x$caller != x- &&
Packit 6c4009
	{ { test -f $dir/$caller.c && srcfile=$dir/$caller.c; } ||
Packit 6c4009
	  { test -f $dir/$caller.S && srcfile=$dir/$caller.S; }; }; } && break;
Packit 6c4009
  done;
Packit 6c4009
  echo $file $srcfile $caller $rest;
Packit 6c4009
done`
Packit 6c4009
Packit 6c4009
# Any calls left?
Packit 6c4009
test -n "$calls" || exit 0
Packit 6c4009
Packit 6c4009
# This uses variables $weak, $strong, and $any_versioned.
Packit 6c4009
emit_weak_aliases()
Packit 6c4009
{
Packit 6c4009
  # A shortcoming in the current gas is that it will only allow one
Packit 6c4009
  # version-alias per symbol.  So we create new strong aliases as needed.
Packit 6c4009
  vcount=""
Packit 6c4009
Packit 6c4009
  # We use the <shlib-compat.h> macros to generate the versioned aliases
Packit 6c4009
  # so that the version sets can be mapped to the configuration's
Packit 6c4009
  # minimum version set as per shlib-versions DEFAULT lines.  If an
Packit 6c4009
  # entry point is specified in the form NAME@VERSION:OBSOLETED, a
Packit 6c4009
  # SHLIB_COMPAT conditional is generated.
Packit 6c4009
  if [ $any_versioned = t ]; then
Packit 6c4009
    echo "	 echo '#include <shlib-compat.h>'; \\"
Packit 6c4009
  fi
Packit 6c4009
Packit 6c4009
  for name in $weak; do
Packit 6c4009
    case $name in
Packit 6c4009
      *@@*)
Packit 6c4009
	base=`echo $name | sed 's/@@.*//'`
Packit 6c4009
	ver=`echo $name | sed 's/.*@@//;s/\./_/g'`
Packit 6c4009
	echo "	 echo '#if IS_IN (libc)'; \\"
Packit 6c4009
	if test -z "$vcount" ; then
Packit 6c4009
	  source=$strong
Packit 6c4009
	  vcount=1
Packit 6c4009
	else
Packit 6c4009
	  source="${strong}_${vcount}"
Packit 6c4009
	  vcount=`expr $vcount + 1`
Packit 6c4009
	  echo "	 echo 'strong_alias ($strong, $source)'; \\"
Packit 6c4009
	fi
Packit 6c4009
	echo "	 echo 'versioned_symbol (libc, $source, $base, $ver)'; \\"
Packit 6c4009
	echo "	 echo '#else'; \\"
Packit 6c4009
	echo "	 echo 'weak_alias ($strong, $base)'; \\"
Packit 6c4009
	echo "	 echo '#endif'; \\"
Packit 6c4009
	;;
Packit 6c4009
      *@*)
Packit 6c4009
	base=`echo $name | sed 's/@.*//'`
Packit 6c4009
	ver=`echo $name | sed 's/.*@//;s/\./_/g'`
Packit 6c4009
	case $ver in
Packit 6c4009
	  *:*)
Packit 6c4009
	    compat_ver=${ver#*:}
Packit 6c4009
	    ver=${ver%%:*}
Packit 6c4009
	    compat_cond=" && SHLIB_COMPAT (libc, $ver, $compat_ver)"
Packit 6c4009
	    ;;
Packit 6c4009
	  *)
Packit 6c4009
	    compat_cond=
Packit 6c4009
	    ;;
Packit 6c4009
	esac
Packit 6c4009
	echo "	 echo '#if defined SHARED && IS_IN (libc)$compat_cond'; \\"
Packit 6c4009
	if test -z "$vcount" ; then
Packit 6c4009
	  source=$strong
Packit 6c4009
	  vcount=1
Packit 6c4009
	else
Packit 6c4009
	  source="${strong}_${vcount}"
Packit 6c4009
	  vcount=`expr $vcount + 1`
Packit 6c4009
	  echo "	 echo 'strong_alias ($strong, $source)'; \\"
Packit 6c4009
	fi
Packit 6c4009
	echo "	 echo 'compat_symbol (libc, $source, $base, $ver)'; \\"
Packit 6c4009
	echo "	 echo '#endif'; \\"
Packit 6c4009
	;;
Packit 6c4009
      !*)
Packit 6c4009
	name=`echo $name | sed 's/.//'`
Packit 6c4009
	echo "	 echo 'strong_alias ($strong, $name)'; \\"
Packit 6c4009
	echo "	 echo 'hidden_def ($name)'; \\"
Packit 6c4009
	;;
Packit 6c4009
      *)
Packit 6c4009
	echo "	 echo 'weak_alias ($strong, $name)'; \\"
Packit 6c4009
	echo "	 echo 'hidden_weak ($name)'; \\"
Packit 6c4009
	;;
Packit 6c4009
    esac
Packit 6c4009
  done
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
# Emit rules to compile the syscalls remaining in $calls.
Packit 6c4009
echo "$calls" |
Packit 6c4009
while read file srcfile caller syscall args strong weak; do
Packit 6c4009
Packit 6c4009
  vdso_syscall=
Packit 6c4009
  case x"$syscall" in
Packit 6c4009
  *:*@*)
Packit 6c4009
    vdso_syscall="${syscall#*:}"
Packit 6c4009
    syscall="${syscall%:*}"
Packit 6c4009
    ;;
Packit 6c4009
  esac
Packit 6c4009
Packit 6c4009
  case x"$syscall" in
Packit 6c4009
  x-) callnum=_ ;;
Packit 6c4009
  *)
Packit 6c4009
  # Figure out if $syscall is defined with a number in syscall.h.
Packit 6c4009
  callnum=-
Packit 6c4009
  eval `{ echo "#include <sysdep.h>";
Packit 6c4009
	echo "callnum=SYS_ify ($syscall)"; } |
Packit 6c4009
	  $asm_CPP -D__OPTIMIZE__ - |
Packit 6c4009
	  sed -n -e "/^callnum=.*$syscall/d" \
Packit 6c4009
		 -e "/^\(callnum=\)[ 	]*\(.*\)/s//\1'\2'/p"`
Packit 6c4009
  ;;
Packit 6c4009
  esac
Packit 6c4009
Packit 6c4009
  noerrno=0
Packit 6c4009
  errval=0
Packit 6c4009
  case $args in
Packit 6c4009
  E*) noerrno=1; args=`echo $args | sed 's/E:\?//'`;;
Packit 6c4009
  V*) errval=1; args=`echo $args | sed 's/V:\?//'`;;
Packit 6c4009
  esac
Packit 6c4009
Packit 6c4009
  # Derive the number of arguments from the argument signature
Packit 6c4009
  case $args in
Packit 6c4009
  [0-9]) nargs=$args;;
Packit 6c4009
  ?:) nargs=0;;
Packit 6c4009
  ?:?) nargs=1;;
Packit 6c4009
  ?:??) nargs=2;;
Packit 6c4009
  ?:???) nargs=3;;
Packit 6c4009
  ?:????) nargs=4;;
Packit 6c4009
  ?:?????) nargs=5;;
Packit 6c4009
  ?:??????) nargs=6;;
Packit 6c4009
  ?:???????) nargs=7;;
Packit 6c4009
  ?:????????) nargs=8;;
Packit 6c4009
  ?:?????????) nargs=9;;
Packit 6c4009
  esac
Packit 6c4009
Packit 6c4009
  # Make sure only the first syscall rule is used, if multiple dirs
Packit 6c4009
  # define the same syscall.
Packit 6c4009
  echo ''
Packit 6c4009
  echo "#### CALL=$file NUMBER=$callnum ARGS=$args SOURCE=$srcfile"
Packit 6c4009
Packit 6c4009
  # If there are versioned aliases the entry is only generated for the
Packit 6c4009
  # shared library, unless it is a default version.
Packit 6c4009
  any_versioned=f
Packit 6c4009
  shared_only=f
Packit 6c4009
  case $weak in
Packit 6c4009
    *@@*) any_versioned=t ;;
Packit 6c4009
    *@*) any_versioned=t shared_only=t ;;
Packit 6c4009
  esac
Packit 6c4009
Packit 6c4009
 case x$srcfile"$callnum" in
Packit 6c4009
 x--)
Packit 6c4009
  # Undefined callnum for an extra syscall.
Packit 6c4009
  if [ x$caller != x- ]; then
Packit 6c4009
    if [ $noerrno != 0 ]; then
Packit 6c4009
      echo >&2 "$0: no number for $fileno, no-error syscall ($strong $weak)"
Packit 6c4009
      exit 2
Packit 6c4009
    fi
Packit 6c4009
    echo "unix-stub-syscalls += $strong $weak"
Packit 6c4009
  fi
Packit 6c4009
  ;;
Packit 6c4009
 x*-) ;; ### Do nothing for undefined callnum
Packit 6c4009
 x-*)
Packit 6c4009
  echo "ifeq (,\$(filter $file,\$(unix-syscalls)))"
Packit 6c4009
Packit 6c4009
  if test $shared_only = t; then
Packit 6c4009
    # The versioned symbols are only in the shared library.
Packit 6c4009
    echo "ifneq (,\$(filter .os,\$(object-suffixes)))"
Packit 6c4009
  fi
Packit 6c4009
  # Accumulate the list of syscall files for this directory.
Packit 6c4009
  echo "unix-syscalls += $file"
Packit 6c4009
  test x$caller = x- || echo "unix-extra-syscalls += $file"
Packit 6c4009
Packit 6c4009
  # Emit a compilation rule for this syscall.
Packit 6c4009
  if test $shared_only = t; then
Packit 6c4009
    # The versioned symbols are only in the shared library.
Packit 6c4009
    echo "shared-only-routines += $file"
Packit 6c4009
    test -n "$vdso_syscall" || echo "\$(objpfx)${file}.os: \\"
Packit 6c4009
  else
Packit 6c4009
    object_suffixes='$(object-suffixes)'
Packit 6c4009
    test -z "$vdso_syscall" || object_suffixes='$(object-suffixes-noshared)'
Packit 6c4009
    echo "\
Packit 6c4009
\$(foreach p,\$(sysd-rules-targets),\
Packit 6c4009
\$(foreach o,${object_suffixes},\$(objpfx)\$(patsubst %,\$p,$file)\$o)): \\"
Packit 6c4009
  fi
Packit 6c4009
Packit 6c4009
  echo "		\$(..)sysdeps/unix/make-syscalls.sh"
Packit 6c4009
  case x"$callnum" in
Packit 6c4009
  x_)
Packit 6c4009
  echo "\
Packit 6c4009
	\$(make-target-directory)
Packit 6c4009
	(echo '/* Dummy module requested by syscalls.list */'; \\"
Packit 6c4009
  ;;
Packit 6c4009
  x*)
Packit 6c4009
  echo "\
Packit 6c4009
	\$(make-target-directory)
Packit 6c4009
	(echo '#define SYSCALL_NAME $syscall'; \\
Packit 6c4009
	 echo '#define SYSCALL_NARGS $nargs'; \\
Packit 6c4009
	 echo '#define SYSCALL_SYMBOL $strong'; \\
Packit 6c4009
	 echo '#define SYSCALL_NOERRNO $noerrno'; \\
Packit 6c4009
	 echo '#define SYSCALL_ERRVAL $errval'; \\
Packit 6c4009
	 echo '#include <syscall-template.S>'; \\"
Packit 6c4009
  ;;
Packit 6c4009
  esac
Packit 6c4009
Packit 6c4009
  # Append any weak aliases or versions defined for this syscall function.
Packit 6c4009
  emit_weak_aliases
Packit 6c4009
Packit 6c4009
  # And finally, pipe this all into the compiler.
Packit 6c4009
  echo '	) | $(compile-syscall) '"\
Packit 6c4009
\$(foreach p,\$(patsubst %$file,%,\$(basename \$(@F))),\$(\$(p)CPPFLAGS))"
Packit 6c4009
Packit 6c4009
  if test -n "$vdso_syscall"; then
Packit 6c4009
    # In the shared library, we're going to emit an IFUNC using a vDSO function.
Packit 6c4009
    # $vdso_syscall looks like "name@KERNEL_X.Y" where "name" is the symbol
Packit 6c4009
    # name in the vDSO and KERNEL_X.Y is its symbol version.
Packit 6c4009
    vdso_symbol="${vdso_syscall%@*}"
Packit 6c4009
    vdso_symver="${vdso_syscall#*@}"
Packit 6c4009
    vdso_symver=`echo "$vdso_symver" | sed 's/\./_/g'`
Packit 6c4009
    cat <
Packit 6c4009
Packit 6c4009
\$(foreach p,\$(sysd-rules-targets),\$(objpfx)\$(patsubst %,\$p,$file).os): \\
Packit 6c4009
		\$(..)sysdeps/unix/make-syscalls.sh
Packit 6c4009
	\$(make-target-directory)
Packit 6c4009
	(echo '#define ${strong} __redirect_${strong}'; \\
Packit 6c4009
	 echo '#include <dl-vdso.h>'; \\
Packit 6c4009
	 echo '#undef ${strong}'; \\
Packit 6c4009
	 echo '#define vdso_ifunc_init() \\'; \\
Packit 6c4009
	 echo '  PREPARE_VERSION_KNOWN (symver, ${vdso_symver})'; \\
Packit 6c4009
	 echo '__ifunc (__redirect_${strong}, ${strong},'; \\
Packit 6c4009
	 echo '         _dl_vdso_vsym ("${vdso_symbol}", &symver), void,'; \\
Packit 6c4009
	 echo '         vdso_ifunc_init)'; \\
Packit 6c4009
EOF
Packit 6c4009
    # This is doing "hidden_def (${strong})", but the compiler
Packit 6c4009
    # doesn't know that we've defined ${strong} in the same file, so
Packit 6c4009
    # we can't do it the normal way.
Packit 6c4009
    cat <
Packit 6c4009
	 echo 'asm (".globl __GI_${strong}");'; \\
Packit 6c4009
	 echo 'asm ("__GI_${strong} = ${strong}");'; \\
Packit 6c4009
EOF
Packit 6c4009
    emit_weak_aliases
Packit 6c4009
    cat <
Packit 6c4009
	) | \$(compile-stdin.c) \
Packit 6c4009
\$(foreach p,\$(patsubst %$file,%,\$(basename \$(@F))),\$(\$(p)CPPFLAGS))
Packit 6c4009
EOF
Packit 6c4009
  fi
Packit 6c4009
Packit 6c4009
  if test $shared_only = t; then
Packit 6c4009
    # The versioned symbols are only in the shared library.
Packit 6c4009
    echo endif
Packit 6c4009
  fi
Packit 6c4009
Packit 6c4009
  echo endif
Packit 6c4009
 ;;
Packit 6c4009
 esac
Packit 6c4009
Packit 6c4009
done