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 "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" 22 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; 28 29 void solaris::Assembler::ConstructJob(Compilation &C, const JobAction &JA, 30 const InputInfo &Output, 31 const InputInfoList &Inputs, 32 const ArgList &Args, 33 const char *LinkingOutput) const { 34 claimNoWarnArgs(Args); 35 ArgStringList CmdArgs; 36 37 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler); 38 39 CmdArgs.push_back("-o"); 40 CmdArgs.push_back(Output.getFilename()); 41 42 for (const auto &II : Inputs) 43 CmdArgs.push_back(II.getFilename()); 44 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)); 48 } 49 50 static bool getPIE(const ArgList &Args, const ToolChain &TC) { 51 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_static) || 52 Args.hasArg(options::OPT_r)) 53 return false; 54 55 Arg *A = Args.getLastArg(options::OPT_pie, options::OPT_no_pie, 56 options::OPT_nopie); 57 if (!A) 58 return TC.isPIEDefault(Args); 59 return A->getOption().matches(options::OPT_pie); 60 } 61 62 void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA, 63 const InputInfo &Output, 64 const InputInfoList &Inputs, 65 const ArgList &Args, 66 const char *LinkingOutput) const { 67 const Driver &D = getToolChain().getDriver(); 68 const bool IsPIE = getPIE(Args, getToolChain()); 69 ArgStringList CmdArgs; 70 71 // Demangle C++ names in errors 72 CmdArgs.push_back("-C"); 73 74 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_shared)) { 75 CmdArgs.push_back("-e"); 76 CmdArgs.push_back("_start"); 77 } 78 79 if (IsPIE) { 80 CmdArgs.push_back("-z"); 81 CmdArgs.push_back("type=pie"); 82 } 83 84 if (Args.hasArg(options::OPT_static)) { 85 CmdArgs.push_back("-Bstatic"); 86 CmdArgs.push_back("-dn"); 87 } else { 88 CmdArgs.push_back("-Bdynamic"); 89 if (Args.hasArg(options::OPT_shared)) { 90 CmdArgs.push_back("-shared"); 91 } 92 93 // libpthread has been folded into libc since Solaris 10, no need to do 94 // anything for pthreads. Claim argument to avoid warning. 95 Args.ClaimAllArgs(options::OPT_pthread); 96 Args.ClaimAllArgs(options::OPT_pthreads); 97 } 98 99 if (Output.isFilename()) { 100 CmdArgs.push_back("-o"); 101 CmdArgs.push_back(Output.getFilename()); 102 } else { 103 assert(Output.isNothing() && "Invalid output."); 104 } 105 106 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles, 107 options::OPT_r)) { 108 if (!Args.hasArg(options::OPT_shared)) 109 CmdArgs.push_back( 110 Args.MakeArgString(getToolChain().GetFilePath("crt1.o"))); 111 112 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crti.o"))); 113 114 const Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi); 115 bool HaveAnsi = false; 116 const LangStandard *LangStd = nullptr; 117 if (Std) { 118 HaveAnsi = Std->getOption().matches(options::OPT_ansi); 119 if (!HaveAnsi) 120 LangStd = LangStandard::getLangStandardForName(Std->getValue()); 121 } 122 123 const char *values_X = "values-Xa.o"; 124 // Use values-Xc.o for -ansi, -std=c*, -std=iso9899:199409. 125 if (HaveAnsi || (LangStd && !LangStd->isGNUMode())) 126 values_X = "values-Xc.o"; 127 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(values_X))); 128 129 const char *values_xpg = "values-xpg6.o"; 130 // Use values-xpg4.o for -std=c90, -std=gnu90, -std=iso9899:199409. 131 if (LangStd && LangStd->getLanguage() == Language::C && !LangStd->isC99()) 132 values_xpg = "values-xpg4.o"; 133 CmdArgs.push_back( 134 Args.MakeArgString(getToolChain().GetFilePath(values_xpg))); 135 136 const char *crtbegin = nullptr; 137 if (Args.hasArg(options::OPT_shared) || IsPIE) 138 crtbegin = "crtbeginS.o"; 139 else 140 crtbegin = "crtbegin.o"; 141 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(crtbegin))); 142 // Add crtfastmath.o if available and fast math is enabled. 143 getToolChain().addFastMathRuntimeIfAvailable(Args, CmdArgs); 144 } 145 146 getToolChain().AddFilePathLibArgs(Args, CmdArgs); 147 148 Args.AddAllArgs(CmdArgs, 149 {options::OPT_L, options::OPT_T_Group, options::OPT_r}); 150 151 bool NeedsSanitizerDeps = addSanitizerRuntimes(getToolChain(), Args, CmdArgs); 152 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA); 153 154 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs, 155 options::OPT_r)) { 156 if (D.CCCIsCXX()) { 157 if (getToolChain().ShouldLinkCXXStdlib(Args)) 158 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs); 159 CmdArgs.push_back("-lm"); 160 } 161 if (Args.hasArg(options::OPT_fstack_protector) || 162 Args.hasArg(options::OPT_fstack_protector_strong) || 163 Args.hasArg(options::OPT_fstack_protector_all)) { 164 // Explicitly link ssp libraries, not folded into Solaris libc. 165 CmdArgs.push_back("-lssp_nonshared"); 166 CmdArgs.push_back("-lssp"); 167 } 168 // LLVM support for atomics on 32-bit SPARC V8+ is incomplete, so 169 // forcibly link with libatomic as a workaround. 170 if (getToolChain().getTriple().getArch() == llvm::Triple::sparc) { 171 CmdArgs.push_back(getAsNeededOption(getToolChain(), true)); 172 CmdArgs.push_back("-latomic"); 173 CmdArgs.push_back(getAsNeededOption(getToolChain(), false)); 174 } 175 CmdArgs.push_back("-lgcc_s"); 176 CmdArgs.push_back("-lc"); 177 if (!Args.hasArg(options::OPT_shared)) { 178 CmdArgs.push_back("-lgcc"); 179 } 180 const SanitizerArgs &SA = getToolChain().getSanitizerArgs(Args); 181 if (NeedsSanitizerDeps) { 182 linkSanitizerRuntimeDeps(getToolChain(), CmdArgs); 183 184 // Work around Solaris/amd64 ld bug when calling __tls_get_addr directly. 185 // However, ld -z relax=transtls is available since Solaris 11.2, but not 186 // in Illumos. 187 if (getToolChain().getTriple().getArch() == llvm::Triple::x86_64 && 188 (SA.needsAsanRt() || SA.needsStatsRt() || 189 (SA.needsUbsanRt() && !SA.requiresMinimalRuntime()))) 190 CmdArgs.push_back("-zrelax=transtls"); 191 } 192 // Avoid AsanInitInternal cycle, Issue #64126. 193 if (getToolChain().getTriple().isX86() && SA.needsSharedRt() && 194 SA.needsAsanRt()) 195 CmdArgs.push_back("-znow"); 196 } 197 198 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles, 199 options::OPT_r)) { 200 if (Args.hasArg(options::OPT_shared) || IsPIE) 201 CmdArgs.push_back( 202 Args.MakeArgString(getToolChain().GetFilePath("crtendS.o"))); 203 else 204 CmdArgs.push_back( 205 Args.MakeArgString(getToolChain().GetFilePath("crtend.o"))); 206 CmdArgs.push_back( 207 Args.MakeArgString(getToolChain().GetFilePath("crtn.o"))); 208 } 209 210 getToolChain().addProfileRTLibs(Args, CmdArgs); 211 212 const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath()); 213 C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(), 214 Exec, CmdArgs, Inputs, Output)); 215 } 216 217 static StringRef getSolarisLibSuffix(const llvm::Triple &Triple) { 218 switch (Triple.getArch()) { 219 case llvm::Triple::x86: 220 case llvm::Triple::sparc: 221 break; 222 case llvm::Triple::x86_64: 223 return "/amd64"; 224 case llvm::Triple::sparcv9: 225 return "/sparcv9"; 226 default: 227 llvm_unreachable("Unsupported architecture"); 228 } 229 return ""; 230 } 231 232 /// Solaris - Solaris tool chain which can call as(1) and ld(1) directly. 233 234 Solaris::Solaris(const Driver &D, const llvm::Triple &Triple, 235 const ArgList &Args) 236 : Generic_ELF(D, Triple, Args) { 237 238 GCCInstallation.init(Triple, Args); 239 240 StringRef LibSuffix = getSolarisLibSuffix(Triple); 241 path_list &Paths = getFilePaths(); 242 if (GCCInstallation.isValid()) { 243 // On Solaris gcc uses both an architecture-specific path with triple in it 244 // as well as a more generic lib path (+arch suffix). 245 addPathIfExists(D, 246 GCCInstallation.getInstallPath() + 247 GCCInstallation.getMultilib().gccSuffix(), 248 Paths); 249 addPathIfExists(D, GCCInstallation.getParentLibPath() + LibSuffix, Paths); 250 } 251 252 // If we are currently running Clang inside of the requested system root, 253 // add its parent library path to those searched. 254 if (StringRef(D.Dir).startswith(D.SysRoot)) 255 addPathIfExists(D, D.Dir + "/../lib", Paths); 256 257 addPathIfExists(D, D.SysRoot + "/usr/lib" + LibSuffix, Paths); 258 } 259 260 SanitizerMask Solaris::getSupportedSanitizers() const { 261 const bool IsX86 = getTriple().getArch() == llvm::Triple::x86; 262 SanitizerMask Res = ToolChain::getSupportedSanitizers(); 263 // FIXME: Omit X86_64 until 64-bit support is figured out. 264 if (IsX86) { 265 Res |= SanitizerKind::Address; 266 Res |= SanitizerKind::PointerCompare; 267 Res |= SanitizerKind::PointerSubtract; 268 } 269 Res |= SanitizerKind::Vptr; 270 return Res; 271 } 272 273 Tool *Solaris::buildAssembler() const { 274 return new tools::solaris::Assembler(*this); 275 } 276 277 Tool *Solaris::buildLinker() const { return new tools::solaris::Linker(*this); } 278 279 void Solaris::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 280 ArgStringList &CC1Args) const { 281 const Driver &D = getDriver(); 282 283 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)) 284 return; 285 286 if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) 287 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/local/include"); 288 289 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 290 SmallString<128> P(D.ResourceDir); 291 llvm::sys::path::append(P, "include"); 292 addSystemInclude(DriverArgs, CC1Args, P); 293 } 294 295 if (DriverArgs.hasArg(options::OPT_nostdlibinc)) 296 return; 297 298 // Check for configure-time C include directories. 299 StringRef CIncludeDirs(C_INCLUDE_DIRS); 300 if (CIncludeDirs != "") { 301 SmallVector<StringRef, 5> dirs; 302 CIncludeDirs.split(dirs, ":"); 303 for (StringRef dir : dirs) { 304 StringRef Prefix = 305 llvm::sys::path::is_absolute(dir) ? "" : StringRef(D.SysRoot); 306 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir); 307 } 308 return; 309 } 310 311 // Add include directories specific to the selected multilib set and multilib. 312 if (GCCInstallation.isValid()) { 313 const MultilibSet::IncludeDirsFunc &Callback = 314 Multilibs.includeDirsCallback(); 315 if (Callback) { 316 for (const auto &Path : Callback(GCCInstallation.getMultilib())) 317 addExternCSystemIncludeIfExists( 318 DriverArgs, CC1Args, GCCInstallation.getInstallPath() + Path); 319 } 320 } 321 322 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include"); 323 } 324 325 void Solaris::addLibStdCxxIncludePaths( 326 const llvm::opt::ArgList &DriverArgs, 327 llvm::opt::ArgStringList &CC1Args) const { 328 // We need a detected GCC installation on Solaris (similar to Linux) 329 // to provide libstdc++'s headers. 330 if (!GCCInstallation.isValid()) 331 return; 332 333 // By default, look for the C++ headers in an include directory adjacent to 334 // the lib directory of the GCC installation. 335 // On Solaris this usually looks like /usr/gcc/X.Y/include/c++/X.Y.Z 336 StringRef LibDir = GCCInstallation.getParentLibPath(); 337 StringRef TripleStr = GCCInstallation.getTriple().str(); 338 const Multilib &Multilib = GCCInstallation.getMultilib(); 339 const GCCVersion &Version = GCCInstallation.getVersion(); 340 341 // The primary search for libstdc++ supports multiarch variants. 342 addLibStdCXXIncludePaths(LibDir.str() + "/../include/c++/" + Version.Text, 343 TripleStr, Multilib.includeSuffix(), DriverArgs, 344 CC1Args); 345 } 346