Coverage Report

Created: 2019-07-24 05:18

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