NetBSD.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. //===--- NetBSD.cpp - NetBSD 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 "NetBSD.h"
  9. #include "Arch/ARM.h"
  10. #include "Arch/Mips.h"
  11. #include "Arch/Sparc.h"
  12. #include "CommonArgs.h"
  13. #include "clang/Driver/Compilation.h"
  14. #include "clang/Driver/Driver.h"
  15. #include "clang/Driver/Options.h"
  16. #include "clang/Driver/SanitizerArgs.h"
  17. #include "llvm/Option/ArgList.h"
  18. #include "llvm/Support/VirtualFileSystem.h"
  19. using namespace clang::driver;
  20. using namespace clang::driver::tools;
  21. using namespace clang::driver::toolchains;
  22. using namespace clang;
  23. using namespace llvm::opt;
  24. void netbsd::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
  25. const InputInfo &Output,
  26. const InputInfoList &Inputs,
  27. const ArgList &Args,
  28. const char *LinkingOutput) const {
  29. const toolchains::NetBSD &ToolChain =
  30. static_cast<const toolchains::NetBSD &>(getToolChain());
  31. const Driver &D = ToolChain.getDriver();
  32. const llvm::Triple &Triple = ToolChain.getTriple();
  33. claimNoWarnArgs(Args);
  34. ArgStringList CmdArgs;
  35. // GNU as needs different flags for creating the correct output format
  36. // on architectures with different ABIs or optional feature sets.
  37. switch (ToolChain.getArch()) {
  38. case llvm::Triple::x86:
  39. CmdArgs.push_back("--32");
  40. break;
  41. case llvm::Triple::arm:
  42. case llvm::Triple::armeb:
  43. case llvm::Triple::thumb:
  44. case llvm::Triple::thumbeb: {
  45. StringRef MArch, MCPU;
  46. arm::getARMArchCPUFromArgs(Args, MArch, MCPU, /*FromAs*/ true);
  47. std::string Arch = arm::getARMTargetCPU(MCPU, MArch, Triple);
  48. CmdArgs.push_back(Args.MakeArgString("-mcpu=" + Arch));
  49. break;
  50. }
  51. case llvm::Triple::mips:
  52. case llvm::Triple::mipsel:
  53. case llvm::Triple::mips64:
  54. case llvm::Triple::mips64el: {
  55. StringRef CPUName;
  56. StringRef ABIName;
  57. mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
  58. CmdArgs.push_back("-march");
  59. CmdArgs.push_back(CPUName.data());
  60. CmdArgs.push_back("-mabi");
  61. CmdArgs.push_back(mips::getGnuCompatibleMipsABIName(ABIName).data());
  62. if (Triple.isLittleEndian())
  63. CmdArgs.push_back("-EL");
  64. else
  65. CmdArgs.push_back("-EB");
  66. AddAssemblerKPIC(ToolChain, Args, CmdArgs);
  67. break;
  68. }
  69. case llvm::Triple::sparc:
  70. case llvm::Triple::sparcel: {
  71. CmdArgs.push_back("-32");
  72. std::string CPU = getCPUName(D, Args, Triple);
  73. CmdArgs.push_back(sparc::getSparcAsmModeForCPU(CPU, Triple));
  74. AddAssemblerKPIC(ToolChain, Args, CmdArgs);
  75. break;
  76. }
  77. case llvm::Triple::sparcv9: {
  78. CmdArgs.push_back("-64");
  79. std::string CPU = getCPUName(D, Args, Triple);
  80. CmdArgs.push_back(sparc::getSparcAsmModeForCPU(CPU, Triple));
  81. AddAssemblerKPIC(ToolChain, Args, CmdArgs);
  82. break;
  83. }
  84. default:
  85. break;
  86. }
  87. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
  88. CmdArgs.push_back("-o");
  89. CmdArgs.push_back(Output.getFilename());
  90. for (const auto &II : Inputs)
  91. CmdArgs.push_back(II.getFilename());
  92. const char *Exec = Args.MakeArgString((ToolChain.GetProgramPath("as")));
  93. C.addCommand(std::make_unique<Command>(JA, *this,
  94. ResponseFileSupport::AtFileCurCP(),
  95. Exec, CmdArgs, Inputs, Output));
  96. }
  97. void netbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
  98. const InputInfo &Output,
  99. const InputInfoList &Inputs,
  100. const ArgList &Args,
  101. const char *LinkingOutput) const {
  102. const toolchains::NetBSD &ToolChain =
  103. static_cast<const toolchains::NetBSD &>(getToolChain());
  104. const Driver &D = ToolChain.getDriver();
  105. const llvm::Triple &Triple = ToolChain.getTriple();
  106. ArgStringList CmdArgs;
  107. if (!D.SysRoot.empty())
  108. CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
  109. CmdArgs.push_back("--eh-frame-hdr");
  110. if (Args.hasArg(options::OPT_static)) {
  111. CmdArgs.push_back("-Bstatic");
  112. if (Args.hasArg(options::OPT_pie)) {
  113. Args.AddAllArgs(CmdArgs, options::OPT_pie);
  114. CmdArgs.push_back("--no-dynamic-linker");
  115. }
  116. } else {
  117. if (Args.hasArg(options::OPT_rdynamic))
  118. CmdArgs.push_back("-export-dynamic");
  119. if (Args.hasArg(options::OPT_shared)) {
  120. CmdArgs.push_back("-Bshareable");
  121. } else {
  122. Args.AddAllArgs(CmdArgs, options::OPT_pie);
  123. CmdArgs.push_back("-dynamic-linker");
  124. CmdArgs.push_back("/libexec/ld.elf_so");
  125. }
  126. }
  127. // Many NetBSD architectures support more than one ABI.
  128. // Determine the correct emulation for ld.
  129. switch (ToolChain.getArch()) {
  130. case llvm::Triple::x86:
  131. CmdArgs.push_back("-m");
  132. CmdArgs.push_back("elf_i386");
  133. break;
  134. case llvm::Triple::arm:
  135. case llvm::Triple::thumb:
  136. CmdArgs.push_back("-m");
  137. switch (Triple.getEnvironment()) {
  138. case llvm::Triple::EABI:
  139. case llvm::Triple::GNUEABI:
  140. CmdArgs.push_back("armelf_nbsd_eabi");
  141. break;
  142. case llvm::Triple::EABIHF:
  143. case llvm::Triple::GNUEABIHF:
  144. CmdArgs.push_back("armelf_nbsd_eabihf");
  145. break;
  146. default:
  147. CmdArgs.push_back("armelf_nbsd");
  148. break;
  149. }
  150. break;
  151. case llvm::Triple::armeb:
  152. case llvm::Triple::thumbeb:
  153. arm::appendBE8LinkFlag(Args, CmdArgs, ToolChain.getEffectiveTriple());
  154. CmdArgs.push_back("-m");
  155. switch (Triple.getEnvironment()) {
  156. case llvm::Triple::EABI:
  157. case llvm::Triple::GNUEABI:
  158. CmdArgs.push_back("armelfb_nbsd_eabi");
  159. break;
  160. case llvm::Triple::EABIHF:
  161. case llvm::Triple::GNUEABIHF:
  162. CmdArgs.push_back("armelfb_nbsd_eabihf");
  163. break;
  164. default:
  165. CmdArgs.push_back("armelfb_nbsd");
  166. break;
  167. }
  168. break;
  169. case llvm::Triple::mips64:
  170. case llvm::Triple::mips64el:
  171. if (mips::hasMipsAbiArg(Args, "32")) {
  172. CmdArgs.push_back("-m");
  173. if (ToolChain.getArch() == llvm::Triple::mips64)
  174. CmdArgs.push_back("elf32btsmip");
  175. else
  176. CmdArgs.push_back("elf32ltsmip");
  177. } else if (mips::hasMipsAbiArg(Args, "64")) {
  178. CmdArgs.push_back("-m");
  179. if (ToolChain.getArch() == llvm::Triple::mips64)
  180. CmdArgs.push_back("elf64btsmip");
  181. else
  182. CmdArgs.push_back("elf64ltsmip");
  183. }
  184. break;
  185. case llvm::Triple::ppc:
  186. CmdArgs.push_back("-m");
  187. CmdArgs.push_back("elf32ppc_nbsd");
  188. break;
  189. case llvm::Triple::ppc64:
  190. case llvm::Triple::ppc64le:
  191. CmdArgs.push_back("-m");
  192. CmdArgs.push_back("elf64ppc");
  193. break;
  194. case llvm::Triple::sparc:
  195. CmdArgs.push_back("-m");
  196. CmdArgs.push_back("elf32_sparc");
  197. break;
  198. case llvm::Triple::sparcv9:
  199. CmdArgs.push_back("-m");
  200. CmdArgs.push_back("elf64_sparc");
  201. break;
  202. default:
  203. break;
  204. }
  205. if (Output.isFilename()) {
  206. CmdArgs.push_back("-o");
  207. CmdArgs.push_back(Output.getFilename());
  208. } else {
  209. assert(Output.isNothing() && "Invalid output.");
  210. }
  211. if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
  212. options::OPT_r)) {
  213. if (!Args.hasArg(options::OPT_shared)) {
  214. CmdArgs.push_back(
  215. Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
  216. }
  217. CmdArgs.push_back(
  218. Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
  219. if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie)) {
  220. CmdArgs.push_back(
  221. Args.MakeArgString(ToolChain.GetFilePath("crtbeginS.o")));
  222. } else {
  223. CmdArgs.push_back(
  224. Args.MakeArgString(ToolChain.GetFilePath("crtbegin.o")));
  225. }
  226. }
  227. Args.AddAllArgs(CmdArgs, options::OPT_L);
  228. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  229. Args.AddAllArgs(CmdArgs, options::OPT_e);
  230. Args.AddAllArgs(CmdArgs, options::OPT_s);
  231. Args.AddAllArgs(CmdArgs, options::OPT_t);
  232. Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
  233. Args.AddAllArgs(CmdArgs, options::OPT_r);
  234. bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
  235. bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
  236. AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
  237. const SanitizerArgs &SanArgs = ToolChain.getSanitizerArgs(Args);
  238. if (SanArgs.needsSharedRt()) {
  239. CmdArgs.push_back("-rpath");
  240. CmdArgs.push_back(Args.MakeArgString(ToolChain.getCompilerRTPath()));
  241. }
  242. VersionTuple OsVersion = Triple.getOSVersion();
  243. bool useLibgcc = true;
  244. if (OsVersion >= VersionTuple(7) || OsVersion.getMajor() == 0) {
  245. switch (ToolChain.getArch()) {
  246. case llvm::Triple::aarch64:
  247. case llvm::Triple::aarch64_be:
  248. case llvm::Triple::arm:
  249. case llvm::Triple::armeb:
  250. case llvm::Triple::thumb:
  251. case llvm::Triple::thumbeb:
  252. case llvm::Triple::ppc:
  253. case llvm::Triple::ppc64:
  254. case llvm::Triple::ppc64le:
  255. case llvm::Triple::sparc:
  256. case llvm::Triple::sparcv9:
  257. case llvm::Triple::x86:
  258. case llvm::Triple::x86_64:
  259. useLibgcc = false;
  260. break;
  261. default:
  262. break;
  263. }
  264. }
  265. if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
  266. options::OPT_r)) {
  267. // Use the static OpenMP runtime with -static-openmp
  268. bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) &&
  269. !Args.hasArg(options::OPT_static);
  270. addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP);
  271. if (D.CCCIsCXX()) {
  272. if (ToolChain.ShouldLinkCXXStdlib(Args))
  273. ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
  274. CmdArgs.push_back("-lm");
  275. }
  276. if (NeedsSanitizerDeps)
  277. linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
  278. if (NeedsXRayDeps)
  279. linkXRayRuntimeDeps(ToolChain, CmdArgs);
  280. if (Args.hasArg(options::OPT_pthread))
  281. CmdArgs.push_back("-lpthread");
  282. CmdArgs.push_back("-lc");
  283. if (useLibgcc) {
  284. if (Args.hasArg(options::OPT_static)) {
  285. // libgcc_eh depends on libc, so resolve as much as possible,
  286. // pull in any new requirements from libc and then get the rest
  287. // of libgcc.
  288. CmdArgs.push_back("-lgcc_eh");
  289. CmdArgs.push_back("-lc");
  290. CmdArgs.push_back("-lgcc");
  291. } else {
  292. CmdArgs.push_back("-lgcc");
  293. CmdArgs.push_back("--as-needed");
  294. CmdArgs.push_back("-lgcc_s");
  295. CmdArgs.push_back("--no-as-needed");
  296. }
  297. }
  298. }
  299. if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
  300. options::OPT_r)) {
  301. if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
  302. CmdArgs.push_back(
  303. Args.MakeArgString(ToolChain.GetFilePath("crtendS.o")));
  304. else
  305. CmdArgs.push_back(
  306. Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
  307. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
  308. }
  309. ToolChain.addProfileRTLibs(Args, CmdArgs);
  310. const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
  311. C.addCommand(std::make_unique<Command>(JA, *this,
  312. ResponseFileSupport::AtFileCurCP(),
  313. Exec, CmdArgs, Inputs, Output));
  314. }
  315. /// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
  316. NetBSD::NetBSD(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
  317. : Generic_ELF(D, Triple, Args) {
  318. if (!Args.hasArg(options::OPT_nostdlib)) {
  319. // When targeting a 32-bit platform, try the special directory used on
  320. // 64-bit hosts, and only fall back to the main library directory if that
  321. // doesn't work.
  322. // FIXME: It'd be nicer to test if this directory exists, but I'm not sure
  323. // what all logic is needed to emulate the '=' prefix here.
  324. switch (Triple.getArch()) {
  325. case llvm::Triple::x86:
  326. getFilePaths().push_back("=/usr/lib/i386");
  327. break;
  328. case llvm::Triple::arm:
  329. case llvm::Triple::armeb:
  330. case llvm::Triple::thumb:
  331. case llvm::Triple::thumbeb:
  332. switch (Triple.getEnvironment()) {
  333. case llvm::Triple::EABI:
  334. case llvm::Triple::GNUEABI:
  335. getFilePaths().push_back("=/usr/lib/eabi");
  336. break;
  337. case llvm::Triple::EABIHF:
  338. case llvm::Triple::GNUEABIHF:
  339. getFilePaths().push_back("=/usr/lib/eabihf");
  340. break;
  341. default:
  342. getFilePaths().push_back("=/usr/lib/oabi");
  343. break;
  344. }
  345. break;
  346. case llvm::Triple::mips64:
  347. case llvm::Triple::mips64el:
  348. if (tools::mips::hasMipsAbiArg(Args, "o32"))
  349. getFilePaths().push_back("=/usr/lib/o32");
  350. else if (tools::mips::hasMipsAbiArg(Args, "64"))
  351. getFilePaths().push_back("=/usr/lib/64");
  352. break;
  353. case llvm::Triple::ppc:
  354. getFilePaths().push_back("=/usr/lib/powerpc");
  355. break;
  356. case llvm::Triple::sparc:
  357. getFilePaths().push_back("=/usr/lib/sparc");
  358. break;
  359. default:
  360. break;
  361. }
  362. getFilePaths().push_back("=/usr/lib");
  363. }
  364. }
  365. Tool *NetBSD::buildAssembler() const {
  366. return new tools::netbsd::Assembler(*this);
  367. }
  368. Tool *NetBSD::buildLinker() const { return new tools::netbsd::Linker(*this); }
  369. ToolChain::CXXStdlibType NetBSD::GetDefaultCXXStdlibType() const {
  370. VersionTuple OsVersion = getTriple().getOSVersion();
  371. if (OsVersion >= VersionTuple(7) || OsVersion.getMajor() == 0) {
  372. switch (getArch()) {
  373. case llvm::Triple::aarch64:
  374. case llvm::Triple::aarch64_be:
  375. case llvm::Triple::arm:
  376. case llvm::Triple::armeb:
  377. case llvm::Triple::thumb:
  378. case llvm::Triple::thumbeb:
  379. case llvm::Triple::ppc:
  380. case llvm::Triple::ppc64:
  381. case llvm::Triple::ppc64le:
  382. case llvm::Triple::sparc:
  383. case llvm::Triple::sparcv9:
  384. case llvm::Triple::x86:
  385. case llvm::Triple::x86_64:
  386. return ToolChain::CST_Libcxx;
  387. default:
  388. break;
  389. }
  390. }
  391. return ToolChain::CST_Libstdcxx;
  392. }
  393. void NetBSD::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
  394. llvm::opt::ArgStringList &CC1Args) const {
  395. const std::string Candidates[] = {
  396. // directory relative to build tree
  397. getDriver().Dir + "/../include/c++/v1",
  398. // system install with full upstream path
  399. getDriver().SysRoot + "/usr/include/c++/v1",
  400. // system install from src
  401. getDriver().SysRoot + "/usr/include/c++",
  402. };
  403. for (const auto &IncludePath : Candidates) {
  404. if (!getVFS().exists(IncludePath + "/__config"))
  405. continue;
  406. // Use the first candidate that looks valid.
  407. addSystemInclude(DriverArgs, CC1Args, IncludePath);
  408. return;
  409. }
  410. }
  411. void NetBSD::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
  412. llvm::opt::ArgStringList &CC1Args) const {
  413. addLibStdCXXIncludePaths(getDriver().SysRoot + "/usr/include/g++", "", "",
  414. DriverArgs, CC1Args);
  415. }
  416. llvm::ExceptionHandling NetBSD::GetExceptionModel(const ArgList &Args) const {
  417. // NetBSD uses Dwarf exceptions on ARM.
  418. llvm::Triple::ArchType TArch = getTriple().getArch();
  419. if (TArch == llvm::Triple::arm || TArch == llvm::Triple::armeb ||
  420. TArch == llvm::Triple::thumb || TArch == llvm::Triple::thumbeb)
  421. return llvm::ExceptionHandling::DwarfCFI;
  422. return llvm::ExceptionHandling::None;
  423. }
  424. SanitizerMask NetBSD::getSupportedSanitizers() const {
  425. const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
  426. const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
  427. SanitizerMask Res = ToolChain::getSupportedSanitizers();
  428. if (IsX86 || IsX86_64) {
  429. Res |= SanitizerKind::Address;
  430. Res |= SanitizerKind::PointerCompare;
  431. Res |= SanitizerKind::PointerSubtract;
  432. Res |= SanitizerKind::Function;
  433. Res |= SanitizerKind::Leak;
  434. Res |= SanitizerKind::SafeStack;
  435. Res |= SanitizerKind::Scudo;
  436. Res |= SanitizerKind::Vptr;
  437. }
  438. if (IsX86_64) {
  439. Res |= SanitizerKind::DataFlow;
  440. Res |= SanitizerKind::Fuzzer;
  441. Res |= SanitizerKind::FuzzerNoLink;
  442. Res |= SanitizerKind::HWAddress;
  443. Res |= SanitizerKind::KernelAddress;
  444. Res |= SanitizerKind::KernelHWAddress;
  445. Res |= SanitizerKind::KernelMemory;
  446. Res |= SanitizerKind::Memory;
  447. Res |= SanitizerKind::Thread;
  448. }
  449. return Res;
  450. }
  451. void NetBSD::addClangTargetOptions(const ArgList &DriverArgs,
  452. ArgStringList &CC1Args,
  453. Action::OffloadKind) const {
  454. const SanitizerArgs &SanArgs = getSanitizerArgs(DriverArgs);
  455. if (SanArgs.hasAnySanitizer())
  456. CC1Args.push_back("-D_REENTRANT");
  457. VersionTuple OsVersion = getTriple().getOSVersion();
  458. bool UseInitArrayDefault =
  459. OsVersion >= VersionTuple(9) || OsVersion.getMajor() == 0 ||
  460. getTriple().getArch() == llvm::Triple::aarch64 ||
  461. getTriple().getArch() == llvm::Triple::aarch64_be ||
  462. getTriple().getArch() == llvm::Triple::arm ||
  463. getTriple().getArch() == llvm::Triple::armeb;
  464. if (!DriverArgs.hasFlag(options::OPT_fuse_init_array,
  465. options::OPT_fno_use_init_array, UseInitArrayDefault))
  466. CC1Args.push_back("-fno-use-init-array");
  467. }