Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Driver/ToolChains/NetBSD.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- NetBSD.cpp - NetBSD 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 "NetBSD.h"
10
#include "Arch/ARM.h"
11
#include "Arch/Mips.h"
12
#include "Arch/Sparc.h"
13
#include "CommonArgs.h"
14
#include "clang/Driver/Compilation.h"
15
#include "clang/Driver/Driver.h"
16
#include "clang/Driver/Options.h"
17
#include "clang/Driver/SanitizerArgs.h"
18
#include "llvm/Option/ArgList.h"
19
#include "llvm/Support/VirtualFileSystem.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 netbsd::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
28
                                     const InputInfo &Output,
29
                                     const InputInfoList &Inputs,
30
                                     const ArgList &Args,
31
65
                                     const char *LinkingOutput) const {
32
65
  claimNoWarnArgs(Args);
33
65
  ArgStringList CmdArgs;
34
65
35
65
  // GNU as needs different flags for creating the correct output format
36
65
  // on architectures with different ABIs or optional feature sets.
37
65
  switch (getToolChain().getArch()) {
38
65
  case llvm::Triple::x86:
39
0
    CmdArgs.push_back("--32");
40
0
    break;
41
65
  case llvm::Triple::arm:
42
9
  case llvm::Triple::armeb:
43
9
  case llvm::Triple::thumb:
44
9
  case llvm::Triple::thumbeb: {
45
9
    StringRef MArch, MCPU;
46
9
    arm::getARMArchCPUFromArgs(Args, MArch, MCPU, /*FromAs*/ true);
47
9
    std::string Arch =
48
9
        arm::getARMTargetCPU(MCPU, MArch, getToolChain().getTriple());
49
9
    CmdArgs.push_back(Args.MakeArgString("-mcpu=" + Arch));
50
9
    break;
51
9
  }
52
9
53
9
  case llvm::Triple::mips:
54
0
  case llvm::Triple::mipsel:
55
0
  case llvm::Triple::mips64:
56
0
  case llvm::Triple::mips64el: {
57
0
    StringRef CPUName;
58
0
    StringRef ABIName;
59
0
    mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
60
0
61
0
    CmdArgs.push_back("-march");
62
0
    CmdArgs.push_back(CPUName.data());
63
0
64
0
    CmdArgs.push_back("-mabi");
65
0
    CmdArgs.push_back(mips::getGnuCompatibleMipsABIName(ABIName).data());
66
0
67
0
    if (getToolChain().getTriple().isLittleEndian())
68
0
      CmdArgs.push_back("-EL");
69
0
    else
70
0
      CmdArgs.push_back("-EB");
71
0
72
0
    AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
73
0
    break;
74
0
  }
75
0
76
27
  case llvm::Triple::sparc:
77
27
  case llvm::Triple::sparcel: {
78
27
    CmdArgs.push_back("-32");
79
27
    std::string CPU = getCPUName(Args, getToolChain().getTriple());
80
27
    CmdArgs.push_back(sparc::getSparcAsmModeForCPU(CPU, getToolChain().getTriple()));
81
27
    AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
82
27
    break;
83
27
  }
84
27
85
28
  case llvm::Triple::sparcv9: {
86
28
    CmdArgs.push_back("-64");
87
28
    std::string CPU = getCPUName(Args, getToolChain().getTriple());
88
28
    CmdArgs.push_back(sparc::getSparcAsmModeForCPU(CPU, getToolChain().getTriple()));
89
28
    AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
90
28
    break;
91
27
  }
92
27
93
27
  default:
94
1
    break;
95
65
  }
96
65
97
65
  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
98
65
99
65
  CmdArgs.push_back("-o");
100
65
  CmdArgs.push_back(Output.getFilename());
101
65
102
65
  for (const auto &II : Inputs)
103
65
    CmdArgs.push_back(II.getFilename());
104
65
105
65
  const char *Exec = Args.MakeArgString((getToolChain().GetProgramPath("as")));
106
65
  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
107
65
}
108
109
void netbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
110
                                  const InputInfo &Output,
111
                                  const InputInfoList &Inputs,
112
                                  const ArgList &Args,
