1 //===--- Linux.h - Linux ToolChain Implementations --------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "Linux.h" 11 #include "Arch/ARM.h" 12 #include "Arch/Mips.h" 13 #include "Arch/PPC.h" 14 #include "CommonArgs.h" 15 #include "clang/Basic/VirtualFileSystem.h" 16 #include "clang/Config/config.h" 17 #include "clang/Driver/Distro.h" 18 #include "clang/Driver/Driver.h" 19 #include "clang/Driver/Options.h" 20 #include "clang/Driver/SanitizerArgs.h" 21 #include "llvm/Option/ArgList.h" 22 #include "llvm/ProfileData/InstrProf.h" 23 #include "llvm/Support/Path.h" 24 #include <system_error> 25 26 using namespace clang::driver; 27 using namespace clang::driver::toolchains; 28 using namespace clang; 29 using namespace llvm::opt; 30 31 using tools::addPathIfExists; 32 33 /// \brief Get our best guess at the multiarch triple for a target. 34 /// 35 /// Debian-based systems are starting to use a multiarch setup where they use 36 /// a target-triple directory in the library and header search paths. 37 /// Unfortunately, this triple does not align with the vanilla target triple, 38 /// so we provide a rough mapping here. 39 static std::string getMultiarchTriple(const Driver &D, 40 const llvm::Triple &TargetTriple, 41 StringRef SysRoot) { 42 llvm::Triple::EnvironmentType TargetEnvironment = 43 TargetTriple.getEnvironment(); 44 45 // For most architectures, just use whatever we have rather than trying to be 46 // clever. 47 switch (TargetTriple.getArch()) { 48 default: 49 break; 50 51 // We use the existence of '/lib/<triple>' as a directory to detect some 52 // common linux triples that don't quite match the Clang triple for both 53 // 32-bit and 64-bit targets. Multiarch fixes its install triples to these 54 // regardless of what the actual target triple is. 55 case llvm::Triple::arm: 56 case llvm::Triple::thumb: 57 if (TargetEnvironment == llvm::Triple::GNUEABIHF) { 58 if (D.getVFS().exists(SysRoot + "/lib/arm-linux-gnueabihf")) 59 return "arm-linux-gnueabihf"; 60 } else { 61 if (D.getVFS().exists(SysRoot + "/lib/arm-linux-gnueabi")) 62 return "arm-linux-gnueabi"; 63 } 64 break; 65 case llvm::Triple::armeb: 66 case llvm::Triple::thumbeb: 67 if (TargetEnvironment == llvm::Triple::GNUEABIHF) { 68 if (D.getVFS().exists(SysRoot + "/lib/armeb-linux-gnueabihf")) 69 return "armeb-linux-gnueabihf"; 70 } else { 71 if (D.getVFS().exists(SysRoot + "/lib/armeb-linux-gnueabi")) 72 return "armeb-linux-gnueabi"; 73 } 74 break; 75 case llvm::Triple::x86: 76 if (D.getVFS().exists(SysRoot + "/lib/i386-linux-gnu")) 77 return "i386-linux-gnu"; 78 break; 79 case llvm::Triple::x86_64: 80 // We don't want this for x32, otherwise it will match x86_64 libs 81 if (TargetEnvironment != llvm::Triple::GNUX32 && 82 D.getVFS().exists(SysRoot + "/lib/x86_64-linux-gnu")) 83 return "x86_64-linux-gnu"; 84 break; 85 case llvm::Triple::aarch64: 86 if (D.getVFS().exists(SysRoot + "/lib/aarch64-linux-gnu")) 87 return "aarch64-linux-gnu"; 88 break; 89 case llvm::Triple::aarch64_be: 90 if (D.getVFS().exists(SysRoot + "/lib/aarch64_be-linux-gnu")) 91 return "aarch64_be-linux-gnu"; 92 break; 93 case llvm::Triple::mips: 94 if (D.getVFS().exists(SysRoot + "/lib/mips-linux-gnu")) 95 return "mips-linux-gnu"; 96 break; 97 case llvm::Triple::mipsel: 98 if (D.getVFS().exists(SysRoot + "/lib/mipsel-linux-gnu")) 99 return "mipsel-linux-gnu"; 100 break; 101 case llvm::Triple::mips64: 102 if (D.getVFS().exists(SysRoot + "/lib/mips64-linux-gnu")) 103 return "mips64-linux-gnu"; 104 if (D.getVFS().exists(SysRoot + "/lib/mips64-linux-gnuabi64")) 105 return "mips64-linux-gnuabi64"; 106 break; 107 case llvm::Triple::mips64el: 108 if (D.getVFS().exists(SysRoot + "/lib/mips64el-linux-gnu")) 109 return "mips64el-linux-gnu"; 110 if (D.getVFS().exists(SysRoot + "/lib/mips64el-linux-gnuabi64")) 111 return "mips64el-linux-gnuabi64"; 112 break; 113 case llvm::Triple::ppc: 114 if (D.getVFS().exists(SysRoot + "/lib/powerpc-linux-gnuspe")) 115 return "powerpc-linux-gnuspe"; 116 if (D.getVFS().exists(SysRoot + "/lib/powerpc-linux-gnu")) 117 return "powerpc-linux-gnu"; 118 break; 119 case llvm::Triple::ppc64: 120 if (D.getVFS().exists(SysRoot + "/lib/powerpc64-linux-gnu")) 121 return "powerpc64-linux-gnu"; 122 break; 123 case llvm::Triple::ppc64le: 124 if (D.getVFS().exists(SysRoot + "/lib/powerpc64le-linux-gnu")) 125 return "powerpc64le-linux-gnu"; 126 break; 127 case llvm::Triple::sparc: 128 if (D.getVFS().exists(SysRoot + "/lib/sparc-linux-gnu")) 129 return "sparc-linux-gnu"; 130 break; 131 case llvm::Triple::sparcv9: 132 if (D.getVFS().exists(SysRoot + "/lib/sparc64-linux-gnu")) 133 return "sparc64-linux-gnu"; 134 break; 135 case llvm::Triple::systemz: 136 if (D.getVFS().exists(SysRoot + "/lib/s390x-linux-gnu")) 137 return "s390x-linux-gnu"; 138 break; 139 } 140 return TargetTriple.str(); 141 } 142 143 static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) { 144 if (tools::isMipsArch(Triple.getArch())) { 145 if (Triple.isAndroid()) { 146 StringRef CPUName; 147 StringRef ABIName; 148 tools::mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName); 149 if (CPUName == "mips32r6") 150 return "libr6"; 151 if (CPUName == "mips32r2") 152 return "libr2"; 153 } 154 // lib32 directory has a special meaning on MIPS targets. 155 // It contains N32 ABI binaries. Use this folder if produce 156 // code for N32 ABI only. 157 if (tools::mips::hasMipsAbiArg(Args, "n32")) 158 return "lib32"; 159 return Triple.isArch32Bit() ? "lib" : "lib64"; 160 } 161 162 // It happens that only x86 and PPC use the 'lib32' variant of oslibdir, and 163 // using that variant while targeting other architectures causes problems 164 // because the libraries are laid out in shared system roots that can't cope 165 // with a 'lib32' library search path being considered. So we only enable 166 // them when we know we may need it. 167 // 168 // FIXME: This is a bit of a hack. We should really unify this code for 169 // reasoning about oslibdir spellings with the lib dir spellings in the 170 // GCCInstallationDetector, but that is a more significant refactoring. 171 if (Triple.getArch() == llvm::Triple::x86 || 172 Triple.getArch() == llvm::Triple::ppc) 173 return "lib32"; 174 175 if (Triple.getArch() == llvm::Triple::x86_64 && 176 Triple.getEnvironment() == llvm::Triple::GNUX32) 177 return "libx32"; 178 179 return Triple.isArch32Bit() ? "lib" : "lib64"; 180 } 181 182 static void addMultilibsFilePaths(const Driver &D, const MultilibSet &Multilibs, 183 const Multilib &Multilib, 184 StringRef InstallPath, 185 ToolChain::path_list &Paths) { 186 if (const auto &PathsCallback = Multilibs.filePathsCallback()) 187 for (const auto &Path : PathsCallback(Multilib)) 188 addPathIfExists(D, InstallPath + Path, Paths); 189 } 190 191 Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) 192 : Generic_ELF(D, Triple, Args) { 193 GCCInstallation.init(Triple, Args); 194 Multilibs = GCCInstallation.getMultilibs(); 195 llvm::Triple::ArchType Arch = Triple.getArch(); 196 std::string SysRoot = computeSysRoot(); 197 198 // Cross-compiling binutils and GCC installations (vanilla and openSUSE at 199 // least) put various tools in a triple-prefixed directory off of the parent 200 // of the GCC installation. We use the GCC triple here to ensure that we end 201 // up with tools that support the same amount of cross compiling as the 202 // detected GCC installation. For example, if we find a GCC installation 203 // targeting x86_64, but it is a bi-arch GCC installation, it can also be 204 // used to target i386. 205 // FIXME: This seems unlikely to be Linux-specific. 206 ToolChain::path_list &PPaths = getProgramPaths(); 207 PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" + 208 GCCInstallation.getTriple().str() + "/bin") 209 .str()); 210 211 Distro Distro(D.getVFS()); 212 213 if (Distro.IsOpenSUSE() || Distro.IsUbuntu()) { 214 ExtraOpts.push_back("-z"); 215 ExtraOpts.push_back("relro"); 216 } 217 218 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb) 219 ExtraOpts.push_back("-X"); 220 221 const bool IsAndroid = Triple.isAndroid(); 222 const bool IsMips = tools::isMipsArch(Arch); 223 const bool IsHexagon = Arch == llvm::Triple::hexagon; 224 225 if (IsMips && !SysRoot.empty()) 226 ExtraOpts.push_back("--sysroot=" + SysRoot); 227 228 // Do not use 'gnu' hash style for Mips targets because .gnu.hash 229 // and the MIPS ABI require .dynsym to be sorted in different ways. 230 // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS 231 // ABI requires a mapping between the GOT and the symbol table. 232 // Android loader does not support .gnu.hash. 233 // Hexagon linker/loader does not support .gnu.hash 234 if (!IsMips && !IsAndroid && !IsHexagon) { 235 if (Distro.IsRedhat() || Distro.IsOpenSUSE() || 236 (Distro.IsUbuntu() && Distro >= Distro::UbuntuMaverick)) 237 ExtraOpts.push_back("--hash-style=gnu"); 238 239 if (Distro.IsDebian() || Distro.IsOpenSUSE() || Distro == Distro::UbuntuLucid || 240 Distro == Distro::UbuntuJaunty || Distro == Distro::UbuntuKarmic) 241 ExtraOpts.push_back("--hash-style=both"); 242 } 243 244 if (Distro.IsRedhat() && Distro != Distro::RHEL5 && Distro != Distro::RHEL6) 245 ExtraOpts.push_back("--no-add-needed"); 246 247 #ifdef ENABLE_LINKER_BUILD_ID 248 ExtraOpts.push_back("--build-id"); 249 #endif 250 251 if (Distro.IsOpenSUSE()) 252 ExtraOpts.push_back("--enable-new-dtags"); 253 254 // The selection of paths to try here is designed to match the patterns which 255 // the GCC driver itself uses, as this is part of the GCC-compatible driver. 256 // This was determined by running GCC in a fake filesystem, creating all 257 // possible permutations of these directories, and seeing which ones it added 258 // to the link paths. 259 path_list &Paths = getFilePaths(); 260 261 const std::string OSLibDir = getOSLibDir(Triple, Args); 262 const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot); 263 264 // Add the multilib suffixed paths where they are available. 265 if (GCCInstallation.isValid()) { 266 const llvm::Triple &GCCTriple = GCCInstallation.getTriple(); 267 const std::string &LibPath = GCCInstallation.getParentLibPath(); 268 const Multilib &Multilib = GCCInstallation.getMultilib(); 269 const MultilibSet &Multilibs = GCCInstallation.getMultilibs(); 270 271 // Add toolchain / multilib specific file paths. 272 addMultilibsFilePaths(D, Multilibs, Multilib, 273 GCCInstallation.getInstallPath(), Paths); 274 275 // Sourcery CodeBench MIPS toolchain holds some libraries under 276 // a biarch-like suffix of the GCC installation. 277 addPathIfExists(D, GCCInstallation.getInstallPath() + Multilib.gccSuffix(), 278 Paths); 279 280 // GCC cross compiling toolchains will install target libraries which ship 281 // as part of the toolchain under <prefix>/<triple>/<libdir> rather than as 282 // any part of the GCC installation in 283 // <prefix>/<libdir>/gcc/<triple>/<version>. This decision is somewhat 284 // debatable, but is the reality today. We need to search this tree even 285 // when we have a sysroot somewhere else. It is the responsibility of 286 // whomever is doing the cross build targeting a sysroot using a GCC 287 // installation that is *not* within the system root to ensure two things: 288 // 289 // 1) Any DSOs that are linked in from this tree or from the install path 290 // above must be present on the system root and found via an 291 // appropriate rpath. 292 // 2) There must not be libraries installed into 293 // <prefix>/<triple>/<libdir> unless they should be preferred over 294 // those within the system root. 295 // 296 // Note that this matches the GCC behavior. See the below comment for where 297 // Clang diverges from GCC's behavior. 298 addPathIfExists(D, LibPath + "/../" + GCCTriple.str() + "/lib/../" + 299 OSLibDir + Multilib.osSuffix(), 300 Paths); 301 302 // If the GCC installation we found is inside of the sysroot, we want to 303 // prefer libraries installed in the parent prefix of the GCC installation. 304 // It is important to *not* use these paths when the GCC installation is 305 // outside of the system root as that can pick up unintended libraries. 306 // This usually happens when there is an external cross compiler on the 307 // host system, and a more minimal sysroot available that is the target of 308 // the cross. Note that GCC does include some of these directories in some 309 // configurations but this seems somewhere between questionable and simply 310 // a bug. 311 if (StringRef(LibPath).startswith(SysRoot)) { 312 addPathIfExists(D, LibPath + "/" + MultiarchTriple, Paths); 313 addPathIfExists(D, LibPath + "/../" + OSLibDir, Paths); 314 } 315 } 316 317 // Similar to the logic for GCC above, if we currently running Clang inside 318 // of the requested system root, add its parent library paths to 319 // those searched. 320 // FIXME: It's not clear whether we should use the driver's installed 321 // directory ('Dir' below) or the ResourceDir. 322 if (StringRef(D.Dir).startswith(SysRoot)) { 323 addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths); 324 addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths); 325 } 326 327 addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths); 328 addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths); 329 addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths); 330 addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths); 331 332 // Try walking via the GCC triple path in case of biarch or multiarch GCC 333 // installations with strange symlinks. 334 if (GCCInstallation.isValid()) { 335 addPathIfExists(D, 336 SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() + 337 "/../../" + OSLibDir, 338 Paths); 339 340 // Add the 'other' biarch variant path 341 Multilib BiarchSibling; 342 if (GCCInstallation.getBiarchSibling(BiarchSibling)) { 343 addPathIfExists(D, GCCInstallation.getInstallPath() + 344 BiarchSibling.gccSuffix(), 345 Paths); 346 } 347 348 // See comments above on the multilib variant for details of why this is 349 // included even from outside the sysroot. 350 const std::string &LibPath = GCCInstallation.getParentLibPath(); 351 const llvm::Triple &GCCTriple = GCCInstallation.getTriple(); 352 const Multilib &Multilib = GCCInstallation.getMultilib(); 353 addPathIfExists(D, LibPath + "/../" + GCCTriple.str() + "/lib" + 354 Multilib.osSuffix(), 355 Paths); 356 357 // See comments above on the multilib variant for details of why this is 358 // only included from within the sysroot. 359 if (StringRef(LibPath).startswith(SysRoot)) 360 addPathIfExists(D, LibPath, Paths); 361 } 362 363 // Similar to the logic for GCC above, if we are currently running Clang 364 // inside of the requested system root, add its parent library path to those 365 // searched. 366 // FIXME: It's not clear whether we should use the driver's installed 367 // directory ('Dir' below) or the ResourceDir. 368 if (StringRef(D.Dir).startswith(SysRoot)) 369 addPathIfExists(D, D.Dir + "/../lib", Paths); 370 371 addPathIfExists(D, SysRoot + "/lib", Paths); 372 addPathIfExists(D, SysRoot + "/usr/lib", Paths); 373 } 374 375 /// \brief Parse a GCCVersion object out of a string of text. 376 /// 377 /// This is the primary means of forming GCCVersion objects. 378 /*static*/ 379 Generic_GCC::GCCVersion Linux::GCCVersion::Parse(StringRef VersionText) { 380 const GCCVersion BadVersion = {VersionText.str(), -1, -1, -1, "", "", ""}; 381 std::pair<StringRef, StringRef> First = VersionText.split('.'); 382 std::pair<StringRef, StringRef> Second = First.second.split('.'); 383 384 GCCVersion GoodVersion = {VersionText.str(), -1, -1, -1, "", "", ""}; 385 if (First.first.getAsInteger(10, GoodVersion.Major) || GoodVersion.Major < 0) 386 return BadVersion; 387 GoodVersion.MajorStr = First.first.str(); 388 if (First.second.empty()) 389 return GoodVersion; 390 if (Second.first.getAsInteger(10, GoodVersion.Minor) || GoodVersion.Minor < 0) 391 return BadVersion; 392 GoodVersion.MinorStr = Second.first.str(); 393 394 // First look for a number prefix and parse that if present. Otherwise just 395 // stash the entire patch string in the suffix, and leave the number 396 // unspecified. This covers versions strings such as: 397 // 5 (handled above) 398 // 4.4 399 // 4.4.0 400 // 4.4.x 401 // 4.4.2-rc4 402 // 4.4.x-patched 403 // And retains any patch number it finds. 404 StringRef PatchText = GoodVersion.PatchSuffix = Second.second.str(); 405 if (!PatchText.empty()) { 406 if (size_t EndNumber = PatchText.find_first_not_of("0123456789")) { 407 // Try to parse the number and any suffix. 408 if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) || 409 GoodVersion.Patch < 0) 410 return BadVersion; 411 GoodVersion.PatchSuffix = PatchText.substr(EndNumber); 412 } 413 } 414 415 return GoodVersion; 416 } 417 418 bool Linux::HasNativeLLVMSupport() const { return true; } 419 420 Tool *Linux::buildLinker() const { return new tools::gnutools::Linker(*this); } 421 422 Tool *Linux::buildAssembler() const { 423 return new tools::gnutools::Assembler(*this); 424 } 425 426 std::string Linux::computeSysRoot() const { 427 if (!getDriver().SysRoot.empty()) 428 return getDriver().SysRoot; 429 430 if (!GCCInstallation.isValid() || !tools::isMipsArch(getTriple().getArch())) 431 return std::string(); 432 433 // Standalone MIPS toolchains use different names for sysroot folder 434 // and put it into different places. Here we try to check some known 435 // variants. 436 437 const StringRef InstallDir = GCCInstallation.getInstallPath(); 438 const StringRef TripleStr = GCCInstallation.getTriple().str(); 439 const Multilib &Multilib = GCCInstallation.getMultilib(); 440 441 std::string Path = 442 (InstallDir + "/../../../../" + TripleStr + "/libc" + Multilib.osSuffix()) 443 .str(); 444 445 if (getVFS().exists(Path)) 446 return Path; 447 448 Path = (InstallDir + "/../../../../sysroot" + Multilib.osSuffix()).str(); 449 450 if (getVFS().exists(Path)) 451 return Path; 452 453 return std::string(); 454 } 455 456 std::string Linux::getDynamicLinker(const ArgList &Args) const { 457 const llvm::Triple::ArchType Arch = getArch(); 458 const llvm::Triple &Triple = getTriple(); 459 460 const Distro Distro(getDriver().getVFS()); 461 462 if (Triple.isAndroid()) 463 return Triple.isArch64Bit() ? "/system/bin/linker64" : "/system/bin/linker"; 464 465 if (Triple.isMusl()) { 466 std::string ArchName; 467 bool IsArm = false; 468 469 switch (Arch) { 470 case llvm::Triple::arm: 471 case llvm::Triple::thumb: 472 ArchName = "arm"; 473 IsArm = true; 474 break; 475 case llvm::Triple::armeb: 476 case llvm::Triple::thumbeb: 477 ArchName = "armeb"; 478 IsArm = true; 479 break; 480 default: 481 ArchName = Triple.getArchName().str(); 482 } 483 if (IsArm && 484 (Triple.getEnvironment() == llvm::Triple::MuslEABIHF || 485 tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard)) 486 ArchName += "hf"; 487 488 return "/lib/ld-musl-" + ArchName + ".so.1"; 489 } 490 491 std::string LibDir; 492 std::string Loader; 493 494 switch (Arch) { 495 default: 496 llvm_unreachable("unsupported architecture"); 497 498 case llvm::Triple::aarch64: 499 LibDir = "lib"; 500 Loader = "ld-linux-aarch64.so.1"; 501 break; 502 case llvm::Triple::aarch64_be: 503 LibDir = "lib"; 504 Loader = "ld-linux-aarch64_be.so.1"; 505 break; 506 case llvm::Triple::arm: 507 case llvm::Triple::thumb: 508 case llvm::Triple::armeb: 509 case llvm::Triple::thumbeb: { 510 const bool HF = 511 Triple.getEnvironment() == llvm::Triple::GNUEABIHF || 512 tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard; 513 514 LibDir = "lib"; 515 Loader = HF ? "ld-linux-armhf.so.3" : "ld-linux.so.3"; 516 break; 517 } 518 case llvm::Triple::mips: 519 case llvm::Triple::mipsel: 520 case llvm::Triple::mips64: 521 case llvm::Triple::mips64el: { 522 bool LE = (Triple.getArch() == llvm::Triple::mipsel) || 523 (Triple.getArch() == llvm::Triple::mips64el); 524 bool IsNaN2008 = tools::mips::isNaN2008(Args, Triple); 525 526 LibDir = "lib" + tools::mips::getMipsABILibSuffix(Args, Triple); 527 528 if (tools::mips::isUCLibc(Args)) 529 Loader = IsNaN2008 ? "ld-uClibc-mipsn8.so.0" : "ld-uClibc.so.0"; 530 else if (!Triple.hasEnvironment() && 531 Triple.getVendor() == llvm::Triple::VendorType::MipsTechnologies) 532 Loader = LE ? "ld-musl-mipsel.so.1" : "ld-musl-mips.so.1"; 533 else 534 Loader = IsNaN2008 ? "ld-linux-mipsn8.so.1" : "ld.so.1"; 535 536 break; 537 } 538 case llvm::Triple::ppc: 539 LibDir = "lib"; 540 Loader = "ld.so.1"; 541 break; 542 case llvm::Triple::ppc64: 543 LibDir = "lib64"; 544 Loader = 545 (tools::ppc::hasPPCAbiArg(Args, "elfv2")) ? "ld64.so.2" : "ld64.so.1"; 546 break; 547 case llvm::Triple::ppc64le: 548 LibDir = "lib64"; 549 Loader = 550 (tools::ppc::hasPPCAbiArg(Args, "elfv1")) ? "ld64.so.1" : "ld64.so.2"; 551 break; 552 case llvm::Triple::sparc: 553 case llvm::Triple::sparcel: 554 LibDir = "lib"; 555 Loader = "ld-linux.so.2"; 556 break; 557 case llvm::Triple::sparcv9: 558 LibDir = "lib64"; 559 Loader = "ld-linux.so.2"; 560 break; 561 case llvm::Triple::systemz: 562 LibDir = "lib"; 563 Loader = "ld64.so.1"; 564 break; 565 case llvm::Triple::x86: 566 LibDir = "lib"; 567 Loader = "ld-linux.so.2"; 568 break; 569 case llvm::Triple::x86_64: { 570 bool X32 = Triple.getEnvironment() == llvm::Triple::GNUX32; 571 572 LibDir = X32 ? "libx32" : "lib64"; 573 Loader = X32 ? "ld-linux-x32.so.2" : "ld-linux-x86-64.so.2"; 574 break; 575 } 576 } 577 578 if (Distro == Distro::Exherbo && (Triple.getVendor() == llvm::Triple::UnknownVendor || 579 Triple.getVendor() == llvm::Triple::PC)) 580 return "/usr/" + Triple.str() + "/lib/" + Loader; 581 return "/" + LibDir + "/" + Loader; 582 } 583 584 void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 585 ArgStringList &CC1Args) const { 586 const Driver &D = getDriver(); 587 std::string SysRoot = computeSysRoot(); 588 589 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)) 590 return; 591 592 if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) 593 addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include"); 594 595 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 596 SmallString<128> P(D.ResourceDir); 597 llvm::sys::path::append(P, "include"); 598 addSystemInclude(DriverArgs, CC1Args, P); 599 } 600 601 if (DriverArgs.hasArg(options::OPT_nostdlibinc)) 602 return; 603 604 // Check for configure-time C include directories. 605 StringRef CIncludeDirs(C_INCLUDE_DIRS); 606 if (CIncludeDirs != "") { 607 SmallVector<StringRef, 5> dirs; 608 CIncludeDirs.split(dirs, ":"); 609 for (StringRef dir : dirs) { 610 StringRef Prefix = 611 llvm::sys::path::is_absolute(dir) ? StringRef(SysRoot) : ""; 612 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir); 613 } 614 return; 615 } 616 617 // Lacking those, try to detect the correct set of system includes for the 618 // target triple. 619 620 // Add include directories specific to the selected multilib set and multilib. 621 if (GCCInstallation.isValid()) { 622 const auto &Callback = Multilibs.includeDirsCallback(); 623 if (Callback) { 624 for (const auto &Path : Callback(GCCInstallation.getMultilib())) 625 addExternCSystemIncludeIfExists( 626 DriverArgs, CC1Args, GCCInstallation.getInstallPath() + Path); 627 } 628 } 629 630 // Implement generic Debian multiarch support. 631 const StringRef X86_64MultiarchIncludeDirs[] = { 632 "/usr/include/x86_64-linux-gnu", 633 634 // FIXME: These are older forms of multiarch. It's not clear that they're 635 // in use in any released version of Debian, so we should consider 636 // removing them. 637 "/usr/include/i686-linux-gnu/64", "/usr/include/i486-linux-gnu/64"}; 638 const StringRef X86MultiarchIncludeDirs[] = { 639 "/usr/include/i386-linux-gnu", 640 641 // FIXME: These are older forms of multiarch. It's not clear that they're 642 // in use in any released version of Debian, so we should consider 643 // removing them. 644 "/usr/include/x86_64-linux-gnu/32", "/usr/include/i686-linux-gnu", 645 "/usr/include/i486-linux-gnu"}; 646 const StringRef AArch64MultiarchIncludeDirs[] = { 647 "/usr/include/aarch64-linux-gnu"}; 648 const StringRef ARMMultiarchIncludeDirs[] = { 649 "/usr/include/arm-linux-gnueabi"}; 650 const StringRef ARMHFMultiarchIncludeDirs[] = { 651 "/usr/include/arm-linux-gnueabihf"}; 652 const StringRef ARMEBMultiarchIncludeDirs[] = { 653 "/usr/include/armeb-linux-gnueabi"}; 654 const StringRef ARMEBHFMultiarchIncludeDirs[] = { 655 "/usr/include/armeb-linux-gnueabihf"}; 656 const StringRef MIPSMultiarchIncludeDirs[] = {"/usr/include/mips-linux-gnu"}; 657 const StringRef MIPSELMultiarchIncludeDirs[] = { 658 "/usr/include/mipsel-linux-gnu"}; 659 const StringRef MIPS64MultiarchIncludeDirs[] = { 660 "/usr/include/mips64-linux-gnu", "/usr/include/mips64-linux-gnuabi64"}; 661 const StringRef MIPS64ELMultiarchIncludeDirs[] = { 662 "/usr/include/mips64el-linux-gnu", 663 "/usr/include/mips64el-linux-gnuabi64"}; 664 const StringRef PPCMultiarchIncludeDirs[] = { 665 "/usr/include/powerpc-linux-gnu"}; 666 const StringRef PPC64MultiarchIncludeDirs[] = { 667 "/usr/include/powerpc64-linux-gnu"}; 668 const StringRef PPC64LEMultiarchIncludeDirs[] = { 669 "/usr/include/powerpc64le-linux-gnu"}; 670 const StringRef SparcMultiarchIncludeDirs[] = { 671 "/usr/include/sparc-linux-gnu"}; 672 const StringRef Sparc64MultiarchIncludeDirs[] = { 673 "/usr/include/sparc64-linux-gnu"}; 674 const StringRef SYSTEMZMultiarchIncludeDirs[] = { 675 "/usr/include/s390x-linux-gnu"}; 676 ArrayRef<StringRef> MultiarchIncludeDirs; 677 switch (getTriple().getArch()) { 678 case llvm::Triple::x86_64: 679 MultiarchIncludeDirs = X86_64MultiarchIncludeDirs; 680 break; 681 case llvm::Triple::x86: 682 MultiarchIncludeDirs = X86MultiarchIncludeDirs; 683 break; 684 case llvm::Triple::aarch64: 685 case llvm::Triple::aarch64_be: 686 MultiarchIncludeDirs = AArch64MultiarchIncludeDirs; 687 break; 688 case llvm::Triple::arm: 689 case llvm::Triple::thumb: 690 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF) 691 MultiarchIncludeDirs = ARMHFMultiarchIncludeDirs; 692 else 693 MultiarchIncludeDirs = ARMMultiarchIncludeDirs; 694 break; 695 case llvm::Triple::armeb: 696 case llvm::Triple::thumbeb: 697 if (getTriple().getEnvironment() == llvm::Triple::GNUEABIHF) 698 MultiarchIncludeDirs = ARMEBHFMultiarchIncludeDirs; 699 else 700 MultiarchIncludeDirs = ARMEBMultiarchIncludeDirs; 701 break; 702 case llvm::Triple::mips: 703 MultiarchIncludeDirs = MIPSMultiarchIncludeDirs; 704 break; 705 case llvm::Triple::mipsel: 706 MultiarchIncludeDirs = MIPSELMultiarchIncludeDirs; 707 break; 708 case llvm::Triple::mips64: 709 MultiarchIncludeDirs = MIPS64MultiarchIncludeDirs; 710 break; 711 case llvm::Triple::mips64el: 712 MultiarchIncludeDirs = MIPS64ELMultiarchIncludeDirs; 713 break; 714 case llvm::Triple::ppc: 715 MultiarchIncludeDirs = PPCMultiarchIncludeDirs; 716 break; 717 case llvm::Triple::ppc64: 718 MultiarchIncludeDirs = PPC64MultiarchIncludeDirs; 719 break; 720 case llvm::Triple::ppc64le: 721 MultiarchIncludeDirs = PPC64LEMultiarchIncludeDirs; 722 break; 723 case llvm::Triple::sparc: 724 MultiarchIncludeDirs = SparcMultiarchIncludeDirs; 725 break; 726 case llvm::Triple::sparcv9: 727 MultiarchIncludeDirs = Sparc64MultiarchIncludeDirs; 728 break; 729 case llvm::Triple::systemz: 730 MultiarchIncludeDirs = SYSTEMZMultiarchIncludeDirs; 731 break; 732 default: 733 break; 734 } 735 for (StringRef Dir : MultiarchIncludeDirs) { 736 if (D.getVFS().exists(SysRoot + Dir)) { 737 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + Dir); 738 break; 739 } 740 } 741 742 if (getTriple().getOS() == llvm::Triple::RTEMS) 743 return; 744 745 // Add an include of '/include' directly. This isn't provided by default by 746 // system GCCs, but is often used with cross-compiling GCCs, and harmless to 747 // add even when Clang is acting as-if it were a system compiler. 748 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include"); 749 750 addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include"); 751 } 752 753 static std::string DetectLibcxxIncludePath(StringRef base) { 754 std::error_code EC; 755 int MaxVersion = 0; 756 std::string MaxVersionString = ""; 757 for (llvm::sys::fs::directory_iterator LI(base, EC), LE; !EC && LI != LE; 758 LI = LI.increment(EC)) { 759 StringRef VersionText = llvm::sys::path::filename(LI->path()); 760 int Version; 761 if (VersionText[0] == 'v' && 762 !VersionText.slice(1, StringRef::npos).getAsInteger(10, Version)) { 763 if (Version > MaxVersion) { 764 MaxVersion = Version; 765 MaxVersionString = VersionText; 766 } 767 } 768 } 769 return MaxVersion ? (base + "/" + MaxVersionString).str() : ""; 770 } 771 772 std::string Linux::findLibCxxIncludePath() const { 773 const std::string LibCXXIncludePathCandidates[] = { 774 DetectLibcxxIncludePath(getDriver().Dir + "/../include/c++"), 775 // If this is a development, non-installed, clang, libcxx will 776 // not be found at ../include/c++ but it likely to be found at 777 // one of the following two locations: 778 DetectLibcxxIncludePath(getDriver().SysRoot + "/usr/local/include/c++"), 779 DetectLibcxxIncludePath(getDriver().SysRoot + "/usr/include/c++") }; 780 for (const auto &IncludePath : LibCXXIncludePathCandidates) { 781 if (IncludePath.empty() || !getVFS().exists(IncludePath)) 782 continue; 783 // Use the first candidate that exists. 784 return IncludePath; 785 } 786 return ""; 787 } 788 789 void Linux::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, 790 llvm::opt::ArgStringList &CC1Args) const { 791 // We need a detected GCC installation on Linux to provide libstdc++'s 792 // headers. 793 if (!GCCInstallation.isValid()) 794 return; 795 796 // By default, look for the C++ headers in an include directory adjacent to 797 // the lib directory of the GCC installation. Note that this is expect to be 798 // equivalent to '/usr/include/c++/X.Y' in almost all cases. 799 StringRef LibDir = GCCInstallation.getParentLibPath(); 800 StringRef InstallDir = GCCInstallation.getInstallPath(); 801 StringRef TripleStr = GCCInstallation.getTriple().str(); 802 const Multilib &Multilib = GCCInstallation.getMultilib(); 803 const std::string GCCMultiarchTriple = getMultiarchTriple( 804 getDriver(), GCCInstallation.getTriple(), getDriver().SysRoot); 805 const std::string TargetMultiarchTriple = 806 getMultiarchTriple(getDriver(), getTriple(), getDriver().SysRoot); 807 const GCCVersion &Version = GCCInstallation.getVersion(); 808 809 // The primary search for libstdc++ supports multiarch variants. 810 if (addLibStdCXXIncludePaths(LibDir.str() + "/../include", 811 "/c++/" + Version.Text, TripleStr, 812 GCCMultiarchTriple, TargetMultiarchTriple, 813 Multilib.includeSuffix(), DriverArgs, CC1Args)) 814 return; 815 816 // Otherwise, fall back on a bunch of options which don't use multiarch 817 // layouts for simplicity. 818 const std::string LibStdCXXIncludePathCandidates[] = { 819 // Gentoo is weird and places its headers inside the GCC install, 820 // so if the first attempt to find the headers fails, try these patterns. 821 InstallDir.str() + "/include/g++-v" + Version.Text, 822 InstallDir.str() + "/include/g++-v" + Version.MajorStr + "." + 823 Version.MinorStr, 824 InstallDir.str() + "/include/g++-v" + Version.MajorStr, 825 // Android standalone toolchain has C++ headers in yet another place. 826 LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text, 827 // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++, 828 // without a subdirectory corresponding to the gcc version. 829 LibDir.str() + "/../include/c++", 830 }; 831 832 for (const auto &IncludePath : LibStdCXXIncludePathCandidates) { 833 if (addLibStdCXXIncludePaths(IncludePath, /*Suffix*/ "", TripleStr, 834 /*GCCMultiarchTriple*/ "", 835 /*TargetMultiarchTriple*/ "", 836 Multilib.includeSuffix(), DriverArgs, CC1Args)) 837 break; 838 } 839 } 840 841 void Linux::AddCudaIncludeArgs(const ArgList &DriverArgs, 842 ArgStringList &CC1Args) const { 843 CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args); 844 } 845 846 void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs, 847 ArgStringList &CC1Args) const { 848 if (GCCInstallation.isValid()) { 849 CC1Args.push_back("-isystem"); 850 CC1Args.push_back(DriverArgs.MakeArgString( 851 GCCInstallation.getParentLibPath() + "/../" + 852 GCCInstallation.getTriple().str() + "/include")); 853 } 854 } 855 856 bool Linux::isPIEDefault() const { return getSanitizerArgs().requiresPIE(); } 857 858 SanitizerMask Linux::getSupportedSanitizers() const { 859 const bool IsX86 = getTriple().getArch() == llvm::Triple::x86; 860 const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64; 861 const bool IsMIPS64 = getTriple().getArch() == llvm::Triple::mips64 || 862 getTriple().getArch() == llvm::Triple::mips64el; 863 const bool IsPowerPC64 = getTriple().getArch() == llvm::Triple::ppc64 || 864 getTriple().getArch() == llvm::Triple::ppc64le; 865 const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64 || 866 getTriple().getArch() == llvm::Triple::aarch64_be; 867 const bool IsArmArch = getTriple().getArch() == llvm::Triple::arm || 868 llvm::Triple::thumb || llvm::Triple::armeb || 869 llvm::Triple::thumbeb; 870 SanitizerMask Res = ToolChain::getSupportedSanitizers(); 871 Res |= SanitizerKind::Address; 872 Res |= SanitizerKind::KernelAddress; 873 Res |= SanitizerKind::Vptr; 874 Res |= SanitizerKind::SafeStack; 875 if (IsX86_64 || IsMIPS64 || IsAArch64) 876 Res |= SanitizerKind::DataFlow; 877 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsArmArch) 878 Res |= SanitizerKind::Leak; 879 if (IsX86_64 || IsMIPS64 || IsAArch64 || IsPowerPC64) 880 Res |= SanitizerKind::Thread; 881 if (IsX86_64 || IsMIPS64 || IsPowerPC64 || IsAArch64) 882 Res |= SanitizerKind::Memory; 883 if (IsX86_64 || IsMIPS64) 884 Res |= SanitizerKind::Efficiency; 885 if (IsX86 || IsX86_64) { 886 Res |= SanitizerKind::Function; 887 } 888 return Res; 889 } 890 891 void Linux::addProfileRTLibs(const llvm::opt::ArgList &Args, 892 llvm::opt::ArgStringList &CmdArgs) const { 893 if (!needsProfileRT(Args)) return; 894 895 // Add linker option -u__llvm_runtime_variable to cause runtime 896 // initialization module to be linked in. 897 if (!Args.hasArg(options::OPT_coverage)) 898 CmdArgs.push_back(Args.MakeArgString( 899 Twine("-u", llvm::getInstrProfRuntimeHookVarName()))); 900 ToolChain::addProfileRTLibs(Args, CmdArgs); 901 } 902