From 2c43b1357db6b09d1645704afd3f45be6de0cf4d Mon Sep 17 00:00:00 2001
From: Khem Raj <raj.khem@gmail.com>
Date: Fri, 15 Jan 2016 06:31:09 +0000
Subject: [PATCH] warn for uses of system directories when cross linking

2008-07-02  Joseph Myers  <joseph@codesourcery.com>

    ld/
    * ld.h (args_type): Add error_poison_system_directories.
    * ld.texinfo (--error-poison-system-directories): Document.
    * ldfile.c (ldfile_add_library_path): Check
    command_line.error_poison_system_directories.
    * ldmain.c (main): Initialize
    command_line.error_poison_system_directories.
    * lexsup.c (enum option_values): Add
    OPTION_ERROR_POISON_SYSTEM_DIRECTORIES.
    (ld_options): Add --error-poison-system-directories.
    (parse_args): Handle new option.

2007-06-13  Joseph Myers  <joseph@codesourcery.com>

    ld/
    * config.in: Regenerate.
    * ld.h (args_type): Add poison_system_directories.
    * ld.texinfo (--no-poison-system-directories): Document.
    * ldfile.c (ldfile_add_library_path): Check
    command_line.poison_system_directories.
    * ldmain.c (main): Initialize
    command_line.poison_system_directories.
    * lexsup.c (enum option_values): Add
    OPTION_NO_POISON_SYSTEM_DIRECTORIES.
    (ld_options): Add --no-poison-system-directories.
    (parse_args): Handle new option.

2007-04-20  Joseph Myers  <joseph@codesourcery.com>

    Merge from Sourcery G++ binutils 2.17:

    2007-03-20  Joseph Myers  <joseph@codesourcery.com>
    Based on patch by Mark Hatle <mark.hatle@windriver.com>.
    ld/
    * configure.in (--enable-poison-system-directories): New option.
    * configure, config.in: Regenerate.
    * ldfile.c (ldfile_add_library_path): If
    ENABLE_POISON_SYSTEM_DIRECTORIES defined, warn for use of /lib,
    /usr/lib, /usr/local/lib or /usr/X11R6/lib.

Upstream-Status: Pending

Signed-off-by: Mark Hatle <mark.hatle@windriver.com>
Signed-off-by: Scott Garman <scott.a.garman@intel.com>
Signed-off-by: Khem Raj <raj.khem@gmail.com>
---
 ld/config.in    |  3 +++
 ld/configure    | 16 ++++++++++++++++
 ld/configure.ac | 10 ++++++++++
 ld/ld.h         |  8 ++++++++
 ld/ld.texi      | 12 ++++++++++++
 ld/ldfile.c     | 17 +++++++++++++++++
 ld/ldlex.h      |  2 ++
 ld/ldmain.c     |  6 ++++--
 ld/lexsup.c     | 16 ++++++++++++++++
 9 files changed, 88 insertions(+), 2 deletions(-)

diff --git a/ld/config.in b/ld/config.in
index d4c1fc420b5..1aece0b2c29 100644
--- a/ld/config.in
+++ b/ld/config.in
@@ -55,6 +55,9 @@
    language is requested. */
 #undef ENABLE_NLS
 
+/* Define to warn for use of native system library directories */
+#undef ENABLE_POISON_SYSTEM_DIRECTORIES
+
 /* Additional extension a shared object might have. */
 #undef EXTRA_SHLIB_EXTENSION
 
diff --git a/ld/configure b/ld/configure
index e58fb7f3a35..d0a467ac101 100755
--- a/ld/configure
+++ b/ld/configure
@@ -836,6 +836,7 @@ with_lib_path
 enable_targets
 enable_64_bit_bfd
 with_sysroot
+enable_poison_system_directories
 enable_gold
 enable_got
 enable_compressed_debug_sections
@@ -1514,6 +1515,8 @@ Optional Features:
   --enable-checking       enable run-time checks
   --enable-targets        alternative target configurations
   --enable-64-bit-bfd     64-bit support (on hosts with narrower word sizes)
