Linux.cpp 33 KB

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