1 From e02e119b278c3f404e97669e7180cac944134c91 Mon Sep 17 00:00:00 2001
2 From: Khem Raj <raj.khem@gmail.com>
3 Date: Mon, 2 Mar 2015 01:41:04 +0000
4 Subject: [PATCH 09/13] Upstream-Status: Inappropriate [distribution:
7 Patch originally created by Mark Hatle, forward-ported to
8 binutils 2.21 by Scott Garman.
10 purpose: warn for uses of system directories when cross linking
12 Code Merged from Sourcery G++ binutils 2.19 - 4.4-277
14 2008-07-02 Joseph Myers <joseph@codesourcery.com>
17 * ld.h (args_type): Add error_poison_system_directories.
18 * ld.texinfo (--error-poison-system-directories): Document.
19 * ldfile.c (ldfile_add_library_path): Check
20 command_line.error_poison_system_directories.
21 * ldmain.c (main): Initialize
22 command_line.error_poison_system_directories.
23 * lexsup.c (enum option_values): Add
24 OPTION_ERROR_POISON_SYSTEM_DIRECTORIES.
25 (ld_options): Add --error-poison-system-directories.
26 (parse_args): Handle new option.
28 2007-06-13 Joseph Myers <joseph@codesourcery.com>
31 * config.in: Regenerate.
32 * ld.h (args_type): Add poison_system_directories.
33 * ld.texinfo (--no-poison-system-directories): Document.
34 * ldfile.c (ldfile_add_library_path): Check
35 command_line.poison_system_directories.
36 * ldmain.c (main): Initialize
37 command_line.poison_system_directories.
38 * lexsup.c (enum option_values): Add
39 OPTION_NO_POISON_SYSTEM_DIRECTORIES.
40 (ld_options): Add --no-poison-system-directories.
41 (parse_args): Handle new option.
43 2007-04-20 Joseph Myers <joseph@codesourcery.com>
45 Merge from Sourcery G++ binutils 2.17:
47 2007-03-20 Joseph Myers <joseph@codesourcery.com>
48 Based on patch by Mark Hatle <mark.hatle@windriver.com>.
50 * configure.in (--enable-poison-system-directories): New option.
51 * configure, config.in: Regenerate.
52 * ldfile.c (ldfile_add_library_path): If
53 ENABLE_POISON_SYSTEM_DIRECTORIES defined, warn for use of /lib,
54 /usr/lib, /usr/local/lib or /usr/X11R6/lib.
56 Signed-off-by: Mark Hatle <mark.hatle@windriver.com>
57 Signed-off-by: Scott Garman <scott.a.garman@intel.com>
58 Signed-off-by: Khem Raj <raj.khem@gmail.com>
61 ld/configure | 14 ++++++++++++++
62 ld/configure.ac | 10 ++++++++++
64 ld/ld.texinfo | 12 ++++++++++++
65 ld/ldfile.c | 17 +++++++++++++++++
68 ld/lexsup.c | 16 ++++++++++++++++
69 9 files changed, 84 insertions(+)
71 diff --git a/ld/config.in b/ld/config.in
72 index 2ab4844..766d23c 100644
76 language is requested. */
79 +/* Define to warn for use of native system library directories */
80 +#undef ENABLE_POISON_SYSTEM_DIRECTORIES
82 /* Additional extension a shared object might have. */
83 #undef EXTRA_SHLIB_EXTENSION
85 diff --git a/ld/configure b/ld/configure
86 index 2fbaebf..5726a08 100755
89 @@ -787,6 +787,7 @@ with_lib_path
93 +enable_poison_system_directories
97 @@ -1443,6 +1444,8 @@ Optional Features:
98 --disable-largefile omit support for large files
99 --enable-targets alternative target configurations
100 --enable-64-bit-bfd 64-bit support (on hosts with narrower word sizes)
101 + --enable-poison-system-directories
102 + warn for use of native system library directories
103 --enable-gold[=ARG] build gold [ARG={default,yes,no}]
104 --enable-got=<type> GOT handling scheme (target, single, negative,
106 @@ -16323,7 +16326,18 @@ else
110 +# Check whether --enable-poison-system-directories was given.
111 +if test "${enable_poison_system_directories+set}" = set; then :
112 + enableval=$enable_poison_system_directories;
114 + enable_poison_system_directories=no
117 +if test "x${enable_poison_system_directories}" = "xyes"; then
119 +$as_echo "#define ENABLE_POISON_SYSTEM_DIRECTORIES 1" >>confdefs.h
123 # Check whether --enable-got was given.
124 if test "${enable_got+set}" = set; then :
125 diff --git a/ld/configure.ac b/ld/configure.ac
126 index 1bddfc9..e9edb7f 100644
127 --- a/ld/configure.ac
128 +++ b/ld/configure.ac
129 @@ -94,6 +94,16 @@ AC_SUBST(use_sysroot)
130 AC_SUBST(TARGET_SYSTEM_ROOT)
131 AC_SUBST(TARGET_SYSTEM_ROOT_DEFINE)
133 +AC_ARG_ENABLE([poison-system-directories],
134 + AS_HELP_STRING([--enable-poison-system-directories],
135 + [warn for use of native system library directories]),,
136 + [enable_poison_system_directories=no])
137 +if test "x${enable_poison_system_directories}" = "xyes"; then
138 + AC_DEFINE([ENABLE_POISON_SYSTEM_DIRECTORIES],
140 + [Define to warn for use of native system library directories])
143 dnl Use --enable-gold to decide if this linker should be the default.
144 dnl "install_as_default" is set to false if gold is the default linker.
145 dnl "installed_linker" is the installed BFD linker name.
146 diff --git a/ld/ld.h b/ld/ld.h
147 index f773ce7..adba0f6 100644
150 @@ -161,6 +161,14 @@ typedef struct {
151 /* If TRUE we'll just print the default output on stdout. */
152 bfd_boolean print_output_format;
154 + /* If TRUE (the default) warn for uses of system directories when
156 + bfd_boolean poison_system_directories;
158 + /* If TRUE (default FALSE) give an error for uses of system
159 + directories when cross linking instead of a warning. */
160 + bfd_boolean error_poison_system_directories;
162 /* Big or little endian as set on command line. */
163 enum endian_enum endian;
165 diff --git a/ld/ld.texinfo b/ld/ld.texinfo
166 index 502582c..dae168a 100644
169 @@ -2212,6 +2212,18 @@ string identifying the original linked file does not change.
171 Passing @code{none} for @var{style} disables the setting from any
172 @code{--build-id} options earlier on the command line.
174 +@kindex --no-poison-system-directories
175 +@item --no-poison-system-directories
176 +Do not warn for @option{-L} options using system directories such as
177 +@file{/usr/lib} when cross linking. This option is intended for use
178 +in chroot environments when such directories contain the correct
179 +libraries for the target system rather than the host.
181 +@kindex --error-poison-system-directories
182 +@item --error-poison-system-directories
183 +Give an error instead of a warning for @option{-L} options using
184 +system directories when cross linking.
188 diff --git a/ld/ldfile.c b/ld/ldfile.c
189 index 782ed7f..19a9ab4 100644
192 @@ -114,6 +114,23 @@ ldfile_add_library_path (const char *name, bfd_boolean cmdline)
193 new_dirs->name = concat (ld_sysroot, name + 1, (const char *) NULL);
195 new_dirs->name = xstrdup (name);
197 +#ifdef ENABLE_POISON_SYSTEM_DIRECTORIES
198 + if (command_line.poison_system_directories
199 + && ((!strncmp (name, "/lib", 4))
200 + || (!strncmp (name, "/usr/lib", 8))
201 + || (!strncmp (name, "/usr/local/lib", 14))
202 + || (!strncmp (name, "/usr/X11R6/lib", 14))))
204 + if (command_line.error_poison_system_directories)
205 + einfo (_("%X%P: error: library search path \"%s\" is unsafe for "
206 + "cross-compilation\n"), name);
208 + einfo (_("%P: warning: library search path \"%s\" is unsafe for "
209 + "cross-compilation\n"), name);
215 /* Try to open a BFD for a lang_input_statement. */
216 diff --git a/ld/ldlex.h b/ld/ldlex.h
217 index e3e9b24..29487a3 100644
220 @@ -140,6 +140,8 @@ enum option_values
221 OPTION_IGNORE_UNRESOLVED_SYMBOL,
224 + OPTION_NO_POISON_SYSTEM_DIRECTORIES,
225 + OPTION_ERROR_POISON_SYSTEM_DIRECTORIES,
228 /* The initial parser states. */
229 diff --git a/ld/ldmain.c b/ld/ldmain.c
230 index 818d108..54c49f2 100644
233 @@ -266,6 +266,8 @@ main (int argc, char **argv)
234 command_line.warn_mismatch = TRUE;
235 command_line.warn_search_mismatch = TRUE;
236 command_line.check_section_addresses = -1;
237 + command_line.poison_system_directories = TRUE;
238 + command_line.error_poison_system_directories = FALSE;
240 /* We initialize DEMANGLING based on the environment variable
241 COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the
242 diff --git a/ld/lexsup.c b/ld/lexsup.c
243 index 4812c97..21b49df 100644
246 @@ -513,6 +513,14 @@ static const struct ld_option ld_options[] =
247 { {"pop-state", no_argument, NULL, OPTION_POP_STATE},
248 '\0', NULL, N_("Pop state of flags governing input file handling"),
250 + { {"no-poison-system-directories", no_argument, NULL,
251 + OPTION_NO_POISON_SYSTEM_DIRECTORIES},
252 + '\0', NULL, N_("Do not warn for -L options using system directories"),
254 + { {"error-poison-system-directories", no_argument, NULL,
255 + + OPTION_ERROR_POISON_SYSTEM_DIRECTORIES},
256 + '\0', NULL, N_("Give an error for -L options using system directories"),
260 #define OPTION_COUNT ARRAY_SIZE (ld_options)
261 @@ -1474,6 +1482,14 @@ parse_args (unsigned argc, char **argv)
266 + case OPTION_NO_POISON_SYSTEM_DIRECTORIES:
267 + command_line.poison_system_directories = FALSE;
270 + case OPTION_ERROR_POISON_SYSTEM_DIRECTORIES:
271 + command_line.error_poison_system_directories = TRUE;