0005-On-linux-targets-pass-as-needed-by-default-to-the-li.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. From 5b8de466aa959a6c929cee2c78e4159a0209b4f8 Mon Sep 17 00:00:00 2001
  2. From: Ariadne Conill <ariadne@dereferenced.org>
  3. Date: Fri, 21 Aug 2020 06:46:56 +0000
  4. Subject: [PATCH] On linux targets pass --as-needed by default to the linker,
  5. but always link the sanitizer libraries with --no-as-needed.
  6. ---
  7. gcc/config/aarch64/aarch64-linux.h | 1 +
  8. gcc/config/alpha/linux-elf.h | 2 +-
  9. gcc/config/arm/linux-elf.h | 1 +
  10. gcc/config/gnu-user.h | 6 +++---
  11. gcc/config/i386/gnu-user.h | 2 +-
  12. gcc/config/i386/gnu-user64.h | 1 +
  13. gcc/config/ia64/linux.h | 2 +-
  14. gcc/config/mips/gnu-user.h | 1 +
  15. gcc/config/riscv/linux.h | 1 +
  16. gcc/config/rs6000/linux64.h | 4 ++--
  17. gcc/config/rs6000/sysv4.h | 2 +-
  18. gcc/config/s390/linux.h | 2 +-
  19. gcc/config/sparc/linux.h | 2 +-
  20. gcc/gcc.c | 28 ++++++++++++++++++++--------
  21. 14 files changed, 36 insertions(+), 19 deletions(-)
  22. diff --git a/gcc/config/aarch64/aarch64-linux.h b/gcc/config/aarch64/aarch64-linux.h
  23. index e587e2e9ad6..1d1017aa523 100644
  24. --- a/gcc/config/aarch64/aarch64-linux.h
  25. +++ b/gcc/config/aarch64/aarch64-linux.h
  26. @@ -35,6 +35,7 @@
  27. #define CPP_SPEC "%{pthread:-D_REENTRANT}"
  28. #define LINUX_TARGET_LINK_SPEC "%{h*} \
  29. + --as-needed \
  30. %{static:-Bstatic} \
  31. %{shared:-shared} \
  32. %{symbolic:-Bsymbolic} \
  33. diff --git a/gcc/config/alpha/linux-elf.h b/gcc/config/alpha/linux-elf.h
  34. index e25fcac3c59..22b2f7b0d14 100644
  35. --- a/gcc/config/alpha/linux-elf.h
  36. +++ b/gcc/config/alpha/linux-elf.h
  37. @@ -37,7 +37,7 @@ along with GCC; see the file COPYING3. If not see
  38. #define ELF_DYNAMIC_LINKER GNU_USER_DYNAMIC_LINKER
  39. -#define LINK_SPEC "-m elf64alpha %{G*} %{relax:-relax} \
  40. +#define LINK_SPEC "-m elf64alpha --as-needed %{G*} %{relax:-relax} \
  41. %{O*:-O3} %{!O*:-O1} \
  42. %{shared:-shared} \
  43. %{!shared: \
  44. diff --git a/gcc/config/arm/linux-elf.h b/gcc/config/arm/linux-elf.h
  45. index 0ec3aa53189..f8bda84de2a 100644
  46. --- a/gcc/config/arm/linux-elf.h
  47. +++ b/gcc/config/arm/linux-elf.h
  48. @@ -70,6 +70,7 @@
  49. %{rdynamic:-export-dynamic} \
  50. %{!shared:-dynamic-linker " GNU_USER_DYNAMIC_LINKER "}} \
  51. -X \
  52. + --as-needed \
  53. %{mbig-endian:-EB} %{mlittle-endian:-EL}" \
  54. SUBTARGET_EXTRA_LINK_SPEC
  55. diff --git a/gcc/config/gnu-user.h b/gcc/config/gnu-user.h
  56. index ff2e880b1fa..7f30c363e4a 100644
  57. --- a/gcc/config/gnu-user.h
  58. +++ b/gcc/config/gnu-user.h
  59. @@ -136,17 +136,17 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  60. #define LIBASAN_EARLY_SPEC "%{!shared:libasan_preinit%O%s} " \
  61. "%{static-libasan:%{!shared:" \
  62. LD_STATIC_OPTION " --whole-archive -lasan --no-whole-archive " \
  63. - LD_DYNAMIC_OPTION "}}%{!static-libasan:-lasan}"
  64. + LD_DYNAMIC_OPTION "}}%{!static-libasan:%{!fuse-ld=gold:--push-state} --no-as-needed -lasan %{fuse-ld=gold:--as-needed;:--pop-state}}"
  65. #undef LIBTSAN_EARLY_SPEC
  66. #define LIBTSAN_EARLY_SPEC "%{!shared:libtsan_preinit%O%s} " \
  67. "%{static-libtsan:%{!shared:" \
  68. LD_STATIC_OPTION " --whole-archive -ltsan --no-whole-archive " \
  69. - LD_DYNAMIC_OPTION "}}%{!static-libtsan:-ltsan}"
  70. + LD_DYNAMIC_OPTION "}}%{!static-libtsan:%{!fuse-ld=gold:--push-state} --no-as-needed -ltsan %{fuse-ld=gold:--as-needed;:--pop-state}}"
  71. #undef LIBLSAN_EARLY_SPEC
  72. #define LIBLSAN_EARLY_SPEC "%{!shared:liblsan_preinit%O%s} " \
  73. "%{static-liblsan:%{!shared:" \
  74. LD_STATIC_OPTION " --whole-archive -llsan --no-whole-archive " \
  75. - LD_DYNAMIC_OPTION "}}%{!static-liblsan:-llsan}"
  76. + LD_DYNAMIC_OPTION "}}%{!static-liblsan:%{!fuse-ld=gold:--push-state} --no-as-needed -llsan %{fuse-ld=gold:--as-needed;:--pop-state}}"
  77. #endif
  78. #undef TARGET_F951_OPTIONS
  79. diff --git a/gcc/config/i386/gnu-user.h b/gcc/config/i386/gnu-user.h
  80. index ae4aa844f02..54b91c0492a 100644
  81. --- a/gcc/config/i386/gnu-user.h
  82. +++ b/gcc/config/i386/gnu-user.h
  83. @@ -74,7 +74,7 @@ along with GCC; see the file COPYING3. If not see
  84. { "link_emulation", GNU_USER_LINK_EMULATION },\
  85. { "dynamic_linker", GNU_USER_DYNAMIC_LINKER }
  86. -#define GNU_USER_TARGET_LINK_SPEC "-m %(link_emulation) %{shared:-shared} \
  87. +#define GNU_USER_TARGET_LINK_SPEC "-m %(link_emulation) --as-needed %{shared:-shared} \
  88. %{!shared: \
  89. %{!static: \
  90. %{!static-pie: \
  91. diff --git a/gcc/config/i386/gnu-user64.h b/gcc/config/i386/gnu-user64.h
  92. index 785c68220b8..21dd66b412a 100644
  93. --- a/gcc/config/i386/gnu-user64.h
  94. +++ b/gcc/config/i386/gnu-user64.h
  95. @@ -56,6 +56,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  96. "%{" SPEC_64 ":-m " GNU_USER_LINK_EMULATION64 "} \
  97. %{" SPEC_32 ":-m " GNU_USER_LINK_EMULATION32 "} \
  98. %{" SPEC_X32 ":-m " GNU_USER_LINK_EMULATIONX32 "} \
  99. + --as-needed \
  100. %{shared:-shared} \
  101. %{!shared: \
  102. %{!static: \
  103. diff --git a/gcc/config/ia64/linux.h b/gcc/config/ia64/linux.h
  104. index ee38e218aec..0325971d818 100644
  105. --- a/gcc/config/ia64/linux.h
  106. +++ b/gcc/config/ia64/linux.h
  107. @@ -58,7 +58,7 @@ do { \
  108. #define GLIBC_DYNAMIC_LINKER "/lib/ld-linux-ia64.so.2"
  109. #undef LINK_SPEC
  110. -#define LINK_SPEC "\
  111. +#define LINK_SPEC " --as-needed \
  112. %{shared:-shared} \
  113. %{!shared: \
  114. %{!static: \
  115. diff --git a/gcc/config/mips/gnu-user.h b/gcc/config/mips/gnu-user.h
  116. index 5d0782100bb..4be5fbb2191 100644
  117. --- a/gcc/config/mips/gnu-user.h
  118. +++ b/gcc/config/mips/gnu-user.h
  119. @@ -55,6 +55,7 @@ along with GCC; see the file COPYING3. If not see
  120. #undef GNU_USER_TARGET_LINK_SPEC
  121. #define GNU_USER_TARGET_LINK_SPEC "\
  122. %{G*} %{EB} %{EL} %{mips*} %{shared} \
  123. + -as-needed \
  124. %{!shared: \
  125. %{!static: \
  126. %{rdynamic:-export-dynamic} \
  127. diff --git a/gcc/config/riscv/linux.h b/gcc/config/riscv/linux.h
  128. index 4afef7c228c..ba1a6e60e05 100644
  129. --- a/gcc/config/riscv/linux.h
  130. +++ b/gcc/config/riscv/linux.h
  131. @@ -58,6 +58,7 @@ along with GCC; see the file COPYING3. If not see
  132. "%{mabi=ilp32:_ilp32}"
  133. #define LINK_SPEC "\
  134. +-as-needed \
  135. -melf" XLEN_SPEC "lriscv" LD_EMUL_SUFFIX " \
  136. %{mno-relax:--no-relax} \
  137. %{shared} \
  138. diff --git a/gcc/config/rs6000/linux64.h b/gcc/config/rs6000/linux64.h
  139. index 2ded3301282..a1e44ddcc0d 100644
  140. --- a/gcc/config/rs6000/linux64.h
  141. +++ b/gcc/config/rs6000/linux64.h
  142. @@ -457,13 +457,13 @@ extern int dot_symbols;
  143. " -m elf64ppc")
  144. #endif
  145. -#define LINK_OS_LINUX_SPEC32 LINK_OS_LINUX_EMUL32 " %{!shared: %{!static: \
  146. +#define LINK_OS_LINUX_SPEC32 LINK_OS_LINUX_EMUL32 " --as-needed %{!shared: %{!static: \
  147. %{!static-pie: \
  148. %{rdynamic:-export-dynamic} \
  149. -dynamic-linker " GNU_USER_DYNAMIC_LINKER32 "}}} \
  150. %(link_os_extra_spec32)"
  151. -#define LINK_OS_LINUX_SPEC64 LINK_OS_LINUX_EMUL64 " %{!shared: %{!static: \
  152. +#define LINK_OS_LINUX_SPEC64 LINK_OS_LINUX_EMUL64 " --as-needed %{!shared: %{!static: \
  153. %{!static-pie: \
  154. %{rdynamic:-export-dynamic} \
  155. -dynamic-linker " GNU_USER_DYNAMIC_LINKER64 "}}} \
  156. diff --git a/gcc/config/rs6000/sysv4.h b/gcc/config/rs6000/sysv4.h
  157. index 9ade72114e9..9490b84a153 100644
  158. --- a/gcc/config/rs6000/sysv4.h
  159. +++ b/gcc/config/rs6000/sysv4.h
  160. @@ -789,7 +789,7 @@ GNU_USER_TARGET_CC1_SPEC
  161. #define GNU_USER_DYNAMIC_LINKER GLIBC_DYNAMIC_LINKER
  162. #endif
  163. -#define LINK_OS_LINUX_SPEC "-m elf32ppclinux %{!shared: %{!static: \
  164. +#define LINK_OS_LINUX_SPEC "-m elf32ppclinux --as-needed %{!shared: %{!static: \
  165. %{rdynamic:-export-dynamic} \
  166. -dynamic-linker " GNU_USER_DYNAMIC_LINKER "}}"
  167. diff --git a/gcc/config/s390/linux.h b/gcc/config/s390/linux.h
  168. index 6919b468f06..56d52d41b31 100644
  169. --- a/gcc/config/s390/linux.h
  170. +++ b/gcc/config/s390/linux.h
  171. @@ -82,7 +82,7 @@ along with GCC; see the file COPYING3. If not see
  172. #undef LINK_SPEC
  173. #define LINK_SPEC \
  174. - "%{m31:-m elf_s390}%{m64:-m elf64_s390} \
  175. + "%{m31:-m elf_s390}%{m64:-m elf64_s390} --as-needed \
  176. %{shared:-shared} \
  177. %{!shared: \
  178. %{static:-static} \
  179. diff --git a/gcc/config/sparc/linux.h b/gcc/config/sparc/linux.h
  180. index 63853e60c03..de6d8675456 100644
  181. --- a/gcc/config/sparc/linux.h
  182. +++ b/gcc/config/sparc/linux.h
  183. @@ -81,7 +81,7 @@ extern const char *host_detect_local_cpu (int argc, const char **argv);
  184. #define GLIBC_DYNAMIC_LINKER "/lib/ld-linux.so.2"
  185. #undef LINK_SPEC
  186. -#define LINK_SPEC "-m elf32_sparc %{shared:-shared} \
  187. +#define LINK_SPEC "-m elf32_sparc --as-needed %{shared:-shared} \
  188. %{!mno-relax:%{!r:-relax}} \
  189. %{!shared: \
  190. %{!static: \
  191. diff --git a/gcc/gcc.c b/gcc/gcc.c
  192. index 625c9ab7902..7aec9d3a016 100644
  193. --- a/gcc/gcc.c
  194. +++ b/gcc/gcc.c
  195. @@ -695,8 +695,11 @@ proper position among the other output files. */
  196. #ifdef LIBASAN_EARLY_SPEC
  197. #define LIBASAN_SPEC STATIC_LIBASAN_LIBS
  198. #elif defined(HAVE_LD_STATIC_DYNAMIC)
  199. -#define LIBASAN_SPEC "%{static-libasan:" LD_STATIC_OPTION \
  200. - "} -lasan %{static-libasan:" LD_DYNAMIC_OPTION "}" \
  201. +#define LIBASAN_SPEC "%{static-libasan:" LD_STATIC_OPTION "}" \
  202. + " %{!static-libasan:%{!fuse-ld=gold:--push-state }--no-as-needed}" \
  203. + " -lasan " \
  204. + " %{static-libasan:" LD_DYNAMIC_OPTION "}" \
  205. + " %{!static-libasan:%{fuse-ld=gold:--as-needed;:--pop-state}}" \
  206. STATIC_LIBASAN_LIBS
  207. #else
  208. #define LIBASAN_SPEC "-lasan" STATIC_LIBASAN_LIBS
  209. @@ -713,8 +716,11 @@ proper position among the other output files. */
  210. #ifdef LIBTSAN_EARLY_SPEC
  211. #define LIBTSAN_SPEC STATIC_LIBTSAN_LIBS
  212. #elif defined(HAVE_LD_STATIC_DYNAMIC)
  213. -#define LIBTSAN_SPEC "%{static-libtsan:" LD_STATIC_OPTION \
  214. - "} -ltsan %{static-libtsan:" LD_DYNAMIC_OPTION "}" \
  215. +#define LIBTSAN_SPEC "%{static-libtsan:" LD_STATIC_OPTION "}" \
  216. + " %{!static-libtsan:%{!fuse-ld=gold:--push-state }--no-as-needed}" \
  217. + " -ltsan " \
  218. + " %{static-libtsan:" LD_DYNAMIC_OPTION "}" \
  219. + " %{!static-libtsan:%{fuse-ld=gold:--as-needed;:--pop-state}}" \
  220. STATIC_LIBTSAN_LIBS
  221. #else
  222. #define LIBTSAN_SPEC "-ltsan" STATIC_LIBTSAN_LIBS
  223. @@ -731,8 +737,11 @@ proper position among the other output files. */
  224. #ifdef LIBLSAN_EARLY_SPEC
  225. #define LIBLSAN_SPEC STATIC_LIBLSAN_LIBS
  226. #elif defined(HAVE_LD_STATIC_DYNAMIC)
  227. -#define LIBLSAN_SPEC "%{static-liblsan:" LD_STATIC_OPTION \
  228. - "} -llsan %{static-liblsan:" LD_DYNAMIC_OPTION "}" \
  229. +#define LIBLSAN_SPEC "%{static-liblsan:" LD_STATIC_OPTION "}" \
  230. + " %{!static-liblsan:%{!fuse-ld=gold:--push-state }--no-as-needed}" \
  231. + " -llsan " \
  232. + " %{static-liblsan:" LD_DYNAMIC_OPTION "}" \
  233. + " %{!static-liblsan:%{fuse-ld=gold:--as-needed;:--pop-state}}" \
  234. STATIC_LIBLSAN_LIBS
  235. #else
  236. #define LIBLSAN_SPEC "-llsan" STATIC_LIBLSAN_LIBS
  237. @@ -747,8 +756,11 @@ proper position among the other output files. */
  238. #define STATIC_LIBUBSAN_LIBS \
  239. " %{static-libubsan|static:%:include(libsanitizer.spec)%(link_libubsan)}"
  240. #ifdef HAVE_LD_STATIC_DYNAMIC
  241. -#define LIBUBSAN_SPEC "%{static-libubsan:" LD_STATIC_OPTION \
  242. - "} -lubsan %{static-libubsan:" LD_DYNAMIC_OPTION "}" \
  243. +#define LIBUBSAN_SPEC "%{static-libubsan:" LD_STATIC_OPTION "}" \
  244. + " %{!static-libubsan:%{!fuse-ld=gold:--push-state }--no-as-needed}" \
  245. + " -lubsan " \
  246. + " %{static-libubsan:" LD_DYNAMIC_OPTION "}" \
  247. + " %{!static-libubsan:%{fuse-ld=gold:--as-needed;:--pop-state}}" \
  248. STATIC_LIBUBSAN_LIBS
  249. #else
  250. #define LIBUBSAN_SPEC "-lubsan" STATIC_LIBUBSAN_LIBS
  251. --
  252. 2.29.2