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