1 //===--- Solaris.cpp - Solaris ToolChain Implementations --------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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 "clang/Driver/SanitizerArgs.h"
18 #include "clang/Driver/ToolChain.h"
19 #include "llvm/Option/ArgList.h"
20 #include "llvm/Support/FileSystem.h"
21 #include "llvm/Support/Path.h"
23 using namespace clang::driver
;
24 using namespace clang::driver::tools
;
25 using namespace clang::driver::toolchains
;
26 using namespace clang
;
27 using namespace llvm::opt
;
29 void solaris::Assembler::ConstructJob(Compilation
&C
, const JobAction
&JA
,
30 const InputInfo
&Output
,
31 const InputInfoList
&Inputs
,
33 const char *LinkingOutput
) const {
34 claimNoWarnArgs(Args
);
35 ArgStringList CmdArgs
;
37 Args
.AddAllArgValues(CmdArgs
, options::OPT_Wa_COMMA
, options::OPT_Xassembler
);
39 CmdArgs
.push_back("-o");
40 CmdArgs
.push_back(Output
.getFilename());
42 for (const auto &II
: Inputs
)
43 CmdArgs
.push_back(II
.getFilename());
45 const char *Exec
= Args
.MakeArgString(getToolChain().GetProgramPath("as"));
46 C
.addCommand(std::make_unique
<Command
>(JA
, *this, ResponseFileSupport::None(),
47 Exec
, CmdArgs
, Inputs
, Output
));
50 void solaris::Linker::ConstructJob(Compilation
&C
, const JobAction
&JA
,
51 const InputInfo
&Output
,
52 const InputInfoList
&Inputs
,
54 const char *LinkingOutput
) const {
55 ArgStringList CmdArgs
;
57 // Demangle C++ names in errors
58 CmdArgs
.push_back("-C");
60 if (!Args
.hasArg(options::OPT_nostdlib
, options::OPT_shared
)) {
61 CmdArgs
.push_back("-e");
62 CmdArgs
.push_back("_start");
65 if (Args
.hasArg(options::OPT_static
)) {
66 CmdArgs
.push_back("-Bstatic");
67 CmdArgs
.push_back("-dn");
69 CmdArgs
.push_back("-Bdynamic");
70 if (Args
.hasArg(options::OPT_shared
)) {
71 CmdArgs
.push_back("-shared");
74 // libpthread has been folded into libc since Solaris 10, no need to do
75 // anything for pthreads. Claim argument to avoid warning.
76 Args
.ClaimAllArgs(options::OPT_pthread
);
77 Args
.ClaimAllArgs(options::OPT_pthreads
);
80 if (Output
.isFilename()) {
81 CmdArgs
.push_back("-o");
82 CmdArgs
.push_back(Output
.getFilename());
84 assert(Output
.isNothing() && "Invalid output.");
87 if (!Args
.hasArg(options::OPT_nostdlib
, options::OPT_nostartfiles
,
89 if (!Args
.hasArg(options::OPT_shared
))
91 Args
.MakeArgString(getToolChain().GetFilePath("crt1.o")));
93 CmdArgs
.push_back(Args
.MakeArgString(getToolChain().GetFilePath("crti.o")));
95 const Arg
*Std
= Args
.getLastArg(options::OPT_std_EQ
, options::OPT_ansi
);
96 bool HaveAnsi
= false;
97 const LangStandard
*LangStd
= nullptr;
99 HaveAnsi
= Std
->getOption().matches(options::OPT_ansi
);
101 LangStd
= LangStandard::getLangStandardForName(Std
->getValue());
104 const char *values_X
= "values-Xa.o";
105 // Use values-Xc.o for -ansi, -std=c*, -std=iso9899:199409.
106 if (HaveAnsi
|| (LangStd
&& !LangStd
->isGNUMode()))
107 values_X
= "values-Xc.o";
108 CmdArgs
.push_back(Args
.MakeArgString(getToolChain().GetFilePath(values_X
)));
110 const char *values_xpg
= "values-xpg6.o";
111 // Use values-xpg4.o for -std=c90, -std=gnu90, -std=iso9899:199409.
112 if (LangStd
&& LangStd
->getLanguage() == Language::C
&& !LangStd
->isC99())
113 values_xpg
= "values-xpg4.o";
115 Args
.MakeArgString(getToolChain().GetFilePath(values_xpg
)));
117 Args
.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
120 getToolChain().AddFilePathLibArgs(Args
, CmdArgs
);
122 Args
.AddAllArgs(CmdArgs
, {options::OPT_L
, options::OPT_T_Group
,
123 options::OPT_e
, options::OPT_r
});
125 bool NeedsSanitizerDeps
= addSanitizerRuntimes(getToolChain(), Args
, CmdArgs
);
126 AddLinkerInputs(getToolChain(), Inputs
, Args
, CmdArgs
, JA
);
128 if (!Args
.hasArg(options::OPT_nostdlib
, options::OPT_nodefaultlibs
,
130 if (getToolChain().ShouldLinkCXXStdlib(Args
))
131 getToolChain().AddCXXStdlibLibArgs(Args
, CmdArgs
);
132 if (Args
.hasArg(options::OPT_fstack_protector
) ||
133 Args
.hasArg(options::OPT_fstack_protector_strong
) ||
134 Args
.hasArg(options::OPT_fstack_protector_all
)) {
135 // Explicitly link ssp libraries, not folded into Solaris libc.
136 CmdArgs
.push_back("-lssp_nonshared");
137 CmdArgs
.push_back("-lssp");
139 // LLVM support for atomics on 32-bit SPARC V8+ is incomplete, so
140 // forcibly link with libatomic as a workaround.
141 if (getToolChain().getTriple().getArch() == llvm::Triple::sparc
) {
142 CmdArgs
.push_back(getAsNeededOption(getToolChain(), true));
143 CmdArgs
.push_back("-latomic");
144 CmdArgs
.push_back(getAsNeededOption(getToolChain(), false));
146 CmdArgs
.push_back("-lgcc_s");
147 CmdArgs
.push_back("-lc");
148 if (!Args
.hasArg(options::OPT_shared
)) {
149 CmdArgs
.push_back("-lgcc");
150 CmdArgs
.push_back("-lm");
152 if (NeedsSanitizerDeps
) {
153 linkSanitizerRuntimeDeps(getToolChain(), CmdArgs
);
155 // Work around Solaris/amd64 ld bug when calling __tls_get_addr directly.
156 // However, ld -z relax=transtls is available since Solaris 11.2, but not
158 const SanitizerArgs
&SA
= getToolChain().getSanitizerArgs(Args
);
159 if (getToolChain().getTriple().getArch() == llvm::Triple::x86_64
&&
160 (SA
.needsAsanRt() || SA
.needsStatsRt() ||
161 (SA
.needsUbsanRt() && !SA
.requiresMinimalRuntime())))
162 CmdArgs
.push_back("-zrelax=transtls");
166 if (!Args
.hasArg(options::OPT_nostdlib
, options::OPT_nostartfiles
,
169 Args
.MakeArgString(getToolChain().GetFilePath("crtend.o")));
171 Args
.MakeArgString(getToolChain().GetFilePath("crtn.o")));
174 getToolChain().addProfileRTLibs(Args
, CmdArgs
);
176 const char *Exec
= Args
.MakeArgString(getToolChain().GetLinkerPath());
177 C
.addCommand(std::make_unique
<Command
>(JA
, *this, ResponseFileSupport::None(),
178 Exec
, CmdArgs
, Inputs
, Output
));
181 static StringRef
getSolarisLibSuffix(const llvm::Triple
&Triple
) {
182 switch (Triple
.getArch()) {
183 case llvm::Triple::x86
:
184 case llvm::Triple::sparc
:
186 case llvm::Triple::x86_64
:
188 case llvm::Triple::sparcv9
:
191 llvm_unreachable("Unsupported architecture");
196 /// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
198 Solaris::Solaris(const Driver
&D
, const llvm::Triple
&Triple
,
200 : Generic_ELF(D
, Triple
, Args
) {
202 GCCInstallation
.init(Triple
, Args
);
204 StringRef LibSuffix
= getSolarisLibSuffix(Triple
);
205 path_list
&Paths
= getFilePaths();
206 if (GCCInstallation
.isValid()) {
207 // On Solaris gcc uses both an architecture-specific path with triple in it
208 // as well as a more generic lib path (+arch suffix).
210 GCCInstallation
.getInstallPath() +
211 GCCInstallation
.getMultilib().gccSuffix(),
213 addPathIfExists(D
, GCCInstallation
.getParentLibPath() + LibSuffix
, Paths
);
216 // If we are currently running Clang inside of the requested system root,
217 // add its parent library path to those searched.
218 if (StringRef(D
.Dir
).startswith(D
.SysRoot
))
219 addPathIfExists(D
, D
.Dir
+ "/../lib", Paths
);
221 addPathIfExists(D
, D
.SysRoot
+ "/usr/lib" + LibSuffix
, Paths
);
224 SanitizerMask
Solaris::getSupportedSanitizers() const {
225 const bool IsX86
= getTriple().getArch() == llvm::Triple::x86
;
226 const bool IsX86_64
= getTriple().getArch() == llvm::Triple::x86_64
;
227 SanitizerMask Res
= ToolChain::getSupportedSanitizers();
228 // FIXME: Omit X86_64 until 64-bit support is figured out.
230 Res
|= SanitizerKind::Address
;
231 Res
|= SanitizerKind::PointerCompare
;
232 Res
|= SanitizerKind::PointerSubtract
;
234 if (IsX86
|| IsX86_64
)
235 Res
|= SanitizerKind::Function
;
236 Res
|= SanitizerKind::Vptr
;
240 Tool
*Solaris::buildAssembler() const {
241 return new tools::solaris::Assembler(*this);
244 Tool
*Solaris::buildLinker() const { return new tools::solaris::Linker(*this); }
246 void Solaris::AddClangSystemIncludeArgs(const ArgList
&DriverArgs
,
247 ArgStringList
&CC1Args
) const {
248 const Driver
&D
= getDriver();
250 if (DriverArgs
.hasArg(clang::driver::options::OPT_nostdinc
))
253 if (!DriverArgs
.hasArg(options::OPT_nostdlibinc
))
254 addSystemInclude(DriverArgs
, CC1Args
, D
.SysRoot
+ "/usr/local/include");
256 if (!DriverArgs
.hasArg(options::OPT_nobuiltininc
)) {
257 SmallString
<128> P(D
.ResourceDir
);
258 llvm::sys::path::append(P
, "include");
259 addSystemInclude(DriverArgs
, CC1Args
, P
);
262 if (DriverArgs
.hasArg(options::OPT_nostdlibinc
))
265 // Check for configure-time C include directories.
266 StringRef
CIncludeDirs(C_INCLUDE_DIRS
);
267 if (CIncludeDirs
!= "") {
268 SmallVector
<StringRef
, 5> dirs
;
269 CIncludeDirs
.split(dirs
, ":");
270 for (StringRef dir
: dirs
) {
272 llvm::sys::path::is_absolute(dir
) ? "" : StringRef(D
.SysRoot
);
273 addExternCSystemInclude(DriverArgs
, CC1Args
, Prefix
+ dir
);
278 // Add include directories specific to the selected multilib set and multilib.
279 if (GCCInstallation
.isValid()) {
280 const MultilibSet::IncludeDirsFunc
&Callback
=
281 Multilibs
.includeDirsCallback();
283 for (const auto &Path
: Callback(GCCInstallation
.getMultilib()))
284 addExternCSystemIncludeIfExists(
285 DriverArgs
, CC1Args
, GCCInstallation
.getInstallPath() + Path
);
289 addExternCSystemInclude(DriverArgs
, CC1Args
, D
.SysRoot
+ "/usr/include");
292 void Solaris::addLibStdCxxIncludePaths(
293 const llvm::opt::ArgList
&DriverArgs
,
294 llvm::opt::ArgStringList
&CC1Args
) const {
295 // We need a detected GCC installation on Solaris (similar to Linux)
296 // to provide libstdc++'s headers.
297 if (!GCCInstallation
.isValid())
300 // By default, look for the C++ headers in an include directory adjacent to
301 // the lib directory of the GCC installation.
302 // On Solaris this usually looks like /usr/gcc/X.Y/include/c++/X.Y.Z
303 StringRef LibDir
= GCCInstallation
.getParentLibPath();
304 StringRef TripleStr
= GCCInstallation
.getTriple().str();
305 const Multilib
&Multilib
= GCCInstallation
.getMultilib();
306 const GCCVersion
&Version
= GCCInstallation
.getVersion();
308 // The primary search for libstdc++ supports multiarch variants.
309 addLibStdCXXIncludePaths(LibDir
.str() + "/../include/c++/" + Version
.Text
,
310 TripleStr
, Multilib
.includeSuffix(), DriverArgs
,