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"
12 #include "clang/Basic/LangStandard.h"
13 #include "clang/Config/config.h"
14 #include "clang/Driver/Compilation.h"
15 #include "clang/Driver/Driver.h"
16 #include "clang/Driver/DriverDiagnostic.h"
17 #include "clang/Driver/Options.h"
18 #include "clang/Driver/SanitizerArgs.h"
19 #include "clang/Driver/ToolChain.h"
20 #include "llvm/ADT/StringSwitch.h"
21 #include "llvm/Option/ArgList.h"
22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/Path.h"
25 using namespace clang::driver
;
26 using namespace clang::driver::tools
;
27 using namespace clang::driver::toolchains
;
28 using namespace clang
;
29 using namespace llvm::opt
;
31 void solaris::Assembler::ConstructJob(Compilation
&C
, const JobAction
&JA
,
32 const InputInfo
&Output
,
33 const InputInfoList
&Inputs
,
35 const char *LinkingOutput
) const {
36 // Just call the Gnu version, which enforces gas on Solaris.
37 gnutools::Assembler::ConstructJob(C
, JA
, Output
, Inputs
, Args
, LinkingOutput
);
40 bool solaris::isLinkerGnuLd(const ToolChain
&TC
, const ArgList
&Args
) {
41 // Only used if targetting Solaris.
42 const Arg
*A
= Args
.getLastArg(options::OPT_fuse_ld_EQ
);
43 StringRef UseLinker
= A
? A
->getValue() : CLANG_DEFAULT_LINKER
;
44 return UseLinker
== "bfd" || UseLinker
== "gld";
47 static bool getPIE(const ArgList
&Args
, const ToolChain
&TC
) {
48 if (Args
.hasArg(options::OPT_shared
) || Args
.hasArg(options::OPT_static
) ||
49 Args
.hasArg(options::OPT_r
))
52 Arg
*A
= Args
.getLastArg(options::OPT_pie
, options::OPT_no_pie
,
55 return TC
.isPIEDefault(Args
);
56 return A
->getOption().matches(options::OPT_pie
);
59 // FIXME: Need to handle CLANG_DEFAULT_LINKER here?
60 std::string
solaris::Linker::getLinkerPath(const ArgList
&Args
) const {
61 const ToolChain
&ToolChain
= getToolChain();
62 if (const Arg
*A
= Args
.getLastArg(options::OPT_fuse_ld_EQ
)) {
63 StringRef UseLinker
= A
->getValue();
64 if (!UseLinker
.empty()) {
65 if (llvm::sys::path::is_absolute(UseLinker
) &&
66 llvm::sys::fs::can_execute(UseLinker
))
67 return std::string(UseLinker
);
69 // Accept 'bfd' and 'gld' as aliases for the GNU linker.
70 if (UseLinker
== "bfd" || UseLinker
== "gld")
71 // FIXME: Could also use /usr/bin/gld here.
72 return "/usr/gnu/bin/ld";
74 // Accept 'ld' as alias for the default linker
75 if (UseLinker
!= "ld")
76 ToolChain
.getDriver().Diag(diag::err_drv_invalid_linker_name
)
77 << A
->getAsString(Args
);
81 // getDefaultLinker() always returns an absolute path.
82 return ToolChain
.getDefaultLinker();
85 void solaris::Linker::ConstructJob(Compilation
&C
, const JobAction
&JA
,
86 const InputInfo
&Output
,
87 const InputInfoList
&Inputs
,
89 const char *LinkingOutput
) const {
90 const Driver
&D
= getToolChain().getDriver();
91 const bool IsPIE
= getPIE(Args
, getToolChain());
92 ArgStringList CmdArgs
;
93 bool LinkerIsGnuLd
= isLinkerGnuLd(getToolChain(), Args
);
95 // Demangle C++ names in errors. GNU ld already defaults to --demangle.
97 CmdArgs
.push_back("-C");
99 if (!Args
.hasArg(options::OPT_nostdlib
, options::OPT_shared
,
101 CmdArgs
.push_back("-e");
102 CmdArgs
.push_back("_start");
107 CmdArgs
.push_back("-pie");
109 CmdArgs
.push_back("-z");
110 CmdArgs
.push_back("type=pie");
114 if (Args
.hasArg(options::OPT_static
)) {
115 CmdArgs
.push_back("-Bstatic");
116 CmdArgs
.push_back("-dn");
118 if (!Args
.hasArg(options::OPT_r
) && Args
.hasArg(options::OPT_shared
))
119 CmdArgs
.push_back("-shared");
121 // libpthread has been folded into libc since Solaris 10, no need to do
122 // anything for pthreads. Claim argument to avoid warning.
123 Args
.ClaimAllArgs(options::OPT_pthread
);
124 Args
.ClaimAllArgs(options::OPT_pthreads
);
128 // Set the correct linker emulation for 32- and 64-bit Solaris.
129 const auto &ToolChain
= static_cast<const Solaris
&>(getToolChain());
130 const llvm::Triple::ArchType Arch
= ToolChain
.getArch();
133 case llvm::Triple::x86
:
134 CmdArgs
.push_back("-m");
135 CmdArgs
.push_back("elf_i386_sol2");
137 case llvm::Triple::x86_64
:
138 CmdArgs
.push_back("-m");
139 CmdArgs
.push_back("elf_x86_64_sol2");
141 case llvm::Triple::sparc
:
142 CmdArgs
.push_back("-m");
143 CmdArgs
.push_back("elf32_sparc_sol2");
145 case llvm::Triple::sparcv9
:
146 CmdArgs
.push_back("-m");
147 CmdArgs
.push_back("elf64_sparc_sol2");
153 if (Args
.hasArg(options::OPT_rdynamic
))
154 CmdArgs
.push_back("-export-dynamic");
156 CmdArgs
.push_back("--eh-frame-hdr");
158 // -rdynamic is a no-op with Solaris ld. Claim argument to avoid warning.
159 Args
.ClaimAllArgs(options::OPT_rdynamic
);
162 assert((Output
.isFilename() || Output
.isNothing()) && "Invalid output.");
163 if (Output
.isFilename()) {
164 CmdArgs
.push_back("-o");
165 CmdArgs
.push_back(Output
.getFilename());
168 if (!Args
.hasArg(options::OPT_nostdlib
, options::OPT_nostartfiles
,
170 if (!Args
.hasArg(options::OPT_shared
))
172 Args
.MakeArgString(getToolChain().GetFilePath("crt1.o")));
174 CmdArgs
.push_back(Args
.MakeArgString(getToolChain().GetFilePath("crti.o")));
176 const Arg
*Std
= Args
.getLastArg(options::OPT_std_EQ
, options::OPT_ansi
);
177 bool HaveAnsi
= false;
178 const LangStandard
*LangStd
= nullptr;
180 HaveAnsi
= Std
->getOption().matches(options::OPT_ansi
);
182 LangStd
= LangStandard::getLangStandardForName(Std
->getValue());
185 const char *values_X
= "values-Xa.o";
186 // Use values-Xc.o for -ansi, -std=c*, -std=iso9899:199409.
187 if (HaveAnsi
|| (LangStd
&& !LangStd
->isGNUMode()))
188 values_X
= "values-Xc.o";
189 CmdArgs
.push_back(Args
.MakeArgString(getToolChain().GetFilePath(values_X
)));
191 const char *values_xpg
= "values-xpg6.o";
192 // Use values-xpg4.o for -std=c90, -std=gnu90, -std=iso9899:199409.
193 if (LangStd
&& LangStd
->getLanguage() == Language::C
&& !LangStd
->isC99())
194 values_xpg
= "values-xpg4.o";
196 Args
.MakeArgString(getToolChain().GetFilePath(values_xpg
)));
198 const char *crtbegin
= nullptr;
199 if (Args
.hasArg(options::OPT_shared
) || IsPIE
)
200 crtbegin
= "crtbeginS.o";
202 crtbegin
= "crtbegin.o";
203 CmdArgs
.push_back(Args
.MakeArgString(getToolChain().GetFilePath(crtbegin
)));
204 // Add crtfastmath.o if available and fast math is enabled.
205 getToolChain().addFastMathRuntimeIfAvailable(Args
, CmdArgs
);
208 getToolChain().AddFilePathLibArgs(Args
, CmdArgs
);
210 Args
.addAllArgs(CmdArgs
,
211 {options::OPT_L
, options::OPT_T_Group
, options::OPT_r
});
213 bool NeedsSanitizerDeps
= addSanitizerRuntimes(getToolChain(), Args
, CmdArgs
);
214 AddLinkerInputs(getToolChain(), Inputs
, Args
, CmdArgs
, JA
);
216 if (!Args
.hasArg(options::OPT_nostdlib
, options::OPT_nodefaultlibs
,
218 // Use the static OpenMP runtime with -static-openmp
219 bool StaticOpenMP
= Args
.hasArg(options::OPT_static_openmp
) &&
220 !Args
.hasArg(options::OPT_static
);
221 addOpenMPRuntime(CmdArgs
, getToolChain(), Args
, StaticOpenMP
);
224 if (getToolChain().ShouldLinkCXXStdlib(Args
))
225 getToolChain().AddCXXStdlibLibArgs(Args
, CmdArgs
);
226 CmdArgs
.push_back("-lm");
228 // Silence warnings when linking C code with a C++ '-stdlib' argument.
229 Args
.ClaimAllArgs(options::OPT_stdlib_EQ
);
230 // Additional linker set-up and flags for Fortran. This is required in order
231 // to generate executables. As Fortran runtime depends on the C runtime,
232 // these dependencies need to be listed before the C runtime below.
233 if (D
.IsFlangMode()) {
234 addFortranRuntimeLibraryPath(getToolChain(), Args
, CmdArgs
);
235 addFortranRuntimeLibs(getToolChain(), CmdArgs
);
236 CmdArgs
.push_back("-lm");
238 if (Args
.hasArg(options::OPT_fstack_protector
) ||
239 Args
.hasArg(options::OPT_fstack_protector_strong
) ||
240 Args
.hasArg(options::OPT_fstack_protector_all
)) {
241 // Explicitly link ssp libraries, not folded into Solaris libc.
242 CmdArgs
.push_back("-lssp_nonshared");
243 CmdArgs
.push_back("-lssp");
245 // LLVM support for atomics on 32-bit SPARC V8+ is incomplete, so
246 // forcibly link with libatomic as a workaround.
247 if (getToolChain().getTriple().getArch() == llvm::Triple::sparc
) {
248 addAsNeededOption(getToolChain(), Args
, CmdArgs
, true);
249 CmdArgs
.push_back("-latomic");
250 addAsNeededOption(getToolChain(), Args
, CmdArgs
, false);
252 addAsNeededOption(getToolChain(), Args
, CmdArgs
, true);
253 CmdArgs
.push_back("-lgcc_s");
254 addAsNeededOption(getToolChain(), Args
, CmdArgs
, false);
255 CmdArgs
.push_back("-lc");
256 if (!Args
.hasArg(options::OPT_shared
)) {
257 CmdArgs
.push_back("-lgcc");
259 const SanitizerArgs
&SA
= getToolChain().getSanitizerArgs(Args
);
260 if (NeedsSanitizerDeps
) {
261 linkSanitizerRuntimeDeps(getToolChain(), Args
, CmdArgs
);
263 // Work around Solaris/amd64 ld bug when calling __tls_get_addr directly.
264 // However, ld -z relax=transtls is available since Solaris 11.2, but not
266 if (getToolChain().getTriple().getArch() == llvm::Triple::x86_64
&&
267 (SA
.needsAsanRt() || SA
.needsStatsRt() ||
268 (SA
.needsUbsanRt() && !SA
.requiresMinimalRuntime())) &&
270 CmdArgs
.push_back("-z");
271 CmdArgs
.push_back("relax=transtls");
274 // Avoid AsanInitInternal cycle, Issue #64126.
275 if (getToolChain().getTriple().isX86() && SA
.needsSharedRt() &&
277 CmdArgs
.push_back("-z");
278 CmdArgs
.push_back("now");
282 if (!Args
.hasArg(options::OPT_nostdlib
, options::OPT_nostartfiles
,
284 if (Args
.hasArg(options::OPT_shared
) || IsPIE
)
286 Args
.MakeArgString(getToolChain().GetFilePath("crtendS.o")));
289 Args
.MakeArgString(getToolChain().GetFilePath("crtend.o")));
291 Args
.MakeArgString(getToolChain().GetFilePath("crtn.o")));
294 getToolChain().addProfileRTLibs(Args
, CmdArgs
);
296 const char *Exec
= Args
.MakeArgString(getLinkerPath(Args
));
297 C
.addCommand(std::make_unique
<Command
>(JA
, *this, ResponseFileSupport::None(),
298 Exec
, CmdArgs
, Inputs
, Output
));
301 static StringRef
getSolarisLibSuffix(const llvm::Triple
&Triple
) {
302 switch (Triple
.getArch()) {
303 case llvm::Triple::x86
:
304 case llvm::Triple::sparc
:
306 case llvm::Triple::x86_64
:
308 case llvm::Triple::sparcv9
:
311 llvm_unreachable("Unsupported architecture");
316 /// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
318 Solaris::Solaris(const Driver
&D
, const llvm::Triple
&Triple
,
320 : Generic_ELF(D
, Triple
, Args
) {
322 GCCInstallation
.init(Triple
, Args
);
324 StringRef LibSuffix
= getSolarisLibSuffix(Triple
);
325 path_list
&Paths
= getFilePaths();
326 if (GCCInstallation
.isValid()) {
327 // On Solaris gcc uses both an architecture-specific path with triple in it
328 // as well as a more generic lib path (+arch suffix).
330 GCCInstallation
.getInstallPath() +
331 GCCInstallation
.getMultilib().gccSuffix(),
333 addPathIfExists(D
, GCCInstallation
.getParentLibPath() + LibSuffix
, Paths
);
336 // If we are currently running Clang inside of the requested system root,
337 // add its parent library path to those searched.
338 if (StringRef(D
.Dir
).startswith(D
.SysRoot
))
339 addPathIfExists(D
, D
.Dir
+ "/../lib", Paths
);
341 addPathIfExists(D
, D
.SysRoot
+ "/usr/lib" + LibSuffix
, Paths
);
344 SanitizerMask
Solaris::getSupportedSanitizers() const {
345 const bool IsX86
= getTriple().getArch() == llvm::Triple::x86
;
346 SanitizerMask Res
= ToolChain::getSupportedSanitizers();
347 // FIXME: Omit X86_64 until 64-bit support is figured out.
349 Res
|= SanitizerKind::Address
;
350 Res
|= SanitizerKind::PointerCompare
;
351 Res
|= SanitizerKind::PointerSubtract
;
353 Res
|= SanitizerKind::Vptr
;
357 const char *Solaris::getDefaultLinker() const {
358 // FIXME: Only handle Solaris ld and GNU ld here.
359 return llvm::StringSwitch
<const char *>(CLANG_DEFAULT_LINKER
)
360 .Cases("bfd", "gld", "/usr/gnu/bin/ld")
361 .Default("/usr/bin/ld");
364 Tool
*Solaris::buildAssembler() const {
365 return new tools::solaris::Assembler(*this);
368 Tool
*Solaris::buildLinker() const { return new tools::solaris::Linker(*this); }
370 void Solaris::AddClangSystemIncludeArgs(const ArgList
&DriverArgs
,
371 ArgStringList
&CC1Args
) const {
372 const Driver
&D
= getDriver();
374 if (DriverArgs
.hasArg(clang::driver::options::OPT_nostdinc
))
377 if (!DriverArgs
.hasArg(options::OPT_nostdlibinc
))
378 addSystemInclude(DriverArgs
, CC1Args
, D
.SysRoot
+ "/usr/local/include");
380 if (!DriverArgs
.hasArg(options::OPT_nobuiltininc
)) {
381 SmallString
<128> P(D
.ResourceDir
);
382 llvm::sys::path::append(P
, "include");
383 addSystemInclude(DriverArgs
, CC1Args
, P
);
386 if (DriverArgs
.hasArg(options::OPT_nostdlibinc
))
389 // Check for configure-time C include directories.
390 StringRef
CIncludeDirs(C_INCLUDE_DIRS
);
391 if (CIncludeDirs
!= "") {
392 SmallVector
<StringRef
, 5> dirs
;
393 CIncludeDirs
.split(dirs
, ":");
394 for (StringRef dir
: dirs
) {
396 llvm::sys::path::is_absolute(dir
) ? "" : StringRef(D
.SysRoot
);
397 addExternCSystemInclude(DriverArgs
, CC1Args
, Prefix
+ dir
);
402 // Add include directories specific to the selected multilib set and multilib.
403 if (GCCInstallation
.isValid()) {
404 const MultilibSet::IncludeDirsFunc
&Callback
=
405 Multilibs
.includeDirsCallback();
407 for (const auto &Path
: Callback(GCCInstallation
.getMultilib()))
408 addExternCSystemIncludeIfExists(
409 DriverArgs
, CC1Args
, GCCInstallation
.getInstallPath() + Path
);
413 addExternCSystemInclude(DriverArgs
, CC1Args
, D
.SysRoot
+ "/usr/include");
416 void Solaris::addLibStdCxxIncludePaths(
417 const llvm::opt::ArgList
&DriverArgs
,
418 llvm::opt::ArgStringList
&CC1Args
) const {
419 // We need a detected GCC installation on Solaris (similar to Linux)
420 // to provide libstdc++'s headers.
421 if (!GCCInstallation
.isValid())
424 // By default, look for the C++ headers in an include directory adjacent to
425 // the lib directory of the GCC installation.
426 // On Solaris this usually looks like /usr/gcc/X.Y/include/c++/X.Y.Z
427 StringRef LibDir
= GCCInstallation
.getParentLibPath();
428 StringRef TripleStr
= GCCInstallation
.getTriple().str();
429 const Multilib
&Multilib
= GCCInstallation
.getMultilib();
430 const GCCVersion
&Version
= GCCInstallation
.getVersion();
432 // The primary search for libstdc++ supports multiarch variants.
433 addLibStdCXXIncludePaths(LibDir
.str() + "/../include/c++/" + Version
.Text
,
434 TripleStr
, Multilib
.includeSuffix(), DriverArgs
,