From 118c6f054711d437167ff125a88c9236bfc8099c Mon Sep 17 00:00:00 2001
From: Khem Raj <raj.khem@gmail.com>
Date: Mon, 8 Mar 2021 16:04:20 -0800
Subject: [PATCH] gcc: poison-system-directories

Add /sw/include and /opt/include based on the original
zecke-no-host-includes.patch patch.  The original patch checked for
/usr/include, /sw/include and /opt/include and then triggered a failure and
aborted.

Instead, we add the two missing items to the current scan.  If the user
wants this to be a failure, they can add "-Werror=poison-system-directories".

Upstream-Status: Pending
Signed-off-by: Mark Hatle <mark.hatle@windriver.com>
Signed-off-by: Khem Raj <raj.khem@gmail.com>
---
 gcc/common.opt      |  4 ++++
 gcc/config.in       | 10 ++++++++++
 gcc/configure       | 19 +++++++++++++++++++
 gcc/configure.ac    | 16 ++++++++++++++++
 gcc/doc/invoke.texi |  9 +++++++++
 gcc/gcc.c           |  9 +++++++--
 gcc/incpath.c       | 21 +++++++++++++++++++++
 7 files changed, 86 insertions(+), 2 deletions(-)

diff --git a/gcc/common.opt b/gcc/common.opt
index c75dd36843e..49acbd6ed44 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -683,6 +683,10 @@ Wreturn-local-addr
 Common Var(warn_return_local_addr) Init(1) Warning
 Warn about returning a pointer/reference to a local or temporary variable.
 
+Wpoison-system-directories
+Common Var(flag_poison_system_directories) Init(1) Warning
+Warn for -I and -L options using system directories if cross compiling
+
 Wshadow
 Common Var(warn_shadow) Warning
 Warn when one variable shadows another.  Same as -Wshadow=global.
diff --git a/gcc/config.in b/gcc/config.in
index 10a13cde586..8848284da41 100644
--- a/gcc/config.in
+++ b/gcc/config.in
@@ -218,6 +218,16 @@
 #endif
 
 
+/* Define to warn for use of native system header directories */
+#ifndef USED_FOR_TARGET
+#undef ENABLE_POISON_SYSTEM_DIRECTORIES
+#endif
+/* Define to warn for use of native system header directories */
+#ifndef USED_FOR_TARGET
+#undef POISON_BY_DEFAULT
+#endif
+
+
 /* Define if you want all operations on RTL (the basic data structure of the
    optimizer and back end) to be checked for dynamic type safety at runtime.
    This is quite expensive. */
diff --git a/gcc/configure b/gcc/configure
index 9bb436ce7bd..3f0734bff11 100755
--- a/gcc/configure
+++ b/gcc/configure
@@ -1020,6 +1020,7 @@ enable_maintainer_mode
 enable_link_mutex
 enable_link_serialization
 enable_version_specific_runtime_libs
+enable_poison_system_directories
 enable_plugin
 enable_host_shared
 enable_libquadmath_support
@@ -1782,6 +1783,8 @@ Optional Features:
   --enable-version-specific-runtime-libs
                           specify that runtime libraries should be installed
                           in a compiler-specific directory
+  --enable-poison-system-directories
+                          warn for use of native system header directories
   --enable-plugin         enable plugin support
   --enable-host-shared    build host code as shared libraries
   --disable-libquadmath-support
@@ -31325,6 +31328,22 @@ if test "${enable_version_specific_runtime_libs+set}" = set; then :
 fi
 
 
+# Check whether --enable-poison-system-directories was given.
+if test "${enable_poison_system_directories+set}" = set; then :
+  enableval=$enable_poison_system_directories;
+else
+  enable_poison_system_directories=no
+fi
+
+if test "x${enable_poison_system_directories}" != "xno"; then
+
+$as_echo "#define ENABLE_POISON_SYSTEM_DIRECTORIES 1" >>confdefs.h
+if test "$enable_poison_system_directories" = "error"; then
+$as_echo "#define POISON_BY_DEFAULT 1" >>confdefs.h
+fi
+
+fi
+
 # Substitute configuration variables
 
 
diff --git a/gcc/configure.ac b/gcc/configure.ac
index caa611933df..54e21764b3e 100644
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -7123,6 +7123,22 @@ AC_ARG_ENABLE(version-specific-runtime-libs,
                 [specify that runtime libraries should be
                  installed in a compiler-specific directory])])
 
