Fix NSIG.

Our sigset_t definition hasn't been tied to our NSIG definition since we
switched to uapi headers, so we can now fix it without breaking the LP32 ABI.
The kernel uapi headers define and use _NSIG, so we need to have our scripts
rename the kernel's definitions out of the way, then we can define _NSIG
and NSIG in terms of the kernel's off-by-one value.

Bug: 12938442
Change-Id: Ic7c86fd5be5ad1d822f7b2b1d88c8a0d70a1ac0f
diff --git a/libc/kernel/tools/cpp.py b/libc/kernel/tools/cpp.py
index 04e4256..2c40d7c 100644
--- a/libc/kernel/tools/cpp.py
+++ b/libc/kernel/tools/cpp.py
@@ -1521,7 +1521,7 @@
         """replace tokens according to the given dict
            """
         for b in self.blocks:
-            if not b.isDirective():
+            if (not b.isDirective()) or b.isDefine():
                 for tok in b.tokens:
                     if tok.id == tokIDENT:
                         if tok.value in replacements:
diff --git a/libc/kernel/tools/defaults.py b/libc/kernel/tools/defaults.py
index f235803..09556c9 100644
--- a/libc/kernel/tools/defaults.py
+++ b/libc/kernel/tools/defaults.py
@@ -57,10 +57,15 @@
     "mips": {"off_t":"__kernel_off_t"},
     "x86": {},
     }
+
 # Replace tokens in the output according to this mapping
 kernel_token_replacements = {
     "asm": "__asm__",
-    "__unused": "__linux_unused", # The kernel usage of __unused conflicts with the macro defined in sys/cdefs.h
+    # The kernel usage of __unused for unused struct fields conflicts with the macro defined in <sys/cdefs.h>.
+    "__unused": "__linux_unused",
+    # The kernel's _NSIG/NSIG are one less than the userspace value, so we need to move them aside.
+    "_NSIG": "_KERNEL__NSIG",
+    "NSIG": "_KERNEL_NSIG",
     }
 
 # this is the set of known static inline functions that we want to keep
diff --git a/libc/kernel/uapi/asm-arm/asm/signal.h b/libc/kernel/uapi/asm-arm/asm/signal.h
index 9297a70..512e22a 100644
--- a/libc/kernel/uapi/asm-arm/asm/signal.h
+++ b/libc/kernel/uapi/asm-arm/asm/signal.h
@@ -21,7 +21,7 @@
 #include <linux/types.h>
 struct siginfo;
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
-#define NSIG 32
+#define _KERNEL_NSIG 32
 typedef unsigned long sigset_t;
 #define SIGHUP 1
 #define SIGINT 2
@@ -67,7 +67,7 @@
 #define SIGUNUSED 31
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 #define SIGRTMIN 32
-#define SIGRTMAX _NSIG
+#define SIGRTMAX _KERNEL__NSIG
 #define SIGSWI 32
 #define SA_NOCLDSTOP 0x00000001
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
diff --git a/libc/kernel/uapi/asm-generic/signal.h b/libc/kernel/uapi/asm-generic/signal.h
index c4c7e00..fe7d9a0 100644
--- a/libc/kernel/uapi/asm-generic/signal.h
+++ b/libc/kernel/uapi/asm-generic/signal.h
@@ -19,10 +19,10 @@
 #ifndef _UAPI__ASM_GENERIC_SIGNAL_H
 #define _UAPI__ASM_GENERIC_SIGNAL_H
 #include <linux/types.h>
-#define _NSIG 64
+#define _KERNEL__NSIG 64
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 #define _NSIG_BPW __BITS_PER_LONG
-#define _NSIG_WORDS (_NSIG / _NSIG_BPW)
+#define _NSIG_WORDS (_KERNEL__NSIG / _NSIG_BPW)
 #define SIGHUP 1
 #define SIGINT 2
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
@@ -68,7 +68,7 @@
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 #define SIGRTMIN 32
 #ifndef SIGRTMAX
-#define SIGRTMAX _NSIG
+#define SIGRTMAX _KERNEL__NSIG
 #endif
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 #define SA_NOCLDSTOP 0x00000001
diff --git a/libc/kernel/uapi/asm-mips/asm/signal.h b/libc/kernel/uapi/asm-mips/asm/signal.h
index 771e31a..53f5015 100644
--- a/libc/kernel/uapi/asm-mips/asm/signal.h
+++ b/libc/kernel/uapi/asm-mips/asm/signal.h
@@ -19,10 +19,10 @@
 #ifndef _UAPI_ASM_SIGNAL_H
 #define _UAPI_ASM_SIGNAL_H
 #include <linux/types.h>
-#define _NSIG 128
+#define _KERNEL__NSIG 128
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 #define _NSIG_BPW (sizeof(unsigned long) * 8)
-#define _NSIG_WORDS (_NSIG / _NSIG_BPW)
+#define _NSIG_WORDS (_KERNEL__NSIG / _NSIG_BPW)
 typedef struct {
  unsigned long sig[_NSIG_WORDS];
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
@@ -72,7 +72,7 @@
 #define SIGXFSZ 31
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 #define SIGRTMIN 32
-#define SIGRTMAX _NSIG
+#define SIGRTMAX _KERNEL__NSIG
 #define SA_ONSTACK 0x08000000
 #define SA_RESETHAND 0x80000000
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
diff --git a/libc/kernel/uapi/asm-x86/asm/signal.h b/libc/kernel/uapi/asm-x86/asm/signal.h
index bba6bc7..6f5b435 100644
--- a/libc/kernel/uapi/asm-x86/asm/signal.h
+++ b/libc/kernel/uapi/asm-x86/asm/signal.h
@@ -24,7 +24,7 @@
 #include <linux/time.h>
 #include <linux/compiler.h>
 struct siginfo;
-#define NSIG 32
+#define _KERNEL_NSIG 32
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 typedef unsigned long sigset_t;
 #endif
@@ -72,7 +72,7 @@
 #define SIGUNUSED 31
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 #define SIGRTMIN 32
-#define SIGRTMAX _NSIG
+#define SIGRTMAX _KERNEL__NSIG
 #define SA_NOCLDSTOP 0x00000001u
 #define SA_NOCLDWAIT 0x00000002u
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
diff --git a/libc/kernel/uapi/asm-x86/asm/stat.h b/libc/kernel/uapi/asm-x86/asm/stat.h
index 0998106..04c5569 100644
--- a/libc/kernel/uapi/asm-x86/asm/stat.h
+++ b/libc/kernel/uapi/asm-x86/asm/stat.h
@@ -102,7 +102,7 @@
  long __linux_unused[3];
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 };
-#define INIT_STRUCT_STAT_PADDING(st) do {   st.__pad0 = 0;   st.__unused[0] = 0;   st.__unused[1] = 0;   st.__unused[2] = 0;  } while (0)
+#define INIT_STRUCT_STAT_PADDING(st) do {   st.__pad0 = 0;   st.__linux_unused[0] = 0;   st.__linux_unused[1] = 0;   st.__linux_unused[2] = 0;  } while (0)
 #endif
 struct __old_kernel_stat {
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */