0005-On-linux-targets-pass-as-needed-by-default-to-the-li.patch (10416B)
1 From: Ariadne Conill <ariadne@dereferenced.org> 2 From: Olsken (updated patches) 3 Date: Sat, 21 Aug 2021 08:51:52 +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 diff --git a/gcc/config/aarch64/aarch64-linux.h b/gcc/config/aarch64/aarch64-linux.h 8 index 7f2529a2a..b097e1688 100644 9 --- a/gcc/config/aarch64/aarch64-linux.h 10 +++ b/gcc/config/aarch64/aarch64-linux.h 11 @@ -35,6 +35,7 @@ 12 #define CPP_SPEC "%{pthread:-D_REENTRANT}" 13 14 #define LINUX_TARGET_LINK_SPEC "%{h*} \ 15 + --as-needed \ 16 %{static:-Bstatic} \ 17 %{shared:-shared} \ 18 %{symbolic:-Bsymbolic} \ 19 diff --git a/gcc/config/alpha/linux-elf.h b/gcc/config/alpha/linux-elf.h 20 index c1dae8ca2..a87dc7401 100644 21 --- a/gcc/config/alpha/linux-elf.h 22 +++ b/gcc/config/alpha/linux-elf.h 23 @@ -37,7 +37,7 @@ along with GCC; see the file COPYING3. If not see 24 25 #define ELF_DYNAMIC_LINKER GNU_USER_DYNAMIC_LINKER 26 27 -#define LINK_SPEC "-m elf64alpha %{G*} %{relax:-relax} \ 28 +#define LINK_SPEC "-m elf64alpha --as-needed %{G*} %{relax:-relax} \ 29 %{O*:-O3} %{!O*:-O1} \ 30 %{shared:-shared} \ 31 %{!shared: \ 32 diff --git a/gcc/config/arm/linux-elf.h b/gcc/config/arm/linux-elf.h 33 index 0c1c4e70b..8b581afac 100644 34 --- a/gcc/config/arm/linux-elf.h 35 +++ b/gcc/config/arm/linux-elf.h 36 @@ -70,6 +70,7 @@ 37 %{rdynamic:-export-dynamic} \ 38 %{!shared:-dynamic-linker " GNU_USER_DYNAMIC_LINKER "}} \ 39 -X \ 40 + --as-needed \ 41 %{mbig-endian:-EB} %{mlittle-endian:-EL}" \ 42 SUBTARGET_EXTRA_LINK_SPEC 43 44 diff --git a/gcc/config/gnu-user.h b/gcc/config/gnu-user.h 45 index 5ebbf42a1..e1a04e9fd 100644 46 --- a/gcc/config/gnu-user.h 47 +++ b/gcc/config/gnu-user.h 48 @@ -136,21 +136,21 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 49 #define LIBASAN_EARLY_SPEC "%{!shared:libasan_preinit%O%s} " \ 50 "%{static-libasan:%{!shared:" \ 51 LD_STATIC_OPTION " --whole-archive -lasan --no-whole-archive " \ 52 - LD_DYNAMIC_OPTION "}}%{!static-libasan:-lasan}" 53 + LD_DYNAMIC_OPTION "}}%{!static-libasan:%{!fuse-ld=gold:--push-state} --no-as-needed -lasan %{fuse-ld=gold:--as-needed;:--pop-state}}" 54 #undef LIBHWASAN_EARLY_SPEC 55 #define LIBHWASAN_EARLY_SPEC "%{static-libhwasan:%{!shared:" \ 56 LD_STATIC_OPTION " --whole-archive -lhwasan --no-whole-archive " \ 57 - LD_DYNAMIC_OPTION "}}%{!static-libhwasan:-lhwasan}" 58 + LD_DYNAMIC_OPTION "}}%{!static-libhwasan:%{!fuse-ld=gold:--push-state} --no-as-needed -lhwasan %{fuse-ld=gold:--as-needed;:--pop-state}}" 59 #undef LIBTSAN_EARLY_SPEC 60 #define LIBTSAN_EARLY_SPEC "%{!shared:libtsan_preinit%O%s} " \ 61 "%{static-libtsan:%{!shared:" \ 62 LD_STATIC_OPTION " --whole-archive -ltsan --no-whole-archive " \ 63 - LD_DYNAMIC_OPTION "}}%{!static-libtsan:-ltsan}" 64 + LD_DYNAMIC_OPTION "}}%{!static-libtsan:%{!fuse-ld=gold:--push-state} --no-as-needed -ltsan %{fuse-ld=gold:--as-needed;:--pop-state}}" 65 #undef LIBLSAN_EARLY_SPEC 66 #define LIBLSAN_EARLY_SPEC "%{!shared:liblsan_preinit%O%s} " \ 67 "%{static-liblsan:%{!shared:" \ 68 LD_STATIC_OPTION " --whole-archive -llsan --no-whole-archive " \ 69 - LD_DYNAMIC_OPTION "}}%{!static-liblsan:-llsan}" 70 + LD_DYNAMIC_OPTION "}}%{!static-liblsan:%{!fuse-ld=gold:--push-state} --no-as-needed -llsan %{fuse-ld=gold:--as-needed;:--pop-state}}" 71 #endif 72 73 #undef TARGET_F951_OPTIONS 74 diff --git a/gcc/config/i386/gnu-user.h b/gcc/config/i386/gnu-user.h 75 index a23e7ab3e..12ac8cb93 100644 76 --- a/gcc/config/i386/gnu-user.h 77 +++ b/gcc/config/i386/gnu-user.h 78 @@ -68,7 +68,7 @@ along with GCC; see the file COPYING3. If not see 79 { "link_emulation", GNU_USER_LINK_EMULATION },\ 80 { "dynamic_linker", GNU_USER_DYNAMIC_LINKER } 81 82 -#define GNU_USER_TARGET_LINK_SPEC "-m %(link_emulation) %{shared:-shared} \ 83 +#define GNU_USER_TARGET_LINK_SPEC "-m %(link_emulation) --as-needed %{shared:-shared} \ 84 %{!shared: \ 85 %{!static: \ 86 %{!static-pie: \ 87 diff --git a/gcc/config/i386/gnu-user64.h b/gcc/config/i386/gnu-user64.h 88 index f3e25b141..712b0f1c3 100644 89 --- a/gcc/config/i386/gnu-user64.h 90 +++ b/gcc/config/i386/gnu-user64.h 91 @@ -56,6 +56,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 92 "%{" SPEC_64 ":-m " GNU_USER_LINK_EMULATION64 "} \ 93 %{" SPEC_32 ":-m " GNU_USER_LINK_EMULATION32 "} \ 94 %{" SPEC_X32 ":-m " GNU_USER_LINK_EMULATIONX32 "} \ 95 + --as-needed \ 96 %{shared:-shared} \ 97 %{!shared: \ 98 %{!static: \ 99 diff --git a/gcc/config/ia64/linux.h b/gcc/config/ia64/linux.h 100 index ac40c3dfd..6e19e25c3 100644 101 --- a/gcc/config/ia64/linux.h 102 +++ b/gcc/config/ia64/linux.h 103 @@ -58,7 +58,7 @@ do { \ 104 #define GLIBC_DYNAMIC_LINKER "/lib/ld-linux-ia64.so.2" 105 106 #undef LINK_SPEC 107 -#define LINK_SPEC "\ 108 +#define LINK_SPEC " --as-needed \ 109 %{shared:-shared} \ 110 %{!shared: \ 111 %{!static: \ 112 diff --git a/gcc/config/mips/gnu-user.h b/gcc/config/mips/gnu-user.h 113 index a390bcece..be0ee2893 100644 114 --- a/gcc/config/mips/gnu-user.h 115 +++ b/gcc/config/mips/gnu-user.h 116 @@ -55,6 +55,7 @@ along with GCC; see the file COPYING3. If not see 117 #undef GNU_USER_TARGET_LINK_SPEC 118 #define GNU_USER_TARGET_LINK_SPEC "\ 119 %{G*} %{EB} %{EL} %{mips*} %{shared} \ 120 + -as-needed \ 121 %{!shared: \ 122 %{!static: \ 123 %{rdynamic:-export-dynamic} \ 124 diff --git a/gcc/config/rs6000/linux64.h b/gcc/config/rs6000/linux64.h 125 index e3f2cd254..43499ed29 100644 126 --- a/gcc/config/rs6000/linux64.h 127 +++ b/gcc/config/rs6000/linux64.h 128 @@ -374,13 +374,13 @@ extern int dot_symbols; 129 " -m elf64ppc") 130 #endif 131 132 -#define LINK_OS_LINUX_SPEC32 LINK_OS_LINUX_EMUL32 " %{!shared: %{!static: \ 133 +#define LINK_OS_LINUX_SPEC32 LINK_OS_LINUX_EMUL32 " --as-needed %{!shared: %{!static: \ 134 %{!static-pie: \ 135 %{rdynamic:-export-dynamic} \ 136 -dynamic-linker " GNU_USER_DYNAMIC_LINKER32 "}}} \ 137 %(link_os_extra_spec32)" 138 139 -#define LINK_OS_LINUX_SPEC64 LINK_OS_LINUX_EMUL64 " %{!shared: %{!static: \ 140 +#define LINK_OS_LINUX_SPEC64 LINK_OS_LINUX_EMUL64 " --as-needed %{!shared: %{!static: \ 141 %{!static-pie: \ 142 %{rdynamic:-export-dynamic} \ 143 -dynamic-linker " GNU_USER_DYNAMIC_LINKER64 "}}} \ 144 diff --git a/gcc/config/rs6000/sysv4.h b/gcc/config/rs6000/sysv4.h 145 index 510abe169..3c1f3a8ec 100644 146 --- a/gcc/config/rs6000/sysv4.h 147 +++ b/gcc/config/rs6000/sysv4.h 148 @@ -789,7 +789,7 @@ GNU_USER_TARGET_CC1_SPEC 149 #define GNU_USER_DYNAMIC_LINKER GLIBC_DYNAMIC_LINKER 150 #endif 151 152 -#define LINK_OS_LINUX_SPEC "-m elf32ppclinux %{!shared: %{!static: \ 153 +#define LINK_OS_LINUX_SPEC "-m elf32ppclinux --as-needed %{!shared: %{!static: \ 154 %{rdynamic:-export-dynamic} \ 155 -dynamic-linker " GNU_USER_DYNAMIC_LINKER "}}" 156 157 diff --git a/gcc/config/s390/linux.h b/gcc/config/s390/linux.h 158 index daa48fdfc..2fcfc5d77 100644 159 --- a/gcc/config/s390/linux.h 160 +++ b/gcc/config/s390/linux.h 161 @@ -82,7 +82,7 @@ along with GCC; see the file COPYING3. If not see 162 163 #undef LINK_SPEC 164 #define LINK_SPEC \ 165 - "%{m31:-m elf_s390}%{m64:-m elf64_s390} \ 166 + "%{m31:-m elf_s390}%{m64:-m elf64_s390} --as-needed \ 167 %{shared:-shared} \ 168 %{!shared: \ 169 %{static:-static} \ 170 diff --git a/gcc/config/sparc/linux.h b/gcc/config/sparc/linux.h 171 index 2550d7ee8..7596d0b27 100644 172 --- a/gcc/config/sparc/linux.h 173 +++ b/gcc/config/sparc/linux.h 174 @@ -81,7 +81,7 @@ extern const char *host_detect_local_cpu (int argc, const char **argv); 175 #define GLIBC_DYNAMIC_LINKER "/lib/ld-linux.so.2" 176 177 #undef LINK_SPEC 178 -#define LINK_SPEC "-m elf32_sparc %{shared:-shared} \ 179 +#define LINK_SPEC "-m elf32_sparc --as-needed %{shared:-shared} \ 180 %{!mno-relax:%{!r:-relax}} \ 181 %{!shared: \ 182 %{!static: \ 183 diff --git a/gcc/gcc.c b/gcc/gcc.c 184 index 4d790f9dd..5b2fa8c93 100644 185 --- a/gcc/gcc.c 186 +++ b/gcc/gcc.c 187 @@ -737,8 +737,11 @@ proper position among the other output files. */ 188 #ifdef LIBASAN_EARLY_SPEC 189 #define LIBASAN_SPEC STATIC_LIBASAN_LIBS 190 #elif defined(HAVE_LD_STATIC_DYNAMIC) 191 -#define LIBASAN_SPEC "%{static-libasan:" LD_STATIC_OPTION \ 192 - "} -lasan %{static-libasan:" LD_DYNAMIC_OPTION "}" \ 193 +#define LIBASAN_SPEC "%{static-libasan:" LD_STATIC_OPTION "}" \ 194 + " %{!static-libasan:%{!fuse-ld=gold:--push-state }--no-as-needed}" \ 195 + " -lasan " \ 196 + " %{static-libasan:" LD_DYNAMIC_OPTION "}" \ 197 + " %{!static-libasan:%{fuse-ld=gold:--as-needed;:--pop-state}}" \ 198 STATIC_LIBASAN_LIBS 199 #else 200 #define LIBASAN_SPEC "-lasan" STATIC_LIBASAN_LIBS 201 @@ -773,8 +776,11 @@ proper position among the other output files. */ 202 #ifdef LIBTSAN_EARLY_SPEC 203 #define LIBTSAN_SPEC STATIC_LIBTSAN_LIBS 204 #elif defined(HAVE_LD_STATIC_DYNAMIC) 205 -#define LIBTSAN_SPEC "%{static-libtsan:" LD_STATIC_OPTION \ 206 - "} -ltsan %{static-libtsan:" LD_DYNAMIC_OPTION "}" \ 207 +#define LIBTSAN_SPEC "%{static-libtsan:" LD_STATIC_OPTION "}" \ 208 + " %{!static-libtsan:%{!fuse-ld=gold:--push-state }--no-as-needed}" \ 209 + " -ltsan " \ 210 + " %{static-libtsan:" LD_DYNAMIC_OPTION "}" \ 211 + " %{!static-libtsan:%{fuse-ld=gold:--as-needed;:--pop-state}}" \ 212 STATIC_LIBTSAN_LIBS 213 #else 214 #define LIBTSAN_SPEC "-ltsan" STATIC_LIBTSAN_LIBS 215 @@ -791,8 +797,11 @@ proper position among the other output files. */ 216 #ifdef LIBLSAN_EARLY_SPEC 217 #define LIBLSAN_SPEC STATIC_LIBLSAN_LIBS 218 #elif defined(HAVE_LD_STATIC_DYNAMIC) 219 -#define LIBLSAN_SPEC "%{static-liblsan:" LD_STATIC_OPTION \ 220 - "} -llsan %{static-liblsan:" LD_DYNAMIC_OPTION "}" \ 221 +#define LIBLSAN_SPEC "%{static-liblsan:" LD_STATIC_OPTION "}" \ 222 + " %{!static-liblsan:%{!fuse-ld=gold:--push-state }--no-as-needed}" \ 223 + " -llsan " \ 224 + " %{static-liblsan:" LD_DYNAMIC_OPTION "}" \ 225 + " %{!static-liblsan:%{fuse-ld=gold:--as-needed;:--pop-state}}" \ 226 STATIC_LIBLSAN_LIBS 227 #else 228 #define LIBLSAN_SPEC "-llsan" STATIC_LIBLSAN_LIBS 229 @@ -807,8 +816,11 @@ proper position among the other output files. */ 230 #define STATIC_LIBUBSAN_LIBS \ 231 " %{static-libubsan|static:%:include(libsanitizer.spec)%(link_libubsan)}" 232 #ifdef HAVE_LD_STATIC_DYNAMIC 233 -#define LIBUBSAN_SPEC "%{static-libubsan:" LD_STATIC_OPTION \ 234 - "} -lubsan %{static-libubsan:" LD_DYNAMIC_OPTION "}" \ 235 +#define LIBUBSAN_SPEC "%{static-libubsan:" LD_STATIC_OPTION "}" \ 236 + " %{!static-libubsan:%{!fuse-ld=gold:--push-state }--no-as-needed}" \ 237 + " -lubsan " \ 238 + " %{static-libubsan:" LD_DYNAMIC_OPTION "}" \ 239 + " %{!static-libubsan:%{fuse-ld=gold:--as-needed;:--pop-state}}" \ 240 STATIC_LIBUBSAN_LIBS 241 #else 242 #define LIBUBSAN_SPEC "-lubsan" STATIC_LIBUBSAN_LIBS