1From 5f62ad7ce534e3384d6ed8892614979da297bd70 Mon Sep 17 00:00:00 2001
2From: Evgeniy Didin <Evgeniy.Didin@synopsys.com>
3Date: Mon, 14 Oct 2019 16:45:15 +0300
4Subject: [PATCH] [PATCH] poison-system-directories
5
6Patch adapted to arc-binutils-gdb-2019.09
7Signed-off-by: Evgeniy Didin <Evgeniy.Didin@synopsys.com>
8
9Patch adapted to binutils 2.23.2 and extended to use
10BR_COMPILER_PARANOID_UNSAFE_PATH by Thomas Petazzoni.
11
12[Romain: rebase on top of 2.26]
13Signed-off-by: Romain Naour <romain.naour@gmail.com>
14[Gustavo: adapt to binutils 2.25]
15Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
16Signed-off-by: Gustavo Zacarias <gustavo@zacarias.com.ar>
17
18Upstream-Status: Inappropriate [distribution: codesourcery]
19
20Patch originally created by Mark Hatle, forward-ported to
21binutils 2.21 by Scott Garman.
22
23purpose:  warn for uses of system directories when cross linking
24
25Code Merged from Sourcery G++ binutils 2.19 - 4.4-277
26
272008-07-02  Joseph Myers  <joseph@codesourcery.com>
28
29    ld/
30    * ld.h (args_type): Add error_poison_system_directories.
31    * ld.texinfo (--error-poison-system-directories): Document.
32    * ldfile.c (ldfile_add_library_path): Check
33    command_line.error_poison_system_directories.
34    * ldmain.c (main): Initialize
35    command_line.error_poison_system_directories.
36    * lexsup.c (enum option_values): Add
37    OPTION_ERROR_POISON_SYSTEM_DIRECTORIES.
38    (ld_options): Add --error-poison-system-directories.
39    (parse_args): Handle new option.
40
412007-06-13  Joseph Myers  <joseph@codesourcery.com>
42
43    ld/
44    * config.in: Regenerate.
45    * ld.h (args_type): Add poison_system_directories.
46    * ld.texinfo (--no-poison-system-directories): Document.
47    * ldfile.c (ldfile_add_library_path): Check
48    command_line.poison_system_directories.
49    * ldmain.c (main): Initialize
50    command_line.poison_system_directories.
51    * lexsup.c (enum option_values): Add
52    OPTION_NO_POISON_SYSTEM_DIRECTORIES.
53    (ld_options): Add --no-poison-system-directories.
54    (parse_args): Handle new option.
55
562007-04-20  Joseph Myers  <joseph@codesourcery.com>
57
58    Merge from Sourcery G++ binutils 2.17:
59
60    2007-03-20  Joseph Myers  <joseph@codesourcery.com>
61    Based on patch by Mark Hatle <mark.hatle@windriver.com>.
62    ld/
63    * configure.ac (--enable-poison-system-directories): New option.
64    * configure, config.in: Regenerate.
65    * ldfile.c (ldfile_add_library_path): If
66    ENABLE_POISON_SYSTEM_DIRECTORIES defined, warn for use of /lib,
67    /usr/lib, /usr/local/lib or /usr/X11R6/lib.
68
69Signed-off-by: Mark Hatle <mark.hatle@windriver.com>
70Signed-off-by: Scott Garman <scott.a.garman@intel.com>
71---
72 ld/config.in    |  3 +++
73 ld/configure    | 14 ++++++++++++++
74 ld/configure.ac | 10 ++++++++++
75 ld/ld.h         |  8 ++++++++
76 ld/ld.texi      | 12 ++++++++++++
77 ld/ldfile.c     | 17 +++++++++++++++++
78 ld/ldlex.h      |  2 ++
79 ld/ldmain.c     |  2 ++
80 ld/lexsup.c     | 21 +++++++++++++++++++++
81 9 files changed, 89 insertions(+)
82
83diff --git a/ld/config.in b/ld/config.in
84index d93c9b08300..5da2742beac 100644
85--- a/ld/config.in
86+++ b/ld/config.in
87@@ -31,6 +31,9 @@
88    language is requested. */
89 #undef ENABLE_NLS
90
91+/* Define to warn for use of native system library directories */
92+#undef ENABLE_POISON_SYSTEM_DIRECTORIES
93+
94 /* Additional extension a shared object might have. */
95 #undef EXTRA_SHLIB_EXTENSION
96
97diff --git a/ld/configure b/ld/configure
98index 2d6ca5c0445..563f9921f7f 100755
99--- a/ld/configure
100+++ b/ld/configure
101@@ -823,6 +823,7 @@ with_lib_path
102 enable_targets
103 enable_64_bit_bfd
104 with_sysroot
105+enable_poison_system_directories
106 enable_gold
107 enable_got
108 enable_compressed_debug_sections
109@@ -1487,6 +1488,8 @@ Optional Features:
110   --disable-largefile     omit support for large files
111   --enable-targets        alternative target configurations
112   --enable-64-bit-bfd     64-bit support (on hosts with narrower word sizes)
113+  --enable-poison-system-directories
114+                          warn for use of native system library directories
115   --enable-gold[=ARG]     build gold [ARG={default,yes,no}]
116   --enable-got=<type>     GOT handling scheme (target, single, negative,
117                           multigot)
118@@ -15804,7 +15807,18 @@ else
119 fi
120
121
122+# Check whether --enable-poison-system-directories was given.
123+if test "${enable_poison_system_directories+set}" = set; then :
124+  enableval=$enable_poison_system_directories;
125+else
126+  enable_poison_system_directories=no
127+fi
128+
129+if test "x${enable_poison_system_directories}" = "xyes"; then
130
131+$as_echo "#define ENABLE_POISON_SYSTEM_DIRECTORIES 1" >>confdefs.h
132+
133+fi
134
135 # Check whether --enable-got was given.
136 if test "${enable_got+set}" = set; then :
137diff --git a/ld/configure.ac b/ld/configure.ac
138index 41a51bbb7e9..dbaa98a9e17 100644
139--- a/ld/configure.ac
140+++ b/ld/configure.ac
141@@ -94,6 +94,16 @@ AC_SUBST(use_sysroot)
142 AC_SUBST(TARGET_SYSTEM_ROOT)
143 AC_SUBST(TARGET_SYSTEM_ROOT_DEFINE)
144
145+AC_ARG_ENABLE([poison-system-directories],
146+         AS_HELP_STRING([--enable-poison-system-directories],
147+                [warn for use of native system library directories]),,
148+         [enable_poison_system_directories=no])
149+if test "x${enable_poison_system_directories}" = "xyes"; then
150+  AC_DEFINE([ENABLE_POISON_SYSTEM_DIRECTORIES],
151+       [1],
152+       [Define to warn for use of native system library directories])
153+fi
154+
155 dnl Use --enable-gold to decide if this linker should be the default.
156 dnl "install_as_default" is set to false if gold is the default linker.
157 dnl "installed_linker" is the installed BFD linker name.
158diff --git a/ld/ld.h b/ld/ld.h
159index 55078a9637b..511e9bc34b7 100644
160--- a/ld/ld.h
161+++ b/ld/ld.h
162@@ -180,6 +180,14 @@ typedef struct
163      in the linker script.  */
164   bfd_boolean force_group_allocation;
165
166+  /* If TRUE (the default) warn for uses of system directories when
167+     cross linking.  */
168+  bfd_boolean poison_system_directories;
169+
170+  /* If TRUE (default FALSE) give an error for uses of system
171+     directories when cross linking instead of a warning.  */
172+  bfd_boolean error_poison_system_directories;
173+
174   /* Big or little endian as set on command line.  */
175   enum endian_enum endian;
176
177diff --git a/ld/ld.texi b/ld/ld.texi
178index fcbc335c95e..6ba7ebdb32a 100644
179--- a/ld/ld.texi
180+++ b/ld/ld.texi
181@@ -2557,6 +2557,18 @@ string identifying the original linked file does not change.
182
183 Passing @code{none} for @var{style} disables the setting from any
184 @code{--build-id} options earlier on the command line.
185+
186+@kindex --no-poison-system-directories
187+@item --no-poison-system-directories
188+Do not warn for @option{-L} options using system directories such as
189+@file{/usr/lib} when cross linking.  This option is intended for use
190+in chroot environments when such directories contain the correct
191+libraries for the target system rather than the host.
192+
193+@kindex --error-poison-system-directories
194+@item --error-poison-system-directories
195+Give an error instead of a warning for @option{-L} options using
196+system directories when cross linking.
197 @end table
198
199 @c man end
200diff --git a/ld/ldfile.c b/ld/ldfile.c
201index 7f60319390e..0bcc06db964 100644
202--- a/ld/ldfile.c
203+++ b/ld/ldfile.c
204@@ -116,6 +116,23 @@ ldfile_add_library_path (const char *name, bfd_boolean cmdline)
205     new_dirs->name = concat (ld_sysroot, name + strlen ("$SYSROOT"), (const char *) NULL);
206   else
207     new_dirs->name = xstrdup (name);
208+
209+#ifdef ENABLE_POISON_SYSTEM_DIRECTORIES
210+  if (command_line.poison_system_directories
211+  && ((!strncmp (name, "/lib", 4))
212+      || (!strncmp (name, "/usr/lib", 8))
213+      || (!strncmp (name, "/usr/local/lib", 14))
214+      || (!strncmp (name, "/usr/X11R6/lib", 14))))
215+   {
216+     if (command_line.error_poison_system_directories)
217+       einfo (_("%X%P: error: library search path \"%s\" is unsafe for "
218+            "cross-compilation\n"), name);
219+     else
220+       einfo (_("%P: warning: library search path \"%s\" is unsafe for "
221+            "cross-compilation\n"), name);
222+   }
223+#endif
224+
225 }
226
227 /* Try to open a BFD for a lang_input_statement.  */
228diff --git a/ld/ldlex.h b/ld/ldlex.h
229index 32a7a6409e8..c02b64bf92f 100644
230--- a/ld/ldlex.h
231+++ b/ld/ldlex.h
232@@ -152,6 +152,8 @@ enum option_values
233   OPTION_NO_PRINT_MAP_DISCARDED,
234   OPTION_NON_CONTIGUOUS_REGIONS,
235   OPTION_NON_CONTIGUOUS_REGIONS_WARNINGS,
236+  OPTION_NO_POISON_SYSTEM_DIRECTORIES,
237+  OPTION_ERROR_POISON_SYSTEM_DIRECTORIES,
238 };
239
240
241 /* The initial parser states.  */
242diff --git a/ld/ldmain.c b/ld/ldmain.c
243index 34c19223137..66d2c3f4bcf 100644
244--- a/ld/ldmain.c
245+++ b/ld/ldmain.c
246@@ -270,6 +270,8 @@ main (int argc, char **argv)
247   command_line.warn_mismatch = TRUE;
248   command_line.warn_search_mismatch = TRUE;
249   command_line.check_section_addresses = -1;
250+  command_line.poison_system_directories = TRUE;
251+  command_line.error_poison_system_directories = FALSE;
252
253   /* We initialize DEMANGLING based on the environment variable
254      COLLECT_NO_DEMANGLE.  The gcc collect2 program will demangle the
255diff --git a/ld/lexsup.c b/ld/lexsup.c
256index 1c15ac29c0c..8b714e10a40 100644
257--- a/ld/lexsup.c
258+++ b/ld/lexsup.c
259@@ -549,6 +549,14 @@ static const struct ld_option ld_options[] =
260   { {"no-print-map-discarded", no_argument, NULL, OPTION_NO_PRINT_MAP_DISCARDED},
261     '\0', NULL, N_("Do not show discarded sections in map file output"),
262     TWO_DASHES },
263+  { {"no-poison-system-directories", no_argument, NULL,
264+    OPTION_NO_POISON_SYSTEM_DIRECTORIES},
265+    '\0', NULL, N_("Do not warn for -L options using system directories"),
266+    TWO_DASHES },
267+  { {"error-poison-system-directories", no_argument, NULL,
268+    OPTION_ERROR_POISON_SYSTEM_DIRECTORIES},
269+    '\0', NULL, N_("Give an error for -L options using system directories"),
270+    TWO_DASHES },
271 };
272
273 #define OPTION_COUNT ARRAY_SIZE (ld_options)
274@@ -561,6 +569,7 @@ parse_args (unsigned argc, char **argv)
275   int ingroup = 0;
276   char *default_dirlist = NULL;
277   char *shortopts;
278+  char *BR_paranoid_env;
279   struct option *longopts;
280   struct option *really_longopts;
281   int last_optind;
282@@ -1549,6 +1558,14 @@ parse_args (unsigned argc, char **argv)
283 	  }
284 	  break;
285
286+	case OPTION_NO_POISON_SYSTEM_DIRECTORIES:
287+	  command_line.poison_system_directories = FALSE;
288+	  break;
289+
290+	case OPTION_ERROR_POISON_SYSTEM_DIRECTORIES:
291+	  command_line.error_poison_system_directories = TRUE;
292+	  break;
293+
294 	case OPTION_PUSH_STATE:
295 	  input_flags.pushed = xmemdup (&input_flags,
296 					sizeof (input_flags),
297@@ -1600,6 +1617,10 @@ parse_args (unsigned argc, char **argv)
298       command_line.soname = NULL;
299     }
300
301+  BR_paranoid_env = getenv("BR_COMPILER_PARANOID_UNSAFE_PATH");
302+  if (BR_paranoid_env && strlen(BR_paranoid_env) > 0)
303+    command_line.error_poison_system_directories = TRUE;
304+
305   while (ingroup)
306     {
307       einfo (_("%P: missing --end-group; added as last command line option\n"));
308--
3092.16.2
310
311