Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Driver/ToolChains/NaCl.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- NaCl.cpp - Native Client 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 "NaCl.h"
10
#include "InputInfo.h"
11
#include "CommonArgs.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/Path.h"
18
19
using namespace clang::driver;
20
using namespace clang::driver::tools;
21
using namespace clang::driver::toolchains;
22
using namespace clang;
23
using namespace llvm::opt;
24
25
// NaCl ARM assembly (inline or standalone) can be written with a set of macros
26
// for the various SFI requirements like register masking. The assembly tool
27
// inserts the file containing the macros as an input into all the assembly
28
// jobs.
29
void nacltools::AssemblerARM::ConstructJob(Compilation &C, const JobAction &JA,
30
                                           const InputInfo &Output,
31
                                           const InputInfoList &Inputs,
32
                                           const ArgList &Args,
33
5
                                           const char *LinkingOutput) const {
34
5
  const toolchains::NaClToolChain &ToolChain =
35
5
      static_cast<const toolchains::NaClToolChain &>(getToolChain());
36
5
  InputInfo NaClMacros(types::TY_PP_Asm, ToolChain.GetNaClArmMacrosPath(),
37
5
                       "nacl-arm-macros.s");
38
5
  InputInfoList NewInputs;
39
5
  NewInputs.push_back(NaClMacros);
40
5
  NewInputs.append(Inputs.begin(), Inputs.end());
41
5
  gnutools::Assembler::ConstructJob(C, JA, Output, NewInputs, Args,
42
5
                                    LinkingOutput);
43
5
}
44
45
// This is quite similar to gnutools::Linker::ConstructJob with changes that
46
// we use static by default, do not yet support sanitizers or LTO, and a few
47
// others. Eventually we can support more of that and hopefully migrate back
48
// to gnutools::Linker.
49
void nacltools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
50
                                     const InputInfo &Output,
51
                                     const InputInfoList &Inputs,
52
                                     const ArgList &Args,
53
13
                                     const char *LinkingOutput) const {
54
13
55
13
  const toolchains::NaClToolChain &ToolChain =
56
13
      static_cast<const toolchains::NaClToolChain &>(getToolChain());
57
13
  const Driver &D = ToolChain.getDriver();
58
13
  const llvm::Triple::ArchType Arch = ToolChain.getArch();
59
13
  const bool IsStatic =
60
13
      !Args.hasArg(options::OPT_dynamic) && !Args.hasArg(options::OPT_shared);
61
13
62
13
  ArgStringList CmdArgs;
63
13
64
13
  // Silence warning for "clang -g foo.o -o foo"
65
13
  Args.ClaimAllArgs(options::OPT_g_Group);
66
13
  // and "clang -emit-llvm foo.o -o foo"
67
13
  Args.ClaimAllArgs(options::OPT_emit_llvm);
68
13
  // and for "clang -w foo.o -o foo". Other warning options are already
69
13
  // handled somewhere else.
70
13
  Args.ClaimAllArgs(options::OPT_w);
71
13
72
13
  if (!D.SysRoot.empty())
73
0
    CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
74
13
75
13
  if (Args.hasArg(options::OPT_rdynamic))
76
0
    CmdArgs.push_back("-export-dynamic");
77
13
78
13
  if (Args.hasArg(options::OPT_s))
79
0
    CmdArgs.push_back("-s");
80
13
81
13
  // NaClToolChain doesn't have ExtraOpts like Linux; the only relevant flag
82
13
  // from there is --build-id, which we do want.
83
13
  CmdArgs.push_back("--build-id");
84
13
85
13
  if (!IsStatic)
86
0
    CmdArgs.push_back("--eh-frame-hdr");
87
13
88
13
  CmdArgs.push_back("-m");
89
13
  if (Arch == llvm::Triple::x86)
90
2
    CmdArgs.push_back("elf_i386_nacl");
91
11
  else if (Arch == llvm::Triple::arm)
92
5
    CmdArgs.push_back("armelf_nacl");
93
6
  else if (Arch == llvm::Triple::x86_64)
94
2
    CmdArgs.push_back("elf_x86_64_nacl");
95
4
  else if (Arch == llvm::Triple::mipsel)
96
2
    CmdArgs.push_back("mipselelf_nacl");
97
2
  else
98
2
    D.Diag(diag::err_target_unsupported_arch) << ToolChain.getArchName()
99
2
                                              << "Native Client";
100
13
101
13
  if (IsStatic)
102
13
    CmdArgs.push_back("-static");
103
0
  else if (Args.hasArg(options::OPT_shared))
104
0
    CmdArgs.push_back("-shared");
105
13
106
13
  CmdArgs.push_back("-o");
107
13
  CmdArgs.push_back(Output.getFilename());
108
13
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
109
13
    if (!Args.hasArg(options::OPT_shared))
110
13
      CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt1.o")));
111
13
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
112
13
113
13
    const char *crtbegin;
114
13
    if (IsStatic)
115
13
      crtbegin = "crtbeginT.o";
116
0
    else if (Args.hasArg(options::OPT_shared))
117
0
      crtbegin = "crtbeginS.o";
118
0
    else
119
0
      crtbegin = "crtbegin.o";
120
13
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
121
13
  }
