clang  6.0.0svn
FreeBSD.cpp
Go to the documentation of this file.
1 //===--- FreeBSD.cpp - FreeBSD 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 "FreeBSD.h"
11 #include "Arch/ARM.h"
12 #include "Arch/Mips.h"
13 #include "Arch/Sparc.h"
14 #include "CommonArgs.h"
17 #include "clang/Driver/Options.h"
19 #include "llvm/Option/ArgList.h"
20 
21 using namespace clang::driver;
22 using namespace clang::driver::tools;
23 using namespace clang::driver::toolchains;
24 using namespace clang;
25 using namespace llvm::opt;
26 
28  const InputInfo &Output,
29  const InputInfoList &Inputs,
30  const ArgList &Args,
31  const char *LinkingOutput) const {
32  claimNoWarnArgs(Args);
33  ArgStringList CmdArgs;
34 
35  // When building 32-bit code on FreeBSD/amd64, we have to explicitly
36  // instruct as in the base system to assemble 32-bit code.
37  switch (getToolChain().getArch()) {
38  default:
39  break;
40  case llvm::Triple::x86:
41  CmdArgs.push_back("--32");
42  break;
43  case llvm::Triple::ppc:
44  CmdArgs.push_back("-a32");
45  break;
46  case llvm::Triple::mips:
47  case llvm::Triple::mipsel:
48  case llvm::Triple::mips64:
49  case llvm::Triple::mips64el: {
50  StringRef CPUName;
51  StringRef ABIName;
52  mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
53 
54  CmdArgs.push_back("-march");
55  CmdArgs.push_back(CPUName.data());
56 
57  CmdArgs.push_back("-mabi");
58  CmdArgs.push_back(mips::getGnuCompatibleMipsABIName(ABIName).data());
59 
60  if (getToolChain().getArch() == llvm::Triple::mips ||
61  getToolChain().getArch() == llvm::Triple::mips64)
62  CmdArgs.push_back("-EB");
63  else
64  CmdArgs.push_back("-EL");
65 
66  if (Arg *A = Args.getLastArg(options::OPT_G)) {
67  StringRef v = A->getValue();
68  CmdArgs.push_back(Args.MakeArgString("-G" + v));
69  A->claim();
70  }
71 
72  AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
73  break;
74  }
75  case llvm::Triple::arm:
76  case llvm::Triple::armeb:
77  case llvm::Triple::thumb:
78  case llvm::Triple::thumbeb: {
79  arm::FloatABI ABI = arm::getARMFloatABI(getToolChain(), Args);
80 
81  if (ABI == arm::FloatABI::Hard)
82  CmdArgs.push_back("-mfpu=vfp");
83  else
84  CmdArgs.push_back("-mfpu=softvfp");
85 
86  switch (getToolChain().getTriple().getEnvironment()) {
87  case llvm::Triple::GNUEABIHF:
88  case llvm::Triple::GNUEABI:
89  case llvm::Triple::EABI:
90  CmdArgs.push_back("-meabi=5");
91  break;
92 
93  default:
94  CmdArgs.push_back("-matpcs");
95  }
96  break;
97  }
98  case llvm::Triple::sparc:
99  case llvm::Triple::sparcel:
100  case llvm::Triple::sparcv9: {
101  std::string CPU = getCPUName(Args, getToolChain().getTriple());
102  CmdArgs.push_back(sparc::getSparcAsmModeForCPU(CPU, getToolChain().getTriple()));
103  AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
104  break;
105  }
106  }
107 
108  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
109 
110  CmdArgs.push_back("-o");
111  CmdArgs.push_back(Output.getFilename());
112 
113  for (const auto &II : Inputs)
114  CmdArgs.push_back(II.getFilename());
115 
116  const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
117  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
118 }
119 
121  const InputInfo &Output,
122  const InputInfoList &Inputs,
123  const ArgList &Args,
124  const char *LinkingOutput) const {
126  static_cast<const toolchains::FreeBSD &>(getToolChain());
127  const Driver &D = ToolChain.getDriver();
128  const llvm::Triple::ArchType Arch = ToolChain.getArch();
129  const bool IsPIE =
130  !Args.hasArg(options::OPT_shared) &&
131  (Args.hasArg(options::OPT_pie) || ToolChain.isPIEDefault());
132  ArgStringList CmdArgs;
133 
134  // Silence warning for "clang -g foo.o -o foo"
135  Args.ClaimAllArgs(options::OPT_g_Group);
136  // and "clang -emit-llvm foo.o -o foo"
137  Args.ClaimAllArgs(options::OPT_emit_llvm);
138  // and for "clang -w foo.o -o foo". Other warning options are already
139  // handled somewhere else.
140  Args.ClaimAllArgs(options::OPT_w);
141 
142  if (!D.SysRoot.empty())
143  CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
144 
145  if (IsPIE)
146  CmdArgs.push_back("-pie");
147 
148  CmdArgs.push_back("--eh-frame-hdr");
149  if (Args.hasArg(options::OPT_static)) {
150  CmdArgs.push_back("-Bstatic");
151  } else {
152  if (Args.hasArg(options::OPT_rdynamic))
153  CmdArgs.push_back("-export-dynamic");
154  if (Args.hasArg(options::OPT_shared)) {
155  CmdArgs.push_back("-Bshareable");
156  } else {
157  CmdArgs.push_back("-dynamic-linker");
158  CmdArgs.push_back("/libexec/ld-elf.so.1");
159  }
160  if (ToolChain.getTriple().getOSMajorVersion() >= 9) {
161  if (Arch == llvm::Triple::arm || Arch == llvm::Triple::sparc ||
162  Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) {
163  CmdArgs.push_back("--hash-style=both");
164  }
165  }
166  CmdArgs.push_back("--enable-new-dtags");
167  }
168 
169  // When building 32-bit code on FreeBSD/amd64, we have to explicitly
170  // instruct ld in the base system to link 32-bit code.
171  if (Arch == llvm::Triple::x86) {
172  CmdArgs.push_back("-m");
173  CmdArgs.push_back("elf_i386_fbsd");
174  }
175 
176  if (Arch == llvm::Triple::ppc) {
177  CmdArgs.push_back("-m");
178  CmdArgs.push_back("elf32ppc_fbsd");
179  }
180 
181  if (Arg *A = Args.getLastArg(options::OPT_G)) {
182  if (ToolChain.getArch() == llvm::Triple::mips ||
183  ToolChain.getArch() == llvm::Triple::mipsel ||
184  ToolChain.getArch() == llvm::Triple::mips64 ||
185  ToolChain.getArch() == llvm::Triple::mips64el) {
186  StringRef v = A->getValue();
187  CmdArgs.push_back(Args.MakeArgString("-G" + v));
188  A->claim();
189  }
190  }
191 
192  if (Output.isFilename()) {
193  CmdArgs.push_back("-o");
194  CmdArgs.push_back(Output.getFilename());
195  } else {
196  assert(Output.isNothing() && "Invalid output.");
197  }
198 
199  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
200  const char *crt1 = nullptr;
201  if (!Args.hasArg(options::OPT_shared)) {
202  if (Args.hasArg(options::OPT_pg))
203  crt1 = "gcrt1.o";
204  else if (IsPIE)
205  crt1 = "Scrt1.o";
206  else
207  crt1 = "crt1.o";
208  }
209  if (crt1)
210  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
211 
212  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
213 
214  const char *crtbegin = nullptr;
215  if (Args.hasArg(options::OPT_static))
216  crtbegin = "crtbeginT.o";
217  else if (Args.hasArg(options::OPT_shared) || IsPIE)
218  crtbegin = "crtbeginS.o";
219  else
220  crtbegin = "crtbegin.o";
221 
222  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
223  }
224 
225  Args.AddAllArgs(CmdArgs, options::OPT_L);
226  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
227  Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
228  Args.AddAllArgs(CmdArgs, options::OPT_e);
229  Args.AddAllArgs(CmdArgs, options::OPT_s);
230  Args.AddAllArgs(CmdArgs, options::OPT_t);
231  Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
232  Args.AddAllArgs(CmdArgs, options::OPT_r);
233 
234  if (D.isUsingLTO())
235  AddGoldPlugin(ToolChain, Args, CmdArgs, D.getLTOMode() == LTOK_Thin, D);
236 
237  bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
238  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
239 
240  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
241  addOpenMPRuntime(CmdArgs, ToolChain, Args);
242  if (D.CCCIsCXX()) {
243  if (ToolChain.ShouldLinkCXXStdlib(Args))
244  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
245  if (Args.hasArg(options::OPT_pg))
246  CmdArgs.push_back("-lm_p");
247  else
248  CmdArgs.push_back("-lm");
249  }
250  if (NeedsSanitizerDeps)
251  linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
252  // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
253  // the default system libraries. Just mimic this for now.
254  if (Args.hasArg(options::OPT_pg))
255  CmdArgs.push_back("-lgcc_p");
256  else
257  CmdArgs.push_back("-lgcc");
258  if (Args.hasArg(options::OPT_static)) {
259  CmdArgs.push_back("-lgcc_eh");
260  } else if (Args.hasArg(options::OPT_pg)) {
261  CmdArgs.push_back("-lgcc_eh_p");
262  } else {
263  CmdArgs.push_back("--as-needed");
264  CmdArgs.push_back("-lgcc_s");
265  CmdArgs.push_back("--no-as-needed");
266  }
267 
268  if (Args.hasArg(options::OPT_pthread)) {
269  if (Args.hasArg(options::OPT_pg))
270  CmdArgs.push_back("-lpthread_p");
271  else
272  CmdArgs.push_back("-lpthread");
273  }
274 
275  if (Args.hasArg(options::OPT_pg)) {
276  if (Args.hasArg(options::OPT_shared))
277  CmdArgs.push_back("-lc");
278  else
279  CmdArgs.push_back("-lc_p");
280  CmdArgs.push_back("-lgcc_p");
281  } else {
282  CmdArgs.push_back("-lc");
283  CmdArgs.push_back("-lgcc");
284  }
285 
286  if (Args.hasArg(options::OPT_static)) {
287  CmdArgs.push_back("-lgcc_eh");
288  } else if (Args.hasArg(options::OPT_pg)) {
289  CmdArgs.push_back("-lgcc_eh_p");
290  } else {
291  CmdArgs.push_back("--as-needed");
292  CmdArgs.push_back("-lgcc_s");
293  CmdArgs.push_back("--no-as-needed");
294  }
295  }
296 
297  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
298  if (Args.hasArg(options::OPT_shared) || IsPIE)
299  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o")));
300  else
301  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
302  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
303  }
304 
305  ToolChain.addProfileRTLibs(Args, CmdArgs);
306 
307  const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
308  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
309 }
310 
311 /// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
312 
313 FreeBSD::FreeBSD(const Driver &D, const llvm::Triple &Triple,
314  const ArgList &Args)
315  : Generic_ELF(D, Triple, Args) {
316 
317  // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
318  // back to '/usr/lib' if it doesn't exist.
319  if ((Triple.getArch() == llvm::Triple::x86 ||
320  Triple.getArch() == llvm::Triple::ppc) &&
321  D.getVFS().exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
322  getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
323  else
324  getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
325 }
326 
328  if (getTriple().getOSMajorVersion() >= 10)
329  return ToolChain::CST_Libcxx;
331 }
332 
334  const llvm::opt::ArgList &DriverArgs,
335  llvm::opt::ArgStringList &CC1Args) const {
336  addLibStdCXXIncludePaths(getDriver().SysRoot, "/usr/include/c++/4.2", "", "",
337  "", "", DriverArgs, CC1Args);
338 }
339 
340 void FreeBSD::AddCXXStdlibLibArgs(const ArgList &Args,
341  ArgStringList &CmdArgs) const {
343  bool Profiling = Args.hasArg(options::OPT_pg);
344 
345  switch (Type) {
347  CmdArgs.push_back(Profiling ? "-lc++_p" : "-lc++");
348  break;
349 
351  CmdArgs.push_back(Profiling ? "-lstdc++_p" : "-lstdc++");
352  break;
353  }
354 }
355 
357  return new tools::freebsd::Assembler(*this);
358 }
359 
360 Tool *FreeBSD::buildLinker() const { return new tools::freebsd::Linker(*this); }
361 
362 llvm::ExceptionHandling FreeBSD::GetExceptionModel(const ArgList &Args) const {
363  // FreeBSD uses SjLj exceptions on ARM oabi.
364  switch (getTriple().getEnvironment()) {
365  case llvm::Triple::GNUEABIHF:
366  case llvm::Triple::GNUEABI:
367  case llvm::Triple::EABI:
369  default:
370  if (getTriple().getArch() == llvm::Triple::arm ||
371  getTriple().getArch() == llvm::Triple::thumb)
372  return llvm::ExceptionHandling::SjLj;
374  }
375 }
376 
377 bool FreeBSD::HasNativeLLVMSupport() const { return true; }
378 
380 
382  const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
383  const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
384  const bool IsMIPS64 = getTriple().getArch() == llvm::Triple::mips64 ||
385  getTriple().getArch() == llvm::Triple::mips64el;
387  Res |= SanitizerKind::Address;
388  Res |= SanitizerKind::Vptr;
389  if (IsX86_64 || IsMIPS64) {
390  Res |= SanitizerKind::Leak;
391  Res |= SanitizerKind::Thread;
392  }
393  if (IsX86 || IsX86_64) {
394  Res |= SanitizerKind::SafeStack;
395  }
396  return Res;
397 }
bool HasNativeLLVMSupport() const override
HasNativeLTOLinker - Check whether the linker and related tools have native LLVM support.
Definition: FreeBSD.cpp:377
const char * getSparcAsmModeForCPU(llvm::StringRef Name, const llvm::Triple &Triple)
CXXStdlibType GetDefaultCXXStdlibType() const override
Definition: FreeBSD.cpp:327
bool isUsingLTO() const
Returns true if we are performing any kind of LTO.
Definition: Driver.h:490
The base class of the type hierarchy.
Definition: Type.h:1353
LTOKind getLTOMode() const
Get the specific kind of LTO being performed.
Definition: Driver.h:493
virtual void addProfileRTLibs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
addProfileRTLibs - When -fprofile-instr-profile is specified, try to pass a suitable profile runtime ...
Definition: ToolChain.cpp:619
FloatABI getARMFloatABI(const ToolChain &TC, const llvm::opt::ArgList &Args)
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:391
void AddGoldPlugin(const ToolChain &ToolChain, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, bool IsThinLTO, const Driver &D)
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
Tool * buildLinker() const override
Definition: FreeBSD.cpp:360
bool addOpenMPRuntime(llvm::opt::ArgStringList &CmdArgs, const ToolChain &TC, const llvm::opt::ArgList &Args, bool IsOffloadingHost=false, bool GompNeedsRT=false)
Returns true, if an OpenMP runtime has been added.
Tool * buildAssembler() const override
Definition: FreeBSD.cpp:356
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:189
do v
Definition: arm_acle.h:78
void AddAssemblerKPIC(const ToolChain &ToolChain, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:183
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:740
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: FreeBSD.cpp:340
vfs::FileSystem & getVFS() const
Definition: Driver.h:284
const Driver & getDriver() const
Definition: ToolChain.h:167
bool CCCIsCXX() const
Whether the driver should follow g++ like behavior.
Definition: Driver.h:174
bool isPIEDefault() const override
Test whether this toolchain defaults to PIE.
Definition: FreeBSD.cpp:379
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:717
Dataflow Directional Tag Classes.
uint64_t SanitizerMask
Definition: Sanitizers.h:24
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
std::string SysRoot
sysroot, if present
Definition: Driver.h:149
Tool - Information on a specific compilation tool.
Definition: Tool.h:34
Defines the virtual file system interface vfs::FileSystem.
const SanitizerArgs & getSanitizerArgs() const
Definition: ToolChain.cpp:105
void claimNoWarnArgs(const llvm::opt::ArgList &Args)
bool exists(const Twine &Path)
Check whether a file exists. Provided for convenience.
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:34
const llvm::Triple & getTriple() const
Definition: ToolChain.h:169
llvm::ExceptionHandling GetExceptionModel(const llvm::opt::ArgList &Args) const override
GetExceptionModel - Return the tool chain exception model.
Definition: FreeBSD.cpp:362
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:776
StringRef getGnuCompatibleMipsABIName(StringRef ABI)
Definition: Mips.cpp:138
void addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Definition: FreeBSD.cpp:333
bool isNothing() const
Definition: InputInfo.h:75
bool isFilename() const
Definition: InputInfo.h:76
virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:645
SanitizerMask getSupportedSanitizers() const override
Return sanitizers which are available in this toolchain.
Definition: FreeBSD.cpp:381
bool addLibStdCXXIncludePaths(Twine Base, Twine Suffix, StringRef GCCTriple, StringRef GCCMultiarchTriple, StringRef TargetMultiarchTriple, Twine IncludeSuffix, const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Helper to add the variant paths of a libstdc++ installation.
Definition: Gnu.cpp:2294
void getMipsCPUAndABI(const llvm::opt::ArgList &Args, const llvm::Triple &Triple, StringRef &CPUName, StringRef &ABIName)
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: FreeBSD.cpp:27
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:73
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: FreeBSD.cpp:120