1 //===--- FreeBSD.cpp - FreeBSD ToolChain Implementations --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include "Arch/Mips.h"
13 #include "Arch/Sparc.h"
14 #include "CommonArgs.h"
15 #include "clang/Basic/VirtualFileSystem.h"
16 #include "clang/Driver/Compilation.h"
17 #include "clang/Driver/Options.h"
18 #include "clang/Driver/SanitizerArgs.h"
19 #include "llvm/Option/ArgList.h"
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;
27 void freebsd::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
28 const InputInfo &Output,
29 const InputInfoList &Inputs,
31 const char *LinkingOutput) const {
32 claimNoWarnArgs(Args);
33 ArgStringList CmdArgs;
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()) {
40 case llvm::Triple::x86:
41 CmdArgs.push_back("--32");
43 case llvm::Triple::ppc:
44 CmdArgs.push_back("-a32");
46 case llvm::Triple::mips:
47 case llvm::Triple::mipsel:
48 case llvm::Triple::mips64:
49 case llvm::Triple::mips64el: {
52 mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
54 CmdArgs.push_back("-march");
55 CmdArgs.push_back(CPUName.data());
57 CmdArgs.push_back("-mabi");
58 CmdArgs.push_back(mips::getGnuCompatibleMipsABIName(ABIName).data());
60 if (getToolChain().getTriple().isLittleEndian())
61 CmdArgs.push_back("-EL");
63 CmdArgs.push_back("-EB");
65 if (Arg *A = Args.getLastArg(options::OPT_G)) {
66 StringRef v = A->getValue();
67 CmdArgs.push_back(Args.MakeArgString("-G" + v));
71 AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
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);
80 if (ABI == arm::FloatABI::Hard)
81 CmdArgs.push_back("-mfpu=vfp");
83 CmdArgs.push_back("-mfpu=softvfp");
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");
93 CmdArgs.push_back("-matpcs");
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);
107 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
109 CmdArgs.push_back("-o");
110 CmdArgs.push_back(Output.getFilename());
112 for (const auto &II : Inputs)
113 CmdArgs.push_back(II.getFilename());
115 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
116 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
119 void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
120 const InputInfo &Output,
121 const InputInfoList &Inputs,
123 const char *LinkingOutput) const {
124 const toolchains::FreeBSD &ToolChain =
125 static_cast<const toolchains::FreeBSD &>(getToolChain());
126 const Driver &D = ToolChain.getDriver();
127 const llvm::Triple::ArchType Arch = ToolChain.getArch();
129 !Args.hasArg(options::OPT_shared) &&
130 (Args.hasArg(options::OPT_pie) || ToolChain.isPIEDefault());
131 ArgStringList CmdArgs;
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);
141 if (!D.SysRoot.empty())
142 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
145 CmdArgs.push_back("-pie");
147 CmdArgs.push_back("--eh-frame-hdr");
148 if (Args.hasArg(options::OPT_static)) {
149 CmdArgs.push_back("-Bstatic");
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");
156 CmdArgs.push_back("-dynamic-linker");
157 CmdArgs.push_back("/libexec/ld-elf.so.1");
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");
165 CmdArgs.push_back("--enable-new-dtags");
168 // Explicitly set the linker emulation for platforms that might not
169 // be the default emulation for the linker.
171 case llvm::Triple::x86:
172 CmdArgs.push_back("-m");
173 CmdArgs.push_back("elf_i386_fbsd");
175 case llvm::Triple::ppc:
176 CmdArgs.push_back("-m");
177 CmdArgs.push_back("elf32ppc_fbsd");
179 case llvm::Triple::mips:
180 CmdArgs.push_back("-m");
181 CmdArgs.push_back("elf32btsmip_fbsd");
183 case llvm::Triple::mipsel:
184 CmdArgs.push_back("-m");
185 CmdArgs.push_back("elf32ltsmip_fbsd");
187 case llvm::Triple::mips64:
188 CmdArgs.push_back("-m");
189 if (tools::mips::hasMipsAbiArg(Args, "n32"))
190 CmdArgs.push_back("elf32btsmipn32_fbsd");
192 CmdArgs.push_back("elf64btsmip_fbsd");
194 case llvm::Triple::mips64el:
195 CmdArgs.push_back("-m");
196 if (tools::mips::hasMipsAbiArg(Args, "n32"))
197 CmdArgs.push_back("elf32ltsmipn32_fbsd");
199 CmdArgs.push_back("elf64ltsmip_fbsd");
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));
213 if (Output.isFilename()) {
214 CmdArgs.push_back("-o");
215 CmdArgs.push_back(Output.getFilename());
217 assert(Output.isNothing() && "Invalid output.");
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))
231 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
233 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
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";
241 crtbegin = "crtbegin.o";
243 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
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);
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);
261 bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
262 bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
263 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
265 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
266 addOpenMPRuntime(CmdArgs, ToolChain, Args);
268 if (ToolChain.ShouldLinkCXXStdlib(Args))
269 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
270 if (Args.hasArg(options::OPT_pg))
271 CmdArgs.push_back("-lm_p");
273 CmdArgs.push_back("-lm");
275 if (NeedsSanitizerDeps)
276 linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
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");
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");
290 CmdArgs.push_back("--as-needed");
291 CmdArgs.push_back("-lgcc_s");
292 CmdArgs.push_back("--no-as-needed");
295 if (Args.hasArg(options::OPT_pthread)) {
296 if (Args.hasArg(options::OPT_pg))
297 CmdArgs.push_back("-lpthread_p");
299 CmdArgs.push_back("-lpthread");
302 if (Args.hasArg(options::OPT_pg)) {
303 if (Args.hasArg(options::OPT_shared))
304 CmdArgs.push_back("-lc");
306 CmdArgs.push_back("-lc_p");
307 CmdArgs.push_back("-lgcc_p");
309 CmdArgs.push_back("-lc");
310 CmdArgs.push_back("-lgcc");
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");
318 CmdArgs.push_back("--as-needed");
319 CmdArgs.push_back("-lgcc_s");
320 CmdArgs.push_back("--no-as-needed");
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")));
328 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
329 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
332 ToolChain.addProfileRTLibs(Args, CmdArgs);
334 const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
335 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
338 /// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
340 FreeBSD::FreeBSD(const Driver &D, const llvm::Triple &Triple,
342 : Generic_ELF(D, Triple, Args) {
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");
351 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
354 ToolChain::CXXStdlibType FreeBSD::GetDefaultCXXStdlibType() const {
355 if (getTriple().getOSMajorVersion() >= 10)
356 return ToolChain::CST_Libcxx;
357 return ToolChain::CST_Libstdcxx;
360 void FreeBSD::addLibStdCxxIncludePaths(
361 const llvm::opt::ArgList &DriverArgs,
362 llvm::opt::ArgStringList &CC1Args) const {
363 addLibStdCXXIncludePaths(getDriver().SysRoot, "/usr/include/c++/4.2", "", "",
364 "", "", DriverArgs, CC1Args);
367 void FreeBSD::AddCXXStdlibLibArgs(const ArgList &Args,
368 ArgStringList &CmdArgs) const {
369 CXXStdlibType Type = GetCXXStdlibType(Args);
370 bool Profiling = Args.hasArg(options::OPT_pg);
373 case ToolChain::CST_Libcxx:
374 CmdArgs.push_back(Profiling ? "-lc++_p" : "-lc++");
377 case ToolChain::CST_Libstdcxx:
378 CmdArgs.push_back(Profiling ? "-lstdc++_p" : "-lstdc++");
383 Tool *FreeBSD::buildAssembler() const {
384 return new tools::freebsd::Assembler(*this);
387 Tool *FreeBSD::buildLinker() const { return new tools::freebsd::Linker(*this); }
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;
397 if (getTriple().getArch() == llvm::Triple::arm ||
398 getTriple().getArch() == llvm::Triple::thumb)
399 return llvm::ExceptionHandling::SjLj;
400 return llvm::ExceptionHandling::None;
404 bool FreeBSD::HasNativeLLVMSupport() const { return true; }
406 bool FreeBSD::isPIEDefault() const { return getSanitizerArgs().requiresPIE(); }
408 SanitizerMask FreeBSD::getSupportedSanitizers() const {
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();
412 SanitizerMask Res = ToolChain::getSupportedSanitizers();
413 Res |= SanitizerKind::Address;
414 Res |= SanitizerKind::Vptr;
415 if (IsX86_64 || IsMIPS64) {
416 Res |= SanitizerKind::Leak;
417 Res |= SanitizerKind::Thread;
419 if (IsX86 || IsX86_64) {
420 Res |= SanitizerKind::Function;
421 Res |= SanitizerKind::SafeStack;
422 Res |= SanitizerKind::Fuzzer;
423 Res |= SanitizerKind::FuzzerNoLink;
426 Res |= SanitizerKind::Memory;