1 | //===--- Linux.h - Linux ToolChain Implementations --------------*- C++ -*-===// |
---|---|
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "Linux.h" |
10 | #include "Arch/ARM.h" |
11 | #include "Arch/LoongArch.h" |
12 | #include "Arch/Mips.h" |
13 | #include "Arch/PPC.h" |
14 | #include "Arch/RISCV.h" |
15 | #include "clang/Config/config.h" |
16 | #include "clang/Driver/CommonArgs.h" |
17 | #include "clang/Driver/Distro.h" |
18 | #include "clang/Driver/Driver.h" |
19 | #include "clang/Driver/Options.h" |
20 | #include "clang/Driver/SanitizerArgs.h" |
21 | #include "llvm/Option/ArgList.h" |
22 | #include "llvm/ProfileData/InstrProf.h" |
23 | #include "llvm/Support/Path.h" |
24 | #include "llvm/Support/ScopedPrinter.h" |
25 | #include "llvm/Support/VirtualFileSystem.h" |
26 | |
27 | using namespace clang::driver; |
28 | using namespace clang::driver::toolchains; |
29 | using namespace clang; |
30 | using namespace llvm::opt; |
31 | |
32 | using tools::addPathIfExists; |
33 | |
34 | /// Get our best guess at the multiarch triple for a target. |
35 | /// |
36 | /// Debian-based systems are starting to use a multiarch setup where they use |
37 | /// a target-triple directory in the library and header search paths. |
38 | /// Unfortunately, this triple does not align with the vanilla target triple, |
39 | /// so we provide a rough mapping here. |
40 | std::string Linux::getMultiarchTriple(const Driver &D, |
41 | const llvm::Triple &TargetTriple, |
42 | StringRef SysRoot) const { |
43 | llvm::Triple::EnvironmentType TargetEnvironment = |
44 | TargetTriple.getEnvironment(); |
45 | bool IsAndroid = TargetTriple.isAndroid(); |
46 | bool IsMipsR6 = TargetTriple.getSubArch() == llvm::Triple::MipsSubArch_r6; |
47 | bool IsMipsN32Abi = TargetTriple.getEnvironment() == llvm::Triple::GNUABIN32; |
48 | |
49 | // For most architectures, just use whatever we have rather than trying to be |
50 | // clever. |
51 | switch (TargetTriple.getArch()) { |
52 | default: |
53 | break; |
54 | |
55 | // We use the existence of '/lib/<triple>' as a directory to detect some |
56 | // common linux triples that don't quite match the Clang triple for both |
57 | // 32-bit and 64-bit targets. Multiarch fixes its install triples to these |
58 | // regardless of what the actual target triple is. |
59 | case llvm::Triple::arm: |
60 | case llvm::Triple::thumb: |
61 | if (IsAndroid) |
62 | return "arm-linux-androideabi"; |
63 | if (TargetEnvironment == llvm::Triple::GNUEABIHF || |
64 | TargetEnvironment == llvm::Triple::MuslEABIHF || |
65 | TargetEnvironment == llvm::Triple::EABIHF) |
66 | return "arm-linux-gnueabihf"; |
67 | return "arm-linux-gnueabi"; |
68 | case llvm::Triple::armeb: |
69 | case llvm::Triple::thumbeb: |
70 | if (TargetEnvironment == llvm::Triple::GNUEABIHF || |
71 | TargetEnvironment == llvm::Triple::MuslEABIHF || |
72 | TargetEnvironment == llvm::Triple::EABIHF) |
73 | return "armeb-linux-gnueabihf"; |
74 | return "armeb-linux-gnueabi"; |
75 | case llvm::Triple::x86: |
76 | if (IsAndroid) |
77 | return "i686-linux-android"; |
78 | return "i386-linux-gnu"; |
79 | case llvm::Triple::x86_64: |
80 | if (IsAndroid) |
81 | return "x86_64-linux-android"; |
82 | if (TargetEnvironment == llvm::Triple::GNUX32) |
83 | return "x86_64-linux-gnux32"; |
84 | return "x86_64-linux-gnu"; |
85 | case llvm::Triple::aarch64: |
86 | if (IsAndroid) |
87 | return "aarch64-linux-android"; |
88 | if (hasEffectiveTriple() && |
89 | getEffectiveTriple().getEnvironment() == llvm::Triple::PAuthTest) |
90 | return "aarch64-linux-pauthtest"; |
91 | return "aarch64-linux-gnu"; |
92 | case llvm::Triple::aarch64_be: |
93 | return "aarch64_be-linux-gnu"; |
94 | |
95 | case llvm::Triple::loongarch64: { |
96 | const char *Libc; |
97 | const char *FPFlavor; |
98 | |
99 | if (TargetTriple.isGNUEnvironment()) { |
100 | Libc = "gnu"; |
101 | } else if (TargetTriple.isMusl()) { |
102 | Libc = "musl"; |
103 | } else { |
104 | return TargetTriple.str(); |
105 | } |
106 | |
107 | switch (TargetEnvironment) { |
108 | default: |
109 | return TargetTriple.str(); |
110 | case llvm::Triple::GNUSF: |
111 | case llvm::Triple::MuslSF: |
112 | FPFlavor = "sf"; |
113 | break; |
114 | case llvm::Triple::GNUF32: |
115 | case llvm::Triple::MuslF32: |
116 | FPFlavor = "f32"; |
117 | break; |
118 | case llvm::Triple::GNU: |
119 | case llvm::Triple::GNUF64: |
120 | case llvm::Triple::Musl: |
121 | // This was going to be "f64" in an earlier Toolchain Conventions |
122 | // revision, but starting from Feb 2023 the F64 ABI variants are |
123 | // unmarked in their canonical forms. |
124 | FPFlavor = ""; |
125 | break; |
126 | } |
127 | |
128 | return (Twine("loongarch64-linux-") + Libc + FPFlavor).str(); |
129 | } |
130 | |
131 | case llvm::Triple::m68k: |
132 | return "m68k-linux-gnu"; |
133 | |
134 | case llvm::Triple::mips: |
135 | return IsMipsR6 ? "mipsisa32r6-linux-gnu": "mips-linux-gnu"; |
136 | case llvm::Triple::mipsel: |
137 | return IsMipsR6 ? "mipsisa32r6el-linux-gnu": "mipsel-linux-gnu"; |
138 | case llvm::Triple::mips64: { |
139 | std::string MT = std::string(IsMipsR6 ? "mipsisa64r6": "mips64") + |
140 | "-linux-"+ (IsMipsN32Abi ? "gnuabin32": "gnuabi64"); |
141 | if (D.getVFS().exists(Path: concat(Path: SysRoot, A: "/lib", B: MT))) |
142 | return MT; |
143 | if (D.getVFS().exists(Path: concat(Path: SysRoot, A: "/lib/mips64-linux-gnu"))) |
144 | return "mips64-linux-gnu"; |
145 | break; |
146 | } |
147 | case llvm::Triple::mips64el: { |
148 | std::string MT = std::string(IsMipsR6 ? "mipsisa64r6el": "mips64el") + |
149 | "-linux-"+ (IsMipsN32Abi ? "gnuabin32": "gnuabi64"); |
150 | if (D.getVFS().exists(Path: concat(Path: SysRoot, A: "/lib", B: MT))) |
151 | return MT; |
152 | if (D.getVFS().exists(Path: concat(Path: SysRoot, A: "/lib/mips64el-linux-gnu"))) |
153 | return "mips64el-linux-gnu"; |
154 | break; |
155 | } |
156 | case llvm::Triple::ppc: |
157 | if (D.getVFS().exists(Path: concat(Path: SysRoot, A: "/lib/powerpc-linux-gnuspe"))) |
158 | return "powerpc-linux-gnuspe"; |
159 | return "powerpc-linux-gnu"; |
160 | case llvm::Triple::ppcle: |
161 | return "powerpcle-linux-gnu"; |
162 | case llvm::Triple::ppc64: |
163 | return "powerpc64-linux-gnu"; |
164 | case llvm::Triple::ppc64le: |
165 | return "powerpc64le-linux-gnu"; |
166 | case llvm::Triple::riscv64: |
167 | if (IsAndroid) |
168 | return "riscv64-linux-android"; |
169 | return "riscv64-linux-gnu"; |
170 | case llvm::Triple::sparc: |
171 | return "sparc-linux-gnu"; |
172 | case llvm::Triple::sparcv9: |
173 | return "sparc64-linux-gnu"; |
174 | case llvm::Triple::systemz: |
175 | return "s390x-linux-gnu"; |
176 | } |
177 | return TargetTriple.str(); |
178 | } |
179 | |
180 | static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) { |
181 | if (Triple.isMIPS()) { |
182 | // lib32 directory has a special meaning on MIPS targets. |
183 | // It contains N32 ABI binaries. Use this folder if produce |
184 | // code for N32 ABI only. |
185 | if (tools::mips::hasMipsAbiArg(Args, Value: "n32")) |
186 | return "lib32"; |
187 | return Triple.isArch32Bit() ? "lib": "lib64"; |
188 | } |
189 | |
190 | // It happens that only x86, PPC and SPARC use the 'lib32' variant of |
191 | // oslibdir, and using that variant while targeting other architectures causes |
192 | // problems because the libraries are laid out in shared system roots that |
193 | // can't cope with a 'lib32' library search path being considered. So we only |
194 | // enable them when we know we may need it. |
195 | // |
196 | // FIXME: This is a bit of a hack. We should really unify this code for |
197 | // reasoning about oslibdir spellings with the lib dir spellings in the |
198 | // GCCInstallationDetector, but that is a more significant refactoring. |
199 | if (Triple.getArch() == llvm::Triple::x86 || Triple.isPPC32() || |
200 | Triple.getArch() == llvm::Triple::sparc) |
201 | return "lib32"; |
202 | |
203 | if (Triple.getArch() == llvm::Triple::x86_64 && Triple.isX32()) |
204 | return "libx32"; |
205 | |
206 | if (Triple.getArch() == llvm::Triple::riscv32) |
207 | return "lib32"; |
208 | |
209 | return Triple.isArch32Bit() ? "lib": "lib64"; |
210 | } |
211 | |
212 | Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) |
213 | : Generic_ELF(D, Triple, Args) { |
214 | GCCInstallation.init(TargetTriple: Triple, Args); |
215 | Multilibs = GCCInstallation.getMultilibs(); |
216 | SelectedMultilibs.assign(IL: {GCCInstallation.getMultilib()}); |
217 | llvm::Triple::ArchType Arch = Triple.getArch(); |
218 | std::string SysRoot = computeSysRoot(); |
219 | ToolChain::path_list &PPaths = getProgramPaths(); |
220 | |
221 | Generic_GCC::PushPPaths(PPaths); |
222 | |
223 | Distro Distro(D.getVFS(), Triple); |
224 | |
225 | if (Distro.IsAlpineLinux() || Triple.isAndroid()) { |
226 | ExtraOpts.push_back(x: "-z"); |
227 | ExtraOpts.push_back(x: "now"); |
228 | } |
229 | |
230 | if (Distro.IsOpenSUSE() || Distro.IsUbuntu() || Distro.IsAlpineLinux() || |
231 | Triple.isAndroid()) { |
232 | ExtraOpts.push_back(x: "-z"); |
233 | ExtraOpts.push_back(x: "relro"); |
234 | } |
235 | |
236 | // Note, lld from 11 onwards default max-page-size to 65536 for both ARM and |
237 | // AArch64. |
238 | if (Triple.isAndroid()) { |
239 | if (Triple.isARM()) { |
240 | // Android ARM uses max-page-size=4096 to reduce VMA usage. |
241 | ExtraOpts.push_back(x: "-z"); |
242 | ExtraOpts.push_back(x: "max-page-size=4096"); |
243 | } else if (Triple.isAArch64() || Triple.getArch() == llvm::Triple::x86_64) { |
244 | // Android AArch64 uses max-page-size=16384 to support 4k/16k page sizes. |
245 | // Android emulates a 16k page size for app testing on x86_64 machines. |
246 | ExtraOpts.push_back(x: "-z"); |
247 | ExtraOpts.push_back(x: "max-page-size=16384"); |
248 | } |
249 | if (Triple.isAndroidVersionLT(Major: 29)) { |
250 | // https://github.com/android/ndk/issues/1196 |
251 | // The unwinder used by the crash handler on versions of Android prior to |
252 | // API 29 did not correctly handle binaries built with rosegment, which is |
253 | // enabled by default for LLD. Android only supports LLD, so it's not an |
254 | // issue that this flag is not accepted by other linkers. |
255 | ExtraOpts.push_back(x: "--no-rosegment"); |
256 | } |
257 | if (!Triple.isAndroidVersionLT(Major: 28)) { |
258 | // Android supports relr packing starting with API 28 and had its own |
259 | // flavor (--pack-dyn-relocs=android) starting in API 23. |
260 | // TODO: It's possible to use both with --pack-dyn-relocs=android+relr, |
261 | // but we need to gather some data on the impact of that form before we |
262 | // can know if it's a good default. |
263 | // On the other hand, relr should always be an improvement. |
264 | ExtraOpts.push_back(x: "--use-android-relr-tags"); |
265 | ExtraOpts.push_back(x: "--pack-dyn-relocs=relr"); |
266 | } |
267 | } |
268 | |
269 | if (GCCInstallation.getParentLibPath().contains(Other: "opt/rh/")) |
270 | // With devtoolset on RHEL, we want to add a bin directory that is relative |
271 | // to the detected gcc install, because if we are using devtoolset gcc then |
272 | // we want to use other tools from devtoolset (e.g. ld) instead of the |
273 | // standard system tools. |
274 | PPaths.push_back(Elt: Twine(GCCInstallation.getParentLibPath() + |
275 | "/../bin").str()); |
276 | |
277 | if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb) |
278 | ExtraOpts.push_back(x: "-X"); |
279 | |
280 | const bool IsAndroid = Triple.isAndroid(); |
281 | const bool IsMips = Triple.isMIPS(); |
282 | const bool IsHexagon = Arch == llvm::Triple::hexagon; |
283 | const bool IsRISCV = Triple.isRISCV(); |
284 | const bool IsCSKY = Triple.isCSKY(); |
285 | |
286 | if (IsCSKY && !SelectedMultilibs.empty()) |
287 | SysRoot = SysRoot + SelectedMultilibs.back().osSuffix(); |
288 | |
289 | if ((IsMips || IsCSKY) && !SysRoot.empty()) |
290 | ExtraOpts.push_back(x: "--sysroot="+ SysRoot); |
291 | |
292 | // Do not use 'gnu' hash style for Mips targets because .gnu.hash |
293 | // and the MIPS ABI require .dynsym to be sorted in different ways. |
294 | // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS |
295 | // ABI requires a mapping between the GOT and the symbol table. |
296 | // Android loader does not support .gnu.hash until API 23. |
297 | // Hexagon linker/loader does not support .gnu.hash |
298 | if (!IsMips && !IsHexagon) { |
299 | if (Distro.IsOpenSUSE() || Distro == Distro::UbuntuLucid || |
300 | Distro == Distro::UbuntuJaunty || Distro == Distro::UbuntuKarmic || |
301 | (IsAndroid && Triple.isAndroidVersionLT(Major: 23))) |
302 | ExtraOpts.push_back(x: "--hash-style=both"); |
303 | else |
304 | ExtraOpts.push_back(x: "--hash-style=gnu"); |
305 | } |
306 | |
307 | #ifdef ENABLE_LINKER_BUILD_ID |
308 | ExtraOpts.push_back("--build-id"); |
309 | #endif |
310 | |
311 | // The selection of paths to try here is designed to match the patterns which |
312 | // the GCC driver itself uses, as this is part of the GCC-compatible driver. |
313 | // This was determined by running GCC in a fake filesystem, creating all |
314 | // possible permutations of these directories, and seeing which ones it added |
315 | // to the link paths. |
316 | path_list &Paths = getFilePaths(); |
317 | |
318 | const std::string OSLibDir = std::string(getOSLibDir(Triple, Args)); |
319 | const std::string MultiarchTriple = getMultiarchTriple(D, TargetTriple: Triple, SysRoot); |
320 | |
321 | // mips32: Debian multilib, we use /libo32, while in other case, /lib is |
322 | // used. We need add both libo32 and /lib. |
323 | if (Arch == llvm::Triple::mips || Arch == llvm::Triple::mipsel) { |
324 | Generic_GCC::AddMultilibPaths(D, SysRoot, OSLibDir: "libo32", MultiarchTriple, Paths); |
325 | addPathIfExists(D, Path: concat(Path: SysRoot, A: "/libo32"), Paths); |
326 | addPathIfExists(D, Path: concat(Path: SysRoot, A: "/usr/libo32"), Paths); |
327 | } |
328 | Generic_GCC::AddMultilibPaths(D, SysRoot, OSLibDir, MultiarchTriple, Paths); |
329 | |
330 | addPathIfExists(D, Path: concat(Path: SysRoot, A: "/lib", B: MultiarchTriple), Paths); |
331 | addPathIfExists(D, Path: concat(Path: SysRoot, A: "/lib/..", B: OSLibDir), Paths); |
332 | |
333 | if (IsAndroid) { |
334 | // Android sysroots contain a library directory for each supported OS |
335 | // version as well as some unversioned libraries in the usual multiarch |
336 | // directory. |
337 | addPathIfExists( |
338 | D, |
339 | Path: concat(Path: SysRoot, A: "/usr/lib", B: MultiarchTriple, |
340 | C: llvm::to_string(Value: Triple.getEnvironmentVersion().getMajor())), |
341 | Paths); |
342 | } |
343 | |
344 | addPathIfExists(D, Path: concat(Path: SysRoot, A: "/usr/lib", B: MultiarchTriple), Paths); |
345 | addPathIfExists(D, Path: concat(Path: SysRoot, A: "/usr", B: OSLibDir), Paths); |
346 | if (IsRISCV) { |
347 | StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple); |
348 | addPathIfExists(D, Path: concat(Path: SysRoot, A: "/", B: OSLibDir, C: ABIName), Paths); |
349 | addPathIfExists(D, Path: concat(Path: SysRoot, A: "/usr", B: OSLibDir, C: ABIName), Paths); |
350 | } |
351 | |
352 | Generic_GCC::AddMultiarchPaths(D, SysRoot, OSLibDir, Paths); |
353 | |
354 | addPathIfExists(D, Path: concat(Path: SysRoot, A: "/lib"), Paths); |
355 | addPathIfExists(D, Path: concat(Path: SysRoot, A: "/usr/lib"), Paths); |
356 | } |
357 | |
358 | ToolChain::RuntimeLibType Linux::GetDefaultRuntimeLibType() const { |
359 | if (getTriple().isAndroid()) |
360 | return ToolChain::RLT_CompilerRT; |
361 | return Generic_ELF::GetDefaultRuntimeLibType(); |
362 | } |
363 | |
364 | unsigned Linux::GetDefaultDwarfVersion() const { |
365 | if (getTriple().isAndroid()) |
366 | return 4; |
367 | return ToolChain::GetDefaultDwarfVersion(); |
368 | } |
369 | |
370 | ToolChain::CXXStdlibType Linux::GetDefaultCXXStdlibType() const { |
371 | if (getTriple().isAndroid()) |
372 | return ToolChain::CST_Libcxx; |
373 | return ToolChain::CST_Libstdcxx; |
374 | } |
375 | |
376 | bool Linux::HasNativeLLVMSupport() const { return true; } |
377 | |
378 | Tool *Linux::buildLinker() const { return new tools::gnutools::Linker(*this); } |
379 | |
380 | Tool *Linux::buildStaticLibTool() const { |
381 | return new tools::gnutools::StaticLibTool(*this); |
382 | } |
383 | |
384 | Tool *Linux::buildAssembler() const { |
385 | return new tools::gnutools::Assembler(*this); |
386 | } |
387 | |
388 | std::string Linux::computeSysRoot() const { |
389 | if (!getDriver().SysRoot.empty()) |
390 | return getDriver().SysRoot; |
391 | |
392 | if (getTriple().isAndroid()) { |
393 | // Android toolchains typically include a sysroot at ../sysroot relative to |
394 | // the clang binary. |
395 | const StringRef ClangDir = getDriver().Dir; |
396 | std::string AndroidSysRootPath = (ClangDir + "/../sysroot").str(); |
397 | if (getVFS().exists(Path: AndroidSysRootPath)) |
398 | return AndroidSysRootPath; |
399 | } |
400 | |
401 | if (getTriple().isCSKY()) { |
402 | // CSKY toolchains use different names for sysroot folder. |
403 | if (!GCCInstallation.isValid()) |
404 | return std::string(); |
405 | // GCCInstallation.getInstallPath() = |
406 | // $GCCToolchainPath/lib/gcc/csky-linux-gnuabiv2/6.3.0 |
407 | // Path = $GCCToolchainPath/csky-linux-gnuabiv2/libc |
408 | std::string Path = (GCCInstallation.getInstallPath() + "/../../../../"+ |
409 | GCCInstallation.getTriple().str() + "/libc") |
410 | .str(); |
411 | if (getVFS().exists(Path)) |
412 | return Path; |
413 | return std::string(); |
414 | } |
415 | |
416 | if (!GCCInstallation.isValid() || !getTriple().isMIPS()) |
417 | return std::string(); |
418 | |
419 | // Standalone MIPS toolchains use different names for sysroot folder |
420 | // and put it into different places. Here we try to check some known |
421 | // variants. |
422 | |
423 | const StringRef InstallDir = GCCInstallation.getInstallPath(); |
424 | const StringRef TripleStr = GCCInstallation.getTriple().str(); |
425 | const Multilib &Multilib = GCCInstallation.getMultilib(); |
426 | |
427 | std::string Path = |
428 | (InstallDir + "/../../../../"+ TripleStr + "/libc"+ Multilib.osSuffix()) |
429 | .str(); |
430 | |
431 | if (getVFS().exists(Path)) |
432 | return Path; |
433 | |
434 | Path = (InstallDir + "/../../../../sysroot"+ Multilib.osSuffix()).str(); |
435 | |
436 | if (getVFS().exists(Path)) |
437 | return Path; |
438 | |
439 | return std::string(); |
440 | } |
441 | |
442 | std::string Linux::getDynamicLinker(const ArgList &Args) const { |
443 | const llvm::Triple::ArchType Arch = getArch(); |
444 | const llvm::Triple &Triple = getTriple(); |
445 | |
446 | const Distro Distro(getDriver().getVFS(), Triple); |
447 | |
448 | if (Triple.isAndroid()) { |
449 | if (getSanitizerArgs(JobArgs: Args).needsHwasanRt() && |
450 | !Triple.isAndroidVersionLT(Major: 34) && Triple.isArch64Bit()) { |
451 | // On Android 14 and newer, there is a special linker_hwasan64 that |
452 | // allows to run HWASan binaries on non-HWASan system images. This |
453 | // is also available on HWASan system images, so we can just always |
454 | // use that instead. |
455 | return "/system/bin/linker_hwasan64"; |
456 | } |
457 | return Triple.isArch64Bit() ? "/system/bin/linker64": "/system/bin/linker"; |
458 | } |
459 | if (Triple.isMusl()) { |
460 | std::string ArchName; |
461 | bool IsArm = false; |
462 | |
463 | switch (Arch) { |
464 | case llvm::Triple::arm: |
465 | case llvm::Triple::thumb: |
466 | ArchName = "arm"; |
467 | IsArm = true; |
468 | break; |
469 | case llvm::Triple::armeb: |
470 | case llvm::Triple::thumbeb: |
471 | ArchName = "armeb"; |
472 | IsArm = true; |
473 | break; |
474 | case llvm::Triple::x86: |
475 | ArchName = "i386"; |
476 | break; |
477 | case llvm::Triple::x86_64: |
478 | ArchName = Triple.isX32() ? "x32": Triple.getArchName().str(); |
479 | break; |
480 | default: |
481 | ArchName = Triple.getArchName().str(); |
482 | } |
483 | if (IsArm && |
484 | (Triple.getEnvironment() == llvm::Triple::MuslEABIHF || |
485 | tools::arm::getARMFloatABI(TC: *this, Args) == tools::arm::FloatABI::Hard)) |
486 | ArchName += "hf"; |
487 | if (Arch == llvm::Triple::ppc && |
488 | Triple.getSubArch() == llvm::Triple::PPCSubArch_spe) |
489 | ArchName = "powerpc-sf"; |
490 | |
491 | return "/lib/ld-musl-"+ ArchName + ".so.1"; |
492 | } |
493 | |
494 | std::string LibDir; |
495 | std::string Loader; |
496 | |
497 | switch (Arch) { |
498 | default: |
499 | llvm_unreachable("unsupported architecture"); |
500 | |
501 | case llvm::Triple::aarch64: |
502 | LibDir = "lib"; |
503 | Loader = "ld-linux-aarch64.so.1"; |
504 | break; |
505 | case llvm::Triple::aarch64_be: |
506 | LibDir = "lib"; |
507 | Loader = "ld-linux-aarch64_be.so.1"; |
508 | break; |
509 | case llvm::Triple::arm: |
510 | case llvm::Triple::thumb: |
511 | case llvm::Triple::armeb: |
512 | case llvm::Triple::thumbeb: { |
513 | const bool HF = |
514 | Triple.getEnvironment() == llvm::Triple::GNUEABIHF || |
515 | Triple.getEnvironment() == llvm::Triple::GNUEABIHFT64 || |
516 | tools::arm::getARMFloatABI(TC: *this, Args) == tools::arm::FloatABI::Hard; |
517 | |
518 | LibDir = "lib"; |
519 | Loader = HF ? "ld-linux-armhf.so.3": "ld-linux.so.3"; |
520 | break; |
521 | } |
522 | case llvm::Triple::loongarch32: { |
523 | LibDir = "lib32"; |
524 | Loader = |
525 | ("ld-linux-loongarch-"+ |
526 | tools::loongarch::getLoongArchABI(D: getDriver(), Args, Triple) + ".so.1") |
527 | .str(); |
528 | break; |
529 | } |
530 | case llvm::Triple::loongarch64: { |
531 | LibDir = "lib64"; |
532 | Loader = |
533 | ("ld-linux-loongarch-"+ |
534 | tools::loongarch::getLoongArchABI(D: getDriver(), Args, Triple) + ".so.1") |
535 | .str(); |
536 | break; |
537 | } |
538 | case llvm::Triple::m68k: |
539 | LibDir = "lib"; |
540 | Loader = "ld.so.1"; |
541 | break; |
542 | case llvm::Triple::mips: |
543 | case llvm::Triple::mipsel: |
544 | case llvm::Triple::mips64: |
545 | case llvm::Triple::mips64el: { |
546 | bool IsNaN2008 = tools::mips::isNaN2008(D: getDriver(), Args, Triple); |
547 | |
548 | LibDir = "lib"+ tools::mips::getMipsABILibSuffix(Args, Triple); |
549 | |
550 | if (tools::mips::isUCLibc(Args)) |
551 | Loader = IsNaN2008 ? "ld-uClibc-mipsn8.so.0": "ld-uClibc.so.0"; |
552 | else if (!Triple.hasEnvironment() && |
553 | Triple.getVendor() == llvm::Triple::VendorType::MipsTechnologies) |
554 | Loader = |
555 | Triple.isLittleEndian() ? "ld-musl-mipsel.so.1": "ld-musl-mips.so.1"; |
556 | else |
557 | Loader = IsNaN2008 ? "ld-linux-mipsn8.so.1": "ld.so.1"; |
558 | |
559 | break; |
560 | } |
561 | case llvm::Triple::ppc: |
562 | LibDir = "lib"; |
563 | Loader = "ld.so.1"; |
564 | break; |
565 | case llvm::Triple::ppcle: |
566 | LibDir = "lib"; |
567 | Loader = "ld.so.1"; |
568 | break; |
569 | case llvm::Triple::ppc64: |
570 | LibDir = "lib64"; |
571 | Loader = |
572 | (tools::ppc::hasPPCAbiArg(Args, Value: "elfv2")) ? "ld64.so.2": "ld64.so.1"; |
573 | break; |
574 | case llvm::Triple::ppc64le: |
575 | LibDir = "lib64"; |
576 | Loader = |
577 | (tools::ppc::hasPPCAbiArg(Args, Value: "elfv1")) ? "ld64.so.1": "ld64.so.2"; |
578 | break; |
579 | case llvm::Triple::riscv32: |
580 | case llvm::Triple::riscv64: { |
581 | StringRef ArchName = llvm::Triple::getArchTypeName(Kind: Arch); |
582 | StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple); |
583 | LibDir = "lib"; |
584 | Loader = ("ld-linux-"+ ArchName + "-"+ ABIName + ".so.1").str(); |
585 | break; |
586 | } |
587 | case llvm::Triple::sparc: |
588 | case llvm::Triple::sparcel: |
589 | LibDir = "lib"; |
590 | Loader = "ld-linux.so.2"; |
591 | break; |
592 | case llvm::Triple::sparcv9: |
593 | LibDir = "lib64"; |
594 | Loader = "ld-linux.so.2"; |
595 | break; |
596 | case llvm::Triple::systemz: |
597 | LibDir = "lib"; |
598 | Loader = "ld64.so.1"; |
599 | break; |
600 | case llvm::Triple::x86: |
601 | LibDir = "lib"; |
602 | Loader = "ld-linux.so.2"; |
603 | break; |
604 | case llvm::Triple::x86_64: { |
605 | bool X32 = Triple.isX32(); |
606 | |
607 | LibDir = X32 ? "libx32": "lib64"; |
608 | Loader = X32 ? "ld-linux-x32.so.2": "ld-linux-x86-64.so.2"; |
609 | break; |
610 | } |
611 | case llvm::Triple::ve: |
612 | return "/opt/nec/ve/lib/ld-linux-ve.so.1"; |
613 | case llvm::Triple::csky: { |
614 | LibDir = "lib"; |
615 | Loader = "ld.so.1"; |
616 | break; |
617 | } |
618 | } |
619 | |
620 | if (Distro == Distro::Exherbo && |
621 | (Triple.getVendor() == llvm::Triple::UnknownVendor || |
622 | Triple.getVendor() == llvm::Triple::PC)) |
623 | return "/usr/"+ Triple.str() + "/lib/"+ Loader; |
624 | return "/"+ LibDir + "/"+ Loader; |
625 | } |
626 | |
627 | void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs, |
628 | ArgStringList &CC1Args) const { |
629 | const Driver &D = getDriver(); |
630 | std::string SysRoot = computeSysRoot(); |
631 | |
632 | if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)) |
633 | return; |
634 | |
635 | // Add 'include' in the resource directory, which is similar to |
636 | // GCC_INCLUDE_DIR (private headers) in GCC. Note: the include directory |
637 | // contains some files conflicting with system /usr/include. musl systems |
638 | // prefer the /usr/include copies which are more relevant. |
639 | SmallString<128> ResourceDirInclude(D.ResourceDir); |
640 | llvm::sys::path::append(path&: ResourceDirInclude, a: "include"); |
641 | if (!DriverArgs.hasArg(options::OPT_nobuiltininc) && |
642 | (!getTriple().isMusl() || DriverArgs.hasArg(options::OPT_nostdlibinc))) |
643 | addSystemInclude(DriverArgs, CC1Args, Path: ResourceDirInclude); |
644 | |
645 | if (DriverArgs.hasArg(options::OPT_nostdlibinc)) |
646 | return; |
647 | |
648 | // LOCAL_INCLUDE_DIR |
649 | addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: SysRoot, A: "/usr/local/include")); |
650 | // TOOL_INCLUDE_DIR |
651 | AddMultilibIncludeArgs(DriverArgs, CC1Args); |
652 | |
653 | // Check for configure-time C include directories. |
654 | StringRef CIncludeDirs(C_INCLUDE_DIRS); |
655 | if (CIncludeDirs != "") { |
656 | SmallVector<StringRef, 5> dirs; |
657 | CIncludeDirs.split(A&: dirs, Separator: ":"); |
658 | for (StringRef dir : dirs) { |
659 | StringRef Prefix = |
660 | llvm::sys::path::is_absolute(path: dir) ? "": StringRef(SysRoot); |
661 | addExternCSystemInclude(DriverArgs, CC1Args, Path: Prefix + dir); |
662 | } |
663 | return; |
664 | } |
665 | |
666 | // On systems using multiarch and Android, add /usr/include/$triple before |
667 | // /usr/include. |
668 | std::string MultiarchIncludeDir = getMultiarchTriple(D, TargetTriple: getTriple(), SysRoot); |
669 | if (!MultiarchIncludeDir.empty() && |
670 | D.getVFS().exists(Path: concat(Path: SysRoot, A: "/usr/include", B: MultiarchIncludeDir))) |
671 | addExternCSystemInclude( |
672 | DriverArgs, CC1Args, |
673 | Path: concat(Path: SysRoot, A: "/usr/include", B: MultiarchIncludeDir)); |
674 | |
675 | if (getTriple().getOS() == llvm::Triple::RTEMS) |
676 | return; |
677 | |
678 | // Add an include of '/include' directly. This isn't provided by default by |
679 | // system GCCs, but is often used with cross-compiling GCCs, and harmless to |
680 | // add even when Clang is acting as-if it were a system compiler. |
681 | addExternCSystemInclude(DriverArgs, CC1Args, Path: concat(Path: SysRoot, A: "/include")); |
682 | |
683 | addExternCSystemInclude(DriverArgs, CC1Args, Path: concat(Path: SysRoot, A: "/usr/include")); |
684 | |
685 | if (!DriverArgs.hasArg(options::OPT_nobuiltininc) && getTriple().isMusl()) |
686 | addSystemInclude(DriverArgs, CC1Args, Path: ResourceDirInclude); |
687 | } |
688 | |
689 | void Linux::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, |
690 | llvm::opt::ArgStringList &CC1Args) const { |
691 | // We need a detected GCC installation on Linux to provide libstdc++'s |
692 | // headers in odd Linuxish places. |
693 | if (!GCCInstallation.isValid()) |
694 | return; |
695 | |
696 | // Detect Debian g++-multiarch-incdir.diff. |
697 | StringRef TripleStr = GCCInstallation.getTriple().str(); |
698 | StringRef DebianMultiarch = |
699 | GCCInstallation.getTriple().getArch() == llvm::Triple::x86 |
700 | ? "i386-linux-gnu" |
701 | : TripleStr; |
702 | |
703 | // Try generic GCC detection first. |
704 | if (Generic_GCC::addGCCLibStdCxxIncludePaths(DriverArgs, CC1Args, |
705 | DebianMultiarch)) |
706 | return; |
707 | |
708 | StringRef LibDir = GCCInstallation.getParentLibPath(); |
709 | const Multilib &Multilib = GCCInstallation.getMultilib(); |
710 | const GCCVersion &Version = GCCInstallation.getVersion(); |
711 | |
712 | const std::string LibStdCXXIncludePathCandidates[] = { |
713 | // Android standalone toolchain has C++ headers in yet another place. |
714 | LibDir.str() + "/../"+ TripleStr.str() + "/include/c++/"+ Version.Text, |
715 | // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++, |
716 | // without a subdirectory corresponding to the gcc version. |
717 | LibDir.str() + "/../include/c++", |
718 | // Cray's gcc installation puts headers under "g++" without a |
719 | // version suffix. |
720 | LibDir.str() + "/../include/g++", |
721 | }; |
722 | |
723 | for (const auto &IncludePath : LibStdCXXIncludePathCandidates) { |
724 | if (addLibStdCXXIncludePaths(IncludeDir: IncludePath, Triple: TripleStr, |
725 | IncludeSuffix: Multilib.includeSuffix(), DriverArgs, CC1Args)) |
726 | break; |
727 | } |
728 | } |
729 | |
730 | void Linux::AddCudaIncludeArgs(const ArgList &DriverArgs, |
731 | ArgStringList &CC1Args) const { |
732 | CudaInstallation->AddCudaIncludeArgs(DriverArgs, CC1Args); |
733 | } |
734 | |
735 | void Linux::AddHIPIncludeArgs(const ArgList &DriverArgs, |
736 | ArgStringList &CC1Args) const { |
737 | RocmInstallation->AddHIPIncludeArgs(DriverArgs, CC1Args); |
738 | } |
739 | |
740 | void Linux::AddHIPRuntimeLibArgs(const ArgList &Args, |
741 | ArgStringList &CmdArgs) const { |
742 | CmdArgs.push_back( |
743 | Elt: Args.MakeArgString(Str: StringRef("-L") + RocmInstallation->getLibPath())); |
744 | |
745 | if (Args.hasFlag(options::OPT_frtlib_add_rpath, |
746 | options::OPT_fno_rtlib_add_rpath, false)) |
747 | CmdArgs.append( |
748 | IL: {"-rpath", Args.MakeArgString(Str: RocmInstallation->getLibPath())}); |
749 | |
750 | CmdArgs.push_back(Elt: "-lamdhip64"); |
751 | } |
752 | |
753 | void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs, |
754 | ArgStringList &CC1Args) const { |
755 | if (GCCInstallation.isValid()) { |
756 | CC1Args.push_back(Elt: "-isystem"); |
757 | CC1Args.push_back(Elt: DriverArgs.MakeArgString( |
758 | Str: GCCInstallation.getParentLibPath() + "/../"+ |
759 | GCCInstallation.getTriple().str() + "/include")); |
760 | } |
761 | } |
762 | |
763 | void Linux::addSYCLIncludeArgs(const ArgList &DriverArgs, |
764 | ArgStringList &CC1Args) const { |
765 | SYCLInstallation->addSYCLIncludeArgs(DriverArgs, CC1Args); |
766 | } |
767 | |
768 | bool Linux::isPIEDefault(const llvm::opt::ArgList &Args) const { |
769 | return CLANG_DEFAULT_PIE_ON_LINUX || getTriple().isAndroid() || |
770 | getTriple().isMusl() || getSanitizerArgs(JobArgs: Args).requiresPIE(); |
771 | } |
772 | |
773 | bool Linux::IsAArch64OutlineAtomicsDefault(const ArgList &Args) const { |
774 | // Outline atomics for AArch64 are supported by compiler-rt |
775 | // and libgcc since 9.3.1 |
776 | assert(getTriple().isAArch64() && "expected AArch64 target!"); |
777 | ToolChain::RuntimeLibType RtLib = GetRuntimeLibType(Args); |
778 | if (RtLib == ToolChain::RLT_CompilerRT) |
779 | return true; |
780 | assert(RtLib == ToolChain::RLT_Libgcc && "unexpected runtime library type!"); |
781 | if (GCCInstallation.getVersion().isOlderThan(RHSMajor: 9, RHSMinor: 3, RHSPatch: 1)) |
782 | return false; |
783 | return true; |
784 | } |
785 | |
786 | bool Linux::IsMathErrnoDefault() const { |
787 | if (getTriple().isAndroid() || getTriple().isMusl()) |
788 | return false; |
789 | return Generic_ELF::IsMathErrnoDefault(); |
790 | } |
791 | |
792 | SanitizerMask Linux::getSupportedSanitizers() const { |
793 | const bool IsX86 = getTriple().getArch() == llvm::Triple::x86; |
794 | const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64; |
795 | const bool IsMIPS = getTriple().isMIPS32(); |
796 | const bool IsMIPS64 = getTriple().isMIPS64(); |
797 | const bool IsPowerPC64 = getTriple().getArch() == llvm::Triple::ppc64 || |
798 | getTriple().getArch() == llvm::Triple::ppc64le; |
799 | const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64 || |
800 | getTriple().getArch() == llvm::Triple::aarch64_be; |
801 | const bool IsArmArch = getTriple().getArch() == llvm::Triple::arm || |
802 | getTriple().getArch() == llvm::Triple::thumb || |
803 | getTriple().getArch() == llvm::Triple::armeb || |
804 | getTriple().getArch() == llvm::Triple::thumbeb; |
805 | const bool IsLoongArch64 = getTriple().getArch() == llvm::Triple::loongarch64; |
806 | const bool IsRISCV64 = getTriple().getArch() == llvm::Triple::riscv64; |
807 | const bool IsSystemZ = getTriple().getArch() == llvm::Triple::systemz; |
808 | const bool IsHexagon = getTriple().getArch() == llvm::Triple::hexagon; |
809 | const bool IsAndroid = getTriple().isAndroid(); |
810 | SanitizerMask Res = ToolChain::getSupportedSanitizers(); |
811 | Res |= SanitizerKind::Address; |
812 | Res |= SanitizerKind::PointerCompare; |
813 | Res |= SanitizerKind::PointerSubtract; |
814 | Res |= SanitizerKind::Realtime; |
815 | Res |= SanitizerKind::Fuzzer; |
816 | Res |= SanitizerKind::FuzzerNoLink; |
817 | Res |= SanitizerKind::KernelAddress; |
818 | Res |= SanitizerKind::Vptr; |
819 | Res |= SanitizerKind::SafeStack; |
820 | if (IsX86_64 || IsMIPS64 || IsAArch64 || IsLoongArch64) |
821 | Res |= SanitizerKind::DataFlow; |
822 | if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsArmArch || IsPowerPC64 || |
823 | IsRISCV64 || IsSystemZ || IsHexagon || IsLoongArch64) |
824 | Res |= SanitizerKind::Leak; |
825 | if (IsX86_64 || IsMIPS64 || IsAArch64 || IsPowerPC64 || IsSystemZ || |
826 | IsLoongArch64 || IsRISCV64) |
827 | Res |= SanitizerKind::Thread; |
828 | if (IsX86_64 || IsAArch64) |
829 | Res |= SanitizerKind::Type; |
830 | if (IsX86_64 || IsSystemZ || IsPowerPC64) |
831 | Res |= SanitizerKind::KernelMemory; |
832 | if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsMIPS || IsArmArch || |
833 | IsPowerPC64 || IsHexagon || IsLoongArch64 || IsRISCV64) |
834 | Res |= SanitizerKind::Scudo; |
835 | if (IsX86_64 || IsAArch64 || IsRISCV64) { |
836 | Res |= SanitizerKind::HWAddress; |
837 | } |
838 | if (IsX86_64 || IsAArch64) { |
839 | Res |= SanitizerKind::KernelHWAddress; |
840 | } |
841 | if (IsX86_64) |
842 | Res |= SanitizerKind::NumericalStability; |
843 | if (!IsAndroid) |
844 | Res |= SanitizerKind::Memory; |
845 | |
846 | // Work around "Cannot represent a difference across sections". |
847 | if (getTriple().getArch() == llvm::Triple::ppc64) |
848 | Res &= ~SanitizerKind::Function; |
849 | return Res; |
850 | } |
851 | |
852 | void Linux::addProfileRTLibs(const llvm::opt::ArgList &Args, |
853 | llvm::opt::ArgStringList &CmdArgs) const { |
854 | // Add linker option -u__llvm_profile_runtime to cause runtime |
855 | // initialization module to be linked in. |
856 | if (needsProfileRT(Args)) |
857 | CmdArgs.push_back(Elt: Args.MakeArgString( |
858 | Str: Twine("-u", llvm::getInstrProfRuntimeHookVarName()))); |
859 | ToolChain::addProfileRTLibs(Args, CmdArgs); |
860 | } |
861 | |
862 | void Linux::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const { |
863 | for (const auto &Opt : ExtraOpts) |
864 | CmdArgs.push_back(Elt: Opt.c_str()); |
865 | } |
866 | |
867 | const char *Linux::getDefaultLinker() const { |
868 | if (getTriple().isAndroid()) |
869 | return "ld.lld"; |
870 | return Generic_ELF::getDefaultLinker(); |
871 | } |
872 |
Definitions
- getMultiarchTriple
- getOSLibDir
- Linux
- GetDefaultRuntimeLibType
- GetDefaultDwarfVersion
- GetDefaultCXXStdlibType
- HasNativeLLVMSupport
- buildLinker
- buildStaticLibTool
- buildAssembler
- computeSysRoot
- getDynamicLinker
- AddClangSystemIncludeArgs
- addLibStdCxxIncludePaths
- AddCudaIncludeArgs
- AddHIPIncludeArgs
- AddHIPRuntimeLibArgs
- AddIAMCUIncludeArgs
- addSYCLIncludeArgs
- isPIEDefault
- IsAArch64OutlineAtomicsDefault
- IsMathErrnoDefault
- getSupportedSanitizers
- addProfileRTLibs
- addExtraOpts
Improve your Profiling and Debugging skills
Find out more