xref: /netbsd-src/external/apache2/llvm/dist/clang/lib/Driver/ToolChains/FreeBSD.cpp (revision e038c9c4676b0f19b1b7dd08a940c6ed64a6d5ae)
17330f729Sjoerg //===--- FreeBSD.cpp - FreeBSD ToolChain Implementations --------*- C++ -*-===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg 
97330f729Sjoerg #include "FreeBSD.h"
107330f729Sjoerg #include "Arch/ARM.h"
117330f729Sjoerg #include "Arch/Mips.h"
127330f729Sjoerg #include "Arch/Sparc.h"
137330f729Sjoerg #include "CommonArgs.h"
147330f729Sjoerg #include "clang/Driver/Compilation.h"
15*e038c9c4Sjoerg #include "clang/Driver/DriverDiagnostic.h"
167330f729Sjoerg #include "clang/Driver/Options.h"
177330f729Sjoerg #include "clang/Driver/SanitizerArgs.h"
187330f729Sjoerg #include "llvm/Option/ArgList.h"
197330f729Sjoerg #include "llvm/Support/VirtualFileSystem.h"
207330f729Sjoerg 
217330f729Sjoerg using namespace clang::driver;
227330f729Sjoerg using namespace clang::driver::tools;
237330f729Sjoerg using namespace clang::driver::toolchains;
247330f729Sjoerg using namespace clang;
257330f729Sjoerg using namespace llvm::opt;
267330f729Sjoerg 
ConstructJob(Compilation & C,const JobAction & JA,const InputInfo & Output,const InputInfoList & Inputs,const ArgList & Args,const char * LinkingOutput) const277330f729Sjoerg void freebsd::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
287330f729Sjoerg                                       const InputInfo &Output,
297330f729Sjoerg                                       const InputInfoList &Inputs,
307330f729Sjoerg                                       const ArgList &Args,
317330f729Sjoerg                                       const char *LinkingOutput) const {
327330f729Sjoerg   claimNoWarnArgs(Args);
337330f729Sjoerg   ArgStringList CmdArgs;
34*e038c9c4Sjoerg   const auto &D = getToolChain().getDriver();
357330f729Sjoerg 
367330f729Sjoerg   // When building 32-bit code on FreeBSD/amd64, we have to explicitly
377330f729Sjoerg   // instruct as in the base system to assemble 32-bit code.
387330f729Sjoerg   switch (getToolChain().getArch()) {
397330f729Sjoerg   default:
407330f729Sjoerg     break;
417330f729Sjoerg   case llvm::Triple::x86:
427330f729Sjoerg     CmdArgs.push_back("--32");
437330f729Sjoerg     break;
447330f729Sjoerg   case llvm::Triple::ppc:
45*e038c9c4Sjoerg   case llvm::Triple::ppcle:
467330f729Sjoerg     CmdArgs.push_back("-a32");
477330f729Sjoerg     break;
487330f729Sjoerg   case llvm::Triple::mips:
497330f729Sjoerg   case llvm::Triple::mipsel:
507330f729Sjoerg   case llvm::Triple::mips64:
517330f729Sjoerg   case llvm::Triple::mips64el: {
527330f729Sjoerg     StringRef CPUName;
537330f729Sjoerg     StringRef ABIName;
547330f729Sjoerg     mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
557330f729Sjoerg 
567330f729Sjoerg     CmdArgs.push_back("-march");
577330f729Sjoerg     CmdArgs.push_back(CPUName.data());
587330f729Sjoerg 
597330f729Sjoerg     CmdArgs.push_back("-mabi");
607330f729Sjoerg     CmdArgs.push_back(mips::getGnuCompatibleMipsABIName(ABIName).data());
617330f729Sjoerg 
627330f729Sjoerg     if (getToolChain().getTriple().isLittleEndian())
637330f729Sjoerg       CmdArgs.push_back("-EL");
647330f729Sjoerg     else
657330f729Sjoerg       CmdArgs.push_back("-EB");
667330f729Sjoerg 
677330f729Sjoerg     if (Arg *A = Args.getLastArg(options::OPT_G)) {
687330f729Sjoerg       StringRef v = A->getValue();
697330f729Sjoerg       CmdArgs.push_back(Args.MakeArgString("-G" + v));
707330f729Sjoerg       A->claim();
717330f729Sjoerg     }
727330f729Sjoerg 
737330f729Sjoerg     AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
747330f729Sjoerg     break;
757330f729Sjoerg   }
767330f729Sjoerg   case llvm::Triple::arm:
777330f729Sjoerg   case llvm::Triple::armeb:
787330f729Sjoerg   case llvm::Triple::thumb:
797330f729Sjoerg   case llvm::Triple::thumbeb: {
807330f729Sjoerg     arm::FloatABI ABI = arm::getARMFloatABI(getToolChain(), Args);
817330f729Sjoerg 
827330f729Sjoerg     if (ABI == arm::FloatABI::Hard)
837330f729Sjoerg       CmdArgs.push_back("-mfpu=vfp");
847330f729Sjoerg     else
857330f729Sjoerg       CmdArgs.push_back("-mfpu=softvfp");
867330f729Sjoerg 
877330f729Sjoerg     switch (getToolChain().getTriple().getEnvironment()) {
887330f729Sjoerg     case llvm::Triple::GNUEABIHF:
897330f729Sjoerg     case llvm::Triple::GNUEABI:
907330f729Sjoerg     case llvm::Triple::EABI:
917330f729Sjoerg       CmdArgs.push_back("-meabi=5");
927330f729Sjoerg       break;
937330f729Sjoerg 
947330f729Sjoerg     default:
957330f729Sjoerg       CmdArgs.push_back("-matpcs");
967330f729Sjoerg     }
977330f729Sjoerg     break;
987330f729Sjoerg   }
997330f729Sjoerg   case llvm::Triple::sparc:
1007330f729Sjoerg   case llvm::Triple::sparcel:
1017330f729Sjoerg   case llvm::Triple::sparcv9: {
1027330f729Sjoerg     std::string CPU = getCPUName(Args, getToolChain().getTriple());
103*e038c9c4Sjoerg     CmdArgs.push_back(
104*e038c9c4Sjoerg         sparc::getSparcAsmModeForCPU(CPU, getToolChain().getTriple()));
1057330f729Sjoerg     AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
1067330f729Sjoerg     break;
1077330f729Sjoerg   }
1087330f729Sjoerg   }
1097330f729Sjoerg 
110*e038c9c4Sjoerg   for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ,
111*e038c9c4Sjoerg                                     options::OPT_fdebug_prefix_map_EQ)) {
112*e038c9c4Sjoerg     StringRef Map = A->getValue();
113*e038c9c4Sjoerg     if (Map.find('=') == StringRef::npos)
114*e038c9c4Sjoerg       D.Diag(diag::err_drv_invalid_argument_to_option)
115*e038c9c4Sjoerg           << Map << A->getOption().getName();
116*e038c9c4Sjoerg     else {
117*e038c9c4Sjoerg       CmdArgs.push_back(Args.MakeArgString("--debug-prefix-map"));
118*e038c9c4Sjoerg       CmdArgs.push_back(Args.MakeArgString(Map));
119*e038c9c4Sjoerg     }
120*e038c9c4Sjoerg     A->claim();
121*e038c9c4Sjoerg   }
122*e038c9c4Sjoerg 
1237330f729Sjoerg   Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
1247330f729Sjoerg 
1257330f729Sjoerg   CmdArgs.push_back("-o");
1267330f729Sjoerg   CmdArgs.push_back(Output.getFilename());
1277330f729Sjoerg 
1287330f729Sjoerg   for (const auto &II : Inputs)
1297330f729Sjoerg     CmdArgs.push_back(II.getFilename());
1307330f729Sjoerg 
1317330f729Sjoerg   const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
132*e038c9c4Sjoerg   C.addCommand(std::make_unique<Command>(JA, *this,
133*e038c9c4Sjoerg                                          ResponseFileSupport::AtFileCurCP(),
134*e038c9c4Sjoerg                                          Exec, CmdArgs, Inputs, Output));
1357330f729Sjoerg }
1367330f729Sjoerg 
ConstructJob(Compilation & C,const JobAction & JA,const InputInfo & Output,const InputInfoList & Inputs,const ArgList & Args,const char * LinkingOutput) const1377330f729Sjoerg void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
1387330f729Sjoerg                                    const InputInfo &Output,
1397330f729Sjoerg                                    const InputInfoList &Inputs,
1407330f729Sjoerg                                    const ArgList &Args,
1417330f729Sjoerg                                    const char *LinkingOutput) const {
1427330f729Sjoerg   const toolchains::FreeBSD &ToolChain =
1437330f729Sjoerg       static_cast<const toolchains::FreeBSD &>(getToolChain());
1447330f729Sjoerg   const Driver &D = ToolChain.getDriver();
1457330f729Sjoerg   const llvm::Triple::ArchType Arch = ToolChain.getArch();
1467330f729Sjoerg   const bool IsPIE =
1477330f729Sjoerg       !Args.hasArg(options::OPT_shared) &&
1487330f729Sjoerg       (Args.hasArg(options::OPT_pie) || ToolChain.isPIEDefault());
1497330f729Sjoerg   ArgStringList CmdArgs;
1507330f729Sjoerg 
1517330f729Sjoerg   // Silence warning for "clang -g foo.o -o foo"
1527330f729Sjoerg   Args.ClaimAllArgs(options::OPT_g_Group);
1537330f729Sjoerg   // and "clang -emit-llvm foo.o -o foo"
1547330f729Sjoerg   Args.ClaimAllArgs(options::OPT_emit_llvm);
1557330f729Sjoerg   // and for "clang -w foo.o -o foo". Other warning options are already
1567330f729Sjoerg   // handled somewhere else.
1577330f729Sjoerg   Args.ClaimAllArgs(options::OPT_w);
1587330f729Sjoerg 
1597330f729Sjoerg   if (!D.SysRoot.empty())
1607330f729Sjoerg     CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
1617330f729Sjoerg 
1627330f729Sjoerg   if (IsPIE)
1637330f729Sjoerg     CmdArgs.push_back("-pie");
1647330f729Sjoerg 
1657330f729Sjoerg   CmdArgs.push_back("--eh-frame-hdr");
1667330f729Sjoerg   if (Args.hasArg(options::OPT_static)) {
1677330f729Sjoerg     CmdArgs.push_back("-Bstatic");
1687330f729Sjoerg   } else {
1697330f729Sjoerg     if (Args.hasArg(options::OPT_rdynamic))
1707330f729Sjoerg       CmdArgs.push_back("-export-dynamic");
1717330f729Sjoerg     if (Args.hasArg(options::OPT_shared)) {
1727330f729Sjoerg       CmdArgs.push_back("-Bshareable");
1737330f729Sjoerg     } else {
1747330f729Sjoerg       CmdArgs.push_back("-dynamic-linker");
1757330f729Sjoerg       CmdArgs.push_back("/libexec/ld-elf.so.1");
1767330f729Sjoerg     }
177*e038c9c4Sjoerg     const llvm::Triple &T = ToolChain.getTriple();
178*e038c9c4Sjoerg     if (T.getOSMajorVersion() >= 9) {
179*e038c9c4Sjoerg       if (Arch == llvm::Triple::arm || Arch == llvm::Triple::sparc || T.isX86())
1807330f729Sjoerg         CmdArgs.push_back("--hash-style=both");
1817330f729Sjoerg     }
1827330f729Sjoerg     CmdArgs.push_back("--enable-new-dtags");
1837330f729Sjoerg   }
1847330f729Sjoerg 
1857330f729Sjoerg   // Explicitly set the linker emulation for platforms that might not
1867330f729Sjoerg   // be the default emulation for the linker.
1877330f729Sjoerg   switch (Arch) {
1887330f729Sjoerg   case llvm::Triple::x86:
1897330f729Sjoerg     CmdArgs.push_back("-m");
1907330f729Sjoerg     CmdArgs.push_back("elf_i386_fbsd");
1917330f729Sjoerg     break;
1927330f729Sjoerg   case llvm::Triple::ppc:
1937330f729Sjoerg     CmdArgs.push_back("-m");
1947330f729Sjoerg     CmdArgs.push_back("elf32ppc_fbsd");
1957330f729Sjoerg     break;
196*e038c9c4Sjoerg   case llvm::Triple::ppcle:
197*e038c9c4Sjoerg     CmdArgs.push_back("-m");
198*e038c9c4Sjoerg     // Use generic -- only usage is for freestanding.
199*e038c9c4Sjoerg     CmdArgs.push_back("elf32lppc");
200*e038c9c4Sjoerg     break;
2017330f729Sjoerg   case llvm::Triple::mips:
2027330f729Sjoerg     CmdArgs.push_back("-m");
2037330f729Sjoerg     CmdArgs.push_back("elf32btsmip_fbsd");
2047330f729Sjoerg     break;
2057330f729Sjoerg   case llvm::Triple::mipsel:
2067330f729Sjoerg     CmdArgs.push_back("-m");
2077330f729Sjoerg     CmdArgs.push_back("elf32ltsmip_fbsd");
2087330f729Sjoerg     break;
2097330f729Sjoerg   case llvm::Triple::mips64:
2107330f729Sjoerg     CmdArgs.push_back("-m");
2117330f729Sjoerg     if (tools::mips::hasMipsAbiArg(Args, "n32"))
2127330f729Sjoerg       CmdArgs.push_back("elf32btsmipn32_fbsd");
2137330f729Sjoerg     else
2147330f729Sjoerg       CmdArgs.push_back("elf64btsmip_fbsd");
2157330f729Sjoerg     break;
2167330f729Sjoerg   case llvm::Triple::mips64el:
2177330f729Sjoerg     CmdArgs.push_back("-m");
2187330f729Sjoerg     if (tools::mips::hasMipsAbiArg(Args, "n32"))
2197330f729Sjoerg       CmdArgs.push_back("elf32ltsmipn32_fbsd");
2207330f729Sjoerg     else
2217330f729Sjoerg       CmdArgs.push_back("elf64ltsmip_fbsd");
2227330f729Sjoerg     break;
2237330f729Sjoerg   case llvm::Triple::riscv32:
2247330f729Sjoerg     CmdArgs.push_back("-m");
2257330f729Sjoerg     CmdArgs.push_back("elf32lriscv");
2267330f729Sjoerg     break;
2277330f729Sjoerg   case llvm::Triple::riscv64:
2287330f729Sjoerg     CmdArgs.push_back("-m");
2297330f729Sjoerg     CmdArgs.push_back("elf64lriscv");
2307330f729Sjoerg     break;
2317330f729Sjoerg   default:
2327330f729Sjoerg     break;
2337330f729Sjoerg   }
2347330f729Sjoerg 
2357330f729Sjoerg   if (Arg *A = Args.getLastArg(options::OPT_G)) {
2367330f729Sjoerg     if (ToolChain.getTriple().isMIPS()) {
2377330f729Sjoerg       StringRef v = A->getValue();
2387330f729Sjoerg       CmdArgs.push_back(Args.MakeArgString("-G" + v));
2397330f729Sjoerg       A->claim();
2407330f729Sjoerg     }
2417330f729Sjoerg   }
2427330f729Sjoerg 
2437330f729Sjoerg   if (Output.isFilename()) {
2447330f729Sjoerg     CmdArgs.push_back("-o");
2457330f729Sjoerg     CmdArgs.push_back(Output.getFilename());
2467330f729Sjoerg   } else {
2477330f729Sjoerg     assert(Output.isNothing() && "Invalid output.");
2487330f729Sjoerg   }
2497330f729Sjoerg 
2507330f729Sjoerg   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
2517330f729Sjoerg     const char *crt1 = nullptr;
2527330f729Sjoerg     if (!Args.hasArg(options::OPT_shared)) {
2537330f729Sjoerg       if (Args.hasArg(options::OPT_pg))
2547330f729Sjoerg         crt1 = "gcrt1.o";
2557330f729Sjoerg       else if (IsPIE)
2567330f729Sjoerg         crt1 = "Scrt1.o";
2577330f729Sjoerg       else
2587330f729Sjoerg         crt1 = "crt1.o";
2597330f729Sjoerg     }
2607330f729Sjoerg     if (crt1)
2617330f729Sjoerg       CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
2627330f729Sjoerg 
2637330f729Sjoerg     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
2647330f729Sjoerg 
2657330f729Sjoerg     const char *crtbegin = nullptr;
2667330f729Sjoerg     if (Args.hasArg(options::OPT_static))
2677330f729Sjoerg       crtbegin = "crtbeginT.o";
2687330f729Sjoerg     else if (Args.hasArg(options::OPT_shared) || IsPIE)
2697330f729Sjoerg       crtbegin = "crtbeginS.o";
2707330f729Sjoerg     else
2717330f729Sjoerg       crtbegin = "crtbegin.o";
2727330f729Sjoerg 
2737330f729Sjoerg     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
2747330f729Sjoerg   }
2757330f729Sjoerg 
2767330f729Sjoerg   Args.AddAllArgs(CmdArgs, options::OPT_L);
2777330f729Sjoerg   ToolChain.AddFilePathLibArgs(Args, CmdArgs);
2787330f729Sjoerg   Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
2797330f729Sjoerg   Args.AddAllArgs(CmdArgs, options::OPT_e);
2807330f729Sjoerg   Args.AddAllArgs(CmdArgs, options::OPT_s);
2817330f729Sjoerg   Args.AddAllArgs(CmdArgs, options::OPT_t);
2827330f729Sjoerg   Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
2837330f729Sjoerg   Args.AddAllArgs(CmdArgs, options::OPT_r);
2847330f729Sjoerg 
2857330f729Sjoerg   if (D.isUsingLTO()) {
2867330f729Sjoerg     assert(!Inputs.empty() && "Must have at least one input.");
287*e038c9c4Sjoerg     addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs[0],
2887330f729Sjoerg                   D.getLTOMode() == LTOK_Thin);
2897330f729Sjoerg   }
2907330f729Sjoerg 
2917330f729Sjoerg   bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
2927330f729Sjoerg   bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
293*e038c9c4Sjoerg   addLinkerCompressDebugSectionsOption(ToolChain, Args, CmdArgs);
2947330f729Sjoerg   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
2957330f729Sjoerg 
2967330f729Sjoerg   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
2977330f729Sjoerg     // Use the static OpenMP runtime with -static-openmp
2987330f729Sjoerg     bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) &&
2997330f729Sjoerg                         !Args.hasArg(options::OPT_static);
3007330f729Sjoerg     addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP);
3017330f729Sjoerg 
3027330f729Sjoerg     if (D.CCCIsCXX()) {
3037330f729Sjoerg       if (ToolChain.ShouldLinkCXXStdlib(Args))
3047330f729Sjoerg         ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
3057330f729Sjoerg       if (Args.hasArg(options::OPT_pg))
3067330f729Sjoerg         CmdArgs.push_back("-lm_p");
3077330f729Sjoerg       else
3087330f729Sjoerg         CmdArgs.push_back("-lm");
3097330f729Sjoerg     }
3107330f729Sjoerg     if (NeedsSanitizerDeps)
3117330f729Sjoerg       linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
3127330f729Sjoerg     if (NeedsXRayDeps)
3137330f729Sjoerg       linkXRayRuntimeDeps(ToolChain, CmdArgs);
3147330f729Sjoerg     // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
3157330f729Sjoerg     // the default system libraries. Just mimic this for now.
3167330f729Sjoerg     if (Args.hasArg(options::OPT_pg))
3177330f729Sjoerg       CmdArgs.push_back("-lgcc_p");
3187330f729Sjoerg     else
3197330f729Sjoerg       CmdArgs.push_back("-lgcc");
3207330f729Sjoerg     if (Args.hasArg(options::OPT_static)) {
3217330f729Sjoerg       CmdArgs.push_back("-lgcc_eh");
3227330f729Sjoerg     } else if (Args.hasArg(options::OPT_pg)) {
3237330f729Sjoerg       CmdArgs.push_back("-lgcc_eh_p");
3247330f729Sjoerg     } else {
3257330f729Sjoerg       CmdArgs.push_back("--as-needed");
3267330f729Sjoerg       CmdArgs.push_back("-lgcc_s");
3277330f729Sjoerg       CmdArgs.push_back("--no-as-needed");
3287330f729Sjoerg     }
3297330f729Sjoerg 
3307330f729Sjoerg     if (Args.hasArg(options::OPT_pthread)) {
3317330f729Sjoerg       if (Args.hasArg(options::OPT_pg))
3327330f729Sjoerg         CmdArgs.push_back("-lpthread_p");
3337330f729Sjoerg       else
3347330f729Sjoerg         CmdArgs.push_back("-lpthread");
3357330f729Sjoerg     }
3367330f729Sjoerg 
3377330f729Sjoerg     if (Args.hasArg(options::OPT_pg)) {
3387330f729Sjoerg       if (Args.hasArg(options::OPT_shared))
3397330f729Sjoerg         CmdArgs.push_back("-lc");
3407330f729Sjoerg       else
3417330f729Sjoerg         CmdArgs.push_back("-lc_p");
3427330f729Sjoerg       CmdArgs.push_back("-lgcc_p");
3437330f729Sjoerg     } else {
3447330f729Sjoerg       CmdArgs.push_back("-lc");
3457330f729Sjoerg       CmdArgs.push_back("-lgcc");
3467330f729Sjoerg     }
3477330f729Sjoerg 
3487330f729Sjoerg     if (Args.hasArg(options::OPT_static)) {
3497330f729Sjoerg       CmdArgs.push_back("-lgcc_eh");
3507330f729Sjoerg     } else if (Args.hasArg(options::OPT_pg)) {
3517330f729Sjoerg       CmdArgs.push_back("-lgcc_eh_p");
3527330f729Sjoerg     } else {
3537330f729Sjoerg       CmdArgs.push_back("--as-needed");
3547330f729Sjoerg       CmdArgs.push_back("-lgcc_s");
3557330f729Sjoerg       CmdArgs.push_back("--no-as-needed");
3567330f729Sjoerg     }
3577330f729Sjoerg   }
3587330f729Sjoerg 
3597330f729Sjoerg   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
3607330f729Sjoerg     if (Args.hasArg(options::OPT_shared) || IsPIE)
3617330f729Sjoerg       CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o")));
3627330f729Sjoerg     else
3637330f729Sjoerg       CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
3647330f729Sjoerg     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
3657330f729Sjoerg   }
3667330f729Sjoerg 
3677330f729Sjoerg   ToolChain.addProfileRTLibs(Args, CmdArgs);
3687330f729Sjoerg 
3697330f729Sjoerg   const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
370*e038c9c4Sjoerg   C.addCommand(std::make_unique<Command>(JA, *this,
371*e038c9c4Sjoerg                                          ResponseFileSupport::AtFileCurCP(),
372*e038c9c4Sjoerg                                          Exec, CmdArgs, Inputs, Output));
3737330f729Sjoerg }
3747330f729Sjoerg 
3757330f729Sjoerg /// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
3767330f729Sjoerg 
FreeBSD(const Driver & D,const llvm::Triple & Triple,const ArgList & Args)3777330f729Sjoerg FreeBSD::FreeBSD(const Driver &D, const llvm::Triple &Triple,
3787330f729Sjoerg                  const ArgList &Args)
3797330f729Sjoerg     : Generic_ELF(D, Triple, Args) {
3807330f729Sjoerg 
3817330f729Sjoerg   // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
3827330f729Sjoerg   // back to '/usr/lib' if it doesn't exist.
3837330f729Sjoerg   if ((Triple.getArch() == llvm::Triple::x86 || Triple.isMIPS32() ||
384*e038c9c4Sjoerg        Triple.isPPC32()) &&
3857330f729Sjoerg       D.getVFS().exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
3867330f729Sjoerg     getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
3877330f729Sjoerg   else
3887330f729Sjoerg     getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
3897330f729Sjoerg }
3907330f729Sjoerg 
GetDefaultCXXStdlibType() const3917330f729Sjoerg ToolChain::CXXStdlibType FreeBSD::GetDefaultCXXStdlibType() const {
3927330f729Sjoerg   if (getTriple().getOSMajorVersion() >= 10)
3937330f729Sjoerg     return ToolChain::CST_Libcxx;
3947330f729Sjoerg   return ToolChain::CST_Libstdcxx;
3957330f729Sjoerg }
3967330f729Sjoerg 
GetDefaultDwarfVersion() const3977330f729Sjoerg unsigned FreeBSD::GetDefaultDwarfVersion() const {
3987330f729Sjoerg   if (getTriple().getOSMajorVersion() < 12)
3997330f729Sjoerg     return 2;
4007330f729Sjoerg   return 4;
4017330f729Sjoerg }
4027330f729Sjoerg 
addLibCxxIncludePaths(const llvm::opt::ArgList & DriverArgs,llvm::opt::ArgStringList & CC1Args) const403*e038c9c4Sjoerg void FreeBSD::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
404*e038c9c4Sjoerg                                     llvm::opt::ArgStringList &CC1Args) const {
405*e038c9c4Sjoerg   addSystemInclude(DriverArgs, CC1Args,
406*e038c9c4Sjoerg                    getDriver().SysRoot + "/usr/include/c++/v1");
407*e038c9c4Sjoerg }
408*e038c9c4Sjoerg 
addLibStdCxxIncludePaths(const llvm::opt::ArgList & DriverArgs,llvm::opt::ArgStringList & CC1Args) const4097330f729Sjoerg void FreeBSD::addLibStdCxxIncludePaths(
4107330f729Sjoerg     const llvm::opt::ArgList &DriverArgs,
4117330f729Sjoerg     llvm::opt::ArgStringList &CC1Args) const {
412*e038c9c4Sjoerg   addLibStdCXXIncludePaths(getDriver().SysRoot + "/usr/include/c++/4.2", "", "",
413*e038c9c4Sjoerg                            DriverArgs, CC1Args);
4147330f729Sjoerg }
4157330f729Sjoerg 
AddCXXStdlibLibArgs(const ArgList & Args,ArgStringList & CmdArgs) const4167330f729Sjoerg void FreeBSD::AddCXXStdlibLibArgs(const ArgList &Args,
4177330f729Sjoerg                                   ArgStringList &CmdArgs) const {
4187330f729Sjoerg   CXXStdlibType Type = GetCXXStdlibType(Args);
4197330f729Sjoerg   bool Profiling = Args.hasArg(options::OPT_pg);
4207330f729Sjoerg 
4217330f729Sjoerg   switch (Type) {
4227330f729Sjoerg   case ToolChain::CST_Libcxx:
4237330f729Sjoerg     CmdArgs.push_back(Profiling ? "-lc++_p" : "-lc++");
4247330f729Sjoerg     break;
4257330f729Sjoerg 
4267330f729Sjoerg   case ToolChain::CST_Libstdcxx:
4277330f729Sjoerg     CmdArgs.push_back(Profiling ? "-lstdc++_p" : "-lstdc++");
4287330f729Sjoerg     break;
4297330f729Sjoerg   }
4307330f729Sjoerg }
4317330f729Sjoerg 
AddCudaIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const432*e038c9c4Sjoerg void FreeBSD::AddCudaIncludeArgs(const ArgList &DriverArgs,
433*e038c9c4Sjoerg                                  ArgStringList &CC1Args) const {
434*e038c9c4Sjoerg   CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
435*e038c9c4Sjoerg }
436*e038c9c4Sjoerg 
AddHIPIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const437*e038c9c4Sjoerg void FreeBSD::AddHIPIncludeArgs(const ArgList &DriverArgs,
438*e038c9c4Sjoerg                                 ArgStringList &CC1Args) const {
439*e038c9c4Sjoerg   RocmInstallation.AddHIPIncludeArgs(DriverArgs, CC1Args);
440*e038c9c4Sjoerg }
441*e038c9c4Sjoerg 
buildAssembler() const4427330f729Sjoerg Tool *FreeBSD::buildAssembler() const {
4437330f729Sjoerg   return new tools::freebsd::Assembler(*this);
4447330f729Sjoerg }
4457330f729Sjoerg 
buildLinker() const4467330f729Sjoerg Tool *FreeBSD::buildLinker() const { return new tools::freebsd::Linker(*this); }
4477330f729Sjoerg 
GetExceptionModel(const ArgList & Args) const4487330f729Sjoerg llvm::ExceptionHandling FreeBSD::GetExceptionModel(const ArgList &Args) const {
4497330f729Sjoerg   // FreeBSD uses SjLj exceptions on ARM oabi.
4507330f729Sjoerg   switch (getTriple().getEnvironment()) {
4517330f729Sjoerg   case llvm::Triple::GNUEABIHF:
4527330f729Sjoerg   case llvm::Triple::GNUEABI:
4537330f729Sjoerg   case llvm::Triple::EABI:
4547330f729Sjoerg     return llvm::ExceptionHandling::None;
4557330f729Sjoerg   default:
4567330f729Sjoerg     if (getTriple().getArch() == llvm::Triple::arm ||
4577330f729Sjoerg         getTriple().getArch() == llvm::Triple::thumb)
4587330f729Sjoerg       return llvm::ExceptionHandling::SjLj;
4597330f729Sjoerg     return llvm::ExceptionHandling::None;
4607330f729Sjoerg   }
4617330f729Sjoerg }
4627330f729Sjoerg 
HasNativeLLVMSupport() const4637330f729Sjoerg bool FreeBSD::HasNativeLLVMSupport() const { return true; }
4647330f729Sjoerg 
IsUnwindTablesDefault(const ArgList & Args) const465*e038c9c4Sjoerg bool FreeBSD::IsUnwindTablesDefault(const ArgList &Args) const { return true; }
466*e038c9c4Sjoerg 
isPIEDefault() const4677330f729Sjoerg bool FreeBSD::isPIEDefault() const { return getSanitizerArgs().requiresPIE(); }
4687330f729Sjoerg 
getSupportedSanitizers() const4697330f729Sjoerg SanitizerMask FreeBSD::getSupportedSanitizers() const {
470*e038c9c4Sjoerg   const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64;
4717330f729Sjoerg   const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
4727330f729Sjoerg   const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
4737330f729Sjoerg   const bool IsMIPS64 = getTriple().isMIPS64();
4747330f729Sjoerg   SanitizerMask Res = ToolChain::getSupportedSanitizers();
4757330f729Sjoerg   Res |= SanitizerKind::Address;
4767330f729Sjoerg   Res |= SanitizerKind::PointerCompare;
4777330f729Sjoerg   Res |= SanitizerKind::PointerSubtract;
4787330f729Sjoerg   Res |= SanitizerKind::Vptr;
4797330f729Sjoerg   if (IsX86_64 || IsMIPS64) {
4807330f729Sjoerg     Res |= SanitizerKind::Leak;
4817330f729Sjoerg     Res |= SanitizerKind::Thread;
4827330f729Sjoerg   }
4837330f729Sjoerg   if (IsX86 || IsX86_64) {
4847330f729Sjoerg     Res |= SanitizerKind::Function;
4857330f729Sjoerg     Res |= SanitizerKind::SafeStack;
4867330f729Sjoerg     Res |= SanitizerKind::Fuzzer;
4877330f729Sjoerg     Res |= SanitizerKind::FuzzerNoLink;
4887330f729Sjoerg   }
489*e038c9c4Sjoerg   if (IsAArch64 || IsX86_64) {
490*e038c9c4Sjoerg     Res |= SanitizerKind::KernelAddress;
491*e038c9c4Sjoerg     Res |= SanitizerKind::KernelMemory;
492*e038c9c4Sjoerg   }
493*e038c9c4Sjoerg   if (IsX86_64) {
4947330f729Sjoerg     Res |= SanitizerKind::Memory;
495*e038c9c4Sjoerg   }
4967330f729Sjoerg   return Res;
4977330f729Sjoerg }
498*e038c9c4Sjoerg 
addClangTargetOptions(const ArgList & DriverArgs,ArgStringList & CC1Args,Action::OffloadKind) const499*e038c9c4Sjoerg void FreeBSD::addClangTargetOptions(const ArgList &DriverArgs,
500*e038c9c4Sjoerg                                     ArgStringList &CC1Args,
501*e038c9c4Sjoerg                                     Action::OffloadKind) const {
502*e038c9c4Sjoerg   if (!DriverArgs.hasFlag(options::OPT_fuse_init_array,
503*e038c9c4Sjoerg                           options::OPT_fno_use_init_array,
504*e038c9c4Sjoerg                           getTriple().getOSMajorVersion() >= 12))
505*e038c9c4Sjoerg     CC1Args.push_back("-fno-use-init-array");
506*e038c9c4Sjoerg }
507