8ae002
Posted upstream at:
8ae002
8ae002
  https://sourceware.org/ml/libc-alpha/2017-04/msg00082.html
8ae002
fa3bfd
sysdeps/unix/sysv/linux/syscall-names.list is stored as a separate patch
978e96
(glibc-rh1439165-syscall-names.patch) in the source RPM, and is the
978e96
baseline for subsequent changes.
8ae002
8ae002
Author: Florian Weimer <fweimer@redhat.com>
8ae002
8ae002
    <bits/syscall.h>: Use an arch-independent system call list on Linux
fa3bfd
8ae002
    This commit changes the way the list of SYS_* system call macros
8ae002
    is created on Linux.  glibc now contains a list of all known system
8ae002
    calls, and the generated <bits/syscall.h> file defines the SYS_
8ae002
    macro only if the correspnding __NR_ macro is defined by the kernel
8ae002
    headers.
fa3bfd
8ae002
    As a result, there glibc does not have to be rebuilt to pick up
8ae002
    system calls if the glibc sources already know about them.  This
8ae002
    means that glibc can be built with older kernel headers, and if
8ae002
    the installed kernel headers are upgraded afterwards, additional
8ae002
    SYS_ macros become available as long as glibc has a record for
8ae002
    those system calls.
8ae002
fa3bfd
The explicit system call list for system call management was not
fa3bfd
accepted upstream.
fa3bfd
fa3bfd
Fedora bug: https://bugzilla.redhat.com/show_bug.cgi?id=1484729
fa3bfd
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
fa3bfd
index 366b1d7..95cff0e 100644
8ae002
--- a/sysdeps/unix/sysv/linux/Makefile
8ae002
+++ b/sysdeps/unix/sysv/linux/Makefile
fa3bfd
@@ -39,75 +39,46 @@ sysdep_headers += sys/mount.h sys/acct.h sys/sysctl.h \
8ae002
 
8ae002
 tests += tst-clone
8ae002
 
8ae002
-# Generate the list of SYS_* macros for the system calls (__NR_* macros).
fa3bfd
-
8ae002
-# If there is more than one syscall list for different architecture
8ae002
-# variants, the CPU/Makefile defines abi-variants to be a list of names
8ae002
-# for those variants (e.g. 32 64), and, for each variant, defines
8ae002
-# abi-$(variant)-options to be compiler options to cause <asm/unistd.h>
8ae002
-# to define the desired list of syscalls and abi-$(variant)-condition to
8ae002
-# be the condition for those options to use in a C #if condition.
8ae002
-# abi-includes may be defined to a list of headers to include
8ae002
-# in the generated header, if the default does not suffice.
8ae002
-#
8ae002
-# The generated header is compiled with `-ffreestanding' to avoid any
8ae002
-# circular dependencies against the installed implementation headers.
8ae002
-# Such a dependency would require the implementation header to be
8ae002
-# installed before the generated header could be built (See bug 15711).
8ae002
-# In current practice the generated header dependencies do not include
8ae002
-# any of the implementation headers removed by the use of `-ffreestanding'.
fa3bfd
-
8ae002
-$(objpfx)bits/syscall%h $(objpfx)bits/syscall%d: ../sysdeps/unix/sysv/linux/sys/syscall.h
fa3bfd
+# Generate the list of SYS_* macros for the system calls (__NR_*
fa3bfd
+# macros).  The file syscall-names.list contains all possible system
fa3bfd
+# call names, and the generated header file produces SYS_* macros for
fa3bfd
+# the __NR_* macros which are actually defined.
fa3bfd
+
fa3bfd
+generated += bits/syscall.h
fa3bfd
+$(objpfx)bits/syscall.h: \
fa3bfd
+  ../sysdeps/unix/sysv/linux/gen-syscall-h.awk \
fa3bfd
+  ../sysdeps/unix/sysv/linux/syscall-names.list
fa3bfd
 	$(make-target-directory)
