/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Driver/ToolChains/Solaris.cpp
Line | Count | Source (jump to first uncovered line) |
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 "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" |
24 | | |
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; |
30 | | |
31 | | void solaris::Assembler::ConstructJob(Compilation &C, const JobAction &JA, |
32 | | const InputInfo &Output, |
33 | | const InputInfoList &Inputs, |
34 | | const ArgList &Args, |
35 | 4 | const char *LinkingOutput) const { |
36 | | // Just call the Gnu version, which enforces gas on Solaris. |
37 | 4 | gnutools::Assembler::ConstructJob(C, JA, Output, Inputs, Args, LinkingOutput); |
38 | 4 | } |
39 | | |
40 | 6.18k | bool solaris::isLinkerGnuLd(const ToolChain &TC, const ArgList &Args) { |
41 | | // Only used if targetting Solaris. |
42 | 6.18k | const Arg *A = Args.getLastArg(options::OPT_fuse_ld_EQ); |
43 | 6.18k | StringRef UseLinker = A ? A->getValue()608 : CLANG_DEFAULT_LINKER5.58k ; |
44 | 6.18k | return UseLinker == "bfd" || UseLinker == "gld"6.18k ; |
45 | 6.18k | } |
46 | | |
47 | 61 | static bool getPIE(const ArgList &Args, const ToolChain &TC) { |
48 | 61 | if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_static)55 || |
49 | 61 | Args.hasArg(options::OPT_r)53 ) |
50 | 11 | return false; |
51 | | |
52 | 50 | Arg *A = Args.getLastArg(options::OPT_pie, options::OPT_no_pie, |
53 | 50 | options::OPT_nopie); |
54 | 50 | if (!A) |
55 | 44 | return TC.isPIEDefault(Args); |
56 | 6 | return A->getOption().matches(options::OPT_pie); |
57 | 50 | } |
58 | | |
59 | | // FIXME: Need to handle CLANG_DEFAULT_LINKER here? |
60 | 61 | std::string solaris::Linker::getLinkerPath(const ArgList &Args) const { |
61 | 61 | const ToolChain &ToolChain = getToolChain(); |
62 | 61 | if (const Arg *A = Args.getLastArg(options::OPT_fuse_ld_EQ)) { |
63 | 21 | StringRef UseLinker = A->getValue(); |
64 | 21 | if (!UseLinker.empty()) { |
65 | 7 | if (llvm::sys::path::is_absolute(UseLinker) && |
66 | 7 | llvm::sys::fs::can_execute(UseLinker)0 ) |
67 | 0 | return std::string(UseLinker); |
68 | | |
69 | | // Accept 'bfd' and 'gld' as aliases for the GNU linker. |
70 | 7 | if (UseLinker == "bfd" || UseLinker == "gld") |
71 | | // FIXME: Could also use /usr/bin/gld here. |
72 | 7 | return "/usr/gnu/bin/ld"; |
73 | | |
74 | | // Accept 'ld' as alias for the default linker |
75 | 0 | if (UseLinker != "ld") |
76 | 0 | ToolChain.getDriver().Diag(diag::err_drv_invalid_linker_name) |
77 | 0 | << A->getAsString(Args); |
78 | 0 | } |
79 | 21 | } |
80 | | |
81 | | // getDefaultLinker() always returns an absolute path. |
82 | 54 | return ToolChain.getDefaultLinker(); |
83 | 61 | } |
84 | | |
85 | | void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA, |
86 | | const InputInfo &Output, |
87 | | const InputInfoList &Inputs, |
88 | | const ArgList &Args, |
89 | 61 | const char *LinkingOutput) const { |
90 | 61 | const Driver &D = getToolChain().getDriver(); |
91 | 61 | const bool IsPIE = getPIE(Args, getToolChain()); |
92 | 61 | ArgStringList CmdArgs; |
93 | 61 | bool LinkerIsGnuLd = isLinkerGnuLd(getToolChain(), Args); |
94 | | |
95 | | // Demangle C++ names in errors. GNU ld already defaults to --demangle. |
96 | 61 | if (!LinkerIsGnuLd) |
97 | 54 | CmdArgs.push_back("-C"); |
98 | | |
99 | 61 | if (!Args.hasArg(options::OPT_nostdlib, options::OPT_shared)) { |
100 | 55 | CmdArgs.push_back("-e"); |
101 | 55 | CmdArgs.push_back("_start"); |
102 | 55 | } |
103 | | |
104 | 61 | if (IsPIE) { |
105 | 3 | if (LinkerIsGnuLd) { |
106 | 1 | CmdArgs.push_back("-pie"); |
107 | 2 | } else { |
108 | 2 | CmdArgs.push_back("-z"); |
109 | 2 | CmdArgs.push_back("type=pie"); |
110 | 2 | } |
111 | 3 | } |
112 | | |
113 | 61 | if (Args.hasArg(options::OPT_static)) { |
114 | 2 | CmdArgs.push_back("-Bstatic"); |
115 | 2 | CmdArgs.push_back("-dn"); |
116 | 59 | } else { |
117 | 59 | CmdArgs.push_back("-Bdynamic"); |
118 | 59 | if (Args.hasArg(options::OPT_shared)) { |
119 | 6 | CmdArgs.push_back("-shared"); |
120 | 6 | } |
121 | | |
122 | | // libpthread has been folded into libc since Solaris 10, no need to do |
123 | | // anything for pthreads. Claim argument to avoid warning. |
124 | 59 | Args.ClaimAllArgs(options::OPT_pthread); |
125 | 59 | Args.ClaimAllArgs(options::OPT_pthreads); |
126 | 59 | } |
127 | | |
128 | 61 | if (LinkerIsGnuLd) { |
129 | | // Set the correct linker emulation for 32- and 64-bit Solaris. |
130 | 7 | const toolchains::Solaris &ToolChain = |
131 | 7 | static_cast<const toolchains::Solaris &>(getToolChain()); |
132 | 7 | const llvm::Triple::ArchType Arch = ToolChain.getArch(); |
133 | | |
134 | 7 | switch (Arch) { |
135 | 0 | case llvm::Triple::x86: |
136 | 0 | CmdArgs.push_back("-m"); |
137 | 0 | CmdArgs.push_back("elf_i386_sol2"); |
138 | 0 | break; |
139 | 1 | case llvm::Triple::x86_64: |
140 | 1 | CmdArgs.push_back("-m"); |
141 | 1 | CmdArgs.push_back("elf_x86_64_sol2"); |
142 | 1 | break; |
143 | 6 | case llvm::Triple::sparc: |
144 | 6 | CmdArgs.push_back("-m"); |
145 | 6 | CmdArgs.push_back("elf32_sparc_sol2"); |
146 | 6 | break; |
147 | 0 | case llvm::Triple::sparcv9: |
148 | 0 | CmdArgs.push_back("-m"); |
149 | 0 | CmdArgs.push_back("elf64_sparc_sol2"); |
150 | 0 | break; |
151 | 0 | default: |
152 | 0 | break; |
153 | 7 | } |
154 | | |
155 | 7 | if (Args.hasArg(options::OPT_rdynamic)) |
156 | 0 | CmdArgs.push_back("-export-dynamic"); |
157 | | |
158 | 7 | CmdArgs.push_back("--eh-frame-hdr"); |
159 | 54 | } else { |
160 | | // -rdynamic is a no-op with Solaris ld. Claim argument to avoid warning. |
161 | 54 | Args.ClaimAllArgs(options::OPT_rdynamic); |
162 | 54 | } |
163 | | |
164 | 61 | if (Output.isFilename()) { |
165 | 61 | CmdArgs.push_back("-o"); |
166 | 61 | CmdArgs.push_back(Output.getFilename()); |
167 | 61 | } else { |
168 | 0 | assert(Output.isNothing() && "Invalid output."); |
169 | 0 | } |
170 | | |
171 | 61 | if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles, |
172 | 61 | options::OPT_r)) { |
173 | 58 | if (!Args.hasArg(options::OPT_shared)) |
174 | 52 | CmdArgs.push_back( |
175 | 52 | Args.MakeArgString(getToolChain().GetFilePath("crt1.o"))); |
176 | | |
177 | 58 | CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crti.o"))); |
178 | | |
179 | 58 | const Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi); |
180 | 58 | bool HaveAnsi = false; |
181 | 58 | const LangStandard *LangStd = nullptr; |
182 | 58 | if (Std) { |
183 | 13 | HaveAnsi = Std->getOption().matches(options::OPT_ansi); |
184 | 13 | if (!HaveAnsi) |
185 | 10 | LangStd = LangStandard::getLangStandardForName(Std->getValue()); |
186 | 13 | } |
187 | | |
188 | 58 | const char *values_X = "values-Xa.o"; |
189 | | // Use values-Xc.o for -ansi, -std=c*, -std=iso9899:199409. |
190 | 58 | if (HaveAnsi || (55 LangStd55 && !LangStd->isGNUMode()10 )) |
191 | 9 | values_X = "values-Xc.o"; |
192 | 58 | CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(values_X))); |
193 | | |
194 | 58 | const char *values_xpg = "values-xpg6.o"; |
195 | | // Use values-xpg4.o for -std=c90, -std=gnu90, -std=iso9899:199409. |
196 | 58 | if (LangStd && LangStd->getLanguage() == Language::C10 && !LangStd->isC99()7 ) |
197 | 5 | values_xpg = "values-xpg4.o"; |
198 | 58 | CmdArgs.push_back( |
199 | 58 | Args.MakeArgString(getToolChain().GetFilePath(values_xpg))); |
200 | | |
201 | 58 | const char *crtbegin = nullptr; |
202 | 58 | if (Args.hasArg(options::OPT_shared) || IsPIE52 ) |
203 | 9 | crtbegin = "crtbeginS.o"; |
204 | 49 | else |
205 | 49 | crtbegin = "crtbegin.o"; |
206 | 58 | CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(crtbegin))); |
207 | | // Add crtfastmath.o if available and fast math is enabled. |
208 | 58 | getToolChain().addFastMathRuntimeIfAvailable(Args, CmdArgs); |
209 | 58 | } |
210 | | |
211 | 61 | getToolChain().AddFilePathLibArgs(Args, CmdArgs); |
212 | | |
213 | 61 | Args.AddAllArgs(CmdArgs, |
214 | 61 | {options::OPT_L, options::OPT_T_Group, options::OPT_r}); |
215 | | |
216 | 61 | bool NeedsSanitizerDeps = addSanitizerRuntimes(getToolChain(), Args, CmdArgs); |
217 | 61 | AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA); |
218 | | |
219 | 61 | if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs, |
220 | 61 | options::OPT_r)) { |
221 | 58 | if (D.CCCIsCXX()) { |
222 | 2 | if (getToolChain().ShouldLinkCXXStdlib(Args)) |
223 | 2 | getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs); |
224 | 2 | CmdArgs.push_back("-lm"); |
225 | 2 | } |
226 | 58 | if (Args.hasArg(options::OPT_fstack_protector) || |
227 | 58 | Args.hasArg(options::OPT_fstack_protector_strong) || |
228 | 58 | Args.hasArg(options::OPT_fstack_protector_all)) { |
229 | | // Explicitly link ssp libraries, not folded into Solaris libc. |
230 | 0 | CmdArgs.push_back("-lssp_nonshared"); |
231 | 0 | CmdArgs.push_back("-lssp"); |
232 | 0 | } |
233 | | // LLVM support for atomics on 32-bit SPARC V8+ is incomplete, so |
234 | | // forcibly link with libatomic as a workaround. |
235 | 58 | if (getToolChain().getTriple().getArch() == llvm::Triple::sparc) { |
236 | 36 | addAsNeededOption(getToolChain(), Args, CmdArgs, true); |
237 | 36 | CmdArgs.push_back("-latomic"); |
238 | 36 | addAsNeededOption(getToolChain(), Args, CmdArgs, false); |
239 | 36 | } |
240 | 58 | addAsNeededOption(getToolChain(), Args, CmdArgs, true); |
241 | 58 | CmdArgs.push_back("-lgcc_s"); |
242 | 58 | addAsNeededOption(getToolChain(), Args, CmdArgs, false); |
243 | 58 | CmdArgs.push_back("-lc"); |
244 | 58 | if (!Args.hasArg(options::OPT_shared)) { |
245 | 52 | CmdArgs.push_back("-lgcc"); |
246 | 52 | } |
247 | 58 | const SanitizerArgs &SA = getToolChain().getSanitizerArgs(Args); |
248 | 58 | if (NeedsSanitizerDeps) { |
249 | 8 | linkSanitizerRuntimeDeps(getToolChain(), Args, CmdArgs); |
250 | | |
251 | | // Work around Solaris/amd64 ld bug when calling __tls_get_addr directly. |
252 | | // However, ld -z relax=transtls is available since Solaris 11.2, but not |
253 | | // in Illumos. |
254 | 8 | if (getToolChain().getTriple().getArch() == llvm::Triple::x86_64 && |
255 | 8 | (3 SA.needsAsanRt()3 || SA.needsStatsRt()3 || |
256 | 3 | (SA.needsUbsanRt() && !SA.requiresMinimalRuntime())) && |
257 | 8 | !LinkerIsGnuLd3 ) { |
258 | 2 | CmdArgs.push_back("-z"); |
259 | 2 | CmdArgs.push_back("relax=transtls"); |
260 | 2 | } |
261 | 8 | } |
262 | | // Avoid AsanInitInternal cycle, Issue #64126. |
263 | 58 | if (getToolChain().getTriple().isX86() && SA.needsSharedRt()17 && |
264 | 58 | SA.needsAsanRt()1 ) { |
265 | 1 | CmdArgs.push_back("-z"); |
266 | 1 | CmdArgs.push_back("now"); |
267 | 1 | } |
268 | 58 | } |
269 | | |
270 | 61 | if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles, |
271 | 61 | options::OPT_r)) { |
272 | 58 | if (Args.hasArg(options::OPT_shared) || IsPIE52 ) |
273 | 9 | CmdArgs.push_back( |
274 | 9 | Args.MakeArgString(getToolChain().GetFilePath("crtendS.o"))); |
275 | 49 | else |
276 | 49 | CmdArgs.push_back( |
277 | 49 | Args.MakeArgString(getToolChain().GetFilePath("crtend.o"))); |
278 | 58 | CmdArgs.push_back( |
279 | 58 | Args.MakeArgString(getToolChain().GetFilePath("crtn.o"))); |
280 | 58 | } |
281 | | |
282 | 61 | getToolChain().addProfileRTLibs(Args, CmdArgs); |
283 | | |
284 | 61 | const char *Exec = Args.MakeArgString(getLinkerPath(Args)); |
285 | 61 | C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(), |
286 | 61 | Exec, CmdArgs, Inputs, Output)); |
287 | 61 | } |
288 | | |
289 | 90 | static StringRef getSolarisLibSuffix(const llvm::Triple &Triple) { |
290 | 90 | switch (Triple.getArch()) { |
291 | 20 | case llvm::Triple::x86: |
292 | 70 | case llvm::Triple::sparc: |
293 | 70 | break; |
294 | 10 | case llvm::Triple::x86_64: |
295 | 10 | return "/amd64"; |
296 | 10 | case llvm::Triple::sparcv9: |
297 | 10 | return "/sparcv9"; |
298 | 0 | default: |
299 | 0 | llvm_unreachable("Unsupported architecture"); |
300 | 90 | } |
301 | 70 | return ""; |
302 | 90 | } |
303 | | |
304 | | /// Solaris - Solaris tool chain which can call as(1) and ld(1) directly. |
305 | | |
306 | | Solaris::Solaris(const Driver &D, const llvm::Triple &Triple, |
307 | | const ArgList &Args) |
308 | 90 | : Generic_ELF(D, Triple, Args) { |
309 | | |
310 | 90 | GCCInstallation.init(Triple, Args); |
311 | | |
312 | 90 | StringRef LibSuffix = getSolarisLibSuffix(Triple); |
313 | 90 | path_list &Paths = getFilePaths(); |
314 | 90 | if (GCCInstallation.isValid()) { |
315 | | // On Solaris gcc uses both an architecture-specific path with triple in it |
316 | | // as well as a more generic lib path (+arch suffix). |
317 | 72 | addPathIfExists(D, |
318 | 72 | GCCInstallation.getInstallPath() + |
319 | 72 | GCCInstallation.getMultilib().gccSuffix(), |
320 | 72 | Paths); |
321 | 72 | addPathIfExists(D, GCCInstallation.getParentLibPath() + LibSuffix, Paths); |
322 | 72 | } |
323 | | |
324 | | // If we are currently running Clang inside of the requested system root, |
325 | | // add its parent library path to those searched. |
326 | 90 | if (StringRef(D.Dir).startswith(D.SysRoot)) |
327 | 29 | addPathIfExists(D, D.Dir + "/../lib", Paths); |
328 | | |
329 | 90 | addPathIfExists(D, D.SysRoot + "/usr/lib" + LibSuffix, Paths); |
330 | 90 | } |
331 | | |
332 | 257 | SanitizerMask Solaris::getSupportedSanitizers() const { |
333 | 257 | const bool IsX86 = getTriple().getArch() == llvm::Triple::x86; |
334 | 257 | SanitizerMask Res = ToolChain::getSupportedSanitizers(); |
335 | | // FIXME: Omit X86_64 until 64-bit support is figured out. |
336 | 257 | if (IsX86) { |
337 | 49 | Res |= SanitizerKind::Address; |
338 | 49 | Res |= SanitizerKind::PointerCompare; |
339 | 49 | Res |= SanitizerKind::PointerSubtract; |
340 | 49 | } |
341 | 257 | Res |= SanitizerKind::Vptr; |
342 | 257 | return Res; |
343 | 257 | } |
344 | | |
345 | 54 | const char *Solaris::getDefaultLinker() const { |
346 | | // FIXME: Only handle Solaris ld and GNU ld here. |
347 | 54 | return llvm::StringSwitch<const char *>(CLANG_DEFAULT_LINKER) |
348 | 54 | .Cases("bfd", "gld", "/usr/gnu/bin/ld") |
349 | 54 | .Default("/usr/bin/ld"); |
350 | 54 | } |
351 | | |
352 | 4 | Tool *Solaris::buildAssembler() const { |
353 | 4 | return new tools::solaris::Assembler(*this); |
354 | 4 | } |
355 | | |
356 | 61 | Tool *Solaris::buildLinker() const { return new tools::solaris::Linker(*this); } |
357 | | |
358 | | void Solaris::AddClangSystemIncludeArgs(const ArgList &DriverArgs, |
359 | 77 | ArgStringList &CC1Args) const { |
360 | 77 | const Driver &D = getDriver(); |
361 | | |
362 | 77 | if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)) |
363 | 0 | return; |
364 | | |
365 | 77 | if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) |
366 | 77 | addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/local/include"); |
367 | | |
368 | 77 | if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { |
369 | 77 | SmallString<128> P(D.ResourceDir); |
370 | 77 | llvm::sys::path::append(P, "include"); |
371 | 77 | addSystemInclude(DriverArgs, CC1Args, P); |
372 | 77 | } |
373 | | |
374 | 77 | if (DriverArgs.hasArg(options::OPT_nostdlibinc)) |
375 | 0 | return; |
376 | | |
377 | | // Check for configure-time C include directories. |
378 | 77 | StringRef CIncludeDirs(C_INCLUDE_DIRS); |
379 | 77 | if (CIncludeDirs != "") { |
380 | 0 | SmallVector<StringRef, 5> dirs; |
381 | 0 | CIncludeDirs.split(dirs, ":"); |
382 | 0 | for (StringRef dir : dirs) { |
383 | 0 | StringRef Prefix = |
384 | 0 | llvm::sys::path::is_absolute(dir) ? "" : StringRef(D.SysRoot); |
385 | 0 | addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir); |
386 | 0 | } |
387 | 0 | return; |
388 | 0 | } |
389 | | |
390 | | // Add include directories specific to the selected multilib set and multilib. |
391 | 77 | if (GCCInstallation.isValid()) { |
392 | 61 | const MultilibSet::IncludeDirsFunc &Callback = |
393 | 61 | Multilibs.includeDirsCallback(); |
394 | 61 | if (Callback) { |
395 | 0 | for (const auto &Path : Callback(GCCInstallation.getMultilib())) |
396 | 0 | addExternCSystemIncludeIfExists( |
397 | 0 | DriverArgs, CC1Args, GCCInstallation.getInstallPath() + Path); |
398 | 0 | } |
399 | 61 | } |
400 | | |
401 | 77 | addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include"); |
402 | 77 | } |
403 | | |
404 | | void Solaris::addLibStdCxxIncludePaths( |
405 | | const llvm::opt::ArgList &DriverArgs, |
406 | 12 | llvm::opt::ArgStringList &CC1Args) const { |
407 | | // We need a detected GCC installation on Solaris (similar to Linux) |
408 | | // to provide libstdc++'s headers. |
409 | 12 | if (!GCCInstallation.isValid()) |
410 | 1 | return; |
411 | | |
412 | | // By default, look for the C++ headers in an include directory adjacent to |
413 | | // the lib directory of the GCC installation. |
414 | | // On Solaris this usually looks like /usr/gcc/X.Y/include/c++/X.Y.Z |
415 | 11 | StringRef LibDir = GCCInstallation.getParentLibPath(); |
416 | 11 | StringRef TripleStr = GCCInstallation.getTriple().str(); |
417 | 11 | const Multilib &Multilib = GCCInstallation.getMultilib(); |
418 | 11 | const GCCVersion &Version = GCCInstallation.getVersion(); |
419 | | |
420 | | // The primary search for libstdc++ supports multiarch variants. |
421 | 11 | addLibStdCXXIncludePaths(LibDir.str() + "/../include/c++/" + Version.Text, |
422 | 11 | TripleStr, Multilib.includeSuffix(), DriverArgs, |
423 | 11 | CC1Args); |
424 | 11 | } |