+AC_ARG_ENABLE([poison-system-directories],
+             AS_HELP_STRING([--enable-poison-system-directories],
+                            [warn for use of native system header directories (no/yes/error)]),,
+             [enable_poison_system_directories=no])
+AC_MSG_NOTICE([poisoned directories $enable_poison_system_directories])
+if test "x${enable_poison_system_directories}" != "xno"; then
+  AC_MSG_NOTICE([poisoned directories enabled])
+  AC_DEFINE([ENABLE_POISON_SYSTEM_DIRECTORIES],
+           [1],
+           [Define to warn for use of native system header directories])
+  if test $enable_poison_system_directories = "error"; then
+   AC_MSG_NOTICE([poisoned directories are fatal])
+   AC_DEFINE([POISON_BY_DEFAULT], [1], [Define to make poison warnings errors])
+  fi
+fi
+
 # Substitute configuration variables
 AC_SUBST(subdirs)
 AC_SUBST(srcdir)
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 7a368959e5e..6659a903bf0 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -369,6 +369,7 @@ Objective-C and Objective-C++ Dialects}.
 -Wpacked  -Wno-packed-bitfield-compat  -Wpacked-not-aligned  -Wpadded @gol
 -Wparentheses  -Wno-pedantic-ms-format @gol
 -Wpointer-arith  -Wno-pointer-compare  -Wno-pointer-to-int-cast @gol
+-Wno-poison-system-directories @gol
 -Wno-pragmas  -Wno-prio-ctor-dtor  -Wredundant-decls @gol
 -Wrestrict  -Wno-return-local-addr  -Wreturn-type @gol
 -Wno-scalar-storage-order  -Wsequence-point @gol
@@ -7735,6 +7736,14 @@ made up of data only and thus requires no special treatment.  But, for
 most targets, it is made up of code and thus requires the stack to be
 made executable in order for the program to work properly.
 
+@item -Wno-poison-system-directories
+@opindex Wno-poison-system-directories
+Do not warn for @option{-I} or @option{-L} options using system
+directories such as @file{/usr/include} when cross compiling.  This
+option is intended for use in chroot environments when such
+directories contain the correct headers and libraries for the target
+system rather than the host.
+
 @item -Wfloat-equal
 @opindex Wfloat-equal
 @opindex Wno-float-equal
diff --git a/gcc/gcc.c b/gcc/gcc.c
index 7837553958b..19c75b6e20d 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -1152,6 +1152,8 @@ proper position among the other output files.  */
    "%{fuse-ld=*:-fuse-ld=%*} " LINK_COMPRESS_DEBUG_SPEC \
    "%X %{o*} %{e*} %{N} %{n} %{r}\
     %{s} %{t} %{u*} %{z} %{Z} %{!nostdlib:%{!r:%{!nostartfiles:%S}}} \
+    %{Wno-poison-system-directories:--no-poison-system-directories} \
+    %{Werror=poison-system-directories:--error-poison-system-directories} \
     %{static|no-pie|static-pie:} %@{L*} %(mfwrap) %(link_libgcc) " \
     VTABLE_VERIFICATION_SPEC " " SANITIZER_EARLY_SPEC " %o "" \
     %{fopenacc|fopenmp|%:gt(%{ftree-parallelize-loops=*:%*} 1):\
@@ -1247,8 +1249,11 @@ static const char *cpp_unique_options =
 static const char *cpp_options =
 "%(cpp_unique_options) %1 %{m*} %{std*&ansi&trigraphs} %{W*&pedantic*} %{w}\
  %{f*} %{g*:%{%:debug-level-gt(0):%{g*}\
- %{!fno-working-directory:-fworking-directory}}} %{O*}\
- %{undef} %{save-temps*:-fpch-preprocess}";
+ %{!fno-working-directory:-fworking-directory}}} %{O*}"
+#ifdef POISON_BY_DEFAULT
+ " -Werror=poison-system-directories"
+#endif
+ " %{undef} %{save-temps*:-fpch-preprocess}";
 
 /* Pass -d* flags, possibly modifying -dumpdir, -dumpbase et al.
 
diff --git a/gcc/incpath.c b/gcc/incpath.c
index 446d280321d..fbfc0ce03b8 100644
--- a/gcc/incpath.c
+++ b/gcc/incpath.c
@@ -26,6 +26,7 @@
 #include "intl.h"
 #include "incpath.h"
 #include "cppdefault.h"
+#include "diagnostic-core.h"
 
 /* Microsoft Windows does not natively support inodes.
    VMS has non-numeric inodes.  */
@@ -395,6 +396,26 @@ merge_include_chains (const char *sysroot, cpp_reader *pfile, int verbose)
 	}
       fprintf (stderr, _("End of search list.\n"));
     }
+
+#ifdef ENABLE_POISON_SYSTEM_DIRECTORIES
+  if (flag_poison_system_directories)
+    {
+       struct cpp_dir *p;
+
+       for (p = heads[INC_QUOTE]; p; p = p->next)
+         {
+          if ((!strncmp (p->name, "/usr/include", 12))
+              || (!strncmp (p->name, "/usr/local/include", 18))
+              || (!strncmp (p->name, "/usr/X11R6/include", 18))
+              || (!strncmp (p->name, "/sw/include", 11))
+              || (!strncmp (p->name, "/opt/include", 12)))
+            warning (OPT_Wpoison_system_directories,
+                     "include location \"%s\" is unsafe for "
+                     "cross-compilation",
+                     p->name);
+         }
+    }
+#endif
 }
 
 /* Use given -I paths for #include "..." but not #include <...>, and
