Linux.cpp 40 KB

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