xref: /freebsd/contrib/llvm-project/clang/lib/Driver/ToolChains/FreeBSD.cpp (revision 4e99f45480598189d49d45a825533a6c9e12f02c)
1 //===--- FreeBSD.cpp - FreeBSD ToolChain Implementations --------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "FreeBSD.h"
10 #include "Arch/ARM.h"
11 #include "Arch/Mips.h"
12 #include "Arch/Sparc.h"
13 #include "CommonArgs.h"
14 #include "clang/Driver/Compilation.h"
15 #include "clang/Driver/DriverDiagnostic.h"
16 #include "clang/Driver/Options.h"
17 #include "clang/Driver/SanitizerArgs.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 
27 void freebsd::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
28                                       const InputInfo &Output,
29                                       const InputInfoList &Inputs,
30                                       const ArgList &Args,
31                                       const char *LinkingOutput) const {
32   claimNoWarnArgs(Args);
33   ArgStringList CmdArgs;
34   const auto &D = getToolChain().getDriver();
35 
36   // When building 32-bit code on FreeBSD/amd64, we have to explicitly
37   // instruct as in the base system to assemble 32-bit code.
38   switch (getToolChain().getArch()) {
39   default:
40     break;
41   case llvm::Triple::x86:
42     CmdArgs.push_back("--32");
43     break;
44   case llvm::Triple::ppc:
45     CmdArgs.push_back("-a32");
46     break;
47   case llvm::Triple::mips:
48   case llvm::Triple::mipsel:
49   case llvm::Triple::mips64:
50   case llvm::Triple::mips64el: {
51     StringRef CPUName;
52     StringRef ABIName;
53     mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
54 
55     CmdArgs.push_back("-march");
56     CmdArgs.push_back(CPUName.data());
57 
58     CmdArgs.push_back("-mabi");
59     CmdArgs.push_back(mips::getGnuCompatibleMipsABIName(ABIName).data());
60 
61     if (getToolChain().getTriple().isLittleEndian())
62       CmdArgs.push_back("-EL");
63     else
64       CmdArgs.push_back("-EB");
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(
103         sparc::getSparcAsmModeForCPU(CPU, getToolChain().getTriple()));
104     AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
105     break;
106   }
107   }
108 
109   for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ,
110                                     options::OPT_fdebug_prefix_map_EQ)) {
111     StringRef Map = A->getValue();
112     if (Map.find('=') == StringRef::npos)
113       D.Diag(diag::err_drv_invalid_argument_to_option)
114           << Map << A->getOption().getName();
115     else {
116       CmdArgs.push_back(Args.MakeArgString("--debug-prefix-map"));
117       CmdArgs.push_back(Args.MakeArgString(Map));
118     }
119     A->claim();
120   }
121 
122   Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
123 
124   CmdArgs.push_back("-o");
125   CmdArgs.push_back(Output.getFilename());
126 
127   for (const auto &II : Inputs)
128     CmdArgs.push_back(II.getFilename());
129 
130   const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
131   C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
132 }
133 
134 void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
135                                    const InputInfo &Output,
136                                    const InputInfoList &Inputs,
137                                    const ArgList &Args,
138                                    const char *LinkingOutput) const {
139   const toolchains::FreeBSD &ToolChain =
140       static_cast<const toolchains::FreeBSD &>(getToolChain());
141   const Driver &D = ToolChain.getDriver();
142   const llvm::Triple::ArchType Arch = ToolChain.getArch();
143   const bool IsPIE =
144       !Args.hasArg(options::OPT_shared) &&
145       (Args.hasArg(options::OPT_pie) || ToolChain.isPIEDefault());
146   ArgStringList CmdArgs;
147 
148   // Silence warning for "clang -g foo.o -o foo"
149   Args.ClaimAllArgs(options::OPT_g_Group);
150   // and "clang -emit-llvm foo.o -o foo"
151   Args.ClaimAllArgs(options::OPT_emit_llvm);
152   // and for "clang -w foo.o -o foo". Other warning options are already
153   // handled somewhere else.
154   Args.ClaimAllArgs(options::OPT_w);
155 
156   if (!D.SysRoot.empty())
157     CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
158 
159   if (IsPIE)
160     CmdArgs.push_back("-pie");
161 
162   CmdArgs.push_back("--eh-frame-hdr");
163   if (Args.hasArg(options::OPT_static)) {
164     CmdArgs.push_back("-Bstatic");
165   } else {
166     if (Args.hasArg(options::OPT_rdynamic))
167       CmdArgs.push_back("-export-dynamic");
168     if (Args.hasArg(options::OPT_shared)) {
169       CmdArgs.push_back("-Bshareable");
170     } else {
171       CmdArgs.push_back("-dynamic-linker");
172       CmdArgs.push_back("/libexec/ld-elf.so.1");
173     }
174     const llvm::Triple &T = ToolChain.getTriple();
175     if (T.getOSMajorVersion() >= 9) {
176       if (Arch == llvm::Triple::arm || Arch == llvm::Triple::sparc || T.isX86())
177         CmdArgs.push_back("--hash-style=both");
178     }
179     CmdArgs.push_back("--enable-new-dtags");
180   }
181 
182   // Explicitly set the linker emulation for platforms that might not
183   // be the default emulation for the linker.
184   switch (Arch) {
185   case llvm::Triple::x86:
186     CmdArgs.push_back("-m");
187     CmdArgs.push_back("elf_i386_fbsd");
188     break;
189   case llvm::Triple::ppc:
190     CmdArgs.push_back("-m");
191     CmdArgs.push_back("elf32ppc_fbsd");
192     break;
193   case llvm::Triple::mips:
194     CmdArgs.push_back("-m");
195     CmdArgs.push_back("elf32btsmip_fbsd");
196     break;
197   case llvm::Triple::mipsel:
198     CmdArgs.push_back("-m");
199     CmdArgs.push_back("elf32ltsmip_fbsd");
200     break;
201   case llvm::Triple::mips64:
202     CmdArgs.push_back("-m");
203     if (tools::mips::hasMipsAbiArg(Args, "n32"))
204       CmdArgs.push_back("elf32btsmipn32_fbsd");
205     else
206       CmdArgs.push_back("elf64btsmip_fbsd");
207     break;
208   case llvm::Triple::mips64el:
209     CmdArgs.push_back("-m");
210     if (tools::mips::hasMipsAbiArg(Args, "n32"))
211       CmdArgs.push_back("elf32ltsmipn32_fbsd");
212     else
213       CmdArgs.push_back("elf64ltsmip_fbsd");
214     break;
215   case llvm::Triple::riscv32:
216     CmdArgs.push_back("-m");
217     CmdArgs.push_back("elf32lriscv");
218     break;
219   case llvm::Triple::riscv64:
220     CmdArgs.push_back("-m");
221     CmdArgs.push_back("elf64lriscv");
222     break;
223   default:
224     break;
225   }
226 
227   if (Arg *A = Args.getLastArg(options::OPT_G)) {
228     if (ToolChain.getTriple().isMIPS()) {
229       StringRef v = A->getValue();
230       CmdArgs.push_back(Args.MakeArgString("-G" + v));
231       A->claim();
232     }
233   }
234 
235   if (Output.isFilename()) {
236     CmdArgs.push_back("-o");
237     CmdArgs.push_back(Output.getFilename());
238   } else {
239     assert(Output.isNothing() && "Invalid output.");
240   }
241 
242   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
243     const char *crt1 = nullptr;
244     if (!Args.hasArg(options::OPT_shared)) {
245       if (Args.hasArg(options::OPT_pg))
246         crt1 = "gcrt1.o";
247       else if (IsPIE)
248         crt1 = "Scrt1.o";
249       else
250         crt1 = "crt1.o";
251     }
252     if (crt1)
253       CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
254 
255     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
256 
257     const char *crtbegin = nullptr;
258     if (Args.hasArg(options::OPT_static))
259       crtbegin = "crtbeginT.o";
260     else if (Args.hasArg(options::OPT_shared) || IsPIE)
261       crtbegin = "crtbeginS.o";
262     else
263       crtbegin = "crtbegin.o";
264 
265     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
266   }
267 
268   Args.AddAllArgs(CmdArgs, options::OPT_L);
269   ToolChain.AddFilePathLibArgs(Args, CmdArgs);
270   Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
271   Args.AddAllArgs(CmdArgs, options::OPT_e);
272   Args.AddAllArgs(CmdArgs, options::OPT_s);
273   Args.AddAllArgs(CmdArgs, options::OPT_t);
274   Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
275   Args.AddAllArgs(CmdArgs, options::OPT_r);
276 
277   if (D.isUsingLTO()) {
278     assert(!Inputs.empty() && "Must have at least one input.");
279     AddGoldPlugin(ToolChain, Args, CmdArgs, Output, Inputs[0],
280                   D.getLTOMode() == LTOK_Thin);
281   }
282 
283   bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
284   bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
285   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
286 
287   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
288     // Use the static OpenMP runtime with -static-openmp
289     bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) &&
290                         !Args.hasArg(options::OPT_static);
291     addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP);
292 
293     if (D.CCCIsCXX()) {
294       if (ToolChain.ShouldLinkCXXStdlib(Args))
295         ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
296       if (Args.hasArg(options::OPT_pg))
297         CmdArgs.push_back("-lm_p");
298       else
299         CmdArgs.push_back("-lm");
300     }
301     if (NeedsSanitizerDeps)
302       linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
303     if (NeedsXRayDeps)
304       linkXRayRuntimeDeps(ToolChain, CmdArgs);
305     // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
306     // the default system libraries. Just mimic this for now.
307     if (Args.hasArg(options::OPT_pg))
308       CmdArgs.push_back("-lgcc_p");
309     else
310       CmdArgs.push_back("-lgcc");
311     if (Args.hasArg(options::OPT_static)) {
312       CmdArgs.push_back("-lgcc_eh");
313     } else if (Args.hasArg(options::OPT_pg)) {
314       CmdArgs.push_back("-lgcc_eh_p");
315     } else {
316       CmdArgs.push_back("--as-needed");
317       CmdArgs.push_back("-lgcc_s");
318       CmdArgs.push_back("--no-as-needed");
319     }
320 
321     if (Args.hasArg(options::OPT_pthread)) {
322       if (Args.hasArg(options::OPT_pg))
323         CmdArgs.push_back("-lpthread_p");
324       else
325         CmdArgs.push_back("-lpthread");
326     }
327 
328     if (Args.hasArg(options::OPT_pg)) {
329       if (Args.hasArg(options::OPT_shared))
330         CmdArgs.push_back("-lc");
331       else
332         CmdArgs.push_back("-lc_p");
333       CmdArgs.push_back("-lgcc_p");
334     } else {
335       CmdArgs.push_back("-lc");
336       CmdArgs.push_back("-lgcc");
337     }
338 
339     if (Args.hasArg(options::OPT_static)) {
340       CmdArgs.push_back("-lgcc_eh");
341     } else if (Args.hasArg(options::OPT_pg)) {
342       CmdArgs.push_back("-lgcc_eh_p");
343     } else {
344       CmdArgs.push_back("--as-needed");
345       CmdArgs.push_back("-lgcc_s");
346       CmdArgs.push_back("--no-as-needed");
347     }
348   }
349 
350   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
351     if (Args.hasArg(options::OPT_shared) || IsPIE)
352       CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o")));
353     else
354       CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
355     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
356   }
357 
358   ToolChain.addProfileRTLibs(Args, CmdArgs);
359 
360   const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
361   C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
362 }
363 
364 /// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
365 
366 FreeBSD::FreeBSD(const Driver &D, const llvm::Triple &Triple,
367                  const ArgList &Args)
368     : Generic_ELF(D, Triple, Args) {
369 
370   // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
371   // back to '/usr/lib' if it doesn't exist.
372   if ((Triple.getArch() == llvm::Triple::x86 || Triple.isMIPS32() ||
373        Triple.getArch() == llvm::Triple::ppc) &&
374       D.getVFS().exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
375     getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
376   else
377     getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
378 }
379 
380 ToolChain::CXXStdlibType FreeBSD::GetDefaultCXXStdlibType() const {
381   if (getTriple().getOSMajorVersion() >= 10)
382     return ToolChain::CST_Libcxx;
383   return ToolChain::CST_Libstdcxx;
384 }
385 
386 unsigned FreeBSD::GetDefaultDwarfVersion() const {
387   if (getTriple().getOSMajorVersion() < 12)
388     return 2;
389   return 4;
390 }
391 
392 void FreeBSD::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
393                                     llvm::opt::ArgStringList &CC1Args) const {
394   addSystemInclude(DriverArgs, CC1Args,
395                    getDriver().SysRoot + "/usr/include/c++/v1");
396 }
397 
398 void FreeBSD::addLibStdCxxIncludePaths(
399     const llvm::opt::ArgList &DriverArgs,
400     llvm::opt::ArgStringList &CC1Args) const {
401   addLibStdCXXIncludePaths(getDriver().SysRoot, "/usr/include/c++/4.2", "", "",
402                            "", "", DriverArgs, CC1Args);
403 }
404 
405 void FreeBSD::AddCXXStdlibLibArgs(const ArgList &Args,
406                                   ArgStringList &CmdArgs) const {
407   CXXStdlibType Type = GetCXXStdlibType(Args);
408   bool Profiling = Args.hasArg(options::OPT_pg);
409 
410   switch (Type) {
411   case ToolChain::CST_Libcxx:
412     CmdArgs.push_back(Profiling ? "-lc++_p" : "-lc++");
413     break;
414 
415   case ToolChain::CST_Libstdcxx:
416     CmdArgs.push_back(Profiling ? "-lstdc++_p" : "-lstdc++");
417     break;
418   }
419 }
420 
421 void FreeBSD::AddCudaIncludeArgs(const ArgList &DriverArgs,
422                                  ArgStringList &CC1Args) const {
423   CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
424 }
425 
426 Tool *FreeBSD::buildAssembler() const {
427   return new tools::freebsd::Assembler(*this);
428 }
429 
430 Tool *FreeBSD::buildLinker() const { return new tools::freebsd::Linker(*this); }
431 
432 llvm::ExceptionHandling FreeBSD::GetExceptionModel(const ArgList &Args) const {
433   // FreeBSD uses SjLj exceptions on ARM oabi.
434   switch (getTriple().getEnvironment()) {
435   case llvm::Triple::GNUEABIHF:
436   case llvm::Triple::GNUEABI:
437   case llvm::Triple::EABI:
438     return llvm::ExceptionHandling::None;
439   default:
440     if (getTriple().getArch() == llvm::Triple::arm ||
441         getTriple().getArch() == llvm::Triple::thumb)
442       return llvm::ExceptionHandling::SjLj;
443     return llvm::ExceptionHandling::None;
444   }
445 }
446 
447 bool FreeBSD::HasNativeLLVMSupport() const { return true; }
448 
449 bool FreeBSD::IsUnwindTablesDefault(const ArgList &Args) const { return true; }
450 
451 bool FreeBSD::isPIEDefault() const { return getSanitizerArgs().requiresPIE(); }
452 
453 SanitizerMask FreeBSD::getSupportedSanitizers() const {
454   const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
455   const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
456   const bool IsMIPS64 = getTriple().isMIPS64();
457   SanitizerMask Res = ToolChain::getSupportedSanitizers();
458   Res |= SanitizerKind::Address;
459   Res |= SanitizerKind::PointerCompare;
460   Res |= SanitizerKind::PointerSubtract;
461   Res |= SanitizerKind::Vptr;
462   if (IsX86_64 || IsMIPS64) {
463     Res |= SanitizerKind::Leak;
464     Res |= SanitizerKind::Thread;
465   }
466   if (IsX86 || IsX86_64) {
467     Res |= SanitizerKind::Function;
468     Res |= SanitizerKind::SafeStack;
469     Res |= SanitizerKind::Fuzzer;
470     Res |= SanitizerKind::FuzzerNoLink;
471   }
472   if (IsX86_64)
473     Res |= SanitizerKind::Memory;
474   return Res;
475 }
476 
477 void FreeBSD::addClangTargetOptions(const ArgList &DriverArgs,
478                                     ArgStringList &CC1Args,
479                                     Action::OffloadKind) const {
480   if (!DriverArgs.hasFlag(options::OPT_fuse_init_array,
481                           options::OPT_fno_use_init_array,
482                           getTriple().getOSMajorVersion() >= 12))
483     CC1Args.push_back("-fno-use-init-array");
484 }
485