Linux.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. //===--- Linux.h - Linux ToolChain Implementations --------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "Linux.h"
  9. #include "Arch/ARM.h"
  10. #include "Arch/Mips.h"
  11. #include "Arch/PPC.h"
  12. #include "Arch/RISCV.h"
  13. #include "CommonArgs.h"
  14. #include "clang/Config/config.h"
  15. #include "clang/Driver/Distro.h"
  16. #include "clang/Driver/Driver.h"
  17. #include "clang/Driver/Options.h"
  18. #include "clang/Driver/SanitizerArgs.h"
  19. #include "llvm/Option/ArgList.h"
  20. #include "llvm/ProfileData/InstrProf.h"
  21. #include "llvm/Support/Path.h"
  22. #include "llvm/Support/ScopedPrinter.h"
  23. #include "llvm/Support/VirtualFileSystem.h"
  24. #include <system_error>
  25. using namespace clang::driver;
  26. using namespace clang::driver::toolchains;
  27. using namespace clang;
  28. using namespace llvm::opt;
  29. using tools::addPathIfExists;
  30. /// Get our best guess at the multiarch triple for a target.
  31. ///
  32. /// Debian-based systems are starting to use a multiarch setup where they use
  33. /// a target-triple directory in the library and header search paths.
  34. /// Unfortunately, this triple does not align with the vanilla target triple,
  35. /// so we provide a rough mapping here.
  36. std::string Linux::getMultiarchTriple(const Driver &D,
  37. const llvm::Triple &TargetTriple,
  38. StringRef SysRoot) const {
  39. llvm::Triple::EnvironmentType TargetEnvironment =
  40. TargetTriple.getEnvironment();
  41. bool IsAndroid = TargetTriple.isAndroid();
  42. bool IsMipsR6 = TargetTriple.getSubArch() == llvm::Triple::MipsSubArch_r6;
  43. bool IsMipsN32Abi = TargetTriple.getEnvironment() == llvm::Triple::GNUABIN32;
  44. // For most architectures, just use whatever we have rather than trying to be
  45. // clever.
  46. switch (TargetTriple.getArch()) {
  47. default:
  48. break;
  49. // We use the existence of '/lib/<triple>' as a directory to detect some
  50. // common linux triples that don't quite match the Clang triple for both
  51. // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
  52. // regardless of what the actual target triple is.
  53. case llvm::Triple::arm:
  54. case llvm::Triple::thumb:
  55. if (IsAndroid)
  56. return "arm-linux-androideabi";
  57. if (TargetEnvironment == llvm::Triple::GNUEABIHF)
  58. return "arm-linux-gnueabihf";
  59. return "arm-linux-gnueabi";
  60. case llvm::Triple::armeb:
  61. case llvm::Triple::thumbeb:
  62. if (TargetEnvironment == llvm::Triple::GNUEABIHF)
  63. return "armeb-linux-gnueabihf";
  64. return "armeb-linux-gnueabi";
  65. case llvm::Triple::x86:
  66. if (IsAndroid)
  67. return "i686-linux-android";
  68. return "i386-linux-gnu";
  69. case llvm::Triple::x86_64:
  70. if (IsAndroid)
  71. return "x86_64-linux-android";
  72. if (TargetEnvironment == llvm::Triple::GNUX32)
  73. return "x86_64-linux-gnux32";
  74. return "x86_64-linux-gnu";
  75. case llvm::Triple::aarch64:
  76. if (IsAndroid)
  77. return "aarch64-linux-android";
  78. return "aarch64-linux-gnu";
  79. case llvm::Triple::aarch64_be:
  80. return "aarch64_be-linux-gnu";
  81. case llvm::Triple::m68k:
  82. return "m68k-linux-gnu";
  83. case llvm::Triple::mips:
  84. return IsMipsR6 ? "mipsisa32r6-linux-gnu" : "mips-linux-gnu";
  85. case llvm::Triple::mipsel:
  86. if (IsAndroid)
  87. return "mipsel-linux-android";
  88. return IsMipsR6 ? "mipsisa32r6el-linux-gnu" : "mipsel-linux-gnu";
  89. case llvm::Triple::mips64: {
  90. std::string MT = std::string(IsMipsR6 ? "mipsisa64r6" : "mips64") +
  91. "-linux-" + (IsMipsN32Abi ? "gnuabin32" : "gnuabi64");
  92. if (D.getVFS().exists(SysRoot + "/lib/" + MT))
  93. return MT;
  94. if (D.getVFS().exists(SysRoot + "/lib/mips64-linux-gnu"))
  95. return "mips64-linux-gnu";
  96. break;
  97. }
  98. case llvm::Triple::mips64el: {
  99. if (IsAndroid)
  100. return "mips64el-linux-android";
  101. std::string MT = std::string(IsMipsR6 ? "mipsisa64r6el" : "mips64el") +
  102. "-linux-" + (IsMipsN32Abi ? "gnuabin32" : "gnuabi64");
  103. if (D.getVFS().exists(SysRoot + "/lib/" + MT))
  104. return MT;
  105. if (D.getVFS().exists(SysRoot + "/lib/mips64el-linux-gnu"))
  106. return "mips64el-linux-gnu";
  107. break;
  108. }
  109. case llvm::Triple::ppc:
  110. if (D.getVFS().exists(SysRoot + "/lib/powerpc-linux-gnuspe"))
  111. return "powerpc-linux-gnuspe";
  112. return "powerpc-linux-gnu";
  113. case llvm::Triple::ppcle:
  114. return "powerpcle-linux-gnu";
  115. case llvm::Triple::ppc64:
  116. return "powerpc64-linux-gnu";
  117. case llvm::Triple::ppc64le:
  118. return "powerpc64le-linux-gnu";
  119. case llvm::Triple::sparc:
  120. return "sparc-linux-gnu";
  121. case llvm::Triple::sparcv9:
  122. return "sparc64-linux-gnu";
  123. case llvm::Triple::systemz:
  124. return "s390x-linux-gnu";
  125. }
  126. return TargetTriple.str();
  127. }
  128. static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) {
  129. if (Triple.isMIPS()) {
  130. if (Triple.isAndroid()) {
  131. StringRef CPUName;
  132. StringRef ABIName;
  133. tools::mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
  134. if (CPUName == "mips32r6")
  135. return "libr6";
  136. if (CPUName == "mips32r2")
  137. return "libr2";
  138. }
  139. // lib32 directory has a special meaning on MIPS targets.
  140. // It contains N32 ABI binaries. Use this folder if produce
  141. // code for N32 ABI only.
  142. if (tools::mips::hasMipsAbiArg(Args, "n32"))
  143. return "lib32";
  144. return Triple.isArch32Bit() ? "lib" : "lib64";
  145. }
  146. // It happens that only x86, PPC and SPARC use the 'lib32' variant of
  147. // oslibdir, and using that variant while targeting other architectures causes
  148. // problems because the libraries are laid out in shared system roots that
  149. // can't cope with a 'lib32' library search path being considered. So we only
  150. // enable them when we know we may need it.
  151. //
  152. // FIXME: This is a bit of a hack. We should really unify this code for
  153. // reasoning about oslibdir spellings with the lib dir spellings in the
  154. // GCCInstallationDetector, but that is a more significant refactoring.
  155. if (Triple.getArch() == llvm::Triple::x86 || Triple.isPPC32() ||
  156. Triple.getArch() == llvm::Triple::sparc)
  157. return "lib32";
  158. if (Triple.getArch() == llvm::Triple::x86_64 && Triple.isX32())
  159. return "libx32";
  160. if (Triple.getArch() == llvm::Triple::riscv32)
  161. return "lib32";
  162. return Triple.isArch32Bit() ? "lib" : "lib64";
  163. }
  164. Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
  165. : Generic_ELF(D, Triple, Args) {
  166. GCCInstallation.init(Triple, Args);
  167. Multilibs = GCCInstallation.getMultilibs();
  168. SelectedMultilib = GCCInstallation.getMultilib();
  169. llvm::Triple::ArchType Arch = Triple.getArch();
  170. std::string SysRoot = computeSysRoot();
  171. ToolChain::path_list &PPaths = getProgramPaths();
  172. Generic_GCC::PushPPaths(PPaths);
  173. Distro Distro(D.getVFS(), Triple);
  174. if (Distro.IsAlpineLinux() || Triple.isAndroid()) {
  175. ExtraOpts.push_back("-z");
  176. ExtraOpts.push_back("now");
  177. }
  178. if (Distro.IsOpenSUSE() || Distro.IsUbuntu() || Distro.IsAlpineLinux() ||
  179. Triple.isAndroid()) {
  180. ExtraOpts.push_back("-z");
  181. ExtraOpts.push_back("relro");
  182. }
  183. // Android ARM/AArch64 use max-page-size=4096 to reduce VMA usage. Note, lld
  184. // from 11 onwards default max-page-size to 65536 for both ARM and AArch64.
  185. if ((Triple.isARM() || Triple.isAArch64()) && Triple.isAndroid()) {
  186. ExtraOpts.push_back("-z");
  187. ExtraOpts.push_back("max-page-size=4096");
  188. }
  189. if (GCCInstallation.getParentLibPath().contains("opt/rh/"))
  190. // With devtoolset on RHEL, we want to add a bin directory that is relative
  191. // to the detected gcc install, because if we are using devtoolset gcc then
  192. // we want to use other tools from devtoolset (e.g. ld) instead of the
  193. // standard system tools.
  194. PPaths.push_back(Twine(GCCInstallation.getParentLibPath() +
  195. "/../bin").str());
  196. if (Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)
  197. ExtraOpts.push_back("-X");
  198. const bool IsAndroid = Triple.isAndroid();
  199. const bool IsMips = Triple.isMIPS();
  200. const bool IsHexagon = Arch == llvm::Triple::hexagon;
  201. const bool IsRISCV = Triple.isRISCV();
  202. if (IsMips && !SysRoot.empty())
  203. ExtraOpts.push_back("--sysroot=" + SysRoot);
  204. // Do not use 'gnu' hash style for Mips targets because .gnu.hash
  205. // and the MIPS ABI require .dynsym to be sorted in different ways.
  206. // .gnu.hash needs symbols to be grouped by hash code whereas the MIPS
  207. // ABI requires a mapping between the GOT and the symbol table.
  208. // Android loader does not support .gnu.hash until API 23.
  209. // Hexagon linker/loader does not support .gnu.hash
  210. if (!IsMips && !IsHexagon) {
  211. if (Distro.IsRedhat() || Distro.IsOpenSUSE() || Distro.IsAlpineLinux() ||
  212. (Distro.IsUbuntu() && Distro >= Distro::UbuntuMaverick) ||
  213. (IsAndroid && !Triple.isAndroidVersionLT(23)))
  214. ExtraOpts.push_back("--hash-style=gnu");
  215. if (Distro.IsDebian() || Distro.IsOpenSUSE() ||
  216. Distro == Distro::UbuntuLucid || Distro == Distro::UbuntuJaunty ||
  217. Distro == Distro::UbuntuKarmic ||
  218. (IsAndroid && Triple.isAndroidVersionLT(23)))
  219. ExtraOpts.push_back("--hash-style=both");
  220. }
  221. #ifdef ENABLE_LINKER_BUILD_ID
  222. ExtraOpts.push_back("--build-id");
  223. #endif
  224. if (IsAndroid || Distro.IsOpenSUSE())
  225. ExtraOpts.push_back("--enable-new-dtags");
  226. // The selection of paths to try here is designed to match the patterns which
  227. // the GCC driver itself uses, as this is part of the GCC-compatible driver.
  228. // This was determined by running GCC in a fake filesystem, creating all
  229. // possible permutations of these directories, and seeing which ones it added
  230. // to the link paths.
  231. path_list &Paths = getFilePaths();
  232. const std::string OSLibDir = std::string(getOSLibDir(Triple, Args));
  233. const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot);
  234. // mips32: Debian multilib, we use /libo32, while in other case, /lib is
  235. // used. We need add both libo32 and /lib.
  236. if (Arch == llvm::Triple::mips || Arch == llvm::Triple::mipsel) {
  237. Generic_GCC::AddMultilibPaths(D, SysRoot, "libo32", MultiarchTriple, Paths);
  238. addPathIfExists(D, SysRoot + "/libo32", Paths);
  239. addPathIfExists(D, SysRoot + "/usr/libo32", Paths);
  240. }
  241. Generic_GCC::AddMultilibPaths(D, SysRoot, OSLibDir, MultiarchTriple, Paths);
  242. addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths);
  243. addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths);
  244. if (IsAndroid) {
  245. // Android sysroots contain a library directory for each supported OS
  246. // version as well as some unversioned libraries in the usual multiarch
  247. // directory.
  248. addPathIfExists(
  249. D,
  250. SysRoot + "/usr/lib/" + MultiarchTriple + "/" +
  251. llvm::to_string(Triple.getEnvironmentVersion().getMajor()),
  252. Paths);
  253. }
  254. addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
  255. // 64-bit OpenEmbedded sysroots may not have a /usr/lib dir. So they cannot
  256. // find /usr/lib64 as it is referenced as /usr/lib/../lib64. So we handle
  257. // this here.
  258. if (Triple.getVendor() == llvm::Triple::OpenEmbedded &&
  259. Triple.isArch64Bit())
  260. addPathIfExists(D, SysRoot + "/usr/" + OSLibDir, Paths);
  261. else
  262. addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths);
  263. if (IsRISCV) {
  264. StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
  265. addPathIfExists(D, SysRoot + "/" + OSLibDir + "/" + ABIName, Paths);
  266. addPathIfExists(D, SysRoot + "/usr/" + OSLibDir + "/" + ABIName, Paths);
  267. }
  268. Generic_GCC::AddMultiarchPaths(D, SysRoot, OSLibDir, Paths);
  269. // The deprecated -DLLVM_ENABLE_PROJECTS=libcxx configuration installs
  270. // libc++.so in D.Dir+"/../lib/". Detect this path.
  271. // TODO Remove once LLVM_ENABLE_PROJECTS=libcxx is unsupported.
  272. if (StringRef(D.Dir).startswith(SysRoot) &&
  273. D.getVFS().exists(D.Dir + "/../lib/libc++.so"))
  274. addPathIfExists(D, D.Dir + "/../lib", Paths);
  275. addPathIfExists(D, SysRoot + "/lib", Paths);
  276. addPathIfExists(D, SysRoot + "/usr/lib", Paths);
  277. }
  278. ToolChain::RuntimeLibType Linux::GetDefaultRuntimeLibType() const {
  279. if (getTriple().isAndroid())
  280. return ToolChain::RLT_CompilerRT;
  281. return Generic_ELF::GetDefaultRuntimeLibType();
  282. }
  283. unsigned Linux::GetDefaultDwarfVersion() const {
  284. if (getTriple().isAndroid())
  285. return 4;
  286. return ToolChain::GetDefaultDwarfVersion();
  287. }
  288. ToolChain::CXXStdlibType Linux::GetDefaultCXXStdlibType() const {
  289. if (getTriple().isAndroid())
  290. return ToolChain::CST_Libcxx;
  291. return ToolChain::CST_Libstdcxx;
  292. }
  293. bool Linux::HasNativeLLVMSupport() const { return true; }
  294. Tool *Linux::buildLinker() const { return new tools::gnutools::Linker(*this); }
  295. Tool *Linux::buildStaticLibTool() const {
  296. return new tools::gnutools::StaticLibTool(*this);
  297. }
  298. Tool *Linux::buildAssembler() const {
  299. return new tools::gnutools::Assembler(*this);
  300. }
  301. std::string Linux::computeSysRoot() const {
  302. if (!getDriver().SysRoot.empty())
  303. return getDriver().SysRoot;
  304. if (getTriple().isAndroid()) {
  305. // Android toolchains typically include a sysroot at ../sysroot relative to
  306. // the clang binary.
  307. const StringRef ClangDir = getDriver().getInstalledDir();
  308. std::string AndroidSysRootPath = (ClangDir + "/../sysroot").str();
  309. if (getVFS().exists(AndroidSysRootPath))
  310. return AndroidSysRootPath;
  311. }
  312. if (!GCCInstallation.isValid() || !getTriple().isMIPS())
  313. return std::string();
  314. // Standalone MIPS toolchains use different names for sysroot folder
  315. // and put it into different places. Here we try to check some known
  316. // variants.
  317. const StringRef InstallDir = GCCInstallation.getInstallPath();
  318. const StringRef TripleStr = GCCInstallation.getTriple().str();
  319. const Multilib &Multilib = GCCInstallation.getMultilib();
  320. std::string Path =
  321. (InstallDir + "/../../../../" + TripleStr + "/libc" + Multilib.osSuffix())
  322. .str();
  323. if (getVFS().exists(Path))
  324. return Path;
  325. Path = (InstallDir + "/../../../../sysroot" + Multilib.osSuffix()).str();
  326. if (getVFS().exists(Path))
  327. return Path;
  328. return std::string();
  329. }
  330. std::string Linux::getDynamicLinker(const ArgList &Args) const {
  331. const llvm::Triple::ArchType Arch = getArch();
  332. const llvm::Triple &Triple = getTriple();
  333. const Distro Distro(getDriver().getVFS(), Triple);
  334. if (Triple.isAndroid())
  335. return Triple.isArch64Bit() ? "/system/bin/linker64" : "/system/bin/linker";
  336. if (Triple.isMusl()) {
  337. std::string ArchName;
  338. bool IsArm = false;
  339. switch (Arch) {
  340. case llvm::Triple::arm:
  341. case llvm::Triple::thumb:
  342. ArchName = "arm";
  343. IsArm = true;
  344. break;
  345. case llvm::Triple::armeb:
  346. case llvm::Triple::thumbeb:
  347. ArchName = "armeb";
  348. IsArm = true;
  349. break;
  350. case llvm::Triple::x86:
  351. ArchName = "i386";
  352. break;
  353. case llvm::Triple::x86_64:
  354. ArchName = Triple.isX32() ? "x32" : Triple.getArchName().str();
  355. break;
  356. default:
  357. ArchName = Triple.getArchName().str();
  358. }
  359. if (IsArm &&
  360. (Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
  361. tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard))
  362. ArchName += "hf";
  363. if (Arch == llvm::Triple::ppc &&
  364. Triple.getSubArch() == llvm::Triple::PPCSubArch_spe)
  365. ArchName = "powerpc-sf";
  366. return "/lib/ld-musl-" + ArchName + ".so.1";
  367. }
  368. std::string LibDir;
  369. std::string Loader;
  370. switch (Arch) {
  371. default:
  372. llvm_unreachable("unsupported architecture");
  373. case llvm::Triple::aarch64:
  374. LibDir = "lib";
  375. Loader = "ld-linux-aarch64.so.1";
  376. break;
  377. case llvm::Triple::aarch64_be:
  378. LibDir = "lib";
  379. Loader = "ld-linux-aarch64_be.so.1";
  380. break;
  381. case llvm::Triple::arm:
  382. case llvm::Triple::thumb:
  383. case llvm::Triple::armeb:
  384. case llvm::Triple::thumbeb: {
  385. const bool HF =
  386. Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
  387. tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard;
  388. LibDir = "lib";
  389. Loader = HF ? "ld-linux-armhf.so.3" : "ld-linux.so.3";
  390. break;
  391. }
  392. case llvm::Triple::m68k:
  393. LibDir = "lib";
  394. Loader = "ld.so.1";
  395. break;
  396. case llvm::Triple::mips:
  397. case llvm::Triple::mipsel:
  398. case llvm::Triple::mips64:
  399. case llvm::Triple::mips64el: {
  400. bool IsNaN2008 = tools::mips::isNaN2008(getDriver(), Args, Triple);
  401. LibDir = "lib" + tools::mips::getMipsABILibSuffix(Args, Triple);
  402. if (tools::mips::isUCLibc(Args))
  403. Loader = IsNaN2008 ? "ld-uClibc-mipsn8.so.0" : "ld-uClibc.so.0";
  404. else if (!Triple.hasEnvironment() &&
  405. Triple.getVendor() == llvm::Triple::VendorType::MipsTechnologies)
  406. Loader =
  407. Triple.isLittleEndian() ? "ld-musl-mipsel.so.1" : "ld-musl-mips.so.1";
  408. else
  409. Loader = IsNaN2008 ? "ld-linux-mipsn8.so.1" : "ld.so.1";
  410. break;
  411. }
  412. case llvm::Triple::ppc:
  413. LibDir = "lib";
  414. Loader = "ld.so.1";
  415. break;
  416. case llvm::Triple::ppcle:
  417. LibDir = "lib";
  418. Loader = "ld.so.1";
  419. break;
  420. case llvm::Triple::ppc64:
  421. LibDir = "lib64";
  422. Loader =
  423. (tools::ppc::hasPPCAbiArg(Args, "elfv2")) ? "ld64.so.2" : "ld64.so.1";
  424. break;
  425. case llvm::Triple::ppc64le:
  426. LibDir = "lib64";
  427. Loader =
  428. (tools::ppc::hasPPCAbiArg(Args, "elfv1")) ? "ld64.so.1" : "ld64.so.2";
  429. break;
  430. case llvm::Triple::riscv32: {
  431. StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
  432. LibDir = "lib";
  433. Loader = ("ld-linux-riscv32-" + ABIName + ".so.1").str();
  434. break;
  435. }
  436. case llvm::Triple::riscv64: {
  437. StringRef ABIName = tools::riscv::getRISCVABI(Args, Triple);
  438. LibDir = "lib";
  439. Loader = ("ld-linux-riscv64-" + ABIName + ".so.1").str();
  440. break;
  441. }
  442. case llvm::Triple::sparc:
  443. case llvm::Triple::sparcel:
  444. LibDir = "lib";
  445. Loader = "ld-linux.so.2";
  446. break;
  447. case llvm::Triple::sparcv9:
  448. LibDir = "lib64";
  449. Loader = "ld-linux.so.2";
  450. break;
  451. case llvm::Triple::systemz:
  452. LibDir = "lib";
  453. Loader = "ld64.so.1";
  454. break;
  455. case llvm::Triple::x86:
  456. LibDir = "lib";
  457. Loader = "ld-linux.so.2";
  458. break;
  459. case llvm::Triple::x86_64: {
  460. bool X32 = Triple.isX32();
  461. LibDir = X32 ? "libx32" : "lib64";
  462. Loader = X32 ? "ld-linux-x32.so.2" : "ld-linux-x86-64.so.2";
  463. break;
  464. }
  465. case llvm::Triple::ve:
  466. return "/opt/nec/ve/lib/ld-linux-ve.so.1";
  467. }
  468. if (Distro == Distro::Exherbo &&
  469. (Triple.getVendor() == llvm::Triple::UnknownVendor ||
  470. Triple.getVendor() == llvm::Triple::PC))
  471. return "/usr/" + Triple.str() + "/lib/" + Loader;
  472. return "/" + LibDir + "/" + Loader;
  473. }
  474. void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
  475. ArgStringList &CC1Args) const {
  476. const Driver &D = getDriver();
  477. std::string SysRoot = computeSysRoot();
  478. if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
  479. return;
  480. // Add 'include' in the resource directory, which is similar to
  481. // GCC_INCLUDE_DIR (private headers) in GCC. Note: the include directory
  482. // contains some files conflicting with system /usr/include. musl systems
  483. // prefer the /usr/include copies which are more relevant.
  484. SmallString<128> ResourceDirInclude(D.ResourceDir);
  485. llvm::sys::path::append(ResourceDirInclude, "include");
  486. if (!DriverArgs.hasArg(options::OPT_nobuiltininc) &&
  487. (!getTriple().isMusl() || DriverArgs.hasArg(options::OPT_nostdlibinc)))
  488. addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude);
  489. if (DriverArgs.hasArg(options::OPT_nostdlibinc))
  490. return;
  491. // LOCAL_INCLUDE_DIR
  492. addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
  493. // TOOL_INCLUDE_DIR
  494. AddMultilibIncludeArgs(DriverArgs, CC1Args);
  495. // Check for configure-time C include directories.
  496. StringRef CIncludeDirs(C_INCLUDE_DIRS);
  497. if (CIncludeDirs != "") {
  498. SmallVector<StringRef, 5> dirs;
  499. CIncludeDirs.split(dirs, ":");
  500. for (StringRef dir : dirs) {
  501. StringRef Prefix =
  502. llvm::sys::path::is_absolute(dir) ? "" : StringRef(SysRoot);
  503. addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
  504. }
  505. return;
  506. }
  507. // On systems using multiarch and Android, add /usr/include/$triple before
  508. // /usr/include.
  509. std::string MultiarchIncludeDir = getMultiarchTriple(D, getTriple(), SysRoot);
  510. if (!MultiarchIncludeDir.empty() &&
  511. D.getVFS().exists(SysRoot + "/usr/include/" + MultiarchIncludeDir))
  512. addExternCSystemInclude(DriverArgs, CC1Args,
  513. SysRoot + "/usr/include/" + MultiarchIncludeDir);
  514. if (getTriple().getOS() == llvm::Triple::RTEMS)
  515. return;
  516. // Add an include of '/include' directly. This isn't provided by default by
  517. // system GCCs, but is often used with cross-compiling GCCs, and harmless to
  518. // add even when Clang is acting as-if it were a system compiler.
  519. addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
  520. addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
  521. if (!DriverArgs.hasArg(options::OPT_nobuiltininc) && getTriple().isMusl())
  522. addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude);
  523. }
  524. void Linux::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
  525. llvm::opt::ArgStringList &CC1Args) const {
  526. // We need a detected GCC installation on Linux to provide libstdc++'s
  527. // headers in odd Linuxish places.
  528. if (!GCCInstallation.isValid())
  529. return;
  530. // Detect Debian g++-multiarch-incdir.diff.
  531. StringRef TripleStr = GCCInstallation.getTriple().str();
  532. StringRef DebianMultiarch =
  533. GCCInstallation.getTriple().getArch() == llvm::Triple::x86
  534. ? "i386-linux-gnu"
  535. : TripleStr;
  536. // Try generic GCC detection first.
  537. if (Generic_GCC::addGCCLibStdCxxIncludePaths(DriverArgs, CC1Args,
  538. DebianMultiarch))
  539. return;
  540. StringRef LibDir = GCCInstallation.getParentLibPath();
  541. const Multilib &Multilib = GCCInstallation.getMultilib();
  542. const GCCVersion &Version = GCCInstallation.getVersion();
  543. const std::string LibStdCXXIncludePathCandidates[] = {
  544. // Android standalone toolchain has C++ headers in yet another place.
  545. LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text,
  546. // Freescale SDK C++ headers are directly in <sysroot>/usr/include/c++,
  547. // without a subdirectory corresponding to the gcc version.
  548. LibDir.str() + "/../include/c++",
  549. // Cray's gcc installation puts headers under "g++" without a
  550. // version suffix.
  551. LibDir.str() + "/../include/g++",
  552. };
  553. for (const auto &IncludePath : LibStdCXXIncludePathCandidates) {
  554. if (addLibStdCXXIncludePaths(IncludePath, TripleStr,
  555. Multilib.includeSuffix(), DriverArgs, CC1Args))
  556. break;
  557. }
  558. }
  559. void Linux::AddCudaIncludeArgs(const ArgList &DriverArgs,
  560. ArgStringList &CC1Args) const {
  561. CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
  562. }
  563. void Linux::AddHIPIncludeArgs(const ArgList &DriverArgs,
  564. ArgStringList &CC1Args) const {
  565. RocmInstallation.AddHIPIncludeArgs(DriverArgs, CC1Args);
  566. }
  567. void Linux::AddIAMCUIncludeArgs(const ArgList &DriverArgs,
  568. ArgStringList &CC1Args) const {
  569. if (GCCInstallation.isValid()) {
  570. CC1Args.push_back("-isystem");
  571. CC1Args.push_back(DriverArgs.MakeArgString(
  572. GCCInstallation.getParentLibPath() + "/../" +
  573. GCCInstallation.getTriple().str() + "/include"));
  574. }
  575. }
  576. bool Linux::isPIEDefault(const llvm::opt::ArgList &Args) const {
  577. return CLANG_DEFAULT_PIE_ON_LINUX || getTriple().isAndroid() ||
  578. getTriple().isMusl() || getSanitizerArgs(Args).requiresPIE();
  579. }
  580. bool Linux::IsAArch64OutlineAtomicsDefault(const ArgList &Args) const {
  581. // Outline atomics for AArch64 are supported by compiler-rt
  582. // and libgcc since 9.3.1
  583. assert(getTriple().isAArch64() && "expected AArch64 target!");
  584. ToolChain::RuntimeLibType RtLib = GetRuntimeLibType(Args);
  585. if (RtLib == ToolChain::RLT_CompilerRT)
  586. return true;
  587. assert(RtLib == ToolChain::RLT_Libgcc && "unexpected runtime library type!");
  588. if (GCCInstallation.getVersion().isOlderThan(9, 3, 1))
  589. return false;
  590. return true;
  591. }
  592. bool Linux::IsMathErrnoDefault() const {
  593. if (getTriple().isAndroid() || getTriple().isMusl())
  594. return false;
  595. return Generic_ELF::IsMathErrnoDefault();
  596. }
  597. SanitizerMask Linux::getSupportedSanitizers() const {
  598. const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
  599. const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
  600. const bool IsMIPS = getTriple().isMIPS32();
  601. const bool IsMIPS64 = getTriple().isMIPS64();
  602. const bool IsPowerPC64 = getTriple().getArch() == llvm::Triple::ppc64 ||
  603. getTriple().getArch() == llvm::Triple::ppc64le;
  604. const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64 ||
  605. getTriple().getArch() == llvm::Triple::aarch64_be;
  606. const bool IsArmArch = getTriple().getArch() == llvm::Triple::arm ||
  607. getTriple().getArch() == llvm::Triple::thumb ||
  608. getTriple().getArch() == llvm::Triple::armeb ||
  609. getTriple().getArch() == llvm::Triple::thumbeb;
  610. const bool IsRISCV64 = getTriple().getArch() == llvm::Triple::riscv64;
  611. const bool IsSystemZ = getTriple().getArch() == llvm::Triple::systemz;
  612. const bool IsHexagon = getTriple().getArch() == llvm::Triple::hexagon;
  613. SanitizerMask Res = ToolChain::getSupportedSanitizers();
  614. Res |= SanitizerKind::Address;
  615. Res |= SanitizerKind::PointerCompare;
  616. Res |= SanitizerKind::PointerSubtract;
  617. Res |= SanitizerKind::Fuzzer;
  618. Res |= SanitizerKind::FuzzerNoLink;
  619. Res |= SanitizerKind::KernelAddress;
  620. Res |= SanitizerKind::Memory;
  621. Res |= SanitizerKind::Vptr;
  622. Res |= SanitizerKind::SafeStack;
  623. if (IsX86_64 || IsMIPS64 || IsAArch64)
  624. Res |= SanitizerKind::DataFlow;
  625. if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsArmArch || IsPowerPC64 ||
  626. IsRISCV64 || IsSystemZ || IsHexagon)
  627. Res |= SanitizerKind::Leak;
  628. if (IsX86_64 || IsMIPS64 || IsAArch64 || IsPowerPC64 || IsSystemZ)
  629. Res |= SanitizerKind::Thread;
  630. if (IsX86_64)
  631. Res |= SanitizerKind::KernelMemory;
  632. if (IsX86 || IsX86_64)
  633. Res |= SanitizerKind::Function;
  634. if (IsX86_64 || IsMIPS64 || IsAArch64 || IsX86 || IsMIPS || IsArmArch ||
  635. IsPowerPC64 || IsHexagon)
  636. Res |= SanitizerKind::Scudo;
  637. if (IsX86_64 || IsAArch64) {
  638. Res |= SanitizerKind::HWAddress;
  639. Res |= SanitizerKind::KernelHWAddress;
  640. }
  641. return Res;
  642. }
  643. void Linux::addProfileRTLibs(const llvm::opt::ArgList &Args,
  644. llvm::opt::ArgStringList &CmdArgs) const {
  645. // Add linker option -u__llvm_profile_runtime to cause runtime
  646. // initialization module to be linked in.
  647. if (needsProfileRT(Args))
  648. CmdArgs.push_back(Args.MakeArgString(
  649. Twine("-u", llvm::getInstrProfRuntimeHookVarName())));
  650. ToolChain::addProfileRTLibs(Args, CmdArgs);
  651. }
  652. llvm::DenormalMode
  653. Linux::getDefaultDenormalModeForType(const llvm::opt::ArgList &DriverArgs,
  654. const JobAction &JA,
  655. const llvm::fltSemantics *FPType) const {
  656. switch (getTriple().getArch()) {
  657. case llvm::Triple::x86:
  658. case llvm::Triple::x86_64: {
  659. std::string Unused;
  660. // DAZ and FTZ are turned on in crtfastmath.o
  661. if (!DriverArgs.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles) &&
  662. isFastMathRuntimeAvailable(DriverArgs, Unused))
  663. return llvm::DenormalMode::getPreserveSign();
  664. return llvm::DenormalMode::getIEEE();
  665. }
  666. default:
  667. return llvm::DenormalMode::getIEEE();
  668. }
  669. }
  670. void Linux::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {
  671. for (const auto &Opt : ExtraOpts)
  672. CmdArgs.push_back(Opt.c_str());
  673. }