blob: 697bdc821934b5cd002bc48311651a1e9a142747 [file] [log] [blame]
Andrew Geisslerc182c622020-05-15 14:13:32 -05001From 74cc21f474402cf3578e37e1d7a1a22bbd070f6a Mon Sep 17 00:00:00 2001
Andrew Geissler82c905d2020-04-13 13:39:40 -05002From: Khem Raj <raj.khem@gmail.com>
3Date: Fri, 29 Mar 2013 08:59:00 +0400
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
14Signed-off-by: Mark Hatle <mark.hatle@windriver.com>
15Signed-off-by: Khem Raj <raj.khem@gmail.com>
Andrew Geissler95ac1b82021-03-31 14:34:31 -050016Signed-off-by: Wang Mingyu <wangmy@cn.fujitsu.com>
Andrew Geissler82c905d2020-04-13 13:39:40 -050017
18Upstream-Status: Pending
19---
20 gcc/common.opt | 4 ++++
Andrew Geissler95ac1b82021-03-31 14:34:31 -050021 gcc/config.in | 10 ++++++++++
22 gcc/configure | 19 +++++++++++++++++++
23 gcc/configure.ac | 16 ++++++++++++++++
Andrew Geissler82c905d2020-04-13 13:39:40 -050024 gcc/doc/invoke.texi | 9 +++++++++
Andrew Geissler95ac1b82021-03-31 14:34:31 -050025 gcc/gcc.c | 9 +++++++--
Andrew Geissler82c905d2020-04-13 13:39:40 -050026 gcc/incpath.c | 21 +++++++++++++++++++++
Andrew Geissler95ac1b82021-03-31 14:34:31 -050027 7 files changed, 86 insertions(+), 2 deletions(-)
Andrew Geissler82c905d2020-04-13 13:39:40 -050028
29diff --git a/gcc/common.opt b/gcc/common.opt
Andrew Geissler95ac1b82021-03-31 14:34:31 -050030index 3ec7743ea..d3c3e51dc 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -050031--- a/gcc/common.opt
32+++ b/gcc/common.opt
Andrew Geisslerc182c622020-05-15 14:13:32 -050033@@ -682,6 +682,10 @@ Wreturn-local-addr
Andrew Geissler82c905d2020-04-13 13:39:40 -050034 Common Var(warn_return_local_addr) Init(1) Warning
35 Warn about returning a pointer/reference to a local or temporary variable.
36
37+Wpoison-system-directories
38+Common Var(flag_poison_system_directories) Init(1) Warning
39+Warn for -I and -L options using system directories if cross compiling
40+
41 Wshadow
42 Common Var(warn_shadow) Warning
43 Warn when one variable shadows another. Same as -Wshadow=global.
44diff --git a/gcc/config.in b/gcc/config.in
Andrew Geissler95ac1b82021-03-31 14:34:31 -050045index 364eba477..7d2c3bbf1 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -050046--- a/gcc/config.in
47+++ b/gcc/config.in
Andrew Geissler95ac1b82021-03-31 14:34:31 -050048@@ -224,6 +224,16 @@
Andrew Geissler82c905d2020-04-13 13:39:40 -050049 #endif
50
51
52+/* Define to warn for use of native system header directories */
53+#ifndef USED_FOR_TARGET
54+#undef ENABLE_POISON_SYSTEM_DIRECTORIES
55+#endif
Andrew Geissler95ac1b82021-03-31 14:34:31 -050056+/* Define to warn for use of native system header directories */
57+#ifndef USED_FOR_TARGET
58+#undef POISON_BY_DEFAULT
59+#endif
Andrew Geissler82c905d2020-04-13 13:39:40 -050060+
61+
62 /* Define if you want all operations on RTL (the basic data structure of the
63 optimizer and back end) to be checked for dynamic type safety at runtime.
64 This is quite expensive. */
65diff --git a/gcc/configure b/gcc/configure
Andrew Geissler95ac1b82021-03-31 14:34:31 -050066index 2a9d646b4..a848792f2 100755
Andrew Geissler82c905d2020-04-13 13:39:40 -050067--- a/gcc/configure
68+++ b/gcc/configure
Andrew Geisslerc182c622020-05-15 14:13:32 -050069@@ -1010,6 +1010,7 @@ with_system_zlib
Andrew Geissler82c905d2020-04-13 13:39:40 -050070 enable_maintainer_mode
71 enable_link_mutex
72 enable_version_specific_runtime_libs
73+enable_poison_system_directories
74 enable_plugin
75 enable_host_shared
76 enable_libquadmath_support
Andrew Geisslerc182c622020-05-15 14:13:32 -050077@@ -1766,6 +1767,8 @@ Optional Features:
Andrew Geissler82c905d2020-04-13 13:39:40 -050078 --enable-version-specific-runtime-libs
79 specify that runtime libraries should be installed
80 in a compiler-specific directory
81+ --enable-poison-system-directories
82+ warn for use of native system header directories
83 --enable-plugin enable plugin support
84 --enable-host-shared build host code as shared libraries
85 --disable-libquadmath-support
Andrew Geissler95ac1b82021-03-31 14:34:31 -050086@@ -30280,6 +30283,22 @@ if test "${enable_version_specific_runtime_libs+set}" = set; then :
Andrew Geissler82c905d2020-04-13 13:39:40 -050087 fi
88
89
90+# Check whether --enable-poison-system-directories was given.
91+if test "${enable_poison_system_directories+set}" = set; then :
92+ enableval=$enable_poison_system_directories;
93+else
94+ enable_poison_system_directories=no
95+fi
96+
Andrew Geissler95ac1b82021-03-31 14:34:31 -050097+if test "x${enable_poison_system_directories}" != "xno"; then
Andrew Geissler82c905d2020-04-13 13:39:40 -050098+
99+$as_echo "#define ENABLE_POISON_SYSTEM_DIRECTORIES 1" >>confdefs.h
Andrew Geissler95ac1b82021-03-31 14:34:31 -0500100+if test "$enable_poison_system_directories" = "error"; then
101+$as_echo "#define POISON_BY_DEFAULT 1" >>confdefs.h
102+fi
Andrew Geissler82c905d2020-04-13 13:39:40 -0500103+
104+fi
105+
106 # Substitute configuration variables
107
108
109diff --git a/gcc/configure.ac b/gcc/configure.ac
Andrew Geissler95ac1b82021-03-31 14:34:31 -0500110index 51cce36ce..66ffde305 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -0500111--- a/gcc/configure.ac
112+++ b/gcc/configure.ac
Andrew Geissler95ac1b82021-03-31 14:34:31 -0500113@@ -6614,6 +6614,22 @@ AC_ARG_ENABLE(version-specific-runtime-libs,
Andrew Geissler82c905d2020-04-13 13:39:40 -0500114 [specify that runtime libraries should be
115 installed in a compiler-specific directory])])
116
117+AC_ARG_ENABLE([poison-system-directories],
118+ AS_HELP_STRING([--enable-poison-system-directories],
Andrew Geissler95ac1b82021-03-31 14:34:31 -0500119+ [warn for use of native system header directories (no/yes/error)]),,
Andrew Geissler82c905d2020-04-13 13:39:40 -0500120+ [enable_poison_system_directories=no])
Andrew Geissler95ac1b82021-03-31 14:34:31 -0500121+AC_MSG_NOTICE([poisoned directories $enable_poison_system_directories])
122+if test "x${enable_poison_system_directories}" != "xno"; then
123+ AC_MSG_NOTICE([poisoned directories enabled])
Andrew Geissler82c905d2020-04-13 13:39:40 -0500124+ AC_DEFINE([ENABLE_POISON_SYSTEM_DIRECTORIES],
125+ [1],
126+ [Define to warn for use of native system header directories])
Andrew Geissler95ac1b82021-03-31 14:34:31 -0500127+ if test $enable_poison_system_directories = "error"; then
128+ AC_MSG_NOTICE([poisoned directories are fatal])
129+ AC_DEFINE([POISON_BY_DEFAULT], [1], [Define to make poison warnings errors])
130+ fi
Andrew Geissler82c905d2020-04-13 13:39:40 -0500131+fi
132+
133 # Substitute configuration variables
134 AC_SUBST(subdirs)
135 AC_SUBST(srcdir)
136diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
Andrew Geissler95ac1b82021-03-31 14:34:31 -0500137index d929eb109..aa5ff88b1 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -0500138--- a/gcc/doc/invoke.texi
139+++ b/gcc/doc/invoke.texi
Andrew Geissler95ac1b82021-03-31 14:34:31 -0500140@@ -351,6 +351,7 @@ Objective-C and Objective-C++ Dialects}.
Andrew Geisslerc182c622020-05-15 14:13:32 -0500141 -Wpacked -Wno-packed-bitfield-compat -Wpacked-not-aligned -Wpadded @gol
Andrew Geissler82c905d2020-04-13 13:39:40 -0500142 -Wparentheses -Wno-pedantic-ms-format @gol
Andrew Geisslerc182c622020-05-15 14:13:32 -0500143 -Wpointer-arith -Wno-pointer-compare -Wno-pointer-to-int-cast @gol
Andrew Geissler82c905d2020-04-13 13:39:40 -0500144+-Wno-poison-system-directories @gol
Andrew Geissler82c905d2020-04-13 13:39:40 -0500145 -Wno-pragmas -Wno-prio-ctor-dtor -Wredundant-decls @gol
Andrew Geisslerc182c622020-05-15 14:13:32 -0500146 -Wrestrict -Wno-return-local-addr -Wreturn-type @gol
147 -Wno-scalar-storage-order -Wsequence-point @gol
Andrew Geissler95ac1b82021-03-31 14:34:31 -0500148@@ -6928,6 +6929,14 @@ made up of data only and thus requires no special treatment. But, for
Andrew Geissler82c905d2020-04-13 13:39:40 -0500149 most targets, it is made up of code and thus requires the stack to be
150 made executable in order for the program to work properly.
151
152+@item -Wno-poison-system-directories
153+@opindex Wno-poison-system-directories
154+Do not warn for @option{-I} or @option{-L} options using system
155+directories such as @file{/usr/include} when cross compiling. This
156+option is intended for use in chroot environments when such
157+directories contain the correct headers and libraries for the target
158+system rather than the host.
159+
160 @item -Wfloat-equal
161 @opindex Wfloat-equal
162 @opindex Wno-float-equal
163diff --git a/gcc/gcc.c b/gcc/gcc.c
Andrew Geissler95ac1b82021-03-31 14:34:31 -0500164index 49c9c6c17..24a92bf27 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -0500165--- a/gcc/gcc.c
166+++ b/gcc/gcc.c
Andrew Geissler95ac1b82021-03-31 14:34:31 -0500167@@ -1044,6 +1044,8 @@ proper position among the other output files. */
Andrew Geissler82c905d2020-04-13 13:39:40 -0500168 "%{fuse-ld=*:-fuse-ld=%*} " LINK_COMPRESS_DEBUG_SPEC \
169 "%X %{o*} %{e*} %{N} %{n} %{r}\
170 %{s} %{t} %{u*} %{z} %{Z} %{!nostdlib:%{!r:%{!nostartfiles:%S}}} \
171+ %{Wno-poison-system-directories:--no-poison-system-directories} \
172+ %{Werror=poison-system-directories:--error-poison-system-directories} \
173 %{static|no-pie|static-pie:} %@{L*} %(mfwrap) %(link_libgcc) " \
174 VTABLE_VERIFICATION_SPEC " " SANITIZER_EARLY_SPEC " %o "" \
175 %{fopenacc|fopenmp|%:gt(%{ftree-parallelize-loops=*:%*} 1):\
Andrew Geissler95ac1b82021-03-31 14:34:31 -0500176@@ -1138,8 +1140,11 @@ static const char *cpp_unique_options =
177 static const char *cpp_options =
178 "%(cpp_unique_options) %1 %{m*} %{std*&ansi&trigraphs} %{W*&pedantic*} %{w}\
179 %{f*} %{g*:%{%:debug-level-gt(0):%{g*}\
180- %{!fno-working-directory:-fworking-directory}}} %{O*}\
181- %{undef} %{save-temps*:-fpch-preprocess}";
182+ %{!fno-working-directory:-fworking-directory}}} %{O*}"
183+#ifdef POISON_BY_DEFAULT
184+ " -Werror=poison-system-directories"
185+#endif
186+ " %{undef} %{save-temps*:-fpch-preprocess}";
187
188 /* This contains cpp options which are not passed when the preprocessor
189 output will be used by another program. */
Andrew Geissler82c905d2020-04-13 13:39:40 -0500190diff --git a/gcc/incpath.c b/gcc/incpath.c
Andrew Geissler95ac1b82021-03-31 14:34:31 -0500191index 94eaba7b1..bfad4ebe3 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -0500192--- a/gcc/incpath.c
193+++ b/gcc/incpath.c
194@@ -26,6 +26,7 @@
195 #include "intl.h"
196 #include "incpath.h"
197 #include "cppdefault.h"
198+#include "diagnostic-core.h"
199
200 /* Microsoft Windows does not natively support inodes.
201 VMS has non-numeric inodes. */
202@@ -393,6 +394,26 @@ merge_include_chains (const char *sysroot, cpp_reader *pfile, int verbose)
203 }
204 fprintf (stderr, _("End of search list.\n"));
205 }
206+
207+#ifdef ENABLE_POISON_SYSTEM_DIRECTORIES
208+ if (flag_poison_system_directories)
209+ {
210+ struct cpp_dir *p;
211+
212+ for (p = heads[INC_QUOTE]; p; p = p->next)
213+ {
214+ if ((!strncmp (p->name, "/usr/include", 12))
215+ || (!strncmp (p->name, "/usr/local/include", 18))
216+ || (!strncmp (p->name, "/usr/X11R6/include", 18))
217+ || (!strncmp (p->name, "/sw/include", 11))
218+ || (!strncmp (p->name, "/opt/include", 12)))
219+ warning (OPT_Wpoison_system_directories,
220+ "include location \"%s\" is unsafe for "
221+ "cross-compilation",
222+ p->name);
223+ }
224+ }
225+#endif
226 }
227
228 /* Use given -I paths for #include "..." but not #include <...>, and
Andrew Geissler95ac1b82021-03-31 14:34:31 -0500229--
2302.25.1
231