Linux.cpp 40 KB

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