122
13
123
13
  Args.AddAllArgs(CmdArgs, options::OPT_L);
124
13
  Args.AddAllArgs(CmdArgs, options::OPT_u);
125
13
126
13
  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
127
13
128
13
  if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
129
0
    CmdArgs.push_back("--no-demangle");
130
13
131
13
  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
132
13
133
13
  if (D.CCCIsCXX() &&
134
13
      
!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)4
) {
135
4
    if (ToolChain.ShouldLinkCXXStdlib(Args)) {
136
4
      bool OnlyLibstdcxxStatic =
137
4
          Args.hasArg(options::OPT_static_libstdcxx) && 
!IsStatic0
;
138
4
      if (OnlyLibstdcxxStatic)
139
0
        CmdArgs.push_back("-Bstatic");
140
4
      ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
141
4
      if (OnlyLibstdcxxStatic)
142
0
        CmdArgs.push_back("-Bdynamic");
143
4
    }
144
4
    CmdArgs.push_back("-lm");
145
4
  }
146
13
147
13
  if (!Args.hasArg(options::OPT_nostdlib)) {
148
13
    if (!Args.hasArg(options::OPT_nodefaultlibs)) {
149
13
      // Always use groups, since it has no effect on dynamic libraries.
150
13
      CmdArgs.push_back("--start-group");
151
13
      CmdArgs.push_back("-lc");
152
13
      // NaCl's libc++ currently requires libpthread, so just always include it
153
13
      // in the group for C++.
154
13
      if (Args.hasArg(options::OPT_pthread) ||
155
13
          Args.hasArg(options::OPT_pthreads) || D.CCCIsCXX()) {
156
4
        // Gold, used by Mips, handles nested groups differently than ld, and
157
4
        // without '-lnacl' it prefers symbols from libpthread.a over libnacl.a,
158
4
        // which is not a desired behaviour here.
159
4
        // See https://sourceware.org/ml/binutils/2015-03/msg00034.html
160
4
        if (getToolChain().getArch() == llvm::Triple::mipsel)
161
1
          CmdArgs.push_back("-lnacl");
162
4
163
4
        CmdArgs.push_back("-lpthread");
164
4
      }
165
13
166
13
      CmdArgs.push_back("-lgcc");
167
13
      CmdArgs.push_back("--as-needed");
168
13
      if (IsStatic)
169
13
        CmdArgs.push_back("-lgcc_eh");
170
0
      else
171
0
        CmdArgs.push_back("-lgcc_s");
172
13
      CmdArgs.push_back("--no-as-needed");
173
13
174
13
      // Mips needs to create and use pnacl_legacy library that contains
175
13
      // definitions from bitcode/pnaclmm.c and definitions for
176
13
      // __nacl_tp_tls_offset() and __nacl_tp_tdb_offset().
177
13
      if (getToolChain().getArch() == llvm::Triple::mipsel)
178
2
        CmdArgs.push_back("-lpnacl_legacy");
179
13
180
13
      CmdArgs.push_back("--end-group");
181
13
    }
182
13
183
13
    if (!Args.hasArg(options::OPT_nostartfiles)) {
184
13
      const char *crtend;
185
13
      if (Args.hasArg(options::OPT_shared))
186
0
        crtend = "crtendS.o";
187
13
      else
188
13
        crtend = "crtend.o";
189
13
190
13
      CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
191
13
      CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
192
13
    }
193
13
  }
