[flang][cuda] Do not register global constants (#118582)
[llvm-project.git] / clang / lib / Driver / ToolChains / Solaris.cpp
blobfd3232b7c1b06fb5e4b842888d0fe8b7485e27db
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 //===----------------------------------------------------------------------===//
9 #include "Solaris.h"
10 #include "CommonArgs.h"
11 #include "Gnu.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,
34 const ArgList &Args,
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))
50 return false;
52 return Args.hasFlag(options::OPT_pie, options::OPT_no_pie,
53 TC.isPIEDefault(Args));
56 // FIXME: Need to handle CLANG_DEFAULT_LINKER here?
57 std::string solaris::Linker::getLinkerPath(const ArgList &Args) const {
58 const ToolChain &ToolChain = getToolChain();
59 if (const Arg *A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
60 StringRef UseLinker = A->getValue();
61 if (!UseLinker.empty()) {
62 if (llvm::sys::path::is_absolute(UseLinker) &&
63 llvm::sys::fs::can_execute(UseLinker))
64 return std::string(UseLinker);
66 // Accept 'bfd' and 'gld' as aliases for the GNU linker.
67 if (UseLinker == "bfd" || UseLinker == "gld")
68 // FIXME: Could also use /usr/bin/gld here.
69 return "/usr/gnu/bin/ld";
71 // Accept 'ld' as alias for the default linker
72 if (UseLinker != "ld")
73 ToolChain.getDriver().Diag(diag::err_drv_invalid_linker_name)
74 << A->getAsString(Args);
78 // getDefaultLinker() always returns an absolute path.
79 return ToolChain.getDefaultLinker();
82 void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
83 const InputInfo &Output,
84 const InputInfoList &Inputs,
85 const ArgList &Args,
86 const char *LinkingOutput) const {
87 const auto &ToolChain = static_cast<const Solaris &>(getToolChain());
88 const Driver &D = ToolChain.getDriver();
89 const llvm::Triple::ArchType Arch = ToolChain.getArch();
90 const bool IsPIE = getPIE(Args, ToolChain);
91 const bool LinkerIsGnuLd = isLinkerGnuLd(ToolChain, Args);
92 ArgStringList CmdArgs;
94 // Demangle C++ names in errors. GNU ld already defaults to --demangle.
95 if (!LinkerIsGnuLd)
96 CmdArgs.push_back("-C");
98 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_shared,
99 options::OPT_r)) {
100 CmdArgs.push_back("-e");
101 CmdArgs.push_back("_start");
104 if (IsPIE) {
105 if (LinkerIsGnuLd) {
106 CmdArgs.push_back("-pie");
107 } else {
108 CmdArgs.push_back("-z");
109 CmdArgs.push_back("type=pie");
113 if (Args.hasArg(options::OPT_static)) {
114 CmdArgs.push_back("-Bstatic");
115 CmdArgs.push_back("-dn");
116 } else {
117 if (!Args.hasArg(options::OPT_r) && Args.hasArg(options::OPT_shared))
118 CmdArgs.push_back("-shared");
120 // libpthread has been folded into libc since Solaris 10, no need to do
121 // anything for pthreads. Claim argument to avoid warning.
122 Args.ClaimAllArgs(options::OPT_pthread);
123 Args.ClaimAllArgs(options::OPT_pthreads);
126 if (LinkerIsGnuLd) {
127 // Set the correct linker emulation for 32- and 64-bit Solaris.
128 switch (Arch) {
129 case llvm::Triple::x86:
130 CmdArgs.push_back("-m");
131 CmdArgs.push_back("elf_i386_sol2");
132 break;
133 case llvm::Triple::x86_64:
134 CmdArgs.push_back("-m");
135 CmdArgs.push_back("elf_x86_64_sol2");
136 break;
137 case llvm::Triple::sparc:
138 CmdArgs.push_back("-m");
139 CmdArgs.push_back("elf32_sparc_sol2");
140 break;
141 case llvm::Triple::sparcv9:
142 CmdArgs.push_back("-m");
143 CmdArgs.push_back("elf64_sparc_sol2");
144 break;
145 default:
146 break;
149 if (Args.hasArg(options::OPT_rdynamic))
150 CmdArgs.push_back("-export-dynamic");
152 CmdArgs.push_back("--eh-frame-hdr");
153 } else {
154 // -rdynamic is a no-op with Solaris ld. Claim argument to avoid warning.
155 Args.ClaimAllArgs(options::OPT_rdynamic);
158 assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
159 if (Output.isFilename()) {
160 CmdArgs.push_back("-o");
161 CmdArgs.push_back(Output.getFilename());
164 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
165 options::OPT_r)) {
166 if (!Args.hasArg(options::OPT_shared))
167 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt1.o")));
169 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
171 const Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi);
172 bool HaveAnsi = false;
173 const LangStandard *LangStd = nullptr;
174 if (Std) {
175 HaveAnsi = Std->getOption().matches(options::OPT_ansi);
176 if (!HaveAnsi)
177 LangStd = LangStandard::getLangStandardForName(Std->getValue());
180 const char *values_X = "values-Xa.o";
181 // Use values-Xc.o for -ansi, -std=c*, -std=iso9899:199409.
182 if (HaveAnsi || (LangStd && !LangStd->isGNUMode()))
183 values_X = "values-Xc.o";
184 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(values_X)));
186 const char *values_xpg = "values-xpg6.o";
187 // Use values-xpg4.o for -std=c90, -std=gnu90, -std=iso9899:199409.
188 if (LangStd && LangStd->getLanguage() == Language::C && !LangStd->isC99())
189 values_xpg = "values-xpg4.o";
190 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(values_xpg)));
192 const char *crtbegin = nullptr;
193 if (Args.hasArg(options::OPT_shared) || IsPIE)
194 crtbegin = "crtbeginS.o";
195 else
196 crtbegin = "crtbegin.o";
197 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
198 // Add crtfastmath.o if available and fast math is enabled.
199 ToolChain.addFastMathRuntimeIfAvailable(Args, CmdArgs);
202 ToolChain.AddFilePathLibArgs(Args, CmdArgs);
204 Args.addAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group});
206 bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
207 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
209 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
210 options::OPT_r)) {
211 // Use the static OpenMP runtime with -static-openmp
212 bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) &&
213 !Args.hasArg(options::OPT_static);
214 addOpenMPRuntime(C, CmdArgs, ToolChain, Args, StaticOpenMP);
216 if (D.CCCIsCXX()) {
217 if (ToolChain.ShouldLinkCXXStdlib(Args))
218 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
219 CmdArgs.push_back("-lm");
221 // Silence warnings when linking C code with a C++ '-stdlib' argument.
222 Args.ClaimAllArgs(options::OPT_stdlib_EQ);
223 // Additional linker set-up and flags for Fortran. This is required in order
224 // to generate executables. As Fortran runtime depends on the C runtime,
225 // these dependencies need to be listed before the C runtime below.
226 if (D.IsFlangMode() &&
227 !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
228 addFortranRuntimeLibraryPath(getToolChain(), Args, CmdArgs);
229 addFortranRuntimeLibs(getToolChain(), Args, CmdArgs);
230 CmdArgs.push_back("-lm");
232 if (Args.hasArg(options::OPT_fstack_protector) ||
233 Args.hasArg(options::OPT_fstack_protector_strong) ||
234 Args.hasArg(options::OPT_fstack_protector_all)) {
235 // Explicitly link ssp libraries, not folded into Solaris libc.
236 CmdArgs.push_back("-lssp_nonshared");
237 CmdArgs.push_back("-lssp");
239 // LLVM support for atomics on 32-bit SPARC V8+ is incomplete, so
240 // forcibly link with libatomic as a workaround.
241 if (Arch == llvm::Triple::sparc) {
242 addAsNeededOption(ToolChain, Args, CmdArgs, true);
243 CmdArgs.push_back("-latomic");
244 addAsNeededOption(ToolChain, Args, CmdArgs, false);
246 addAsNeededOption(ToolChain, Args, CmdArgs, true);
247 CmdArgs.push_back("-lgcc_s");
248 addAsNeededOption(ToolChain, Args, CmdArgs, false);
249 CmdArgs.push_back("-lc");
250 if (!Args.hasArg(options::OPT_shared)) {
251 CmdArgs.push_back("-lgcc");
253 const SanitizerArgs &SA = ToolChain.getSanitizerArgs(Args);
254 if (NeedsSanitizerDeps) {
255 linkSanitizerRuntimeDeps(ToolChain, Args, CmdArgs);
257 // Work around Solaris/amd64 ld bug when calling __tls_get_addr directly.
258 // However, ld -z relax=transtls is available since Solaris 11.2, but not
259 // in Illumos.
260 if (Arch == llvm::Triple::x86_64 &&
261 (SA.needsAsanRt() || SA.needsStatsRt() ||
262 (SA.needsUbsanRt() && !SA.requiresMinimalRuntime())) &&
263 !LinkerIsGnuLd) {
264 CmdArgs.push_back("-z");
265 CmdArgs.push_back("relax=transtls");
268 // Avoid AsanInitInternal cycle, Issue #64126.
269 if (SA.needsSharedRt() && SA.needsAsanRt()) {
270 CmdArgs.push_back("-z");
271 CmdArgs.push_back("now");
275 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
276 options::OPT_r)) {
277 const char *crtend = nullptr;
278 if (Args.hasArg(options::OPT_shared) || IsPIE)
279 crtend = "crtendS.o";
280 else
281 crtend = "crtend.o";
282 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
283 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
286 ToolChain.addProfileRTLibs(Args, CmdArgs);
288 const char *Exec = Args.MakeArgString(getLinkerPath(Args));
289 C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
290 Exec, CmdArgs, Inputs, Output));
293 static StringRef getSolarisLibSuffix(const llvm::Triple &Triple) {
294 switch (Triple.getArch()) {
295 case llvm::Triple::x86:
296 case llvm::Triple::sparc:
297 default:
298 break;
299 case llvm::Triple::x86_64:
300 return "/amd64";
301 case llvm::Triple::sparcv9:
302 return "/sparcv9";
304 return "";
307 /// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
309 Solaris::Solaris(const Driver &D, const llvm::Triple &Triple,
310 const ArgList &Args)
311 : Generic_ELF(D, Triple, Args) {
313 GCCInstallation.init(Triple, Args);
315 StringRef LibSuffix = getSolarisLibSuffix(Triple);
316 path_list &Paths = getFilePaths();
317 if (GCCInstallation.isValid()) {
318 // On Solaris gcc uses both an architecture-specific path with triple in it
319 // as well as a more generic lib path (+arch suffix).
320 addPathIfExists(D,
321 GCCInstallation.getInstallPath() +
322 GCCInstallation.getMultilib().gccSuffix(),
323 Paths);
324 addPathIfExists(D, GCCInstallation.getParentLibPath() + LibSuffix, Paths);
327 // If we are currently running Clang inside of the requested system root,
328 // add its parent library path to those searched.
329 if (StringRef(D.Dir).starts_with(D.SysRoot))
330 addPathIfExists(D, D.Dir + "/../lib", Paths);
332 addPathIfExists(D, D.SysRoot + "/usr/lib" + LibSuffix, Paths);
335 SanitizerMask Solaris::getSupportedSanitizers() const {
336 const bool IsSparc = getTriple().getArch() == llvm::Triple::sparc;
337 const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
338 SanitizerMask Res = ToolChain::getSupportedSanitizers();
339 // FIXME: Omit SparcV9 and X86_64 until 64-bit support is figured out.
340 if (IsSparc || IsX86) {
341 Res |= SanitizerKind::Address;
342 Res |= SanitizerKind::PointerCompare;
343 Res |= SanitizerKind::PointerSubtract;
345 Res |= SanitizerKind::SafeStack;
346 Res |= SanitizerKind::Vptr;
347 return Res;
350 const char *Solaris::getDefaultLinker() const {
351 // FIXME: Only handle Solaris ld and GNU ld here.
352 return llvm::StringSwitch<const char *>(CLANG_DEFAULT_LINKER)
353 .Cases("bfd", "gld", "/usr/gnu/bin/ld")
354 .Default("/usr/bin/ld");
357 Tool *Solaris::buildAssembler() const {
358 return new tools::solaris::Assembler(*this);
361 Tool *Solaris::buildLinker() const { return new tools::solaris::Linker(*this); }
363 void Solaris::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
364 ArgStringList &CC1Args) const {
365 const Driver &D = getDriver();
367 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
368 return;
370 if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
371 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/local/include");
373 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
374 SmallString<128> P(D.ResourceDir);
375 llvm::sys::path::append(P, "include");
376 addSystemInclude(DriverArgs, CC1Args, P);
379 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
380 return;
382 // Check for configure-time C include directories.
383 StringRef CIncludeDirs(C_INCLUDE_DIRS);
384 if (CIncludeDirs != "") {
385 SmallVector<StringRef, 5> dirs;
386 CIncludeDirs.split(dirs, ":");
387 for (StringRef dir : dirs) {
388 StringRef Prefix =
389 llvm::sys::path::is_absolute(dir) ? "" : StringRef(D.SysRoot);
390 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
392 return;
395 // Add include directories specific to the selected multilib set and multilib.
396 if (GCCInstallation.isValid()) {
397 const MultilibSet::IncludeDirsFunc &Callback =
398 Multilibs.includeDirsCallback();
399 if (Callback) {
400 for (const auto &Path : Callback(GCCInstallation.getMultilib()))
401 addExternCSystemIncludeIfExists(
402 DriverArgs, CC1Args, GCCInstallation.getInstallPath() + Path);
406 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include");
409 void Solaris::addLibStdCxxIncludePaths(
410 const llvm::opt::ArgList &DriverArgs,
411 llvm::opt::ArgStringList &CC1Args) const {
412 // We need a detected GCC installation on Solaris (similar to Linux)
413 // to provide libstdc++'s headers.
414 if (!GCCInstallation.isValid())
415 return;
417 // By default, look for the C++ headers in an include directory adjacent to
418 // the lib directory of the GCC installation.
419 // On Solaris this usually looks like /usr/gcc/X.Y/include/c++/X.Y.Z
420 StringRef LibDir = GCCInstallation.getParentLibPath();
421 StringRef TripleStr = GCCInstallation.getTriple().str();
422 const Multilib &Multilib = GCCInstallation.getMultilib();
423 const GCCVersion &Version = GCCInstallation.getVersion();
425 // The primary search for libstdc++ supports multiarch variants.
426 addLibStdCXXIncludePaths(LibDir.str() + "/../include/c++/" + Version.Text,
427 TripleStr, Multilib.includeSuffix(), DriverArgs,
428 CC1Args);