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 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); 38 } 39 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"; 45 } 46 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; 51 52 return Args.hasFlag(options::OPT_pie, options::OPT_no_pie, 53 TC.isPIEDefault(Args)); 54 } 55 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); 65 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"; 70 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); 75 } 76 } 77 78 // getDefaultLinker() always returns an absolute path. 79 return ToolChain.getDefaultLinker(); 80 } 81 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; 93 94 // Demangle C++ names in errors. GNU ld already defaults to --demangle. 95 if (!LinkerIsGnuLd) 96 CmdArgs.push_back("-C"); 97 98 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_shared, 99 options::OPT_r)) { 100 CmdArgs.push_back("-e"); 101 CmdArgs.push_back("_start"); 102 } 103 104 if (IsPIE) { 105 if (LinkerIsGnuLd) { 106 CmdArgs.push_back("-pie"); 107 } else { 108 CmdArgs.push_back("-z"); 109 CmdArgs.push_back("type=pie"); 110 } 111 } 112 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"); 119 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); 124 } 125 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; 147 } 148 149 if (Args.hasArg(options::OPT_rdynamic)) 150 CmdArgs.push_back("-export-dynamic"); 151 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); 156 } 157 158 assert((Output.isFilename() || Output.isNothing()) && "Invalid output."); 159 if (Output.isFilename()) { 160 CmdArgs.push_back("-o"); 161 CmdArgs.push_back(Output.getFilename()); 162 } 163 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"))); 168 169 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o"))); 170 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()); 178 } 179 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))); 185 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))); 191 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); 200 } 201 202 ToolChain.AddFilePathLibArgs(Args, CmdArgs); 203 204 Args.addAllArgs(CmdArgs, 205 {options::OPT_L, options::OPT_T_Group, options::OPT_r}); 206 207 bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs); 208 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); 209 210 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs, 211 options::OPT_r)) { 212 // Use the static OpenMP runtime with -static-openmp 213 bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) && 214 !Args.hasArg(options::OPT_static); 215 addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP); 216 217 if (D.CCCIsCXX()) { 218 if (ToolChain.ShouldLinkCXXStdlib(Args)) 219 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs); 220 CmdArgs.push_back("-lm"); 221 } 222 // Silence warnings when linking C code with a C++ '-stdlib' argument. 223 Args.ClaimAllArgs(options::OPT_stdlib_EQ); 224 // Additional linker set-up and flags for Fortran. This is required in order 225 // to generate executables. As Fortran runtime depends on the C runtime, 226 // these dependencies need to be listed before the C runtime below. 227 if (D.IsFlangMode()) { 228 addFortranRuntimeLibraryPath(getToolChain(), Args, CmdArgs); 229 addFortranRuntimeLibs(getToolChain(), Args, CmdArgs); 230 CmdArgs.push_back("-lm"); 231 } 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"); 238 } 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); 245 } 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"); 252 } 253 const SanitizerArgs &SA = ToolChain.getSanitizerArgs(Args); 254 if (NeedsSanitizerDeps) { 255 linkSanitizerRuntimeDeps(ToolChain, Args, CmdArgs); 256 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"); 266 } 267 } 268 // Avoid AsanInitInternal cycle, Issue #64126. 269 if (ToolChain.getTriple().isX86() && SA.needsSharedRt() && 270 SA.needsAsanRt()) { 271 CmdArgs.push_back("-z"); 272 CmdArgs.push_back("now"); 273 } 274 } 275 276 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles, 277 options::OPT_r)) { 278 const char *crtend = nullptr; 279 if (Args.hasArg(options::OPT_shared) || IsPIE) 280 crtend = "crtendS.o"; 281 else 282 crtend = "crtend.o"; 283 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend))); 284 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o"))); 285 } 286 287 ToolChain.addProfileRTLibs(Args, CmdArgs); 288 289 const char *Exec = Args.MakeArgString(getLinkerPath(Args)); 290 C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(), 291 Exec, CmdArgs, Inputs, Output)); 292 } 293 294 static StringRef getSolarisLibSuffix(const llvm::Triple &Triple) { 295 switch (Triple.getArch()) { 296 case llvm::Triple::x86: 297 case llvm::Triple::sparc: 298 default: 299 break; 300 case llvm::Triple::x86_64: 301 return "/amd64"; 302 case llvm::Triple::sparcv9: 303 return "/sparcv9"; 304 } 305 return ""; 306 } 307 308 /// Solaris - Solaris tool chain which can call as(1) and ld(1) directly. 309 310 Solaris::Solaris(const Driver &D, const llvm::Triple &Triple, 311 const ArgList &Args) 312 : Generic_ELF(D, Triple, Args) { 313 314 GCCInstallation.init(Triple, Args); 315 316 StringRef LibSuffix = getSolarisLibSuffix(Triple); 317 path_list &Paths = getFilePaths(); 318 if (GCCInstallation.isValid()) { 319 // On Solaris gcc uses both an architecture-specific path with triple in it 320 // as well as a more generic lib path (+arch suffix). 321 addPathIfExists(D, 322 GCCInstallation.getInstallPath() + 323 GCCInstallation.getMultilib().gccSuffix(), 324 Paths); 325 addPathIfExists(D, GCCInstallation.getParentLibPath() + LibSuffix, Paths); 326 } 327 328 // If we are currently running Clang inside of the requested system root, 329 // add its parent library path to those searched. 330 if (StringRef(D.Dir).starts_with(D.SysRoot)) 331 addPathIfExists(D, D.Dir + "/../lib", Paths); 332 333 addPathIfExists(D, D.SysRoot + "/usr/lib" + LibSuffix, Paths); 334 } 335 336 SanitizerMask Solaris::getSupportedSanitizers() const { 337 const bool IsX86 = getTriple().getArch() == llvm::Triple::x86; 338 SanitizerMask Res = ToolChain::getSupportedSanitizers(); 339 // FIXME: Omit X86_64 until 64-bit support is figured out. 340 if (IsX86) { 341 Res |= SanitizerKind::Address; 342 Res |= SanitizerKind::PointerCompare; 343 Res |= SanitizerKind::PointerSubtract; 344 } 345 Res |= SanitizerKind::Vptr; 346 return Res; 347 } 348 349 const char *Solaris::getDefaultLinker() const { 350 // FIXME: Only handle Solaris ld and GNU ld here. 351 return llvm::StringSwitch<const char *>(CLANG_DEFAULT_LINKER) 352 .Cases("bfd", "gld", "/usr/gnu/bin/ld") 353 .Default("/usr/bin/ld"); 354 } 355 356 Tool *Solaris::buildAssembler() const { 357 return new tools::solaris::Assembler(*this); 358 } 359 360 Tool *Solaris::buildLinker() const { return new tools::solaris::Linker(*this); } 361 362 void Solaris::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 363 ArgStringList &CC1Args) const { 364 const Driver &D = getDriver(); 365 366 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)) 367 return; 368 369 if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) 370 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/local/include"); 371 372 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 373 SmallString<128> P(D.ResourceDir); 374 llvm::sys::path::append(P, "include"); 375 addSystemInclude(DriverArgs, CC1Args, P); 376 } 377 378 if (DriverArgs.hasArg(options::OPT_nostdlibinc)) 379 return; 380 381 // Check for configure-time C include directories. 382 StringRef CIncludeDirs(C_INCLUDE_DIRS); 383 if (CIncludeDirs != "") { 384 SmallVector<StringRef, 5> dirs; 385 CIncludeDirs.split(dirs, ":"); 386 for (StringRef dir : dirs) { 387 StringRef Prefix = 388 llvm::sys::path::is_absolute(dir) ? "" : StringRef(D.SysRoot); 389 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir); 390 } 391 return; 392 } 393 394 // Add include directories specific to the selected multilib set and multilib. 395 if (GCCInstallation.isValid()) { 396 const MultilibSet::IncludeDirsFunc &Callback = 397 Multilibs.includeDirsCallback(); 398 if (Callback) { 399 for (const auto &Path : Callback(GCCInstallation.getMultilib())) 400 addExternCSystemIncludeIfExists( 401 DriverArgs, CC1Args, GCCInstallation.getInstallPath() + Path); 402 } 403 } 404 405 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include"); 406 } 407 408 void Solaris::addLibStdCxxIncludePaths( 409 const llvm::opt::ArgList &DriverArgs, 410 llvm::opt::ArgStringList &CC1Args) const { 411 // We need a detected GCC installation on Solaris (similar to Linux) 412 // to provide libstdc++'s headers. 413 if (!GCCInstallation.isValid()) 414 return; 415 416 // By default, look for the C++ headers in an include directory adjacent to 417 // the lib directory of the GCC installation. 418 // On Solaris this usually looks like /usr/gcc/X.Y/include/c++/X.Y.Z 419 StringRef LibDir = GCCInstallation.getParentLibPath(); 420 StringRef TripleStr = GCCInstallation.getTriple().str(); 421 const Multilib &Multilib = GCCInstallation.getMultilib(); 422 const GCCVersion &Version = GCCInstallation.getVersion(); 423 424 // The primary search for libstdc++ supports multiarch variants. 425 addLibStdCXXIncludePaths(LibDir.str() + "/../include/c++/" + Version.Text, 426 TripleStr, Multilib.includeSuffix(), DriverArgs, 427 CC1Args); 428 } 429