194
13
195
13
  const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
196
13
  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
197
13
}
198
199
/// NaCl Toolchain
200
NaClToolChain::NaClToolChain(const Driver &D, const llvm::Triple &Triple,
201
                             const ArgList &Args)
202
23
    : Generic_ELF(D, Triple, Args) {
203
23
204
23
  // Remove paths added by Generic_GCC. NaCl Toolchain cannot use the
205
23
  // default paths, and must instead only use the paths provided
206
23
  // with this toolchain based on architecture.
207
23
  path_list &file_paths = getFilePaths();
208
23
  path_list &prog_paths = getProgramPaths();
209
23
210
23
  file_paths.clear();
211
23
  prog_paths.clear();
212
23
213
23
  // Path for library files (libc.a, ...)
214
23
  std::string FilePath(getDriver().Dir + "/../");
215
23
216
23
  // Path for tools (clang, ld, etc..)
217
23
  std::string ProgPath(getDriver().Dir + "/../");
218
23
219
23
  // Path for toolchain libraries (libgcc.a, ...)
220
23
  std::string ToolPath(getDriver().ResourceDir + "/lib/");
221
23
222
23
  switch (Triple.getArch()) {
223
23
  case llvm::Triple::x86:
224
2
    file_paths.push_back(FilePath + "x86_64-nacl/lib32");
225
2
    file_paths.push_back(FilePath + "i686-nacl/usr/lib");
226
2
    prog_paths.push_back(ProgPath + "x86_64-nacl/bin");
227
2
    file_paths.push_back(ToolPath + "i686-nacl");
228
2
    break;
229
23
  case llvm::Triple::x86_64:
230
5
    file_paths.push_back(FilePath + "x86_64-nacl/lib");
231
5
    file_paths.push_back(FilePath + "x86_64-nacl/usr/lib");
232
5
    prog_paths.push_back(ProgPath + "x86_64-nacl/bin");
233
5
    file_paths.push_back(ToolPath + "x86_64-nacl");
234
5
    break;
235
23
  case llvm::Triple::arm:
236
5
    file_paths.push_back(FilePath + "arm-nacl/lib");
237
5
    file_paths.push_back(FilePath + "arm-nacl/usr/lib");
238
5
    prog_paths.push_back(ProgPath + "arm-nacl/bin");
239
5
    file_paths.push_back(ToolPath + "arm-nacl");
240
5
    break;
241
23
  case llvm::Triple::mipsel:
242
5
    file_paths.push_back(FilePath + "mipsel-nacl/lib");
243
5
    file_paths.push_back(FilePath + "mipsel-nacl/usr/lib");
244
5
    prog_paths.push_back(ProgPath + "bin");
245
5
    file_paths.push_back(ToolPath + "mipsel-nacl");
246
5
    break;
247
23
  default:
248
6
    break;
249
23
  }
250
23
251
23
  NaClArmMacrosPath = GetFilePath("nacl-arm-macros.s");
252
23
}
253
254
void NaClToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
255
23
                                              ArgStringList &CC1Args) const {
256
23
  const Driver &D = getDriver();
257
23
  if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
258
0
    return;
259
23
260
23
  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
261
23
    SmallString<128> P(D.ResourceDir);
262
23
    llvm::sys::path::append(P, "include");
263
23
    addSystemInclude(DriverArgs, CC1Args, P.str());
264
23
  }
265
23
266
23
  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
267
0
    return;
268
23
269
23
  SmallString<128> P(D.Dir + "/../");