8ae002
-	{ \
8ae002
-	 echo '/* Generated at libc build time from kernel syscall list.  */';\
8ae002
-	 echo ''; \
8ae002
-	 echo '#ifndef _SYSCALL_H'; \
8ae002
-	 echo '# error "Never use <bits/syscall.h> directly; include <sys/syscall.h> instead."'; \
8ae002
-	 echo '#endif'; \
8ae002
-	 echo ''; \
8ae002
-	 $(foreach h,$(abi-includes), echo '#include <$(h)>';) \
8ae002
-	 echo ''; \
8ae002
-	 $(if $(abi-variants), \
8ae002
-	 $(foreach v,$(abi-variants),\
8ae002
-	 $(CC) -ffreestanding -E -MD -MP -MF $(@:.h=.d)-t$(v) -MT '$(@:.d=.h) $(@:.h=.d)' \
8ae002
-	       -x c $(sysincludes) $< $(abi-$(v)-options) \
8ae002
-	       -D_LIBC -dM | \
8ae002
-	 sed -n 's@^#define __NR_\([^ ]*\) .*$$@#define SYS_\1 __NR_\1@p' | \
8ae002
-	 LC_ALL=C sort > $(@:.d=.h).new$(v); \
8ae002
-	 $(if $(abi-$(v)-condition),\
8ae002
-	 echo '#if $(abi-$(v)-condition)';) \
8ae002
-	 cat $(@:.d=.h).new$(v); \
8ae002
-	 $(if $(abi-$(v)-condition),echo '#endif';) \
8ae002
-	 rm -f $(@:.d=.h).new$(v); \
8ae002
-	 ), \
8ae002
-	 $(CC) -ffreestanding -E -MD -MP -MF $(@:.h=.d)-t$(v) -MT '$(@:.d=.h) $(@:.h=.d)' \
8ae002
-	       -x c $(sysincludes) $< \
8ae002
-	       -D_LIBC -dM | \
8ae002
-	 sed -n 's@^#define __NR_\([^ ]*\) .*$$@#define SYS_\1 __NR_\1@p' | \
8ae002
-	 LC_ALL=C sort;) \
8ae002
-	} > $(@:.d=.h).new
8ae002
-	mv -f $(@:.d=.h).new $(@:.d=.h)
8ae002
-ifdef abi-variants
8ae002
-ifneq (,$(objpfx))
8ae002
-	sed $(sed-remove-objpfx) \
8ae002
-	 $(foreach v,$(abi-variants),$(@:.h=.d)-t$(v)) > $(@:.h=.d)-t3
8ae002
-else
8ae002
-	cat $(foreach v,$(abi-variants),$(@:.h=.d)-t$(v)) \
8ae002
-	 > $(@:.h=.d)-t3
8ae002
-endif
8ae002
-	rm -f $(foreach v,$(abi-variants),$(@:.h=.d)-t$(v))
8ae002
-	mv -f $(@:.h=.d)-t3 $(@:.h=.d)
8ae002
-else
8ae002
-	mv -f $(@:.h=.d)-t $(@:.h=.d)
8ae002
-endif
8ae002
-
8ae002
-ifndef no_deps
8ae002
-# Get the generated list of dependencies (probably /usr/include/asm/unistd.h).
8ae002
--include $(objpfx)bits/syscall.d
8ae002
-endif
8ae002
-generated += bits/syscall.h bits/syscall.d
8ae002
-endif
fa3bfd
+	$(AWK) -f $^ > $@-tmp
fa3bfd
+	$(move-if-change) $@-tmp $@
fa3bfd
+
fa3bfd
+# All macros defined by <sys/syscall.h>.  Include <bits/syscall.h>
fa3bfd
+# explicitly because <sys/sycall.h> skips it if _LIBC is defined.
fa3bfd
+$(objpfx)tst-syscall-list-macros.list: \
fa3bfd
+  $(objpfx)bits/syscall.h ../sysdeps/unix/sysv/linux/sys/syscall.h
fa3bfd
+	printf '#include <sys/syscall.h>\n#include <bits/syscall.h>\n' | \
fa3bfd
+	  $(CC) -E -o $@-tmp $(CFLAGS) $(CPPFLAGS) -x c - -dM
fa3bfd
+	$(move-if-change) $@-tmp $@
fa3bfd
+
fa3bfd
+# __NR_* system call names.  Used by the test below.
fa3bfd
+$(objpfx)tst-syscall-list-nr.list: \
fa3bfd
+  ../sysdeps/unix/sysv/linux/filter-nr-syscalls.awk \
fa3bfd
+  $(objpfx)tst-syscall-list-macros.list
fa3bfd
+	$(AWK) -f $^ > $@-tmp
fa3bfd
+	$(move-if-change) $@-tmp $@
fa3bfd
+
fa3bfd
+# SYS_* system call names.  Used by the test below.
fa3bfd
+$(objpfx)tst-syscall-list-sys.list: $(objpfx)tst-syscall-list-macros.list
fa3bfd
+	$(AWK) '/^#define SYS_/ { print substr($$2, 5) }' $< > $@-tmp
fa3bfd
+	$(move-if-change) $@-tmp $@
fa3bfd
+
fa3bfd
+tests: $(objpfx)tst-syscall-list.out
fa3bfd
+$(objpfx)tst-syscall-list.out: \
fa3bfd
+  ../sysdeps/unix/sysv/linux/tst-syscall-list.sh \
fa3bfd
+  $(objpfx)tst-syscall-list-nr.list $(objpfx)tst-syscall-list-sys.list
fa3bfd
+	$(BASH) $^ > $@
fa3bfd
+
8ae002
+endif # $(subdir) == misc
8ae002
 
