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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. From 8de5d46c530ceca9f4add46d5cae3cd52c38eba3 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 7f2529a2a1d..b097e1688aa 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 c1dae8ca2cf..a87dc740171 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 0c1c4e70b6b..8b581afacdd 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 5ebbf42a13d..902378e1bad 100644
  57. --- a/gcc/config/gnu-user.h
  58. +++ b/gcc/config/gnu-user.h
  59. @@ -136,7 +136,7 @@ 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 LIBHWASAN_EARLY_SPEC
  66. #define LIBHWASAN_EARLY_SPEC "%{static-libhwasan:%{!shared:" \
  67. LD_STATIC_OPTION " --whole-archive -lhwasan --no-whole-archive " \
  68. @@ -145,12 +145,12 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  69. #define LIBTSAN_EARLY_SPEC "%{!shared:libtsan_preinit%O%s} " \
  70. "%{static-libtsan:%{!shared:" \
  71. LD_STATIC_OPTION " --whole-archive -ltsan --no-whole-archive " \
  72. - LD_DYNAMIC_OPTION "}}%{!static-libtsan:-ltsan}"
  73. + LD_DYNAMIC_OPTION "}}%{!static-libtsan:%{!fuse-ld=gold:--push-state} --no-as-needed -ltsan %{fuse-ld=gold:--as-needed;:--pop-state}}"
  74. #undef LIBLSAN_EARLY_SPEC
  75. #define LIBLSAN_EARLY_SPEC "%{!shared:liblsan_preinit%O%s} " \
  76. "%{static-liblsan:%{!shared:" \
  77. LD_STATIC_OPTION " --whole-archive -llsan --no-whole-archive " \
  78. - LD_DYNAMIC_OPTION "}}%{!static-liblsan:-llsan}"
  79. + LD_DYNAMIC_OPTION "}}%{!static-liblsan:%{!fuse-ld=gold:--push-state} --no-as-needed -llsan %{fuse-ld=gold:--as-needed;:--pop-state}}"
  80. #endif
  81. #undef TARGET_F951_OPTIONS
  82. diff --git a/gcc/config/i386/gnu-user.h b/gcc/config/i386/gnu-user.h
  83. index a23e7ab3eb7..12ac8cb93a9 100644
  84. --- a/gcc/config/i386/gnu-user.h
  85. +++ b/gcc/config/i386/gnu-user.h
  86. @@ -68,7 +68,7 @@ along with GCC; see the file COPYING3. If not see
  87. { "link_emulation", GNU_USER_LINK_EMULATION },\
  88. { "dynamic_linker", GNU_USER_DYNAMIC_LINKER }
  89. -#define GNU_USER_TARGET_LINK_SPEC "-m %(link_emulation) %{shared:-shared} \
  90. +#define GNU_USER_TARGET_LINK_SPEC "-m %(link_emulation) --as-needed %{shared:-shared} \
  91. %{!shared: \
  92. %{!static: \
  93. %{!static-pie: \
  94. diff --git a/gcc/config/i386/gnu-user64.h b/gcc/config/i386/gnu-user64.h
  95. index f3e25b1412a..712b0f1c30e 100644
  96. --- a/gcc/config/i386/gnu-user64.h
  97. +++ b/gcc/config/i386/gnu-user64.h
  98. @@ -56,6 +56,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  99. "%{" SPEC_64 ":-m " GNU_USER_LINK_EMULATION64 "} \
  100. %{" SPEC_32 ":-m " GNU_USER_LINK_EMULATION32 "} \
  101. %{" SPEC_X32 ":-m " GNU_USER_LINK_EMULATIONX32 "} \
  102. + --as-needed \
  103. %{shared:-shared} \
  104. %{!shared: \
  105. %{!static: \
  106. diff --git a/gcc/config/ia64/linux.h b/gcc/config/ia64/linux.h
  107. index ac40c3dfd3f..6e19e25c35a 100644
  108. --- a/gcc/config/ia64/linux.h
  109. +++ b/gcc/config/ia64/linux.h
  110. @@ -58,7 +58,7 @@ do { \
  111. #define GLIBC_DYNAMIC_LINKER "/lib/ld-linux-ia64.so.2"
  112. #undef LINK_SPEC
  113. -#define LINK_SPEC "\
  114. +#define LINK_SPEC " --as-needed \
  115. %{shared:-shared} \
  116. %{!shared: \
  117. %{!static: \
  118. diff --git a/gcc/config/mips/gnu-user.h b/gcc/config/mips/gnu-user.h
  119. index a390bcece83..be0ee289367 100644
  120. --- a/gcc/config/mips/gnu-user.h
  121. +++ b/gcc/config/mips/gnu-user.h
  122. @@ -55,6 +55,7 @@ along with GCC; see the file COPYING3. If not see
  123. #undef GNU_USER_TARGET_LINK_SPEC
  124. #define GNU_USER_TARGET_LINK_SPEC "\
  125. %{G*} %{EB} %{EL} %{mips*} %{shared} \
  126. + -as-needed \
  127. %{!shared: \
  128. %{!static: \
  129. %{rdynamic:-export-dynamic} \
  130. diff --git a/gcc/config/riscv/linux.h b/gcc/config/riscv/linux.h
  131. index fce5b896e6e..ee29979dd34 100644
  132. --- a/gcc/config/riscv/linux.h
  133. +++ b/gcc/config/riscv/linux.h
  134. @@ -58,6 +58,7 @@ along with GCC; see the file COPYING3. If not see
  135. "%{mabi=ilp32:_ilp32}"
  136. #define LINK_SPEC "\
  137. +-as-needed \
  138. -melf" XLEN_SPEC DEFAULT_ENDIAN_SPEC "riscv" LD_EMUL_SUFFIX " \
  139. %{mno-relax:--no-relax} \
  140. %{mbig-endian:-EB} \
  141. diff --git a/gcc/config/rs6000/linux64.h b/gcc/config/rs6000/linux64.h
  142. index e3f2cd254f6..43499ed29cf 100644
  143. --- a/gcc/config/rs6000/linux64.h
  144. +++ b/gcc/config/rs6000/linux64.h
  145. @@ -374,13 +374,13 @@ extern int dot_symbols;
  146. " -m elf64ppc")
  147. #endif
  148. -#define LINK_OS_LINUX_SPEC32 LINK_OS_LINUX_EMUL32 " %{!shared: %{!static: \
  149. +#define LINK_OS_LINUX_SPEC32 LINK_OS_LINUX_EMUL32 " --as-needed %{!shared: %{!static: \
  150. %{!static-pie: \
  151. %{rdynamic:-export-dynamic} \
  152. -dynamic-linker " GNU_USER_DYNAMIC_LINKER32 "}}} \
  153. %(link_os_extra_spec32)"
  154. -#define LINK_OS_LINUX_SPEC64 LINK_OS_LINUX_EMUL64 " %{!shared: %{!static: \
  155. +#define LINK_OS_LINUX_SPEC64 LINK_OS_LINUX_EMUL64 " --as-needed %{!shared: %{!static: \
  156. %{!static-pie: \
  157. %{rdynamic:-export-dynamic} \
  158. -dynamic-linker " GNU_USER_DYNAMIC_LINKER64 "}}} \
  159. diff --git a/gcc/config/rs6000/sysv4.h b/gcc/config/rs6000/sysv4.h
  160. index 510abe169c5..3c1f3a8ec5d 100644
  161. --- a/gcc/config/rs6000/sysv4.h
  162. +++ b/gcc/config/rs6000/sysv4.h
  163. @@ -789,7 +789,7 @@ GNU_USER_TARGET_CC1_SPEC
  164. #define GNU_USER_DYNAMIC_LINKER GLIBC_DYNAMIC_LINKER
  165. #endif
  166. -#define LINK_OS_LINUX_SPEC "-m elf32ppclinux %{!shared: %{!static: \
  167. +#define LINK_OS_LINUX_SPEC "-m elf32ppclinux --as-needed %{!shared: %{!static: \
  168. %{rdynamic:-export-dynamic} \
  169. -dynamic-linker " GNU_USER_DYNAMIC_LINKER "}}"
  170. diff --git a/gcc/config/s390/linux.h b/gcc/config/s390/linux.h
  171. index daa48fdfc2b..2fcfc5d770f 100644
  172. --- a/gcc/config/s390/linux.h
  173. +++ b/gcc/config/s390/linux.h
  174. @@ -82,7 +82,7 @@ along with GCC; see the file COPYING3. If not see
  175. #undef LINK_SPEC
  176. #define LINK_SPEC \
  177. - "%{m31:-m elf_s390}%{m64:-m elf64_s390} \
  178. + "%{m31:-m elf_s390}%{m64:-m elf64_s390} --as-needed \
  179. %{shared:-shared} \
  180. %{!shared: \
  181. %{static:-static} \
  182. diff --git a/gcc/config/sparc/linux.h b/gcc/config/sparc/linux.h
  183. index 2550d7ee8f0..7596d0b2757 100644
  184. --- a/gcc/config/sparc/linux.h
  185. +++ b/gcc/config/sparc/linux.h
  186. @@ -81,7 +81,7 @@ extern const char *host_detect_local_cpu (int argc, const char **argv);
  187. #define GLIBC_DYNAMIC_LINKER "/lib/ld-linux.so.2"
  188. #undef LINK_SPEC
  189. -#define LINK_SPEC "-m elf32_sparc %{shared:-shared} \
  190. +#define LINK_SPEC "-m elf32_sparc --as-needed %{shared:-shared} \
  191. %{!mno-relax:%{!r:-relax}} \
  192. %{!shared: \
  193. %{!static: \
  194. diff --git a/gcc/gcc.c b/gcc/gcc.c
  195. index faf9ce76b3d..1ad5da6d769 100644
  196. --- a/gcc/gcc.c
  197. +++ b/gcc/gcc.c
  198. @@ -737,8 +737,11 @@ proper position among the other output files. */
  199. #ifdef LIBASAN_EARLY_SPEC
  200. #define LIBASAN_SPEC STATIC_LIBASAN_LIBS
  201. #elif defined(HAVE_LD_STATIC_DYNAMIC)
  202. -#define LIBASAN_SPEC "%{static-libasan:" LD_STATIC_OPTION \
  203. - "} -lasan %{static-libasan:" LD_DYNAMIC_OPTION "}" \
  204. +#define LIBASAN_SPEC "%{static-libasan:" LD_STATIC_OPTION "}" \
  205. + " %{!static-libasan:%{!fuse-ld=gold:--push-state }--no-as-needed}" \
  206. + " -lasan " \
  207. + " %{static-libasan:" LD_DYNAMIC_OPTION "}" \
  208. + " %{!static-libasan:%{fuse-ld=gold:--as-needed;:--pop-state}}" \
  209. STATIC_LIBASAN_LIBS
  210. #else
  211. #define LIBASAN_SPEC "-lasan" STATIC_LIBASAN_LIBS
  212. @@ -773,8 +776,11 @@ proper position among the other output files. */
  213. #ifdef LIBTSAN_EARLY_SPEC
  214. #define LIBTSAN_SPEC STATIC_LIBTSAN_LIBS
  215. #elif defined(HAVE_LD_STATIC_DYNAMIC)
  216. -#define LIBTSAN_SPEC "%{static-libtsan:" LD_STATIC_OPTION \
  217. - "} -ltsan %{static-libtsan:" LD_DYNAMIC_OPTION "}" \
  218. +#define LIBTSAN_SPEC "%{static-libtsan:" LD_STATIC_OPTION "}" \
  219. + " %{!static-libtsan:%{!fuse-ld=gold:--push-state }--no-as-needed}" \
  220. + " -ltsan " \
  221. + " %{static-libtsan:" LD_DYNAMIC_OPTION "}" \
  222. + " %{!static-libtsan:%{fuse-ld=gold:--as-needed;:--pop-state}}" \
  223. STATIC_LIBTSAN_LIBS
  224. #else
  225. #define LIBTSAN_SPEC "-ltsan" STATIC_LIBTSAN_LIBS
  226. @@ -791,8 +797,11 @@ proper position among the other output files. */
  227. #ifdef LIBLSAN_EARLY_SPEC
  228. #define LIBLSAN_SPEC STATIC_LIBLSAN_LIBS
  229. #elif defined(HAVE_LD_STATIC_DYNAMIC)
  230. -#define LIBLSAN_SPEC "%{static-liblsan:" LD_STATIC_OPTION \
  231. - "} -llsan %{static-liblsan:" LD_DYNAMIC_OPTION "}" \
  232. +#define LIBLSAN_SPEC "%{static-liblsan:" LD_STATIC_OPTION "}" \
  233. + " %{!static-liblsan:%{!fuse-ld=gold:--push-state }--no-as-needed}" \
  234. + " -llsan " \
  235. + " %{static-liblsan:" LD_DYNAMIC_OPTION "}" \
  236. + " %{!static-liblsan:%{fuse-ld=gold:--as-needed;:--pop-state}}" \
  237. STATIC_LIBLSAN_LIBS
  238. #else
  239. #define LIBLSAN_SPEC "-llsan" STATIC_LIBLSAN_LIBS
  240. @@ -807,8 +816,11 @@ proper position among the other output files. */
  241. #define STATIC_LIBUBSAN_LIBS \
  242. " %{static-libubsan|static:%:include(libsanitizer.spec)%(link_libubsan)}"
  243. #ifdef HAVE_LD_STATIC_DYNAMIC
  244. -#define LIBUBSAN_SPEC "%{static-libubsan:" LD_STATIC_OPTION \
  245. - "} -lubsan %{static-libubsan:" LD_DYNAMIC_OPTION "}" \
  246. +#define LIBUBSAN_SPEC "%{static-libubsan:" LD_STATIC_OPTION "}" \
  247. + " %{!static-libubsan:%{!fuse-ld=gold:--push-state }--no-as-needed}" \
  248. + " -lubsan " \
  249. + " %{static-libubsan:" LD_DYNAMIC_OPTION "}" \
  250. + " %{!static-libubsan:%{fuse-ld=gold:--as-needed;:--pop-state}}" \
  251. STATIC_LIBUBSAN_LIBS
  252. #else
  253. #define LIBUBSAN_SPEC "-lubsan" STATIC_LIBUBSAN_LIBS
  254. --
  255. 2.35.1