16#include "clang/Config/config.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#include <system_error>
42 const llvm::Triple &TargetTriple,
43 StringRef SysRoot)
const {
44 llvm::Triple::EnvironmentType TargetEnvironment =
45 TargetTriple.getEnvironment();
46 bool IsAndroid = TargetTriple.isAndroid();
47 bool IsMipsR6 = TargetTriple.getSubArch() == llvm::Triple::MipsSubArch_r6;
48 bool IsMipsN32Abi = TargetTriple.getEnvironment() == llvm::Triple::GNUABIN32;
52 switch (TargetTriple.getArch()) {
60 case llvm::Triple::arm:
61 case llvm::Triple::thumb:
63 return "arm-linux-androideabi";
64 if (TargetEnvironment == llvm::Triple::GNUEABIHF ||
65 TargetEnvironment == llvm::Triple::MuslEABIHF ||
66 TargetEnvironment == llvm::Triple::EABIHF)
67 return "arm-linux-gnueabihf";
68 return "arm-linux-gnueabi";
69 case llvm::Triple::armeb:
70 case llvm::Triple::thumbeb:
71 if (TargetEnvironment == llvm::Triple::GNUEABIHF ||
72 TargetEnvironment == llvm::Triple::MuslEABIHF ||
73 TargetEnvironment == llvm::Triple::EABIHF)
74 return "armeb-linux-gnueabihf";
75 return "armeb-linux-gnueabi";
76 case llvm::Triple::x86:
78 return "i686-linux-android";
79 return "i386-linux-gnu";
80 case llvm::Triple::x86_64:
82 return "x86_64-linux-android";
83 if (TargetEnvironment == llvm::Triple::GNUX32)
84 return "x86_64-linux-gnux32";
85 return "x86_64-linux-gnu";
86 case llvm::Triple::aarch64:
88 return "aarch64-linux-android";
91 return "aarch64-linux-pauthtest";
92 return "aarch64-linux-gnu";
93 case llvm::Triple::aarch64_be:
94 return "aarch64_be-linux-gnu";
96 case llvm::Triple::loongarch64: {
100 if (TargetTriple.isGNUEnvironment()) {
102 }
else if (TargetTriple.isMusl()) {
105 return TargetTriple.str();
108 switch (TargetEnvironment) {
110 return TargetTriple.str();
111 case llvm::Triple::GNUSF:
112 case llvm::Triple::MuslSF:
115 case llvm::Triple::GNUF32:
116 case llvm::Triple::MuslF32:
119 case llvm::Triple::GNU:
120 case llvm::Triple::GNUF64:
121 case llvm::Triple::Musl:
129 return (Twine(
"loongarch64-linux-") + Libc + FPFlavor).str();
132 case llvm::Triple::m68k:
133 return "m68k-linux-gnu";
135 case llvm::Triple::mips:
136 return IsMipsR6 ?
"mipsisa32r6-linux-gnu" :
"mips-linux-gnu";
137 case llvm::Triple::mipsel:
138 return IsMipsR6 ?
"mipsisa32r6el-linux-gnu" :
"mipsel-linux-gnu";
139 case llvm::Triple::mips64: {
140 std::string MT = std::string(IsMipsR6 ?
"mipsisa64r6" :
"mips64") +
141 "-linux-" + (IsMipsN32Abi ?
"gnuabin32" :
"gnuabi64");
142 if (
D.getVFS().exists(
concat(SysRoot,
"/lib", MT)))
144 if (
D.getVFS().exists(
concat(SysRoot,
"/lib/mips64-linux-gnu")))
145 return "mips64-linux-gnu";
148 case llvm::Triple::mips64el: {
149 std::string MT = std::string(IsMipsR6 ?
"mipsisa64r6el" :
"mips64el") +
150 "-linux-" + (IsMipsN32Abi ?
"gnuabin32" :
"gnuabi64");
151 if (
D.getVFS().exists(
concat(SysRoot,
"/lib", MT)))
153 if (
D.getVFS().exists(
concat(SysRoot,
"/lib/mips64el-linux-gnu")))
154 return "mips64el-linux-gnu";
157 case llvm::Triple::ppc:
158 if (
D.getVFS().exists(
concat(SysRoot,
"/lib/powerpc-linux-gnuspe")))
159 return "powerpc-linux-gnuspe";
160 return "powerpc-linux-gnu";
161 case llvm::Triple::ppcle:
162 return "powerpcle-linux-gnu";
163 case llvm::Triple::ppc64:
164 return "powerpc64-linux-gnu";
165 case llvm::Triple::ppc64le:
166 return "powerpc64le-linux-gnu";
167 case llvm::Triple::riscv64:
169 return "riscv64-linux-android";
170 return "riscv64-linux-gnu";
171 case llvm::Triple::sparc:
172 return "sparc-linux-gnu";
173 case llvm::Triple::sparcv9:
174 return "sparc64-linux-gnu";
175 case llvm::Triple::systemz:
176 return "s390x-linux-gnu";
178 return TargetTriple.str();
181static StringRef
getOSLibDir(
const llvm::Triple &Triple,
const ArgList &Args) {
182 if (Triple.isMIPS()) {
183 if (Triple.isAndroid()) {
187 if (CPUName ==
"mips32r6")
189 if (CPUName ==
"mips32r2")
197 return Triple.isArch32Bit() ?
"lib" :
"lib64";
209 if (Triple.getArch() == llvm::Triple::x86 || Triple.isPPC32() ||
210 Triple.getArch() == llvm::Triple::sparc)
213 if (Triple.getArch() == llvm::Triple::x86_64 && Triple.isX32())
216 if (Triple.getArch() == llvm::Triple::riscv32)
219 return Triple.isArch32Bit() ?
"lib" :
"lib64";
227 llvm::Triple::ArchType Arch = Triple.getArch();
241 Triple.isAndroid()) {
248 if (Triple.isAndroid()) {
249 if (Triple.isARM()) {
252 ExtraOpts.push_back(
"max-page-size=4096");
253 }
else if (Triple.isAArch64() || Triple.getArch() == llvm::Triple::x86_64) {
257 ExtraOpts.push_back(
"max-page-size=16384");
259 if (Triple.isAndroidVersionLT(29)) {
267 if (!Triple.isAndroidVersionLT(28)) {
274 ExtraOpts.push_back(
"--use-android-relr-tags");
275 ExtraOpts.push_back(
"--pack-dyn-relocs=relr");
287 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
290 const bool IsAndroid = Triple.isAndroid();
291 const bool IsMips = Triple.isMIPS();
292 const bool IsHexagon = Arch == llvm::Triple::hexagon;
293 const bool IsRISCV = Triple.isRISCV();
294 const bool IsCSKY = Triple.isCSKY();
299 if ((IsMips || IsCSKY) && !SysRoot.empty())
300 ExtraOpts.push_back(
"--sysroot=" + SysRoot);
308 if (!IsMips && !IsHexagon) {
311 (IsAndroid && Triple.isAndroidVersionLT(23)))
312 ExtraOpts.push_back(
"--hash-style=both");
317#ifdef ENABLE_LINKER_BUILD_ID
328 const std::string OSLibDir = std::string(
getOSLibDir(Triple, Args));
333 if (Arch == llvm::Triple::mips || Arch == llvm::Triple::mipsel) {
335 addPathIfExists(
D,
concat(SysRoot,
"/libo32"), Paths);
336 addPathIfExists(
D,
concat(SysRoot,
"/usr/libo32"), Paths);
340 addPathIfExists(
D,
concat(SysRoot,
"/lib", MultiarchTriple), Paths);
341 addPathIfExists(
D,
concat(SysRoot,
"/lib/..", OSLibDir), Paths);
349 concat(SysRoot,
"/usr/lib", MultiarchTriple,
350 llvm::to_string(Triple.getEnvironmentVersion().getMajor())),
354 addPathIfExists(
D,
concat(SysRoot,
"/usr/lib", MultiarchTriple), Paths);
358 if (Triple.getVendor() == llvm::Triple::OpenEmbedded &&
359 Triple.isArch64Bit())
360 addPathIfExists(
D,
concat(SysRoot,
"/usr", OSLibDir), Paths);
362 addPathIfExists(
D,
concat(SysRoot,
"/usr/lib/..", OSLibDir), Paths);
365 addPathIfExists(
D,
concat(SysRoot,
"/", OSLibDir, ABIName), Paths);
366 addPathIfExists(
D,
concat(SysRoot,
"/usr", OSLibDir, ABIName), Paths);
371 addPathIfExists(
D,
concat(SysRoot,
"/lib"), Paths);
372 addPathIfExists(
D,
concat(SysRoot,
"/usr/lib"), Paths);
413 std::string AndroidSysRootPath = (ClangDir +
"/../sysroot").str();
414 if (
getVFS().exists(AndroidSysRootPath))
415 return AndroidSysRootPath;
421 return std::string();
430 return std::string();
434 return std::string();
456 return std::string();
460 const llvm::Triple::ArchType Arch =
getArch();
461 const llvm::Triple &Triple =
getTriple();
465 if (Triple.isAndroid()) {
467 !Triple.isAndroidVersionLT(34) && Triple.isArch64Bit()) {
472 return "/system/bin/linker_hwasan64";
474 return Triple.isArch64Bit() ?
"/system/bin/linker64" :
"/system/bin/linker";
476 if (Triple.isMusl()) {
477 std::string ArchName;
481 case llvm::Triple::arm:
482 case llvm::Triple::thumb:
486 case llvm::Triple::armeb:
487 case llvm::Triple::thumbeb:
491 case llvm::Triple::x86:
494 case llvm::Triple::x86_64:
495 ArchName = Triple.isX32() ?
"x32" : Triple.getArchName().str();
498 ArchName = Triple.getArchName().str();
501 (Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
504 if (Arch == llvm::Triple::ppc &&
505 Triple.getSubArch() == llvm::Triple::PPCSubArch_spe)
506 ArchName =
"powerpc-sf";
508 return "/lib/ld-musl-" + ArchName +
".so.1";
516 llvm_unreachable(
"unsupported architecture");
518 case llvm::Triple::aarch64:
520 Loader =
"ld-linux-aarch64.so.1";
522 case llvm::Triple::aarch64_be:
524 Loader =
"ld-linux-aarch64_be.so.1";
526 case llvm::Triple::arm:
527 case llvm::Triple::thumb:
528 case llvm::Triple::armeb:
529 case llvm::Triple::thumbeb: {
531 Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
532 Triple.getEnvironment() == llvm::Triple::GNUEABIHFT64 ||
536 Loader = HF ?
"ld-linux-armhf.so.3" :
"ld-linux.so.3";
539 case llvm::Triple::loongarch32: {
542 (
"ld-linux-loongarch-" +
547 case llvm::Triple::loongarch64: {
550 (
"ld-linux-loongarch-" +
555 case llvm::Triple::m68k:
559 case llvm::Triple::mips:
560 case llvm::Triple::mipsel:
561 case llvm::Triple::mips64:
562 case llvm::Triple::mips64el: {
568 Loader = IsNaN2008 ?
"ld-uClibc-mipsn8.so.0" :
"ld-uClibc.so.0";
569 else if (!Triple.hasEnvironment() &&
570 Triple.getVendor() == llvm::Triple::VendorType::MipsTechnologies)
572 Triple.isLittleEndian() ?
"ld-musl-mipsel.so.1" :
"ld-musl-mips.so.1";
574 Loader = IsNaN2008 ?
"ld-linux-mipsn8.so.1" :
"ld.so.1";
578 case llvm::Triple::ppc:
582 case llvm::Triple::ppcle:
586 case llvm::Triple::ppc64:
591 case llvm::Triple::ppc64le:
596 case llvm::Triple::riscv32:
597 case llvm::Triple::riscv64: {
598 StringRef ArchName = llvm::Triple::getArchTypeName(Arch);
601 Loader = (
"ld-linux-" + ArchName +
"-" + ABIName +
".so.1").str();
604 case llvm::Triple::sparc:
605 case llvm::Triple::sparcel:
607 Loader =
"ld-linux.so.2";
609 case llvm::Triple::sparcv9:
611 Loader =
"ld-linux.so.2";
613 case llvm::Triple::systemz:
615 Loader =
"ld64.so.1";
617 case llvm::Triple::x86:
619 Loader =
"ld-linux.so.2";
621 case llvm::Triple::x86_64: {
622 bool X32 = Triple.isX32();
624 LibDir = X32 ?
"libx32" :
"lib64";
625 Loader = X32 ?
"ld-linux-x32.so.2" :
"ld-linux-x86-64.so.2";
628 case llvm::Triple::ve:
629 return "/opt/nec/ve/lib/ld-linux-ve.so.1";
630 case llvm::Triple::csky: {
638 (Triple.getVendor() == llvm::Triple::UnknownVendor ||
639 Triple.getVendor() == llvm::Triple::PC))
640 return "/usr/" + Triple.str() +
"/lib/" + Loader;
641 return "/" + LibDir +
"/" + Loader;
645 ArgStringList &CC1Args)
const {
649 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
657 llvm::sys::path::append(ResourceDirInclude,
"include");
658 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) &&
659 (!
getTriple().isMusl() || DriverArgs.hasArg(options::OPT_nostdlibinc)))
662 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
671 StringRef CIncludeDirs(C_INCLUDE_DIRS);
672 if (CIncludeDirs !=
"") {
674 CIncludeDirs.split(dirs,
":");
675 for (StringRef dir : dirs) {
677 llvm::sys::path::is_absolute(dir) ?
"" : StringRef(SysRoot);
686 if (!MultiarchIncludeDir.empty() &&
687 D.getVFS().exists(
concat(SysRoot,
"/usr/include", MultiarchIncludeDir)))
690 concat(SysRoot,
"/usr/include", MultiarchIncludeDir));
702 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) &&
getTriple().isMusl())
707 llvm::opt::ArgStringList &CC1Args)
const {
715 StringRef DebianMultiarch =
729 const std::string LibStdCXXIncludePathCandidates[] = {
731 LibDir.str() +
"/../" + TripleStr.str() +
"/include/c++/" + Version.
Text,
734 LibDir.str() +
"/../include/c++",
737 LibDir.str() +
"/../include/g++",
740 for (
const auto &IncludePath : LibStdCXXIncludePathCandidates) {
748 ArgStringList &CC1Args)
const {
753 ArgStringList &CC1Args)
const {
758 ArgStringList &CmdArgs)
const {
762 if (Args.hasFlag(options::OPT_frtlib_add_rpath,
763 options::OPT_fno_rtlib_add_rpath,
false))
767 CmdArgs.push_back(
"-lamdhip64");
771 ArgStringList &CC1Args)
const {
773 CC1Args.push_back(
"-isystem");
774 CC1Args.push_back(DriverArgs.MakeArgString(
781 return CLANG_DEFAULT_PIE_ON_LINUX ||
getTriple().isAndroid() ||
788 assert(
getTriple().isAArch64() &&
"expected AArch64 target!");
805 const bool IsX86 =
getTriple().getArch() == llvm::Triple::x86;
806 const bool IsX86_64 =
getTriple().getArch() == llvm::Triple::x86_64;
807 const bool IsMIPS =
getTriple().isMIPS32();
808 const bool IsMIPS64 =
getTriple().isMIPS64();
809 const bool IsPowerPC64 =
getTriple().getArch() == llvm::Triple::ppc64 ||
810 getTriple().getArch() == llvm::Triple::ppc64le;
811 const bool IsAArch64 =
getTriple().getArch() == llvm::Triple::aarch64 ||
812 getTriple().getArch() == llvm::Triple::aarch64_be;
813 const bool IsArmArch =
getTriple().getArch() == llvm::Triple::arm ||
814 getTriple().getArch() == llvm::Triple::thumb ||
815 getTriple().getArch() == llvm::Triple::armeb ||
816 getTriple().getArch() == llvm::Triple::thumbeb;
817 const bool IsLoongArch64 =
getTriple().getArch() == llvm::Triple::loongarch64;
818 const bool IsRISCV64 =
getTriple().getArch() == llvm::Triple::riscv64;
819 const bool IsSystemZ =
getTriple().getArch() == llvm::Triple::systemz;
820 const bool IsHexagon =
getTriple().getArch() == llvm::Triple::hexagon;
822 Res |= SanitizerKind::Address;
823 Res |= SanitizerKind::PointerCompare;
824 Res |= SanitizerKind::PointerSubtract;
825 Res |= SanitizerKind::Realtime;
826 Res |= SanitizerKind::Fuzzer;
827 Res |= SanitizerKind::FuzzerNoLink;
828 Res |= SanitizerKind::KernelAddress;
829 Res |= SanitizerKind::Memory;
830 Res |= SanitizerKind::Vptr;
831 Res |= SanitizerKind::SafeStack;
832 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsLoongArch64)
833 Res |= SanitizerKind::DataFlow;
834 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsArmArch || IsPowerPC64 ||
835 IsRISCV64 || IsSystemZ || IsHexagon || IsLoongArch64)
836 Res |= SanitizerKind::Leak;
837 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsPowerPC64 || IsSystemZ ||
838 IsLoongArch64 || IsRISCV64)
839 Res |= SanitizerKind::Thread;
840 if (IsX86_64 || IsAArch64)
841 Res |= SanitizerKind::Type;
842 if (IsX86_64 || IsSystemZ || IsPowerPC64)
843 Res |= SanitizerKind::KernelMemory;
844 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsMIPS || IsArmArch ||
845 IsPowerPC64 || IsHexagon || IsLoongArch64 || IsRISCV64)
846 Res |= SanitizerKind::Scudo;
847 if (IsX86_64 || IsAArch64 || IsRISCV64) {
848 Res |= SanitizerKind::HWAddress;
850 if (IsX86_64 || IsAArch64) {
851 Res |= SanitizerKind::KernelHWAddress;
854 Res |= SanitizerKind::NumericalStability;
863 llvm::opt::ArgStringList &CmdArgs)
const {
867 CmdArgs.push_back(Args.MakeArgString(
868 Twine(
"-u", llvm::getInstrProfRuntimeHookVarName())));
874 CmdArgs.push_back(Opt.c_str());
static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args)
Distro - Helper class for detecting and classifying Linux distributions.
bool IsAlpineLinux() const
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
std::string SysRoot
sysroot, if present
std::string Dir
The path the driver executable was in, as invoked from the command line.
This corresponds to a single GCC Multilib, or a segment of one controlled by a command line flag.
const std::string & osSuffix() const
Get the detected os path suffix for the multi-arch target variant.
const std::string & includeSuffix() const
Get the include directory suffix.
The JSON file list parser is used to communicate input to InstallAPI.