FreeBSD.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. //===--- FreeBSD.cpp - FreeBSD 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 "FreeBSD.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/DriverDiagnostic.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 freebsd::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. claimNoWarnArgs(Args);
  30. ArgStringList CmdArgs;
  31. const auto &D = getToolChain().getDriver();
  32. // When building 32-bit code on FreeBSD/amd64, we have to explicitly
  33. // instruct as in the base system to assemble 32-bit code.
  34. switch (getToolChain().getArch()) {
  35. default:
  36. break;
  37. case llvm::Triple::x86:
  38. CmdArgs.push_back("--32");
  39. break;
  40. case llvm::Triple::ppc:
  41. case llvm::Triple::ppcle:
  42. CmdArgs.push_back("-a32");
  43. break;
  44. case llvm::Triple::mips:
  45. case llvm::Triple::mipsel:
  46. case llvm::Triple::mips64:
  47. case llvm::Triple::mips64el: {
  48. StringRef CPUName;
  49. StringRef ABIName;
  50. mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
  51. CmdArgs.push_back("-march");
  52. CmdArgs.push_back(CPUName.data());
  53. CmdArgs.push_back("-mabi");
  54. CmdArgs.push_back(mips::getGnuCompatibleMipsABIName(ABIName).data());
  55. if (getToolChain().getTriple().isLittleEndian())
  56. CmdArgs.push_back("-EL");
  57. else
  58. CmdArgs.push_back("-EB");
  59. if (Arg *A = Args.getLastArg(options::OPT_G)) {
  60. StringRef v = A->getValue();
  61. CmdArgs.push_back(Args.MakeArgString("-G" + v));
  62. A->claim();
  63. }
  64. AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
  65. break;
  66. }
  67. case llvm::Triple::arm:
  68. case llvm::Triple::armeb:
  69. case llvm::Triple::thumb:
  70. case llvm::Triple::thumbeb: {
  71. arm::FloatABI ABI = arm::getARMFloatABI(getToolChain(), Args);
  72. if (ABI == arm::FloatABI::Hard)
  73. CmdArgs.push_back("-mfpu=vfp");
  74. else
  75. CmdArgs.push_back("-mfpu=softvfp");
  76. switch (getToolChain().getTriple().getEnvironment()) {
  77. case llvm::Triple::GNUEABIHF:
  78. case llvm::Triple::GNUEABI:
  79. case llvm::Triple::EABI:
  80. CmdArgs.push_back("-meabi=5");
  81. break;
  82. default:
  83. CmdArgs.push_back("-matpcs");
  84. }
  85. break;
  86. }
  87. case llvm::Triple::sparc:
  88. case llvm::Triple::sparcel:
  89. case llvm::Triple::sparcv9: {
  90. std::string CPU = getCPUName(D, Args, getToolChain().getTriple());
  91. CmdArgs.push_back(
  92. sparc::getSparcAsmModeForCPU(CPU, getToolChain().getTriple()));
  93. AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
  94. break;
  95. }
  96. }
  97. for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ,
  98. options::OPT_fdebug_prefix_map_EQ)) {
  99. StringRef Map = A->getValue();
  100. if (!Map.contains('='))
  101. D.Diag(diag::err_drv_invalid_argument_to_option)
  102. << Map << A->getOption().getName();
  103. else {
  104. CmdArgs.push_back(Args.MakeArgString("--debug-prefix-map"));
  105. CmdArgs.push_back(Args.MakeArgString(Map));
  106. }
  107. A->claim();
  108. }
  109. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
  110. CmdArgs.push_back("-o");
  111. CmdArgs.push_back(Output.getFilename());
  112. for (const auto &II : Inputs)
  113. CmdArgs.push_back(II.getFilename());
  114. const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
  115. C.addCommand(std::make_unique<Command>(JA, *this,
  116. ResponseFileSupport::AtFileCurCP(),
  117. Exec, CmdArgs, Inputs, Output));
  118. }
  119. void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
  120. const InputInfo &Output,
  121. const InputInfoList &Inputs,
  122. const ArgList &Args,
  123. const char *LinkingOutput) const {
  124. const toolchains::FreeBSD &ToolChain =
  125. static_cast<const toolchains::FreeBSD &>(getToolChain());
  126. const Driver &D = ToolChain.getDriver();
  127. const llvm::Triple::ArchType Arch = ToolChain.getArch();
  128. const bool IsPIE =
  129. !Args.hasArg(options::OPT_shared) &&
  130. (Args.hasArg(options::OPT_pie) || ToolChain.isPIEDefault(Args));
  131. ArgStringList CmdArgs;
  132. // Silence warning for "clang -g foo.o -o foo"
  133. Args.ClaimAllArgs(options::OPT_g_Group);
  134. // and "clang -emit-llvm foo.o -o foo"
  135. Args.ClaimAllArgs(options::OPT_emit_llvm);
  136. // and for "clang -w foo.o -o foo". Other warning options are already
  137. // handled somewhere else.
  138. Args.ClaimAllArgs(options::OPT_w);
  139. if (!D.SysRoot.empty())
  140. CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
  141. if (IsPIE)
  142. CmdArgs.push_back("-pie");
  143. CmdArgs.push_back("--eh-frame-hdr");
  144. if (Args.hasArg(options::OPT_static)) {
  145. CmdArgs.push_back("-Bstatic");
  146. } else {
  147. if (Args.hasArg(options::OPT_rdynamic))
  148. CmdArgs.push_back("-export-dynamic");
  149. if (Args.hasArg(options::OPT_shared)) {
  150. CmdArgs.push_back("-Bshareable");
  151. } else {
  152. CmdArgs.push_back("-dynamic-linker");
  153. CmdArgs.push_back("/libexec/ld-elf.so.1");
  154. }
  155. const llvm::Triple &T = ToolChain.getTriple();
  156. if (T.getOSMajorVersion() >= 9) {
  157. if (Arch == llvm::Triple::arm || Arch == llvm::Triple::sparc || T.isX86())
  158. CmdArgs.push_back("--hash-style=both");
  159. }
  160. CmdArgs.push_back("--enable-new-dtags");
  161. }
  162. // Explicitly set the linker emulation for platforms that might not
  163. // be the default emulation for the linker.
  164. switch (Arch) {
  165. case llvm::Triple::x86:
  166. CmdArgs.push_back("-m");
  167. CmdArgs.push_back("elf_i386_fbsd");
  168. break;
  169. case llvm::Triple::ppc:
  170. CmdArgs.push_back("-m");
  171. CmdArgs.push_back("elf32ppc_fbsd");
  172. break;
  173. case llvm::Triple::ppcle:
  174. CmdArgs.push_back("-m");
  175. // Use generic -- only usage is for freestanding.
  176. CmdArgs.push_back("elf32lppc");
  177. break;
  178. case llvm::Triple::mips:
  179. CmdArgs.push_back("-m");
  180. CmdArgs.push_back("elf32btsmip_fbsd");
  181. break;
  182. case llvm::Triple::mipsel:
  183. CmdArgs.push_back("-m");
  184. CmdArgs.push_back("elf32ltsmip_fbsd");
  185. break;
  186. case llvm::Triple::mips64:
  187. CmdArgs.push_back("-m");
  188. if (tools::mips::hasMipsAbiArg(Args, "n32"))
  189. CmdArgs.push_back("elf32btsmipn32_fbsd");
  190. else
  191. CmdArgs.push_back("elf64btsmip_fbsd");
  192. break;
  193. case llvm::Triple::mips64el:
  194. CmdArgs.push_back("-m");
  195. if (tools::mips::hasMipsAbiArg(Args, "n32"))
  196. CmdArgs.push_back("elf32ltsmipn32_fbsd");
  197. else
  198. CmdArgs.push_back("elf64ltsmip_fbsd");
  199. break;
  200. case llvm::Triple::riscv32:
  201. CmdArgs.push_back("-m");
  202. CmdArgs.push_back("elf32lriscv");
  203. break;
  204. case llvm::Triple::riscv64:
  205. CmdArgs.push_back("-m");
  206. CmdArgs.push_back("elf64lriscv");
  207. break;
  208. default:
  209. break;
  210. }
  211. if (Arg *A = Args.getLastArg(options::OPT_G)) {
  212. if (ToolChain.getTriple().isMIPS()) {
  213. StringRef v = A->getValue();
  214. CmdArgs.push_back(Args.MakeArgString("-G" + v));
  215. A->claim();
  216. }
  217. }
  218. if (Output.isFilename()) {
  219. CmdArgs.push_back("-o");
  220. CmdArgs.push_back(Output.getFilename());
  221. } else {
  222. assert(Output.isNothing() && "Invalid output.");
  223. }
  224. if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
  225. options::OPT_r)) {
  226. const char *crt1 = nullptr;
  227. if (!Args.hasArg(options::OPT_shared)) {
  228. if (Args.hasArg(options::OPT_pg))
  229. crt1 = "gcrt1.o";
  230. else if (IsPIE)
  231. crt1 = "Scrt1.o";
  232. else
  233. crt1 = "crt1.o";
  234. }
  235. if (crt1)
  236. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
  237. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
  238. const char *crtbegin = nullptr;
  239. if (Args.hasArg(options::OPT_static))
  240. crtbegin = "crtbeginT.o";
  241. else if (Args.hasArg(options::OPT_shared) || IsPIE)
  242. crtbegin = "crtbeginS.o";
  243. else
  244. crtbegin = "crtbegin.o";
  245. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
  246. }
  247. Args.AddAllArgs(CmdArgs, options::OPT_L);
  248. ToolChain.AddFilePathLibArgs(Args, CmdArgs);
  249. Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
  250. Args.AddAllArgs(CmdArgs, options::OPT_e);
  251. Args.AddAllArgs(CmdArgs, options::OPT_s);
  252. Args.AddAllArgs(CmdArgs, options::OPT_t);
  253. Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
  254. Args.AddAllArgs(CmdArgs, options::OPT_r);
  255. if (D.isUsingLTO()) {
  256. assert(!Inputs.empty() && "Must have at least one input.");
  257. addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs[0],
  258. D.getLTOMode() == LTOK_Thin);
  259. }
  260. bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
  261. bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
  262. addLinkerCompressDebugSectionsOption(ToolChain, Args, CmdArgs);
  263. AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
  264. unsigned Major = ToolChain.getTriple().getOSMajorVersion();
  265. bool Profiling = Args.hasArg(options::OPT_pg) && Major != 0 && Major < 14;
  266. if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
  267. options::OPT_r)) {
  268. // Use the static OpenMP runtime with -static-openmp
  269. bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) &&
  270. !Args.hasArg(options::OPT_static);
  271. addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP);
  272. if (D.CCCIsCXX()) {
  273. if (ToolChain.ShouldLinkCXXStdlib(Args))
  274. ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
  275. if (Profiling)
  276. CmdArgs.push_back("-lm_p");
  277. else
  278. CmdArgs.push_back("-lm");
  279. }
  280. if (NeedsSanitizerDeps)
  281. linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
  282. if (NeedsXRayDeps)
  283. linkXRayRuntimeDeps(ToolChain, CmdArgs);
  284. // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
  285. // the default system libraries. Just mimic this for now.
  286. if (Profiling)
  287. CmdArgs.push_back("-lgcc_p");
  288. else
  289. CmdArgs.push_back("-lgcc");
  290. if (Args.hasArg(options::OPT_static)) {
  291. CmdArgs.push_back("-lgcc_eh");
  292. } else if (Profiling) {
  293. CmdArgs.push_back("-lgcc_eh_p");
  294. } else {
  295. CmdArgs.push_back("--as-needed");
  296. CmdArgs.push_back("-lgcc_s");
  297. CmdArgs.push_back("--no-as-needed");
  298. }
  299. if (Args.hasArg(options::OPT_pthread)) {
  300. if (Profiling)
  301. CmdArgs.push_back("-lpthread_p");
  302. else
  303. CmdArgs.push_back("-lpthread");
  304. }
  305. if (Profiling) {
  306. if (Args.hasArg(options::OPT_shared))
  307. CmdArgs.push_back("-lc");
  308. else
  309. CmdArgs.push_back("-lc_p");
  310. CmdArgs.push_back("-lgcc_p");
  311. } else {
  312. CmdArgs.push_back("-lc");
  313. CmdArgs.push_back("-lgcc");
  314. }
  315. if (Args.hasArg(options::OPT_static)) {
  316. CmdArgs.push_back("-lgcc_eh");
  317. } else if (Profiling) {
  318. CmdArgs.push_back("-lgcc_eh_p");
  319. } else {
  320. CmdArgs.push_back("--as-needed");
  321. CmdArgs.push_back("-lgcc_s");
  322. CmdArgs.push_back("--no-as-needed");
  323. }
  324. }
  325. if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
  326. options::OPT_r)) {
  327. if (Args.hasArg(options::OPT_shared) || IsPIE)
  328. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o")));
  329. else
  330. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
  331. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
  332. }
  333. ToolChain.addProfileRTLibs(Args, CmdArgs);
  334. const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
  335. C.addCommand(std::make_unique<Command>(JA, *this,
  336. ResponseFileSupport::AtFileCurCP(),
  337. Exec, CmdArgs, Inputs, Output));
  338. }
  339. /// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
  340. FreeBSD::FreeBSD(const Driver &D, const llvm::Triple &Triple,
  341. const ArgList &Args)
  342. : Generic_ELF(D, Triple, Args) {
  343. // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
  344. // back to '/usr/lib' if it doesn't exist.
  345. if ((Triple.getArch() == llvm::Triple::x86 || Triple.isMIPS32() ||
  346. Triple.isPPC32()) &&
  347. D.getVFS().exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
  348. getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
  349. else
  350. getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
  351. }
  352. ToolChain::CXXStdlibType FreeBSD::GetDefaultCXXStdlibType() const {
  353. unsigned Major = getTriple().getOSMajorVersion();
  354. if (Major >= 10 || Major == 0)
  355. return ToolChain::CST_Libcxx;
  356. return ToolChain::CST_Libstdcxx;
  357. }
  358. unsigned FreeBSD::GetDefaultDwarfVersion() const {
  359. if (getTriple().getOSMajorVersion() < 12)
  360. return 2;
  361. return 4;
  362. }
  363. void FreeBSD::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
  364. llvm::opt::ArgStringList &CC1Args) const {
  365. addSystemInclude(DriverArgs, CC1Args,
  366. getDriver().SysRoot + "/usr/include/c++/v1");
  367. }
  368. void FreeBSD::addLibStdCxxIncludePaths(
  369. const llvm::opt::ArgList &DriverArgs,
  370. llvm::opt::ArgStringList &CC1Args) const {
  371. addLibStdCXXIncludePaths(getDriver().SysRoot + "/usr/include/c++/4.2", "", "",
  372. DriverArgs, CC1Args);
  373. }
  374. void FreeBSD::AddCXXStdlibLibArgs(const ArgList &Args,
  375. ArgStringList &CmdArgs) const {
  376. CXXStdlibType Type = GetCXXStdlibType(Args);
  377. unsigned Major = getTriple().getOSMajorVersion();
  378. bool Profiling = Args.hasArg(options::OPT_pg) && Major != 0 && Major < 14;
  379. switch (Type) {
  380. case ToolChain::CST_Libcxx:
  381. CmdArgs.push_back(Profiling ? "-lc++_p" : "-lc++");
  382. break;
  383. case ToolChain::CST_Libstdcxx:
  384. CmdArgs.push_back(Profiling ? "-lstdc++_p" : "-lstdc++");
  385. break;
  386. }
  387. }
  388. void FreeBSD::AddCudaIncludeArgs(const ArgList &DriverArgs,
  389. ArgStringList &CC1Args) const {
  390. CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
  391. }
  392. void FreeBSD::AddHIPIncludeArgs(const ArgList &DriverArgs,
  393. ArgStringList &CC1Args) const {
  394. RocmInstallation.AddHIPIncludeArgs(DriverArgs, CC1Args);
  395. }
  396. Tool *FreeBSD::buildAssembler() const {
  397. return new tools::freebsd::Assembler(*this);
  398. }
  399. Tool *FreeBSD::buildLinker() const { return new tools::freebsd::Linker(*this); }
  400. llvm::ExceptionHandling FreeBSD::GetExceptionModel(const ArgList &Args) const {
  401. // FreeBSD uses SjLj exceptions on ARM oabi.
  402. switch (getTriple().getEnvironment()) {
  403. case llvm::Triple::GNUEABIHF:
  404. case llvm::Triple::GNUEABI:
  405. case llvm::Triple::EABI:
  406. return llvm::ExceptionHandling::None;
  407. default:
  408. if (getTriple().getArch() == llvm::Triple::arm ||
  409. getTriple().getArch() == llvm::Triple::thumb)
  410. return llvm::ExceptionHandling::SjLj;
  411. return llvm::ExceptionHandling::None;
  412. }
  413. }
  414. bool FreeBSD::HasNativeLLVMSupport() const { return true; }
  415. bool FreeBSD::IsUnwindTablesDefault(const ArgList &Args) const { return true; }
  416. bool FreeBSD::isPIEDefault(const llvm::opt::ArgList &Args) const {
  417. return getSanitizerArgs(Args).requiresPIE();
  418. }
  419. SanitizerMask FreeBSD::getSupportedSanitizers() const {
  420. const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64;
  421. const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
  422. const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
  423. const bool IsMIPS64 = getTriple().isMIPS64();
  424. SanitizerMask Res = ToolChain::getSupportedSanitizers();
  425. Res |= SanitizerKind::Address;
  426. Res |= SanitizerKind::PointerCompare;
  427. Res |= SanitizerKind::PointerSubtract;
  428. Res |= SanitizerKind::Vptr;
  429. if (IsX86_64 || IsMIPS64) {
  430. Res |= SanitizerKind::Leak;
  431. Res |= SanitizerKind::Thread;
  432. }
  433. if (IsX86 || IsX86_64) {
  434. Res |= SanitizerKind::Function;
  435. Res |= SanitizerKind::SafeStack;
  436. Res |= SanitizerKind::Fuzzer;
  437. Res |= SanitizerKind::FuzzerNoLink;
  438. }
  439. if (IsAArch64 || IsX86_64) {
  440. Res |= SanitizerKind::KernelAddress;
  441. Res |= SanitizerKind::KernelMemory;
  442. }
  443. if (IsX86_64) {
  444. Res |= SanitizerKind::Memory;
  445. }
  446. return Res;
  447. }
  448. void FreeBSD::addClangTargetOptions(const ArgList &DriverArgs,
  449. ArgStringList &CC1Args,
  450. Action::OffloadKind) const {
  451. if (!DriverArgs.hasFlag(options::OPT_fuse_init_array,
  452. options::OPT_fno_use_init_array,
  453. getTriple().getOSMajorVersion() >= 12))
  454. CC1Args.push_back("-fno-use-init-array");
  455. }