0008-warn-for-uses-of-system-directories-when-cross-linki.patch 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. From 99e1565b9d2b929d4ee4e9da6f63766b22b23fc3 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 | 2 ++
  53. ld/lexsup.c | 16 ++++++++++++++++
  54. 9 files changed, 86 insertions(+)
  55. --- a/ld/config.in
  56. +++ b/ld/config.in
  57. @@ -43,6 +43,9 @@
  58. language is requested. */
  59. #undef ENABLE_NLS
  60. +/* Define to warn for use of native system library directories */
  61. +#undef ENABLE_POISON_SYSTEM_DIRECTORIES
  62. +
  63. /* Additional extension a shared object might have. */
  64. #undef EXTRA_SHLIB_EXTENSION
  65. --- a/ld/configure
  66. +++ b/ld/configure
  67. @@ -829,6 +829,7 @@ with_lib_path
  68. enable_targets
  69. enable_64_bit_bfd
  70. with_sysroot
  71. +enable_poison_system_directories
  72. enable_gold
  73. enable_got
  74. enable_compressed_debug_sections
  75. @@ -1498,6 +1499,8 @@ Optional Features:
  76. --enable-checking enable run-time checks
  77. --enable-targets alternative target configurations
  78. --enable-64-bit-bfd 64-bit support (on hosts with narrower word sizes)
  79. + --enable-poison-system-directories
  80. + warn for use of native system library directories
  81. --enable-gold[=ARG] build gold [ARG={default,yes,no}]
  82. --enable-got=<type> GOT handling scheme (target, single, negative,
  83. multigot)
  84. @@ -15215,6 +15218,19 @@ fi
  85. +# Check whether --enable-poison-system-directories was given.
  86. +if test "${enable_poison_system_directories+set}" = set; then :
  87. + enableval=$enable_poison_system_directories;
  88. +else
  89. + enable_poison_system_directories=no
  90. +fi
  91. +
  92. +if test "x${enable_poison_system_directories}" = "xyes"; then
  93. +
  94. +$as_echo "#define ENABLE_POISON_SYSTEM_DIRECTORIES 1" >>confdefs.h
  95. +
  96. +fi
  97. +
  98. # Check whether --enable-gold was given.
  99. if test "${enable_gold+set}" = set; then :
  100. enableval=$enable_gold; case "${enableval}" in
  101. --- a/ld/configure.ac
  102. +++ b/ld/configure.ac
  103. @@ -106,6 +106,16 @@ AC_SUBST(use_sysroot)
  104. AC_SUBST(TARGET_SYSTEM_ROOT)
  105. AC_SUBST(TARGET_SYSTEM_ROOT_DEFINE)
  106. +AC_ARG_ENABLE([poison-system-directories],
  107. + AS_HELP_STRING([--enable-poison-system-directories],
  108. + [warn for use of native system library directories]),,
  109. + [enable_poison_system_directories=no])
  110. +if test "x${enable_poison_system_directories}" = "xyes"; then
  111. + AC_DEFINE([ENABLE_POISON_SYSTEM_DIRECTORIES],
  112. + [1],
  113. + [Define to warn for use of native system library directories])
  114. +fi
  115. +
  116. dnl Use --enable-gold to decide if this linker should be the default.
  117. dnl "install_as_default" is set to false if gold is the default linker.
  118. dnl "installed_linker" is the installed BFD linker name.
  119. --- a/ld/ld.h
  120. +++ b/ld/ld.h
  121. @@ -162,6 +162,14 @@ typedef struct
  122. in the linker script. */
  123. bool force_group_allocation;
  124. + /* If TRUE (the default) warn for uses of system directories when
  125. + cross linking. */
  126. + bool poison_system_directories;
  127. +
  128. + /* If TRUE (default FALSE) give an error for uses of system
  129. + directories when cross linking instead of a warning. */
  130. + bool error_poison_system_directories;
  131. +
  132. /* Big or little endian as set on command line. */
  133. enum endian_enum endian;
  134. --- a/ld/ld.texi
  135. +++ b/ld/ld.texi
  136. @@ -2863,6 +2863,18 @@ string identifying the original linked f
  137. Passing @code{none} for @var{style} disables the setting from any
  138. @code{--build-id} options earlier on the command line.
  139. +
  140. +@kindex --no-poison-system-directories
  141. +@item --no-poison-system-directories
  142. +Do not warn for @option{-L} options using system directories such as
  143. +@file{/usr/lib} when cross linking. This option is intended for use
  144. +in chroot environments when such directories contain the correct
  145. +libraries for the target system rather than the host.
  146. +
  147. +@kindex --error-poison-system-directories
  148. +@item --error-poison-system-directories
  149. +Give an error instead of a warning for @option{-L} options using
  150. +system directories when cross linking.
  151. @end table
  152. @c man end
  153. --- a/ld/ldfile.c
  154. +++ b/ld/ldfile.c
  155. @@ -117,6 +117,23 @@ ldfile_add_library_path (const char *nam
  156. new_dirs->name = concat (ld_sysroot, name + strlen ("$SYSROOT"), (const char *) NULL);
  157. else
  158. new_dirs->name = xstrdup (name);
  159. +
  160. +#ifdef ENABLE_POISON_SYSTEM_DIRECTORIES
  161. + if (command_line.poison_system_directories
  162. + && ((!strncmp (name, "/lib", 4))
  163. + || (!strncmp (name, "/usr/lib", 8))
  164. + || (!strncmp (name, "/usr/local/lib", 14))
  165. + || (!strncmp (name, "/usr/X11R6/lib", 14))))
  166. + {
  167. + if (command_line.error_poison_system_directories)
  168. + einfo (_("%X%P: error: library search path \"%s\" is unsafe for "
  169. + "cross-compilation\n"), name);
  170. + else
  171. + einfo (_("%P: warning: library search path \"%s\" is unsafe for "
  172. + "cross-compilation\n"), name);
  173. + }
  174. +#endif
  175. +
  176. }
  177. /* Try to open a BFD for a lang_input_statement. */
  178. --- a/ld/ldlex.h
  179. +++ b/ld/ldlex.h
  180. @@ -163,6 +163,8 @@ enum option_values
  181. OPTION_CTF_VARIABLES,
  182. OPTION_NO_CTF_VARIABLES,
  183. OPTION_CTF_SHARE_TYPES,
  184. + OPTION_NO_POISON_SYSTEM_DIRECTORIES,
  185. + OPTION_ERROR_POISON_SYSTEM_DIRECTORIES,
  186. };
  187. /* The initial parser states. */
  188. --- a/ld/ldmain.c
  189. +++ b/ld/ldmain.c
  190. @@ -321,6 +321,8 @@ main (int argc, char **argv)
  191. command_line.warn_mismatch = true;
  192. command_line.warn_search_mismatch = true;
  193. command_line.check_section_addresses = -1;
  194. + command_line.poison_system_directories = true;
  195. + command_line.error_poison_system_directories = false;
  196. /* We initialize DEMANGLING based on the environment variable
  197. COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the
  198. @@ -1444,7 +1446,7 @@ undefined_symbol (struct bfd_link_info *
  199. argv[1] = "undefined-symbol";
  200. argv[2] = (char *) name;
  201. argv[3] = NULL;
  202. -
  203. +
  204. if (verbose)
  205. einfo (_("%P: About to run error handling script '%s' with arguments: '%s' '%s'\n"),
  206. argv[0], argv[1], argv[2]);
  207. @@ -1465,7 +1467,7 @@ undefined_symbol (struct bfd_link_info *
  208. carry on to issue the normal error message. */
  209. }
  210. #endif /* SUPPORT_ERROR_HANDLING_SCRIPT */
  211. -
  212. +
  213. if (section != NULL)
  214. {
  215. if (error_count < MAX_ERRORS_IN_A_ROW)
  216. --- a/ld/lexsup.c
  217. +++ b/ld/lexsup.c
  218. @@ -595,6 +595,14 @@ static const struct ld_option ld_options
  219. " <method> is: share-unconflicted (default),\n"
  220. " share-duplicated"),
  221. TWO_DASHES },
  222. + { {"no-poison-system-directories", no_argument, NULL,
  223. + OPTION_NO_POISON_SYSTEM_DIRECTORIES},
  224. + '\0', NULL, N_("Do not warn for -L options using system directories"),
  225. + TWO_DASHES },
  226. + { {"error-poison-system-directories", no_argument, NULL,
  227. + + OPTION_ERROR_POISON_SYSTEM_DIRECTORIES},
  228. + '\0', NULL, N_("Give an error for -L options using system directories"),
  229. + TWO_DASHES },
  230. };
  231. #define OPTION_COUNT ARRAY_SIZE (ld_options)
  232. @@ -1686,6 +1694,14 @@ parse_args (unsigned argc, char **argv)
  233. config.print_map_discarded = true;
  234. break;
  235. + case OPTION_NO_POISON_SYSTEM_DIRECTORIES:
  236. + command_line.poison_system_directories = false;
  237. + break;
  238. +
  239. + case OPTION_ERROR_POISON_SYSTEM_DIRECTORIES:
  240. + command_line.error_poison_system_directories = true;
  241. + break;
  242. +
  243. case OPTION_DEPENDENCY_FILE:
  244. config.dependency_file = optarg;
  245. break;