0006-warn-for-uses-of-system-directories-when-cross-linki.patch 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. From 2c43b1357db6b09d1645704afd3f45be6de0cf4d Mon Sep 17 00:00:00 2001
  2. From: Khem Raj <raj.khem@gmail.com>
  3. Date: Fri, 15 Jan 2016 06:31:09 +0000
  4. Subject: [PATCH] warn for uses of system directories when cross linking
  5. 2008-07-02 Joseph Myers <joseph@codesourcery.com>
  6. ld/
  7. * ld.h (args_type): Add error_poison_system_directories.
  8. * ld.texinfo (--error-poison-system-directories): Document.
  9. * ldfile.c (ldfile_add_library_path): Check
  10. command_line.error_poison_system_directories.
  11. * ldmain.c (main): Initialize
  12. command_line.error_poison_system_directories.
  13. * lexsup.c (enum option_values): Add
  14. OPTION_ERROR_POISON_SYSTEM_DIRECTORIES.
  15. (ld_options): Add --error-poison-system-directories.
  16. (parse_args): Handle new option.
  17. 2007-06-13 Joseph Myers <joseph@codesourcery.com>
  18. ld/
  19. * config.in: Regenerate.
  20. * ld.h (args_type): Add poison_system_directories.
  21. * ld.texinfo (--no-poison-system-directories): Document.
  22. * ldfile.c (ldfile_add_library_path): Check
  23. command_line.poison_system_directories.
  24. * ldmain.c (main): Initialize
  25. command_line.poison_system_directories.
  26. * lexsup.c (enum option_values): Add
  27. OPTION_NO_POISON_SYSTEM_DIRECTORIES.
  28. (ld_options): Add --no-poison-system-directories.
  29. (parse_args): Handle new option.
  30. 2007-04-20 Joseph Myers <joseph@codesourcery.com>
  31. Merge from Sourcery G++ binutils 2.17:
  32. 2007-03-20 Joseph Myers <joseph@codesourcery.com>
  33. Based on patch by Mark Hatle <mark.hatle@windriver.com>.
  34. ld/
  35. * configure.in (--enable-poison-system-directories): New option.
  36. * configure, config.in: Regenerate.
  37. * ldfile.c (ldfile_add_library_path): If
  38. ENABLE_POISON_SYSTEM_DIRECTORIES defined, warn for use of /lib,
  39. /usr/lib, /usr/local/lib or /usr/X11R6/lib.
  40. Upstream-Status: Pending
  41. Signed-off-by: Mark Hatle <mark.hatle@windriver.com>
  42. Signed-off-by: Scott Garman <scott.a.garman@intel.com>
  43. Signed-off-by: Khem Raj <raj.khem@gmail.com>
  44. ---
  45. ld/config.in | 3 +++
  46. ld/configure | 16 ++++++++++++++++
  47. ld/configure.ac | 10 ++++++++++
  48. ld/ld.h | 8 ++++++++
  49. ld/ld.texi | 12 ++++++++++++
  50. ld/ldfile.c | 17 +++++++++++++++++
  51. ld/ldlex.h | 2 ++
  52. ld/ldmain.c | 6 ++++--
  53. ld/lexsup.c | 16 ++++++++++++++++
  54. 9 files changed, 88 insertions(+), 2 deletions(-)
  55. diff --git a/ld/config.in b/ld/config.in
  56. index d4c1fc420b5..1aece0b2c29 100644
  57. --- a/ld/config.in
  58. +++ b/ld/config.in
  59. @@ -55,6 +55,9 @@
  60. language is requested. */
  61. #undef ENABLE_NLS
  62. +/* Define to warn for use of native system library directories */
  63. +#undef ENABLE_POISON_SYSTEM_DIRECTORIES
  64. +
  65. /* Additional extension a shared object might have. */
  66. #undef EXTRA_SHLIB_EXTENSION
  67. diff --git a/ld/configure b/ld/configure
  68. index e58fb7f3a35..d0a467ac101 100755
  69. --- a/ld/configure
  70. +++ b/ld/configure
  71. @@ -836,6 +836,7 @@ with_lib_path
  72. enable_targets
  73. enable_64_bit_bfd
  74. with_sysroot
  75. +enable_poison_system_directories
  76. enable_gold
  77. enable_got
  78. enable_compressed_debug_sections
  79. @@ -1514,6 +1515,8 @@ Optional Features:
  80. --enable-checking enable run-time checks
  81. --enable-targets alternative target configurations
  82. --enable-64-bit-bfd 64-bit support (on hosts with narrower word sizes)
  83. + --enable-poison-system-directories
  84. + warn for use of native system library directories
  85. --enable-gold[=ARG] build gold [ARG={default,yes,no}]
  86. --enable-got=<type> GOT handling scheme (target, single, negative,
  87. multigot)
  88. @@ -15349,6 +15352,19 @@ fi
  89. +# Check whether --enable-poison-system-directories was given.
  90. +if test "${enable_poison_system_directories+set}" = set; then :
  91. + enableval=$enable_poison_system_directories;
  92. +else
  93. + enable_poison_system_directories=no
  94. +fi
  95. +
  96. +if test "x${enable_poison_system_directories}" = "xyes"; then
  97. +
  98. +$as_echo "#define ENABLE_POISON_SYSTEM_DIRECTORIES 1" >>confdefs.h
  99. +
  100. +fi
  101. +
  102. # Check whether --enable-gold was given.
  103. if test "${enable_gold+set}" = set; then :
  104. enableval=$enable_gold; case "${enableval}" in
  105. diff --git a/ld/configure.ac b/ld/configure.ac
  106. index 4331d6b1302..e2976bc2926 100644
  107. --- a/ld/configure.ac
  108. +++ b/ld/configure.ac
  109. @@ -102,6 +102,16 @@ AC_SUBST(use_sysroot)
  110. AC_SUBST(TARGET_SYSTEM_ROOT)
  111. AC_SUBST(TARGET_SYSTEM_ROOT_DEFINE)
  112. +AC_ARG_ENABLE([poison-system-directories],
  113. + AS_HELP_STRING([--enable-poison-system-directories],
  114. + [warn for use of native system library directories]),,
  115. + [enable_poison_system_directories=no])
  116. +if test "x${enable_poison_system_directories}" = "xyes"; then
  117. + AC_DEFINE([ENABLE_POISON_SYSTEM_DIRECTORIES],
  118. + [1],
  119. + [Define to warn for use of native system library directories])
  120. +fi
  121. +
  122. dnl Use --enable-gold to decide if this linker should be the default.
  123. dnl "install_as_default" is set to false if gold is the default linker.
  124. dnl "installed_linker" is the installed BFD linker name.
  125. diff --git a/ld/ld.h b/ld/ld.h
  126. index f3086bf30de..db5064243c7 100644
  127. --- a/ld/ld.h
  128. +++ b/ld/ld.h
  129. @@ -162,6 +162,14 @@ typedef struct
  130. in the linker script. */
  131. bool force_group_allocation;
  132. + /* If TRUE (the default) warn for uses of system directories when
  133. + cross linking. */
  134. + bool poison_system_directories;
  135. +
  136. + /* If TRUE (default FALSE) give an error for uses of system
  137. + directories when cross linking instead of a warning. */
  138. + bool error_poison_system_directories;
  139. +
  140. /* Big or little endian as set on command line. */
  141. enum endian_enum endian;
  142. diff --git a/ld/ld.texi b/ld/ld.texi
  143. index eabbec8faa9..c4680e4947e 100644
  144. --- a/ld/ld.texi
  145. +++ b/ld/ld.texi
  146. @@ -2947,6 +2947,18 @@ creation of the metadata note, if one had been enabled by an earlier
  147. occurrence of the --package-metdata option.
  148. If the linker has been built with libjansson, then the JSON string
  149. will be validated.
  150. +
  151. +@kindex --no-poison-system-directories
  152. +@item --no-poison-system-directories
  153. +Do not warn for @option{-L} options using system directories such as
  154. +@file{/usr/lib} when cross linking. This option is intended for use
  155. +in chroot environments when such directories contain the correct
  156. +libraries for the target system rather than the host.
  157. +
  158. +@kindex --error-poison-system-directories
  159. +@item --error-poison-system-directories
  160. +Give an error instead of a warning for @option{-L} options using
  161. +system directories when cross linking.
  162. @end table
  163. @c man end
  164. diff --git a/ld/ldfile.c b/ld/ldfile.c
  165. index 731ae5f7aed..dd8f03fd960 100644
  166. --- a/ld/ldfile.c
  167. +++ b/ld/ldfile.c
  168. @@ -117,6 +117,23 @@ ldfile_add_library_path (const char *name, bool cmdline)
  169. new_dirs->name = concat (ld_sysroot, name + strlen ("$SYSROOT"), (const char *) NULL);
  170. else
  171. new_dirs->name = xstrdup (name);
  172. +
  173. +#ifdef ENABLE_POISON_SYSTEM_DIRECTORIES
  174. + if (command_line.poison_system_directories
  175. + && ((!strncmp (name, "/lib", 4))
  176. + || (!strncmp (name, "/usr/lib", 8))
  177. + || (!strncmp (name, "/usr/local/lib", 14))
  178. + || (!strncmp (name, "/usr/X11R6/lib", 14))))
  179. + {
  180. + if (command_line.error_poison_system_directories)
  181. + einfo (_("%X%P: error: library search path \"%s\" is unsafe for "
  182. + "cross-compilation\n"), name);
  183. + else
  184. + einfo (_("%P: warning: library search path \"%s\" is unsafe for "
  185. + "cross-compilation\n"), name);
  186. + }
  187. +#endif
  188. +
  189. }
  190. /* Try to open a BFD for a lang_input_statement. */
  191. diff --git a/ld/ldlex.h b/ld/ldlex.h
  192. index 57ade1f754b..64007ff8684 100644
  193. --- a/ld/ldlex.h
  194. +++ b/ld/ldlex.h
  195. @@ -168,6 +168,8 @@ enum option_values
  196. OPTION_NO_WARN_EXECSTACK,
  197. OPTION_WARN_RWX_SEGMENTS,
  198. OPTION_NO_WARN_RWX_SEGMENTS,
  199. + OPTION_NO_POISON_SYSTEM_DIRECTORIES,
  200. + OPTION_ERROR_POISON_SYSTEM_DIRECTORIES,
  201. };
  202. /* The initial parser states. */
  203. diff --git a/ld/ldmain.c b/ld/ldmain.c
  204. index 1ae90a77749..f40750fd816 100644
  205. --- a/ld/ldmain.c
  206. +++ b/ld/ldmain.c
  207. @@ -322,6 +322,8 @@ main (int argc, char **argv)
  208. command_line.warn_mismatch = true;
  209. command_line.warn_search_mismatch = true;
  210. command_line.check_section_addresses = -1;
  211. + command_line.poison_system_directories = true;
  212. + command_line.error_poison_system_directories = false;
  213. /* We initialize DEMANGLING based on the environment variable
  214. COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the
  215. @@ -1447,7 +1449,7 @@ undefined_symbol (struct bfd_link_info *info,
  216. argv[1] = "undefined-symbol";
  217. argv[2] = (char *) name;
  218. argv[3] = NULL;
  219. -
  220. +
  221. if (verbose)
  222. einfo (_("%P: About to run error handling script '%s' with arguments: '%s' '%s'\n"),
  223. argv[0], argv[1], argv[2]);
  224. @@ -1468,7 +1470,7 @@ undefined_symbol (struct bfd_link_info *info,
  225. carry on to issue the normal error message. */
  226. }
  227. #endif /* SUPPORT_ERROR_HANDLING_SCRIPT */
  228. -
  229. +
  230. if (section != NULL)
  231. {
  232. if (error_count < MAX_ERRORS_IN_A_ROW)
  233. diff --git a/ld/lexsup.c b/ld/lexsup.c
  234. index 9225f71b3ce..92fb66f1fa2 100644
  235. --- a/ld/lexsup.c
  236. +++ b/ld/lexsup.c
  237. @@ -608,6 +608,14 @@ static const struct ld_option ld_options[] =
  238. " <method> is: share-unconflicted (default),\n"
  239. " share-duplicated"),
  240. TWO_DASHES },
  241. + { {"no-poison-system-directories", no_argument, NULL,
  242. + OPTION_NO_POISON_SYSTEM_DIRECTORIES},
  243. + '\0', NULL, N_("Do not warn for -L options using system directories"),
  244. + TWO_DASHES },
  245. + { {"error-poison-system-directories", no_argument, NULL,
  246. + + OPTION_ERROR_POISON_SYSTEM_DIRECTORIES},
  247. + '\0', NULL, N_("Give an error for -L options using system directories"),
  248. + TWO_DASHES },
  249. };
  250. #define OPTION_COUNT ARRAY_SIZE (ld_options)
  251. @@ -1722,6 +1730,14 @@ parse_args (unsigned argc, char **argv)
  252. config.print_map_discarded = true;
  253. break;
  254. + case OPTION_NO_POISON_SYSTEM_DIRECTORIES:
  255. + command_line.poison_system_directories = false;
  256. + break;
  257. +
  258. + case OPTION_ERROR_POISON_SYSTEM_DIRECTORIES:
  259. + command_line.error_poison_system_directories = true;
  260. + break;
  261. +
  262. case OPTION_DEPENDENCY_FILE:
  263. config.dependency_file = optarg;
  264. break;