8ae002
 ifeq ($(subdir),time)
8ae002
 sysdep_headers += sys/timex.h bits/timex.h
fa3bfd
diff --git a/sysdeps/unix/sysv/linux/filter-nr-syscalls.awk b/sysdeps/unix/sysv/linux/filter-nr-syscalls.awk
fa3bfd
new file mode 100644
fa3bfd
index 0000000..15b052a
8ae002
--- /dev/null
8ae002
+++ b/sysdeps/unix/sysv/linux/filter-nr-syscalls.awk
8ae002
@@ -0,0 +1,35 @@
8ae002
+# Filter preprocessor __NR_* macros and extract system call names.
8ae002
+# Copyright (C) 2017 Free Software Foundation, Inc.
8ae002
+# This file is part of the GNU C Library.
8ae002
+#
8ae002
+# The GNU C Library is free software; you can redistribute it and/or
8ae002
+# modify it under the terms of the GNU Lesser General Public
8ae002
+# License as published by the Free Software Foundation; either
8ae002
+# version 2.1 of the License, or (at your option) any later version.
8ae002
+#
8ae002
+# The GNU C Library is distributed in the hope that it will be useful,
8ae002
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+# Lesser General Public License for more details.
8ae002
+#
8ae002
+# You should have received a copy of the GNU Lesser General Public
8ae002
+# License along with the GNU C Library; if not, see
8ae002
+# <http://www.gnu.org/licenses/>.
8ae002
+
8ae002
+# Skip reserved system calls.
8ae002
+/^#define __NR_(unused|reserved)[0-9]+ / {
8ae002
+    next;
8ae002
+}
8ae002
+
8ae002
+# Skip pseudo-system calls which describe ranges.
8ae002
+/^#define __NR_(syscalls|arch_specific_syscall|(OABI_)?SYSCALL_BASE) / {
8ae002
+    next;
8ae002
+}
8ae002
+/^#define __NR_(|64_|[NO]32_)Linux(_syscalls)? / {
8ae002
+    next;
8ae002
+}
8ae002
+
8ae002
+# Print the remaining _NR_* macros as system call names.
8ae002
+/^#define __NR_/ {
8ae002
+    print substr($2, 6);
8ae002
+}
fa3bfd
diff --git a/sysdeps/unix/sysv/linux/gen-syscall-h.awk b/sysdeps/unix/sysv/linux/gen-syscall-h.awk
fa3bfd
new file mode 100644
fa3bfd
index 0000000..0a27b3c
8ae002
--- /dev/null
8ae002
+++ b/sysdeps/unix/sysv/linux/gen-syscall-h.awk
8ae002
@@ -0,0 +1,75 @@
8ae002
+# Generate SYS_* macros from a list in a text file.
8ae002
+# Copyright (C) 2017 Free Software Foundation, Inc.
8ae002
+# This file is part of the GNU C Library.
8ae002
+#
8ae002
+# The GNU C Library is free software; you can redistribute it and/or
8ae002
+# modify it under the terms of the GNU Lesser General Public
8ae002
+# License as published by the Free Software Foundation; either
8ae002
+# version 2.1 of the License, or (at your option) any later version.
8ae002
+#
8ae002
+# The GNU C Library is distributed in the hope that it will be useful,
8ae002
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+# Lesser General Public License for more details.
8ae002
+#
8ae002
+# You should have received a copy of the GNU Lesser General Public
8ae002
+# License along with the GNU C Library; if not, see
8ae002
+# <http://www.gnu.org/licenses/>.
8ae002
+
8ae002
+# Emit a conditional definition for SYS_NAME.
8ae002
+function emit(name) {
8ae002
+    print "#ifdef __NR_" name;
8ae002
+    print "# define SYS_" name " __NR_" name;
8ae002
+    print "#endif";
8ae002
+    print "";
8ae002
+}
8ae002
+
8ae002
+# Bail out with an error.
8ae002
+function fatal(message) {
8ae002
+    print FILENAME ":" FNR ": " message > "/dev/stderr";
8ae002
+    exit 1;
8ae002
+}
8ae002
+
8ae002
+BEGIN {
8ae002
+    name = "";
8ae002
+    kernel = "";
8ae002
+}
8ae002
+
8ae002
+# Skip empty lines and comments.
8ae002
+/^\s*(|#.*)$/ {
8ae002
+    next;
8ae002
+}
8ae002
+
8ae002
+# Kernel version.  Used for documentation purposes only.
8ae002
+/^kernel [0-9.]+$/ {
8ae002
+    if (kernel != "") {
8ae002
+        fatal("duplicate kernel directive");
8ae002
+    }
8ae002
+    kernel = $2;
8ae002
+    print "/* Generated at libc build time from syscall list.  */";
8ae002
+    print "/* The system call list corresponds to kernel " kernel ".  */";
8ae002
+    print "";
8ae002
+    print "#ifndef _SYSCALL_H"
8ae002
+    print "# error \"Never use <bits/syscall.h> directly; include <sys/syscall.h> instead.\"";
8ae002
+    print "#endif";
8ae002
+    print "";
8ae002
+    next;
8ae002
+}
8ae002
+
8ae002
+# If there is just one word, it is a system call.
8ae002
+/^[a-zA-Z_][a-zA-Z0-9_]+$/ {
8ae002
+    if (kernel == "") {
8ae002
+        fatal("expected kernel directive before this line");
8ae002
+    }
8ae002
+    if ($1 <= name) {
8ae002
+        fatal("name " name " violates ordering");
8ae002
+    }
8ae002
+    emit($1);
8ae002
+    name = $1;
8ae002
+    next;
8ae002
+}
8ae002
+
8ae002
+# The rest has to be syntax errors.
8ae002
+// {
8ae002
+    fatal("unrecognized syntax");
8ae002
+}
fa3bfd
diff --git a/sysdeps/unix/sysv/linux/tst-syscall-list.sh b/sysdeps/unix/sysv/linux/tst-syscall-list.sh
fa3bfd
new file mode 100644
fa3bfd
index 0000000..f48b7cd
8ae002
--- /dev/null
8ae002
+++ b/sysdeps/unix/sysv/linux/tst-syscall-list.sh
8ae002
@@ -0,0 +1,72 @@
8ae002
+#!/bin/bash
8ae002
+# Consistency checks for the system call list
8ae002
+# Copyright (C) 2017 Free Software Foundation, Inc.
8ae002
+# This file is part of the GNU C Library.
8ae002
+#
8ae002
+# The GNU C Library is free software; you can redistribute it and/or
8ae002
+# modify it under the terms of the GNU Lesser General Public
8ae002
+# License as published by the Free Software Foundation; either
8ae002
+# version 2.1 of the License, or (at your option) any later version.
8ae002
+#
8ae002
+# The GNU C Library is distributed in the hope that it will be useful,
8ae002
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+# Lesser General Public License for more details.
8ae002
+#
8ae002
+# You should have received a copy of the GNU Lesser General Public
8ae002
+# License along with the GNU C Library; if not, see
8ae002
+# <http://www.gnu.org/licenses/>.
8ae002
+
8ae002
+export LC_ALL=C
8ae002
+set -e
8ae002
+set -o pipefail
8ae002
+
8ae002
+if test $# != 2 ; then
8ae002
+    echo "error: wrong number of arguments: $#"
8ae002
+    exit 1
8ae002
+fi
8ae002
+
8ae002
+list_nr="$1"
8ae002
+list_sys="$2"
8ae002
+
8ae002
+errors=0
8ae002
+
8ae002
+# Use getpid as a system call which is expected to be always defined.
8ae002
+# alpha uses getxpid instead, so it is permitted as an alternative.
8ae002
+if ! grep -E -q '^getx?pid$' -- "$list_nr" ; then
8ae002
+    echo "error: __NR_getpid not defined"
8ae002
+    errors=1
8ae002
+fi
8ae002
+if ! grep -E -q '^getx?pid$' -- "$list_sys" ; then
8ae002
+    echo "error: SYS_getpid not defined"
8ae002
+    errors=1
8ae002
+fi
8ae002
+
8ae002
+comm_1="$(mktemp)"
8ae002
+comm_2="$(mktemp)"
8ae002
+comm_result="$(mktemp)"
8ae002
+cleanup () {
8ae002
+    rm -f -- "$comm_1" "$comm_2" "$comm_result"
8ae002
+}
8ae002
+trap cleanup 0
8ae002
+
8ae002
+sort -o "$comm_1" -- "$list_nr"
8ae002
+sort -o "$comm_2" -- "$list_sys"
8ae002
+
8ae002
+# Check for missing SYS_* macros.
8ae002
+comm --check-order -2 -3 -- "$comm_1" "$comm_2" > "$comm_result"
8ae002
+if test -s "$comm_result"; then
8ae002
+    echo "error: These system calls need to be added to syscall-names.list:"
8ae002
+    cat -- "$comm_result"
8ae002
+    errors=1
8ae002
+fi
8ae002
+
8ae002
+# Check for additional SYS_* macros.
8ae002
+comm --check-order -1 -3 -- "$comm_1" "$comm_2" > "$comm_result"
8ae002
+if test -s "$comm_result"; then
8ae002
+    echo "error: The following system calls have unexpected SYS_* macros:"
8ae002
+    cat -- "$comm_result"
8ae002
+    errors=1
8ae002
+fi
8ae002
+
8ae002
+exit "$errors"