+  --enable-poison-system-directories
+                          warn for use of native system library directories
   --enable-gold[=ARG]     build gold [ARG={default,yes,no}]
   --enable-got=<type>     GOT handling scheme (target, single, negative,
                           multigot)
@@ -15349,6 +15352,19 @@ fi
 
 
 
+# Check whether --enable-poison-system-directories was given.
+if test "${enable_poison_system_directories+set}" = set; then :
+  enableval=$enable_poison_system_directories;
+else
+  enable_poison_system_directories=no
+fi
+
+if test "x${enable_poison_system_directories}" = "xyes"; then
+
+$as_echo "#define ENABLE_POISON_SYSTEM_DIRECTORIES 1" >>confdefs.h
+
+fi
+
 # Check whether --enable-gold was given.
 if test "${enable_gold+set}" = set; then :
   enableval=$enable_gold; case "${enableval}" in
diff --git a/ld/configure.ac b/ld/configure.ac
index 4331d6b1302..e2976bc2926 100644
--- a/ld/configure.ac
+++ b/ld/configure.ac
@@ -102,6 +102,16 @@ AC_SUBST(use_sysroot)
 AC_SUBST(TARGET_SYSTEM_ROOT)
 AC_SUBST(TARGET_SYSTEM_ROOT_DEFINE)
 
+AC_ARG_ENABLE([poison-system-directories],
+         AS_HELP_STRING([--enable-poison-system-directories],
+                [warn for use of native system library directories]),,
+         [enable_poison_system_directories=no])
+if test "x${enable_poison_system_directories}" = "xyes"; then
+  AC_DEFINE([ENABLE_POISON_SYSTEM_DIRECTORIES],
+       [1],
+       [Define to warn for use of native system library directories])
+fi
+
 dnl Use --enable-gold to decide if this linker should be the default.
 dnl "install_as_default" is set to false if gold is the default linker.
 dnl "installed_linker" is the installed BFD linker name.
diff --git a/ld/ld.h b/ld/ld.h
index f3086bf30de..db5064243c7 100644
--- a/ld/ld.h
+++ b/ld/ld.h
@@ -162,6 +162,14 @@ typedef struct
      in the linker script.  */
   bool force_group_allocation;
 
+  /* If TRUE (the default) warn for uses of system directories when
+     cross linking.  */
+  bool poison_system_directories;
+
+  /* If TRUE (default FALSE) give an error for uses of system
+     directories when cross linking instead of a warning.  */
+  bool error_poison_system_directories;
+
   /* Big or little endian as set on command line.  */
   enum endian_enum endian;
 
diff --git a/ld/ld.texi b/ld/ld.texi
index eabbec8faa9..c4680e4947e 100644
--- a/ld/ld.texi
+++ b/ld/ld.texi
@@ -2947,6 +2947,18 @@ creation of the metadata note, if one had been enabled by an earlier
 occurrence of the --package-metdata option.
 If the linker has been built with libjansson, then the JSON string
 will be validated.
+
+@kindex --no-poison-system-directories
+@item --no-poison-system-directories
+Do not warn for @option{-L} options using system directories such as
+@file{/usr/lib} when cross linking.  This option is intended for use
+in chroot environments when such directories contain the correct
+libraries for the target system rather than the host.
+
+@kindex --error-poison-system-directories
+@item --error-poison-system-directories
+Give an error instead of a warning for @option{-L} options using
+system directories when cross linking.
 @end table
 
 @c man end
diff --git a/ld/ldfile.c b/ld/ldfile.c
index 731ae5f7aed..dd8f03fd960 100644
--- a/ld/ldfile.c
+++ b/ld/ldfile.c
@@ -117,6 +117,23 @@ ldfile_add_library_path (const char *name, bool cmdline)
     new_dirs->name = concat (ld_sysroot, name + strlen ("$SYSROOT"), (const char *) NULL);
   else
     new_dirs->name = xstrdup (name);
