OpenBSD.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. //===--- OpenBSD.cpp - OpenBSD 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 "OpenBSD.h"
  9. #include "Arch/ARM.h"
  10. #include "Arch/Mips.h"
  11. #include "Arch/Sparc.h"
  12. #include "CommonArgs.h"
  13. #include "clang/Config/config.h"
  14. #include "clang/Driver/Compilation.h"
  15. #include "clang/Driver/Options.h"
  16. #include "clang/Driver/SanitizerArgs.h"
  17. #include "llvm/Option/ArgList.h"
  18. #include "llvm/Support/Path.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 openbsd::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::OpenBSD &ToolChain =
  30. static_cast<const toolchains::OpenBSD &>(getToolChain());
  31. const Driver &D = ToolChain.getDriver();
  32. const llvm::Triple &Triple = ToolChain.getTriple();
  33. claimNoWarnArgs(Args);
  34. ArgStringList CmdArgs;
  35. switch (ToolChain.getArch()) {
  36. case llvm::Triple::x86:
  37. // When building 32-bit code on OpenBSD/amd64, we have to explicitly
  38. // instruct as in the base system to assemble 32-bit code.
  39. CmdArgs.push_back("--32");
  40. break;
  41. case llvm::Triple::arm:
  42. case llvm::Triple::armeb: {
  43. StringRef MArch, MCPU;
  44. arm::getARMArchCPUFromArgs(Args, MArch, MCPU, /*FromAs*/ true);
  45. std::string Arch = arm::getARMTargetCPU(MCPU, MArch, Triple);
  46. CmdArgs.push_back(Args.MakeArgString("-mcpu=" + Arch));
  47. break;
  48. }
  49. case llvm::Triple::ppc:
  50. CmdArgs.push_back("-mppc");
  51. CmdArgs.push_back("-many");
  52. break;
  53. case llvm::Triple::sparcv9: {
  54. CmdArgs.push_back("-64");
  55. std::string CPU = getCPUName(D, Args, Triple);
  56. CmdArgs.push_back(sparc::getSparcAsmModeForCPU(CPU, Triple));
  57. AddAssemblerKPIC(ToolChain, Args, CmdArgs);
  58. break;
  59. }
  60. case llvm::Triple::mips64:
  61. case llvm::Triple::mips64el: {
  62. StringRef CPUName;
  63. StringRef ABIName;
  64. mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
  65. CmdArgs.push_back("-march");
  66. CmdArgs.push_back(CPUName.data());
  67. CmdArgs.push_back("-mabi");
  68. CmdArgs.push_back(mips::getGnuCompatibleMipsABIName(ABIName).data());
  69. if (Triple.isLittleEndian())
  70. CmdArgs.push_back("-EL");
  71. else
  72. CmdArgs.push_back("-EB");
  73. AddAssemblerKPIC(ToolChain, Args, CmdArgs);
  74. break;
  75. }
  76. default:
  77. break;
  78. }
  79. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
  80. CmdArgs.push_back("-o");
  81. CmdArgs.push_back(Output.getFilename());
  82. for (const auto &II : Inputs)
  83. CmdArgs.push_back(II.getFilename());
  84. const char *Exec = Args.MakeArgString(ToolChain.GetProgramPath("as"));
  85. C.addCommand(std::make_unique<Command>(JA, *this,
  86. ResponseFileSupport::AtFileCurCP(),
  87. Exec, CmdArgs, Inputs, Output));
  88. }
  89. void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
  90. const InputInfo &Output,
  91. const InputInfoList &Inputs,
  92. const ArgList &Args,
  93. const char *LinkingOutput) const {
  94. const toolchains::OpenBSD &ToolChain =
  95. static_cast<const toolchains::OpenBSD &>(getToolChain());
  96. const Driver &D = ToolChain.getDriver();
  97. ArgStringList CmdArgs;
  98. // Silence warning for "clang -g foo.o -o foo"
  99. Args.ClaimAllArgs(options::OPT_g_Group);
  100. // and "clang -emit-llvm foo.o -o foo"
  101. Args.ClaimAllArgs(options::OPT_emit_llvm);
  102. // and for "clang -w foo.o -o foo". Other warning options are already
  103. // handled somewhere else.
  104. Args.ClaimAllArgs(options::OPT_w);
  105. if (ToolChain.getArch() == llvm::Triple::mips64)
  106. CmdArgs.push_back("-EB");
  107. else if (ToolChain.getArch() == llvm::Triple::mips64el)
  108. CmdArgs.push_back("-EL");
  109. if (!Args.hasArg(options::OPT_nostdlib, options::OPT_shared)) {
  110. CmdArgs.push_back("-e");
  111. CmdArgs.push_back("__start");
  112. }
  113. CmdArgs.push_back("--eh-frame-hdr");
  114. if (Args.hasArg(options::OPT_static)) {
  115. CmdArgs.push_back("-Bstatic");
  116. } else {
  117. if (Args.hasArg(options::OPT_rdynamic))
  118. CmdArgs.push_back("-export-dynamic");
  119. CmdArgs.push_back("-Bdynamic");
  120. if (Args.hasArg(options::OPT_shared)) {
  121. CmdArgs.push_back("-shared");
  122. } else {
  123. CmdArgs.push_back("-dynamic-linker");
  124. CmdArgs.push_back("/usr/libexec/ld.so");
  125. }
  126. }
  127. if (Args.hasArg(options::OPT_pie))
  128. CmdArgs.push_back("-pie");
  129. if (Args.hasArg(options::OPT_nopie) || Args.hasArg(options::OPT_pg))
  130. CmdArgs.push_back("-nopie");
  131. if (Output.isFilename()) {
  132. CmdArgs.push_back("-o");
  133. CmdArgs.push_back(Output.getFilename());
  134. } else {
  135. assert(Output.isNothing() && "Invalid output.");
  136. }
  137. if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
  138. options::OPT_r)) {
  139. const char *crt0 = nullptr;
  140. const char *crtbegin = nullptr;
  141. if (!Args.hasArg(options::OPT_shared)) {
  142. if (Args.hasArg(options::OPT_pg))
  143. crt0 = "gcrt0.o";
  144. else if (Args.hasArg(options::OPT_static) &&
  145. !Args.hasArg(options::OPT_nopie))
  146. crt0 = "rcrt0.o";
  147. else
  148. crt0 = "crt0.o";
  149. crtbegin = "crtbegin.o";
  150. } else {
  151. crtbegin = "crtbeginS.o";
  152. }
  153. if (crt0)
  154. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt0)));
  155. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
  156. }
  157. Args.AddAllArgs(CmdArgs, options::OPT_L);
  158. ToolChain.AddFilePathLibArgs(Args, CmdArgs);
  159. Args.AddAllArgs(CmdArgs, {options::OPT_T_Group, options::OPT_e,
  160. options::OPT_s, options::OPT_t,
  161. options::OPT_Z_Flag, options::OPT_r});
  162. bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
  163. bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
  164. AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
  165. if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
  166. options::OPT_r)) {
  167. // Use the static OpenMP runtime with -static-openmp
  168. bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) &&
  169. !Args.hasArg(options::OPT_static);
  170. addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP);
  171. if (D.CCCIsCXX()) {
  172. if (ToolChain.ShouldLinkCXXStdlib(Args))
  173. ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
  174. if (Args.hasArg(options::OPT_pg))
  175. CmdArgs.push_back("-lm_p");
  176. else
  177. CmdArgs.push_back("-lm");
  178. }
  179. if (NeedsSanitizerDeps) {
  180. CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins"));
  181. linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
  182. }
  183. if (NeedsXRayDeps) {
  184. CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins"));
  185. linkXRayRuntimeDeps(ToolChain, CmdArgs);
  186. }
  187. // FIXME: For some reason GCC passes -lgcc before adding
  188. // the default system libraries. Just mimic this for now.
  189. CmdArgs.push_back("-lcompiler_rt");
  190. if (Args.hasArg(options::OPT_pthread)) {
  191. if (!Args.hasArg(options::OPT_shared) && Args.hasArg(options::OPT_pg))
  192. CmdArgs.push_back("-lpthread_p");
  193. else
  194. CmdArgs.push_back("-lpthread");
  195. }
  196. if (!Args.hasArg(options::OPT_shared)) {
  197. if (Args.hasArg(options::OPT_pg))
  198. CmdArgs.push_back("-lc_p");
  199. else
  200. CmdArgs.push_back("-lc");
  201. }
  202. CmdArgs.push_back("-lcompiler_rt");
  203. }
  204. if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
  205. options::OPT_r)) {
  206. const char *crtend = nullptr;
  207. if (!Args.hasArg(options::OPT_shared))
  208. crtend = "crtend.o";
  209. else
  210. crtend = "crtendS.o";
  211. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
  212. }
  213. ToolChain.addProfileRTLibs(Args, CmdArgs);
  214. const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
  215. C.addCommand(std::make_unique<Command>(JA, *this,
  216. ResponseFileSupport::AtFileCurCP(),
  217. Exec, CmdArgs, Inputs, Output));
  218. }
  219. SanitizerMask OpenBSD::getSupportedSanitizers() const {
  220. const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
  221. const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
  222. // For future use, only UBsan at the moment
  223. SanitizerMask Res = ToolChain::getSupportedSanitizers();
  224. if (IsX86 || IsX86_64) {
  225. Res |= SanitizerKind::Vptr;
  226. Res |= SanitizerKind::Fuzzer;
  227. Res |= SanitizerKind::FuzzerNoLink;
  228. }
  229. return Res;
  230. }
  231. /// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
  232. OpenBSD::OpenBSD(const Driver &D, const llvm::Triple &Triple,
  233. const ArgList &Args)
  234. : Generic_ELF(D, Triple, Args) {
  235. getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
  236. }
  237. void OpenBSD::AddClangSystemIncludeArgs(
  238. const llvm::opt::ArgList &DriverArgs,
  239. llvm::opt::ArgStringList &CC1Args) const {
  240. const Driver &D = getDriver();
  241. if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
  242. return;
  243. if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
  244. SmallString<128> Dir(D.ResourceDir);
  245. llvm::sys::path::append(Dir, "include");
  246. addSystemInclude(DriverArgs, CC1Args, Dir.str());
  247. }
  248. if (DriverArgs.hasArg(options::OPT_nostdlibinc))
  249. return;
  250. // Check for configure-time C include directories.
  251. StringRef CIncludeDirs(C_INCLUDE_DIRS);
  252. if (CIncludeDirs != "") {
  253. SmallVector<StringRef, 5> dirs;
  254. CIncludeDirs.split(dirs, ":");
  255. for (StringRef dir : dirs) {
  256. StringRef Prefix =
  257. llvm::sys::path::is_absolute(dir) ? StringRef(D.SysRoot) : "";
  258. addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
  259. }
  260. return;
  261. }
  262. addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include");
  263. }
  264. void OpenBSD::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
  265. llvm::opt::ArgStringList &CC1Args) const {
  266. addSystemInclude(DriverArgs, CC1Args,
  267. getDriver().SysRoot + "/usr/include/c++/v1");
  268. }
  269. void OpenBSD::AddCXXStdlibLibArgs(const ArgList &Args,
  270. ArgStringList &CmdArgs) const {
  271. bool Profiling = Args.hasArg(options::OPT_pg);
  272. CmdArgs.push_back(Profiling ? "-lc++_p" : "-lc++");
  273. CmdArgs.push_back(Profiling ? "-lc++abi_p" : "-lc++abi");
  274. CmdArgs.push_back(Profiling ? "-lpthread_p" : "-lpthread");
  275. }
  276. std::string OpenBSD::getCompilerRT(const ArgList &Args,
  277. StringRef Component,
  278. FileType Type) const {
  279. SmallString<128> Path(getDriver().SysRoot);
  280. llvm::sys::path::append(Path, "/usr/lib/libcompiler_rt.a");
  281. return std::string(Path.str());
  282. }
  283. Tool *OpenBSD::buildAssembler() const {
  284. return new tools::openbsd::Assembler(*this);
  285. }
  286. Tool *OpenBSD::buildLinker() const { return new tools::openbsd::Linker(*this); }
  287. bool OpenBSD::HasNativeLLVMSupport() const { return true; }
  288. bool OpenBSD::IsUnwindTablesDefault(const ArgList &Args) const {
  289. switch (getArch()) {
  290. case llvm::Triple::arm:
  291. return false;
  292. default:
  293. return true;
  294. }
  295. }