blob: 18a9fb87012f696776b317e465c1ac5123d697ee [file] [log] [blame]
Andrew Geisslerc926e172021-05-07 16:11:35 -05001From 118c6f054711d437167ff125a88c9236bfc8099c Mon Sep 17 00:00:00 2001
Andrew Geissler82c905d2020-04-13 13:39:40 -05002From: Khem Raj <raj.khem@gmail.com>
Andrew Geisslerc926e172021-05-07 16:11:35 -05003Date: Mon, 8 Mar 2021 16:04:20 -0800
Andrew Geisslerc182c622020-05-15 14:13:32 -05004Subject: [PATCH] gcc: poison-system-directories
Andrew Geissler82c905d2020-04-13 13:39:40 -05005
6Add /sw/include and /opt/include based on the original
7zecke-no-host-includes.patch patch. The original patch checked for
8/usr/include, /sw/include and /opt/include and then triggered a failure and
9aborted.
10
11Instead, we add the two missing items to the current scan. If the user
12wants this to be a failure, they can add "-Werror=poison-system-directories".
13
Andrew Geisslerc926e172021-05-07 16:11:35 -050014Upstream-Status: Pending
Andrew Geissler82c905d2020-04-13 13:39:40 -050015Signed-off-by: Mark Hatle <mark.hatle@windriver.com>
16Signed-off-by: Khem Raj <raj.khem@gmail.com>
Andrew Geissler82c905d2020-04-13 13:39:40 -050017---
18 gcc/common.opt | 4 ++++
Andrew Geissler95ac1b82021-03-31 14:34:31 -050019 gcc/config.in | 10 ++++++++++
20 gcc/configure | 19 +++++++++++++++++++
21 gcc/configure.ac | 16 ++++++++++++++++
Andrew Geissler82c905d2020-04-13 13:39:40 -050022 gcc/doc/invoke.texi | 9 +++++++++
Andrew Geissler95ac1b82021-03-31 14:34:31 -050023 gcc/gcc.c | 9 +++++++--
Andrew Geissler82c905d2020-04-13 13:39:40 -050024 gcc/incpath.c | 21 +++++++++++++++++++++
Andrew Geissler95ac1b82021-03-31 14:34:31 -050025 7 files changed, 86 insertions(+), 2 deletions(-)
Andrew Geissler82c905d2020-04-13 13:39:40 -050026
27diff --git a/gcc/common.opt b/gcc/common.opt
Andrew Geisslerc926e172021-05-07 16:11:35 -050028index c75dd36843e..49acbd6ed44 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -050029--- a/gcc/common.opt
30+++ b/gcc/common.opt
Andrew Geisslerc926e172021-05-07 16:11:35 -050031@@ -683,6 +683,10 @@ Wreturn-local-addr
Andrew Geissler82c905d2020-04-13 13:39:40 -050032 Common Var(warn_return_local_addr) Init(1) Warning
33 Warn about returning a pointer/reference to a local or temporary variable.
34
35+Wpoison-system-directories
36+Common Var(flag_poison_system_directories) Init(1) Warning
37+Warn for -I and -L options using system directories if cross compiling
38+
39 Wshadow
40 Common Var(warn_shadow) Warning
41 Warn when one variable shadows another. Same as -Wshadow=global.
42diff --git a/gcc/config.in b/gcc/config.in
Andrew Geisslerc926e172021-05-07 16:11:35 -050043index 10a13cde586..8848284da41 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -050044--- a/gcc/config.in
45+++ b/gcc/config.in
Andrew Geisslerc926e172021-05-07 16:11:35 -050046@@ -218,6 +218,16 @@
Andrew Geissler82c905d2020-04-13 13:39:40 -050047 #endif
48
49
50+/* Define to warn for use of native system header directories */
51+#ifndef USED_FOR_TARGET
52+#undef ENABLE_POISON_SYSTEM_DIRECTORIES
53+#endif
Andrew Geissler95ac1b82021-03-31 14:34:31 -050054+/* Define to warn for use of native system header directories */
55+#ifndef USED_FOR_TARGET
56+#undef POISON_BY_DEFAULT
57+#endif
Andrew Geissler82c905d2020-04-13 13:39:40 -050058+
59+
60 /* Define if you want all operations on RTL (the basic data structure of the
61 optimizer and back end) to be checked for dynamic type safety at runtime.
62 This is quite expensive. */
63diff --git a/gcc/configure b/gcc/configure
Andrew Geisslerc926e172021-05-07 16:11:35 -050064index 9bb436ce7bd..3f0734bff11 100755
Andrew Geissler82c905d2020-04-13 13:39:40 -050065--- a/gcc/configure
66+++ b/gcc/configure
Andrew Geisslerc926e172021-05-07 16:11:35 -050067@@ -1020,6 +1020,7 @@ enable_maintainer_mode
Andrew Geissler82c905d2020-04-13 13:39:40 -050068 enable_link_mutex
Andrew Geisslerc926e172021-05-07 16:11:35 -050069 enable_link_serialization
Andrew Geissler82c905d2020-04-13 13:39:40 -050070 enable_version_specific_runtime_libs
71+enable_poison_system_directories
72 enable_plugin
73 enable_host_shared
74 enable_libquadmath_support
Andrew Geisslerc926e172021-05-07 16:11:35 -050075@@ -1782,6 +1783,8 @@ Optional Features:
Andrew Geissler82c905d2020-04-13 13:39:40 -050076 --enable-version-specific-runtime-libs
77 specify that runtime libraries should be installed
78 in a compiler-specific directory
79+ --enable-poison-system-directories
80+ warn for use of native system header directories
81 --enable-plugin enable plugin support
82 --enable-host-shared build host code as shared libraries
83 --disable-libquadmath-support
Andrew Geisslerc926e172021-05-07 16:11:35 -050084@@ -31325,6 +31328,22 @@ if test "${enable_version_specific_runtime_libs+set}" = set; then :
Andrew Geissler82c905d2020-04-13 13:39:40 -050085 fi
86
87
88+# Check whether --enable-poison-system-directories was given.
89+if test "${enable_poison_system_directories+set}" = set; then :
90+ enableval=$enable_poison_system_directories;
91+else
92+ enable_poison_system_directories=no
93+fi
94+
Andrew Geissler95ac1b82021-03-31 14:34:31 -050095+if test "x${enable_poison_system_directories}" != "xno"; then
Andrew Geissler82c905d2020-04-13 13:39:40 -050096+
97+$as_echo "#define ENABLE_POISON_SYSTEM_DIRECTORIES 1" >>confdefs.h
Andrew Geissler95ac1b82021-03-31 14:34:31 -050098+if test "$enable_poison_system_directories" = "error"; then
99+$as_echo "#define POISON_BY_DEFAULT 1" >>confdefs.h
100+fi
Andrew Geissler82c905d2020-04-13 13:39:40 -0500101+
102+fi
103+
104 # Substitute configuration variables
105
106
107diff --git a/gcc/configure.ac b/gcc/configure.ac
Andrew Geisslerc926e172021-05-07 16:11:35 -0500108index caa611933df..54e21764b3e 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -0500109--- a/gcc/configure.ac
110+++ b/gcc/configure.ac
Andrew Geisslerc926e172021-05-07 16:11:35 -0500111@@ -7123,6 +7123,22 @@ AC_ARG_ENABLE(version-specific-runtime-libs,
Andrew Geissler82c905d2020-04-13 13:39:40 -0500112 [specify that runtime libraries should be
113 installed in a compiler-specific directory])])
114
115+AC_ARG_ENABLE([poison-system-directories],
116+ AS_HELP_STRING([--enable-poison-system-directories],
Andrew Geisslerc926e172021-05-07 16:11:35 -0500117+ [warn for use of native system header directories (no/yes/error)]),,
Andrew Geissler82c905d2020-04-13 13:39:40 -0500118+ [enable_poison_system_directories=no])
Andrew Geissler95ac1b82021-03-31 14:34:31 -0500119+AC_MSG_NOTICE([poisoned directories $enable_poison_system_directories])
120+if test "x${enable_poison_system_directories}" != "xno"; then
121+ AC_MSG_NOTICE([poisoned directories enabled])
Andrew Geissler82c905d2020-04-13 13:39:40 -0500122+ AC_DEFINE([ENABLE_POISON_SYSTEM_DIRECTORIES],
123+ [1],
124+ [Define to warn for use of native system header directories])
Andrew Geissler95ac1b82021-03-31 14:34:31 -0500125+ if test $enable_poison_system_directories = "error"; then
126+ AC_MSG_NOTICE([poisoned directories are fatal])
127+ AC_DEFINE([POISON_BY_DEFAULT], [1], [Define to make poison warnings errors])
128+ fi
Andrew Geissler82c905d2020-04-13 13:39:40 -0500129+fi
130+
131 # Substitute configuration variables
132 AC_SUBST(subdirs)
133 AC_SUBST(srcdir)
134diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
Andrew Geisslerc926e172021-05-07 16:11:35 -0500135index 7a368959e5e..6659a903bf0 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -0500136--- a/gcc/doc/invoke.texi
137+++ b/gcc/doc/invoke.texi
Andrew Geisslerc926e172021-05-07 16:11:35 -0500138@@ -369,6 +369,7 @@ Objective-C and Objective-C++ Dialects}.
Andrew Geisslerc182c622020-05-15 14:13:32 -0500139 -Wpacked -Wno-packed-bitfield-compat -Wpacked-not-aligned -Wpadded @gol
Andrew Geissler82c905d2020-04-13 13:39:40 -0500140 -Wparentheses -Wno-pedantic-ms-format @gol
Andrew Geisslerc182c622020-05-15 14:13:32 -0500141 -Wpointer-arith -Wno-pointer-compare -Wno-pointer-to-int-cast @gol
Andrew Geissler82c905d2020-04-13 13:39:40 -0500142+-Wno-poison-system-directories @gol
Andrew Geissler82c905d2020-04-13 13:39:40 -0500143 -Wno-pragmas -Wno-prio-ctor-dtor -Wredundant-decls @gol
Andrew Geisslerc182c622020-05-15 14:13:32 -0500144 -Wrestrict -Wno-return-local-addr -Wreturn-type @gol
145 -Wno-scalar-storage-order -Wsequence-point @gol
Andrew Geisslerc926e172021-05-07 16:11:35 -0500146@@ -7735,6 +7736,14 @@ made up of data only and thus requires no special treatment. But, for
Andrew Geissler82c905d2020-04-13 13:39:40 -0500147 most targets, it is made up of code and thus requires the stack to be
148 made executable in order for the program to work properly.
149
150+@item -Wno-poison-system-directories
151+@opindex Wno-poison-system-directories
152+Do not warn for @option{-I} or @option{-L} options using system
153+directories such as @file{/usr/include} when cross compiling. This
154+option is intended for use in chroot environments when such
155+directories contain the correct headers and libraries for the target
156+system rather than the host.
157+
158 @item -Wfloat-equal
159 @opindex Wfloat-equal
160 @opindex Wno-float-equal
161diff --git a/gcc/gcc.c b/gcc/gcc.c
Andrew Geisslerc926e172021-05-07 16:11:35 -0500162index 7837553958b..19c75b6e20d 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -0500163--- a/gcc/gcc.c
164+++ b/gcc/gcc.c
Andrew Geisslerc926e172021-05-07 16:11:35 -0500165@@ -1152,6 +1152,8 @@ proper position among the other output files. */
Andrew Geissler82c905d2020-04-13 13:39:40 -0500166 "%{fuse-ld=*:-fuse-ld=%*} " LINK_COMPRESS_DEBUG_SPEC \
167 "%X %{o*} %{e*} %{N} %{n} %{r}\
168 %{s} %{t} %{u*} %{z} %{Z} %{!nostdlib:%{!r:%{!nostartfiles:%S}}} \
169+ %{Wno-poison-system-directories:--no-poison-system-directories} \
170+ %{Werror=poison-system-directories:--error-poison-system-directories} \
171 %{static|no-pie|static-pie:} %@{L*} %(mfwrap) %(link_libgcc) " \
172 VTABLE_VERIFICATION_SPEC " " SANITIZER_EARLY_SPEC " %o "" \
173 %{fopenacc|fopenmp|%:gt(%{ftree-parallelize-loops=*:%*} 1):\
Andrew Geisslerc926e172021-05-07 16:11:35 -0500174@@ -1247,8 +1249,11 @@ static const char *cpp_unique_options =
Andrew Geissler95ac1b82021-03-31 14:34:31 -0500175 static const char *cpp_options =
176 "%(cpp_unique_options) %1 %{m*} %{std*&ansi&trigraphs} %{W*&pedantic*} %{w}\
177 %{f*} %{g*:%{%:debug-level-gt(0):%{g*}\
178- %{!fno-working-directory:-fworking-directory}}} %{O*}\
179- %{undef} %{save-temps*:-fpch-preprocess}";
180+ %{!fno-working-directory:-fworking-directory}}} %{O*}"
181+#ifdef POISON_BY_DEFAULT
182+ " -Werror=poison-system-directories"
183+#endif
184+ " %{undef} %{save-temps*:-fpch-preprocess}";
185
Andrew Geisslerc926e172021-05-07 16:11:35 -0500186 /* Pass -d* flags, possibly modifying -dumpdir, -dumpbase et al.
187
Andrew Geissler82c905d2020-04-13 13:39:40 -0500188diff --git a/gcc/incpath.c b/gcc/incpath.c
Andrew Geisslerc926e172021-05-07 16:11:35 -0500189index 446d280321d..fbfc0ce03b8 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -0500190--- a/gcc/incpath.c
191+++ b/gcc/incpath.c
192@@ -26,6 +26,7 @@
193 #include "intl.h"
194 #include "incpath.h"
195 #include "cppdefault.h"
196+#include "diagnostic-core.h"
197
198 /* Microsoft Windows does not natively support inodes.
199 VMS has non-numeric inodes. */
Andrew Geisslerc926e172021-05-07 16:11:35 -0500200@@ -395,6 +396,26 @@ merge_include_chains (const char *sysroot, cpp_reader *pfile, int verbose)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500201 }
202 fprintf (stderr, _("End of search list.\n"));
203 }
204+
205+#ifdef ENABLE_POISON_SYSTEM_DIRECTORIES
206+ if (flag_poison_system_directories)
207+ {
208+ struct cpp_dir *p;
209+
210+ for (p = heads[INC_QUOTE]; p; p = p->next)
211+ {
212+ if ((!strncmp (p->name, "/usr/include", 12))
213+ || (!strncmp (p->name, "/usr/local/include", 18))
214+ || (!strncmp (p->name, "/usr/X11R6/include", 18))
215+ || (!strncmp (p->name, "/sw/include", 11))
216+ || (!strncmp (p->name, "/opt/include", 12)))
217+ warning (OPT_Wpoison_system_directories,
218+ "include location \"%s\" is unsafe for "
219+ "cross-compilation",
220+ p->name);
221+ }
222+ }
223+#endif
224 }
225
226 /* Use given -I paths for #include "..." but not #include <...>, and