1 //===--- Gnu.cpp - Gnu Tool and 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 "Gnu.h" 10 #include "Arch/ARM.h" 11 #include "Arch/Mips.h" 12 #include "Arch/PPC.h" 13 #include "Arch/RISCV.h" 14 #include "Arch/Sparc.h" 15 #include "Arch/SystemZ.h" 16 #include "CommonArgs.h" 17 #include "Linux.h" 18 #include "clang/Config/config.h" // for GCC_INSTALL_PREFIX 19 #include "clang/Driver/Compilation.h" 20 #include "clang/Driver/Driver.h" 21 #include "clang/Driver/DriverDiagnostic.h" 22 #include "clang/Driver/Options.h" 23 #include "clang/Driver/Tool.h" 24 #include "clang/Driver/ToolChain.h" 25 #include "llvm/Option/ArgList.h" 26 #include "llvm/Support/CodeGen.h" 27 #include "llvm/Support/Path.h" 28 #include "llvm/Support/TargetParser.h" 29 #include "llvm/Support/VirtualFileSystem.h" 30 #include <system_error> 31 32 using namespace clang::driver; 33 using namespace clang::driver::toolchains; 34 using namespace clang; 35 using namespace llvm::opt; 36 37 using tools::addMultilibFlag; 38 39 void tools::GnuTool::anchor() {} 40 41 static bool forwardToGCC(const Option &O) { 42 // Don't forward inputs from the original command line. They are added from 43 // InputInfoList. 44 return O.getKind() != Option::InputClass && 45 !O.hasFlag(options::DriverOption) && !O.hasFlag(options::LinkerInput); 46 } 47 48 // Switch CPU names not recognized by GNU assembler to a close CPU that it does 49 // recognize, instead of a lower march from being picked in the absence of a cpu 50 // flag. 51 static void normalizeCPUNamesForAssembler(const ArgList &Args, 52 ArgStringList &CmdArgs) { 53 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) { 54 StringRef CPUArg(A->getValue()); 55 if (CPUArg.equals_lower("krait")) 56 CmdArgs.push_back("-mcpu=cortex-a15"); 57 else if(CPUArg.equals_lower("kryo")) 58 CmdArgs.push_back("-mcpu=cortex-a57"); 59 else 60 Args.AddLastArg(CmdArgs, options::OPT_mcpu_EQ); 61 } 62 } 63 64 void tools::gcc::Common::ConstructJob(Compilation &C, const JobAction &JA, 65 const InputInfo &Output, 66 const InputInfoList &Inputs, 67 const ArgList &Args, 68 const char *LinkingOutput) const { 69 const Driver &D = getToolChain().getDriver(); 70 ArgStringList CmdArgs; 71 72 for (const auto &A : Args) { 73 if (forwardToGCC(A->getOption())) { 74 // It is unfortunate that we have to claim here, as this means 75 // we will basically never report anything interesting for 76 // platforms using a generic gcc, even if we are just using gcc 77 // to get to the assembler. 78 A->claim(); 79 80 // Don't forward any -g arguments to assembly steps. 81 if (isa<AssembleJobAction>(JA) && 82 A->getOption().matches(options::OPT_g_Group)) 83 continue; 84 85 // Don't forward any -W arguments to assembly and link steps. 86 if ((isa<AssembleJobAction>(JA) || isa<LinkJobAction>(JA)) && 87 A->getOption().matches(options::OPT_W_Group)) 88 continue; 89 90 // Don't forward -mno-unaligned-access since GCC doesn't understand 91 // it and because it doesn't affect the assembly or link steps. 92 if ((isa<AssembleJobAction>(JA) || isa<LinkJobAction>(JA)) && 93 (A->getOption().matches(options::OPT_munaligned_access) || 94 A->getOption().matches(options::OPT_mno_unaligned_access))) 95 continue; 96 97 A->render(Args, CmdArgs); 98 } 99 } 100 101 RenderExtraToolArgs(JA, CmdArgs); 102 103 // If using a driver driver, force the arch. 104 if (getToolChain().getTriple().isOSDarwin()) { 105 CmdArgs.push_back("-arch"); 106 CmdArgs.push_back( 107 Args.MakeArgString(getToolChain().getDefaultUniversalArchName())); 108 } 109 110 // Try to force gcc to match the tool chain we want, if we recognize 111 // the arch. 112 // 113 // FIXME: The triple class should directly provide the information we want 114 // here. 115 switch (getToolChain().getArch()) { 116 default: 117 break; 118 case llvm::Triple::x86: 119 case llvm::Triple::ppc: 120 CmdArgs.push_back("-m32"); 121 break; 122 case llvm::Triple::x86_64: 123 case llvm::Triple::ppc64: 124 case llvm::Triple::ppc64le: 125 CmdArgs.push_back("-m64"); 126 break; 127 case llvm::Triple::sparcel: 128 CmdArgs.push_back("-EL"); 129 break; 130 } 131 132 if (Output.isFilename()) { 133 CmdArgs.push_back("-o"); 134 CmdArgs.push_back(Output.getFilename()); 135 } else { 136 assert(Output.isNothing() && "Unexpected output"); 137 CmdArgs.push_back("-fsyntax-only"); 138 } 139 140 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler); 141 142 // Only pass -x if gcc will understand it; otherwise hope gcc 143 // understands the suffix correctly. The main use case this would go 144 // wrong in is for linker inputs if they happened to have an odd 145 // suffix; really the only way to get this to happen is a command 146 // like '-x foobar a.c' which will treat a.c like a linker input. 147 // 148 // FIXME: For the linker case specifically, can we safely convert 149 // inputs into '-Wl,' options? 150 for (const auto &II : Inputs) { 151 // Don't try to pass LLVM or AST inputs to a generic gcc. 152 if (types::isLLVMIR(II.getType())) 153 D.Diag(clang::diag::err_drv_no_linker_llvm_support) 154 << getToolChain().getTripleString(); 155 else if (II.getType() == types::TY_AST) 156 D.Diag(diag::err_drv_no_ast_support) << getToolChain().getTripleString(); 157 else if (II.getType() == types::TY_ModuleFile) 158 D.Diag(diag::err_drv_no_module_support) 159 << getToolChain().getTripleString(); 160 161 if (types::canTypeBeUserSpecified(II.getType())) { 162 CmdArgs.push_back("-x"); 163 CmdArgs.push_back(types::getTypeName(II.getType())); 164 } 165 166 if (II.isFilename()) 167 CmdArgs.push_back(II.getFilename()); 168 else { 169 const Arg &A = II.getInputArg(); 170 171 // Reverse translate some rewritten options. 172 if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) { 173 CmdArgs.push_back("-lstdc++"); 174 continue; 175 } 176 177 // Don't render as input, we need gcc to do the translations. 178 A.render(Args, CmdArgs); 179 } 180 } 181 182 const std::string &customGCCName = D.getCCCGenericGCCName(); 183 const char *GCCName; 184 if (!customGCCName.empty()) 185 GCCName = customGCCName.c_str(); 186 else if (D.CCCIsCXX()) { 187 GCCName = "g++"; 188 } else 189 GCCName = "gcc"; 190 191 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath(GCCName)); 192 C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs)); 193 } 194 195 void tools::gcc::Preprocessor::RenderExtraToolArgs( 196 const JobAction &JA, ArgStringList &CmdArgs) const { 197 CmdArgs.push_back("-E"); 198 } 199 200 void tools::gcc::Compiler::RenderExtraToolArgs(const JobAction &JA, 201 ArgStringList &CmdArgs) const { 202 const Driver &D = getToolChain().getDriver(); 203 204 switch (JA.getType()) { 205 // If -flto, etc. are present then make sure not to force assembly output. 206 case types::TY_LLVM_IR: 207 case types::TY_LTO_IR: 208 case types::TY_LLVM_BC: 209 case types::TY_LTO_BC: 210 CmdArgs.push_back("-c"); 211 break; 212 // We assume we've got an "integrated" assembler in that gcc will produce an 213 // object file itself. 214 case types::TY_Object: 215 CmdArgs.push_back("-c"); 216 break; 217 case types::TY_PP_Asm: 218 CmdArgs.push_back("-S"); 219 break; 220 case types::TY_Nothing: 221 CmdArgs.push_back("-fsyntax-only"); 222 break; 223 default: 224 D.Diag(diag::err_drv_invalid_gcc_output_type) << getTypeName(JA.getType()); 225 } 226 } 227 228 void tools::gcc::Linker::RenderExtraToolArgs(const JobAction &JA, 229 ArgStringList &CmdArgs) const { 230 // The types are (hopefully) good enough. 231 } 232 233 // On Arm the endianness of the output file is determined by the target and 234 // can be overridden by the pseudo-target flags '-mlittle-endian'/'-EL' and 235 // '-mbig-endian'/'-EB'. Unlike other targets the flag does not result in a 236 // normalized triple so we must handle the flag here. 237 static bool isArmBigEndian(const llvm::Triple &Triple, 238 const ArgList &Args) { 239 bool IsBigEndian = false; 240 switch (Triple.getArch()) { 241 case llvm::Triple::armeb: 242 case llvm::Triple::thumbeb: 243 IsBigEndian = true; 244 LLVM_FALLTHROUGH; 245 case llvm::Triple::arm: 246 case llvm::Triple::thumb: 247 if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian, 248 options::OPT_mbig_endian)) 249 IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian); 250 break; 251 default: 252 break; 253 } 254 return IsBigEndian; 255 } 256 257 static const char *getLDMOption(const llvm::Triple &T, const ArgList &Args) { 258 switch (T.getArch()) { 259 case llvm::Triple::x86: 260 if (T.isOSIAMCU()) 261 return "elf_iamcu"; 262 return "elf_i386"; 263 case llvm::Triple::aarch64: 264 return "aarch64linux"; 265 case llvm::Triple::aarch64_be: 266 return "aarch64linuxb"; 267 case llvm::Triple::arm: 268 case llvm::Triple::thumb: 269 case llvm::Triple::armeb: 270 case llvm::Triple::thumbeb: 271 return isArmBigEndian(T, Args) ? "armelfb_linux_eabi" : "armelf_linux_eabi"; 272 case llvm::Triple::ppc: 273 return "elf32ppclinux"; 274 case llvm::Triple::ppc64: 275 return "elf64ppc"; 276 case llvm::Triple::ppc64le: 277 return "elf64lppc"; 278 case llvm::Triple::riscv32: 279 return "elf32lriscv"; 280 case llvm::Triple::riscv64: 281 return "elf64lriscv"; 282 case llvm::Triple::sparc: 283 case llvm::Triple::sparcel: 284 return "elf32_sparc"; 285 case llvm::Triple::sparcv9: 286 return "elf64_sparc"; 287 case llvm::Triple::mips: 288 return "elf32btsmip"; 289 case llvm::Triple::mipsel: 290 return "elf32ltsmip"; 291 case llvm::Triple::mips64: 292 if (tools::mips::hasMipsAbiArg(Args, "n32") || 293 T.getEnvironment() == llvm::Triple::GNUABIN32) 294 return "elf32btsmipn32"; 295 return "elf64btsmip"; 296 case llvm::Triple::mips64el: 297 if (tools::mips::hasMipsAbiArg(Args, "n32") || 298 T.getEnvironment() == llvm::Triple::GNUABIN32) 299 return "elf32ltsmipn32"; 300 return "elf64ltsmip"; 301 case llvm::Triple::systemz: 302 return "elf64_s390"; 303 case llvm::Triple::x86_64: 304 if (T.getEnvironment() == llvm::Triple::GNUX32) 305 return "elf32_x86_64"; 306 return "elf_x86_64"; 307 default: 308 return nullptr; 309 } 310 } 311 312 static bool getPIE(const ArgList &Args, const toolchains::Linux &ToolChain) { 313 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_static) || 314 Args.hasArg(options::OPT_r) || Args.hasArg(options::OPT_static_pie)) 315 return false; 316 317 Arg *A = Args.getLastArg(options::OPT_pie, options::OPT_no_pie, 318 options::OPT_nopie); 319 if (!A) 320 return ToolChain.isPIEDefault(); 321 return A->getOption().matches(options::OPT_pie); 322 } 323 324 static bool getStaticPIE(const ArgList &Args, 325 const toolchains::Linux &ToolChain) { 326 bool HasStaticPIE = Args.hasArg(options::OPT_static_pie); 327 // -no-pie is an alias for -nopie. So, handling -nopie takes care of 328 // -no-pie as well. 329 if (HasStaticPIE && Args.hasArg(options::OPT_nopie)) { 330 const Driver &D = ToolChain.getDriver(); 331 const llvm::opt::OptTable &Opts = D.getOpts(); 332 const char *StaticPIEName = Opts.getOptionName(options::OPT_static_pie); 333 const char *NoPIEName = Opts.getOptionName(options::OPT_nopie); 334 D.Diag(diag::err_drv_cannot_mix_options) << StaticPIEName << NoPIEName; 335 } 336 return HasStaticPIE; 337 } 338 339 static bool getStatic(const ArgList &Args) { 340 return Args.hasArg(options::OPT_static) && 341 !Args.hasArg(options::OPT_static_pie); 342 } 343 344 void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA, 345 const InputInfo &Output, 346 const InputInfoList &Inputs, 347 const ArgList &Args, 348 const char *LinkingOutput) const { 349 const toolchains::Linux &ToolChain = 350 static_cast<const toolchains::Linux &>(getToolChain()); 351 const Driver &D = ToolChain.getDriver(); 352 353 const llvm::Triple &Triple = getToolChain().getEffectiveTriple(); 354 355 const llvm::Triple::ArchType Arch = ToolChain.getArch(); 356 const bool isAndroid = ToolChain.getTriple().isAndroid(); 357 const bool IsIAMCU = ToolChain.getTriple().isOSIAMCU(); 358 const bool IsPIE = getPIE(Args, ToolChain); 359 const bool IsStaticPIE = getStaticPIE(Args, ToolChain); 360 const bool IsStatic = getStatic(Args); 361 const bool HasCRTBeginEndFiles = 362 ToolChain.getTriple().hasEnvironment() || 363 (ToolChain.getTriple().getVendor() != llvm::Triple::MipsTechnologies); 364 365 ArgStringList CmdArgs; 366 367 // Silence warning for "clang -g foo.o -o foo" 368 Args.ClaimAllArgs(options::OPT_g_Group); 369 // and "clang -emit-llvm foo.o -o foo" 370 Args.ClaimAllArgs(options::OPT_emit_llvm); 371 // and for "clang -w foo.o -o foo". Other warning options are already 372 // handled somewhere else. 373 Args.ClaimAllArgs(options::OPT_w); 374 375 if (!D.SysRoot.empty()) 376 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot)); 377 378 if (IsPIE) 379 CmdArgs.push_back("-pie"); 380 381 if (IsStaticPIE) { 382 CmdArgs.push_back("-static"); 383 CmdArgs.push_back("-pie"); 384 CmdArgs.push_back("--no-dynamic-linker"); 385 CmdArgs.push_back("-z"); 386 CmdArgs.push_back("text"); 387 } 388 389 if (ToolChain.isNoExecStackDefault()) { 390 CmdArgs.push_back("-z"); 391 CmdArgs.push_back("noexecstack"); 392 } 393 394 if (Args.hasArg(options::OPT_rdynamic)) 395 CmdArgs.push_back("-export-dynamic"); 396 397 if (Args.hasArg(options::OPT_s)) 398 CmdArgs.push_back("-s"); 399 400 if (Triple.isARM() || Triple.isThumb() || Triple.isAArch64()) { 401 bool IsBigEndian = isArmBigEndian(Triple, Args); 402 if (IsBigEndian) 403 arm::appendBE8LinkFlag(Args, CmdArgs, Triple); 404 IsBigEndian = IsBigEndian || Arch == llvm::Triple::aarch64_be; 405 CmdArgs.push_back(IsBigEndian ? "-EB" : "-EL"); 406 } 407 408 // Most Android ARM64 targets should enable the linker fix for erratum 409 // 843419. Only non-Cortex-A53 devices are allowed to skip this flag. 410 if (Arch == llvm::Triple::aarch64 && isAndroid) { 411 std::string CPU = getCPUName(Args, Triple); 412 if (CPU.empty() || CPU == "generic" || CPU == "cortex-a53") 413 CmdArgs.push_back("--fix-cortex-a53-843419"); 414 } 415 416 // Android does not allow shared text relocations. Emit a warning if the 417 // user's code contains any. 418 if (isAndroid) 419 CmdArgs.push_back("--warn-shared-textrel"); 420 421 for (const auto &Opt : ToolChain.ExtraOpts) 422 CmdArgs.push_back(Opt.c_str()); 423 424 CmdArgs.push_back("--eh-frame-hdr"); 425 426 if (const char *LDMOption = getLDMOption(ToolChain.getTriple(), Args)) { 427 CmdArgs.push_back("-m"); 428 CmdArgs.push_back(LDMOption); 429 } else { 430 D.Diag(diag::err_target_unknown_triple) << Triple.str(); 431 return; 432 } 433 434 if (IsStatic) { 435 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::armeb || 436 Arch == llvm::Triple::thumb || Arch == llvm::Triple::thumbeb) 437 CmdArgs.push_back("-Bstatic"); 438 else 439 CmdArgs.push_back("-static"); 440 } else if (Args.hasArg(options::OPT_shared)) { 441 CmdArgs.push_back("-shared"); 442 } 443 444 if (!IsStatic) { 445 if (Args.hasArg(options::OPT_rdynamic)) 446 CmdArgs.push_back("-export-dynamic"); 447 448 if (!Args.hasArg(options::OPT_shared) && !IsStaticPIE) { 449 const std::string Loader = 450 D.DyldPrefix + ToolChain.getDynamicLinker(Args); 451 CmdArgs.push_back("-dynamic-linker"); 452 CmdArgs.push_back(Args.MakeArgString(Loader)); 453 } 454 } 455 456 CmdArgs.push_back("-o"); 457 CmdArgs.push_back(Output.getFilename()); 458 459 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) { 460 if (!isAndroid && !IsIAMCU) { 461 const char *crt1 = nullptr; 462 if (!Args.hasArg(options::OPT_shared)) { 463 if (Args.hasArg(options::OPT_pg)) 464 crt1 = "gcrt1.o"; 465 else if (IsPIE) 466 crt1 = "Scrt1.o"; 467 else if (IsStaticPIE) 468 crt1 = "rcrt1.o"; 469 else 470 crt1 = "crt1.o"; 471 } 472 if (crt1) 473 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1))); 474 475 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o"))); 476 } 477 478 if (IsIAMCU) 479 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o"))); 480 else if (HasCRTBeginEndFiles) { 481 std::string P; 482 if (ToolChain.GetRuntimeLibType(Args) == ToolChain::RLT_CompilerRT && 483 !isAndroid) { 484 std::string crtbegin = ToolChain.getCompilerRT(Args, "crtbegin", 485 ToolChain::FT_Object); 486 if (ToolChain.getVFS().exists(crtbegin)) 487 P = crtbegin; 488 } 489 if (P.empty()) { 490 const char *crtbegin; 491 if (IsStatic) 492 crtbegin = isAndroid ? "crtbegin_static.o" : "crtbeginT.o"; 493 else if (Args.hasArg(options::OPT_shared)) 494 crtbegin = isAndroid ? "crtbegin_so.o" : "crtbeginS.o"; 495 else if (IsPIE || IsStaticPIE) 496 crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbeginS.o"; 497 else 498 crtbegin = isAndroid ? "crtbegin_dynamic.o" : "crtbegin.o"; 499 P = ToolChain.GetFilePath(crtbegin); 500 } 501 CmdArgs.push_back(Args.MakeArgString(P)); 502 } 503 504 // Add crtfastmath.o if available and fast math is enabled. 505 ToolChain.AddFastMathRuntimeIfAvailable(Args, CmdArgs); 506 } 507 508 Args.AddAllArgs(CmdArgs, options::OPT_L); 509 Args.AddAllArgs(CmdArgs, options::OPT_u); 510 511 ToolChain.AddFilePathLibArgs(Args, CmdArgs); 512 513 if (D.isUsingLTO()) { 514 assert(!Inputs.empty() && "Must have at least one input."); 515 AddGoldPlugin(ToolChain, Args, CmdArgs, Output, Inputs[0], 516 D.getLTOMode() == LTOK_Thin); 517 } 518 519 if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle)) 520 CmdArgs.push_back("--no-demangle"); 521 522 bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs); 523 bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs); 524 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); 525 // The profile runtime also needs access to system libraries. 526 getToolChain().addProfileRTLibs(Args, CmdArgs); 527 528 if (D.CCCIsCXX() && 529 !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { 530 if (ToolChain.ShouldLinkCXXStdlib(Args)) { 531 bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) && 532 !Args.hasArg(options::OPT_static); 533 if (OnlyLibstdcxxStatic) 534 CmdArgs.push_back("-Bstatic"); 535 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs); 536 if (OnlyLibstdcxxStatic) 537 CmdArgs.push_back("-Bdynamic"); 538 } 539 CmdArgs.push_back("-lm"); 540 } 541 // Silence warnings when linking C code with a C++ '-stdlib' argument. 542 Args.ClaimAllArgs(options::OPT_stdlib_EQ); 543 544 if (!Args.hasArg(options::OPT_nostdlib)) { 545 if (!Args.hasArg(options::OPT_nodefaultlibs)) { 546 if (IsStatic || IsStaticPIE) 547 CmdArgs.push_back("--start-group"); 548 549 if (NeedsSanitizerDeps) 550 linkSanitizerRuntimeDeps(ToolChain, CmdArgs); 551 552 if (NeedsXRayDeps) 553 linkXRayRuntimeDeps(ToolChain, CmdArgs); 554 555 bool WantPthread = Args.hasArg(options::OPT_pthread) || 556 Args.hasArg(options::OPT_pthreads); 557 558 // Use the static OpenMP runtime with -static-openmp 559 bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) && 560 !Args.hasArg(options::OPT_static); 561 562 // FIXME: Only pass GompNeedsRT = true for platforms with libgomp that 563 // require librt. Most modern Linux platforms do, but some may not. 564 if (addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP, 565 JA.isHostOffloading(Action::OFK_OpenMP), 566 /* GompNeedsRT= */ true)) 567 // OpenMP runtimes implies pthreads when using the GNU toolchain. 568 // FIXME: Does this really make sense for all GNU toolchains? 569 WantPthread = true; 570 571 AddRunTimeLibs(ToolChain, D, CmdArgs, Args); 572 573 if (WantPthread && !isAndroid) 574 CmdArgs.push_back("-lpthread"); 575 576 if (Args.hasArg(options::OPT_fsplit_stack)) 577 CmdArgs.push_back("--wrap=pthread_create"); 578 579 if (!Args.hasArg(options::OPT_nolibc)) 580 CmdArgs.push_back("-lc"); 581 582 // Add IAMCU specific libs, if needed. 583 if (IsIAMCU) 584 CmdArgs.push_back("-lgloss"); 585 586 if (IsStatic || IsStaticPIE) 587 CmdArgs.push_back("--end-group"); 588 else 589 AddRunTimeLibs(ToolChain, D, CmdArgs, Args); 590 591 // Add IAMCU specific libs (outside the group), if needed. 592 if (IsIAMCU) { 593 CmdArgs.push_back("--as-needed"); 594 CmdArgs.push_back("-lsoftfp"); 595 CmdArgs.push_back("--no-as-needed"); 596 } 597 } 598 599 if (!Args.hasArg(options::OPT_nostartfiles) && !IsIAMCU) { 600 if (HasCRTBeginEndFiles) { 601 std::string P; 602 if (ToolChain.GetRuntimeLibType(Args) == ToolChain::RLT_CompilerRT && 603 !isAndroid) { 604 std::string crtend = ToolChain.getCompilerRT(Args, "crtend", 605 ToolChain::FT_Object); 606 if (ToolChain.getVFS().exists(crtend)) 607 P = crtend; 608 } 609 if (P.empty()) { 610 const char *crtend; 611 if (Args.hasArg(options::OPT_shared)) 612 crtend = isAndroid ? "crtend_so.o" : "crtendS.o"; 613 else if (IsPIE || IsStaticPIE) 614 crtend = isAndroid ? "crtend_android.o" : "crtendS.o"; 615 else 616 crtend = isAndroid ? "crtend_android.o" : "crtend.o"; 617 P = ToolChain.GetFilePath(crtend); 618 } 619 CmdArgs.push_back(Args.MakeArgString(P)); 620 } 621 if (!isAndroid) 622 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o"))); 623 } 624 } 625 626 // Add HIP offloading linker script args if required. 627 AddHIPLinkerScript(getToolChain(), C, Output, Inputs, Args, CmdArgs, JA, 628 *this); 629 630 const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath()); 631 C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs)); 632 } 633 634 void tools::gnutools::Assembler::ConstructJob(Compilation &C, 635 const JobAction &JA, 636 const InputInfo &Output, 637 const InputInfoList &Inputs, 638 const ArgList &Args, 639 const char *LinkingOutput) const { 640 const auto &D = getToolChain().getDriver(); 641 642 claimNoWarnArgs(Args); 643 644 ArgStringList CmdArgs; 645 646 llvm::Reloc::Model RelocationModel; 647 unsigned PICLevel; 648 bool IsPIE; 649 std::tie(RelocationModel, PICLevel, IsPIE) = 650 ParsePICArgs(getToolChain(), Args); 651 652 if (const Arg *A = Args.getLastArg(options::OPT_gz, options::OPT_gz_EQ)) { 653 if (A->getOption().getID() == options::OPT_gz) { 654 CmdArgs.push_back("--compress-debug-sections"); 655 } else { 656 StringRef Value = A->getValue(); 657 if (Value == "none" || Value == "zlib" || Value == "zlib-gnu") { 658 CmdArgs.push_back( 659 Args.MakeArgString("--compress-debug-sections=" + Twine(Value))); 660 } else { 661 D.Diag(diag::err_drv_unsupported_option_argument) 662 << A->getOption().getName() << Value; 663 } 664 } 665 } 666 667 if (getToolChain().isNoExecStackDefault()) { 668 CmdArgs.push_back("--noexecstack"); 669 } 670 671 switch (getToolChain().getArch()) { 672 default: 673 break; 674 // Add --32/--64 to make sure we get the format we want. 675 // This is incomplete 676 case llvm::Triple::x86: 677 CmdArgs.push_back("--32"); 678 break; 679 case llvm::Triple::x86_64: 680 if (getToolChain().getTriple().getEnvironment() == llvm::Triple::GNUX32) 681 CmdArgs.push_back("--x32"); 682 else 683 CmdArgs.push_back("--64"); 684 break; 685 case llvm::Triple::ppc: { 686 CmdArgs.push_back("-a32"); 687 CmdArgs.push_back("-mppc"); 688 CmdArgs.push_back( 689 ppc::getPPCAsmModeForCPU(getCPUName(Args, getToolChain().getTriple()))); 690 break; 691 } 692 case llvm::Triple::ppc64: { 693 CmdArgs.push_back("-a64"); 694 CmdArgs.push_back("-mppc64"); 695 CmdArgs.push_back( 696 ppc::getPPCAsmModeForCPU(getCPUName(Args, getToolChain().getTriple()))); 697 break; 698 } 699 case llvm::Triple::ppc64le: { 700 CmdArgs.push_back("-a64"); 701 CmdArgs.push_back("-mppc64"); 702 CmdArgs.push_back("-mlittle-endian"); 703 CmdArgs.push_back( 704 ppc::getPPCAsmModeForCPU(getCPUName(Args, getToolChain().getTriple()))); 705 break; 706 } 707 case llvm::Triple::riscv32: 708 case llvm::Triple::riscv64: { 709 StringRef ABIName = riscv::getRISCVABI(Args, getToolChain().getTriple()); 710 CmdArgs.push_back("-mabi"); 711 CmdArgs.push_back(ABIName.data()); 712 StringRef MArchName = riscv::getRISCVArch(Args, getToolChain().getTriple()); 713 CmdArgs.push_back("-march"); 714 CmdArgs.push_back(MArchName.data()); 715 break; 716 } 717 case llvm::Triple::sparc: 718 case llvm::Triple::sparcel: { 719 CmdArgs.push_back("-32"); 720 std::string CPU = getCPUName(Args, getToolChain().getTriple()); 721 CmdArgs.push_back( 722 sparc::getSparcAsmModeForCPU(CPU, getToolChain().getTriple())); 723 AddAssemblerKPIC(getToolChain(), Args, CmdArgs); 724 break; 725 } 726 case llvm::Triple::sparcv9: { 727 CmdArgs.push_back("-64"); 728 std::string CPU = getCPUName(Args, getToolChain().getTriple()); 729 CmdArgs.push_back( 730 sparc::getSparcAsmModeForCPU(CPU, getToolChain().getTriple())); 731 AddAssemblerKPIC(getToolChain(), Args, CmdArgs); 732 break; 733 } 734 case llvm::Triple::arm: 735 case llvm::Triple::armeb: 736 case llvm::Triple::thumb: 737 case llvm::Triple::thumbeb: { 738 const llvm::Triple &Triple2 = getToolChain().getTriple(); 739 CmdArgs.push_back(isArmBigEndian(Triple2, Args) ? "-EB" : "-EL"); 740 switch (Triple2.getSubArch()) { 741 case llvm::Triple::ARMSubArch_v7: 742 CmdArgs.push_back("-mfpu=neon"); 743 break; 744 case llvm::Triple::ARMSubArch_v8: 745 CmdArgs.push_back("-mfpu=crypto-neon-fp-armv8"); 746 break; 747 default: 748 break; 749 } 750 751 switch (arm::getARMFloatABI(getToolChain(), Args)) { 752 case arm::FloatABI::Invalid: llvm_unreachable("must have an ABI!"); 753 case arm::FloatABI::Soft: 754 CmdArgs.push_back(Args.MakeArgString("-mfloat-abi=soft")); 755 break; 756 case arm::FloatABI::SoftFP: 757 CmdArgs.push_back(Args.MakeArgString("-mfloat-abi=softfp")); 758 break; 759 case arm::FloatABI::Hard: 760 CmdArgs.push_back(Args.MakeArgString("-mfloat-abi=hard")); 761 break; 762 } 763 764 Args.AddLastArg(CmdArgs, options::OPT_march_EQ); 765 normalizeCPUNamesForAssembler(Args, CmdArgs); 766 767 Args.AddLastArg(CmdArgs, options::OPT_mfpu_EQ); 768 break; 769 } 770 case llvm::Triple::aarch64: 771 case llvm::Triple::aarch64_be: { 772 CmdArgs.push_back( 773 getToolChain().getArch() == llvm::Triple::aarch64_be ? "-EB" : "-EL"); 774 Args.AddLastArg(CmdArgs, options::OPT_march_EQ); 775 normalizeCPUNamesForAssembler(Args, CmdArgs); 776 777 break; 778 } 779 case llvm::Triple::mips: 780 case llvm::Triple::mipsel: 781 case llvm::Triple::mips64: 782 case llvm::Triple::mips64el: { 783 StringRef CPUName; 784 StringRef ABIName; 785 mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName); 786 ABIName = mips::getGnuCompatibleMipsABIName(ABIName); 787 788 CmdArgs.push_back("-march"); 789 CmdArgs.push_back(CPUName.data()); 790 791 CmdArgs.push_back("-mabi"); 792 CmdArgs.push_back(ABIName.data()); 793 794 // -mno-shared should be emitted unless -fpic, -fpie, -fPIC, -fPIE, 795 // or -mshared (not implemented) is in effect. 796 if (RelocationModel == llvm::Reloc::Static) 797 CmdArgs.push_back("-mno-shared"); 798 799 // LLVM doesn't support -mplt yet and acts as if it is always given. 800 // However, -mplt has no effect with the N64 ABI. 801 if (ABIName != "64" && !Args.hasArg(options::OPT_mno_abicalls)) 802 CmdArgs.push_back("-call_nonpic"); 803 804 if (getToolChain().getTriple().isLittleEndian()) 805 CmdArgs.push_back("-EL"); 806 else 807 CmdArgs.push_back("-EB"); 808 809 if (Arg *A = Args.getLastArg(options::OPT_mnan_EQ)) { 810 if (StringRef(A->getValue()) == "2008") 811 CmdArgs.push_back(Args.MakeArgString("-mnan=2008")); 812 } 813 814 // Add the last -mfp32/-mfpxx/-mfp64 or -mfpxx if it is enabled by default. 815 if (Arg *A = Args.getLastArg(options::OPT_mfp32, options::OPT_mfpxx, 816 options::OPT_mfp64)) { 817 A->claim(); 818 A->render(Args, CmdArgs); 819 } else if (mips::shouldUseFPXX( 820 Args, getToolChain().getTriple(), CPUName, ABIName, 821 mips::getMipsFloatABI(getToolChain().getDriver(), Args, 822 getToolChain().getTriple()))) 823 CmdArgs.push_back("-mfpxx"); 824 825 // Pass on -mmips16 or -mno-mips16. However, the assembler equivalent of 826 // -mno-mips16 is actually -no-mips16. 827 if (Arg *A = 828 Args.getLastArg(options::OPT_mips16, options::OPT_mno_mips16)) { 829 if (A->getOption().matches(options::OPT_mips16)) { 830 A->claim(); 831 A->render(Args, CmdArgs); 832 } else { 833 A->claim(); 834 CmdArgs.push_back("-no-mips16"); 835 } 836 } 837 838 Args.AddLastArg(CmdArgs, options::OPT_mmicromips, 839 options::OPT_mno_micromips); 840 Args.AddLastArg(CmdArgs, options::OPT_mdsp, options::OPT_mno_dsp); 841 Args.AddLastArg(CmdArgs, options::OPT_mdspr2, options::OPT_mno_dspr2); 842 843 if (Arg *A = Args.getLastArg(options::OPT_mmsa, options::OPT_mno_msa)) { 844 // Do not use AddLastArg because not all versions of MIPS assembler 845 // support -mmsa / -mno-msa options. 846 if (A->getOption().matches(options::OPT_mmsa)) 847 CmdArgs.push_back(Args.MakeArgString("-mmsa")); 848 } 849 850 Args.AddLastArg(CmdArgs, options::OPT_mhard_float, 851 options::OPT_msoft_float); 852 853 Args.AddLastArg(CmdArgs, options::OPT_mdouble_float, 854 options::OPT_msingle_float); 855 856 Args.AddLastArg(CmdArgs, options::OPT_modd_spreg, 857 options::OPT_mno_odd_spreg); 858 859 AddAssemblerKPIC(getToolChain(), Args, CmdArgs); 860 break; 861 } 862 case llvm::Triple::systemz: { 863 // Always pass an -march option, since our default of z10 is later 864 // than the GNU assembler's default. 865 std::string CPUName = systemz::getSystemZTargetCPU(Args); 866 CmdArgs.push_back(Args.MakeArgString("-march=" + CPUName)); 867 break; 868 } 869 } 870 871 for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ, 872 options::OPT_fdebug_prefix_map_EQ)) { 873 StringRef Map = A->getValue(); 874 if (Map.find('=') == StringRef::npos) 875 D.Diag(diag::err_drv_invalid_argument_to_option) 876 << Map << A->getOption().getName(); 877 else { 878 CmdArgs.push_back(Args.MakeArgString("--debug-prefix-map")); 879 CmdArgs.push_back(Args.MakeArgString(Map)); 880 } 881 A->claim(); 882 } 883 884 Args.AddAllArgs(CmdArgs, options::OPT_I); 885 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler); 886 887 CmdArgs.push_back("-o"); 888 CmdArgs.push_back(Output.getFilename()); 889 890 for (const auto &II : Inputs) 891 CmdArgs.push_back(II.getFilename()); 892 893 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as")); 894 C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs)); 895 896 // Handle the debug info splitting at object creation time if we're 897 // creating an object. 898 // TODO: Currently only works on linux with newer objcopy. 899 if (Args.hasArg(options::OPT_gsplit_dwarf) && 900 getToolChain().getTriple().isOSLinux()) 901 SplitDebugInfo(getToolChain(), C, *this, JA, Args, Output, 902 SplitDebugName(Args, Inputs[0], Output)); 903 } 904 905 namespace { 906 // Filter to remove Multilibs that don't exist as a suffix to Path 907 class FilterNonExistent { 908 StringRef Base, File; 909 llvm::vfs::FileSystem &VFS; 910 911 public: 912 FilterNonExistent(StringRef Base, StringRef File, llvm::vfs::FileSystem &VFS) 913 : Base(Base), File(File), VFS(VFS) {} 914 bool operator()(const Multilib &M) { 915 return !VFS.exists(Base + M.gccSuffix() + File); 916 } 917 }; 918 } // end anonymous namespace 919 920 static bool isSoftFloatABI(const ArgList &Args) { 921 Arg *A = Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float, 922 options::OPT_mfloat_abi_EQ); 923 if (!A) 924 return false; 925 926 return A->getOption().matches(options::OPT_msoft_float) || 927 (A->getOption().matches(options::OPT_mfloat_abi_EQ) && 928 A->getValue() == StringRef("soft")); 929 } 930 931 static bool isArmOrThumbArch(llvm::Triple::ArchType Arch) { 932 return Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb; 933 } 934 935 static bool isMipsEL(llvm::Triple::ArchType Arch) { 936 return Arch == llvm::Triple::mipsel || Arch == llvm::Triple::mips64el; 937 } 938 939 static bool isMips16(const ArgList &Args) { 940 Arg *A = Args.getLastArg(options::OPT_mips16, options::OPT_mno_mips16); 941 return A && A->getOption().matches(options::OPT_mips16); 942 } 943 944 static bool isMicroMips(const ArgList &Args) { 945 Arg *A = Args.getLastArg(options::OPT_mmicromips, options::OPT_mno_micromips); 946 return A && A->getOption().matches(options::OPT_mmicromips); 947 } 948 949 static bool isMSP430(llvm::Triple::ArchType Arch) { 950 return Arch == llvm::Triple::msp430; 951 } 952 953 static Multilib makeMultilib(StringRef commonSuffix) { 954 return Multilib(commonSuffix, commonSuffix, commonSuffix); 955 } 956 957 static bool findMipsCsMultilibs(const Multilib::flags_list &Flags, 958 FilterNonExistent &NonExistent, 959 DetectedMultilibs &Result) { 960 // Check for Code Sourcery toolchain multilibs 961 MultilibSet CSMipsMultilibs; 962 { 963 auto MArchMips16 = makeMultilib("/mips16").flag("+m32").flag("+mips16"); 964 965 auto MArchMicroMips = 966 makeMultilib("/micromips").flag("+m32").flag("+mmicromips"); 967 968 auto MArchDefault = makeMultilib("").flag("-mips16").flag("-mmicromips"); 969 970 auto UCLibc = makeMultilib("/uclibc").flag("+muclibc"); 971 972 auto SoftFloat = makeMultilib("/soft-float").flag("+msoft-float"); 973 974 auto Nan2008 = makeMultilib("/nan2008").flag("+mnan=2008"); 975 976 auto DefaultFloat = 977 makeMultilib("").flag("-msoft-float").flag("-mnan=2008"); 978 979 auto BigEndian = makeMultilib("").flag("+EB").flag("-EL"); 980 981 auto LittleEndian = makeMultilib("/el").flag("+EL").flag("-EB"); 982 983 // Note that this one's osSuffix is "" 984 auto MAbi64 = makeMultilib("") 985 .gccSuffix("/64") 986 .includeSuffix("/64") 987 .flag("+mabi=n64") 988 .flag("-mabi=n32") 989 .flag("-m32"); 990 991 CSMipsMultilibs = 992 MultilibSet() 993 .Either(MArchMips16, MArchMicroMips, MArchDefault) 994 .Maybe(UCLibc) 995 .Either(SoftFloat, Nan2008, DefaultFloat) 996 .FilterOut("/micromips/nan2008") 997 .FilterOut("/mips16/nan2008") 998 .Either(BigEndian, LittleEndian) 999 .Maybe(MAbi64) 1000 .FilterOut("/mips16.*/64") 1001 .FilterOut("/micromips.*/64") 1002 .FilterOut(NonExistent) 1003 .setIncludeDirsCallback([](const Multilib &M) { 1004 std::vector<std::string> Dirs({"/include"}); 1005 if (StringRef(M.includeSuffix()).startswith("/uclibc")) 1006 Dirs.push_back( 1007 "/../../../../mips-linux-gnu/libc/uclibc/usr/include"); 1008 else 1009 Dirs.push_back("/../../../../mips-linux-gnu/libc/usr/include"); 1010 return Dirs; 1011 }); 1012 } 1013 1014 MultilibSet DebianMipsMultilibs; 1015 { 1016 Multilib MAbiN32 = 1017 Multilib().gccSuffix("/n32").includeSuffix("/n32").flag("+mabi=n32"); 1018 1019 Multilib M64 = Multilib() 1020 .gccSuffix("/64") 1021 .includeSuffix("/64") 1022 .flag("+m64") 1023 .flag("-m32") 1024 .flag("-mabi=n32"); 1025 1026 Multilib M32 = Multilib().flag("-m64").flag("+m32").flag("-mabi=n32"); 1027 1028 DebianMipsMultilibs = 1029 MultilibSet().Either(M32, M64, MAbiN32).FilterOut(NonExistent); 1030 } 1031 1032 // Sort candidates. Toolchain that best meets the directories tree goes first. 1033 // Then select the first toolchains matches command line flags. 1034 MultilibSet *Candidates[] = {&CSMipsMultilibs, &DebianMipsMultilibs}; 1035 if (CSMipsMultilibs.size() < DebianMipsMultilibs.size()) 1036 std::iter_swap(Candidates, Candidates + 1); 1037 for (const MultilibSet *Candidate : Candidates) { 1038 if (Candidate->select(Flags, Result.SelectedMultilib)) { 1039 if (Candidate == &DebianMipsMultilibs) 1040 Result.BiarchSibling = Multilib(); 1041 Result.Multilibs = *Candidate; 1042 return true; 1043 } 1044 } 1045 return false; 1046 } 1047 1048 static bool findMipsAndroidMultilibs(llvm::vfs::FileSystem &VFS, StringRef Path, 1049 const Multilib::flags_list &Flags, 1050 FilterNonExistent &NonExistent, 1051 DetectedMultilibs &Result) { 1052 1053 MultilibSet AndroidMipsMultilibs = 1054 MultilibSet() 1055 .Maybe(Multilib("/mips-r2").flag("+march=mips32r2")) 1056 .Maybe(Multilib("/mips-r6").flag("+march=mips32r6")) 1057 .FilterOut(NonExistent); 1058 1059 MultilibSet AndroidMipselMultilibs = 1060 MultilibSet() 1061 .Either(Multilib().flag("+march=mips32"), 1062 Multilib("/mips-r2", "", "/mips-r2").flag("+march=mips32r2"), 1063 Multilib("/mips-r6", "", "/mips-r6").flag("+march=mips32r6")) 1064 .FilterOut(NonExistent); 1065 1066 MultilibSet AndroidMips64elMultilibs = 1067 MultilibSet() 1068 .Either( 1069 Multilib().flag("+march=mips64r6"), 1070 Multilib("/32/mips-r1", "", "/mips-r1").flag("+march=mips32"), 1071 Multilib("/32/mips-r2", "", "/mips-r2").flag("+march=mips32r2"), 1072 Multilib("/32/mips-r6", "", "/mips-r6").flag("+march=mips32r6")) 1073 .FilterOut(NonExistent); 1074 1075 MultilibSet *MS = &AndroidMipsMultilibs; 1076 if (VFS.exists(Path + "/mips-r6")) 1077 MS = &AndroidMipselMultilibs; 1078 else if (VFS.exists(Path + "/32")) 1079 MS = &AndroidMips64elMultilibs; 1080 if (MS->select(Flags, Result.SelectedMultilib)) { 1081 Result.Multilibs = *MS; 1082 return true; 1083 } 1084 return false; 1085 } 1086 1087 static bool findMipsMuslMultilibs(const Multilib::flags_list &Flags, 1088 FilterNonExistent &NonExistent, 1089 DetectedMultilibs &Result) { 1090 // Musl toolchain multilibs 1091 MultilibSet MuslMipsMultilibs; 1092 { 1093 auto MArchMipsR2 = makeMultilib("") 1094 .osSuffix("/mips-r2-hard-musl") 1095 .flag("+EB") 1096 .flag("-EL") 1097 .flag("+march=mips32r2"); 1098 1099 auto MArchMipselR2 = makeMultilib("/mipsel-r2-hard-musl") 1100 .flag("-EB") 1101 .flag("+EL") 1102 .flag("+march=mips32r2"); 1103 1104 MuslMipsMultilibs = MultilibSet().Either(MArchMipsR2, MArchMipselR2); 1105 1106 // Specify the callback that computes the include directories. 1107 MuslMipsMultilibs.setIncludeDirsCallback([](const Multilib &M) { 1108 return std::vector<std::string>( 1109 {"/../sysroot" + M.osSuffix() + "/usr/include"}); 1110 }); 1111 } 1112 if (MuslMipsMultilibs.select(Flags, Result.SelectedMultilib)) { 1113 Result.Multilibs = MuslMipsMultilibs; 1114 return true; 1115 } 1116 return false; 1117 } 1118 1119 static bool findMipsMtiMultilibs(const Multilib::flags_list &Flags, 1120 FilterNonExistent &NonExistent, 1121 DetectedMultilibs &Result) { 1122 // CodeScape MTI toolchain v1.2 and early. 1123 MultilibSet MtiMipsMultilibsV1; 1124 { 1125 auto MArchMips32 = makeMultilib("/mips32") 1126 .flag("+m32") 1127 .flag("-m64") 1128 .flag("-mmicromips") 1129 .flag("+march=mips32"); 1130 1131 auto MArchMicroMips = makeMultilib("/micromips") 1132 .flag("+m32") 1133 .flag("-m64") 1134 .flag("+mmicromips"); 1135 1136 auto MArchMips64r2 = makeMultilib("/mips64r2") 1137 .flag("-m32") 1138 .flag("+m64") 1139 .flag("+march=mips64r2"); 1140 1141 auto MArchMips64 = makeMultilib("/mips64").flag("-m32").flag("+m64").flag( 1142 "-march=mips64r2"); 1143 1144 auto MArchDefault = makeMultilib("") 1145 .flag("+m32") 1146 .flag("-m64") 1147 .flag("-mmicromips") 1148 .flag("+march=mips32r2"); 1149 1150 auto Mips16 = makeMultilib("/mips16").flag("+mips16"); 1151 1152 auto UCLibc = makeMultilib("/uclibc").flag("+muclibc"); 1153 1154 auto MAbi64 = 1155 makeMultilib("/64").flag("+mabi=n64").flag("-mabi=n32").flag("-m32"); 1156 1157 auto BigEndian = makeMultilib("").flag("+EB").flag("-EL"); 1158 1159 auto LittleEndian = makeMultilib("/el").flag("+EL").flag("-EB"); 1160 1161 auto SoftFloat = makeMultilib("/sof").flag("+msoft-float"); 1162 1163 auto Nan2008 = makeMultilib("/nan2008").flag("+mnan=2008"); 1164 1165 MtiMipsMultilibsV1 = 1166 MultilibSet() 1167 .Either(MArchMips32, MArchMicroMips, MArchMips64r2, MArchMips64, 1168 MArchDefault) 1169 .Maybe(UCLibc) 1170 .Maybe(Mips16) 1171 .FilterOut("/mips64/mips16") 1172 .FilterOut("/mips64r2/mips16") 1173 .FilterOut("/micromips/mips16") 1174 .Maybe(MAbi64) 1175 .FilterOut("/micromips/64") 1176 .FilterOut("/mips32/64") 1177 .FilterOut("^/64") 1178 .FilterOut("/mips16/64") 1179 .Either(BigEndian, LittleEndian) 1180 .Maybe(SoftFloat) 1181 .Maybe(Nan2008) 1182 .FilterOut(".*sof/nan2008") 1183 .FilterOut(NonExistent) 1184 .setIncludeDirsCallback([](const Multilib &M) { 1185 std::vector<std::string> Dirs({"/include"}); 1186 if (StringRef(M.includeSuffix()).startswith("/uclibc")) 1187 Dirs.push_back("/../../../../sysroot/uclibc/usr/include"); 1188 else 1189 Dirs.push_back("/../../../../sysroot/usr/include"); 1190 return Dirs; 1191 }); 1192 } 1193 1194 // CodeScape IMG toolchain starting from v1.3. 1195 MultilibSet MtiMipsMultilibsV2; 1196 { 1197 auto BeHard = makeMultilib("/mips-r2-hard") 1198 .flag("+EB") 1199 .flag("-msoft-float") 1200 .flag("-mnan=2008") 1201 .flag("-muclibc"); 1202 auto BeSoft = makeMultilib("/mips-r2-soft") 1203 .flag("+EB") 1204 .flag("+msoft-float") 1205 .flag("-mnan=2008"); 1206 auto ElHard = makeMultilib("/mipsel-r2-hard") 1207 .flag("+EL") 1208 .flag("-msoft-float") 1209 .flag("-mnan=2008") 1210 .flag("-muclibc"); 1211 auto ElSoft = makeMultilib("/mipsel-r2-soft") 1212 .flag("+EL") 1213 .flag("+msoft-float") 1214 .flag("-mnan=2008") 1215 .flag("-mmicromips"); 1216 auto BeHardNan = makeMultilib("/mips-r2-hard-nan2008") 1217 .flag("+EB") 1218 .flag("-msoft-float") 1219 .flag("+mnan=2008") 1220 .flag("-muclibc"); 1221 auto ElHardNan = makeMultilib("/mipsel-r2-hard-nan2008") 1222 .flag("+EL") 1223 .flag("-msoft-float") 1224 .flag("+mnan=2008") 1225 .flag("-muclibc") 1226 .flag("-mmicromips"); 1227 auto BeHardNanUclibc = makeMultilib("/mips-r2-hard-nan2008-uclibc") 1228 .flag("+EB") 1229 .flag("-msoft-float") 1230 .flag("+mnan=2008") 1231 .flag("+muclibc"); 1232 auto ElHardNanUclibc = makeMultilib("/mipsel-r2-hard-nan2008-uclibc") 1233 .flag("+EL") 1234 .flag("-msoft-float") 1235 .flag("+mnan=2008") 1236 .flag("+muclibc"); 1237 auto BeHardUclibc = makeMultilib("/mips-r2-hard-uclibc") 1238 .flag("+EB") 1239 .flag("-msoft-float") 1240 .flag("-mnan=2008") 1241 .flag("+muclibc"); 1242 auto ElHardUclibc = makeMultilib("/mipsel-r2-hard-uclibc") 1243 .flag("+EL") 1244 .flag("-msoft-float") 1245 .flag("-mnan=2008") 1246 .flag("+muclibc"); 1247 auto ElMicroHardNan = makeMultilib("/micromipsel-r2-hard-nan2008") 1248 .flag("+EL") 1249 .flag("-msoft-float") 1250 .flag("+mnan=2008") 1251 .flag("+mmicromips"); 1252 auto ElMicroSoft = makeMultilib("/micromipsel-r2-soft") 1253 .flag("+EL") 1254 .flag("+msoft-float") 1255 .flag("-mnan=2008") 1256 .flag("+mmicromips"); 1257 1258 auto O32 = 1259 makeMultilib("/lib").osSuffix("").flag("-mabi=n32").flag("-mabi=n64"); 1260 auto N32 = 1261 makeMultilib("/lib32").osSuffix("").flag("+mabi=n32").flag("-mabi=n64"); 1262 auto N64 = 1263 makeMultilib("/lib64").osSuffix("").flag("-mabi=n32").flag("+mabi=n64"); 1264 1265 MtiMipsMultilibsV2 = 1266 MultilibSet() 1267 .Either({BeHard, BeSoft, ElHard, ElSoft, BeHardNan, ElHardNan, 1268 BeHardNanUclibc, ElHardNanUclibc, BeHardUclibc, 1269 ElHardUclibc, ElMicroHardNan, ElMicroSoft}) 1270 .Either(O32, N32, N64) 1271 .FilterOut(NonExistent) 1272 .setIncludeDirsCallback([](const Multilib &M) { 1273 return std::vector<std::string>({"/../../../../sysroot" + 1274 M.includeSuffix() + 1275 "/../usr/include"}); 1276 }) 1277 .setFilePathsCallback([](const Multilib &M) { 1278 return std::vector<std::string>( 1279 {"/../../../../mips-mti-linux-gnu/lib" + M.gccSuffix()}); 1280 }); 1281 } 1282 for (auto Candidate : {&MtiMipsMultilibsV1, &MtiMipsMultilibsV2}) { 1283 if (Candidate->select(Flags, Result.SelectedMultilib)) { 1284 Result.Multilibs = *Candidate; 1285 return true; 1286 } 1287 } 1288 return false; 1289 } 1290 1291 static bool findMipsImgMultilibs(const Multilib::flags_list &Flags, 1292 FilterNonExistent &NonExistent, 1293 DetectedMultilibs &Result) { 1294 // CodeScape IMG toolchain v1.2 and early. 1295 MultilibSet ImgMultilibsV1; 1296 { 1297 auto Mips64r6 = makeMultilib("/mips64r6").flag("+m64").flag("-m32"); 1298 1299 auto LittleEndian = makeMultilib("/el").flag("+EL").flag("-EB"); 1300 1301 auto MAbi64 = 1302 makeMultilib("/64").flag("+mabi=n64").flag("-mabi=n32").flag("-m32"); 1303 1304 ImgMultilibsV1 = 1305 MultilibSet() 1306 .Maybe(Mips64r6) 1307 .Maybe(MAbi64) 1308 .Maybe(LittleEndian) 1309 .FilterOut(NonExistent) 1310 .setIncludeDirsCallback([](const Multilib &M) { 1311 return std::vector<std::string>( 1312 {"/include", "/../../../../sysroot/usr/include"}); 1313 }); 1314 } 1315 1316 // CodeScape IMG toolchain starting from v1.3. 1317 MultilibSet ImgMultilibsV2; 1318 { 1319 auto BeHard = makeMultilib("/mips-r6-hard") 1320 .flag("+EB") 1321 .flag("-msoft-float") 1322 .flag("-mmicromips"); 1323 auto BeSoft = makeMultilib("/mips-r6-soft") 1324 .flag("+EB") 1325 .flag("+msoft-float") 1326 .flag("-mmicromips"); 1327 auto ElHard = makeMultilib("/mipsel-r6-hard") 1328 .flag("+EL") 1329 .flag("-msoft-float") 1330 .flag("-mmicromips"); 1331 auto ElSoft = makeMultilib("/mipsel-r6-soft") 1332 .flag("+EL") 1333 .flag("+msoft-float") 1334 .flag("-mmicromips"); 1335 auto BeMicroHard = makeMultilib("/micromips-r6-hard") 1336 .flag("+EB") 1337 .flag("-msoft-float") 1338 .flag("+mmicromips"); 1339 auto BeMicroSoft = makeMultilib("/micromips-r6-soft") 1340 .flag("+EB") 1341 .flag("+msoft-float") 1342 .flag("+mmicromips"); 1343 auto ElMicroHard = makeMultilib("/micromipsel-r6-hard") 1344 .flag("+EL") 1345 .flag("-msoft-float") 1346 .flag("+mmicromips"); 1347 auto ElMicroSoft = makeMultilib("/micromipsel-r6-soft") 1348 .flag("+EL") 1349 .flag("+msoft-float") 1350 .flag("+mmicromips"); 1351 1352 auto O32 = 1353 makeMultilib("/lib").osSuffix("").flag("-mabi=n32").flag("-mabi=n64"); 1354 auto N32 = 1355 makeMultilib("/lib32").osSuffix("").flag("+mabi=n32").flag("-mabi=n64"); 1356 auto N64 = 1357 makeMultilib("/lib64").osSuffix("").flag("-mabi=n32").flag("+mabi=n64"); 1358 1359 ImgMultilibsV2 = 1360 MultilibSet() 1361 .Either({BeHard, BeSoft, ElHard, ElSoft, BeMicroHard, BeMicroSoft, 1362 ElMicroHard, ElMicroSoft}) 1363 .Either(O32, N32, N64) 1364 .FilterOut(NonExistent) 1365 .setIncludeDirsCallback([](const Multilib &M) { 1366 return std::vector<std::string>({"/../../../../sysroot" + 1367 M.includeSuffix() + 1368 "/../usr/include"}); 1369 }) 1370 .setFilePathsCallback([](const Multilib &M) { 1371 return std::vector<std::string>( 1372 {"/../../../../mips-img-linux-gnu/lib" + M.gccSuffix()}); 1373 }); 1374 } 1375 for (auto Candidate : {&ImgMultilibsV1, &ImgMultilibsV2}) { 1376 if (Candidate->select(Flags, Result.SelectedMultilib)) { 1377 Result.Multilibs = *Candidate; 1378 return true; 1379 } 1380 } 1381 return false; 1382 } 1383 1384 bool clang::driver::findMIPSMultilibs(const Driver &D, 1385 const llvm::Triple &TargetTriple, 1386 StringRef Path, const ArgList &Args, 1387 DetectedMultilibs &Result) { 1388 FilterNonExistent NonExistent(Path, "/crtbegin.o", D.getVFS()); 1389 1390 StringRef CPUName; 1391 StringRef ABIName; 1392 tools::mips::getMipsCPUAndABI(Args, TargetTriple, CPUName, ABIName); 1393 1394 llvm::Triple::ArchType TargetArch = TargetTriple.getArch(); 1395 1396 Multilib::flags_list Flags; 1397 addMultilibFlag(TargetTriple.isMIPS32(), "m32", Flags); 1398 addMultilibFlag(TargetTriple.isMIPS64(), "m64", Flags); 1399 addMultilibFlag(isMips16(Args), "mips16", Flags); 1400 addMultilibFlag(CPUName == "mips32", "march=mips32", Flags); 1401 addMultilibFlag(CPUName == "mips32r2" || CPUName == "mips32r3" || 1402 CPUName == "mips32r5" || CPUName == "p5600", 1403 "march=mips32r2", Flags); 1404 addMultilibFlag(CPUName == "mips32r6", "march=mips32r6", Flags); 1405 addMultilibFlag(CPUName == "mips64", "march=mips64", Flags); 1406 addMultilibFlag(CPUName == "mips64r2" || CPUName == "mips64r3" || 1407 CPUName == "mips64r5" || CPUName == "octeon" || 1408 CPUName == "octeon+", 1409 "march=mips64r2", Flags); 1410 addMultilibFlag(CPUName == "mips64r6", "march=mips64r6", Flags); 1411 addMultilibFlag(isMicroMips(Args), "mmicromips", Flags); 1412 addMultilibFlag(tools::mips::isUCLibc(Args), "muclibc", Flags); 1413 addMultilibFlag(tools::mips::isNaN2008(Args, TargetTriple), "mnan=2008", 1414 Flags); 1415 addMultilibFlag(ABIName == "n32", "mabi=n32", Flags); 1416 addMultilibFlag(ABIName == "n64", "mabi=n64", Flags); 1417 addMultilibFlag(isSoftFloatABI(Args), "msoft-float", Flags); 1418 addMultilibFlag(!isSoftFloatABI(Args), "mhard-float", Flags); 1419 addMultilibFlag(isMipsEL(TargetArch), "EL", Flags); 1420 addMultilibFlag(!isMipsEL(TargetArch), "EB", Flags); 1421 1422 if (TargetTriple.isAndroid()) 1423 return findMipsAndroidMultilibs(D.getVFS(), Path, Flags, NonExistent, 1424 Result); 1425 1426 if (TargetTriple.getVendor() == llvm::Triple::MipsTechnologies && 1427 TargetTriple.getOS() == llvm::Triple::Linux && 1428 TargetTriple.getEnvironment() == llvm::Triple::UnknownEnvironment) 1429 return findMipsMuslMultilibs(Flags, NonExistent, Result); 1430 1431 if (TargetTriple.getVendor() == llvm::Triple::MipsTechnologies && 1432 TargetTriple.getOS() == llvm::Triple::Linux && 1433 TargetTriple.isGNUEnvironment()) 1434 return findMipsMtiMultilibs(Flags, NonExistent, Result); 1435 1436 if (TargetTriple.getVendor() == llvm::Triple::ImaginationTechnologies && 1437 TargetTriple.getOS() == llvm::Triple::Linux && 1438 TargetTriple.isGNUEnvironment()) 1439 return findMipsImgMultilibs(Flags, NonExistent, Result); 1440 1441 if (findMipsCsMultilibs(Flags, NonExistent, Result)) 1442 return true; 1443 1444 // Fallback to the regular toolchain-tree structure. 1445 Multilib Default; 1446 Result.Multilibs.push_back(Default); 1447 Result.Multilibs.FilterOut(NonExistent); 1448 1449 if (Result.Multilibs.select(Flags, Result.SelectedMultilib)) { 1450 Result.BiarchSibling = Multilib(); 1451 return true; 1452 } 1453 1454 return false; 1455 } 1456 1457 static void findAndroidArmMultilibs(const Driver &D, 1458 const llvm::Triple &TargetTriple, 1459 StringRef Path, const ArgList &Args, 1460 DetectedMultilibs &Result) { 1461 // Find multilibs with subdirectories like armv7-a, thumb, armv7-a/thumb. 1462 FilterNonExistent NonExistent(Path, "/crtbegin.o", D.getVFS()); 1463 Multilib ArmV7Multilib = makeMultilib("/armv7-a") 1464 .flag("+march=armv7-a") 1465 .flag("-mthumb"); 1466 Multilib ThumbMultilib = makeMultilib("/thumb") 1467 .flag("-march=armv7-a") 1468 .flag("+mthumb"); 1469 Multilib ArmV7ThumbMultilib = makeMultilib("/armv7-a/thumb") 1470 .flag("+march=armv7-a") 1471 .flag("+mthumb"); 1472 Multilib DefaultMultilib = makeMultilib("") 1473 .flag("-march=armv7-a") 1474 .flag("-mthumb"); 1475 MultilibSet AndroidArmMultilibs = 1476 MultilibSet() 1477 .Either(ThumbMultilib, ArmV7Multilib, 1478 ArmV7ThumbMultilib, DefaultMultilib) 1479 .FilterOut(NonExistent); 1480 1481 Multilib::flags_list Flags; 1482 llvm::StringRef Arch = Args.getLastArgValue(options::OPT_march_EQ); 1483 bool IsArmArch = TargetTriple.getArch() == llvm::Triple::arm; 1484 bool IsThumbArch = TargetTriple.getArch() == llvm::Triple::thumb; 1485 bool IsV7SubArch = TargetTriple.getSubArch() == llvm::Triple::ARMSubArch_v7; 1486 bool IsThumbMode = IsThumbArch || 1487 Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, false) || 1488 (IsArmArch && llvm::ARM::parseArchISA(Arch) == llvm::ARM::ISAKind::THUMB); 1489 bool IsArmV7Mode = (IsArmArch || IsThumbArch) && 1490 (llvm::ARM::parseArchVersion(Arch) == 7 || 1491 (IsArmArch && Arch == "" && IsV7SubArch)); 1492 addMultilibFlag(IsArmV7Mode, "march=armv7-a", Flags); 1493 addMultilibFlag(IsThumbMode, "mthumb", Flags); 1494 1495 if (AndroidArmMultilibs.select(Flags, Result.SelectedMultilib)) 1496 Result.Multilibs = AndroidArmMultilibs; 1497 } 1498 1499 static bool findMSP430Multilibs(const Driver &D, 1500 const llvm::Triple &TargetTriple, 1501 StringRef Path, const ArgList &Args, 1502 DetectedMultilibs &Result) { 1503 FilterNonExistent NonExistent(Path, "/crtbegin.o", D.getVFS()); 1504 Multilib MSP430Multilib = makeMultilib("/430"); 1505 // FIXME: when clang starts to support msp430x ISA additional logic 1506 // to select between multilib must be implemented 1507 // Multilib MSP430xMultilib = makeMultilib("/large"); 1508 1509 Result.Multilibs.push_back(MSP430Multilib); 1510 Result.Multilibs.FilterOut(NonExistent); 1511 1512 Multilib::flags_list Flags; 1513 if (Result.Multilibs.select(Flags, Result.SelectedMultilib)) 1514 return true; 1515 1516 return false; 1517 } 1518 1519 static void findRISCVBareMetalMultilibs(const Driver &D, 1520 const llvm::Triple &TargetTriple, 1521 StringRef Path, const ArgList &Args, 1522 DetectedMultilibs &Result) { 1523 FilterNonExistent NonExistent(Path, "/crtbegin.o", D.getVFS()); 1524 struct RiscvMultilib { 1525 StringRef march; 1526 StringRef mabi; 1527 }; 1528 // currently only support the set of multilibs like riscv-gnu-toolchain does. 1529 // TODO: support MULTILIB_REUSE 1530 SmallVector<RiscvMultilib, 8> RISCVMultilibSet = { 1531 {"rv32i", "ilp32"}, {"rv32im", "ilp32"}, {"rv32iac", "ilp32"}, 1532 {"rv32imac", "ilp32"}, {"rv32imafc", "ilp32f"}, {"rv64imac", "lp64"}, 1533 {"rv64imafdc", "lp64d"}}; 1534 1535 std::vector<Multilib> Ms; 1536 for (auto Element : RISCVMultilibSet) { 1537 // multilib path rule is ${march}/${mabi} 1538 Ms.emplace_back( 1539 makeMultilib((Twine(Element.march) + "/" + Twine(Element.mabi)).str()) 1540 .flag(Twine("+march=", Element.march).str()) 1541 .flag(Twine("+mabi=", Element.mabi).str())); 1542 } 1543 MultilibSet RISCVMultilibs = 1544 MultilibSet() 1545 .Either(ArrayRef<Multilib>(Ms)) 1546 .FilterOut(NonExistent) 1547 .setFilePathsCallback([](const Multilib &M) { 1548 return std::vector<std::string>( 1549 {M.gccSuffix(), 1550 "/../../../../riscv64-unknown-elf/lib" + M.gccSuffix(), 1551 "/../../../../riscv32-unknown-elf/lib" + M.gccSuffix()}); 1552 }); 1553 1554 1555 Multilib::flags_list Flags; 1556 llvm::StringSet<> Added_ABIs; 1557 StringRef ABIName = tools::riscv::getRISCVABI(Args, TargetTriple); 1558 StringRef MArch = tools::riscv::getRISCVArch(Args, TargetTriple); 1559 for (auto Element : RISCVMultilibSet) { 1560 addMultilibFlag(MArch == Element.march, 1561 Twine("march=", Element.march).str().c_str(), Flags); 1562 if (!Added_ABIs.count(Element.mabi)) { 1563 Added_ABIs.insert(Element.mabi); 1564 addMultilibFlag(ABIName == Element.mabi, 1565 Twine("mabi=", Element.mabi).str().c_str(), Flags); 1566 } 1567 } 1568 1569 if (RISCVMultilibs.select(Flags, Result.SelectedMultilib)) 1570 Result.Multilibs = RISCVMultilibs; 1571 } 1572 1573 static void findRISCVMultilibs(const Driver &D, 1574 const llvm::Triple &TargetTriple, StringRef Path, 1575 const ArgList &Args, DetectedMultilibs &Result) { 1576 if (TargetTriple.getOS() == llvm::Triple::UnknownOS) 1577 return findRISCVBareMetalMultilibs(D, TargetTriple, Path, Args, Result); 1578 1579 FilterNonExistent NonExistent(Path, "/crtbegin.o", D.getVFS()); 1580 Multilib Ilp32 = makeMultilib("lib32/ilp32").flag("+m32").flag("+mabi=ilp32"); 1581 Multilib Ilp32f = 1582 makeMultilib("lib32/ilp32f").flag("+m32").flag("+mabi=ilp32f"); 1583 Multilib Ilp32d = 1584 makeMultilib("lib32/ilp32d").flag("+m32").flag("+mabi=ilp32d"); 1585 Multilib Lp64 = makeMultilib("lib64/lp64").flag("+m64").flag("+mabi=lp64"); 1586 Multilib Lp64f = makeMultilib("lib64/lp64f").flag("+m64").flag("+mabi=lp64f"); 1587 Multilib Lp64d = makeMultilib("lib64/lp64d").flag("+m64").flag("+mabi=lp64d"); 1588 MultilibSet RISCVMultilibs = 1589 MultilibSet() 1590 .Either({Ilp32, Ilp32f, Ilp32d, Lp64, Lp64f, Lp64d}) 1591 .FilterOut(NonExistent); 1592 1593 Multilib::flags_list Flags; 1594 bool IsRV64 = TargetTriple.getArch() == llvm::Triple::riscv64; 1595 StringRef ABIName = tools::riscv::getRISCVABI(Args, TargetTriple); 1596 1597 addMultilibFlag(!IsRV64, "m32", Flags); 1598 addMultilibFlag(IsRV64, "m64", Flags); 1599 addMultilibFlag(ABIName == "ilp32", "mabi=ilp32", Flags); 1600 addMultilibFlag(ABIName == "ilp32f", "mabi=ilp32f", Flags); 1601 addMultilibFlag(ABIName == "ilp32d", "mabi=ilp32d", Flags); 1602 addMultilibFlag(ABIName == "lp64", "mabi=lp64", Flags); 1603 addMultilibFlag(ABIName == "lp64f", "mabi=lp64f", Flags); 1604 addMultilibFlag(ABIName == "lp64d", "mabi=lp64d", Flags); 1605 1606 if (RISCVMultilibs.select(Flags, Result.SelectedMultilib)) 1607 Result.Multilibs = RISCVMultilibs; 1608 } 1609 1610 static bool findBiarchMultilibs(const Driver &D, 1611 const llvm::Triple &TargetTriple, 1612 StringRef Path, const ArgList &Args, 1613 bool NeedsBiarchSuffix, 1614 DetectedMultilibs &Result) { 1615 Multilib Default; 1616 1617 // Some versions of SUSE and Fedora on ppc64 put 32-bit libs 1618 // in what would normally be GCCInstallPath and put the 64-bit 1619 // libs in a subdirectory named 64. The simple logic we follow is that 1620 // *if* there is a subdirectory of the right name with crtbegin.o in it, 1621 // we use that. If not, and if not a biarch triple alias, we look for 1622 // crtbegin.o without the subdirectory. 1623 1624 StringRef Suff64 = "/64"; 1625 // Solaris uses platform-specific suffixes instead of /64. 1626 if (TargetTriple.getOS() == llvm::Triple::Solaris) { 1627 switch (TargetTriple.getArch()) { 1628 case llvm::Triple::x86: 1629 case llvm::Triple::x86_64: 1630 Suff64 = "/amd64"; 1631 break; 1632 case llvm::Triple::sparc: 1633 case llvm::Triple::sparcv9: 1634 Suff64 = "/sparcv9"; 1635 break; 1636 default: 1637 break; 1638 } 1639 } 1640 1641 Multilib Alt64 = Multilib() 1642 .gccSuffix(Suff64) 1643 .includeSuffix(Suff64) 1644 .flag("-m32") 1645 .flag("+m64") 1646 .flag("-mx32"); 1647 Multilib Alt32 = Multilib() 1648 .gccSuffix("/32") 1649 .includeSuffix("/32") 1650 .flag("+m32") 1651 .flag("-m64") 1652 .flag("-mx32"); 1653 Multilib Altx32 = Multilib() 1654 .gccSuffix("/x32") 1655 .includeSuffix("/x32") 1656 .flag("-m32") 1657 .flag("-m64") 1658 .flag("+mx32"); 1659 1660 // GCC toolchain for IAMCU doesn't have crtbegin.o, so look for libgcc.a. 1661 FilterNonExistent NonExistent( 1662 Path, TargetTriple.isOSIAMCU() ? "/libgcc.a" : "/crtbegin.o", D.getVFS()); 1663 1664 // Determine default multilib from: 32, 64, x32 1665 // Also handle cases such as 64 on 32, 32 on 64, etc. 1666 enum { UNKNOWN, WANT32, WANT64, WANTX32 } Want = UNKNOWN; 1667 const bool IsX32 = TargetTriple.getEnvironment() == llvm::Triple::GNUX32; 1668 if (TargetTriple.isArch32Bit() && !NonExistent(Alt32)) 1669 Want = WANT64; 1670 else if (TargetTriple.isArch64Bit() && IsX32 && !NonExistent(Altx32)) 1671 Want = WANT64; 1672 else if (TargetTriple.isArch64Bit() && !IsX32 && !NonExistent(Alt64)) 1673 Want = WANT32; 1674 else { 1675 if (TargetTriple.isArch32Bit()) 1676 Want = NeedsBiarchSuffix ? WANT64 : WANT32; 1677 else if (IsX32) 1678 Want = NeedsBiarchSuffix ? WANT64 : WANTX32; 1679 else 1680 Want = NeedsBiarchSuffix ? WANT32 : WANT64; 1681 } 1682 1683 if (Want == WANT32) 1684 Default.flag("+m32").flag("-m64").flag("-mx32"); 1685 else if (Want == WANT64) 1686 Default.flag("-m32").flag("+m64").flag("-mx32"); 1687 else if (Want == WANTX32) 1688 Default.flag("-m32").flag("-m64").flag("+mx32"); 1689 else 1690 return false; 1691 1692 Result.Multilibs.push_back(Default); 1693 Result.Multilibs.push_back(Alt64); 1694 Result.Multilibs.push_back(Alt32); 1695 Result.Multilibs.push_back(Altx32); 1696 1697 Result.Multilibs.FilterOut(NonExistent); 1698 1699 Multilib::flags_list Flags; 1700 addMultilibFlag(TargetTriple.isArch64Bit() && !IsX32, "m64", Flags); 1701 addMultilibFlag(TargetTriple.isArch32Bit(), "m32", Flags); 1702 addMultilibFlag(TargetTriple.isArch64Bit() && IsX32, "mx32", Flags); 1703 1704 if (!Result.Multilibs.select(Flags, Result.SelectedMultilib)) 1705 return false; 1706 1707 if (Result.SelectedMultilib == Alt64 || Result.SelectedMultilib == Alt32 || 1708 Result.SelectedMultilib == Altx32) 1709 Result.BiarchSibling = Default; 1710 1711 return true; 1712 } 1713 1714 /// Generic_GCC - A tool chain using the 'gcc' command to perform 1715 /// all subcommands; this relies on gcc translating the majority of 1716 /// command line options. 1717 1718 /// Less-than for GCCVersion, implementing a Strict Weak Ordering. 1719 bool Generic_GCC::GCCVersion::isOlderThan(int RHSMajor, int RHSMinor, 1720 int RHSPatch, 1721 StringRef RHSPatchSuffix) const { 1722 if (Major != RHSMajor) 1723 return Major < RHSMajor; 1724 if (Minor != RHSMinor) 1725 return Minor < RHSMinor; 1726 if (Patch != RHSPatch) { 1727 // Note that versions without a specified patch sort higher than those with 1728 // a patch. 1729 if (RHSPatch == -1) 1730 return true; 1731 if (Patch == -1) 1732 return false; 1733 1734 // Otherwise just sort on the patch itself. 1735 return Patch < RHSPatch; 1736 } 1737 if (PatchSuffix != RHSPatchSuffix) { 1738 // Sort empty suffixes higher. 1739 if (RHSPatchSuffix.empty()) 1740 return true; 1741 if (PatchSuffix.empty()) 1742 return false; 1743 1744 // Provide a lexicographic sort to make this a total ordering. 1745 return PatchSuffix < RHSPatchSuffix; 1746 } 1747 1748 // The versions are equal. 1749 return false; 1750 } 1751 1752 /// Parse a GCCVersion object out of a string of text. 1753 /// 1754 /// This is the primary means of forming GCCVersion objects. 1755 /*static*/ 1756 Generic_GCC::GCCVersion Generic_GCC::GCCVersion::Parse(StringRef VersionText) { 1757 const GCCVersion BadVersion = {VersionText.str(), -1, -1, -1, "", "", ""}; 1758 std::pair<StringRef, StringRef> First = VersionText.split('.'); 1759 std::pair<StringRef, StringRef> Second = First.second.split('.'); 1760 1761 GCCVersion GoodVersion = {VersionText.str(), -1, -1, -1, "", "", ""}; 1762 if (First.first.getAsInteger(10, GoodVersion.Major) || GoodVersion.Major < 0) 1763 return BadVersion; 1764 GoodVersion.MajorStr = First.first.str(); 1765 if (First.second.empty()) 1766 return GoodVersion; 1767 StringRef MinorStr = Second.first; 1768 if (Second.second.empty()) { 1769 if (size_t EndNumber = MinorStr.find_first_not_of("0123456789")) { 1770 GoodVersion.PatchSuffix = MinorStr.substr(EndNumber); 1771 MinorStr = MinorStr.slice(0, EndNumber); 1772 } 1773 } 1774 if (MinorStr.getAsInteger(10, GoodVersion.Minor) || GoodVersion.Minor < 0) 1775 return BadVersion; 1776 GoodVersion.MinorStr = MinorStr.str(); 1777 1778 // First look for a number prefix and parse that if present. Otherwise just 1779 // stash the entire patch string in the suffix, and leave the number 1780 // unspecified. This covers versions strings such as: 1781 // 5 (handled above) 1782 // 4.4 1783 // 4.4-patched 1784 // 4.4.0 1785 // 4.4.x 1786 // 4.4.2-rc4 1787 // 4.4.x-patched 1788 // And retains any patch number it finds. 1789 StringRef PatchText = Second.second; 1790 if (!PatchText.empty()) { 1791 if (size_t EndNumber = PatchText.find_first_not_of("0123456789")) { 1792 // Try to parse the number and any suffix. 1793 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) || 1794 GoodVersion.Patch < 0) 1795 return BadVersion; 1796 GoodVersion.PatchSuffix = PatchText.substr(EndNumber); 1797 } 1798 } 1799 1800 return GoodVersion; 1801 } 1802 1803 static llvm::StringRef getGCCToolchainDir(const ArgList &Args, 1804 llvm::StringRef SysRoot) { 1805 const Arg *A = Args.getLastArg(clang::driver::options::OPT_gcc_toolchain); 1806 if (A) 1807 return A->getValue(); 1808 1809 // If we have a SysRoot, ignore GCC_INSTALL_PREFIX. 1810 // GCC_INSTALL_PREFIX specifies the gcc installation for the default 1811 // sysroot and is likely not valid with a different sysroot. 1812 if (!SysRoot.empty()) 1813 return ""; 1814 1815 return GCC_INSTALL_PREFIX; 1816 } 1817 1818 /// Initialize a GCCInstallationDetector from the driver. 1819 /// 1820 /// This performs all of the autodetection and sets up the various paths. 1821 /// Once constructed, a GCCInstallationDetector is essentially immutable. 1822 /// 1823 /// FIXME: We shouldn't need an explicit TargetTriple parameter here, and 1824 /// should instead pull the target out of the driver. This is currently 1825 /// necessary because the driver doesn't store the final version of the target 1826 /// triple. 1827 void Generic_GCC::GCCInstallationDetector::init( 1828 const llvm::Triple &TargetTriple, const ArgList &Args, 1829 ArrayRef<std::string> ExtraTripleAliases) { 1830 llvm::Triple BiarchVariantTriple = TargetTriple.isArch32Bit() 1831 ? TargetTriple.get64BitArchVariant() 1832 : TargetTriple.get32BitArchVariant(); 1833 // The library directories which may contain GCC installations. 1834 SmallVector<StringRef, 4> CandidateLibDirs, CandidateBiarchLibDirs; 1835 // The compatible GCC triples for this particular architecture. 1836 SmallVector<StringRef, 16> CandidateTripleAliases; 1837 SmallVector<StringRef, 16> CandidateBiarchTripleAliases; 1838 CollectLibDirsAndTriples(TargetTriple, BiarchVariantTriple, CandidateLibDirs, 1839 CandidateTripleAliases, CandidateBiarchLibDirs, 1840 CandidateBiarchTripleAliases); 1841 1842 // Compute the set of prefixes for our search. 1843 SmallVector<std::string, 8> Prefixes(D.PrefixDirs.begin(), 1844 D.PrefixDirs.end()); 1845 1846 StringRef GCCToolchainDir = getGCCToolchainDir(Args, D.SysRoot); 1847 if (GCCToolchainDir != "") { 1848 if (GCCToolchainDir.back() == '/') 1849 GCCToolchainDir = GCCToolchainDir.drop_back(); // remove the / 1850 1851 Prefixes.push_back(GCCToolchainDir); 1852 } else { 1853 // If we have a SysRoot, try that first. 1854 if (!D.SysRoot.empty()) { 1855 Prefixes.push_back(D.SysRoot); 1856 AddDefaultGCCPrefixes(TargetTriple, Prefixes, D.SysRoot); 1857 } 1858 1859 // Then look for gcc installed alongside clang. 1860 Prefixes.push_back(D.InstalledDir + "/.."); 1861 1862 // Next, look for prefix(es) that correspond to distribution-supplied gcc 1863 // installations. 1864 if (D.SysRoot.empty()) { 1865 // Typically /usr. 1866 AddDefaultGCCPrefixes(TargetTriple, Prefixes, D.SysRoot); 1867 } 1868 } 1869 1870 // Try to respect gcc-config on Gentoo. However, do that only 1871 // if --gcc-toolchain is not provided or equal to the Gentoo install 1872 // in /usr. This avoids accidentally enforcing the system GCC version 1873 // when using a custom toolchain. 1874 if (GCCToolchainDir == "" || GCCToolchainDir == D.SysRoot + "/usr") { 1875 SmallVector<StringRef, 16> GentooTestTriples; 1876 // Try to match an exact triple as target triple first. 1877 // e.g. crossdev -S x86_64-gentoo-linux-gnu will install gcc libs for 1878 // x86_64-gentoo-linux-gnu. But "clang -target x86_64-gentoo-linux-gnu" 1879 // may pick the libraries for x86_64-pc-linux-gnu even when exact matching 1880 // triple x86_64-gentoo-linux-gnu is present. 1881 GentooTestTriples.push_back(TargetTriple.str()); 1882 // Check rest of triples. 1883 GentooTestTriples.append(ExtraTripleAliases.begin(), 1884 ExtraTripleAliases.end()); 1885 GentooTestTriples.append(CandidateTripleAliases.begin(), 1886 CandidateTripleAliases.end()); 1887 if (ScanGentooConfigs(TargetTriple, Args, GentooTestTriples, 1888 CandidateBiarchTripleAliases)) 1889 return; 1890 } 1891 1892 // Loop over the various components which exist and select the best GCC 1893 // installation available. GCC installs are ranked by version number. 1894 Version = GCCVersion::Parse("0.0.0"); 1895 for (const std::string &Prefix : Prefixes) { 1896 if (!D.getVFS().exists(Prefix)) 1897 continue; 1898 for (StringRef Suffix : CandidateLibDirs) { 1899 const std::string LibDir = Prefix + Suffix.str(); 1900 if (!D.getVFS().exists(LibDir)) 1901 continue; 1902 // Try to match the exact target triple first. 1903 ScanLibDirForGCCTriple(TargetTriple, Args, LibDir, TargetTriple.str()); 1904 // Try rest of possible triples. 1905 for (StringRef Candidate : ExtraTripleAliases) // Try these first. 1906 ScanLibDirForGCCTriple(TargetTriple, Args, LibDir, Candidate); 1907 for (StringRef Candidate : CandidateTripleAliases) 1908 ScanLibDirForGCCTriple(TargetTriple, Args, LibDir, Candidate); 1909 } 1910 for (StringRef Suffix : CandidateBiarchLibDirs) { 1911 const std::string LibDir = Prefix + Suffix.str(); 1912 if (!D.getVFS().exists(LibDir)) 1913 continue; 1914 for (StringRef Candidate : CandidateBiarchTripleAliases) 1915 ScanLibDirForGCCTriple(TargetTriple, Args, LibDir, Candidate, 1916 /*NeedsBiarchSuffix=*/ true); 1917 } 1918 } 1919 } 1920 1921 void Generic_GCC::GCCInstallationDetector::print(raw_ostream &OS) const { 1922 for (const auto &InstallPath : CandidateGCCInstallPaths) 1923 OS << "Found candidate GCC installation: " << InstallPath << "\n"; 1924 1925 if (!GCCInstallPath.empty()) 1926 OS << "Selected GCC installation: " << GCCInstallPath << "\n"; 1927 1928 for (const auto &Multilib : Multilibs) 1929 OS << "Candidate multilib: " << Multilib << "\n"; 1930 1931 if (Multilibs.size() != 0 || !SelectedMultilib.isDefault()) 1932 OS << "Selected multilib: " << SelectedMultilib << "\n"; 1933 } 1934 1935 bool Generic_GCC::GCCInstallationDetector::getBiarchSibling(Multilib &M) const { 1936 if (BiarchSibling.hasValue()) { 1937 M = BiarchSibling.getValue(); 1938 return true; 1939 } 1940 return false; 1941 } 1942 1943 void Generic_GCC::GCCInstallationDetector::AddDefaultGCCPrefixes( 1944 const llvm::Triple &TargetTriple, SmallVectorImpl<std::string> &Prefixes, 1945 StringRef SysRoot) { 1946 if (TargetTriple.getOS() == llvm::Triple::Solaris) { 1947 // Solaris is a special case. 1948 // The GCC installation is under 1949 // /usr/gcc/<major>.<minor>/lib/gcc/<triple>/<major>.<minor>.<patch>/ 1950 // so we need to find those /usr/gcc/*/lib/gcc libdirs and go with 1951 // /usr/gcc/<version> as a prefix. 1952 1953 std::string PrefixDir = SysRoot.str() + "/usr/gcc"; 1954 std::error_code EC; 1955 for (llvm::vfs::directory_iterator LI = D.getVFS().dir_begin(PrefixDir, EC), 1956 LE; 1957 !EC && LI != LE; LI = LI.increment(EC)) { 1958 StringRef VersionText = llvm::sys::path::filename(LI->path()); 1959 GCCVersion CandidateVersion = GCCVersion::Parse(VersionText); 1960 1961 // Filter out obviously bad entries. 1962 if (CandidateVersion.Major == -1 || CandidateVersion.isOlderThan(4, 1, 1)) 1963 continue; 1964 1965 std::string CandidatePrefix = PrefixDir + "/" + VersionText.str(); 1966 std::string CandidateLibPath = CandidatePrefix + "/lib/gcc"; 1967 if (!D.getVFS().exists(CandidateLibPath)) 1968 continue; 1969 1970 Prefixes.push_back(CandidatePrefix); 1971 } 1972 return; 1973 } 1974 1975 // Non-Solaris is much simpler - most systems just go with "/usr". 1976 if (SysRoot.empty() && TargetTriple.getOS() == llvm::Triple::Linux) { 1977 // Yet, still look for RHEL devtoolsets. 1978 Prefixes.push_back("/opt/rh/devtoolset-8/root/usr"); 1979 Prefixes.push_back("/opt/rh/devtoolset-7/root/usr"); 1980 Prefixes.push_back("/opt/rh/devtoolset-6/root/usr"); 1981 Prefixes.push_back("/opt/rh/devtoolset-4/root/usr"); 1982 Prefixes.push_back("/opt/rh/devtoolset-3/root/usr"); 1983 Prefixes.push_back("/opt/rh/devtoolset-2/root/usr"); 1984 } 1985 Prefixes.push_back(SysRoot.str() + "/usr"); 1986 } 1987 1988 /*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples( 1989 const llvm::Triple &TargetTriple, const llvm::Triple &BiarchTriple, 1990 SmallVectorImpl<StringRef> &LibDirs, 1991 SmallVectorImpl<StringRef> &TripleAliases, 1992 SmallVectorImpl<StringRef> &BiarchLibDirs, 1993 SmallVectorImpl<StringRef> &BiarchTripleAliases) { 1994 // Declare a bunch of static data sets that we'll select between below. These 1995 // are specifically designed to always refer to string literals to avoid any 1996 // lifetime or initialization issues. 1997 static const char *const AArch64LibDirs[] = {"/lib64", "/lib"}; 1998 static const char *const AArch64Triples[] = { 1999 "aarch64-none-linux-gnu", "aarch64-linux-gnu", "aarch64-redhat-linux", 2000 "aarch64-suse-linux", "aarch64-linux-android"}; 2001 static const char *const AArch64beLibDirs[] = {"/lib"}; 2002 static const char *const AArch64beTriples[] = {"aarch64_be-none-linux-gnu", 2003 "aarch64_be-linux-gnu"}; 2004 2005 static const char *const ARMLibDirs[] = {"/lib"}; 2006 static const char *const ARMTriples[] = {"arm-linux-gnueabi", 2007 "arm-linux-androideabi"}; 2008 static const char *const ARMHFTriples[] = {"arm-linux-gnueabihf", 2009 "armv7hl-redhat-linux-gnueabi", 2010 "armv6hl-suse-linux-gnueabi", 2011 "armv7hl-suse-linux-gnueabi"}; 2012 static const char *const ARMebLibDirs[] = {"/lib"}; 2013 static const char *const ARMebTriples[] = {"armeb-linux-gnueabi", 2014 "armeb-linux-androideabi"}; 2015 static const char *const ARMebHFTriples[] = { 2016 "armeb-linux-gnueabihf", "armebv7hl-redhat-linux-gnueabi"}; 2017 2018 static const char *const AVRLibDirs[] = {"/lib"}; 2019 static const char *const AVRTriples[] = {"avr"}; 2020 2021 static const char *const X86_64LibDirs[] = {"/lib64", "/lib"}; 2022 static const char *const X86_64Triples[] = { 2023 "x86_64-linux-gnu", "x86_64-unknown-linux-gnu", 2024 "x86_64-pc-linux-gnu", "x86_64-redhat-linux6E", 2025 "x86_64-redhat-linux", "x86_64-suse-linux", 2026 "x86_64-manbo-linux-gnu", "x86_64-linux-gnu", 2027 "x86_64-slackware-linux", "x86_64-unknown-linux", 2028 "x86_64-amazon-linux", "x86_64-linux-android"}; 2029 static const char *const X32LibDirs[] = {"/libx32"}; 2030 static const char *const X86LibDirs[] = {"/lib32", "/lib"}; 2031 static const char *const X86Triples[] = { 2032 "i686-linux-gnu", "i686-pc-linux-gnu", "i486-linux-gnu", 2033 "i386-linux-gnu", "i386-redhat-linux6E", "i686-redhat-linux", 2034 "i586-redhat-linux", "i386-redhat-linux", "i586-suse-linux", 2035 "i486-slackware-linux", "i686-montavista-linux", "i586-linux-gnu", 2036 "i686-linux-android", "i386-gnu", "i486-gnu", 2037 "i586-gnu", "i686-gnu"}; 2038 2039 static const char *const MIPSLibDirs[] = {"/lib"}; 2040 static const char *const MIPSTriples[] = { 2041 "mips-linux-gnu", "mips-mti-linux", "mips-mti-linux-gnu", 2042 "mips-img-linux-gnu", "mipsisa32r6-linux-gnu"}; 2043 static const char *const MIPSELLibDirs[] = {"/lib"}; 2044 static const char *const MIPSELTriples[] = { 2045 "mipsel-linux-gnu", "mips-img-linux-gnu", "mipsisa32r6el-linux-gnu", 2046 "mipsel-linux-android"}; 2047 2048 static const char *const MIPS64LibDirs[] = {"/lib64", "/lib"}; 2049 static const char *const MIPS64Triples[] = { 2050 "mips64-linux-gnu", "mips-mti-linux-gnu", 2051 "mips-img-linux-gnu", "mips64-linux-gnuabi64", 2052 "mipsisa64r6-linux-gnu", "mipsisa64r6-linux-gnuabi64"}; 2053 static const char *const MIPS64ELLibDirs[] = {"/lib64", "/lib"}; 2054 static const char *const MIPS64ELTriples[] = { 2055 "mips64el-linux-gnu", "mips-mti-linux-gnu", 2056 "mips-img-linux-gnu", "mips64el-linux-gnuabi64", 2057 "mipsisa64r6el-linux-gnu", "mipsisa64r6el-linux-gnuabi64", 2058 "mips64el-linux-android"}; 2059 2060 static const char *const MIPSN32LibDirs[] = {"/lib32"}; 2061 static const char *const MIPSN32Triples[] = {"mips64-linux-gnuabin32", 2062 "mipsisa64r6-linux-gnuabin32"}; 2063 static const char *const MIPSN32ELLibDirs[] = {"/lib32"}; 2064 static const char *const MIPSN32ELTriples[] = { 2065 "mips64el-linux-gnuabin32", "mipsisa64r6el-linux-gnuabin32"}; 2066 2067 static const char *const MSP430LibDirs[] = {"/lib"}; 2068 static const char *const MSP430Triples[] = {"msp430-elf"}; 2069 2070 static const char *const PPCLibDirs[] = {"/lib32", "/lib"}; 2071 static const char *const PPCTriples[] = { 2072 "powerpc-linux-gnu", "powerpc-unknown-linux-gnu", "powerpc-linux-gnuspe", 2073 // On 32-bit PowerPC systems running SUSE Linux, gcc is configured as a 2074 // 64-bit compiler which defaults to "-m32", hence "powerpc64-suse-linux". 2075 "powerpc64-suse-linux", "powerpc-montavista-linuxspe"}; 2076 static const char *const PPC64LibDirs[] = {"/lib64", "/lib"}; 2077 static const char *const PPC64Triples[] = { 2078 "powerpc64-linux-gnu", "powerpc64-unknown-linux-gnu", 2079 "powerpc64-suse-linux", "ppc64-redhat-linux"}; 2080 static const char *const PPC64LELibDirs[] = {"/lib64", "/lib"}; 2081 static const char *const PPC64LETriples[] = { 2082 "powerpc64le-linux-gnu", "powerpc64le-unknown-linux-gnu", 2083 "powerpc64le-suse-linux", "ppc64le-redhat-linux"}; 2084 2085 static const char *const RISCV32LibDirs[] = {"/lib32", "/lib"}; 2086 static const char *const RISCV32Triples[] = {"riscv32-unknown-linux-gnu", 2087 "riscv32-linux-gnu", 2088 "riscv32-unknown-elf"}; 2089 static const char *const RISCV64LibDirs[] = {"/lib64", "/lib"}; 2090 static const char *const RISCV64Triples[] = {"riscv64-unknown-linux-gnu", 2091 "riscv64-linux-gnu", 2092 "riscv64-unknown-elf", 2093 "riscv64-suse-linux"}; 2094 2095 static const char *const SPARCv8LibDirs[] = {"/lib32", "/lib"}; 2096 static const char *const SPARCv8Triples[] = {"sparc-linux-gnu", 2097 "sparcv8-linux-gnu"}; 2098 static const char *const SPARCv9LibDirs[] = {"/lib64", "/lib"}; 2099 static const char *const SPARCv9Triples[] = {"sparc64-linux-gnu", 2100 "sparcv9-linux-gnu"}; 2101 2102 static const char *const SystemZLibDirs[] = {"/lib64", "/lib"}; 2103 static const char *const SystemZTriples[] = { 2104 "s390x-linux-gnu", "s390x-unknown-linux-gnu", "s390x-ibm-linux-gnu", 2105 "s390x-suse-linux", "s390x-redhat-linux"}; 2106 2107 2108 using std::begin; 2109 using std::end; 2110 2111 if (TargetTriple.getOS() == llvm::Triple::Solaris) { 2112 static const char *const SolarisLibDirs[] = {"/lib"}; 2113 static const char *const SolarisSparcV8Triples[] = { 2114 "sparc-sun-solaris2.11", "sparc-sun-solaris2.12"}; 2115 static const char *const SolarisSparcV9Triples[] = { 2116 "sparcv9-sun-solaris2.11", "sparcv9-sun-solaris2.12"}; 2117 static const char *const SolarisX86Triples[] = {"i386-pc-solaris2.11", 2118 "i386-pc-solaris2.12"}; 2119 static const char *const SolarisX86_64Triples[] = {"x86_64-pc-solaris2.11", 2120 "x86_64-pc-solaris2.12"}; 2121 LibDirs.append(begin(SolarisLibDirs), end(SolarisLibDirs)); 2122 BiarchLibDirs.append(begin(SolarisLibDirs), end(SolarisLibDirs)); 2123 switch (TargetTriple.getArch()) { 2124 case llvm::Triple::x86: 2125 TripleAliases.append(begin(SolarisX86Triples), end(SolarisX86Triples)); 2126 BiarchTripleAliases.append(begin(SolarisX86_64Triples), 2127 end(SolarisX86_64Triples)); 2128 break; 2129 case llvm::Triple::x86_64: 2130 TripleAliases.append(begin(SolarisX86_64Triples), 2131 end(SolarisX86_64Triples)); 2132 BiarchTripleAliases.append(begin(SolarisX86Triples), 2133 end(SolarisX86Triples)); 2134 break; 2135 case llvm::Triple::sparc: 2136 TripleAliases.append(begin(SolarisSparcV8Triples), 2137 end(SolarisSparcV8Triples)); 2138 BiarchTripleAliases.append(begin(SolarisSparcV9Triples), 2139 end(SolarisSparcV9Triples)); 2140 break; 2141 case llvm::Triple::sparcv9: 2142 TripleAliases.append(begin(SolarisSparcV9Triples), 2143 end(SolarisSparcV9Triples)); 2144 BiarchTripleAliases.append(begin(SolarisSparcV8Triples), 2145 end(SolarisSparcV8Triples)); 2146 break; 2147 default: 2148 break; 2149 } 2150 return; 2151 } 2152 2153 // Android targets should not use GNU/Linux tools or libraries. 2154 if (TargetTriple.isAndroid()) { 2155 static const char *const AArch64AndroidTriples[] = { 2156 "aarch64-linux-android"}; 2157 static const char *const ARMAndroidTriples[] = {"arm-linux-androideabi"}; 2158 static const char *const MIPSELAndroidTriples[] = {"mipsel-linux-android"}; 2159 static const char *const MIPS64ELAndroidTriples[] = { 2160 "mips64el-linux-android"}; 2161 static const char *const X86AndroidTriples[] = {"i686-linux-android"}; 2162 static const char *const X86_64AndroidTriples[] = {"x86_64-linux-android"}; 2163 2164 switch (TargetTriple.getArch()) { 2165 case llvm::Triple::aarch64: 2166 LibDirs.append(begin(AArch64LibDirs), end(AArch64LibDirs)); 2167 TripleAliases.append(begin(AArch64AndroidTriples), 2168 end(AArch64AndroidTriples)); 2169 break; 2170 case llvm::Triple::arm: 2171 case llvm::Triple::thumb: 2172 LibDirs.append(begin(ARMLibDirs), end(ARMLibDirs)); 2173 TripleAliases.append(begin(ARMAndroidTriples), end(ARMAndroidTriples)); 2174 break; 2175 case llvm::Triple::mipsel: 2176 LibDirs.append(begin(MIPSELLibDirs), end(MIPSELLibDirs)); 2177 TripleAliases.append(begin(MIPSELAndroidTriples), 2178 end(MIPSELAndroidTriples)); 2179 BiarchLibDirs.append(begin(MIPS64ELLibDirs), end(MIPS64ELLibDirs)); 2180 BiarchTripleAliases.append(begin(MIPS64ELAndroidTriples), 2181 end(MIPS64ELAndroidTriples)); 2182 break; 2183 case llvm::Triple::mips64el: 2184 LibDirs.append(begin(MIPS64ELLibDirs), end(MIPS64ELLibDirs)); 2185 TripleAliases.append(begin(MIPS64ELAndroidTriples), 2186 end(MIPS64ELAndroidTriples)); 2187 BiarchLibDirs.append(begin(MIPSELLibDirs), end(MIPSELLibDirs)); 2188 BiarchTripleAliases.append(begin(MIPSELAndroidTriples), 2189 end(MIPSELAndroidTriples)); 2190 break; 2191 case llvm::Triple::x86_64: 2192 LibDirs.append(begin(X86_64LibDirs), end(X86_64LibDirs)); 2193 TripleAliases.append(begin(X86_64AndroidTriples), 2194 end(X86_64AndroidTriples)); 2195 BiarchLibDirs.append(begin(X86LibDirs), end(X86LibDirs)); 2196 BiarchTripleAliases.append(begin(X86AndroidTriples), 2197 end(X86AndroidTriples)); 2198 break; 2199 case llvm::Triple::x86: 2200 LibDirs.append(begin(X86LibDirs), end(X86LibDirs)); 2201 TripleAliases.append(begin(X86AndroidTriples), end(X86AndroidTriples)); 2202 BiarchLibDirs.append(begin(X86_64LibDirs), end(X86_64LibDirs)); 2203 BiarchTripleAliases.append(begin(X86_64AndroidTriples), 2204 end(X86_64AndroidTriples)); 2205 break; 2206 default: 2207 break; 2208 } 2209 2210 return; 2211 } 2212 2213 switch (TargetTriple.getArch()) { 2214 case llvm::Triple::aarch64: 2215 LibDirs.append(begin(AArch64LibDirs), end(AArch64LibDirs)); 2216 TripleAliases.append(begin(AArch64Triples), end(AArch64Triples)); 2217 BiarchLibDirs.append(begin(AArch64LibDirs), end(AArch64LibDirs)); 2218 BiarchTripleAliases.append(begin(AArch64Triples), end(AArch64Triples)); 2219 break; 2220 case llvm::Triple::aarch64_be: 2221 LibDirs.append(begin(AArch64beLibDirs), end(AArch64beLibDirs)); 2222 TripleAliases.append(begin(AArch64beTriples), end(AArch64beTriples)); 2223 BiarchLibDirs.append(begin(AArch64beLibDirs), end(AArch64beLibDirs)); 2224 BiarchTripleAliases.append(begin(AArch64beTriples), end(AArch64beTriples)); 2225 break; 2226 case llvm::Triple::arm: 2227 case llvm::Triple::thumb: 2228 LibDirs.append(begin(ARMLibDirs), end(ARMLibDirs)); 2229 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) { 2230 TripleAliases.append(begin(ARMHFTriples), end(ARMHFTriples)); 2231 } else { 2232 TripleAliases.append(begin(ARMTriples), end(ARMTriples)); 2233 } 2234 break; 2235 case llvm::Triple::armeb: 2236 case llvm::Triple::thumbeb: 2237 LibDirs.append(begin(ARMebLibDirs), end(ARMebLibDirs)); 2238 if (TargetTriple.getEnvironment() == llvm::Triple::GNUEABIHF) { 2239 TripleAliases.append(begin(ARMebHFTriples), end(ARMebHFTriples)); 2240 } else { 2241 TripleAliases.append(begin(ARMebTriples), end(ARMebTriples)); 2242 } 2243 break; 2244 case llvm::Triple::avr: 2245 LibDirs.append(begin(AVRLibDirs), end(AVRLibDirs)); 2246 TripleAliases.append(begin(AVRTriples), end(AVRTriples)); 2247 break; 2248 case llvm::Triple::x86_64: 2249 LibDirs.append(begin(X86_64LibDirs), end(X86_64LibDirs)); 2250 TripleAliases.append(begin(X86_64Triples), end(X86_64Triples)); 2251 // x32 is always available when x86_64 is available, so adding it as 2252 // secondary arch with x86_64 triples 2253 if (TargetTriple.getEnvironment() == llvm::Triple::GNUX32) { 2254 BiarchLibDirs.append(begin(X32LibDirs), end(X32LibDirs)); 2255 BiarchTripleAliases.append(begin(X86_64Triples), end(X86_64Triples)); 2256 } else { 2257 BiarchLibDirs.append(begin(X86LibDirs), end(X86LibDirs)); 2258 BiarchTripleAliases.append(begin(X86Triples), end(X86Triples)); 2259 } 2260 break; 2261 case llvm::Triple::x86: 2262 LibDirs.append(begin(X86LibDirs), end(X86LibDirs)); 2263 // MCU toolchain is 32 bit only and its triple alias is TargetTriple 2264 // itself, which will be appended below. 2265 if (!TargetTriple.isOSIAMCU()) { 2266 TripleAliases.append(begin(X86Triples), end(X86Triples)); 2267 BiarchLibDirs.append(begin(X86_64LibDirs), end(X86_64LibDirs)); 2268 BiarchTripleAliases.append(begin(X86_64Triples), end(X86_64Triples)); 2269 } 2270 break; 2271 case llvm::Triple::mips: 2272 LibDirs.append(begin(MIPSLibDirs), end(MIPSLibDirs)); 2273 TripleAliases.append(begin(MIPSTriples), end(MIPSTriples)); 2274 BiarchLibDirs.append(begin(MIPS64LibDirs), end(MIPS64LibDirs)); 2275 BiarchTripleAliases.append(begin(MIPS64Triples), end(MIPS64Triples)); 2276 BiarchLibDirs.append(begin(MIPSN32LibDirs), end(MIPSN32LibDirs)); 2277 BiarchTripleAliases.append(begin(MIPSN32Triples), end(MIPSN32Triples)); 2278 break; 2279 case llvm::Triple::mipsel: 2280 LibDirs.append(begin(MIPSELLibDirs), end(MIPSELLibDirs)); 2281 TripleAliases.append(begin(MIPSELTriples), end(MIPSELTriples)); 2282 TripleAliases.append(begin(MIPSTriples), end(MIPSTriples)); 2283 BiarchLibDirs.append(begin(MIPS64ELLibDirs), end(MIPS64ELLibDirs)); 2284 BiarchTripleAliases.append(begin(MIPS64ELTriples), end(MIPS64ELTriples)); 2285 BiarchLibDirs.append(begin(MIPSN32ELLibDirs), end(MIPSN32ELLibDirs)); 2286 BiarchTripleAliases.append(begin(MIPSN32ELTriples), end(MIPSN32ELTriples)); 2287 break; 2288 case llvm::Triple::mips64: 2289 LibDirs.append(begin(MIPS64LibDirs), end(MIPS64LibDirs)); 2290 TripleAliases.append(begin(MIPS64Triples), end(MIPS64Triples)); 2291 BiarchLibDirs.append(begin(MIPSLibDirs), end(MIPSLibDirs)); 2292 BiarchTripleAliases.append(begin(MIPSTriples), end(MIPSTriples)); 2293 BiarchLibDirs.append(begin(MIPSN32LibDirs), end(MIPSN32LibDirs)); 2294 BiarchTripleAliases.append(begin(MIPSN32Triples), end(MIPSN32Triples)); 2295 break; 2296 case llvm::Triple::mips64el: 2297 LibDirs.append(begin(MIPS64ELLibDirs), end(MIPS64ELLibDirs)); 2298 TripleAliases.append(begin(MIPS64ELTriples), end(MIPS64ELTriples)); 2299 BiarchLibDirs.append(begin(MIPSELLibDirs), end(MIPSELLibDirs)); 2300 BiarchTripleAliases.append(begin(MIPSELTriples), end(MIPSELTriples)); 2301 BiarchLibDirs.append(begin(MIPSN32ELLibDirs), end(MIPSN32ELLibDirs)); 2302 BiarchTripleAliases.append(begin(MIPSN32ELTriples), end(MIPSN32ELTriples)); 2303 BiarchTripleAliases.append(begin(MIPSTriples), end(MIPSTriples)); 2304 break; 2305 case llvm::Triple::msp430: 2306 LibDirs.append(begin(MSP430LibDirs), end(MSP430LibDirs)); 2307 TripleAliases.append(begin(MSP430Triples), end(MSP430Triples)); 2308 break; 2309 case llvm::Triple::ppc: 2310 LibDirs.append(begin(PPCLibDirs), end(PPCLibDirs)); 2311 TripleAliases.append(begin(PPCTriples), end(PPCTriples)); 2312 BiarchLibDirs.append(begin(PPC64LibDirs), end(PPC64LibDirs)); 2313 BiarchTripleAliases.append(begin(PPC64Triples), end(PPC64Triples)); 2314 break; 2315 case llvm::Triple::ppc64: 2316 LibDirs.append(begin(PPC64LibDirs), end(PPC64LibDirs)); 2317 TripleAliases.append(begin(PPC64Triples), end(PPC64Triples)); 2318 BiarchLibDirs.append(begin(PPCLibDirs), end(PPCLibDirs)); 2319 BiarchTripleAliases.append(begin(PPCTriples), end(PPCTriples)); 2320 break; 2321 case llvm::Triple::ppc64le: 2322 LibDirs.append(begin(PPC64LELibDirs), end(PPC64LELibDirs)); 2323 TripleAliases.append(begin(PPC64LETriples), end(PPC64LETriples)); 2324 break; 2325 case llvm::Triple::riscv32: 2326 LibDirs.append(begin(RISCV32LibDirs), end(RISCV32LibDirs)); 2327 TripleAliases.append(begin(RISCV32Triples), end(RISCV32Triples)); 2328 BiarchLibDirs.append(begin(RISCV64LibDirs), end(RISCV64LibDirs)); 2329 BiarchTripleAliases.append(begin(RISCV64Triples), end(RISCV64Triples)); 2330 break; 2331 case llvm::Triple::riscv64: 2332 LibDirs.append(begin(RISCV64LibDirs), end(RISCV64LibDirs)); 2333 TripleAliases.append(begin(RISCV64Triples), end(RISCV64Triples)); 2334 BiarchLibDirs.append(begin(RISCV32LibDirs), end(RISCV32LibDirs)); 2335 BiarchTripleAliases.append(begin(RISCV32Triples), end(RISCV32Triples)); 2336 break; 2337 case llvm::Triple::sparc: 2338 case llvm::Triple::sparcel: 2339 LibDirs.append(begin(SPARCv8LibDirs), end(SPARCv8LibDirs)); 2340 TripleAliases.append(begin(SPARCv8Triples), end(SPARCv8Triples)); 2341 BiarchLibDirs.append(begin(SPARCv9LibDirs), end(SPARCv9LibDirs)); 2342 BiarchTripleAliases.append(begin(SPARCv9Triples), end(SPARCv9Triples)); 2343 break; 2344 case llvm::Triple::sparcv9: 2345 LibDirs.append(begin(SPARCv9LibDirs), end(SPARCv9LibDirs)); 2346 TripleAliases.append(begin(SPARCv9Triples), end(SPARCv9Triples)); 2347 BiarchLibDirs.append(begin(SPARCv8LibDirs), end(SPARCv8LibDirs)); 2348 BiarchTripleAliases.append(begin(SPARCv8Triples), end(SPARCv8Triples)); 2349 break; 2350 case llvm::Triple::systemz: 2351 LibDirs.append(begin(SystemZLibDirs), end(SystemZLibDirs)); 2352 TripleAliases.append(begin(SystemZTriples), end(SystemZTriples)); 2353 break; 2354 default: 2355 // By default, just rely on the standard lib directories and the original 2356 // triple. 2357 break; 2358 } 2359 2360 // Always append the drivers target triple to the end, in case it doesn't 2361 // match any of our aliases. 2362 TripleAliases.push_back(TargetTriple.str()); 2363 2364 // Also include the multiarch variant if it's different. 2365 if (TargetTriple.str() != BiarchTriple.str()) 2366 BiarchTripleAliases.push_back(BiarchTriple.str()); 2367 } 2368 2369 bool Generic_GCC::GCCInstallationDetector::ScanGCCForMultilibs( 2370 const llvm::Triple &TargetTriple, const ArgList &Args, 2371 StringRef Path, bool NeedsBiarchSuffix) { 2372 llvm::Triple::ArchType TargetArch = TargetTriple.getArch(); 2373 DetectedMultilibs Detected; 2374 2375 // Android standalone toolchain could have multilibs for ARM and Thumb. 2376 // Debian mips multilibs behave more like the rest of the biarch ones, 2377 // so handle them there 2378 if (isArmOrThumbArch(TargetArch) && TargetTriple.isAndroid()) { 2379 // It should also work without multilibs in a simplified toolchain. 2380 findAndroidArmMultilibs(D, TargetTriple, Path, Args, Detected); 2381 } else if (TargetTriple.isMIPS()) { 2382 if (!findMIPSMultilibs(D, TargetTriple, Path, Args, Detected)) 2383 return false; 2384 } else if (TargetTriple.isRISCV()) { 2385 findRISCVMultilibs(D, TargetTriple, Path, Args, Detected); 2386 } else if (isMSP430(TargetArch)) { 2387 findMSP430Multilibs(D, TargetTriple, Path, Args, Detected); 2388 } else if (TargetArch == llvm::Triple::avr) { 2389 // AVR has no multilibs. 2390 } else if (!findBiarchMultilibs(D, TargetTriple, Path, Args, 2391 NeedsBiarchSuffix, Detected)) { 2392 return false; 2393 } 2394 2395 Multilibs = Detected.Multilibs; 2396 SelectedMultilib = Detected.SelectedMultilib; 2397 BiarchSibling = Detected.BiarchSibling; 2398 2399 return true; 2400 } 2401 2402 void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple( 2403 const llvm::Triple &TargetTriple, const ArgList &Args, 2404 const std::string &LibDir, StringRef CandidateTriple, 2405 bool NeedsBiarchSuffix) { 2406 llvm::Triple::ArchType TargetArch = TargetTriple.getArch(); 2407 // Locations relative to the system lib directory where GCC's triple-specific 2408 // directories might reside. 2409 struct GCCLibSuffix { 2410 // Path from system lib directory to GCC triple-specific directory. 2411 std::string LibSuffix; 2412 // Path from GCC triple-specific directory back to system lib directory. 2413 // This is one '..' component per component in LibSuffix. 2414 StringRef ReversePath; 2415 // Whether this library suffix is relevant for the triple. 2416 bool Active; 2417 } Suffixes[] = { 2418 // This is the normal place. 2419 {"gcc/" + CandidateTriple.str(), "../..", true}, 2420 2421 // Debian puts cross-compilers in gcc-cross. 2422 {"gcc-cross/" + CandidateTriple.str(), "../..", 2423 TargetTriple.getOS() != llvm::Triple::Solaris}, 2424 2425 // The Freescale PPC SDK has the gcc libraries in 2426 // <sysroot>/usr/lib/<triple>/x.y.z so have a look there as well. Only do 2427 // this on Freescale triples, though, since some systems put a *lot* of 2428 // files in that location, not just GCC installation data. 2429 {CandidateTriple.str(), "..", 2430 TargetTriple.getVendor() == llvm::Triple::Freescale || 2431 TargetTriple.getVendor() == llvm::Triple::OpenEmbedded}, 2432 2433 // Natively multiarch systems sometimes put the GCC triple-specific 2434 // directory within their multiarch lib directory, resulting in the 2435 // triple appearing twice. 2436 {CandidateTriple.str() + "/gcc/" + CandidateTriple.str(), "../../..", 2437 TargetTriple.getOS() != llvm::Triple::Solaris}, 2438 2439 // Deal with cases (on Ubuntu) where the system architecture could be i386 2440 // but the GCC target architecture could be (say) i686. 2441 // FIXME: It may be worthwhile to generalize this and look for a second 2442 // triple. 2443 {"i386-linux-gnu/gcc/" + CandidateTriple.str(), "../../..", 2444 (TargetArch == llvm::Triple::x86 && 2445 TargetTriple.getOS() != llvm::Triple::Solaris)}, 2446 {"i386-gnu/gcc/" + CandidateTriple.str(), "../../..", 2447 (TargetArch == llvm::Triple::x86 && 2448 TargetTriple.getOS() != llvm::Triple::Solaris)}}; 2449 2450 for (auto &Suffix : Suffixes) { 2451 if (!Suffix.Active) 2452 continue; 2453 2454 StringRef LibSuffix = Suffix.LibSuffix; 2455 std::error_code EC; 2456 for (llvm::vfs::directory_iterator 2457 LI = D.getVFS().dir_begin(LibDir + "/" + LibSuffix, EC), 2458 LE; 2459 !EC && LI != LE; LI = LI.increment(EC)) { 2460 StringRef VersionText = llvm::sys::path::filename(LI->path()); 2461 GCCVersion CandidateVersion = GCCVersion::Parse(VersionText); 2462 if (CandidateVersion.Major != -1) // Filter obviously bad entries. 2463 if (!CandidateGCCInstallPaths.insert(LI->path()).second) 2464 continue; // Saw this path before; no need to look at it again. 2465 if (CandidateVersion.isOlderThan(4, 1, 1)) 2466 continue; 2467 if (CandidateVersion <= Version) 2468 continue; 2469 2470 if (!ScanGCCForMultilibs(TargetTriple, Args, LI->path(), 2471 NeedsBiarchSuffix)) 2472 continue; 2473 2474 Version = CandidateVersion; 2475 GCCTriple.setTriple(CandidateTriple); 2476 // FIXME: We hack together the directory name here instead of 2477 // using LI to ensure stable path separators across Windows and 2478 // Linux. 2479 GCCInstallPath = (LibDir + "/" + LibSuffix + "/" + VersionText).str(); 2480 GCCParentLibPath = (GCCInstallPath + "/../" + Suffix.ReversePath).str(); 2481 IsValid = true; 2482 } 2483 } 2484 } 2485 2486 bool Generic_GCC::GCCInstallationDetector::ScanGentooConfigs( 2487 const llvm::Triple &TargetTriple, const ArgList &Args, 2488 const SmallVectorImpl<StringRef> &CandidateTriples, 2489 const SmallVectorImpl<StringRef> &CandidateBiarchTriples) { 2490 for (StringRef CandidateTriple : CandidateTriples) { 2491 if (ScanGentooGccConfig(TargetTriple, Args, CandidateTriple)) 2492 return true; 2493 } 2494 2495 for (StringRef CandidateTriple : CandidateBiarchTriples) { 2496 if (ScanGentooGccConfig(TargetTriple, Args, CandidateTriple, true)) 2497 return true; 2498 } 2499 return false; 2500 } 2501 2502 bool Generic_GCC::GCCInstallationDetector::ScanGentooGccConfig( 2503 const llvm::Triple &TargetTriple, const ArgList &Args, 2504 StringRef CandidateTriple, bool NeedsBiarchSuffix) { 2505 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File = 2506 D.getVFS().getBufferForFile(D.SysRoot + "/etc/env.d/gcc/config-" + 2507 CandidateTriple.str()); 2508 if (File) { 2509 SmallVector<StringRef, 2> Lines; 2510 File.get()->getBuffer().split(Lines, "\n"); 2511 for (StringRef Line : Lines) { 2512 Line = Line.trim(); 2513 // CURRENT=triple-version 2514 if (!Line.consume_front("CURRENT=")) 2515 continue; 2516 // Process the config file pointed to by CURRENT. 2517 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ConfigFile = 2518 D.getVFS().getBufferForFile(D.SysRoot + "/etc/env.d/gcc/" + 2519 Line.str()); 2520 std::pair<StringRef, StringRef> ActiveVersion = Line.rsplit('-'); 2521 // List of paths to scan for libraries. 2522 SmallVector<StringRef, 4> GentooScanPaths; 2523 // Scan the Config file to find installed GCC libraries path. 2524 // Typical content of the GCC config file: 2525 // LDPATH="/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.x:/usr/lib/gcc/ 2526 // (continued from previous line) x86_64-pc-linux-gnu/4.9.x/32" 2527 // MANPATH="/usr/share/gcc-data/x86_64-pc-linux-gnu/4.9.x/man" 2528 // INFOPATH="/usr/share/gcc-data/x86_64-pc-linux-gnu/4.9.x/info" 2529 // STDCXX_INCDIR="/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.x/include/g++-v4" 2530 // We are looking for the paths listed in LDPATH=... . 2531 if (ConfigFile) { 2532 SmallVector<StringRef, 2> ConfigLines; 2533 ConfigFile.get()->getBuffer().split(ConfigLines, "\n"); 2534 for (StringRef ConfLine : ConfigLines) { 2535 ConfLine = ConfLine.trim(); 2536 if (ConfLine.consume_front("LDPATH=")) { 2537 // Drop '"' from front and back if present. 2538 ConfLine.consume_back("\""); 2539 ConfLine.consume_front("\""); 2540 // Get all paths sperated by ':' 2541 ConfLine.split(GentooScanPaths, ':', -1, /*AllowEmpty*/ false); 2542 } 2543 } 2544 } 2545 // Test the path based on the version in /etc/env.d/gcc/config-{tuple}. 2546 std::string basePath = "/usr/lib/gcc/" + ActiveVersion.first.str() + "/" 2547 + ActiveVersion.second.str(); 2548 GentooScanPaths.push_back(StringRef(basePath)); 2549 2550 // Scan all paths for GCC libraries. 2551 for (const auto &GentooScanPath : GentooScanPaths) { 2552 std::string GentooPath = D.SysRoot + std::string(GentooScanPath); 2553 if (D.getVFS().exists(GentooPath + "/crtbegin.o")) { 2554 if (!ScanGCCForMultilibs(TargetTriple, Args, GentooPath, 2555 NeedsBiarchSuffix)) 2556 continue; 2557 2558 Version = GCCVersion::Parse(ActiveVersion.second); 2559 GCCInstallPath = GentooPath; 2560 GCCParentLibPath = GentooPath + std::string("/../../.."); 2561 GCCTriple.setTriple(ActiveVersion.first); 2562 IsValid = true; 2563 return true; 2564 } 2565 } 2566 } 2567 } 2568 2569 return false; 2570 } 2571 2572 Generic_GCC::Generic_GCC(const Driver &D, const llvm::Triple &Triple, 2573 const ArgList &Args) 2574 : ToolChain(D, Triple, Args), GCCInstallation(D), 2575 CudaInstallation(D, Triple, Args) { 2576 getProgramPaths().push_back(getDriver().getInstalledDir()); 2577 if (getDriver().getInstalledDir() != getDriver().Dir) 2578 getProgramPaths().push_back(getDriver().Dir); 2579 } 2580 2581 Generic_GCC::~Generic_GCC() {} 2582 2583 Tool *Generic_GCC::getTool(Action::ActionClass AC) const { 2584 switch (AC) { 2585 case Action::PreprocessJobClass: 2586 if (!Preprocess) 2587 Preprocess.reset(new clang::driver::tools::gcc::Preprocessor(*this)); 2588 return Preprocess.get(); 2589 case Action::CompileJobClass: 2590 if (!Compile) 2591 Compile.reset(new tools::gcc::Compiler(*this)); 2592 return Compile.get(); 2593 default: 2594 return ToolChain::getTool(AC); 2595 } 2596 } 2597 2598 Tool *Generic_GCC::buildAssembler() const { 2599 return new tools::gnutools::Assembler(*this); 2600 } 2601 2602 Tool *Generic_GCC::buildLinker() const { return new tools::gcc::Linker(*this); } 2603 2604 void Generic_GCC::printVerboseInfo(raw_ostream &OS) const { 2605 // Print the information about how we detected the GCC installation. 2606 GCCInstallation.print(OS); 2607 CudaInstallation.print(OS); 2608 } 2609 2610 bool Generic_GCC::IsUnwindTablesDefault(const ArgList &Args) const { 2611 return getArch() == llvm::Triple::x86_64; 2612 } 2613 2614 bool Generic_GCC::isPICDefault() const { 2615 switch (getArch()) { 2616 case llvm::Triple::x86_64: 2617 return getTriple().isOSWindows(); 2618 case llvm::Triple::mips64: 2619 case llvm::Triple::mips64el: 2620 return true; 2621 default: 2622 return false; 2623 } 2624 } 2625 2626 bool Generic_GCC::isPIEDefault() const { return false; } 2627 2628 bool Generic_GCC::isPICDefaultForced() const { 2629 return getArch() == llvm::Triple::x86_64 && getTriple().isOSWindows(); 2630 } 2631 2632 bool Generic_GCC::IsIntegratedAssemblerDefault() const { 2633 switch (getTriple().getArch()) { 2634 case llvm::Triple::x86: 2635 case llvm::Triple::x86_64: 2636 case llvm::Triple::aarch64: 2637 case llvm::Triple::aarch64_be: 2638 case llvm::Triple::arm: 2639 case llvm::Triple::armeb: 2640 case llvm::Triple::avr: 2641 case llvm::Triple::bpfel: 2642 case llvm::Triple::bpfeb: 2643 case llvm::Triple::thumb: 2644 case llvm::Triple::thumbeb: 2645 case llvm::Triple::ppc: 2646 case llvm::Triple::ppc64: 2647 case llvm::Triple::ppc64le: 2648 case llvm::Triple::riscv32: 2649 case llvm::Triple::riscv64: 2650 case llvm::Triple::systemz: 2651 case llvm::Triple::mips: 2652 case llvm::Triple::mipsel: 2653 case llvm::Triple::mips64: 2654 case llvm::Triple::mips64el: 2655 case llvm::Triple::msp430: 2656 return true; 2657 case llvm::Triple::sparc: 2658 case llvm::Triple::sparcel: 2659 case llvm::Triple::sparcv9: 2660 if (getTriple().isOSFreeBSD() || getTriple().isOSOpenBSD() || 2661 getTriple().isOSSolaris()) 2662 return true; 2663 return false; 2664 default: 2665 return false; 2666 } 2667 } 2668 2669 void Generic_GCC::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 2670 ArgStringList &CC1Args) const { 2671 if (DriverArgs.hasArg(options::OPT_nostdlibinc) || 2672 DriverArgs.hasArg(options::OPT_nostdincxx)) 2673 return; 2674 2675 switch (GetCXXStdlibType(DriverArgs)) { 2676 case ToolChain::CST_Libcxx: 2677 addLibCxxIncludePaths(DriverArgs, CC1Args); 2678 break; 2679 2680 case ToolChain::CST_Libstdcxx: 2681 addLibStdCxxIncludePaths(DriverArgs, CC1Args); 2682 break; 2683 } 2684 } 2685 2686 static std::string DetectLibcxxIncludePath(llvm::vfs::FileSystem &vfs, 2687 StringRef base) { 2688 std::error_code EC; 2689 int MaxVersion = 0; 2690 std::string MaxVersionString; 2691 for (llvm::vfs::directory_iterator LI = vfs.dir_begin(base, EC), LE; 2692 !EC && LI != LE; LI = LI.increment(EC)) { 2693 StringRef VersionText = llvm::sys::path::filename(LI->path()); 2694 int Version; 2695 if (VersionText[0] == 'v' && 2696 !VersionText.slice(1, StringRef::npos).getAsInteger(10, Version)) { 2697 if (Version > MaxVersion) { 2698 MaxVersion = Version; 2699 MaxVersionString = VersionText; 2700 } 2701 } 2702 } 2703 return MaxVersion ? (base + "/" + MaxVersionString).str() : ""; 2704 } 2705 2706 void 2707 Generic_GCC::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, 2708 llvm::opt::ArgStringList &CC1Args) const { 2709 const std::string& SysRoot = getDriver().SysRoot; 2710 auto AddIncludePath = [&](std::string Path) { 2711 std::string IncludePath = DetectLibcxxIncludePath(getVFS(), Path); 2712 if (IncludePath.empty() || !getVFS().exists(IncludePath)) 2713 return false; 2714 addSystemInclude(DriverArgs, CC1Args, IncludePath); 2715 return true; 2716 }; 2717 // Android never uses the libc++ headers installed alongside the toolchain, 2718 // which are generally incompatible with the NDK libraries anyway. 2719 if (!getTriple().isAndroid()) 2720 if (AddIncludePath(getDriver().Dir + "/../include/c++")) 2721 return; 2722 // If this is a development, non-installed, clang, libcxx will 2723 // not be found at ../include/c++ but it likely to be found at 2724 // one of the following two locations: 2725 if (AddIncludePath(SysRoot + "/usr/local/include/c++")) 2726 return; 2727 if (AddIncludePath(SysRoot + "/usr/include/c++")) 2728 return; 2729 } 2730 2731 /// Helper to add the variant paths of a libstdc++ installation. 2732 bool Generic_GCC::addLibStdCXXIncludePaths( 2733 Twine Base, Twine Suffix, StringRef GCCTriple, StringRef GCCMultiarchTriple, 2734 StringRef TargetMultiarchTriple, Twine IncludeSuffix, 2735 const ArgList &DriverArgs, ArgStringList &CC1Args) const { 2736 if (!getVFS().exists(Base + Suffix)) 2737 return false; 2738 2739 addSystemInclude(DriverArgs, CC1Args, Base + Suffix); 2740 2741 // The vanilla GCC layout of libstdc++ headers uses a triple subdirectory. If 2742 // that path exists or we have neither a GCC nor target multiarch triple, use 2743 // this vanilla search path. 2744 if ((GCCMultiarchTriple.empty() && TargetMultiarchTriple.empty()) || 2745 getVFS().exists(Base + Suffix + "/" + GCCTriple + IncludeSuffix)) { 2746 addSystemInclude(DriverArgs, CC1Args, 2747 Base + Suffix + "/" + GCCTriple + IncludeSuffix); 2748 } else { 2749 // Otherwise try to use multiarch naming schemes which have normalized the 2750 // triples and put the triple before the suffix. 2751 // 2752 // GCC surprisingly uses *both* the GCC triple with a multilib suffix and 2753 // the target triple, so we support that here. 2754 addSystemInclude(DriverArgs, CC1Args, 2755 Base + "/" + GCCMultiarchTriple + Suffix + IncludeSuffix); 2756 addSystemInclude(DriverArgs, CC1Args, 2757 Base + "/" + TargetMultiarchTriple + Suffix); 2758 } 2759 2760 addSystemInclude(DriverArgs, CC1Args, Base + Suffix + "/backward"); 2761 return true; 2762 } 2763 2764 bool 2765 Generic_GCC::addGCCLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, 2766 llvm::opt::ArgStringList &CC1Args) const { 2767 // Use GCCInstallation to know where libstdc++ headers are installed. 2768 if (!GCCInstallation.isValid()) 2769 return false; 2770 2771 // By default, look for the C++ headers in an include directory adjacent to 2772 // the lib directory of the GCC installation. Note that this is expect to be 2773 // equivalent to '/usr/include/c++/X.Y' in almost all cases. 2774 StringRef LibDir = GCCInstallation.getParentLibPath(); 2775 StringRef InstallDir = GCCInstallation.getInstallPath(); 2776 StringRef TripleStr = GCCInstallation.getTriple().str(); 2777 const Multilib &Multilib = GCCInstallation.getMultilib(); 2778 const std::string GCCMultiarchTriple = getMultiarchTriple( 2779 getDriver(), GCCInstallation.getTriple(), getDriver().SysRoot); 2780 const std::string TargetMultiarchTriple = 2781 getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot); 2782 const GCCVersion &Version = GCCInstallation.getVersion(); 2783 2784 // The primary search for libstdc++ supports multiarch variants. 2785 if (addLibStdCXXIncludePaths(LibDir.str() + "/../include", 2786 "/c++/" + Version.Text, TripleStr, 2787 GCCMultiarchTriple, TargetMultiarchTriple, 2788 Multilib.includeSuffix(), DriverArgs, CC1Args)) 2789 return true; 2790 2791 // Otherwise, fall back on a bunch of options which don't use multiarch 2792 // layouts for simplicity. 2793 const std::string LibStdCXXIncludePathCandidates[] = { 2794 // Gentoo is weird and places its headers inside the GCC install, 2795 // so if the first attempt to find the headers fails, try these patterns. 2796 InstallDir.str() + "/include/g++-v" + Version.Text, 2797 InstallDir.str() + "/include/g++-v" + Version.MajorStr + "." + 2798 Version.MinorStr, 2799 InstallDir.str() + "/include/g++-v" + Version.MajorStr, 2800 }; 2801 2802 for (const auto &IncludePath : LibStdCXXIncludePathCandidates) { 2803 if (addLibStdCXXIncludePaths(IncludePath, /*Suffix*/ "", TripleStr, 2804 /*GCCMultiarchTriple*/ "", 2805 /*TargetMultiarchTriple*/ "", 2806 Multilib.includeSuffix(), DriverArgs, CC1Args)) 2807 return true; 2808 } 2809 return false; 2810 } 2811 2812 void 2813 Generic_GCC::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, 2814 llvm::opt::ArgStringList &CC1Args) const { 2815 addGCCLibStdCxxIncludePaths(DriverArgs, CC1Args); 2816 } 2817 2818 llvm::opt::DerivedArgList * 2819 Generic_GCC::TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef, 2820 Action::OffloadKind DeviceOffloadKind) const { 2821 2822 // If this tool chain is used for an OpenMP offloading device we have to make 2823 // sure we always generate a shared library regardless of the commands the 2824 // user passed to the host. This is required because the runtime library 2825 // is required to load the device image dynamically at run time. 2826 if (DeviceOffloadKind == Action::OFK_OpenMP) { 2827 DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs()); 2828 const OptTable &Opts = getDriver().getOpts(); 2829 2830 // Request the shared library. Given that these options are decided 2831 // implicitly, they do not refer to any base argument. 2832 DAL->AddFlagArg(/*BaseArg=*/nullptr, Opts.getOption(options::OPT_shared)); 2833 DAL->AddFlagArg(/*BaseArg=*/nullptr, Opts.getOption(options::OPT_fPIC)); 2834 2835 // Filter all the arguments we don't care passing to the offloading 2836 // toolchain as they can mess up with the creation of a shared library. 2837 for (auto *A : Args) { 2838 switch ((options::ID)A->getOption().getID()) { 2839 default: 2840 DAL->append(A); 2841 break; 2842 case options::OPT_shared: 2843 case options::OPT_dynamic: 2844 case options::OPT_static: 2845 case options::OPT_fPIC: 2846 case options::OPT_fno_PIC: 2847 case options::OPT_fpic: 2848 case options::OPT_fno_pic: 2849 case options::OPT_fPIE: 2850 case options::OPT_fno_PIE: 2851 case options::OPT_fpie: 2852 case options::OPT_fno_pie: 2853 break; 2854 } 2855 } 2856 return DAL; 2857 } 2858 return nullptr; 2859 } 2860 2861 void Generic_ELF::anchor() {} 2862 2863 void Generic_ELF::addClangTargetOptions(const ArgList &DriverArgs, 2864 ArgStringList &CC1Args, 2865 Action::OffloadKind) const { 2866 if (!DriverArgs.hasFlag(options::OPT_fuse_init_array, 2867 options::OPT_fno_use_init_array, true)) 2868 CC1Args.push_back("-fno-use-init-array"); 2869 } 2870