xref: /freebsd/contrib/llvm-project/clang/lib/Driver/ToolChains/Solaris.cpp (revision d30a1689f5b37e78ea189232a8b94a7011dc0dc8)
1 //===--- Solaris.cpp - Solaris 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 "Solaris.h"
10 #include "CommonArgs.h"
11 #include "clang/Basic/LangStandard.h"
12 #include "clang/Config/config.h"
13 #include "clang/Driver/Compilation.h"
14 #include "clang/Driver/Driver.h"
15 #include "clang/Driver/DriverDiagnostic.h"
16 #include "clang/Driver/Options.h"
17 #include "llvm/Option/ArgList.h"
18 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/Path.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 solaris::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 
35   Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
36 
37   CmdArgs.push_back("-o");
38   CmdArgs.push_back(Output.getFilename());
39 
40   for (const auto &II : Inputs)
41     CmdArgs.push_back(II.getFilename());
42 
43   const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
44   C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
45                                          Exec, CmdArgs, Inputs, Output));
46 }
47 
48 void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
49                                    const InputInfo &Output,
50                                    const InputInfoList &Inputs,
51                                    const ArgList &Args,
52                                    const char *LinkingOutput) const {
53   ArgStringList CmdArgs;
54 
55   // Demangle C++ names in errors
56   CmdArgs.push_back("-C");
57 
58   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_shared)) {
59     CmdArgs.push_back("-e");
60     CmdArgs.push_back("_start");
61   }
62 
63   if (Args.hasArg(options::OPT_static)) {
64     CmdArgs.push_back("-Bstatic");
65     CmdArgs.push_back("-dn");
66   } else {
67     CmdArgs.push_back("-Bdynamic");
68     if (Args.hasArg(options::OPT_shared)) {
69       CmdArgs.push_back("-shared");
70     }
71 
72     // libpthread has been folded into libc since Solaris 10, no need to do
73     // anything for pthreads. Claim argument to avoid warning.
74     Args.ClaimAllArgs(options::OPT_pthread);
75     Args.ClaimAllArgs(options::OPT_pthreads);
76   }
77 
78   if (Output.isFilename()) {
79     CmdArgs.push_back("-o");
80     CmdArgs.push_back(Output.getFilename());
81   } else {
82     assert(Output.isNothing() && "Invalid output.");
83   }
84 
85   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
86                    options::OPT_r)) {
87     if (!Args.hasArg(options::OPT_shared))
88       CmdArgs.push_back(
89           Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
90 
91     CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
92 
93     const Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi);
94     bool HaveAnsi = false;
95     const LangStandard *LangStd = nullptr;
96     if (Std) {
97       HaveAnsi = Std->getOption().matches(options::OPT_ansi);
98       if (!HaveAnsi)
99         LangStd = LangStandard::getLangStandardForName(Std->getValue());
100     }
101 
102     const char *values_X = "values-Xa.o";
103     // Use values-Xc.o for -ansi, -std=c*, -std=iso9899:199409.
104     if (HaveAnsi || (LangStd && !LangStd->isGNUMode()))
105       values_X = "values-Xc.o";
106     CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(values_X)));
107 
108     const char *values_xpg = "values-xpg6.o";
109     // Use values-xpg4.o for -std=c90, -std=gnu90, -std=iso9899:199409.
110     if (LangStd && LangStd->getLanguage() == Language::C && !LangStd->isC99())
111       values_xpg = "values-xpg4.o";
112     CmdArgs.push_back(
113         Args.MakeArgString(getToolChain().GetFilePath(values_xpg)));
114     CmdArgs.push_back(
115         Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
116   }
117 
118   getToolChain().AddFilePathLibArgs(Args, CmdArgs);
119 
120   Args.AddAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group,
121                             options::OPT_e, options::OPT_r});
122 
123   bool NeedsSanitizerDeps = addSanitizerRuntimes(getToolChain(), Args, CmdArgs);
124   AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
125 
126   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
127                    options::OPT_r)) {
128     if (getToolChain().ShouldLinkCXXStdlib(Args))
129       getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
130     if (Args.hasArg(options::OPT_fstack_protector) ||
131         Args.hasArg(options::OPT_fstack_protector_strong) ||
132         Args.hasArg(options::OPT_fstack_protector_all)) {
133       // Explicitly link ssp libraries, not folded into Solaris libc.
134       CmdArgs.push_back("-lssp_nonshared");
135       CmdArgs.push_back("-lssp");
136     }
137     // LLVM support for atomics on 32-bit SPARC V8+ is incomplete, so
138     // forcibly link with libatomic as a workaround.
139     if (getToolChain().getTriple().getArch() == llvm::Triple::sparc) {
140       CmdArgs.push_back(getAsNeededOption(getToolChain(), true));
141       CmdArgs.push_back("-latomic");
142       CmdArgs.push_back(getAsNeededOption(getToolChain(), false));
143     }
144     CmdArgs.push_back("-lgcc_s");
145     CmdArgs.push_back("-lc");
146     if (!Args.hasArg(options::OPT_shared)) {
147       CmdArgs.push_back("-lgcc");
148       CmdArgs.push_back("-lm");
149     }
150     if (NeedsSanitizerDeps)
151       linkSanitizerRuntimeDeps(getToolChain(), CmdArgs);
152   }
153 
154   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
155                    options::OPT_r)) {
156     CmdArgs.push_back(
157         Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
158     CmdArgs.push_back(
159         Args.MakeArgString(getToolChain().GetFilePath("crtn.o")));
160   }
161 
162   getToolChain().addProfileRTLibs(Args, CmdArgs);
163 
164   const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
165   C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
166                                          Exec, CmdArgs, Inputs, Output));
167 }
168 
169 static StringRef getSolarisLibSuffix(const llvm::Triple &Triple) {
170   switch (Triple.getArch()) {
171   case llvm::Triple::x86:
172   case llvm::Triple::sparc:
173     break;
174   case llvm::Triple::x86_64:
175     return "/amd64";
176   case llvm::Triple::sparcv9:
177     return "/sparcv9";
178   default:
179     llvm_unreachable("Unsupported architecture");
180   }
181   return "";
182 }
183 
184 /// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
185 
186 Solaris::Solaris(const Driver &D, const llvm::Triple &Triple,
187                  const ArgList &Args)
188     : Generic_ELF(D, Triple, Args) {
189 
190   GCCInstallation.init(Triple, Args);
191 
192   StringRef LibSuffix = getSolarisLibSuffix(Triple);
193   path_list &Paths = getFilePaths();
194   if (GCCInstallation.isValid()) {
195     // On Solaris gcc uses both an architecture-specific path with triple in it
196     // as well as a more generic lib path (+arch suffix).
197     addPathIfExists(D,
198                     GCCInstallation.getInstallPath() +
199                         GCCInstallation.getMultilib().gccSuffix(),
200                     Paths);
201     addPathIfExists(D, GCCInstallation.getParentLibPath() + LibSuffix, Paths);
202   }
203 
204   // If we are currently running Clang inside of the requested system root,
205   // add its parent library path to those searched.
206   if (StringRef(D.Dir).startswith(D.SysRoot))
207     addPathIfExists(D, D.Dir + "/../lib", Paths);
208 
209   addPathIfExists(D, D.SysRoot + "/usr/lib" + LibSuffix, Paths);
210 }
211 
212 SanitizerMask Solaris::getSupportedSanitizers() const {
213   const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
214   const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
215   SanitizerMask Res = ToolChain::getSupportedSanitizers();
216   // FIXME: Omit X86_64 until 64-bit support is figured out.
217   if (IsX86) {
218     Res |= SanitizerKind::Address;
219     Res |= SanitizerKind::PointerCompare;
220     Res |= SanitizerKind::PointerSubtract;
221   }
222   if (IsX86 || IsX86_64)
223     Res |= SanitizerKind::Function;
224   Res |= SanitizerKind::Vptr;
225   return Res;
226 }
227 
228 Tool *Solaris::buildAssembler() const {
229   return new tools::solaris::Assembler(*this);
230 }
231 
232 Tool *Solaris::buildLinker() const { return new tools::solaris::Linker(*this); }
233 
234 void Solaris::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
235                                         ArgStringList &CC1Args) const {
236   const Driver &D = getDriver();
237 
238   if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
239     return;
240 
241   if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
242     addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/local/include");
243 
244   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
245     SmallString<128> P(D.ResourceDir);
246     llvm::sys::path::append(P, "include");
247     addSystemInclude(DriverArgs, CC1Args, P);
248   }
249 
250   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
251     return;
252 
253   // Check for configure-time C include directories.
254   StringRef CIncludeDirs(C_INCLUDE_DIRS);
255   if (CIncludeDirs != "") {
256     SmallVector<StringRef, 5> dirs;
257     CIncludeDirs.split(dirs, ":");
258     for (StringRef dir : dirs) {
259       StringRef Prefix =
260           llvm::sys::path::is_absolute(dir) ? "" : StringRef(D.SysRoot);
261       addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
262     }
263     return;
264   }
265 
266   // Add include directories specific to the selected multilib set and multilib.
267   if (GCCInstallation.isValid()) {
268     const MultilibSet::IncludeDirsFunc &Callback =
269         Multilibs.includeDirsCallback();
270     if (Callback) {
271       for (const auto &Path : Callback(GCCInstallation.getMultilib()))
272         addExternCSystemIncludeIfExists(
273             DriverArgs, CC1Args, GCCInstallation.getInstallPath() + Path);
274     }
275   }
276 
277   addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include");
278 }
279 
280 void Solaris::addLibStdCxxIncludePaths(
281     const llvm::opt::ArgList &DriverArgs,
282     llvm::opt::ArgStringList &CC1Args) const {
283   // We need a detected GCC installation on Solaris (similar to Linux)
284   // to provide libstdc++'s headers.
285   if (!GCCInstallation.isValid())
286     return;
287 
288   // By default, look for the C++ headers in an include directory adjacent to
289   // the lib directory of the GCC installation.
290   // On Solaris this usually looks like /usr/gcc/X.Y/include/c++/X.Y.Z
291   StringRef LibDir = GCCInstallation.getParentLibPath();
292   StringRef TripleStr = GCCInstallation.getTriple().str();
293   const Multilib &Multilib = GCCInstallation.getMultilib();
294   const GCCVersion &Version = GCCInstallation.getVersion();
295 
296   // The primary search for libstdc++ supports multiarch variants.
297   addLibStdCXXIncludePaths(LibDir.str() + "/../include/c++/" + Version.Text,
298                            TripleStr, Multilib.includeSuffix(), DriverArgs,
299                            CC1Args);
300 }
301