clang  6.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"
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().getArch() == llvm::Triple::mips64)
71  CmdArgs.push_back("-EB");
72  else
73  CmdArgs.push_back("-EL");
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(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
93 }
94 
96  const InputInfo &Output,
97  const InputInfoList &Inputs,
98  const ArgList &Args,
99  const char *LinkingOutput) const {
100  const Driver &D = getToolChain().getDriver();
101  ArgStringList CmdArgs;
102 
103  // Silence warning for "clang -g foo.o -o foo"
104  Args.ClaimAllArgs(options::OPT_g_Group);
105  // and "clang -emit-llvm foo.o -o foo"
106  Args.ClaimAllArgs(options::OPT_emit_llvm);
107  // and for "clang -w foo.o -o foo". Other warning options are already
108  // handled somewhere else.
109  Args.ClaimAllArgs(options::OPT_w);
110 
111  if (getToolChain().getArch() == llvm::Triple::mips64)
112  CmdArgs.push_back("-EB");
113  else if (getToolChain().getArch() == llvm::Triple::mips64el)
114  CmdArgs.push_back("-EL");
115 
116  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_shared)) {
117  CmdArgs.push_back("-e");
118  CmdArgs.push_back("__start");
119  }
120 
121  CmdArgs.push_back("--eh-frame-hdr");
122  if (Args.hasArg(options::OPT_static)) {
123  CmdArgs.push_back("-Bstatic");
124  } else {
125  if (Args.hasArg(options::OPT_rdynamic))
126  CmdArgs.push_back("-export-dynamic");
127  CmdArgs.push_back("-Bdynamic");
128  if (Args.hasArg(options::OPT_shared)) {
129  CmdArgs.push_back("-shared");
130  } else {
131  CmdArgs.push_back("-dynamic-linker");
132  CmdArgs.push_back("/usr/libexec/ld.so");
133  }
134  }
135 
136  if (Args.hasArg(options::OPT_pie))
137  CmdArgs.push_back("-pie");
138  if (Args.hasArg(options::OPT_nopie))
139  CmdArgs.push_back("-nopie");
140 
141  if (Output.isFilename()) {
142  CmdArgs.push_back("-o");
143  CmdArgs.push_back(Output.getFilename());
144  } else {
145  assert(Output.isNothing() && "Invalid output.");
146  }
147 
148  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
149  if (!Args.hasArg(options::OPT_shared)) {
150  if (Args.hasArg(options::OPT_pg))
151  CmdArgs.push_back(
152  Args.MakeArgString(getToolChain().GetFilePath("gcrt0.o")));
153  else if (Args.hasArg(options::OPT_static) &&
154  !Args.hasArg(options::OPT_nopie))
155  CmdArgs.push_back(
156  Args.MakeArgString(getToolChain().GetFilePath("rcrt0.o")));
157  else
158  CmdArgs.push_back(
159  Args.MakeArgString(getToolChain().GetFilePath("crt0.o")));
160  CmdArgs.push_back(
161  Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
162  } else {
163  CmdArgs.push_back(
164  Args.MakeArgString(getToolChain().GetFilePath("crtbeginS.o")));
165  }
166  }
167 
168  std::string Triple = getToolChain().getTripleString();
169  if (Triple.substr(0, 6) == "x86_64")
170  Triple.replace(0, 6, "amd64");
171  CmdArgs.push_back(
172  Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple + "/4.2.1"));
173 
174  Args.AddAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group,
175  options::OPT_e, options::OPT_s, options::OPT_t,
176  options::OPT_Z_Flag, options::OPT_r});
177 
178  AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
179 
180  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
181  if (D.CCCIsCXX()) {
182  if (getToolChain().ShouldLinkCXXStdlib(Args))
183  getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
184  if (Args.hasArg(options::OPT_pg))
185  CmdArgs.push_back("-lm_p");
186  else
187  CmdArgs.push_back("-lm");
188  }
189 
190  // FIXME: For some reason GCC passes -lgcc before adding
191  // the default system libraries. Just mimic this for now.
192  CmdArgs.push_back("-lgcc");
193 
194  if (Args.hasArg(options::OPT_pthread)) {
195  if (!Args.hasArg(options::OPT_shared) && Args.hasArg(options::OPT_pg))
196  CmdArgs.push_back("-lpthread_p");
197  else
198  CmdArgs.push_back("-lpthread");
199  }
200 
201  if (!Args.hasArg(options::OPT_shared)) {
202  if (Args.hasArg(options::OPT_pg))
203  CmdArgs.push_back("-lc_p");
204  else
205  CmdArgs.push_back("-lc");
206  }
207 
208  CmdArgs.push_back("-lgcc");
209  }
210 
211  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
212  if (!Args.hasArg(options::OPT_shared))
213  CmdArgs.push_back(
214  Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
215  else
216  CmdArgs.push_back(
217  Args.MakeArgString(getToolChain().GetFilePath("crtendS.o")));
218  }
219 
220  const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
221  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
222 }
223 
224 /// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
225 
226 OpenBSD::OpenBSD(const Driver &D, const llvm::Triple &Triple,
227  const ArgList &Args)
228  : Generic_ELF(D, Triple, Args) {
229  getFilePaths().push_back(getDriver().Dir + "/../lib");
230  getFilePaths().push_back("/usr/lib");
231 }
232 
234  return new tools::openbsd::Assembler(*this);
235 }
236 
237 Tool *OpenBSD::buildLinker() const { return new tools::openbsd::Linker(*this); }
const char * getSparcAsmModeForCPU(llvm::StringRef Name, const llvm::Triple &Triple)
Tool * buildLinker() const override
Definition: OpenBSD.cpp:237
std::string getCPUName(const llvm::opt::ArgList &Args, const llvm::Triple &T, bool FromAs=false)
const char * getFilename() const
Definition: InputInfo.h:84
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:23
path_list & getFilePaths()
Definition: ToolChain.h:202
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:65
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:189
void AddAssemblerKPIC(const ToolChain &ToolChain, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
const Driver & getDriver() const
Definition: ToolChain.h:167
Tool * buildAssembler() const override
Definition: OpenBSD.cpp:233
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:95
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)
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:34
StringRef getGnuCompatibleMipsABIName(StringRef ABI)
Definition: Mips.cpp:138
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)