1 //===--- ToolChain.cpp - Collections of tools for one platform ------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "Tools.h" 11 #include "clang/Basic/ObjCRuntime.h" 12 #include "clang/Driver/Action.h" 13 #include "clang/Driver/Driver.h" 14 #include "clang/Driver/DriverDiagnostic.h" 15 #include "clang/Driver/Options.h" 16 #include "clang/Driver/SanitizerArgs.h" 17 #include "clang/Driver/ToolChain.h" 18 #include "llvm/ADT/SmallString.h" 19 #include "llvm/ADT/StringSwitch.h" 20 #include "llvm/Option/Arg.h" 21 #include "llvm/Option/ArgList.h" 22 #include "llvm/Option/Option.h" 23 #include "llvm/Support/ErrorHandling.h" 24 #include "llvm/Support/FileSystem.h" 25 #include "llvm/Support/TargetRegistry.h" 26 #include "llvm/Support/TargetParser.h" 27 28 using namespace clang::driver; 29 using namespace clang::driver::tools; 30 using namespace clang; 31 using namespace llvm; 32 using namespace llvm::opt; 33 34 static llvm::opt::Arg *GetRTTIArgument(const ArgList &Args) { 35 return Args.getLastArg(options::OPT_mkernel, options::OPT_fapple_kext, 36 options::OPT_fno_rtti, options::OPT_frtti); 37 } 38 39 static ToolChain::RTTIMode CalculateRTTIMode(const ArgList &Args, 40 const llvm::Triple &Triple, 41 const Arg *CachedRTTIArg) { 42 // Explicit rtti/no-rtti args 43 if (CachedRTTIArg) { 44 if (CachedRTTIArg->getOption().matches(options::OPT_frtti)) 45 return ToolChain::RM_EnabledExplicitly; 46 else 47 return ToolChain::RM_DisabledExplicitly; 48 } 49 50 // -frtti is default, except for the PS4 CPU. 51 if (!Triple.isPS4CPU()) 52 return ToolChain::RM_EnabledImplicitly; 53 54 // On the PS4, turning on c++ exceptions turns on rtti. 55 // We're assuming that, if we see -fexceptions, rtti gets turned on. 56 Arg *Exceptions = Args.getLastArgNoClaim( 57 options::OPT_fcxx_exceptions, options::OPT_fno_cxx_exceptions, 58 options::OPT_fexceptions, options::OPT_fno_exceptions); 59 if (Exceptions && 60 (Exceptions->getOption().matches(options::OPT_fexceptions) || 61 Exceptions->getOption().matches(options::OPT_fcxx_exceptions))) 62 return ToolChain::RM_EnabledImplicitly; 63 64 return ToolChain::RM_DisabledImplicitly; 65 } 66 67 ToolChain::ToolChain(const Driver &D, const llvm::Triple &T, 68 const ArgList &Args) 69 : D(D), Triple(T), Args(Args), CachedRTTIArg(GetRTTIArgument(Args)), 70 CachedRTTIMode(CalculateRTTIMode(Args, Triple, CachedRTTIArg)) { 71 if (Arg *A = Args.getLastArg(options::OPT_mthread_model)) 72 if (!isThreadModelSupported(A->getValue())) 73 D.Diag(diag::err_drv_invalid_thread_model_for_target) 74 << A->getValue() << A->getAsString(Args); 75 } 76 77 ToolChain::~ToolChain() { 78 } 79 80 vfs::FileSystem &ToolChain::getVFS() const { return getDriver().getVFS(); } 81 82 bool ToolChain::useIntegratedAs() const { 83 return Args.hasFlag(options::OPT_fintegrated_as, 84 options::OPT_fno_integrated_as, 85 IsIntegratedAssemblerDefault()); 86 } 87 88 const SanitizerArgs& ToolChain::getSanitizerArgs() const { 89 if (!SanitizerArguments.get()) 90 SanitizerArguments.reset(new SanitizerArgs(*this, Args)); 91 return *SanitizerArguments.get(); 92 } 93 94 namespace { 95 struct DriverSuffix { 96 const char *Suffix; 97 const char *ModeFlag; 98 }; 99 100 const DriverSuffix *FindDriverSuffix(StringRef ProgName) { 101 // A list of known driver suffixes. Suffixes are compared against the 102 // program name in order. If there is a match, the frontend type is updated as 103 // necessary by applying the ModeFlag. 104 static const DriverSuffix DriverSuffixes[] = { 105 {"clang", nullptr}, 106 {"clang++", "--driver-mode=g++"}, 107 {"clang-c++", "--driver-mode=g++"}, 108 {"clang-cc", nullptr}, 109 {"clang-cpp", "--driver-mode=cpp"}, 110 {"clang-g++", "--driver-mode=g++"}, 111 {"clang-gcc", nullptr}, 112 {"clang-cl", "--driver-mode=cl"}, 113 {"cc", nullptr}, 114 {"cpp", "--driver-mode=cpp"}, 115 {"cl", "--driver-mode=cl"}, 116 {"++", "--driver-mode=g++"}, 117 }; 118 119 for (size_t i = 0; i < llvm::array_lengthof(DriverSuffixes); ++i) 120 if (ProgName.endswith(DriverSuffixes[i].Suffix)) 121 return &DriverSuffixes[i]; 122 return nullptr; 123 } 124 125 /// Normalize the program name from argv[0] by stripping the file extension if 126 /// present and lower-casing the string on Windows. 127 std::string normalizeProgramName(llvm::StringRef Argv0) { 128 std::string ProgName = llvm::sys::path::stem(Argv0); 129 #ifdef LLVM_ON_WIN32 130 // Transform to lowercase for case insensitive file systems. 131 std::transform(ProgName.begin(), ProgName.end(), ProgName.begin(), ::tolower); 132 #endif 133 return ProgName; 134 } 135 136 const DriverSuffix *parseDriverSuffix(StringRef ProgName) { 137 // Try to infer frontend type and default target from the program name by 138 // comparing it against DriverSuffixes in order. 139 140 // If there is a match, the function tries to identify a target as prefix. 141 // E.g. "x86_64-linux-clang" as interpreted as suffix "clang" with target 142 // prefix "x86_64-linux". If such a target prefix is found, it may be 143 // added via -target as implicit first argument. 144 const DriverSuffix *DS = FindDriverSuffix(ProgName); 145 146 if (!DS) { 147 // Try again after stripping any trailing version number: 148 // clang++3.5 -> clang++ 149 ProgName = ProgName.rtrim("0123456789."); 150 DS = FindDriverSuffix(ProgName); 151 } 152 153 if (!DS) { 154 // Try again after stripping trailing -component. 155 // clang++-tot -> clang++ 156 ProgName = ProgName.slice(0, ProgName.rfind('-')); 157 DS = FindDriverSuffix(ProgName); 158 } 159 return DS; 160 } 161 } // anonymous namespace 162 163 std::pair<std::string, std::string> 164 ToolChain::getTargetAndModeFromProgramName(StringRef PN) { 165 std::string ProgName = normalizeProgramName(PN); 166 const DriverSuffix *DS = parseDriverSuffix(ProgName); 167 if (!DS) 168 return std::make_pair("", ""); 169 std::string ModeFlag = DS->ModeFlag == nullptr ? "" : DS->ModeFlag; 170 171 std::string::size_type LastComponent = 172 ProgName.rfind('-', ProgName.size() - strlen(DS->Suffix)); 173 if (LastComponent == std::string::npos) 174 return std::make_pair("", ModeFlag); 175 176 // Infer target from the prefix. 177 StringRef Prefix(ProgName); 178 Prefix = Prefix.slice(0, LastComponent); 179 std::string IgnoredError; 180 std::string Target; 181 if (llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError)) { 182 Target = Prefix; 183 } 184 return std::make_pair(Target, ModeFlag); 185 } 186 187 StringRef ToolChain::getDefaultUniversalArchName() const { 188 // In universal driver terms, the arch name accepted by -arch isn't exactly 189 // the same as the ones that appear in the triple. Roughly speaking, this is 190 // an inverse of the darwin::getArchTypeForDarwinArchName() function, but the 191 // only interesting special case is powerpc. 192 switch (Triple.getArch()) { 193 case llvm::Triple::ppc: 194 return "ppc"; 195 case llvm::Triple::ppc64: 196 return "ppc64"; 197 case llvm::Triple::ppc64le: 198 return "ppc64le"; 199 default: 200 return Triple.getArchName(); 201 } 202 } 203 204 bool ToolChain::IsUnwindTablesDefault() const { 205 return false; 206 } 207 208 Tool *ToolChain::getClang() const { 209 if (!Clang) 210 Clang.reset(new tools::Clang(*this)); 211 return Clang.get(); 212 } 213 214 Tool *ToolChain::buildAssembler() const { 215 return new tools::ClangAs(*this); 216 } 217 218 Tool *ToolChain::buildLinker() const { 219 llvm_unreachable("Linking is not supported by this toolchain"); 220 } 221 222 Tool *ToolChain::getAssemble() const { 223 if (!Assemble) 224 Assemble.reset(buildAssembler()); 225 return Assemble.get(); 226 } 227 228 Tool *ToolChain::getClangAs() const { 229 if (!Assemble) 230 Assemble.reset(new tools::ClangAs(*this)); 231 return Assemble.get(); 232 } 233 234 Tool *ToolChain::getLink() const { 235 if (!Link) 236 Link.reset(buildLinker()); 237 return Link.get(); 238 } 239 240 Tool *ToolChain::getTool(Action::ActionClass AC) const { 241 switch (AC) { 242 case Action::AssembleJobClass: 243 return getAssemble(); 244 245 case Action::LinkJobClass: 246 return getLink(); 247 248 case Action::InputClass: 249 case Action::BindArchClass: 250 case Action::CudaDeviceClass: 251 case Action::CudaHostClass: 252 case Action::LipoJobClass: 253 case Action::DsymutilJobClass: 254 case Action::VerifyDebugInfoJobClass: 255 llvm_unreachable("Invalid tool kind."); 256 257 case Action::CompileJobClass: 258 case Action::PrecompileJobClass: 259 case Action::PreprocessJobClass: 260 case Action::AnalyzeJobClass: 261 case Action::MigrateJobClass: 262 case Action::VerifyPCHJobClass: 263 case Action::BackendJobClass: 264 return getClang(); 265 } 266 267 llvm_unreachable("Invalid tool kind."); 268 } 269 270 static StringRef getArchNameForCompilerRTLib(const ToolChain &TC, 271 const ArgList &Args) { 272 const llvm::Triple &Triple = TC.getTriple(); 273 bool IsWindows = Triple.isOSWindows(); 274 275 if (Triple.isWindowsMSVCEnvironment() && TC.getArch() == llvm::Triple::x86) 276 return "i386"; 277 278 if (TC.getArch() == llvm::Triple::arm || TC.getArch() == llvm::Triple::armeb) 279 return (arm::getARMFloatABI(TC, Args) == arm::FloatABI::Hard && !IsWindows) 280 ? "armhf" 281 : "arm"; 282 283 return TC.getArchName(); 284 } 285 286 std::string ToolChain::getCompilerRT(const ArgList &Args, StringRef Component, 287 bool Shared) const { 288 const llvm::Triple &TT = getTriple(); 289 const char *Env = TT.isAndroid() ? "-android" : ""; 290 bool IsITANMSVCWindows = 291 TT.isWindowsMSVCEnvironment() || TT.isWindowsItaniumEnvironment(); 292 293 StringRef Arch = getArchNameForCompilerRTLib(*this, Args); 294 const char *Prefix = IsITANMSVCWindows ? "" : "lib"; 295 const char *Suffix = Shared ? (Triple.isOSWindows() ? ".dll" : ".so") 296 : (IsITANMSVCWindows ? ".lib" : ".a"); 297 298 SmallString<128> Path(getDriver().ResourceDir); 299 StringRef OSLibName = Triple.isOSFreeBSD() ? "freebsd" : getOS(); 300 llvm::sys::path::append(Path, "lib", OSLibName); 301 llvm::sys::path::append(Path, Prefix + Twine("clang_rt.") + Component + "-" + 302 Arch + Env + Suffix); 303 return Path.str(); 304 } 305 306 const char *ToolChain::getCompilerRTArgString(const llvm::opt::ArgList &Args, 307 StringRef Component, 308 bool Shared) const { 309 return Args.MakeArgString(getCompilerRT(Args, Component, Shared)); 310 } 311 312 bool ToolChain::needsProfileRT(const ArgList &Args) { 313 if (Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs, 314 false) || 315 Args.hasArg(options::OPT_fprofile_generate) || 316 Args.hasArg(options::OPT_fprofile_generate_EQ) || 317 Args.hasArg(options::OPT_fprofile_instr_generate) || 318 Args.hasArg(options::OPT_fprofile_instr_generate_EQ) || 319 Args.hasArg(options::OPT_fcreate_profile) || 320 Args.hasArg(options::OPT_coverage)) 321 return true; 322 323 return false; 324 } 325 326 Tool *ToolChain::SelectTool(const JobAction &JA) const { 327 if (getDriver().ShouldUseClangCompiler(JA)) return getClang(); 328 Action::ActionClass AC = JA.getKind(); 329 if (AC == Action::AssembleJobClass && useIntegratedAs()) 330 return getClangAs(); 331 return getTool(AC); 332 } 333 334 std::string ToolChain::GetFilePath(const char *Name) const { 335 return D.GetFilePath(Name, *this); 336 337 } 338 339 std::string ToolChain::GetProgramPath(const char *Name) const { 340 return D.GetProgramPath(Name, *this); 341 } 342 343 std::string ToolChain::GetLinkerPath() const { 344 if (Arg *A = Args.getLastArg(options::OPT_fuse_ld_EQ)) { 345 StringRef Suffix = A->getValue(); 346 347 // If we're passed -fuse-ld= with no argument, or with the argument ld, 348 // then use whatever the default system linker is. 349 if (Suffix.empty() || Suffix == "ld") 350 return GetProgramPath("ld"); 351 352 llvm::SmallString<8> LinkerName("ld."); 353 LinkerName.append(Suffix); 354 355 std::string LinkerPath(GetProgramPath(LinkerName.c_str())); 356 if (llvm::sys::fs::exists(LinkerPath)) 357 return LinkerPath; 358 359 getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args); 360 return ""; 361 } 362 363 return GetProgramPath("ld"); 364 } 365 366 types::ID ToolChain::LookupTypeForExtension(const char *Ext) const { 367 return types::lookupTypeForExtension(Ext); 368 } 369 370 bool ToolChain::HasNativeLLVMSupport() const { 371 return false; 372 } 373 374 bool ToolChain::isCrossCompiling() const { 375 llvm::Triple HostTriple(LLVM_HOST_TRIPLE); 376 switch (HostTriple.getArch()) { 377 // The A32/T32/T16 instruction sets are not separate architectures in this 378 // context. 379 case llvm::Triple::arm: 380 case llvm::Triple::armeb: 381 case llvm::Triple::thumb: 382 case llvm::Triple::thumbeb: 383 return getArch() != llvm::Triple::arm && getArch() != llvm::Triple::thumb && 384 getArch() != llvm::Triple::armeb && getArch() != llvm::Triple::thumbeb; 385 default: 386 return HostTriple.getArch() != getArch(); 387 } 388 } 389 390 ObjCRuntime ToolChain::getDefaultObjCRuntime(bool isNonFragile) const { 391 return ObjCRuntime(isNonFragile ? ObjCRuntime::GNUstep : ObjCRuntime::GCC, 392 VersionTuple()); 393 } 394 395 bool ToolChain::isThreadModelSupported(const StringRef Model) const { 396 if (Model == "single") { 397 // FIXME: 'single' is only supported on ARM and WebAssembly so far. 398 return Triple.getArch() == llvm::Triple::arm || 399 Triple.getArch() == llvm::Triple::armeb || 400 Triple.getArch() == llvm::Triple::thumb || 401 Triple.getArch() == llvm::Triple::thumbeb || 402 Triple.getArch() == llvm::Triple::wasm32 || 403 Triple.getArch() == llvm::Triple::wasm64; 404 } else if (Model == "posix") 405 return true; 406 407 return false; 408 } 409 410 std::string ToolChain::ComputeLLVMTriple(const ArgList &Args, 411 types::ID InputType) const { 412 switch (getTriple().getArch()) { 413 default: 414 return getTripleString(); 415 416 case llvm::Triple::x86_64: { 417 llvm::Triple Triple = getTriple(); 418 if (!Triple.isOSBinFormatMachO()) 419 return getTripleString(); 420 421 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) { 422 // x86_64h goes in the triple. Other -march options just use the 423 // vanilla triple we already have. 424 StringRef MArch = A->getValue(); 425 if (MArch == "x86_64h") 426 Triple.setArchName(MArch); 427 } 428 return Triple.getTriple(); 429 } 430 case llvm::Triple::aarch64: { 431 llvm::Triple Triple = getTriple(); 432 if (!Triple.isOSBinFormatMachO()) 433 return getTripleString(); 434 435 // FIXME: older versions of ld64 expect the "arm64" component in the actual 436 // triple string and query it to determine whether an LTO file can be 437 // handled. Remove this when we don't care any more. 438 Triple.setArchName("arm64"); 439 return Triple.getTriple(); 440 } 441 case llvm::Triple::arm: 442 case llvm::Triple::armeb: 443 case llvm::Triple::thumb: 444 case llvm::Triple::thumbeb: { 445 // FIXME: Factor into subclasses. 446 llvm::Triple Triple = getTriple(); 447 bool IsBigEndian = getTriple().getArch() == llvm::Triple::armeb || 448 getTriple().getArch() == llvm::Triple::thumbeb; 449 450 // Handle pseudo-target flags '-mlittle-endian'/'-EL' and 451 // '-mbig-endian'/'-EB'. 452 if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian, 453 options::OPT_mbig_endian)) { 454 IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian); 455 } 456 457 // Thumb2 is the default for V7 on Darwin. 458 // 459 // FIXME: Thumb should just be another -target-feaure, not in the triple. 460 StringRef MCPU, MArch; 461 if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) 462 MCPU = A->getValue(); 463 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) 464 MArch = A->getValue(); 465 std::string CPU = 466 Triple.isOSBinFormatMachO() 467 ? tools::arm::getARMCPUForMArch(MArch, Triple).str() 468 : tools::arm::getARMTargetCPU(MCPU, MArch, Triple); 469 StringRef Suffix = 470 tools::arm::getLLVMArchSuffixForARM(CPU, MArch, Triple); 471 bool IsMProfile = ARM::parseArchProfile(Suffix) == ARM::PK_M; 472 bool ThumbDefault = IsMProfile || (ARM::parseArchVersion(Suffix) == 7 && 473 getTriple().isOSBinFormatMachO()); 474 // FIXME: this is invalid for WindowsCE 475 if (getTriple().isOSWindows()) 476 ThumbDefault = true; 477 std::string ArchName; 478 if (IsBigEndian) 479 ArchName = "armeb"; 480 else 481 ArchName = "arm"; 482 483 // Assembly files should start in ARM mode, unless arch is M-profile. 484 if (IsMProfile || (InputType != types::TY_PP_Asm && 485 Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault))) 486 { 487 if (IsBigEndian) 488 ArchName = "thumbeb"; 489 else 490 ArchName = "thumb"; 491 } 492 Triple.setArchName(ArchName + Suffix.str()); 493 494 return Triple.getTriple(); 495 } 496 } 497 } 498 499 std::string ToolChain::ComputeEffectiveClangTriple(const ArgList &Args, 500 types::ID InputType) const { 501 return ComputeLLVMTriple(Args, InputType); 502 } 503 504 void ToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 505 ArgStringList &CC1Args) const { 506 // Each toolchain should provide the appropriate include flags. 507 } 508 509 void ToolChain::addClangTargetOptions(const ArgList &DriverArgs, 510 ArgStringList &CC1Args) const { 511 } 512 513 void ToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {} 514 515 void ToolChain::addProfileRTLibs(const llvm::opt::ArgList &Args, 516 llvm::opt::ArgStringList &CmdArgs) const { 517 if (!needsProfileRT(Args)) return; 518 519 CmdArgs.push_back(getCompilerRTArgString(Args, "profile")); 520 return; 521 } 522 523 ToolChain::RuntimeLibType ToolChain::GetRuntimeLibType( 524 const ArgList &Args) const { 525 if (Arg *A = Args.getLastArg(options::OPT_rtlib_EQ)) { 526 StringRef Value = A->getValue(); 527 if (Value == "compiler-rt") 528 return ToolChain::RLT_CompilerRT; 529 if (Value == "libgcc") 530 return ToolChain::RLT_Libgcc; 531 getDriver().Diag(diag::err_drv_invalid_rtlib_name) 532 << A->getAsString(Args); 533 } 534 535 return GetDefaultRuntimeLibType(); 536 } 537 538 ToolChain::CXXStdlibType ToolChain::GetCXXStdlibType(const ArgList &Args) const{ 539 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) { 540 StringRef Value = A->getValue(); 541 if (Value == "libc++") 542 return ToolChain::CST_Libcxx; 543 if (Value == "libstdc++") 544 return ToolChain::CST_Libstdcxx; 545 getDriver().Diag(diag::err_drv_invalid_stdlib_name) 546 << A->getAsString(Args); 547 } 548 549 return ToolChain::CST_Libstdcxx; 550 } 551 552 /// \brief Utility function to add a system include directory to CC1 arguments. 553 /*static*/ void ToolChain::addSystemInclude(const ArgList &DriverArgs, 554 ArgStringList &CC1Args, 555 const Twine &Path) { 556 CC1Args.push_back("-internal-isystem"); 557 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 558 } 559 560 /// \brief Utility function to add a system include directory with extern "C" 561 /// semantics to CC1 arguments. 562 /// 563 /// Note that this should be used rarely, and only for directories that 564 /// historically and for legacy reasons are treated as having implicit extern 565 /// "C" semantics. These semantics are *ignored* by and large today, but its 566 /// important to preserve the preprocessor changes resulting from the 567 /// classification. 568 /*static*/ void ToolChain::addExternCSystemInclude(const ArgList &DriverArgs, 569 ArgStringList &CC1Args, 570 const Twine &Path) { 571 CC1Args.push_back("-internal-externc-isystem"); 572 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 573 } 574 575 void ToolChain::addExternCSystemIncludeIfExists(const ArgList &DriverArgs, 576 ArgStringList &CC1Args, 577 const Twine &Path) { 578 if (llvm::sys::fs::exists(Path)) 579 addExternCSystemInclude(DriverArgs, CC1Args, Path); 580 } 581 582 /// \brief Utility function to add a list of system include directories to CC1. 583 /*static*/ void ToolChain::addSystemIncludes(const ArgList &DriverArgs, 584 ArgStringList &CC1Args, 585 ArrayRef<StringRef> Paths) { 586 for (StringRef Path : Paths) { 587 CC1Args.push_back("-internal-isystem"); 588 CC1Args.push_back(DriverArgs.MakeArgString(Path)); 589 } 590 } 591 592 void ToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 593 ArgStringList &CC1Args) const { 594 // Header search paths should be handled by each of the subclasses. 595 // Historically, they have not been, and instead have been handled inside of 596 // the CC1-layer frontend. As the logic is hoisted out, this generic function 597 // will slowly stop being called. 598 // 599 // While it is being called, replicate a bit of a hack to propagate the 600 // '-stdlib=' flag down to CC1 so that it can in turn customize the C++ 601 // header search paths with it. Once all systems are overriding this 602 // function, the CC1 flag and this line can be removed. 603 DriverArgs.AddAllArgs(CC1Args, options::OPT_stdlib_EQ); 604 } 605 606 void ToolChain::AddCXXStdlibLibArgs(const ArgList &Args, 607 ArgStringList &CmdArgs) const { 608 CXXStdlibType Type = GetCXXStdlibType(Args); 609 610 switch (Type) { 611 case ToolChain::CST_Libcxx: 612 CmdArgs.push_back("-lc++"); 613 break; 614 615 case ToolChain::CST_Libstdcxx: 616 CmdArgs.push_back("-lstdc++"); 617 break; 618 } 619 } 620 621 void ToolChain::AddCCKextLibArgs(const ArgList &Args, 622 ArgStringList &CmdArgs) const { 623 CmdArgs.push_back("-lcc_kext"); 624 } 625 626 bool ToolChain::AddFastMathRuntimeIfAvailable(const ArgList &Args, 627 ArgStringList &CmdArgs) const { 628 // Do not check for -fno-fast-math or -fno-unsafe-math when -Ofast passed 629 // (to keep the linker options consistent with gcc and clang itself). 630 if (!isOptimizationLevelFast(Args)) { 631 // Check if -ffast-math or -funsafe-math. 632 Arg *A = 633 Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math, 634 options::OPT_funsafe_math_optimizations, 635 options::OPT_fno_unsafe_math_optimizations); 636 637 if (!A || A->getOption().getID() == options::OPT_fno_fast_math || 638 A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations) 639 return false; 640 } 641 // If crtfastmath.o exists add it to the arguments. 642 std::string Path = GetFilePath("crtfastmath.o"); 643 if (Path == "crtfastmath.o") // Not found. 644 return false; 645 646 CmdArgs.push_back(Args.MakeArgString(Path)); 647 return true; 648 } 649 650 SanitizerMask ToolChain::getSupportedSanitizers() const { 651 // Return sanitizers which don't require runtime support and are not 652 // platform dependent. 653 using namespace SanitizerKind; 654 SanitizerMask Res = (Undefined & ~Vptr & ~Function) | (CFI & ~CFIICall) | 655 CFICastStrict | UnsignedIntegerOverflow | LocalBounds; 656 if (getTriple().getArch() == llvm::Triple::x86 || 657 getTriple().getArch() == llvm::Triple::x86_64) 658 Res |= CFIICall; 659 return Res; 660 } 661