clang  12.0.0git
OpenBSD.cpp
Go to the documentation of this file.
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 
9 #include "OpenBSD.h"
10 #include "Arch/Mips.h"
11 #include "Arch/Sparc.h"
12 #include "CommonArgs.h"
14 #include "clang/Driver/Options.h"
16 #include "llvm/Option/ArgList.h"
17 
18 using namespace clang::driver;
19 using namespace clang::driver::tools;
20 using namespace clang::driver::toolchains;
21 using namespace clang;
22 using namespace llvm::opt;
23 
25  const InputInfo &Output,
26  const InputInfoList &Inputs,
27  const ArgList &Args,
28  const char *LinkingOutput) const {
29  claimNoWarnArgs(Args);
30  ArgStringList CmdArgs;
31 
32  switch (getToolChain().getArch()) {
33  case llvm::Triple::x86:
34  // When building 32-bit code on OpenBSD/amd64, we have to explicitly
35  // instruct as in the base system to assemble 32-bit code.
36  CmdArgs.push_back("--32");
37  break;
38 
39  case llvm::Triple::ppc:
40  CmdArgs.push_back("-mppc");
41  CmdArgs.push_back("-many");
42  break;
43 
44  case llvm::Triple::sparc:
45  case llvm::Triple::sparcel: {
46  CmdArgs.push_back("-32");
47  std::string CPU = getCPUName(Args, getToolChain().getTriple());
48  CmdArgs.push_back(sparc::getSparcAsmModeForCPU(CPU, getToolChain().getTriple()));
49  AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
50  break;
51  }
52 
53  case llvm::Triple::sparcv9: {
54  CmdArgs.push_back("-64");
55  std::string CPU = getCPUName(Args, getToolChain().getTriple());
56  CmdArgs.push_back(sparc::getSparcAsmModeForCPU(CPU, getToolChain().getTriple()));
57  AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
58  break;
59  }
60 
61  case llvm::Triple::mips64:
62  case llvm::Triple::mips64el: {
63  StringRef CPUName;
64  StringRef ABIName;
65  mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
66 
67  CmdArgs.push_back("-mabi");
68  CmdArgs.push_back(mips::getGnuCompatibleMipsABIName(ABIName).data());
69 
70  if (getToolChain().getTriple().isLittleEndian())
71  CmdArgs.push_back("-EL");
72  else
73  CmdArgs.push_back("-EB");
74 
75  AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
76  break;
77  }
78 
79  default:
80  break;
81  }
82 
83  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
84 
85  CmdArgs.push_back("-o");
86  CmdArgs.push_back(Output.getFilename());
87 
88  for (const auto &II : Inputs)
89  CmdArgs.push_back(II.getFilename());
90 
91  const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
92  C.addCommand(std::make_unique<Command>(
93  JA, *this, ResponseFileSupport::AtFileCurCP(), Exec, CmdArgs, Inputs));
94 }
95 
97  const InputInfo &Output,
98  const InputInfoList &Inputs,
99  const ArgList &Args,
100  const char *LinkingOutput) const {
102  static_cast<const toolchains::OpenBSD &>(getToolChain());
103  const Driver &D = getToolChain().getDriver();
104  ArgStringList CmdArgs;
105 
106  // Silence warning for "clang -g foo.o -o foo"
107  Args.ClaimAllArgs(options::OPT_g_Group);
108  // and "clang -emit-llvm foo.o -o foo"
109  Args.ClaimAllArgs(options::OPT_emit_llvm);
110  // and for "clang -w foo.o -o foo". Other warning options are already
111  // handled somewhere else.
112  Args.ClaimAllArgs(options::OPT_w);
113 
114  if (ToolChain.getArch() == llvm::Triple::mips64)
115  CmdArgs.push_back("-EB");
116  else if (ToolChain.getArch() == llvm::Triple::mips64el)
117  CmdArgs.push_back("-EL");
118 
119  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_shared)) {
120  CmdArgs.push_back("-e");
121  CmdArgs.push_back("__start");
122  }
123 
124  CmdArgs.push_back("--eh-frame-hdr");
125  if (Args.hasArg(options::OPT_static)) {
126  CmdArgs.push_back("-Bstatic");
127  } else {
128  if (Args.hasArg(options::OPT_rdynamic))
129  CmdArgs.push_back("-export-dynamic");
130  CmdArgs.push_back("-Bdynamic");
131  if (Args.hasArg(options::OPT_shared)) {
132  CmdArgs.push_back("-shared");
133  } else {
134  CmdArgs.push_back("-dynamic-linker");
135  CmdArgs.push_back("/usr/libexec/ld.so");
136  }
137  }
138 
139  if (Args.hasArg(options::OPT_pie))
140  CmdArgs.push_back("-pie");
141  if (Args.hasArg(options::OPT_nopie) || Args.hasArg(options::OPT_pg))
142  CmdArgs.push_back("-nopie");
143 
144  if (Output.isFilename()) {
145  CmdArgs.push_back("-o");
146  CmdArgs.push_back(Output.getFilename());
147  } else {
148  assert(Output.isNothing() && "Invalid output.");
149  }
150 
151  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
152  const char *crt0 = nullptr;
153  const char *crtbegin = nullptr;
154  if (!Args.hasArg(options::OPT_shared)) {
155  if (Args.hasArg(options::OPT_pg))
156  crt0 = "gcrt0.o";
157  else if (Args.hasArg(options::OPT_static) &&
158  !Args.hasArg(options::OPT_nopie))
159  crt0 = "rcrt0.o";
160  else
161  crt0 = "crt0.o";
162  crtbegin = "crtbegin.o";
163  } else {
164  crtbegin = "crtbeginS.o";
165  }
166 
167  if (crt0)
168  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt0)));
169  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
170  }
171 
172  Args.AddAllArgs(CmdArgs, options::OPT_L);
173  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
174  Args.AddAllArgs(CmdArgs, {options::OPT_T_Group, options::OPT_e,
175  options::OPT_s, options::OPT_t,
176  options::OPT_Z_Flag, options::OPT_r});
177 
178  bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
179  bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
180  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
181 
182  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
183  if (D.CCCIsCXX()) {
184  if (ToolChain.ShouldLinkCXXStdlib(Args))
185  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
186  if (Args.hasArg(options::OPT_pg))
187  CmdArgs.push_back("-lm_p");
188  else
189  CmdArgs.push_back("-lm");
190  }
191  if (NeedsSanitizerDeps) {
192  CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins"));
193  linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
194  }
195  if (NeedsXRayDeps) {
196  CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins"));
197  linkXRayRuntimeDeps(ToolChain, CmdArgs);
198  }
199  // FIXME: For some reason GCC passes -lgcc before adding
200  // the default system libraries. Just mimic this for now.
201  CmdArgs.push_back("-lcompiler_rt");
202 
203  if (Args.hasArg(options::OPT_pthread)) {
204  if (!Args.hasArg(options::OPT_shared) && Args.hasArg(options::OPT_pg))
205  CmdArgs.push_back("-lpthread_p");
206  else
207  CmdArgs.push_back("-lpthread");
208  }
209 
210  if (!Args.hasArg(options::OPT_shared)) {
211  if (Args.hasArg(options::OPT_pg))
212  CmdArgs.push_back("-lc_p");
213  else
214  CmdArgs.push_back("-lc");
215  }
216 
217  CmdArgs.push_back("-lcompiler_rt");
218  }
219 
220  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
221  const char *crtend = nullptr;
222  if (!Args.hasArg(options::OPT_shared))
223  crtend = "crtend.o";
224  else
225  crtend = "crtendS.o";
226 
227  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
228  }
229 
230  const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
231  C.addCommand(std::make_unique<Command>(
232  JA, *this, ResponseFileSupport::AtFileCurCP(), Exec, CmdArgs, Inputs));
233 }
234 
235 SanitizerMask OpenBSD::getSupportedSanitizers() const {
236  const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
237  const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
238 
239  // For future use, only UBsan at the moment
241 
242  if (IsX86 || IsX86_64) {
243  Res |= SanitizerKind::Vptr;
244  Res |= SanitizerKind::Fuzzer;
245  Res |= SanitizerKind::FuzzerNoLink;
246  }
247 
248  return Res;
249 }
250 
251 /// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
252 
253 OpenBSD::OpenBSD(const Driver &D, const llvm::Triple &Triple,
254  const ArgList &Args)
255  : Generic_ELF(D, Triple, Args) {
256  getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
257 }
258 
259 void OpenBSD::AddCXXStdlibLibArgs(const ArgList &Args,
260  ArgStringList &CmdArgs) const {
261  bool Profiling = Args.hasArg(options::OPT_pg);
262 
263  CmdArgs.push_back(Profiling ? "-lc++_p" : "-lc++");
264  CmdArgs.push_back(Profiling ? "-lc++abi_p" : "-lc++abi");
265 }
266 
268  return new tools::openbsd::Assembler(*this);
269 }
270 
271 Tool *OpenBSD::buildLinker() const { return new tools::openbsd::Linker(*this); }
272 
273 void OpenBSD::addClangTargetOptions(const ArgList &DriverArgs,
274  ArgStringList &CC1Args,
275  Action::OffloadKind) const {
276  // Support for .init_array is still new (Aug 2016).
277  if (!DriverArgs.hasFlag(options::OPT_fuse_init_array,
278  options::OPT_fno_use_init_array, false))
279  CC1Args.push_back("-fno-use-init-array");
280 }
bool addXRayRuntime(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
const char * getSparcAsmModeForCPU(llvm::StringRef Name, const llvm::Triple &Triple)
Tool * buildLinker() const override
Definition: OpenBSD.cpp:271
std::string GetLinkerPath() const
Returns the linker path, respecting the -fuse-ld= argument to determine the linker suffix or name...
Definition: ToolChain.cpp:549
std::string getCPUName(const llvm::opt::ArgList &Args, const llvm::Triple &T, bool FromAs=false)
const char * getFilename() const
Definition: InputInfo.h:83
void linkSanitizerRuntimeDeps(const ToolChain &TC, llvm::opt::ArgStringList &CmdArgs)
void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, Action::OffloadKind DeviceOffloadKind) const override
Add options that need to be passed to cc1 for this target.
Definition: OpenBSD.cpp:273
bool addSanitizerRuntimes(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:541
path_list & getFilePaths()
Definition: ToolChain.h:242
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
void linkXRayRuntimeDeps(const ToolChain &TC, llvm::opt::ArgStringList &CmdArgs)
void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const override
AddCXXStdlibLibArgs - Add the system specific linker arguments to use for the given C++ standard libr...
Definition: OpenBSD.cpp:259
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs...
Definition: OpenBSD.cpp:24
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:205
void AddAssemblerKPIC(const ToolChain &ToolChain, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:220
void AddFilePathLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddFilePathLibArgs - Add each thing in getFilePaths() as a "-L" option.
Definition: ToolChain.cpp:945
const Driver & getDriver() const
Definition: ToolChain.h:204
Tool * buildAssembler() const override
Definition: OpenBSD.cpp:267
bool CCCIsCXX() const
Whether the driver should follow g++ like behavior.
Definition: Driver.h:173
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs...
Definition: OpenBSD.cpp:96
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:922
Dataflow Directional Tag Classes.
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
void claimNoWarnArgs(const llvm::opt::ArgList &Args)
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:988
StringRef getGnuCompatibleMipsABIName(StringRef ABI)
Definition: Mips.cpp:143
bool isNothing() const
Definition: InputInfo.h:74
const char * getCompilerRTArgString(const llvm::opt::ArgList &Args, StringRef Component, FileType Type=ToolChain::FT_Static) const
Definition: ToolChain.cpp:462
static constexpr ResponseFileSupport AtFileCurCP()
Definition: Job.h:91
bool isFilename() const
Definition: InputInfo.h:75
void getMipsCPUAndABI(const llvm::opt::ArgList &Args, const llvm::Triple &Triple, StringRef &CPUName, StringRef &ABIName)
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:90