270
23
  switch (getTriple().getArch()) {
271
23
  case llvm::Triple::x86:
272
2
    // x86 is special because multilib style uses x86_64-nacl/include for libc
273
2
    // headers but the SDK wants i686-nacl/usr/include. The other architectures
274
2
    // have the same substring.
275
2
    llvm::sys::path::append(P, "i686-nacl/usr/include");
276
2
    addSystemInclude(DriverArgs, CC1Args, P.str());
277
2
    llvm::sys::path::remove_filename(P);
278
2
    llvm::sys::path::remove_filename(P);
279
2
    llvm::sys::path::remove_filename(P);
280
2
    llvm::sys::path::append(P, "x86_64-nacl/include");
281
2
    addSystemInclude(DriverArgs, CC1Args, P.str());
282
2
    return;
283
23
  case llvm::Triple::arm:
284
5
    llvm::sys::path::append(P, "arm-nacl/usr/include");
285
5
    break;
286
23
  case llvm::Triple::x86_64:
287
5
    llvm::sys::path::append(P, "x86_64-nacl/usr/include");
288
5
    break;
289
23
  case llvm::Triple::mipsel:
290
5
    llvm::sys::path::append(P, "mipsel-nacl/usr/include");
291
5
    break;
292
23
  default:
293
6
    return;
294
15
  }
295
15
296
15
  addSystemInclude(DriverArgs, CC1Args, P.str());
297
15
  llvm::sys::path::remove_filename(P);
298
15
  llvm::sys::path::remove_filename(P);
299
15
  llvm::sys::path::append(P, "include");
300
15
  addSystemInclude(DriverArgs, CC1Args, P.str());
301
15
}
302
303
void NaClToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
304
4
                                        ArgStringList &CmdArgs) const {
305
4
  // Check for -stdlib= flags. We only support libc++ but this consumes the arg
306
4
  // if the value is libc++, and emits an error for other values.
307
4
  GetCXXStdlibType(Args);
308
4
  CmdArgs.push_back("-lc++");
309
4
}
310
311
void NaClToolChain::addLibCxxIncludePaths(
312
    const llvm::opt::ArgList &DriverArgs,
313
14
    llvm::opt::ArgStringList &CC1Args) const {
314
14
  const Driver &D = getDriver();
315
14
316
14
  SmallString<128> P(D.Dir + "/../");
317
14
  switch (getTriple().getArch()) {
318
14
  default:
319
4
    break;
320
14
  case llvm::Triple::arm:
321
1
    llvm::sys::path::append(P, "arm-nacl/include/c++/v1");
322
1
    addSystemInclude(DriverArgs, CC1Args, P.str());
323
1
    break;
324
14
  case llvm::Triple::x86:
325
1
    llvm::sys::path::append(P, "x86_64-nacl/include/c++/v1");
326
1
    addSystemInclude(DriverArgs, CC1Args, P.str());
327
1
    break;
328
14
  case llvm::Triple::x86_64:
329
4
    llvm::sys::path::append(P, "x86_64-nacl/include/c++/v1");
330
4
    addSystemInclude(DriverArgs, CC1Args, P.str());
331
4
    break;
332
14
  case llvm::Triple::mipsel:
333
4
    llvm::sys::path::append(P, "mipsel-nacl/include/c++/v1");
334
4
    addSystemInclude(DriverArgs, CC1Args, P.str());
335
4
    break;
336
14
  }
337
14
}
338
339
ToolChain::CXXStdlibType
340
18
NaClToolChain::GetCXXStdlibType(const ArgList &Args) const {
341
18
  if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
342
0
    StringRef Value = A->getValue();
343
0
    if (Value == "libc++")
344
0
      return ToolChain::CST_Libcxx;
345
0
    getDriver().Diag(clang::diag::err_drv_invalid_stdlib_name)
346
0
        << A->getAsString(Args);
347
0
  }
348
18
349
18
  return ToolChain::CST_Libcxx;
350
18
}
351
352
std::string
353
NaClToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
354
49
                                           types::ID InputType) const {
355
49
  llvm::Triple TheTriple(ComputeLLVMTriple(Args, InputType));
356
49
  if (TheTriple.getArch() == llvm::Triple::arm &&
357
49
      
TheTriple.getEnvironment() == llvm::Triple::UnknownEnvironment15
)
358
3
    TheTriple.setEnvironment(llvm::Triple::GNUEABIHF);
359
49
  return TheTriple.getTriple();
360
49
}
361
362
13
Tool *NaClToolChain::buildLinker() const {
363
13
  return new tools::nacltools::Linker(*this);
364
13
}
365
366
13
Tool *NaClToolChain::buildAssembler() const {
367
13
  if (getTriple().getArch() == llvm::Triple::arm)
368
5
    return new tools::nacltools::AssemblerARM(*this);
369
8
  return new tools::gnutools::Assembler(*this);
370
8
}