0002-gcc-poison-system-directories.patch 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. From e1dbdcd0ea667bab4b551294354e04c6fe288ab6 Mon Sep 17 00:00:00 2001
  2. From: Khem Raj <raj.khem@gmail.com>
  3. Date: Mon, 8 Mar 2021 16:04:20 -0800
  4. Subject: [PATCH] gcc: poison-system-directories
  5. Add /sw/include and /opt/include based on the original
  6. zecke-no-host-includes.patch patch. The original patch checked for
  7. /usr/include, /sw/include and /opt/include and then triggered a failure and
  8. aborted.
  9. Instead, we add the two missing items to the current scan. If the user
  10. wants this to be a failure, they can add "-Werror=poison-system-directories".
  11. Upstream-Status: Pending
  12. Signed-off-by: Mark Hatle <mark.hatle@windriver.com>
  13. Signed-off-by: Khem Raj <raj.khem@gmail.com>
  14. ---
  15. gcc/common.opt | 4 ++++
  16. gcc/config.in | 10 ++++++++++
  17. gcc/configure | 19 +++++++++++++++++++
  18. gcc/configure.ac | 16 ++++++++++++++++
  19. gcc/doc/invoke.texi | 9 +++++++++
  20. gcc/gcc.cc | 9 +++++++--
  21. gcc/incpath.cc | 21 +++++++++++++++++++++
  22. 7 files changed, 86 insertions(+), 2 deletions(-)
  23. diff --git a/gcc/common.opt b/gcc/common.opt
  24. index 8a0dafc522d..0357868e22c 100644
  25. --- a/gcc/common.opt
  26. +++ b/gcc/common.opt
  27. @@ -710,6 +710,10 @@ Wreturn-local-addr
  28. Common Var(warn_return_local_addr) Init(1) Warning
  29. Warn about returning a pointer/reference to a local or temporary variable.
  30. +Wpoison-system-directories
  31. +Common Var(flag_poison_system_directories) Init(1) Warning
  32. +Warn for -I and -L options using system directories if cross compiling
  33. +
  34. Wshadow
  35. Common Var(warn_shadow) Warning
  36. Warn when one variable shadows another. Same as -Wshadow=global.
  37. diff --git a/gcc/config.in b/gcc/config.in
  38. index 64c27c9cfac..a693cb8a886 100644
  39. --- a/gcc/config.in
  40. +++ b/gcc/config.in
  41. @@ -230,6 +230,16 @@
  42. #endif
  43. +/* Define to warn for use of native system header directories */
  44. +#ifndef USED_FOR_TARGET
  45. +#undef ENABLE_POISON_SYSTEM_DIRECTORIES
  46. +#endif
  47. +/* Define to warn for use of native system header directories */
  48. +#ifndef USED_FOR_TARGET
  49. +#undef POISON_BY_DEFAULT
  50. +#endif
  51. +
  52. +
  53. /* Define if you want all operations on RTL (the basic data structure of the
  54. optimizer and back end) to be checked for dynamic type safety at runtime.
  55. This is quite expensive. */
  56. diff --git a/gcc/configure b/gcc/configure
  57. index 5ce0557719a..dc2d59701ad 100755
  58. --- a/gcc/configure
  59. +++ b/gcc/configure
  60. @@ -1023,6 +1023,7 @@ enable_maintainer_mode
  61. enable_link_mutex
  62. enable_link_serialization
  63. enable_version_specific_runtime_libs
  64. +enable_poison_system_directories
  65. enable_plugin
  66. enable_host_shared
  67. enable_libquadmath_support
  68. @@ -1785,6 +1786,8 @@ Optional Features:
  69. --enable-version-specific-runtime-libs
  70. specify that runtime libraries should be installed
  71. in a compiler-specific directory
  72. + --enable-poison-system-directories
  73. + warn for use of native system header directories
  74. --enable-plugin enable plugin support
  75. --enable-host-shared build host code as shared libraries
  76. --disable-libquadmath-support
  77. @@ -31982,6 +31985,22 @@ if test "${enable_version_specific_runtime_libs+set}" = set; then :
  78. fi
  79. +# Check whether --enable-poison-system-directories was given.
  80. +if test "${enable_poison_system_directories+set}" = set; then :
  81. + enableval=$enable_poison_system_directories;
  82. +else
  83. + enable_poison_system_directories=no
  84. +fi
  85. +
  86. +if test "x${enable_poison_system_directories}" != "xno"; then
  87. +
  88. +$as_echo "#define ENABLE_POISON_SYSTEM_DIRECTORIES 1" >>confdefs.h
  89. +if test "$enable_poison_system_directories" = "error"; then
  90. +$as_echo "#define POISON_BY_DEFAULT 1" >>confdefs.h
  91. +fi
  92. +
  93. +fi
  94. +
  95. # Substitute configuration variables
  96. diff --git a/gcc/configure.ac b/gcc/configure.ac
  97. index 23bee7010a3..36ce78924de 100644
  98. --- a/gcc/configure.ac
  99. +++ b/gcc/configure.ac
  100. @@ -7421,6 +7421,22 @@ AC_ARG_ENABLE(version-specific-runtime-libs,
  101. [specify that runtime libraries should be
  102. installed in a compiler-specific directory])])
  103. +AC_ARG_ENABLE([poison-system-directories],
  104. + AS_HELP_STRING([--enable-poison-system-directories],
  105. + [warn for use of native system header directories (no/yes/error)]),,
  106. + [enable_poison_system_directories=no])
  107. +AC_MSG_NOTICE([poisoned directories $enable_poison_system_directories])
  108. +if test "x${enable_poison_system_directories}" != "xno"; then
  109. + AC_MSG_NOTICE([poisoned directories enabled])
  110. + AC_DEFINE([ENABLE_POISON_SYSTEM_DIRECTORIES],
  111. + [1],
  112. + [Define to warn for use of native system header directories])
  113. + if test $enable_poison_system_directories = "error"; then
  114. + AC_MSG_NOTICE([poisoned directories are fatal])
  115. + AC_DEFINE([POISON_BY_DEFAULT], [1], [Define to make poison warnings errors])
  116. + fi
  117. +fi
  118. +
  119. # Substitute configuration variables
  120. AC_SUBST(subdirs)
  121. AC_SUBST(srcdir)
  122. diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
  123. index 07b440190c3..b2de464798a 100644
  124. --- a/gcc/doc/invoke.texi
  125. +++ b/gcc/doc/invoke.texi
  126. @@ -379,6 +379,7 @@ Objective-C and Objective-C++ Dialects}.
  127. -Wpacked -Wno-packed-bitfield-compat -Wpacked-not-aligned -Wpadded @gol
  128. -Wparentheses -Wno-pedantic-ms-format @gol
  129. -Wpointer-arith -Wno-pointer-compare -Wno-pointer-to-int-cast @gol
  130. +-Wno-poison-system-directories @gol
  131. -Wno-pragmas -Wno-prio-ctor-dtor -Wredundant-decls @gol
  132. -Wrestrict -Wno-return-local-addr -Wreturn-type @gol
  133. -Wno-scalar-storage-order -Wsequence-point @gol
  134. @@ -8029,6 +8030,14 @@ made up of data only and thus requires no special treatment. But, for
  135. most targets, it is made up of code and thus requires the stack to be
  136. made executable in order for the program to work properly.
  137. +@item -Wno-poison-system-directories
  138. +@opindex Wno-poison-system-directories
  139. +Do not warn for @option{-I} or @option{-L} options using system
  140. +directories such as @file{/usr/include} when cross compiling. This
  141. +option is intended for use in chroot environments when such
  142. +directories contain the correct headers and libraries for the target
  143. +system rather than the host.
  144. +
  145. @item -Wfloat-equal
  146. @opindex Wfloat-equal
  147. @opindex Wno-float-equal
  148. diff --git a/gcc/gcc.cc b/gcc/gcc.cc
  149. index bb07cc244e3..ce161d3c853 100644
  150. --- a/gcc/gcc.cc
  151. +++ b/gcc/gcc.cc
  152. @@ -1159,6 +1159,8 @@ proper position among the other output files. */
  153. "%{fuse-ld=*:-fuse-ld=%*} " LINK_COMPRESS_DEBUG_SPEC \
  154. "%X %{o*} %{e*} %{N} %{n} %{r}\
  155. %{s} %{t} %{u*} %{z} %{Z} %{!nostdlib:%{!r:%{!nostartfiles:%S}}} \
  156. + %{Wno-poison-system-directories:--no-poison-system-directories} \
  157. + %{Werror=poison-system-directories:--error-poison-system-directories} \
  158. %{static|no-pie|static-pie:} %@{L*} %(mfwrap) %(link_libgcc) " \
  159. VTABLE_VERIFICATION_SPEC " " SANITIZER_EARLY_SPEC " %o "" \
  160. %{fopenacc|fopenmp|%:gt(%{ftree-parallelize-loops=*:%*} 1):\
  161. @@ -1254,8 +1256,11 @@ static const char *cpp_unique_options =
  162. static const char *cpp_options =
  163. "%(cpp_unique_options) %1 %{m*} %{std*&ansi&trigraphs} %{W*&pedantic*} %{w}\
  164. %{f*} %{g*:%{%:debug-level-gt(0):%{g*}\
  165. - %{!fno-working-directory:-fworking-directory}}} %{O*}\
  166. - %{undef} %{save-temps*:-fpch-preprocess}";
  167. + %{!fno-working-directory:-fworking-directory}}} %{O*}"
  168. +#ifdef POISON_BY_DEFAULT
  169. + " -Werror=poison-system-directories"
  170. +#endif
  171. + " %{undef} %{save-temps*:-fpch-preprocess}";
  172. /* Pass -d* flags, possibly modifying -dumpdir, -dumpbase et al.
  173. diff --git a/gcc/incpath.cc b/gcc/incpath.cc
  174. index bd2a97938eb..c80f100f476 100644
  175. --- a/gcc/incpath.cc
  176. +++ b/gcc/incpath.cc
  177. @@ -26,6 +26,7 @@
  178. #include "intl.h"
  179. #include "incpath.h"
  180. #include "cppdefault.h"
  181. +#include "diagnostic-core.h"
  182. /* Microsoft Windows does not natively support inodes.
  183. VMS has non-numeric inodes. */
  184. @@ -399,6 +400,26 @@ merge_include_chains (const char *sysroot, cpp_reader *pfile, int verbose)
  185. }
  186. fprintf (stderr, _("End of search list.\n"));
  187. }
  188. +
  189. +#ifdef ENABLE_POISON_SYSTEM_DIRECTORIES
  190. + if (flag_poison_system_directories)
  191. + {
  192. + struct cpp_dir *p;
  193. +
  194. + for (p = heads[INC_QUOTE]; p; p = p->next)
  195. + {
  196. + if ((!strncmp (p->name, "/usr/include", 12))
  197. + || (!strncmp (p->name, "/usr/local/include", 18))
  198. + || (!strncmp (p->name, "/usr/X11R6/include", 18))
  199. + || (!strncmp (p->name, "/sw/include", 11))
  200. + || (!strncmp (p->name, "/opt/include", 12)))
  201. + warning (OPT_Wpoison_system_directories,
  202. + "include location \"%s\" is unsafe for "
  203. + "cross-compilation",
  204. + p->name);
  205. + }
  206. + }
  207. +#endif
  208. }
  209. /* Use given -I paths for #include "..." but not #include <...>, and