The SYS_ constants should cover all __NR_ values.

<sys/linux-syscalls.h> only contains constants for the syscalls
we're generating stubs for. We want all the syscalls available
on the architecture in question.

Keep using <sys/linux-syscalls.h> on ARM for now because the
__NR_ARM_set_tls and __NR_ARM_cacheflush values aren't in <asm/unistd.h>.

Change-Id: I66683950d87d9b18d6107d0acc0ed238a4496f44
diff --git a/libc/tools/gensyscalls.py b/libc/tools/gensyscalls.py
index aaa3de3..26cf3a5 100755
--- a/libc/tools/gensyscalls.py
+++ b/libc/tools/gensyscalls.py
@@ -48,7 +48,7 @@
 x86_header = """/* autogenerated by gensyscalls.py */
 #include <linux/err.h>
 #include <machine/asm.h>
-#include <sys/linux-syscalls.h>
+#include <asm/unistd.h>
 
 ENTRY(%(fname)s)
 """
@@ -115,7 +115,7 @@
 #
 
 mips_call = """/* autogenerated by gensyscalls.py */
-#include <sys/linux-syscalls.h>
+#include <asm/unistd.h>
     .text
     .globl %(fname)s
     .align 4
@@ -303,9 +303,19 @@
                 t["asm-mips"] = self.mips_genstub(syscall_func,"__NR_"+syscall_name)
 
 
-    def gen_NR_syscall(self, glibc_fp, linux_fp, name, id):
+    def gen_NR_syscall(self, linux_fp, name, id):
         linux_fp.write("#define __NR_%-25s    (__NR_SYSCALL_BASE + %d)\n" % (name,id))
-        glibc_fp.write("#define SYS_%-25s __NR_%s\n" % (name, name))
+
+
+    def scan_linux_unistd_h(self, fp, path):
+        pattern = re.compile(r'^#define __NR_([a-z]\S+) .*')
+        syscalls = set() # MIPS defines everything three times; work around that.
+        for line in open(path):
+            m = re.search(pattern, line)
+            if m:
+                syscalls.add(m.group(1))
+        for syscall in sorted(syscalls):
+            fp.write("#define SYS_%s __NR_%s\n" % (syscall, syscall))
 
 
     def gen_linux_syscalls_h(self):
@@ -315,6 +325,18 @@
         glibc_fp.write("#ifndef _BIONIC_GLIBC_SYSCALLS_H_\n")
         glibc_fp.write("#define _BIONIC_GLIBC_SYSCALLS_H_\n")
 
+        glibc_fp.write("#if defined(__arm__)\n")
+        self.scan_linux_unistd_h(glibc_fp, "libc/kernel/arch-arm/asm/unistd.h")
+        glibc_fp.write("#elif defined(__mips__)\n")
+        self.scan_linux_unistd_h(glibc_fp, "libc/kernel/arch-mips/asm/unistd.h")
+        glibc_fp.write("#elif defined(__i386__)\n")
+        self.scan_linux_unistd_h(glibc_fp, "libc/kernel/arch-x86/asm/unistd_32.h")
+        glibc_fp.write("#endif\n")
+
+        glibc_fp.write("#endif /* _BIONIC_GLIBC_SYSCALLS_H_ */\n")
+        glibc_fp.close()
+        self.other_files.append(glibc_syscalls_h_path)
+
         linux_syscalls_h_path = "include/sys/linux-syscalls.h"
         D("generating " + linux_syscalls_h_path)
         fp = create_file(linux_syscalls_h_path)
@@ -335,50 +357,40 @@
             sc_id  = sc["common"]
             sc_name = sc["name"]
             if sc_id >= 0:
-                self.gen_NR_syscall(glibc_fp, fp, sc_name, sc_id)
+                self.gen_NR_syscall(fp, sc_name, sc_id)
 
         # now, all arm-specific syscalls
         fp.write("\n#ifdef __arm__\n")
-        glibc_fp.write( "\n#ifdef __arm__\n")
         for sc in self.syscalls:
             sc_id  = sc["armid"]
             sc_name = sc["name"]
             if sc_id >= 0:
-                self.gen_NR_syscall(glibc_fp, fp, sc_name, sc_id)
+                self.gen_NR_syscall(fp, sc_name, sc_id)
         fp.write("#endif\n")
-        glibc_fp.write("#endif\n")
 
         gen_syscalls = {}
         # all i386-specific syscalls
         fp.write("\n#ifdef __i386__\n")
-        glibc_fp.write("\n#ifdef __i386__\n")
         for sc in sorted(self.syscalls,key=lambda x:x["x86id"]):
             sc_id  = sc["x86id"]
             sc_name = sc["name"]
             if sc_id >= 0 and sc_name not in gen_syscalls:
-                self.gen_NR_syscall(glibc_fp, fp, sc_name, sc_id)
+                self.gen_NR_syscall(fp, sc_name, sc_id)
                 gen_syscalls[sc_name] = True
         fp.write("#endif\n")
-        glibc_fp.write("#endif\n")
 
         # all mips-specific syscalls
         fp.write("\n#ifdef __mips__\n")
-        glibc_fp.write("\n#ifdef __mips__\n")
         for sc in sorted(self.syscalls,key=lambda x:x["mipsid"]):
             sc_id = sc["mipsid"]
             if sc_id >= 0:
-                self.gen_NR_syscall(glibc_fp, fp, sc["name"], sc_id)
+                self.gen_NR_syscall(fp, sc["name"], sc_id)
         fp.write( "#endif\n" );
-        glibc_fp.write( "#endif\n" );
 
         fp.write( "\n#endif\n" )
         fp.write( "\n#endif /* _BIONIC_LINUX_SYSCALLS_H_ */\n" );
         fp.close()
 
-        glibc_fp.write("#endif\n")
-        glibc_fp.close()
-
-        self.other_files.append(glibc_syscalls_h_path)
         self.other_files.append(linux_syscalls_h_path)