0001-poison-system-directories.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. From 5f62ad7ce534e3384d6ed8892614979da297bd70 Mon Sep 17 00:00:00 2001
  2. From: Evgeniy Didin <Evgeniy.Didin@synopsys.com>
  3. Date: Mon, 14 Oct 2019 16:45:15 +0300
  4. Subject: [PATCH] [PATCH] poison-system-directories
  5. Patch adapted to arc-binutils-gdb-2019.09
  6. Signed-off-by: Evgeniy Didin <Evgeniy.Didin@synopsys.com>
  7. Patch adapted to binutils 2.23.2 and extended to use
  8. BR_COMPILER_PARANOID_UNSAFE_PATH by Thomas Petazzoni.
  9. [Romain: rebase on top of 2.26]
  10. Signed-off-by: Romain Naour <romain.naour@gmail.com>
  11. [Gustavo: adapt to binutils 2.25]
  12. Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  13. Signed-off-by: Gustavo Zacarias <gustavo@zacarias.com.ar>
  14. Upstream-Status: Inappropriate [distribution: codesourcery]
  15. Patch originally created by Mark Hatle, forward-ported to
  16. binutils 2.21 by Scott Garman.
  17. purpose: warn for uses of system directories when cross linking
  18. Code Merged from Sourcery G++ binutils 2.19 - 4.4-277
  19. 2008-07-02 Joseph Myers <joseph@codesourcery.com>
  20. ld/
  21. * ld.h (args_type): Add error_poison_system_directories.
  22. * ld.texinfo (--error-poison-system-directories): Document.
  23. * ldfile.c (ldfile_add_library_path): Check
  24. command_line.error_poison_system_directories.
  25. * ldmain.c (main): Initialize
  26. command_line.error_poison_system_directories.
  27. * lexsup.c (enum option_values): Add
  28. OPTION_ERROR_POISON_SYSTEM_DIRECTORIES.
  29. (ld_options): Add --error-poison-system-directories.
  30. (parse_args): Handle new option.
  31. 2007-06-13 Joseph Myers <joseph@codesourcery.com>
  32. ld/
  33. * config.in: Regenerate.
  34. * ld.h (args_type): Add poison_system_directories.
  35. * ld.texinfo (--no-poison-system-directories): Document.
  36. * ldfile.c (ldfile_add_library_path): Check
  37. command_line.poison_system_directories.
  38. * ldmain.c (main): Initialize
  39. command_line.poison_system_directories.
  40. * lexsup.c (enum option_values): Add
  41. OPTION_NO_POISON_SYSTEM_DIRECTORIES.
  42. (ld_options): Add --no-poison-system-directories.
  43. (parse_args): Handle new option.
  44. 2007-04-20 Joseph Myers <joseph@codesourcery.com>
  45. Merge from Sourcery G++ binutils 2.17:
  46. 2007-03-20 Joseph Myers <joseph@codesourcery.com>
  47. Based on patch by Mark Hatle <mark.hatle@windriver.com>.
  48. ld/
  49. * configure.ac (--enable-poison-system-directories): New option.
  50. * configure, config.in: Regenerate.
  51. * ldfile.c (ldfile_add_library_path): If
  52. ENABLE_POISON_SYSTEM_DIRECTORIES defined, warn for use of /lib,
  53. /usr/lib, /usr/local/lib or /usr/X11R6/lib.
  54. Signed-off-by: Mark Hatle <mark.hatle@windriver.com>
  55. Signed-off-by: Scott Garman <scott.a.garman@intel.com>
  56. ---
  57. ld/config.in | 3 +++
  58. ld/configure | 14 ++++++++++++++
  59. ld/configure.ac | 10 ++++++++++
  60. ld/ld.h | 8 ++++++++
  61. ld/ld.texi | 12 ++++++++++++
  62. ld/ldfile.c | 17 +++++++++++++++++
  63. ld/ldlex.h | 2 ++
  64. ld/ldmain.c | 2 ++
  65. ld/lexsup.c | 21 +++++++++++++++++++++
  66. 9 files changed, 89 insertions(+)
  67. diff --git a/ld/config.in b/ld/config.in
  68. index d93c9b08300..5da2742beac 100644
  69. --- a/ld/config.in
  70. +++ b/ld/config.in
  71. @@ -31,6 +31,9 @@
  72. language is requested. */
  73. #undef ENABLE_NLS
  74. +/* Define to warn for use of native system library directories */
  75. +#undef ENABLE_POISON_SYSTEM_DIRECTORIES
  76. +
  77. /* Additional extension a shared object might have. */
  78. #undef EXTRA_SHLIB_EXTENSION
  79. diff --git a/ld/configure b/ld/configure
  80. index 2d6ca5c0445..563f9921f7f 100755
  81. --- a/ld/configure
  82. +++ b/ld/configure
  83. @@ -823,6 +823,7 @@ with_lib_path
  84. enable_targets
  85. enable_64_bit_bfd
  86. with_sysroot
  87. +enable_poison_system_directories
  88. enable_gold
  89. enable_got
  90. enable_compressed_debug_sections
  91. @@ -1487,6 +1488,8 @@ Optional Features:
  92. --disable-largefile omit support for large files
  93. --enable-targets alternative target configurations
  94. --enable-64-bit-bfd 64-bit support (on hosts with narrower word sizes)
  95. + --enable-poison-system-directories
  96. + warn for use of native system library directories
  97. --enable-gold[=ARG] build gold [ARG={default,yes,no}]
  98. --enable-got=<type> GOT handling scheme (target, single, negative,
  99. multigot)
  100. @@ -15804,7 +15807,18 @@ else
  101. fi
  102. +# Check whether --enable-poison-system-directories was given.
  103. +if test "${enable_poison_system_directories+set}" = set; then :
  104. + enableval=$enable_poison_system_directories;
  105. +else
  106. + enable_poison_system_directories=no
  107. +fi
  108. +
  109. +if test "x${enable_poison_system_directories}" = "xyes"; then
  110. +$as_echo "#define ENABLE_POISON_SYSTEM_DIRECTORIES 1" >>confdefs.h
  111. +
  112. +fi
  113. # Check whether --enable-got was given.
  114. if test "${enable_got+set}" = set; then :
  115. diff --git a/ld/configure.ac b/ld/configure.ac
  116. index 41a51bbb7e9..dbaa98a9e17 100644
  117. --- a/ld/configure.ac
  118. +++ b/ld/configure.ac
  119. @@ -94,6 +94,16 @@ AC_SUBST(use_sysroot)
  120. AC_SUBST(TARGET_SYSTEM_ROOT)
  121. AC_SUBST(TARGET_SYSTEM_ROOT_DEFINE)
  122. +AC_ARG_ENABLE([poison-system-directories],
  123. + AS_HELP_STRING([--enable-poison-system-directories],
  124. + [warn for use of native system library directories]),,
  125. + [enable_poison_system_directories=no])
  126. +if test "x${enable_poison_system_directories}" = "xyes"; then
  127. + AC_DEFINE([ENABLE_POISON_SYSTEM_DIRECTORIES],
  128. + [1],
  129. + [Define to warn for use of native system library directories])
  130. +fi
  131. +
  132. dnl Use --enable-gold to decide if this linker should be the default.
  133. dnl "install_as_default" is set to false if gold is the default linker.
  134. dnl "installed_linker" is the installed BFD linker name.
  135. diff --git a/ld/ld.h b/ld/ld.h
  136. index 55078a9637b..511e9bc34b7 100644
  137. --- a/ld/ld.h
  138. +++ b/ld/ld.h
  139. @@ -180,6 +180,14 @@ typedef struct
  140. in the linker script. */
  141. bfd_boolean force_group_allocation;
  142. + /* If TRUE (the default) warn for uses of system directories when
  143. + cross linking. */
  144. + bfd_boolean poison_system_directories;
  145. +
  146. + /* If TRUE (default FALSE) give an error for uses of system
  147. + directories when cross linking instead of a warning. */
  148. + bfd_boolean error_poison_system_directories;
  149. +
  150. /* Big or little endian as set on command line. */
  151. enum endian_enum endian;
  152. diff --git a/ld/ld.texi b/ld/ld.texi
  153. index fcbc335c95e..6ba7ebdb32a 100644
  154. --- a/ld/ld.texi
  155. +++ b/ld/ld.texi
  156. @@ -2557,6 +2557,18 @@ string identifying the original linked file does not change.
  157. Passing @code{none} for @var{style} disables the setting from any
  158. @code{--build-id} options earlier on the command line.
  159. +
  160. +@kindex --no-poison-system-directories
  161. +@item --no-poison-system-directories
  162. +Do not warn for @option{-L} options using system directories such as
  163. +@file{/usr/lib} when cross linking. This option is intended for use
  164. +in chroot environments when such directories contain the correct
  165. +libraries for the target system rather than the host.
  166. +
  167. +@kindex --error-poison-system-directories
  168. +@item --error-poison-system-directories
  169. +Give an error instead of a warning for @option{-L} options using
  170. +system directories when cross linking.
  171. @end table
  172. @c man end
  173. diff --git a/ld/ldfile.c b/ld/ldfile.c
  174. index 7f60319390e..0bcc06db964 100644
  175. --- a/ld/ldfile.c
  176. +++ b/ld/ldfile.c
  177. @@ -116,6 +116,23 @@ ldfile_add_library_path (const char *name, bfd_boolean cmdline)
  178. new_dirs->name = concat (ld_sysroot, name + strlen ("$SYSROOT"), (const char *) NULL);
  179. else
  180. new_dirs->name = xstrdup (name);
  181. +
  182. +#ifdef ENABLE_POISON_SYSTEM_DIRECTORIES
  183. + if (command_line.poison_system_directories
  184. + && ((!strncmp (name, "/lib", 4))
  185. + || (!strncmp (name, "/usr/lib", 8))
  186. + || (!strncmp (name, "/usr/local/lib", 14))
  187. + || (!strncmp (name, "/usr/X11R6/lib", 14))))
  188. + {
  189. + if (command_line.error_poison_system_directories)
  190. + einfo (_("%X%P: error: library search path \"%s\" is unsafe for "
  191. + "cross-compilation\n"), name);
  192. + else
  193. + einfo (_("%P: warning: library search path \"%s\" is unsafe for "
  194. + "cross-compilation\n"), name);
  195. + }
  196. +#endif
  197. +
  198. }
  199. /* Try to open a BFD for a lang_input_statement. */
  200. diff --git a/ld/ldlex.h b/ld/ldlex.h
  201. index 32a7a6409e8..c02b64bf92f 100644
  202. --- a/ld/ldlex.h
  203. +++ b/ld/ldlex.h
  204. @@ -152,6 +152,8 @@ enum option_values
  205. OPTION_NO_PRINT_MAP_DISCARDED,
  206. OPTION_NON_CONTIGUOUS_REGIONS,
  207. OPTION_NON_CONTIGUOUS_REGIONS_WARNINGS,
  208. + OPTION_NO_POISON_SYSTEM_DIRECTORIES,
  209. + OPTION_ERROR_POISON_SYSTEM_DIRECTORIES,
  210. };
  211. /* The initial parser states. */
  212. diff --git a/ld/ldmain.c b/ld/ldmain.c
  213. index 34c19223137..66d2c3f4bcf 100644
  214. --- a/ld/ldmain.c
  215. +++ b/ld/ldmain.c
  216. @@ -270,6 +270,8 @@ main (int argc, char **argv)
  217. command_line.warn_mismatch = TRUE;
  218. command_line.warn_search_mismatch = TRUE;
  219. command_line.check_section_addresses = -1;
  220. + command_line.poison_system_directories = TRUE;
  221. + command_line.error_poison_system_directories = FALSE;
  222. /* We initialize DEMANGLING based on the environment variable
  223. COLLECT_NO_DEMANGLE. The gcc collect2 program will demangle the
  224. diff --git a/ld/lexsup.c b/ld/lexsup.c
  225. index 1c15ac29c0c..8b714e10a40 100644
  226. --- a/ld/lexsup.c
  227. +++ b/ld/lexsup.c
  228. @@ -549,6 +549,14 @@ static const struct ld_option ld_options[] =
  229. { {"no-print-map-discarded", no_argument, NULL, OPTION_NO_PRINT_MAP_DISCARDED},
  230. '\0', NULL, N_("Do not show discarded sections in map file output"),
  231. TWO_DASHES },
  232. + { {"no-poison-system-directories", no_argument, NULL,
  233. + OPTION_NO_POISON_SYSTEM_DIRECTORIES},
  234. + '\0', NULL, N_("Do not warn for -L options using system directories"),
  235. + TWO_DASHES },
  236. + { {"error-poison-system-directories", no_argument, NULL,
  237. + OPTION_ERROR_POISON_SYSTEM_DIRECTORIES},
  238. + '\0', NULL, N_("Give an error for -L options using system directories"),
  239. + TWO_DASHES },
  240. };
  241. #define OPTION_COUNT ARRAY_SIZE (ld_options)
  242. @@ -561,6 +569,7 @@ parse_args (unsigned argc, char **argv)
  243. int ingroup = 0;
  244. char *default_dirlist = NULL;
  245. char *shortopts;
  246. + char *BR_paranoid_env;
  247. struct option *longopts;
  248. struct option *really_longopts;
  249. int last_optind;
  250. @@ -1549,6 +1558,14 @@ parse_args (unsigned argc, char **argv)
  251. }
  252. break;
  253. + case OPTION_NO_POISON_SYSTEM_DIRECTORIES:
  254. + command_line.poison_system_directories = FALSE;
  255. + break;
  256. +
  257. + case OPTION_ERROR_POISON_SYSTEM_DIRECTORIES:
  258. + command_line.error_poison_system_directories = TRUE;
  259. + break;
  260. +
  261. case OPTION_PUSH_STATE:
  262. input_flags.pushed = xmemdup (&input_flags,
  263. sizeof (input_flags),
  264. @@ -1600,6 +1617,10 @@ parse_args (unsigned argc, char **argv)
  265. command_line.soname = NULL;
  266. }
  267. + BR_paranoid_env = getenv("BR_COMPILER_PARANOID_UNSAFE_PATH");
  268. + if (BR_paranoid_env && strlen(BR_paranoid_env) > 0)
  269. + command_line.error_poison_system_directories = TRUE;
  270. +
  271. while (ingroup)
  272. {
  273. einfo (_("%P: missing --end-group; added as last command line option\n"));
  274. --
  275. 2.16.2