blob: 5aa635b3d4e529bb1b60e0ba29257f58888ff98e [file] [log] [blame]
Andrew Geisslerea144b032023-01-27 16:03:57 -06001From 99f1e61b2957226254a116fde7fd73bf07034012 Mon Sep 17 00:00:00 2001
Brad Bishopbec4ebc2022-08-03 09:55:16 -04002From: Khem Raj <raj.khem@gmail.com>
3Date: Mon, 8 Mar 2021 16:04:20 -0800
4Subject: [PATCH] gcc: poison-system-directories
5
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
14Upstream-Status: Pending
15Signed-off-by: Mark Hatle <mark.hatle@windriver.com>
16Signed-off-by: Khem Raj <raj.khem@gmail.com>
17---
18 gcc/common.opt | 4 ++++
19 gcc/config.in | 10 ++++++++++
20 gcc/configure | 19 +++++++++++++++++++
21 gcc/configure.ac | 16 ++++++++++++++++
22 gcc/doc/invoke.texi | 9 +++++++++
Andrew Geisslerea144b032023-01-27 16:03:57 -060023 gcc/gcc.cc | 15 ++++++++++++---
24 gcc/incpath.cc | 21 +++++++++++++++++++++
25 7 files changed, 91 insertions(+), 3 deletions(-)
Brad Bishopbec4ebc2022-08-03 09:55:16 -040026
27diff --git a/gcc/common.opt b/gcc/common.opt
Andrew Geisslerea144b032023-01-27 16:03:57 -060028index 8a0dafc52..0357868e2 100644
Brad Bishopbec4ebc2022-08-03 09:55:16 -040029--- a/gcc/common.opt
30+++ b/gcc/common.opt
Andrew Geisslerea144b032023-01-27 16:03:57 -060031@@ -710,6 +710,10 @@ Wreturn-local-addr
Brad Bishopbec4ebc2022-08-03 09:55:16 -040032 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 Geisslerea144b032023-01-27 16:03:57 -060043index 64c27c9cf..a693cb8a8 100644
Brad Bishopbec4ebc2022-08-03 09:55:16 -040044--- a/gcc/config.in
45+++ b/gcc/config.in
Patrick Williams2194f502022-10-16 14:26:09 -050046@@ -230,6 +230,16 @@
Brad Bishopbec4ebc2022-08-03 09:55:16 -040047 #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
54+/* Define to warn for use of native system header directories */
55+#ifndef USED_FOR_TARGET
56+#undef POISON_BY_DEFAULT
57+#endif
58+
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 Geisslerea144b032023-01-27 16:03:57 -060064index 2b83acfb0..8bb97578c 100755
Brad Bishopbec4ebc2022-08-03 09:55:16 -040065--- a/gcc/configure
66+++ b/gcc/configure
Andrew Geisslerea144b032023-01-27 16:03:57 -060067@@ -1023,6 +1023,7 @@ enable_maintainer_mode
Brad Bishopbec4ebc2022-08-03 09:55:16 -040068 enable_link_mutex
69 enable_link_serialization
70 enable_version_specific_runtime_libs
71+enable_poison_system_directories
72 enable_plugin
73 enable_host_shared
74 enable_libquadmath_support
Andrew Geisslerea144b032023-01-27 16:03:57 -060075@@ -1785,6 +1786,8 @@ Optional Features:
Brad Bishopbec4ebc2022-08-03 09:55:16 -040076 --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 Geisslerea144b032023-01-27 16:03:57 -060084@@ -31996,6 +31999,22 @@ if test "${enable_version_specific_runtime_libs+set}" = set; then :
Brad Bishopbec4ebc2022-08-03 09:55:16 -040085 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+
95+if test "x${enable_poison_system_directories}" != "xno"; then
96+
97+$as_echo "#define ENABLE_POISON_SYSTEM_DIRECTORIES 1" >>confdefs.h
98+if test "$enable_poison_system_directories" = "error"; then
99+$as_echo "#define POISON_BY_DEFAULT 1" >>confdefs.h
100+fi
101+
102+fi
103+
104 # Substitute configuration variables
105
106
107diff --git a/gcc/configure.ac b/gcc/configure.ac
Andrew Geisslerea144b032023-01-27 16:03:57 -0600108index daf2a708c..6155b83a7 100644
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400109--- a/gcc/configure.ac
110+++ b/gcc/configure.ac
Andrew Geisslerea144b032023-01-27 16:03:57 -0600111@@ -7435,6 +7435,22 @@ AC_ARG_ENABLE(version-specific-runtime-libs,
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400112 [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],
117+ [warn for use of native system header directories (no/yes/error)]),,
118+ [enable_poison_system_directories=no])
119+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])
122+ AC_DEFINE([ENABLE_POISON_SYSTEM_DIRECTORIES],
123+ [1],
124+ [Define to warn for use of native system header directories])
125+ 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
129+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 Geisslerea144b032023-01-27 16:03:57 -0600135index ff6c338be..a8ebfa59a 100644
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400136--- a/gcc/doc/invoke.texi
137+++ b/gcc/doc/invoke.texi
Andrew Geisslerea144b032023-01-27 16:03:57 -0600138@@ -379,6 +379,7 @@ Objective-C and Objective-C++ Dialects}.
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400139 -Wpacked -Wno-packed-bitfield-compat -Wpacked-not-aligned -Wpadded @gol
140 -Wparentheses -Wno-pedantic-ms-format @gol
141 -Wpointer-arith -Wno-pointer-compare -Wno-pointer-to-int-cast @gol
142+-Wno-poison-system-directories @gol
143 -Wno-pragmas -Wno-prio-ctor-dtor -Wredundant-decls @gol
144 -Wrestrict -Wno-return-local-addr -Wreturn-type @gol
145 -Wno-scalar-storage-order -Wsequence-point @gol
Andrew Geisslerea144b032023-01-27 16:03:57 -0600146@@ -8029,6 +8030,14 @@ made up of data only and thus requires no special treatment. But, for
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400147 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
Andrew Geisslerea144b032023-01-27 16:03:57 -0600161diff --git a/gcc/gcc.cc b/gcc/gcc.cc
162index beefde7f6..4e6557b3c 100644
163--- a/gcc/gcc.cc
164+++ b/gcc/gcc.cc
165@@ -1162,6 +1162,8 @@ proper position among the other output files. */
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400166 "%{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 Geisslerea144b032023-01-27 16:03:57 -0600174@@ -1257,8 +1259,11 @@ static const char *cpp_unique_options =
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400175 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
Andrew Geisslerea144b032023-01-27 16:03:57 -0600182+ " %{!Wno-error=poison-system-directories:-Werror=poison-system-directories}"
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400183+#endif
184+ " %{undef} %{save-temps*:-fpch-preprocess}";
185
186 /* Pass -d* flags, possibly modifying -dumpdir, -dumpbase et al.
187
Andrew Geisslerea144b032023-01-27 16:03:57 -0600188@@ -1287,7 +1292,11 @@ static const char *cc1_options =
189 %{coverage:-fprofile-arcs -ftest-coverage}\
190 %{fprofile-arcs|fprofile-generate*|coverage:\
191 %{!fprofile-update=single:\
192- %{pthread:-fprofile-update=prefer-atomic}}}";
193+ %{pthread:-fprofile-update=prefer-atomic}}}"
194+#ifdef POISON_BY_DEFAULT
195+ " %{!Wno-error=poison-system-directories:-Werror=poison-system-directories}"
196+#endif
197+ ;
198
199 static const char *asm_options =
200 "%{-target-help:%:print-asm-header()} "
201diff --git a/gcc/incpath.cc b/gcc/incpath.cc
202index 622204a38..5ac03c086 100644
203--- a/gcc/incpath.cc
204+++ b/gcc/incpath.cc
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400205@@ -26,6 +26,7 @@
206 #include "intl.h"
207 #include "incpath.h"
208 #include "cppdefault.h"
209+#include "diagnostic-core.h"
210
211 /* Microsoft Windows does not natively support inodes.
212 VMS has non-numeric inodes. */
Andrew Geisslerea144b032023-01-27 16:03:57 -0600213@@ -399,6 +400,26 @@ merge_include_chains (const char *sysroot, cpp_reader *pfile, int verbose)
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400214 }
215 fprintf (stderr, _("End of search list.\n"));
216 }
217+
218+#ifdef ENABLE_POISON_SYSTEM_DIRECTORIES
219+ if (flag_poison_system_directories)
220+ {
221+ struct cpp_dir *p;
222+
223+ for (p = heads[INC_QUOTE]; p; p = p->next)
224+ {
225+ if ((!strncmp (p->name, "/usr/include", 12))
226+ || (!strncmp (p->name, "/usr/local/include", 18))
227+ || (!strncmp (p->name, "/usr/X11R6/include", 18))
228+ || (!strncmp (p->name, "/sw/include", 11))
229+ || (!strncmp (p->name, "/opt/include", 12)))
230+ warning (OPT_Wpoison_system_directories,
231+ "include location \"%s\" is unsafe for "
232+ "cross-compilation",
233+ p->name);
234+ }
235+ }
236+#endif
237 }
238
239 /* Use given -I paths for #include "..." but not #include <...>, and