clang  9.0.0svn
Solaris.cpp
Go to the documentation of this file.
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"
13 #include "clang/Driver/Driver.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 
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 
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  } else {
69  CmdArgs.push_back("--dynamic-linker");
70  CmdArgs.push_back(
71  Args.MakeArgString(getToolChain().GetFilePath("ld.so.1")));
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  if (!Args.hasArg(options::OPT_shared))
89  CmdArgs.push_back(
90  Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
91 
92  CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
93  CmdArgs.push_back(
94  Args.MakeArgString(getToolChain().GetFilePath("values-Xa.o")));
95  CmdArgs.push_back(
96  Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
97  }
98 
99  // Provide __start___sancov_guards. Solaris ld doesn't automatically create
100  // __start_SECNAME labels.
101  CmdArgs.push_back("--whole-archive");
102  CmdArgs.push_back(
103  getToolChain().getCompilerRTArgString(Args, "sancov_begin"));
104  CmdArgs.push_back("--no-whole-archive");
105 
106  getToolChain().AddFilePathLibArgs(Args, CmdArgs);
107 
108  Args.AddAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group,
109  options::OPT_e, options::OPT_r});
110 
111  bool NeedsSanitizerDeps = addSanitizerRuntimes(getToolChain(), Args, CmdArgs);
112  AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
113 
114  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
115  if (getToolChain().ShouldLinkCXXStdlib(Args))
116  getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
117  if (Args.hasArg(options::OPT_fstack_protector) ||
118  Args.hasArg(options::OPT_fstack_protector_strong) ||
119  Args.hasArg(options::OPT_fstack_protector_all)) {
120  // Explicitly link ssp libraries, not folded into Solaris libc.
121  CmdArgs.push_back("-lssp_nonshared");
122  CmdArgs.push_back("-lssp");
123  }
124  CmdArgs.push_back("-lgcc_s");
125  CmdArgs.push_back("-lc");
126  if (!Args.hasArg(options::OPT_shared)) {
127  CmdArgs.push_back("-lgcc");
128  CmdArgs.push_back("-lm");
129  }
130  if (NeedsSanitizerDeps)
131  linkSanitizerRuntimeDeps(getToolChain(), CmdArgs);
132  }
133 
134  // Provide __stop___sancov_guards. Solaris ld doesn't automatically create
135  // __stop_SECNAME labels.
136  CmdArgs.push_back("--whole-archive");
137  CmdArgs.push_back(
138  getToolChain().getCompilerRTArgString(Args, "sancov_end"));
139  CmdArgs.push_back("--no-whole-archive");
140 
141  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
142  CmdArgs.push_back(
143  Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
144  }
145  CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtn.o")));
146 
147  getToolChain().addProfileRTLibs(Args, CmdArgs);
148 
149  const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
150  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
151 }
152 
153 static StringRef getSolarisLibSuffix(const llvm::Triple &Triple) {
154  switch (Triple.getArch()) {
155  case llvm::Triple::x86:
156  case llvm::Triple::sparc:
157  break;
158  case llvm::Triple::x86_64:
159  return "/amd64";
160  case llvm::Triple::sparcv9:
161  return "/sparcv9";
162  default:
163  llvm_unreachable("Unsupported architecture");
164  }
165  return "";
166 }
167 
168 /// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
169 
170 Solaris::Solaris(const Driver &D, const llvm::Triple &Triple,
171  const ArgList &Args)
172  : Generic_ELF(D, Triple, Args) {
173 
174  GCCInstallation.init(Triple, Args);
175 
176  StringRef LibSuffix = getSolarisLibSuffix(Triple);
177  path_list &Paths = getFilePaths();
178  if (GCCInstallation.isValid()) {
179  // On Solaris gcc uses both an architecture-specific path with triple in it
180  // as well as a more generic lib path (+arch suffix).
181  addPathIfExists(D,
184  Paths);
185  addPathIfExists(D, GCCInstallation.getParentLibPath() + LibSuffix, Paths);
186  }
187 
188  // If we are currently running Clang inside of the requested system root,
189  // add its parent library path to those searched.
190  if (StringRef(D.Dir).startswith(D.SysRoot))
191  addPathIfExists(D, D.Dir + "/../lib", Paths);
192 
193  addPathIfExists(D, D.SysRoot + "/usr/lib" + LibSuffix, Paths);
194 }
195 
197  const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
199  // FIXME: Omit X86_64 until 64-bit support is figured out.
200  if (IsX86) {
201  Res |= SanitizerKind::Address;
202  }
203  Res |= SanitizerKind::Vptr;
204  return Res;
205 }
206 
208  return new tools::solaris::Assembler(*this);
209 }
210 
211 Tool *Solaris::buildLinker() const { return new tools::solaris::Linker(*this); }
212 
213 void Solaris::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
214  ArgStringList &CC1Args) const {
215  const Driver &D = getDriver();
216 
217  if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
218  return;
219 
220  if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
221  addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/local/include");
222 
223  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
225  llvm::sys::path::append(P, "include");
226  addSystemInclude(DriverArgs, CC1Args, P);
227  }
228 
229  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
230  return;
231 
232  // Check for configure-time C include directories.
233  StringRef CIncludeDirs(C_INCLUDE_DIRS);
234  if (CIncludeDirs != "") {
236  CIncludeDirs.split(dirs, ":");
237  for (StringRef dir : dirs) {
238  StringRef Prefix =
239  llvm::sys::path::is_absolute(dir) ? StringRef(D.SysRoot) : "";
240  addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
241  }
242  return;
243  }
244 
245  // Add include directories specific to the selected multilib set and multilib.
246  if (GCCInstallation.isValid()) {
247  const MultilibSet::IncludeDirsFunc &Callback =
249  if (Callback) {
250  for (const auto &Path : Callback(GCCInstallation.getMultilib()))
252  DriverArgs, CC1Args, GCCInstallation.getInstallPath() + Path);
253  }
254  }
255 
256  addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include");
257 }
258 
260  const llvm::opt::ArgList &DriverArgs,
261  llvm::opt::ArgStringList &CC1Args) const {
262  // We need a detected GCC installation on Solaris (similar to Linux)
263  // to provide libstdc++'s headers.
264  if (!GCCInstallation.isValid())
265  return;
266 
267  // By default, look for the C++ headers in an include directory adjacent to
268  // the lib directory of the GCC installation.
269  // On Solaris this usually looks like /usr/gcc/X.Y/include/c++/X.Y.Z
270  StringRef LibDir = GCCInstallation.getParentLibPath();
271  StringRef TripleStr = GCCInstallation.getTriple().str();
273  const GCCVersion &Version = GCCInstallation.getVersion();
274 
275  // The primary search for libstdc++ supports multiarch variants.
276  addLibStdCXXIncludePaths(LibDir.str() + "/../include", "/c++/" + Version.Text,
277  TripleStr,
278  /*GCCMultiarchTriple*/ "",
279  /*TargetMultiarchTriple*/ "",
280  Multilib.includeSuffix(), DriverArgs, CC1Args);
281 }
static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory with extern "C" semantics to CC1 arguments...
Definition: ToolChain.cpp:716
SanitizerMask getSupportedSanitizers() const override
Return sanitizers which are available in this toolchain.
Definition: Solaris.cpp:196
static void addExternCSystemIncludeIfExists(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Definition: ToolChain.cpp:723
Tool * buildLinker() const override
Definition: Solaris.cpp:211
const IncludeDirsFunc & includeDirsCallback() const
Definition: Multilib.h:174
const llvm::Triple & getTriple() const
Get the GCC triple for the detected install.
Definition: Gnu.h:219
StringRef P
Tool * buildAssembler() const override
Definition: Solaris.cpp:207
void addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Definition: Solaris.cpp:259
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs...
Definition: Solaris.cpp:46
Struct to store and manipulate GCC versions.
Definition: Gnu.h:156
static StringRef getSolarisLibSuffix(const llvm::Triple &Triple)
Definition: Solaris.cpp:153
const char * getFilename() const
Definition: InputInfo.h:83
GCCInstallationDetector GCCInstallation
Definition: Gnu.h:279
void linkSanitizerRuntimeDeps(const ToolChain &TC, llvm::opt::ArgStringList &CmdArgs)
const std::string & includeSuffix() const
Get the include directory suffix.
Definition: Multilib.h:66
std::string Dir
The path the driver executable was in, as invoked from the command line.
Definition: Driver.h:119
const std::string & gccSuffix() const
Get the detected GCC installation path suffix for the multi-arch target variant.
Definition: Multilib.h:44
bool addSanitizerRuntimes(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
path_list & getFilePaths()
Definition: ToolChain.h:223
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:57
const GCCVersion & getVersion() const
Get the detected GCC version string.
Definition: Gnu.h:238
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
Definition: Solaris.cpp:213
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:205
This corresponds to a single GCC Multilib, or a segment of one controlled by a command line flag...
Definition: Multilib.h:28
const Driver & getDriver() const
Definition: ToolChain.h:185
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory to CC1 arguments.
Definition: ToolChain.cpp:701
StringRef getParentLibPath() const
Get the detected GCC parent lib path.
Definition: Gnu.h:225
Dataflow Directional Tag Classes.
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
std::string SysRoot
sysroot, if present
Definition: Driver.h:147
Tool - Information on a specific compilation tool.
Definition: Tool.h:33
void claimNoWarnArgs(const llvm::opt::ArgList &Args)
std::function< std::vector< std::string >(const Multilib &M)> IncludeDirsFunc
Definition: Multilib.h:115
uint64_t SanitizerMask
Definition: Sanitizers.h:25
std::string Text
The unparsed text of the version.
Definition: Gnu.h:158
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
const llvm::Triple & getTriple() const
Definition: ToolChain.h:187
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:817
bool isNothing() const
Definition: InputInfo.h:74
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs...
Definition: Solaris.cpp:26
void addPathIfExists(const Driver &D, const Twine &Path, ToolChain::path_list &Paths)
Definition: CommonArgs.cpp:61
bool isFilename() const
Definition: InputInfo.h:75
bool addLibStdCXXIncludePaths(Twine Base, Twine Suffix, StringRef GCCTriple, StringRef GCCMultiarchTriple, StringRef TargetMultiarchTriple, Twine IncludeSuffix, const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Helper to add the variant paths of a libstdc++ installation.
Definition: Gnu.cpp:2555
void init(const llvm::Triple &TargetTriple, const llvm::opt::ArgList &Args, ArrayRef< std::string > ExtraTripleAliases=None)
Initialize a GCCInstallationDetector from the driver.
Definition: Gnu.cpp:1701
StringRef getInstallPath() const
Get the detected GCC installation path.
Definition: Gnu.h:222
const Multilib & getMultilib() const
Get the detected Multilib.
Definition: Gnu.h:228
std::string ResourceDir
The path to the compiler resource directory.
Definition: Driver.h:131
bool isValid() const
Check whether we detected a valid GCC install.
Definition: Gnu.h:216