0002-poison-system-directories.patch 10 KB

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