Coverage Report

Created: 2020-02-25 14:32

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