+
+#ifdef ENABLE_POISON_SYSTEM_DIRECTORIES
+  if (command_line.poison_system_directories
+  && ((!strncmp (name, "/lib", 4))
+      || (!strncmp (name, "/usr/lib", 8))
+      || (!strncmp (name, "/usr/local/lib", 14))
+      || (!strncmp (name, "/usr/X11R6/lib", 14))))
+   {
+     if (command_line.error_poison_system_directories)
+       einfo (_("%X%P: error: library search path \"%s\" is unsafe for "
+            "cross-compilation\n"), name);
+     else
+       einfo (_("%P: warning: library search path \"%s\" is unsafe for "
+            "cross-compilation\n"), name);
+   }
+#endif
+
 }
 
 /* Try to open a BFD for a lang_input_statement.  */
diff --git a/ld/ldlex.h b/ld/ldlex.h
index 57ade1f754b..64007ff8684 100644
--- a/ld/ldlex.h
+++ b/ld/ldlex.h
@@ -168,6 +168,8 @@ enum option_values
   OPTION_NO_WARN_EXECSTACK,
   OPTION_WARN_RWX_SEGMENTS,
   OPTION_NO_WARN_RWX_SEGMENTS,
+  OPTION_NO_POISON_SYSTEM_DIRECTORIES,
+  OPTION_ERROR_POISON_SYSTEM_DIRECTORIES,
 };
 
 /* The initial parser states.  */
diff --git a/ld/ldmain.c b/ld/ldmain.c
index 1ae90a77749..f40750fd816 100644
--- a/ld/ldmain.c
+++ b/ld/ldmain.c
@@ -322,6 +322,8 @@ main (int argc, char **argv)
   command_line.warn_mismatch = true;
   command_line.warn_search_mismatch = true;
   command_line.check_section_addresses = -1;
+  command_line.poison_system_directories = true;
+  command_line.error_poison_system_directories = false;
 
   /* We initialize DEMANGLING based on the environment variable
      COLLECT_NO_DEMANGLE.  The gcc collect2 program will demangle the
@@ -1447,7 +1449,7 @@ undefined_symbol (struct bfd_link_info *info,
       argv[1] = "undefined-symbol";
       argv[2] = (char *) name;
       argv[3] = NULL;
-      
+
       if (verbose)
 	einfo (_("%P: About to run error handling script '%s' with arguments: '%s' '%s'\n"),
 	       argv[0], argv[1], argv[2]);
@@ -1468,7 +1470,7 @@ undefined_symbol (struct bfd_link_info *info,
 	 carry on to issue the normal error message.  */
     }
 #endif /* SUPPORT_ERROR_HANDLING_SCRIPT */
-  
+
   if (section != NULL)
     {
       if (error_count < MAX_ERRORS_IN_A_ROW)
diff --git a/ld/lexsup.c b/ld/lexsup.c
index 9225f71b3ce..92fb66f1fa2 100644
--- a/ld/lexsup.c
+++ b/ld/lexsup.c
@@ -608,6 +608,14 @@ static const struct ld_option ld_options[] =
 		   "                                <method> is: share-unconflicted (default),\n"
 		   "                                             share-duplicated"),
     TWO_DASHES },
+  { {"no-poison-system-directories", no_argument, NULL,
+     OPTION_NO_POISON_SYSTEM_DIRECTORIES},
+    '\0', NULL, N_("Do not warn for -L options using system directories"),
+    TWO_DASHES },
+  { {"error-poison-system-directories", no_argument, NULL,
+    +     OPTION_ERROR_POISON_SYSTEM_DIRECTORIES},
+    '\0', NULL, N_("Give an error for -L options using system directories"),
+    TWO_DASHES },
 };
 
 #define OPTION_COUNT ARRAY_SIZE (ld_options)
@@ -1722,6 +1730,14 @@ parse_args (unsigned argc, char **argv)
 	  config.print_map_discarded = true;
 	  break;
 
+	case OPTION_NO_POISON_SYSTEM_DIRECTORIES:
+	  command_line.poison_system_directories = false;
+	  break;
+
+	case OPTION_ERROR_POISON_SYSTEM_DIRECTORIES:
+	  command_line.error_poison_system_directories = true;
+	  break;
+
 	case OPTION_DEPENDENCY_FILE:
 	  config.dependency_file = optarg;
 	  break;
