clang  5.0.0
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  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
244  if (Args.hasArg(options::OPT_pg))
245  CmdArgs.push_back("-lm_p");
246  else
247  CmdArgs.push_back("-lm");
248  }
249  if (NeedsSanitizerDeps)
250  linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
251  // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
252  // the default system libraries. Just mimic this for now.
253  if (Args.hasArg(options::OPT_pg))
254  CmdArgs.push_back("-lgcc_p");
255  else
256  CmdArgs.push_back("-lgcc");
257  if (Args.hasArg(options::OPT_static)) {
258  CmdArgs.push_back("-lgcc_eh");
259  } else if (Args.hasArg(options::OPT_pg)) {
260  CmdArgs.push_back("-lgcc_eh_p");
261  } else {
262  CmdArgs.push_back("--as-needed");
263  CmdArgs.push_back("-lgcc_s");
264  CmdArgs.push_back("--no-as-needed");
265  }
266 
267  if (Args.hasArg(options::OPT_pthread)) {
268  if (Args.hasArg(options::OPT_pg))
269  CmdArgs.push_back("-lpthread_p");
270  else
271  CmdArgs.push_back("-lpthread");
272  }
273 
274  if (Args.hasArg(options::OPT_pg)) {
275  if (Args.hasArg(options::OPT_shared))
276  CmdArgs.push_back("-lc");
277  else
278  CmdArgs.push_back("-lc_p");
279  CmdArgs.push_back("-lgcc_p");
280  } else {
281  CmdArgs.push_back("-lc");
282  CmdArgs.push_back("-lgcc");
283  }
284 
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 
296  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
297  if (Args.hasArg(options::OPT_shared) || IsPIE)
298  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o")));
299  else
300  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
301  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
302  }
303 
304  ToolChain.addProfileRTLibs(Args, CmdArgs);
305 
306  const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
307  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
308 }
309 
310 /// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
311 
312 FreeBSD::FreeBSD(const Driver &D, const llvm::Triple &Triple,
313  const ArgList &Args)
314  : Generic_ELF(D, Triple, Args) {
315 
316  // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
317  // back to '/usr/lib' if it doesn't exist.
318  if ((Triple.getArch() == llvm::Triple::x86 ||
319  Triple.getArch() == llvm::Triple::ppc) &&
320  D.getVFS().exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
321  getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
322  else
323  getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
324 }
325 
327  if (getTriple().getOSMajorVersion() >= 10)
328  return ToolChain::CST_Libcxx;
330 }
331 
333  const llvm::opt::ArgList &DriverArgs,
334  llvm::opt::ArgStringList &CC1Args) const {
335  addLibStdCXXIncludePaths(getDriver().SysRoot, "/usr/include/c++/4.2", "", "",
336  "", "", DriverArgs, CC1Args);
337 }
338 
339 void FreeBSD::AddCXXStdlibLibArgs(const ArgList &Args,
340  ArgStringList &CmdArgs) const {
342  bool Profiling = Args.hasArg(options::OPT_pg);
343 
344  switch (Type) {
346  CmdArgs.push_back(Profiling ? "-lc++_p" : "-lc++");
347  break;
348 
350  CmdArgs.push_back(Profiling ? "-lstdc++_p" : "-lstdc++");
351  break;
352  }
353 }
354 
356  return new tools::freebsd::Assembler(*this);
357 }
358 
359 Tool *FreeBSD::buildLinker() const { return new tools::freebsd::Linker(*this); }
360 
361 bool FreeBSD::UseSjLjExceptions(const ArgList &Args) const {
362  // FreeBSD uses SjLj exceptions on ARM oabi.
363  switch (getTriple().getEnvironment()) {
364  case llvm::Triple::GNUEABIHF:
365  case llvm::Triple::GNUEABI:
366  case llvm::Triple::EABI:
367  return false;
368 
369  default:
370  return (getTriple().getArch() == llvm::Triple::arm ||
371  getTriple().getArch() == llvm::Triple::thumb);
372  }
373 }
374 
375 bool FreeBSD::HasNativeLLVMSupport() const { return true; }
376 
378 
380  const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
381  const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
382  const bool IsMIPS64 = getTriple().getArch() == llvm::Triple::mips64 ||
383  getTriple().getArch() == llvm::Triple::mips64el;
385  Res |= SanitizerKind::Address;
386  Res |= SanitizerKind::Vptr;
387  if (IsX86_64 || IsMIPS64) {
388  Res |= SanitizerKind::Leak;
389  Res |= SanitizerKind::Thread;
390  }
391  if (IsX86 || IsX86_64) {
392  Res |= SanitizerKind::SafeStack;
393  }
394  return Res;
395 }
const llvm::Triple & getTriple() const
Definition: ToolChain.h:144
bool HasNativeLLVMSupport() const override
HasNativeLTOLinker - Check whether the linker and related tools have native LLVM support.
Definition: FreeBSD.cpp:375
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:553
const char * getSparcAsmModeForCPU(llvm::StringRef Name, const llvm::Triple &Triple)
virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:579
CXXStdlibType GetDefaultCXXStdlibType() const override
Definition: FreeBSD.cpp:326
The base class of the type hierarchy.
Definition: Type.h:1303
bool isFilename() const
Definition: InputInfo.h:76
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:153
FloatABI getARMFloatABI(const ToolChain &TC, const llvm::opt::ArgList &Args)
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:702
bool CCCIsCXX() const
Whether the driver should follow g++ like behavior.
Definition: Driver.h:174
std::string getCPUName(const llvm::opt::ArgList &Args, const llvm::Triple &T, bool FromAs=false)
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
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:172
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:65
const Driver & getDriver() const
Definition: ToolChain.h:142
Tool * buildLinker() const override
Definition: FreeBSD.cpp:359
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:355
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:189
vfs::FileSystem & getVFS() const
Definition: Driver.h:284
do v
Definition: arm_acle.h:78
const char * getFilename() const
Definition: InputInfo.h:84
void AddAssemblerKPIC(const ToolChain &ToolChain, const llvm::opt::ArgList &Args, 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: FreeBSD.cpp:339
bool UseSjLjExceptions(const llvm::opt::ArgList &Args) const override
UseSjLjExceptions - Does this tool chain use SjLj exceptions.
Definition: FreeBSD.cpp:361
LTOKind getLTOMode() const
Get the specific kind of LTO being performed.
Definition: Driver.h:487
bool isPIEDefault() const override
Test whether this toolchain defaults to PIE.
Definition: FreeBSD.cpp:377
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:146
Tool - Information on a specific compilation tool.
Definition: Tool.h:34
Defines the virtual file system interface vfs::FileSystem.
void claimNoWarnArgs(const llvm::opt::ArgList &Args)
bool exists(const Twine &Path)
Check whether a file exists. Provided for convenience.
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:2386
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:34
bool isUsingLTO() const
Returns true if we are performing any kind of LTO.
Definition: Driver.h:484
StringRef getGnuCompatibleMipsABIName(StringRef ABI)
Definition: Mips.cpp:138
void addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Definition: FreeBSD.cpp:332
bool isNothing() const
Definition: InputInfo.h:75
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:666
SanitizerMask getSupportedSanitizers() const override
Return sanitizers which are available in this toolchain.
Definition: FreeBSD.cpp:379
void getMipsCPUAndABI(const llvm::opt::ArgList &Args, const llvm::Triple &Triple, StringRef &CPUName, StringRef &ABIName)
const SanitizerArgs & getSanitizerArgs() const
Definition: ToolChain.cpp:98
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:367
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:50
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