113
153
                                  const char *LinkingOutput) const {
114
153
  const toolchains::NetBSD &ToolChain =
115
153
    static_cast<const toolchains::NetBSD &>(getToolChain());
116
153
  const Driver &D = ToolChain.getDriver();
117
153
  ArgStringList CmdArgs;
118
153
119
153
  if (!D.SysRoot.empty())
120
101
    CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
121
153
122
153
  CmdArgs.push_back("--eh-frame-hdr");
123
153
  if (Args.hasArg(options::OPT_static)) {
124
35
    CmdArgs.push_back("-Bstatic");
125
35
    if (Args.hasArg(options::OPT_pie)) {
126
1
      Args.AddAllArgs(CmdArgs, options::OPT_pie);
127
1
      CmdArgs.push_back("--no-dynamic-linker");
128
1
    }
129
118
  } else {
130
118
    if (Args.hasArg(options::OPT_rdynamic))
131
0
      CmdArgs.push_back("-export-dynamic");
132
118
    if (Args.hasArg(options::OPT_shared)) {
133
1
      CmdArgs.push_back("-Bshareable");
134
117
    } else {
135
117
      Args.AddAllArgs(CmdArgs, options::OPT_pie);
136
117
      CmdArgs.push_back("-dynamic-linker");
137
117
      CmdArgs.push_back("/libexec/ld.elf_so");
138
117
    }
139
118
  }
140
153
141
153
  // Many NetBSD architectures support more than one ABI.
142
153
  // Determine the correct emulation for ld.
143
153
  switch (ToolChain.getArch()) {
144
153
  case llvm::Triple::x86:
145
10
    CmdArgs.push_back("-m");
146
10
    CmdArgs.push_back("elf_i386");
147
10
    break;
148
153
  case llvm::Triple::arm:
149
20
  case llvm::Triple::thumb:
150
20
    CmdArgs.push_back("-m");
151
20
    switch (ToolChain.getTriple().getEnvironment()) {
152
20
    case llvm::Triple::EABI:
153
15
    case llvm::Triple::GNUEABI:
154
15
      CmdArgs.push_back("armelf_nbsd_eabi");
155
15
      break;
156
15
    case llvm::Triple::EABIHF:
157
3
    case llvm::Triple::GNUEABIHF:
158
3
      CmdArgs.push_back("armelf_nbsd_eabihf");
159
3
      break;
160
3
    default:
161
2
      CmdArgs.push_back("armelf_nbsd");
162
2
      break;
163
20
    }
164
20
    break;
165
20
  case llvm::Triple::armeb:
166
6
  case llvm::Triple::thumbeb:
167
6
    arm::appendBE8LinkFlag(Args, CmdArgs, ToolChain.getEffectiveTriple());
168
6
    CmdArgs.push_back("-m");
169
6
    switch (ToolChain.getTriple().getEnvironment()) {
170
6
    case llvm::Triple::EABI:
171
6
    case llvm::Triple::GNUEABI:
172
6
      CmdArgs.push_back("armelfb_nbsd_eabi");
173
6
      break;
174
6
    case llvm::Triple::EABIHF:
175
0
    case llvm::Triple::GNUEABIHF:
176
0
      CmdArgs.push_back("armelfb_nbsd_eabihf");
177
0
      break;
178
0
    default:
179
0
      CmdArgs.push_back("armelfb_nbsd");
180
0
      break;
181
6
    }
182
6
    break;
183
6
  case llvm::Triple::mips64:
184
2
  case llvm::Triple::mips64el:
185
2
    if (mips::hasMipsAbiArg(Args, "32")) {
186
0
      CmdArgs.push_back("-m");
187
0
      if (ToolChain.getArch() == llvm::Triple::mips64)
188
0
        CmdArgs.push_back("elf32btsmip");
189
0
      else
190
0
        CmdArgs.push_back("elf32ltsmip");
191
2
    } else if (mips::hasMipsAbiArg(Args, "64")) {
192
0
      CmdArgs.push_back("-m");
193
0
      if (ToolChain.getArch() == llvm::Triple::mips64)
194
0
        CmdArgs.push_back("elf64btsmip");
195
0
      else
196
0
        CmdArgs.push_back("elf64ltsmip");
197
0
    }
198
2
    break;
199
4
  case llvm::Triple::ppc:
200
4
    CmdArgs.push_back("-m");
201
4
    CmdArgs.push_back("elf32ppc_nbsd");
202
4
    break;
203
2
204
4
  case llvm::Triple::ppc64:
205
4
  case llvm::Triple::ppc64le:
206
4
    CmdArgs.push_back("-m");
207
4
    CmdArgs.push_back("elf64ppc");
208
4
    break;
209
4
210
27
  case llvm::Triple::sparc:
211
27
    CmdArgs.push_back("-m");
212
27
    CmdArgs.push_back("elf32_sparc");
213
27
    break;
214
4
215
20
  case llvm::Triple::sparcv9:
216
20
    CmdArgs.push_back("-m");
217
20
    CmdArgs.push_back("elf64_sparc");
218
20
    break;
219
4
220
60
  default:
221
60
    break;
222
153
  }
223
153
224
153
  if (Output.isFilename()) {
225
153
    CmdArgs.push_back("-o");
226
153
    CmdArgs.push_back(Output.getFilename());
227
153
  } else {
228
0
    assert(Output.isNothing() && "Invalid output.");
229
0
  }
230
153
231
153
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
232
150
    if (!Args.hasArg(options::OPT_shared)) {
233
149
      CmdArgs.push_back(
234
149
          Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
235
149
    }
236
150
    CmdArgs.push_back(
237
150
        Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
238
150
    if (Args.hasArg(options::OPT_shared) || 
Args.hasArg(options::OPT_pie)149
) {
239
3
      CmdArgs.push_back(
240
3
          Args.MakeArgString(ToolChain.GetFilePath("crtbeginS.o")));
241
147
    } else {
242
147
      CmdArgs.push_back(
243
147
          Args.MakeArgString(ToolChain.GetFilePath("crtbegin.o")));
244
147
    }
245
150
  }
246
153
247
153
  Args.AddAllArgs(CmdArgs, options::OPT_L);
248
153
  Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
249
153
  Args.AddAllArgs(CmdArgs, options::OPT_e);
250
153
  Args.AddAllArgs(CmdArgs, options::OPT_s);
251
153
  Args.AddAllArgs(CmdArgs, options::OPT_t);
252
153
  Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
253
153
  Args.AddAllArgs(CmdArgs, options::OPT_r);
254
153
255
153
  bool NeedsSanitizerDeps = addSanitizerRuntimes(getToolChain(), Args, CmdArgs);
256
153
  bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
257
153
  AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
258
153
259
153
  const SanitizerArgs &SanArgs = ToolChain.getSanitizerArgs();
260
153
  if (SanArgs.needsSharedRt()) {
261
0
    CmdArgs.push_back("-rpath");
262
0
    CmdArgs.push_back(Args.MakeArgString(
263
0
        ToolChain.getCompilerRTPath().c_str()));
264
0
  }
265
153
266
153
  unsigned Major, Minor, Micro;
267
153
  ToolChain.getTriple().getOSVersion(Major, Minor, Micro);
268
153
  bool useLibgcc = true;
269
153
  if (Major >= 7 || 
Major == 0131
) {
270
139
    switch (ToolChain.getArch()) {
271
139
    case llvm::Triple::aarch64:
272
134
    case llvm::Triple::aarch64_be:
273
134
    case llvm::Triple::arm:
274
134
    case llvm::Triple::armeb:
275
134
    case llvm::Triple::thumb:
276
134
    case llvm::Triple::thumbeb:
277
134
    case llvm::Triple::ppc:
278
134
    case llvm::Triple::ppc64:
279
134
    case llvm::Triple::ppc64le:
280
134
    case llvm::Triple::sparc:
281
134
    case llvm::Triple::sparcv9:
282
134
    case llvm::Triple::x86:
283
134
    case llvm::Triple::x86_64:
284
134
      useLibgcc = false;
285
134
      break;
286
134
    default:
287
5
      break;
288
153
    }
289
153
  }
290
153
291
153
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
292
150
    addOpenMPRuntime(CmdArgs, getToolChain(), Args);
293
150
    if (D.CCCIsCXX()) {
294
34
      if (ToolChain.ShouldLinkCXXStdlib(Args))
295
34
        ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
296
34
      CmdArgs.push_back("-lm");
297
34
    }
298
150
    if (NeedsSanitizerDeps)
299
18
      linkSanitizerRuntimeDeps(getToolChain(), CmdArgs);
300
150
    if (NeedsXRayDeps)
301
0
      linkXRayRuntimeDeps(ToolChain, CmdArgs);
302
150
    if (Args.hasArg(options::OPT_pthread))
303
0
      CmdArgs.push_back("-lpthread");
304
150
    CmdArgs.push_back("-lc");
305
150
306
150
    if (useLibgcc) {
307
19
      if (Args.hasArg(options::OPT_static)) {
308
8
        // libgcc_eh depends on libc, so resolve as much as possible,
309
8
        // pull in any new requirements from libc and then get the rest
310
8
        // of libgcc.
311
8
        CmdArgs.push_back("-lgcc_eh");
312
8
        CmdArgs.push_back("-lc");
313
8
        CmdArgs.push_back("-lgcc");
314
11
      } else {
315
11
        CmdArgs.push_back("-lgcc");
316
11
        CmdArgs.push_back("--as-needed");
317
11
        CmdArgs.push_back("-lgcc_s");
318
11
        CmdArgs.push_back("--no-as-needed");
319
11
      }
320
19
    }
321
150
  }
322
153
323
153
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
324
150
    if (Args.hasArg(options::OPT_shared) || 
Args.hasArg(options::OPT_pie)149
)
325
3
      CmdArgs.push_back(
326
3
          Args.MakeArgString(ToolChain.GetFilePath("crtendS.o")));
327
147
    else
328
147
      CmdArgs.push_back(
329
147
          Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
330
150
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
331
150
  }
332
153
333
153
  ToolChain.addProfileRTLibs(Args, CmdArgs);
334
153
335
153
  const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
336
153
  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
337
153
}
338
339
/// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly.
340
341
NetBSD::NetBSD(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
342
188
    : Generic_ELF(D, Triple, Args) {
343
188
  if (!Args.hasArg(options::OPT_nostdlib)) {
344
185
    // When targeting a 32-bit platform, try the special directory used on
345
185
    // 64-bit hosts, and only fall back to the main library directory if that
346
185
    // doesn't work.
347
185
    // FIXME: It'd be nicer to test if this directory exists, but I'm not sure
348
185
    // what all logic is needed to emulate the '=' prefix here.
349
185
    switch (Triple.getArch()) {
350
185
    case llvm::Triple::x86:
351
10
      getFilePaths().push_back("=/usr/lib/i386");
352
10
      break;
353
185
    case llvm::Triple::arm:
354
39
    case llvm::Triple::armeb:
355
39
    case llvm::Triple::thumb:
356
39
    case llvm::Triple::thumbeb:
357
39
      switch (Triple.getEnvironment()) {
358
39
      case llvm::Triple::EABI:
359
23
      case llvm::Triple::GNUEABI:
360
23
        getFilePaths().push_back("=/usr/lib/eabi");
361
23
        break;
362
23
      case llvm::Triple::EABIHF:
363
6
      case llvm::Triple::GNUEABIHF:
364
6
        getFilePaths().push_back("=/usr/lib/eabihf");
365
6
        break;
366
10
      default:
367
10
        getFilePaths().push_back("=/usr/lib/oabi");
368
10
        break;
369
39
      }
370
39
      break;
371
39
    case llvm::Triple::mips64:
372
2
    case llvm::Triple::mips64el:
373
2
      if (tools::mips::hasMipsAbiArg(Args, "o32"))
374
0
        getFilePaths().push_back("=/usr/lib/o32");
375
2
      else if (tools::mips::hasMipsAbiArg(Args, "64"))
376
0
        getFilePaths().push_back("=/usr/lib/64");
377
2
      break;
378
5
    case llvm::Triple::ppc:
379
5
      getFilePaths().push_back("=/usr/lib/powerpc");
380
5
      break;
381
27
    case llvm::Triple::sparc:
382
27
      getFilePaths().push_back("=/usr/lib/sparc");
383
27
      break;
384
102
    default:
385
102
      break;
386
185
    }
387
185
388
185
    getFilePaths().push_back("=/usr/lib");
389
185
  }
390
188
}
391
392
65
Tool *NetBSD::buildAssembler() const {
393
65
  return new tools::netbsd::Assembler(*this);
394
65
}
395
396
153
Tool *NetBSD::buildLinker() const { return new tools::netbsd::Linker(*this); }
397
398
68
ToolChain::CXXStdlibType NetBSD::GetDefaultCXXStdlibType() const {
399
68
  unsigned Major, Minor, Micro;
400
68
  getTriple().getOSVersion(Major, Minor, Micro);
401
68
  if (Major >= 7 || 
Major == 044
) {
402
52
    switch (getArch()) {
403
52
    case llvm::Triple::aarch64:
404
52
    case llvm::Triple::aarch64_be:
405
52
    case llvm::Triple::arm:
406
52
    case llvm::Triple::armeb:
407
52
    case llvm::Triple::thumb:
408
52
    case llvm::Triple::thumbeb:
409
52
    case llvm::Triple::ppc:
410
52
    case llvm::Triple::ppc64:
411
52
    case llvm::Triple::ppc64le:
412
52
    case llvm::Triple::sparc:
413
52
    case llvm::Triple::sparcv9:
414
52
    case llvm::Triple::x86:
415
52
    case llvm::Triple::x86_64:
416
52
      return ToolChain::CST_Libcxx;
417
52
    default:
418
0
      break;
419
16
    }
420
16
  }
421
16
  return ToolChain::CST_Libstdcxx;
422
16
}
423
424
void NetBSD::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
425
26
                                   llvm::opt::ArgStringList &CC1Args) const {
426
26
  const std::string Candidates[] = {
427
26
    // directory relative to build tree
428
26
    getDriver().Dir + "/../include/c++/v1",
429
26
    // system install with full upstream path
430
26
    getDriver().SysRoot + "/usr/include/c++/v1",
431
26
    // system install from src
432
26
    getDriver().SysRoot + "/usr/include/c++",
433
26
  };
434
26
435
26
  for (const auto &IncludePath : Candidates) {
436
26
    if (!getVFS().exists(IncludePath + "/__config"))
437
0
      continue;
438
26
439
26
    // Use the first candidate that looks valid.
440
26
    addSystemInclude(DriverArgs, CC1Args, IncludePath);
441
26
    return;
442
26
  }
443
26
}
444
445
void NetBSD::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
446
8
                                      llvm::opt::ArgStringList &CC1Args) const {
447
8
  addLibStdCXXIncludePaths(getDriver().SysRoot, "/usr/include/g++", "", "", "",
448
8
                           "", DriverArgs, CC1Args);
449
8
}
450
451
188
llvm::ExceptionHandling NetBSD::GetExceptionModel(const ArgList &Args) const {
452
188
  // NetBSD uses Dwarf exceptions on ARM.
453
188
  llvm::Triple::ArchType TArch = getTriple().getArch();
454
188
  if (TArch == llvm::Triple::arm || 
TArch == llvm::Triple::armeb161
||
455
188
      
TArch == llvm::Triple::thumb152
||
TArch == llvm::Triple::thumbeb150
)
456
39
    return llvm::ExceptionHandling::DwarfCFI;
457
149
  return llvm::ExceptionHandling::None;
458
149
}
459
460
188
SanitizerMask NetBSD::getSupportedSanitizers() const {
461
188
  const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
462
188
  const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
463
188
  SanitizerMask Res = ToolChain::getSupportedSanitizers();
464
188
  if (IsX86 || 
IsX86_64178
) {
465
58
    Res |= SanitizerKind::Address;
466
58
    Res |= SanitizerKind::PointerCompare;
467
58
    Res |= SanitizerKind::PointerSubtract;
468
58
    Res |= SanitizerKind::Function;
469
58
    Res |= SanitizerKind::Leak;
470
58
    Res |= SanitizerKind::SafeStack;
471
58
    Res |= SanitizerKind::Scudo;
472
58
    Res |= SanitizerKind::Vptr;
473
58
  }
474
188
  if (IsX86_64) {
475
48
    Res |= SanitizerKind::DataFlow;
476
48
    Res |= SanitizerKind::Fuzzer;
477
48
    Res |= SanitizerKind::FuzzerNoLink;
478
48
    Res |= SanitizerKind::HWAddress;
479
48
    Res |= SanitizerKind::KernelAddress;
480
48
    Res |= SanitizerKind::KernelHWAddress;
481
48
    Res |= SanitizerKind::KernelMemory;
482
48
    Res |= SanitizerKind::Memory;
483
48
    Res |= SanitizerKind::Thread;
484
48
  }
485
188
  return Res;
486
188
}
487
488
void NetBSD::addClangTargetOptions(const ArgList &,
489
                                   ArgStringList &CC1Args,
490
188
                                   Action::OffloadKind) const {
491
188
  const SanitizerArgs &SanArgs = getSanitizerArgs();
492
188
  if (SanArgs.hasAnySanitizer())
493
24
    CC1Args.push_back("-D_REENTRANT");
494
188
}