clang  8.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"
16 #include "clang/Driver/Options.h"
18 #include "llvm/Option/ArgList.h"
19 #include "llvm/Support/VirtualFileSystem.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().getTriple().isLittleEndian())
61  CmdArgs.push_back("-EL");
62  else
63  CmdArgs.push_back("-EB");
64 
65  if (Arg *A = Args.getLastArg(options::OPT_G)) {
66  StringRef v = A->getValue();
67  CmdArgs.push_back(Args.MakeArgString("-G" + v));
68  A->claim();
69  }
70 
71  AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
72  break;
73  }
74  case llvm::Triple::arm:
75  case llvm::Triple::armeb:
76  case llvm::Triple::thumb:
77  case llvm::Triple::thumbeb: {
78  arm::FloatABI ABI = arm::getARMFloatABI(getToolChain(), Args);
79 
80  if (ABI == arm::FloatABI::Hard)
81  CmdArgs.push_back("-mfpu=vfp");
82  else
83  CmdArgs.push_back("-mfpu=softvfp");
84 
85  switch (getToolChain().getTriple().getEnvironment()) {
86  case llvm::Triple::GNUEABIHF:
87  case llvm::Triple::GNUEABI:
88  case llvm::Triple::EABI:
89  CmdArgs.push_back("-meabi=5");
90  break;
91 
92  default:
93  CmdArgs.push_back("-matpcs");
94  }
95  break;
96  }
97  case llvm::Triple::sparc:
98  case llvm::Triple::sparcel:
99  case llvm::Triple::sparcv9: {
100  std::string CPU = getCPUName(Args, getToolChain().getTriple());
101  CmdArgs.push_back(sparc::getSparcAsmModeForCPU(CPU, getToolChain().getTriple()));
102  AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
103  break;
104  }
105  }
106 
107  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
108 
109  CmdArgs.push_back("-o");
110  CmdArgs.push_back(Output.getFilename());
111 
112  for (const auto &II : Inputs)
113  CmdArgs.push_back(II.getFilename());
114 
115  const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
116  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
117 }
118 
120  const InputInfo &Output,
121  const InputInfoList &Inputs,
122  const ArgList &Args,
123  const char *LinkingOutput) const {
125  static_cast<const toolchains::FreeBSD &>(getToolChain());
126  const Driver &D = ToolChain.getDriver();
127  const llvm::Triple::ArchType Arch = ToolChain.getArch();
128  const bool IsPIE =
129  !Args.hasArg(options::OPT_shared) &&
130  (Args.hasArg(options::OPT_pie) || ToolChain.isPIEDefault());
131  ArgStringList CmdArgs;
132 
133  // Silence warning for "clang -g foo.o -o foo"
134  Args.ClaimAllArgs(options::OPT_g_Group);
135  // and "clang -emit-llvm foo.o -o foo"
136  Args.ClaimAllArgs(options::OPT_emit_llvm);
137  // and for "clang -w foo.o -o foo". Other warning options are already
138  // handled somewhere else.
139  Args.ClaimAllArgs(options::OPT_w);
140 
141  if (!D.SysRoot.empty())
142  CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
143 
144  if (IsPIE)
145  CmdArgs.push_back("-pie");
146 
147  CmdArgs.push_back("--eh-frame-hdr");
148  if (Args.hasArg(options::OPT_static)) {
149  CmdArgs.push_back("-Bstatic");
150  } else {
151  if (Args.hasArg(options::OPT_rdynamic))
152  CmdArgs.push_back("-export-dynamic");
153  if (Args.hasArg(options::OPT_shared)) {
154  CmdArgs.push_back("-Bshareable");
155  } else {
156  CmdArgs.push_back("-dynamic-linker");
157  CmdArgs.push_back("/libexec/ld-elf.so.1");
158  }
159  if (ToolChain.getTriple().getOSMajorVersion() >= 9) {
160  if (Arch == llvm::Triple::arm || Arch == llvm::Triple::sparc ||
161  Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) {
162  CmdArgs.push_back("--hash-style=both");
163  }
164  }
165  CmdArgs.push_back("--enable-new-dtags");
166  }
167 
168  // Explicitly set the linker emulation for platforms that might not
169  // be the default emulation for the linker.
170  switch (Arch) {
171  case llvm::Triple::x86:
172  CmdArgs.push_back("-m");
173  CmdArgs.push_back("elf_i386_fbsd");
174  break;
175  case llvm::Triple::ppc:
176  CmdArgs.push_back("-m");
177  CmdArgs.push_back("elf32ppc_fbsd");
178  break;
179  case llvm::Triple::mips:
180  CmdArgs.push_back("-m");
181  CmdArgs.push_back("elf32btsmip_fbsd");
182  break;
183  case llvm::Triple::mipsel:
184  CmdArgs.push_back("-m");
185  CmdArgs.push_back("elf32ltsmip_fbsd");
186  break;
187  case llvm::Triple::mips64:
188  CmdArgs.push_back("-m");
189  if (tools::mips::hasMipsAbiArg(Args, "n32"))
190  CmdArgs.push_back("elf32btsmipn32_fbsd");
191  else
192  CmdArgs.push_back("elf64btsmip_fbsd");
193  break;
194  case llvm::Triple::mips64el:
195  CmdArgs.push_back("-m");
196  if (tools::mips::hasMipsAbiArg(Args, "n32"))
197  CmdArgs.push_back("elf32ltsmipn32_fbsd");
198  else
199  CmdArgs.push_back("elf64ltsmip_fbsd");
200  break;
201  default:
202  break;
203  }
204 
205  if (Arg *A = Args.getLastArg(options::OPT_G)) {
206  if (ToolChain.getTriple().isMIPS()) {
207  StringRef v = A->getValue();
208  CmdArgs.push_back(Args.MakeArgString("-G" + v));
209  A->claim();
210  }
211  }
212 
213  if (Output.isFilename()) {
214  CmdArgs.push_back("-o");
215  CmdArgs.push_back(Output.getFilename());
216  } else {
217  assert(Output.isNothing() && "Invalid output.");
218  }
219 
220  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
221  const char *crt1 = nullptr;
222  if (!Args.hasArg(options::OPT_shared)) {
223  if (Args.hasArg(options::OPT_pg))
224  crt1 = "gcrt1.o";
225  else if (IsPIE)
226  crt1 = "Scrt1.o";
227  else
228  crt1 = "crt1.o";
229  }
230  if (crt1)
231  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
232 
233  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
234 
235  const char *crtbegin = nullptr;
236  if (Args.hasArg(options::OPT_static))
237  crtbegin = "crtbeginT.o";
238  else if (Args.hasArg(options::OPT_shared) || IsPIE)
239  crtbegin = "crtbeginS.o";
240  else
241  crtbegin = "crtbegin.o";
242 
243  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
244  }
245 
246  Args.AddAllArgs(CmdArgs, options::OPT_L);
247  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
248  Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
249  Args.AddAllArgs(CmdArgs, options::OPT_e);
250  Args.AddAllArgs(CmdArgs, options::OPT_s);
251  Args.AddAllArgs(CmdArgs, options::OPT_t);
252  Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
253  Args.AddAllArgs(CmdArgs, options::OPT_r);
254 
255  if (D.isUsingLTO()) {
256  assert(!Inputs.empty() && "Must have at least one input.");
257  AddGoldPlugin(ToolChain, Args, CmdArgs, Output, Inputs[0],
258  D.getLTOMode() == LTOK_Thin);
259  }
260 
261  bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
262  bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
263  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
264 
265  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
266  addOpenMPRuntime(CmdArgs, ToolChain, Args);
267  if (D.CCCIsCXX()) {
268  if (ToolChain.ShouldLinkCXXStdlib(Args))
269  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
270  if (Args.hasArg(options::OPT_pg))
271  CmdArgs.push_back("-lm_p");
272  else
273  CmdArgs.push_back("-lm");
274  }
275  if (NeedsSanitizerDeps)
276  linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
277  if (NeedsXRayDeps)
278  linkXRayRuntimeDeps(ToolChain, CmdArgs);
279  // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
280  // the default system libraries. Just mimic this for now.
281  if (Args.hasArg(options::OPT_pg))
282  CmdArgs.push_back("-lgcc_p");
283  else
284  CmdArgs.push_back("-lgcc");
285  if (Args.hasArg(options::OPT_static)) {
286  CmdArgs.push_back("-lgcc_eh");
287  } else if (Args.hasArg(options::OPT_pg)) {
288  CmdArgs.push_back("-lgcc_eh_p");
289  } else {
290  CmdArgs.push_back("--as-needed");
291  CmdArgs.push_back("-lgcc_s");
292  CmdArgs.push_back("--no-as-needed");
293  }
294 
295  if (Args.hasArg(options::OPT_pthread)) {
296  if (Args.hasArg(options::OPT_pg))
297  CmdArgs.push_back("-lpthread_p");
298  else
299  CmdArgs.push_back("-lpthread");
300  }
301 
302  if (Args.hasArg(options::OPT_pg)) {
303  if (Args.hasArg(options::OPT_shared))
304  CmdArgs.push_back("-lc");
305  else
306  CmdArgs.push_back("-lc_p");
307  CmdArgs.push_back("-lgcc_p");
308  } else {
309  CmdArgs.push_back("-lc");
310  CmdArgs.push_back("-lgcc");
311  }
312 
313  if (Args.hasArg(options::OPT_static)) {
314  CmdArgs.push_back("-lgcc_eh");
315  } else if (Args.hasArg(options::OPT_pg)) {
316  CmdArgs.push_back("-lgcc_eh_p");
317  } else {
318  CmdArgs.push_back("--as-needed");
319  CmdArgs.push_back("-lgcc_s");
320  CmdArgs.push_back("--no-as-needed");
321  }
322  }
323 
324  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
325  if (Args.hasArg(options::OPT_shared) || IsPIE)
326  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o")));
327  else
328  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
329  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
330  }
331 
332  ToolChain.addProfileRTLibs(Args, CmdArgs);
333 
334  const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
335  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
336 }
337 
338 /// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
339 
340 FreeBSD::FreeBSD(const Driver &D, const llvm::Triple &Triple,
341  const ArgList &Args)
342  : Generic_ELF(D, Triple, Args) {
343 
344  // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
345  // back to '/usr/lib' if it doesn't exist.
346  if ((Triple.getArch() == llvm::Triple::x86 || Triple.isMIPS32() ||
347  Triple.getArch() == llvm::Triple::ppc) &&
348  D.getVFS().exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
349  getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
350  else
351  getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
352 }
353 
355  if (getTriple().getOSMajorVersion() >= 10)
356  return ToolChain::CST_Libcxx;
358 }
359 
361  const llvm::opt::ArgList &DriverArgs,
362  llvm::opt::ArgStringList &CC1Args) const {
363  addLibStdCXXIncludePaths(getDriver().SysRoot, "/usr/include/c++/4.2", "", "",
364  "", "", DriverArgs, CC1Args);
365 }
366 
367 void FreeBSD::AddCXXStdlibLibArgs(const ArgList &Args,
368  ArgStringList &CmdArgs) const {
370  bool Profiling = Args.hasArg(options::OPT_pg);
371 
372  switch (Type) {
374  CmdArgs.push_back(Profiling ? "-lc++_p" : "-lc++");
375  break;
376 
378  CmdArgs.push_back(Profiling ? "-lstdc++_p" : "-lstdc++");
379  break;
380  }
381 }
382 
384  return new tools::freebsd::Assembler(*this);
385 }
386 
387 Tool *FreeBSD::buildLinker() const { return new tools::freebsd::Linker(*this); }
388 
389 llvm::ExceptionHandling FreeBSD::GetExceptionModel(const ArgList &Args) const {
390  // FreeBSD uses SjLj exceptions on ARM oabi.
391  switch (getTriple().getEnvironment()) {
392  case llvm::Triple::GNUEABIHF:
393  case llvm::Triple::GNUEABI:
394  case llvm::Triple::EABI:
395  return llvm::ExceptionHandling::None;
396  default:
397  if (getTriple().getArch() == llvm::Triple::arm ||
398  getTriple().getArch() == llvm::Triple::thumb)
399  return llvm::ExceptionHandling::SjLj;
400  return llvm::ExceptionHandling::None;
401  }
402 }
403 
404 bool FreeBSD::HasNativeLLVMSupport() const { return true; }
405 
407 
409  const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
410  const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
411  const bool IsMIPS64 = getTriple().isMIPS64();
413  Res |= SanitizerKind::Address;
414  Res |= SanitizerKind::Vptr;
415  if (IsX86_64 || IsMIPS64) {
416  Res |= SanitizerKind::Leak;
417  Res |= SanitizerKind::Thread;
418  }
419  if (IsX86 || IsX86_64) {
420  Res |= SanitizerKind::Function;
421  Res |= SanitizerKind::SafeStack;
422  Res |= SanitizerKind::Fuzzer;
423  Res |= SanitizerKind::FuzzerNoLink;
424  }
425  if (IsX86_64)
426  Res |= SanitizerKind::Memory;
427  return Res;
428 }
bool HasNativeLLVMSupport() const override
HasNativeLTOLinker - Check whether the linker and related tools have native LLVM support.
Definition: FreeBSD.cpp:404
bool addXRayRuntime(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
const char * getSparcAsmModeForCPU(llvm::StringRef Name, const llvm::Triple &Triple)
CXXStdlibType GetDefaultCXXStdlibType() const override
Definition: FreeBSD.cpp:354
bool isUsingLTO() const
Returns true if we are performing any kind of LTO.
Definition: Driver.h:518
The base class of the type hierarchy.
Definition: Type.h:1415
LTOKind getLTOMode() const
Get the specific kind of LTO being performed.
Definition: Driver.h:521
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:653
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)
void AddGoldPlugin(const ToolChain &ToolChain, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const InputInfo &Output, const InputInfo &Input, bool IsThinLTO)
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
bool hasMipsAbiArg(const llvm::opt::ArgList &Args, const char *Value)
Tool * buildLinker() const override
Definition: FreeBSD.cpp:387
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.
llvm::vfs::FileSystem & getVFS() const
Definition: Driver.h:300
void linkXRayRuntimeDeps(const ToolChain &TC, llvm::opt::ArgStringList &CmdArgs)
Tool * buildAssembler() const override
Definition: FreeBSD.cpp:383
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:206
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: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
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:367
const Driver & getDriver() const
Definition: ToolChain.h:184
bool CCCIsCXX() const
Whether the driver should follow g++ like behavior.
Definition: Driver.h:173
bool isPIEDefault() const override
Test whether this toolchain defaults to PIE.
Definition: FreeBSD.cpp:406
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)
std::string SysRoot
sysroot, if present
Definition: Driver.h:148
Tool - Information on a specific compilation tool.
Definition: Tool.h:34
const SanitizerArgs & getSanitizerArgs() const
Definition: ToolChain.cpp:116
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
const llvm::Triple & getTriple() const
Definition: ToolChain.h:186
llvm::ExceptionHandling GetExceptionModel(const llvm::opt::ArgList &Args) const override
GetExceptionModel - Return the tool chain exception model.
Definition: FreeBSD.cpp:389
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:814
StringRef getGnuCompatibleMipsABIName(StringRef ABI)
Definition: Mips.cpp:136
void addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Definition: FreeBSD.cpp:360
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:679
SanitizerMask getSupportedSanitizers() const override
Return sanitizers which are available in this toolchain.
Definition: FreeBSD.cpp:408
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:2459
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:87
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:119