Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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: |
| 5 | codesourcery] |
| 6 | |
| 7 | Patch originally created by Mark Hatle, forward-ported to |
| 8 | binutils 2.21 by Scott Garman. |
| 9 | |
| 10 | purpose: warn for uses of system directories when cross linking |
| 11 | |
| 12 | Code Merged from Sourcery G++ binutils 2.19 - 4.4-277 |
| 13 | |
| 14 | 2008-07-02 Joseph Myers <joseph@codesourcery.com> |
| 15 | |
| 16 | ld/ |
| 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. |
| 27 | |
| 28 | 2007-06-13 Joseph Myers <joseph@codesourcery.com> |
| 29 | |
| 30 | ld/ |
| 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. |
| 42 | |
| 43 | 2007-04-20 Joseph Myers <joseph@codesourcery.com> |
| 44 | |
| 45 | Merge from Sourcery G++ binutils 2.17: |
| 46 | |
| 47 | 2007-03-20 Joseph Myers <joseph@codesourcery.com> |
| 48 | Based on patch by Mark Hatle <mark.hatle@windriver.com>. |
| 49 | ld/ |
| 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. |
| 55 | |
| 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> |
| 59 | --- |
| 60 | ld/config.in | 3 +++ |
| 61 | ld/configure | 14 ++++++++++++++ |
| 62 | ld/configure.ac | 10 ++++++++++ |
| 63 | ld/ld.h | 8 ++++++++ |
| 64 | ld/ld.texinfo | 12 ++++++++++++ |
| 65 | ld/ldfile.c | 17 +++++++++++++++++ |
| 66 | ld/ldlex.h | 2 ++ |
| 67 | ld/ldmain.c | 2 ++ |
| 68 | ld/lexsup.c | 16 ++++++++++++++++ |
| 69 | 9 files changed, 84 insertions(+) |
| 70 | |
| 71 | diff --git a/ld/config.in b/ld/config.in |
| 72 | index 2ab4844..766d23c 100644 |
| 73 | --- a/ld/config.in |
| 74 | +++ b/ld/config.in |
| 75 | @@ -11,6 +11,9 @@ |
| 76 | language is requested. */ |
| 77 | #undef ENABLE_NLS |
| 78 | |
| 79 | +/* Define to warn for use of native system library directories */ |
| 80 | +#undef ENABLE_POISON_SYSTEM_DIRECTORIES |
| 81 | + |
| 82 | /* Additional extension a shared object might have. */ |
| 83 | #undef EXTRA_SHLIB_EXTENSION |
| 84 | |
| 85 | diff --git a/ld/configure b/ld/configure |
| 86 | index 2fbaebf..5726a08 100755 |
| 87 | --- a/ld/configure |
| 88 | +++ b/ld/configure |
| 89 | @@ -787,6 +787,7 @@ with_lib_path |
| 90 | enable_targets |
| 91 | enable_64_bit_bfd |
| 92 | with_sysroot |
| 93 | +enable_poison_system_directories |
| 94 | enable_gold |
| 95 | enable_got |
| 96 | enable_werror |
| 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, |
| 105 | multigot) |
| 106 | @@ -16323,7 +16326,18 @@ else |
| 107 | fi |
| 108 | |
| 109 | |
| 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; |
| 113 | +else |
| 114 | + enable_poison_system_directories=no |
| 115 | +fi |
| 116 | + |
| 117 | +if test "x${enable_poison_system_directories}" = "xyes"; then |
| 118 | |
| 119 | +$as_echo "#define ENABLE_POISON_SYSTEM_DIRECTORIES 1" >>confdefs.h |
| 120 | + |
| 121 | +fi |
| 122 | |
| 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) |
| 132 | |
| 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], |
| 139 | + [1], |
| 140 | + [Define to warn for use of native system library directories]) |
| 141 | +fi |
| 142 | + |
| 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 |
| 148 | --- a/ld/ld.h |
| 149 | +++ b/ld/ld.h |
| 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; |
| 153 | |
| 154 | + /* If TRUE (the default) warn for uses of system directories when |
| 155 | + cross linking. */ |
| 156 | + bfd_boolean poison_system_directories; |
| 157 | + |
| 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; |
| 161 | + |
| 162 | /* Big or little endian as set on command line. */ |
| 163 | enum endian_enum endian; |
| 164 | |
| 165 | diff --git a/ld/ld.texinfo b/ld/ld.texinfo |
| 166 | index 502582c..dae168a 100644 |
| 167 | --- a/ld/ld.texinfo |
| 168 | +++ b/ld/ld.texinfo |
| 169 | @@ -2212,6 +2212,18 @@ string identifying the original linked file does not change. |
| 170 | |
| 171 | Passing @code{none} for @var{style} disables the setting from any |
| 172 | @code{--build-id} options earlier on the command line. |
| 173 | + |
| 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. |
| 180 | + |
| 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. |
| 185 | @end table |
| 186 | |
| 187 | @c man end |
| 188 | diff --git a/ld/ldfile.c b/ld/ldfile.c |
| 189 | index 782ed7f..19a9ab4 100644 |
| 190 | --- a/ld/ldfile.c |
| 191 | +++ b/ld/ldfile.c |
| 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); |
| 194 | else |
| 195 | new_dirs->name = xstrdup (name); |
| 196 | + |
| 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)))) |
| 203 | + { |
| 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); |
| 207 | + else |
| 208 | + einfo (_("%P: warning: library search path \"%s\" is unsafe for " |
| 209 | + "cross-compilation\n"), name); |
| 210 | + } |
| 211 | +#endif |
| 212 | + |
| 213 | } |
| 214 | |
| 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 |
| 218 | --- a/ld/ldlex.h |
| 219 | +++ b/ld/ldlex.h |
| 220 | @@ -140,6 +140,8 @@ enum option_values |
| 221 | OPTION_IGNORE_UNRESOLVED_SYMBOL, |
| 222 | OPTION_PUSH_STATE, |
| 223 | OPTION_POP_STATE, |
| 224 | + OPTION_NO_POISON_SYSTEM_DIRECTORIES, |
| 225 | + OPTION_ERROR_POISON_SYSTEM_DIRECTORIES, |
| 226 | }; |
| 227 | |
| 228 | /* The initial parser states. */ |
| 229 | diff --git a/ld/ldmain.c b/ld/ldmain.c |
| 230 | index 818d108..54c49f2 100644 |
| 231 | --- a/ld/ldmain.c |
| 232 | +++ b/ld/ldmain.c |
| 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; |
| 239 | |
| 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 |
| 244 | --- a/ld/lexsup.c |
| 245 | +++ b/ld/lexsup.c |
| 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"), |
| 249 | TWO_DASHES }, |
| 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"), |
| 253 | + TWO_DASHES }, |
| 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"), |
| 257 | + TWO_DASHES }, |
| 258 | }; |
| 259 | |
| 260 | #define OPTION_COUNT ARRAY_SIZE (ld_options) |
| 261 | @@ -1474,6 +1482,14 @@ parse_args (unsigned argc, char **argv) |
| 262 | free (oldp); |
| 263 | } |
| 264 | break; |
| 265 | + |
| 266 | + case OPTION_NO_POISON_SYSTEM_DIRECTORIES: |
| 267 | + command_line.poison_system_directories = FALSE; |
| 268 | + break; |
| 269 | + |
| 270 | + case OPTION_ERROR_POISON_SYSTEM_DIRECTORIES: |
| 271 | + command_line.error_poison_system_directories = TRUE; |
| 272 | + break; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | -- |
| 277 | 2.1.4 |
| 278 | |