xref: /freebsd/contrib/llvm-project/clang/lib/Driver/ToolChains/Solaris.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===--- Solaris.cpp - Solaris ToolChain Implementations --------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "Solaris.h"
100b57cec5SDimitry Andric #include "CommonArgs.h"
115f757f3fSDimitry Andric #include "Gnu.h"
12a7dea167SDimitry Andric #include "clang/Basic/LangStandard.h"
130b57cec5SDimitry Andric #include "clang/Config/config.h"
140b57cec5SDimitry Andric #include "clang/Driver/Compilation.h"
150b57cec5SDimitry Andric #include "clang/Driver/Driver.h"
160b57cec5SDimitry Andric #include "clang/Driver/DriverDiagnostic.h"
170b57cec5SDimitry Andric #include "clang/Driver/Options.h"
1881ad6265SDimitry Andric #include "clang/Driver/SanitizerArgs.h"
1981ad6265SDimitry Andric #include "clang/Driver/ToolChain.h"
205f757f3fSDimitry Andric #include "llvm/ADT/StringSwitch.h"
210b57cec5SDimitry Andric #include "llvm/Option/ArgList.h"
220b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
230b57cec5SDimitry Andric #include "llvm/Support/Path.h"
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric using namespace clang::driver;
260b57cec5SDimitry Andric using namespace clang::driver::tools;
270b57cec5SDimitry Andric using namespace clang::driver::toolchains;
280b57cec5SDimitry Andric using namespace clang;
290b57cec5SDimitry Andric using namespace llvm::opt;
300b57cec5SDimitry Andric 
ConstructJob(Compilation & C,const JobAction & JA,const InputInfo & Output,const InputInfoList & Inputs,const ArgList & Args,const char * LinkingOutput) const310b57cec5SDimitry Andric void solaris::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
320b57cec5SDimitry Andric                                       const InputInfo &Output,
330b57cec5SDimitry Andric                                       const InputInfoList &Inputs,
340b57cec5SDimitry Andric                                       const ArgList &Args,
350b57cec5SDimitry Andric                                       const char *LinkingOutput) const {
365f757f3fSDimitry Andric   // Just call the Gnu version, which enforces gas on Solaris.
375f757f3fSDimitry Andric   gnutools::Assembler::ConstructJob(C, JA, Output, Inputs, Args, LinkingOutput);
385f757f3fSDimitry Andric }
390b57cec5SDimitry Andric 
isLinkerGnuLd(const ToolChain & TC,const ArgList & Args)405f757f3fSDimitry Andric bool solaris::isLinkerGnuLd(const ToolChain &TC, const ArgList &Args) {
415f757f3fSDimitry Andric   // Only used if targetting Solaris.
425f757f3fSDimitry Andric   const Arg *A = Args.getLastArg(options::OPT_fuse_ld_EQ);
435f757f3fSDimitry Andric   StringRef UseLinker = A ? A->getValue() : CLANG_DEFAULT_LINKER;
445f757f3fSDimitry Andric   return UseLinker == "bfd" || UseLinker == "gld";
450b57cec5SDimitry Andric }
460b57cec5SDimitry Andric 
getPIE(const ArgList & Args,const ToolChain & TC)478a4dda33SDimitry Andric static bool getPIE(const ArgList &Args, const ToolChain &TC) {
488a4dda33SDimitry Andric   if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_static) ||
498a4dda33SDimitry Andric       Args.hasArg(options::OPT_r))
508a4dda33SDimitry Andric     return false;
518a4dda33SDimitry Andric 
525f757f3fSDimitry Andric   return Args.hasFlag(options::OPT_pie, options::OPT_no_pie,
535f757f3fSDimitry Andric                       TC.isPIEDefault(Args));
545f757f3fSDimitry Andric }
555f757f3fSDimitry Andric 
565f757f3fSDimitry Andric // FIXME: Need to handle CLANG_DEFAULT_LINKER here?
getLinkerPath(const ArgList & Args) const575f757f3fSDimitry Andric std::string solaris::Linker::getLinkerPath(const ArgList &Args) const {
585f757f3fSDimitry Andric   const ToolChain &ToolChain = getToolChain();
595f757f3fSDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
605f757f3fSDimitry Andric     StringRef UseLinker = A->getValue();
615f757f3fSDimitry Andric     if (!UseLinker.empty()) {
625f757f3fSDimitry Andric       if (llvm::sys::path::is_absolute(UseLinker) &&
635f757f3fSDimitry Andric           llvm::sys::fs::can_execute(UseLinker))
645f757f3fSDimitry Andric         return std::string(UseLinker);
655f757f3fSDimitry Andric 
665f757f3fSDimitry Andric       // Accept 'bfd' and 'gld' as aliases for the GNU linker.
675f757f3fSDimitry Andric       if (UseLinker == "bfd" || UseLinker == "gld")
685f757f3fSDimitry Andric         // FIXME: Could also use /usr/bin/gld here.
695f757f3fSDimitry Andric         return "/usr/gnu/bin/ld";
705f757f3fSDimitry Andric 
715f757f3fSDimitry Andric       // Accept 'ld' as alias for the default linker
725f757f3fSDimitry Andric       if (UseLinker != "ld")
735f757f3fSDimitry Andric         ToolChain.getDriver().Diag(diag::err_drv_invalid_linker_name)
745f757f3fSDimitry Andric             << A->getAsString(Args);
755f757f3fSDimitry Andric     }
765f757f3fSDimitry Andric   }
775f757f3fSDimitry Andric 
785f757f3fSDimitry Andric   // getDefaultLinker() always returns an absolute path.
795f757f3fSDimitry Andric   return ToolChain.getDefaultLinker();
808a4dda33SDimitry Andric }
818a4dda33SDimitry Andric 
ConstructJob(Compilation & C,const JobAction & JA,const InputInfo & Output,const InputInfoList & Inputs,const ArgList & Args,const char * LinkingOutput) const820b57cec5SDimitry Andric void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
830b57cec5SDimitry Andric                                    const InputInfo &Output,
840b57cec5SDimitry Andric                                    const InputInfoList &Inputs,
850b57cec5SDimitry Andric                                    const ArgList &Args,
860b57cec5SDimitry Andric                                    const char *LinkingOutput) const {
875f757f3fSDimitry Andric   const auto &ToolChain = static_cast<const Solaris &>(getToolChain());
885f757f3fSDimitry Andric   const Driver &D = ToolChain.getDriver();
895f757f3fSDimitry Andric   const llvm::Triple::ArchType Arch = ToolChain.getArch();
905f757f3fSDimitry Andric   const bool IsPIE = getPIE(Args, ToolChain);
915f757f3fSDimitry Andric   const bool LinkerIsGnuLd = isLinkerGnuLd(ToolChain, Args);
920b57cec5SDimitry Andric   ArgStringList CmdArgs;
930b57cec5SDimitry Andric 
945f757f3fSDimitry Andric   // Demangle C++ names in errors.  GNU ld already defaults to --demangle.
955f757f3fSDimitry Andric   if (!LinkerIsGnuLd)
960b57cec5SDimitry Andric     CmdArgs.push_back("-C");
970b57cec5SDimitry Andric 
985f757f3fSDimitry Andric   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_shared,
995f757f3fSDimitry Andric                    options::OPT_r)) {
1000b57cec5SDimitry Andric     CmdArgs.push_back("-e");
1010b57cec5SDimitry Andric     CmdArgs.push_back("_start");
1020b57cec5SDimitry Andric   }
1030b57cec5SDimitry Andric 
1048a4dda33SDimitry Andric   if (IsPIE) {
1055f757f3fSDimitry Andric     if (LinkerIsGnuLd) {
1065f757f3fSDimitry Andric       CmdArgs.push_back("-pie");
1075f757f3fSDimitry Andric     } else {
1088a4dda33SDimitry Andric       CmdArgs.push_back("-z");
1098a4dda33SDimitry Andric       CmdArgs.push_back("type=pie");
1108a4dda33SDimitry Andric     }
1115f757f3fSDimitry Andric   }
1128a4dda33SDimitry Andric 
1130b57cec5SDimitry Andric   if (Args.hasArg(options::OPT_static)) {
1140b57cec5SDimitry Andric     CmdArgs.push_back("-Bstatic");
1150b57cec5SDimitry Andric     CmdArgs.push_back("-dn");
1160b57cec5SDimitry Andric   } else {
1175f757f3fSDimitry Andric     if (!Args.hasArg(options::OPT_r) && Args.hasArg(options::OPT_shared))
1180b57cec5SDimitry Andric       CmdArgs.push_back("-shared");
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric     // libpthread has been folded into libc since Solaris 10, no need to do
1210b57cec5SDimitry Andric     // anything for pthreads. Claim argument to avoid warning.
1220b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_pthread);
1230b57cec5SDimitry Andric     Args.ClaimAllArgs(options::OPT_pthreads);
1240b57cec5SDimitry Andric   }
1250b57cec5SDimitry Andric 
1265f757f3fSDimitry Andric   if (LinkerIsGnuLd) {
1275f757f3fSDimitry Andric     // Set the correct linker emulation for 32- and 64-bit Solaris.
1285f757f3fSDimitry Andric     switch (Arch) {
1295f757f3fSDimitry Andric     case llvm::Triple::x86:
1305f757f3fSDimitry Andric       CmdArgs.push_back("-m");
1315f757f3fSDimitry Andric       CmdArgs.push_back("elf_i386_sol2");
1325f757f3fSDimitry Andric       break;
1335f757f3fSDimitry Andric     case llvm::Triple::x86_64:
1345f757f3fSDimitry Andric       CmdArgs.push_back("-m");
1355f757f3fSDimitry Andric       CmdArgs.push_back("elf_x86_64_sol2");
1365f757f3fSDimitry Andric       break;
1375f757f3fSDimitry Andric     case llvm::Triple::sparc:
1385f757f3fSDimitry Andric       CmdArgs.push_back("-m");
1395f757f3fSDimitry Andric       CmdArgs.push_back("elf32_sparc_sol2");
1405f757f3fSDimitry Andric       break;
1415f757f3fSDimitry Andric     case llvm::Triple::sparcv9:
1425f757f3fSDimitry Andric       CmdArgs.push_back("-m");
1435f757f3fSDimitry Andric       CmdArgs.push_back("elf64_sparc_sol2");
1445f757f3fSDimitry Andric       break;
1455f757f3fSDimitry Andric     default:
1465f757f3fSDimitry Andric       break;
1475f757f3fSDimitry Andric     }
1485f757f3fSDimitry Andric 
1495f757f3fSDimitry Andric     if (Args.hasArg(options::OPT_rdynamic))
1505f757f3fSDimitry Andric       CmdArgs.push_back("-export-dynamic");
1515f757f3fSDimitry Andric 
1525f757f3fSDimitry Andric     CmdArgs.push_back("--eh-frame-hdr");
1535f757f3fSDimitry Andric   } else {
1545f757f3fSDimitry Andric     // -rdynamic is a no-op with Solaris ld.  Claim argument to avoid warning.
1555f757f3fSDimitry Andric     Args.ClaimAllArgs(options::OPT_rdynamic);
1565f757f3fSDimitry Andric   }
1575f757f3fSDimitry Andric 
1585f757f3fSDimitry Andric   assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
1590b57cec5SDimitry Andric   if (Output.isFilename()) {
1600b57cec5SDimitry Andric     CmdArgs.push_back("-o");
1610b57cec5SDimitry Andric     CmdArgs.push_back(Output.getFilename());
1620b57cec5SDimitry Andric   }
1630b57cec5SDimitry Andric 
1642a66634dSDimitry Andric   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
1652a66634dSDimitry Andric                    options::OPT_r)) {
1660b57cec5SDimitry Andric     if (!Args.hasArg(options::OPT_shared))
1675f757f3fSDimitry Andric       CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt1.o")));
1680b57cec5SDimitry Andric 
1695f757f3fSDimitry Andric     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
170a7dea167SDimitry Andric 
171a7dea167SDimitry Andric     const Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi);
172a7dea167SDimitry Andric     bool HaveAnsi = false;
173a7dea167SDimitry Andric     const LangStandard *LangStd = nullptr;
174a7dea167SDimitry Andric     if (Std) {
175a7dea167SDimitry Andric       HaveAnsi = Std->getOption().matches(options::OPT_ansi);
176a7dea167SDimitry Andric       if (!HaveAnsi)
177a7dea167SDimitry Andric         LangStd = LangStandard::getLangStandardForName(Std->getValue());
178a7dea167SDimitry Andric     }
179a7dea167SDimitry Andric 
180a7dea167SDimitry Andric     const char *values_X = "values-Xa.o";
181a7dea167SDimitry Andric     // Use values-Xc.o for -ansi, -std=c*, -std=iso9899:199409.
182a7dea167SDimitry Andric     if (HaveAnsi || (LangStd && !LangStd->isGNUMode()))
183a7dea167SDimitry Andric       values_X = "values-Xc.o";
1845f757f3fSDimitry Andric     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(values_X)));
185a7dea167SDimitry Andric 
186a7dea167SDimitry Andric     const char *values_xpg = "values-xpg6.o";
187a7dea167SDimitry Andric     // Use values-xpg4.o for -std=c90, -std=gnu90, -std=iso9899:199409.
188a7dea167SDimitry Andric     if (LangStd && LangStd->getLanguage() == Language::C && !LangStd->isC99())
189a7dea167SDimitry Andric       values_xpg = "values-xpg4.o";
1905f757f3fSDimitry Andric     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(values_xpg)));
1918a4dda33SDimitry Andric 
1928a4dda33SDimitry Andric     const char *crtbegin = nullptr;
1938a4dda33SDimitry Andric     if (Args.hasArg(options::OPT_shared) || IsPIE)
1948a4dda33SDimitry Andric       crtbegin = "crtbeginS.o";
1958a4dda33SDimitry Andric     else
1968a4dda33SDimitry Andric       crtbegin = "crtbegin.o";
1975f757f3fSDimitry Andric     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
198bdd1243dSDimitry Andric     // Add crtfastmath.o if available and fast math is enabled.
1995f757f3fSDimitry Andric     ToolChain.addFastMathRuntimeIfAvailable(Args, CmdArgs);
2000b57cec5SDimitry Andric   }
2010b57cec5SDimitry Andric 
2025f757f3fSDimitry Andric   ToolChain.AddFilePathLibArgs(Args, CmdArgs);
2030b57cec5SDimitry Andric 
204*0fca6ea1SDimitry Andric   Args.addAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group});
2050b57cec5SDimitry Andric 
2065f757f3fSDimitry Andric   bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
2075f757f3fSDimitry Andric   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
2080b57cec5SDimitry Andric 
2092a66634dSDimitry Andric   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
2102a66634dSDimitry Andric                    options::OPT_r)) {
2115f757f3fSDimitry Andric     // Use the static OpenMP runtime with -static-openmp
2125f757f3fSDimitry Andric     bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) &&
2135f757f3fSDimitry Andric                         !Args.hasArg(options::OPT_static);
214*0fca6ea1SDimitry Andric     addOpenMPRuntime(C, CmdArgs, ToolChain, Args, StaticOpenMP);
2155f757f3fSDimitry Andric 
216b121cb00SDimitry Andric     if (D.CCCIsCXX()) {
2175f757f3fSDimitry Andric       if (ToolChain.ShouldLinkCXXStdlib(Args))
2185f757f3fSDimitry Andric         ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
2195f757f3fSDimitry Andric       CmdArgs.push_back("-lm");
2205f757f3fSDimitry Andric     }
2215f757f3fSDimitry Andric     // Silence warnings when linking C code with a C++ '-stdlib' argument.
2225f757f3fSDimitry Andric     Args.ClaimAllArgs(options::OPT_stdlib_EQ);
2235f757f3fSDimitry Andric     // Additional linker set-up and flags for Fortran. This is required in order
2245f757f3fSDimitry Andric     // to generate executables. As Fortran runtime depends on the C runtime,
2255f757f3fSDimitry Andric     // these dependencies need to be listed before the C runtime below.
2265f757f3fSDimitry Andric     if (D.IsFlangMode()) {
2275f757f3fSDimitry Andric       addFortranRuntimeLibraryPath(getToolChain(), Args, CmdArgs);
2285f757f3fSDimitry Andric       addFortranRuntimeLibs(getToolChain(), Args, CmdArgs);
229b121cb00SDimitry Andric       CmdArgs.push_back("-lm");
230b121cb00SDimitry Andric     }
2310b57cec5SDimitry Andric     if (Args.hasArg(options::OPT_fstack_protector) ||
2320b57cec5SDimitry Andric         Args.hasArg(options::OPT_fstack_protector_strong) ||
2330b57cec5SDimitry Andric         Args.hasArg(options::OPT_fstack_protector_all)) {
2340b57cec5SDimitry Andric       // Explicitly link ssp libraries, not folded into Solaris libc.
2350b57cec5SDimitry Andric       CmdArgs.push_back("-lssp_nonshared");
2360b57cec5SDimitry Andric       CmdArgs.push_back("-lssp");
2370b57cec5SDimitry Andric     }
238d781ede6SDimitry Andric     // LLVM support for atomics on 32-bit SPARC V8+ is incomplete, so
239d781ede6SDimitry Andric     // forcibly link with libatomic as a workaround.
2405f757f3fSDimitry Andric     if (Arch == llvm::Triple::sparc) {
2415f757f3fSDimitry Andric       addAsNeededOption(ToolChain, Args, CmdArgs, true);
242d781ede6SDimitry Andric       CmdArgs.push_back("-latomic");
2435f757f3fSDimitry Andric       addAsNeededOption(ToolChain, Args, CmdArgs, false);
244d781ede6SDimitry Andric     }
2455f757f3fSDimitry Andric     addAsNeededOption(ToolChain, Args, CmdArgs, true);
2460b57cec5SDimitry Andric     CmdArgs.push_back("-lgcc_s");
2475f757f3fSDimitry Andric     addAsNeededOption(ToolChain, Args, CmdArgs, false);
2480b57cec5SDimitry Andric     CmdArgs.push_back("-lc");
2490b57cec5SDimitry Andric     if (!Args.hasArg(options::OPT_shared)) {
2500b57cec5SDimitry Andric       CmdArgs.push_back("-lgcc");
2510b57cec5SDimitry Andric     }
2525f757f3fSDimitry Andric     const SanitizerArgs &SA = ToolChain.getSanitizerArgs(Args);
25381ad6265SDimitry Andric     if (NeedsSanitizerDeps) {
2545f757f3fSDimitry Andric       linkSanitizerRuntimeDeps(ToolChain, Args, CmdArgs);
25581ad6265SDimitry Andric 
25681ad6265SDimitry Andric       // Work around Solaris/amd64 ld bug when calling __tls_get_addr directly.
25781ad6265SDimitry Andric       // However, ld -z relax=transtls is available since Solaris 11.2, but not
25881ad6265SDimitry Andric       // in Illumos.
2595f757f3fSDimitry Andric       if (Arch == llvm::Triple::x86_64 &&
26081ad6265SDimitry Andric           (SA.needsAsanRt() || SA.needsStatsRt() ||
2615f757f3fSDimitry Andric            (SA.needsUbsanRt() && !SA.requiresMinimalRuntime())) &&
2625f757f3fSDimitry Andric           !LinkerIsGnuLd) {
2635f757f3fSDimitry Andric         CmdArgs.push_back("-z");
2645f757f3fSDimitry Andric         CmdArgs.push_back("relax=transtls");
2655f757f3fSDimitry Andric       }
26681ad6265SDimitry Andric     }
2678a4dda33SDimitry Andric     // Avoid AsanInitInternal cycle, Issue #64126.
2685f757f3fSDimitry Andric     if (ToolChain.getTriple().isX86() && SA.needsSharedRt() &&
2695f757f3fSDimitry Andric         SA.needsAsanRt()) {
2705f757f3fSDimitry Andric       CmdArgs.push_back("-z");
2715f757f3fSDimitry Andric       CmdArgs.push_back("now");
2725f757f3fSDimitry Andric     }
2730b57cec5SDimitry Andric   }
2740b57cec5SDimitry Andric 
2752a66634dSDimitry Andric   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
2762a66634dSDimitry Andric                    options::OPT_r)) {
2775f757f3fSDimitry Andric     const char *crtend = nullptr;
2788a4dda33SDimitry Andric     if (Args.hasArg(options::OPT_shared) || IsPIE)
2795f757f3fSDimitry Andric       crtend = "crtendS.o";
2808a4dda33SDimitry Andric     else
2815f757f3fSDimitry Andric       crtend = "crtend.o";
2825f757f3fSDimitry Andric     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
2835f757f3fSDimitry Andric     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
2840b57cec5SDimitry Andric   }
2850b57cec5SDimitry Andric 
2865f757f3fSDimitry Andric   ToolChain.addProfileRTLibs(Args, CmdArgs);
2870b57cec5SDimitry Andric 
2885f757f3fSDimitry Andric   const char *Exec = Args.MakeArgString(getLinkerPath(Args));
2895ffd83dbSDimitry Andric   C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
290e8d8bef9SDimitry Andric                                          Exec, CmdArgs, Inputs, Output));
2910b57cec5SDimitry Andric }
2920b57cec5SDimitry Andric 
getSolarisLibSuffix(const llvm::Triple & Triple)2930b57cec5SDimitry Andric static StringRef getSolarisLibSuffix(const llvm::Triple &Triple) {
2940b57cec5SDimitry Andric   switch (Triple.getArch()) {
2950b57cec5SDimitry Andric   case llvm::Triple::x86:
2960b57cec5SDimitry Andric   case llvm::Triple::sparc:
297647cbc5dSDimitry Andric   default:
2980b57cec5SDimitry Andric     break;
2990b57cec5SDimitry Andric   case llvm::Triple::x86_64:
3000b57cec5SDimitry Andric     return "/amd64";
3010b57cec5SDimitry Andric   case llvm::Triple::sparcv9:
3020b57cec5SDimitry Andric     return "/sparcv9";
3030b57cec5SDimitry Andric   }
3040b57cec5SDimitry Andric   return "";
3050b57cec5SDimitry Andric }
3060b57cec5SDimitry Andric 
3070b57cec5SDimitry Andric /// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
3080b57cec5SDimitry Andric 
Solaris(const Driver & D,const llvm::Triple & Triple,const ArgList & Args)3090b57cec5SDimitry Andric Solaris::Solaris(const Driver &D, const llvm::Triple &Triple,
3100b57cec5SDimitry Andric                  const ArgList &Args)
3110b57cec5SDimitry Andric     : Generic_ELF(D, Triple, Args) {
3120b57cec5SDimitry Andric 
3130b57cec5SDimitry Andric   GCCInstallation.init(Triple, Args);
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric   StringRef LibSuffix = getSolarisLibSuffix(Triple);
3160b57cec5SDimitry Andric   path_list &Paths = getFilePaths();
3170b57cec5SDimitry Andric   if (GCCInstallation.isValid()) {
3180b57cec5SDimitry Andric     // On Solaris gcc uses both an architecture-specific path with triple in it
3190b57cec5SDimitry Andric     // as well as a more generic lib path (+arch suffix).
3200b57cec5SDimitry Andric     addPathIfExists(D,
3210b57cec5SDimitry Andric                     GCCInstallation.getInstallPath() +
3220b57cec5SDimitry Andric                         GCCInstallation.getMultilib().gccSuffix(),
3230b57cec5SDimitry Andric                     Paths);
3240b57cec5SDimitry Andric     addPathIfExists(D, GCCInstallation.getParentLibPath() + LibSuffix, Paths);
3250b57cec5SDimitry Andric   }
3260b57cec5SDimitry Andric 
3270b57cec5SDimitry Andric   // If we are currently running Clang inside of the requested system root,
3280b57cec5SDimitry Andric   // add its parent library path to those searched.
3295f757f3fSDimitry Andric   if (StringRef(D.Dir).starts_with(D.SysRoot))
3300b57cec5SDimitry Andric     addPathIfExists(D, D.Dir + "/../lib", Paths);
3310b57cec5SDimitry Andric 
3320b57cec5SDimitry Andric   addPathIfExists(D, D.SysRoot + "/usr/lib" + LibSuffix, Paths);
3330b57cec5SDimitry Andric }
3340b57cec5SDimitry Andric 
getSupportedSanitizers() const3350b57cec5SDimitry Andric SanitizerMask Solaris::getSupportedSanitizers() const {
3360b57cec5SDimitry Andric   const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
3370b57cec5SDimitry Andric   SanitizerMask Res = ToolChain::getSupportedSanitizers();
3380b57cec5SDimitry Andric   // FIXME: Omit X86_64 until 64-bit support is figured out.
3390b57cec5SDimitry Andric   if (IsX86) {
3400b57cec5SDimitry Andric     Res |= SanitizerKind::Address;
3410b57cec5SDimitry Andric     Res |= SanitizerKind::PointerCompare;
3420b57cec5SDimitry Andric     Res |= SanitizerKind::PointerSubtract;
3430b57cec5SDimitry Andric   }
344*0fca6ea1SDimitry Andric   Res |= SanitizerKind::SafeStack;
3450b57cec5SDimitry Andric   Res |= SanitizerKind::Vptr;
3460b57cec5SDimitry Andric   return Res;
3470b57cec5SDimitry Andric }
3480b57cec5SDimitry Andric 
getDefaultLinker() const3495f757f3fSDimitry Andric const char *Solaris::getDefaultLinker() const {
3505f757f3fSDimitry Andric   // FIXME: Only handle Solaris ld and GNU ld here.
3515f757f3fSDimitry Andric   return llvm::StringSwitch<const char *>(CLANG_DEFAULT_LINKER)
3525f757f3fSDimitry Andric       .Cases("bfd", "gld", "/usr/gnu/bin/ld")
3535f757f3fSDimitry Andric       .Default("/usr/bin/ld");
3545f757f3fSDimitry Andric }
3555f757f3fSDimitry Andric 
buildAssembler() const3560b57cec5SDimitry Andric Tool *Solaris::buildAssembler() const {
3570b57cec5SDimitry Andric   return new tools::solaris::Assembler(*this);
3580b57cec5SDimitry Andric }
3590b57cec5SDimitry Andric 
buildLinker() const3600b57cec5SDimitry Andric Tool *Solaris::buildLinker() const { return new tools::solaris::Linker(*this); }
3610b57cec5SDimitry Andric 
AddClangSystemIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const3620b57cec5SDimitry Andric void Solaris::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
3630b57cec5SDimitry Andric                                         ArgStringList &CC1Args) const {
3640b57cec5SDimitry Andric   const Driver &D = getDriver();
3650b57cec5SDimitry Andric 
3660b57cec5SDimitry Andric   if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
3670b57cec5SDimitry Andric     return;
3680b57cec5SDimitry Andric 
3690b57cec5SDimitry Andric   if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
3700b57cec5SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/local/include");
3710b57cec5SDimitry Andric 
3720b57cec5SDimitry Andric   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
3730b57cec5SDimitry Andric     SmallString<128> P(D.ResourceDir);
3740b57cec5SDimitry Andric     llvm::sys::path::append(P, "include");
3750b57cec5SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, P);
3760b57cec5SDimitry Andric   }
3770b57cec5SDimitry Andric 
3780b57cec5SDimitry Andric   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
3790b57cec5SDimitry Andric     return;
3800b57cec5SDimitry Andric 
3810b57cec5SDimitry Andric   // Check for configure-time C include directories.
3820b57cec5SDimitry Andric   StringRef CIncludeDirs(C_INCLUDE_DIRS);
3830b57cec5SDimitry Andric   if (CIncludeDirs != "") {
3840b57cec5SDimitry Andric     SmallVector<StringRef, 5> dirs;
3850b57cec5SDimitry Andric     CIncludeDirs.split(dirs, ":");
3860b57cec5SDimitry Andric     for (StringRef dir : dirs) {
3870b57cec5SDimitry Andric       StringRef Prefix =
3885ffd83dbSDimitry Andric           llvm::sys::path::is_absolute(dir) ? "" : StringRef(D.SysRoot);
3890b57cec5SDimitry Andric       addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
3900b57cec5SDimitry Andric     }
3910b57cec5SDimitry Andric     return;
3920b57cec5SDimitry Andric   }
3930b57cec5SDimitry Andric 
3940b57cec5SDimitry Andric   // Add include directories specific to the selected multilib set and multilib.
3950b57cec5SDimitry Andric   if (GCCInstallation.isValid()) {
3960b57cec5SDimitry Andric     const MultilibSet::IncludeDirsFunc &Callback =
3970b57cec5SDimitry Andric         Multilibs.includeDirsCallback();
3980b57cec5SDimitry Andric     if (Callback) {
3990b57cec5SDimitry Andric       for (const auto &Path : Callback(GCCInstallation.getMultilib()))
4000b57cec5SDimitry Andric         addExternCSystemIncludeIfExists(
4010b57cec5SDimitry Andric             DriverArgs, CC1Args, GCCInstallation.getInstallPath() + Path);
4020b57cec5SDimitry Andric     }
4030b57cec5SDimitry Andric   }
4040b57cec5SDimitry Andric 
4050b57cec5SDimitry Andric   addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include");
4060b57cec5SDimitry Andric }
4070b57cec5SDimitry Andric 
addLibStdCxxIncludePaths(const llvm::opt::ArgList & DriverArgs,llvm::opt::ArgStringList & CC1Args) const4080b57cec5SDimitry Andric void Solaris::addLibStdCxxIncludePaths(
4090b57cec5SDimitry Andric     const llvm::opt::ArgList &DriverArgs,
4100b57cec5SDimitry Andric     llvm::opt::ArgStringList &CC1Args) const {
4110b57cec5SDimitry Andric   // We need a detected GCC installation on Solaris (similar to Linux)
4120b57cec5SDimitry Andric   // to provide libstdc++'s headers.
4130b57cec5SDimitry Andric   if (!GCCInstallation.isValid())
4140b57cec5SDimitry Andric     return;
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric   // By default, look for the C++ headers in an include directory adjacent to
4170b57cec5SDimitry Andric   // the lib directory of the GCC installation.
4180b57cec5SDimitry Andric   // On Solaris this usually looks like /usr/gcc/X.Y/include/c++/X.Y.Z
4190b57cec5SDimitry Andric   StringRef LibDir = GCCInstallation.getParentLibPath();
4200b57cec5SDimitry Andric   StringRef TripleStr = GCCInstallation.getTriple().str();
4210b57cec5SDimitry Andric   const Multilib &Multilib = GCCInstallation.getMultilib();
4220b57cec5SDimitry Andric   const GCCVersion &Version = GCCInstallation.getVersion();
4230b57cec5SDimitry Andric 
4240b57cec5SDimitry Andric   // The primary search for libstdc++ supports multiarch variants.
425fe6060f1SDimitry Andric   addLibStdCXXIncludePaths(LibDir.str() + "/../include/c++/" + Version.Text,
426fe6060f1SDimitry Andric                            TripleStr, Multilib.includeSuffix(), DriverArgs,
427fe6060f1SDimitry Andric                            CC1Args);
4280b57cec5SDimitry Andric }
429