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 (getToolChain().getArch() == llvm::Triple::mips64)
115  CmdArgs.push_back("-EB");
116  else if (getToolChain().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  if (!Args.hasArg(options::OPT_shared)) {
153  if (Args.hasArg(options::OPT_pg))
154  CmdArgs.push_back(
155  Args.MakeArgString(getToolChain().GetFilePath("gcrt0.o")));
156  else if (Args.hasArg(options::OPT_static) &&
157  !Args.hasArg(options::OPT_nopie))
158  CmdArgs.push_back(
159  Args.MakeArgString(getToolChain().GetFilePath("rcrt0.o")));
160  else
161  CmdArgs.push_back(
162  Args.MakeArgString(getToolChain().GetFilePath("crt0.o")));
163  CmdArgs.push_back(
164  Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
165  } else {
166  CmdArgs.push_back(
167  Args.MakeArgString(getToolChain().GetFilePath("crtbeginS.o")));
168  }
169  }
170 
171  std::string Triple = getToolChain().getTripleString();
172  if (Triple.substr(0, 6) == "x86_64")
173  Triple.replace(0, 6, "amd64");
174  CmdArgs.push_back(
175  Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple + "/4.2.1"));
176  CmdArgs.push_back(Args.MakeArgString("-L/usr/lib"));
177 
178  Args.AddAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group,
179  options::OPT_e, options::OPT_s, options::OPT_t,
180  options::OPT_Z_Flag, options::OPT_r});
181 
182  bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
183  bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
184  AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
185 
186  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
187  if (D.CCCIsCXX()) {
188  if (getToolChain().ShouldLinkCXXStdlib(Args))
189  getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
190  if (Args.hasArg(options::OPT_pg))
191  CmdArgs.push_back("-lm_p");
192  else
193  CmdArgs.push_back("-lm");
194  }
195  if (NeedsSanitizerDeps) {
196  CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins", false));
197  linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
198  }
199  if (NeedsXRayDeps) {
200  CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins", false));
201  linkXRayRuntimeDeps(ToolChain, CmdArgs);
202  }
203  // FIXME: For some reason GCC passes -lgcc before adding
204  // the default system libraries. Just mimic this for now.
205  CmdArgs.push_back("-lgcc");
206 
207  if (Args.hasArg(options::OPT_pthread)) {
208  if (!Args.hasArg(options::OPT_shared) && Args.hasArg(options::OPT_pg))
209  CmdArgs.push_back("-lpthread_p");
210  else
211  CmdArgs.push_back("-lpthread");
212  }
213 
214  if (!Args.hasArg(options::OPT_shared)) {
215  if (Args.hasArg(options::OPT_pg))
216  CmdArgs.push_back("-lc_p");
217  else
218  CmdArgs.push_back("-lc");
219  }
220 
221  CmdArgs.push_back("-lgcc");
222  }
223 
224  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
225  if (!Args.hasArg(options::OPT_shared))
226  CmdArgs.push_back(
227  Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
228  else
229  CmdArgs.push_back(
230  Args.MakeArgString(getToolChain().GetFilePath("crtendS.o")));
231  }
232 
233  const char *Exec = Args.MakeArgString(
234  !NeedsSanitizerDeps ? getToolChain().GetLinkerPath()
235  : getToolChain().GetProgramPath("ld.lld"));
236  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
237 }
238 
239 SanitizerMask OpenBSD::getSupportedSanitizers() const {
240  const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
241  const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
242 
243  // For future use, only UBsan at the moment
245 
246  if (IsX86 || IsX86_64) {
247  Res |= SanitizerKind::Vptr;
248  Res |= SanitizerKind::Fuzzer;
249  Res |= SanitizerKind::FuzzerNoLink;
250  }
251 
252  return Res;
253 }
254 
255 /// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
256 
257 OpenBSD::OpenBSD(const Driver &D, const llvm::Triple &Triple,
258  const ArgList &Args)
259  : Generic_ELF(D, Triple, Args) {
260  getFilePaths().push_back(getDriver().Dir + "/../lib");
261  getFilePaths().push_back("/usr/lib");
262 }
263 
264 void OpenBSD::AddCXXStdlibLibArgs(const ArgList &Args,
265  ArgStringList &CmdArgs) const {
266  bool Profiling = Args.hasArg(options::OPT_pg);
267 
268  CmdArgs.push_back(Profiling ? "-lc++_p" : "-lc++");
269  CmdArgs.push_back(Profiling ? "-lc++abi_p" : "-lc++abi");
270 }
271 
273  return new tools::openbsd::Assembler(*this);
274 }
275 
276 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:381
Tool * buildLinker() const override
Definition: OpenBSD.cpp:276
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
path_list & getFilePaths()
Definition: ToolChain.h:216
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:264
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)
const Driver & getDriver() const
Definition: ToolChain.h:181
Tool * buildAssembler() const override
Definition: OpenBSD.cpp:272
bool CCCIsCXX() const
Whether the driver should follow g++ like behavior.
Definition: Driver.h:174
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
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:799
StringRef getGnuCompatibleMipsABIName(StringRef ABI)
Definition: Mips.cpp:136
bool isNothing() const
Definition: InputInfo.h:75
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:88