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/Config/config.h" 12 #include "clang/Driver/Compilation.h" 13 #include "clang/Driver/Driver.h" 14 #include "clang/Driver/DriverDiagnostic.h" 15 #include "clang/Driver/Options.h" 16 #include "llvm/Option/ArgList.h" 17 #include "llvm/Support/FileSystem.h" 18 #include "llvm/Support/Path.h" 19 20 using namespace clang::driver; 21 using namespace clang::driver::tools; 22 using namespace clang::driver::toolchains; 23 using namespace clang; 24 using namespace llvm::opt; 25 26 void solaris::Assembler::ConstructJob(Compilation &C, const JobAction &JA, 27 const InputInfo &Output, 28 const InputInfoList &Inputs, 29 const ArgList &Args, 30 const char *LinkingOutput) const { 31 claimNoWarnArgs(Args); 32 ArgStringList CmdArgs; 33 34 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler); 35 36 CmdArgs.push_back("-o"); 37 CmdArgs.push_back(Output.getFilename()); 38 39 for (const auto &II : Inputs) 40 CmdArgs.push_back(II.getFilename()); 41 42 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as")); 43 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs)); 44 } 45 46 void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA, 47 const InputInfo &Output, 48 const InputInfoList &Inputs, 49 const ArgList &Args, 50 const char *LinkingOutput) const { 51 ArgStringList CmdArgs; 52 53 // Demangle C++ names in errors 54 CmdArgs.push_back("-C"); 55 56 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_shared)) { 57 CmdArgs.push_back("-e"); 58 CmdArgs.push_back("_start"); 59 } 60 61 if (Args.hasArg(options::OPT_static)) { 62 CmdArgs.push_back("-Bstatic"); 63 CmdArgs.push_back("-dn"); 64 } else { 65 CmdArgs.push_back("-Bdynamic"); 66 if (Args.hasArg(options::OPT_shared)) { 67 CmdArgs.push_back("-shared"); 68 } 69 70 // libpthread has been folded into libc since Solaris 10, no need to do 71 // anything for pthreads. Claim argument to avoid warning. 72 Args.ClaimAllArgs(options::OPT_pthread); 73 Args.ClaimAllArgs(options::OPT_pthreads); 74 } 75 76 if (Output.isFilename()) { 77 CmdArgs.push_back("-o"); 78 CmdArgs.push_back(Output.getFilename()); 79 } else { 80 assert(Output.isNothing() && "Invalid output."); 81 } 82 83 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) { 84 if (!Args.hasArg(options::OPT_shared)) 85 CmdArgs.push_back( 86 Args.MakeArgString(getToolChain().GetFilePath("crt1.o"))); 87 88 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crti.o"))); 89 CmdArgs.push_back( 90 Args.MakeArgString(getToolChain().GetFilePath("values-Xa.o"))); 91 CmdArgs.push_back( 92 Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o"))); 93 } 94 95 getToolChain().AddFilePathLibArgs(Args, CmdArgs); 96 97 Args.AddAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group, 98 options::OPT_e, options::OPT_r}); 99 100 bool NeedsSanitizerDeps = addSanitizerRuntimes(getToolChain(), Args, CmdArgs); 101 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA); 102 103 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { 104 if (getToolChain().ShouldLinkCXXStdlib(Args)) 105 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs); 106 if (Args.hasArg(options::OPT_fstack_protector) || 107 Args.hasArg(options::OPT_fstack_protector_strong) || 108 Args.hasArg(options::OPT_fstack_protector_all)) { 109 // Explicitly link ssp libraries, not folded into Solaris libc. 110 CmdArgs.push_back("-lssp_nonshared"); 111 CmdArgs.push_back("-lssp"); 112 } 113 CmdArgs.push_back("-lgcc_s"); 114 CmdArgs.push_back("-lc"); 115 if (!Args.hasArg(options::OPT_shared)) { 116 CmdArgs.push_back("-lgcc"); 117 CmdArgs.push_back("-lm"); 118 } 119 if (NeedsSanitizerDeps) 120 linkSanitizerRuntimeDeps(getToolChain(), CmdArgs); 121 } 122 123 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) { 124 CmdArgs.push_back( 125 Args.MakeArgString(getToolChain().GetFilePath("crtend.o"))); 126 } 127 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtn.o"))); 128 129 getToolChain().addProfileRTLibs(Args, CmdArgs); 130 131 const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath()); 132 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs)); 133 } 134 135 static StringRef getSolarisLibSuffix(const llvm::Triple &Triple) { 136 switch (Triple.getArch()) { 137 case llvm::Triple::x86: 138 case llvm::Triple::sparc: 139 break; 140 case llvm::Triple::x86_64: 141 return "/amd64"; 142 case llvm::Triple::sparcv9: 143 return "/sparcv9"; 144 default: 145 llvm_unreachable("Unsupported architecture"); 146 } 147 return ""; 148 } 149 150 /// Solaris - Solaris tool chain which can call as(1) and ld(1) directly. 151 152 Solaris::Solaris(const Driver &D, const llvm::Triple &Triple, 153 const ArgList &Args) 154 : Generic_ELF(D, Triple, Args) { 155 156 GCCInstallation.init(Triple, Args); 157 158 StringRef LibSuffix = getSolarisLibSuffix(Triple); 159 path_list &Paths = getFilePaths(); 160 if (GCCInstallation.isValid()) { 161 // On Solaris gcc uses both an architecture-specific path with triple in it 162 // as well as a more generic lib path (+arch suffix). 163 addPathIfExists(D, 164 GCCInstallation.getInstallPath() + 165 GCCInstallation.getMultilib().gccSuffix(), 166 Paths); 167 addPathIfExists(D, GCCInstallation.getParentLibPath() + LibSuffix, Paths); 168 } 169 170 // If we are currently running Clang inside of the requested system root, 171 // add its parent library path to those searched. 172 if (StringRef(D.Dir).startswith(D.SysRoot)) 173 addPathIfExists(D, D.Dir + "/../lib", Paths); 174 175 addPathIfExists(D, D.SysRoot + "/usr/lib" + LibSuffix, Paths); 176 } 177 178 SanitizerMask Solaris::getSupportedSanitizers() const { 179 const bool IsX86 = getTriple().getArch() == llvm::Triple::x86; 180 SanitizerMask Res = ToolChain::getSupportedSanitizers(); 181 // FIXME: Omit X86_64 until 64-bit support is figured out. 182 if (IsX86) { 183 Res |= SanitizerKind::Address; 184 Res |= SanitizerKind::PointerCompare; 185 Res |= SanitizerKind::PointerSubtract; 186 } 187 Res |= SanitizerKind::Vptr; 188 return Res; 189 } 190 191 Tool *Solaris::buildAssembler() const { 192 return new tools::solaris::Assembler(*this); 193 } 194 195 Tool *Solaris::buildLinker() const { return new tools::solaris::Linker(*this); } 196 197 void Solaris::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 198 ArgStringList &CC1Args) const { 199 const Driver &D = getDriver(); 200 201 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)) 202 return; 203 204 if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) 205 addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/local/include"); 206 207 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 208 SmallString<128> P(D.ResourceDir); 209 llvm::sys::path::append(P, "include"); 210 addSystemInclude(DriverArgs, CC1Args, P); 211 } 212 213 if (DriverArgs.hasArg(options::OPT_nostdlibinc)) 214 return; 215 216 // Check for configure-time C include directories. 217 StringRef CIncludeDirs(C_INCLUDE_DIRS); 218 if (CIncludeDirs != "") { 219 SmallVector<StringRef, 5> dirs; 220 CIncludeDirs.split(dirs, ":"); 221 for (StringRef dir : dirs) { 222 StringRef Prefix = 223 llvm::sys::path::is_absolute(dir) ? StringRef(D.SysRoot) : ""; 224 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir); 225 } 226 return; 227 } 228 229 // Add include directories specific to the selected multilib set and multilib. 230 if (GCCInstallation.isValid()) { 231 const MultilibSet::IncludeDirsFunc &Callback = 232 Multilibs.includeDirsCallback(); 233 if (Callback) { 234 for (const auto &Path : Callback(GCCInstallation.getMultilib())) 235 addExternCSystemIncludeIfExists( 236 DriverArgs, CC1Args, GCCInstallation.getInstallPath() + Path); 237 } 238 } 239 240 addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include"); 241 } 242 243 void Solaris::addLibStdCxxIncludePaths( 244 const llvm::opt::ArgList &DriverArgs, 245 llvm::opt::ArgStringList &CC1Args) const { 246 // We need a detected GCC installation on Solaris (similar to Linux) 247 // to provide libstdc++'s headers. 248 if (!GCCInstallation.isValid()) 249 return; 250 251 // By default, look for the C++ headers in an include directory adjacent to 252 // the lib directory of the GCC installation. 253 // On Solaris this usually looks like /usr/gcc/X.Y/include/c++/X.Y.Z 254 StringRef LibDir = GCCInstallation.getParentLibPath(); 255 StringRef TripleStr = GCCInstallation.getTriple().str(); 256 const Multilib &Multilib = GCCInstallation.getMultilib(); 257 const GCCVersion &Version = GCCInstallation.getVersion(); 258 259 // The primary search for libstdc++ supports multiarch variants. 260 addLibStdCXXIncludePaths(LibDir.str() + "/../include", "/c++/" + Version.Text, 261 TripleStr, 262 /*GCCMultiarchTriple*/ "", 263 /*TargetMultiarchTriple*/ "", 264 Multilib.includeSuffix(), DriverArgs, CC1Args); 265 } 266