clang  8.0.0svn
OpenBSD.cpp
Go to the documentation of this file.
1 //===--- OpenBSD.cpp - OpenBSD 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 
10 #include "OpenBSD.h"
11 #include "Arch/Mips.h"
12 #include "Arch/Sparc.h"
13 #include "CommonArgs.h"
15 #include "clang/Driver/Options.h"
17 #include "llvm/Option/ArgList.h"
18 
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 
26  const InputInfo &Output,
27  const InputInfoList &Inputs,
28  const ArgList &Args,
29  const char *LinkingOutput) const {
30  claimNoWarnArgs(Args);
31  ArgStringList CmdArgs;
32 
33  switch (getToolChain().getArch()) {
34  case llvm::Triple::x86:
35  // When building 32-bit code on OpenBSD/amd64, we have to explicitly
36  // instruct as in the base system to assemble 32-bit code.
37  CmdArgs.push_back("--32");
38  break;
39 
40  case llvm::Triple::ppc:
41  CmdArgs.push_back("-mppc");
42  CmdArgs.push_back("-many");
43  break;
44 
45  case llvm::Triple::sparc:
46  case llvm::Triple::sparcel: {
47  CmdArgs.push_back("-32");
48  std::string CPU = getCPUName(Args, getToolChain().getTriple());
49  CmdArgs.push_back(sparc::getSparcAsmModeForCPU(CPU, getToolChain().getTriple()));
50  AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
51  break;
52  }
53 
54  case llvm::Triple::sparcv9: {
55  CmdArgs.push_back("-64");
56  std::string CPU = getCPUName(Args, getToolChain().getTriple());
57  CmdArgs.push_back(sparc::getSparcAsmModeForCPU(CPU, getToolChain().getTriple()));
58  AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
59  break;
60  }
61 
62  case llvm::Triple::mips64:
63  case llvm::Triple::mips64el: {
64  StringRef CPUName;
65  StringRef ABIName;
66  mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
67 
68  CmdArgs.push_back("-mabi");
69  CmdArgs.push_back(mips::getGnuCompatibleMipsABIName(ABIName).data());
70 
71  if (getToolChain().getTriple().isLittleEndian())
72  CmdArgs.push_back("-EL");
73  else
74  CmdArgs.push_back("-EB");
75 
76  AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
77  break;
78  }
79 
80  default:
81  break;
82  }
83 
84  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
85 
86  CmdArgs.push_back("-o");
87  CmdArgs.push_back(Output.getFilename());
88 
89  for (const auto &II : Inputs)
90  CmdArgs.push_back(II.getFilename());
91 
92  const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
93  C.addCommand(llvm::make_unique<Command>(JA, *this, 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))
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", false));
193  linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
194  }
195  if (NeedsXRayDeps) {
196  CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins", false));
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(
231  !NeedsSanitizerDeps ? ToolChain.GetLinkerPath()
232  : ToolChain.GetProgramPath("ld.lld"));
233  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
234 }
235 
236 SanitizerMask OpenBSD::getSupportedSanitizers() const {
237  const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
238  const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
239 
240  // For future use, only UBsan at the moment
242 
243  if (IsX86 || IsX86_64) {
244  Res |= SanitizerKind::Vptr;
245  Res |= SanitizerKind::Fuzzer;
246  Res |= SanitizerKind::FuzzerNoLink;
247  }
248 
249  return Res;
250 }
251 
252 /// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
253 
254 OpenBSD::OpenBSD(const Driver &D, const llvm::Triple &Triple,
255  const ArgList &Args)
256  : Generic_ELF(D, Triple, Args) {
257  getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
258 }
259 
260 void OpenBSD::AddCXXStdlibLibArgs(const ArgList &Args,
261  ArgStringList &CmdArgs) const {
262  bool Profiling = Args.hasArg(options::OPT_pg);
263 
264  CmdArgs.push_back(Profiling ? "-lc++_p" : "-lc++");
265  CmdArgs.push_back(Profiling ? "-lc++abi_p" : "-lc++abi");
266 }
267 
269  return new tools::openbsd::Assembler(*this);
270 }
271 
272 Tool *OpenBSD::buildLinker() const { return new tools::openbsd::Linker(*this); }
bool addXRayRuntime(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
const char * getSparcAsmModeForCPU(llvm::StringRef Name, const llvm::Triple &Triple)
const char * getCompilerRTArgString(const llvm::opt::ArgList &Args, StringRef Component, bool Shared=false) const
Definition: ToolChain.cpp:392
Tool * buildLinker() const override
Definition: OpenBSD.cpp:272
std::string GetLinkerPath() const
Returns the linker path, respecting the -fuse-ld= argument to determine the linker suffix or name...
Definition: ToolChain.cpp:435
std::string getCPUName(const llvm::opt::ArgList &Args, const llvm::Triple &T, bool FromAs=false)
const char * getFilename() const
Definition: InputInfo.h:84
void linkSanitizerRuntimeDeps(const ToolChain &TC, llvm::opt::ArgStringList &CmdArgs)
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:23
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:427
path_list & getFilePaths()
Definition: ToolChain.h:222
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:58
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:260
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:25
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:206
void AddAssemblerKPIC(const ToolChain &ToolChain, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:200
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:774
const Driver & getDriver() const
Definition: ToolChain.h:184
Tool * buildAssembler() const override
Definition: OpenBSD.cpp:268
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:751
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:34
void claimNoWarnArgs(const llvm::opt::ArgList &Args)
uint64_t SanitizerMask
Definition: Sanitizers.h:26
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:46
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:814
StringRef getGnuCompatibleMipsABIName(StringRef ABI)
Definition: Mips.cpp:139
bool isNothing() const
Definition: InputInfo.h:75
std::string GetProgramPath(const char *Name) const
Definition: ToolChain.cpp:431
bool isFilename() const
Definition: InputInfo.h:76
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:87