Coverage Report

Created: 2020-09-19 12:23

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Driver/ToolChains/FreeBSD.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- FreeBSD.cpp - FreeBSD 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 "FreeBSD.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/DriverDiagnostic.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 freebsd::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
28
                                      const InputInfo &Output,
29
                                      const InputInfoList &Inputs,
30
                                      const ArgList &Args,
31
24
                                      const char *LinkingOutput) const {
32
24
  claimNoWarnArgs(Args);
33
24
  ArgStringList CmdArgs;
34
24
  const auto &D = getToolChain().getDriver();
35
24
36
  // When building 32-bit code on FreeBSD/amd64, we have to explicitly
37
  // instruct as in the base system to assemble 32-bit code.
38
24
  switch (getToolChain().getArch()) {
39
2
  default:
40
2
    break;
41
0
  case llvm::Triple::x86:
42
0
    CmdArgs.push_back("--32");
43
0
    break;
44
0
  case llvm::Triple::ppc:
45
0
    CmdArgs.push_back("-a32");
46
0
    break;
47
17
  case llvm::Triple::mips:
48
17
  case llvm::Triple::mipsel:
49
17
  case llvm::Triple::mips64:
50
17
  case llvm::Triple::mips64el: {
51
17
    StringRef CPUName;
52
17
    StringRef ABIName;
53
17
    mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName);
54
17
55
17
    CmdArgs.push_back("-march");
56
17
    CmdArgs.push_back(CPUName.data());
57
17
58
17
    CmdArgs.push_back("-mabi");
59
17
    CmdArgs.push_back(mips::getGnuCompatibleMipsABIName(ABIName).data());
60
17
61
17
    if (getToolChain().getTriple().isLittleEndian())
62
4
      CmdArgs.push_back("-EL");
63
13
    else
64
13
      CmdArgs.push_back("-EB");
65
17
66
17
    if (Arg *A = Args.getLastArg(options::OPT_G)) {
67
1
      StringRef v = A->getValue();
68
1
      CmdArgs.push_back(Args.MakeArgString("-G" + v));
69
1
      A->claim();
70
1
    }
71
17
72
17
    AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
73
17
    break;
74
17
  }
75
3
  case llvm::Triple::arm:
76
3
  case llvm::Triple::armeb:
77
3
  case llvm::Triple::thumb:
78
3
  case llvm::Triple::thumbeb: {
79
3
    arm::FloatABI ABI = arm::getARMFloatABI(getToolChain(), Args);
80
3
81
3
    if (ABI == arm::FloatABI::Hard)
82
1
      CmdArgs.push_back("-mfpu=vfp");
83
2
    else
84
2
      CmdArgs.push_back("-mfpu=softvfp");
85
3
86
3
    switch (getToolChain().getTriple().getEnvironment()) {
87
2
    case llvm::Triple::GNUEABIHF:
88
2
    case llvm::Triple::GNUEABI:
89
2
    case llvm::Triple::EABI:
90
2
      CmdArgs.push_back("-meabi=5");
91
2
      break;
92
2
93
1
    default:
94
1
      CmdArgs.push_back("-matpcs");
95
3
    }
96
3
    break;
97
3
  }
98
2
  case llvm::Triple::sparc:
99
2
  case llvm::Triple::sparcel:
100
2
  case llvm::Triple::sparcv9: {
101
2
    std::string CPU = getCPUName(Args, getToolChain().getTriple());
102
2
    CmdArgs.push_back(
103
2
        sparc::getSparcAsmModeForCPU(CPU, getToolChain().getTriple()));
104
2
    AddAssemblerKPIC(getToolChain(), Args, CmdArgs);
105
2
    break;
106
24
  }
107
24
  }
108
24
109
24
  for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ,
110
0
                                    options::OPT_fdebug_prefix_map_EQ)) {
111
0
    StringRef Map = A->getValue();
112
0
    if (Map.find('=') == StringRef::npos)
113
0
      D.Diag(diag::err_drv_invalid_argument_to_option)
114
0
          << Map << A->getOption().getName();
115
0
    else {
116
0
      CmdArgs.push_back(Args.MakeArgString("--debug-prefix-map"));
117
0
      CmdArgs.push_back(Args.MakeArgString(Map));
118
0
    }
119
0
    A->claim();
120
0
  }
121
24
122
24
  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
123
24
124
24
  CmdArgs.push_back("-o");
125
24
  CmdArgs.push_back(Output.getFilename());
126
24
127
24
  for (const auto &II : Inputs)
128
24
    CmdArgs.push_back(II.getFilename());
129
24
130
24
  const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
131
24
  C.addCommand(std::make_unique<Command>(
132
24
      JA, *this, ResponseFileSupport::AtFileCurCP(), Exec, CmdArgs, Inputs));
133
24
}
134
135
void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
136
                                   const InputInfo &Output,
137
                                   const InputInfoList &Inputs,
138
                                   const ArgList &Args,
139
81
                                   const char *LinkingOutput) const {
140
81
  const toolchains::FreeBSD &ToolChain =
141
81
      static_cast<const toolchains::FreeBSD &>(getToolChain());
142
81
  const Driver &D = ToolChain.getDriver();
143
81
  const llvm::Triple::ArchType Arch = ToolChain.getArch();
144
81
  const bool IsPIE =
145
81
      !Args.hasArg(options::OPT_shared) &&
146
79
      (Args.hasArg(options::OPT_pie) || 
ToolChain.isPIEDefault()77
);
147
81
  ArgStringList CmdArgs;
148
81
149
  // Silence warning for "clang -g foo.o -o foo"
150
81
  Args.ClaimAllArgs(options::OPT_g_Group);
151
  // and "clang -emit-llvm foo.o -o foo"
152
81
  Args.ClaimAllArgs(options::OPT_emit_llvm);
153
  // and for "clang -w foo.o -o foo". Other warning options are already
154
  // handled somewhere else.
155
81
  Args.ClaimAllArgs(options::OPT_w);
156
81
157
81
  if (!D.SysRoot.empty())
158
32
    CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
159
81
160
81
  if (IsPIE)
161
3
    CmdArgs.push_back("-pie");
162
81
163
81
  CmdArgs.push_back("--eh-frame-hdr");
164
81
  if (Args.hasArg(options::OPT_static)) {
165
2
    CmdArgs.push_back("-Bstatic");
166
79
  } else {
167
79
    if (Args.hasArg(options::OPT_rdynamic))
168
0
      CmdArgs.push_back("-export-dynamic");
169
79
    if (Args.hasArg(options::OPT_shared)) {
170
2
      CmdArgs.push_back("-Bshareable");
171
77
    } else {
172
77
      CmdArgs.push_back("-dynamic-linker");
173
77
      CmdArgs.push_back("/libexec/ld-elf.so.1");
174
77
    }
175
79
    const llvm::Triple &T = ToolChain.getTriple();
176
79
    if (T.getOSMajorVersion() >= 9) {
177
20
      if (Arch == llvm::Triple::arm || 
Arch == llvm::Triple::sparc17
||
T.isX86()17
)
178
9
        CmdArgs.push_back("--hash-style=both");
179
20
    }
180
79
    CmdArgs.push_back("--enable-new-dtags");
181
79
  }
182
81
183
  // Explicitly set the linker emulation for platforms that might not
184
  // be the default emulation for the linker.
185
81
  switch (Arch) {
186
7
  case llvm::Triple::x86:
187
7
    CmdArgs.push_back("-m");
188
7
    CmdArgs.push_back("elf_i386_fbsd");
189
7
    break;
190
1
  case llvm::Triple::ppc:
191
1
    CmdArgs.push_back("-m");
192
1
    CmdArgs.push_back("elf32ppc_fbsd");
193
1
    break;
194
4
  case llvm::Triple::mips:
195
4
    CmdArgs.push_back("-m");
196
4
    CmdArgs.push_back("elf32btsmip_fbsd");
197
4
    break;
198
2
  case llvm::Triple::mipsel:
199
2
    CmdArgs.push_back("-m");
200
2
    CmdArgs.push_back("elf32ltsmip_fbsd");
201
2
    break;
202
4
  case llvm::Triple::mips64:
203
4
    CmdArgs.push_back("-m");
204
4
    if (tools::mips::hasMipsAbiArg(Args, "n32"))
205
1
      CmdArgs.push_back("elf32btsmipn32_fbsd");
206
3
    else
207
3
      CmdArgs.push_back("elf64btsmip_fbsd");
208
4
    break;
209
3
  case llvm::Triple::mips64el:
210
3
    CmdArgs.push_back("-m");
211
3
    if (tools::mips::hasMipsAbiArg(Args, "n32"))
212
1
      CmdArgs.push_back("elf32ltsmipn32_fbsd");
213
2
    else
214
2
      CmdArgs.push_back("elf64ltsmip_fbsd");
215
3
    break;
216
1
  case llvm::Triple::riscv32:
217
1
    CmdArgs.push_back("-m");
218
1
    CmdArgs.push_back("elf32lriscv");
219
1
    break;
220
1
  case llvm::Triple::riscv64:
221
1
    CmdArgs.push_back("-m");
222
1
    CmdArgs.push_back("elf64lriscv");
223
1
    break;
224
58
  default:
225
58
    break;
226
81
  }
227
81
228
81
  if (Arg *A = Args.getLastArg(options::OPT_G)) {
229
1
    if (ToolChain.getTriple().isMIPS()) {
230
1
      StringRef v = A->getValue();
231
1
      CmdArgs.push_back(Args.MakeArgString("-G" + v));
232
1
      A->claim();
233
1
    }
234
1
  }
235
81
236
81
  if (Output.isFilename()) {
237
81
    CmdArgs.push_back("-o");
238
81
    CmdArgs.push_back(Output.getFilename());
239
0
  } else {
240
0
    assert(Output.isNothing() && "Invalid output.");
241
0
  }
242
81
243
81
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
244
78
    const char *crt1 = nullptr;
245
78
    if (!Args.hasArg(options::OPT_shared)) {
246
76
      if (Args.hasArg(options::OPT_pg))
247
2
        crt1 = "gcrt1.o";
248
74
      else if (IsPIE)
249
3
        crt1 = "Scrt1.o";
250
71
      else
251
71
        crt1 = "crt1.o";
252
76
    }
253
78
    if (crt1)
254
76
      CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
255
78
256
78
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
257
78
258
78
    const char *crtbegin = nullptr;
259
78
    if (Args.hasArg(options::OPT_static))
260
2
      crtbegin = "crtbeginT.o";
261
76
    else if (Args.hasArg(options::OPT_shared) || 
IsPIE74
)
262
5
      crtbegin = "crtbeginS.o";
263
71
    else
264
71
      crtbegin = "crtbegin.o";
265
78
266
78
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
267
78
  }
268
81
269
81
  Args.AddAllArgs(CmdArgs, options::OPT_L);
270
81
  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
271
81
  Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
272
81
  Args.AddAllArgs(CmdArgs, options::OPT_e);
273
81
  Args.AddAllArgs(CmdArgs, options::OPT_s);
274
81
  Args.AddAllArgs(CmdArgs, options::OPT_t);
275
81
  Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
276
81
  Args.AddAllArgs(CmdArgs, options::OPT_r);
277
81
278
81
  if (D.isUsingLTO()) {
279
0
    assert(!Inputs.empty() && "Must have at least one input.");
280
0
    addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs[0],
281
0
                  D.getLTOMode() == LTOK_Thin);
282
0
  }
283
81
284
81
  bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
285
81
  bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
286
81
  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
287
81
288
81
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
289
    // Use the static OpenMP runtime with -static-openmp
290
78
    bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) &&
291
4
                        !Args.hasArg(options::OPT_static);
292
78
    addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP);
293
78
294
78
    if (D.CCCIsCXX()) {
295
4
      if (ToolChain.ShouldLinkCXXStdlib(Args))
296
4
        ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
297
4
      if (Args.hasArg(options::OPT_pg))
298
2
        CmdArgs.push_back("-lm_p");
299
2
      else
300
2
        CmdArgs.push_back("-lm");
301
4
    }
302
78
    if (NeedsSanitizerDeps)
303
4
      linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
304
78
    if (NeedsXRayDeps)
305
0
      linkXRayRuntimeDeps(ToolChain, CmdArgs);
306
    // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
307
    // the default system libraries. Just mimic this for now.
308
78
    if (Args.hasArg(options::OPT_pg))
309
2
      CmdArgs.push_back("-lgcc_p");
310
76
    else
311
76
      CmdArgs.push_back("-lgcc");
312
78
    if (Args.hasArg(options::OPT_static)) {
313
2
      CmdArgs.push_back("-lgcc_eh");
314
76
    } else if (Args.hasArg(options::OPT_pg)) {
315
2
      CmdArgs.push_back("-lgcc_eh_p");
316
74
    } else {
317
74
      CmdArgs.push_back("--as-needed");
318
74
      CmdArgs.push_back("-lgcc_s");
319
74
      CmdArgs.push_back("--no-as-needed");
320
74
    }
321
78
322
78
    if (Args.hasArg(options::OPT_pthread)) {
323
0
      if (Args.hasArg(options::OPT_pg))
324
0
        CmdArgs.push_back("-lpthread_p");
325
0
      else
326
0
        CmdArgs.push_back("-lpthread");
327
0
    }
328
78
329
78
    if (Args.hasArg(options::OPT_pg)) {
330
2
      if (Args.hasArg(options::OPT_shared))
331
0
        CmdArgs.push_back("-lc");
332
2
      else
333
2
        CmdArgs.push_back("-lc_p");
334
2
      CmdArgs.push_back("-lgcc_p");
335
76
    } else {
336
76
      CmdArgs.push_back("-lc");
337
76
      CmdArgs.push_back("-lgcc");
338
76
    }
339
78
340
78
    if (Args.hasArg(options::OPT_static)) {
341
2
      CmdArgs.push_back("-lgcc_eh");
342
76
    } else if (Args.hasArg(options::OPT_pg)) {
343
2
      CmdArgs.push_back("-lgcc_eh_p");
344
74
    } else {
345
74
      CmdArgs.push_back("--as-needed");
346
74
      CmdArgs.push_back("-lgcc_s");
347
74
      CmdArgs.push_back("--no-as-needed");
348
74
    }
349
78
  }
350
81
351
81
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
352
78
    if (Args.hasArg(options::OPT_shared) || 
IsPIE76
)
353
5
      CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o")));
354
73
    else
355
73
      CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
356
78
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
357
78
  }
358
81
359
81
  ToolChain.addProfileRTLibs(Args, CmdArgs);
360
81
361
81
  const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
362
81
  C.addCommand(std::make_unique<Command>(
363
81
      JA, *this, ResponseFileSupport::AtFileCurCP(), Exec, CmdArgs, Inputs));
364
81
}
365
366
/// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly.
367
368
FreeBSD::FreeBSD(const Driver &D, const llvm::Triple &Triple,
369
                 const ArgList &Args)
370
157
    : Generic_ELF(D, Triple, Args) {
371
157
372
  // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall
373
  // back to '/usr/lib' if it doesn't exist.
374
157
  if ((Triple.getArch() == llvm::Triple::x86 || 
Triple.isMIPS32()144
||
375
123
       Triple.getArch() == llvm::Triple::ppc) &&
376
36
      D.getVFS().exists(getDriver().SysRoot + "/usr/lib32/crt1.o"))
377
6
    getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32");
378
151
  else
379
151
    getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
380
157
}
381
382
106
ToolChain::CXXStdlibType FreeBSD::GetDefaultCXXStdlibType() const {
383
106
  if (getTriple().getOSMajorVersion() >= 10)
384
4
    return ToolChain::CST_Libcxx;
385
102
  return ToolChain::CST_Libstdcxx;
386
102
}
387
388
16
unsigned FreeBSD::GetDefaultDwarfVersion() const {
389
16
  if (getTriple().getOSMajorVersion() < 12)
390
14
    return 2;
391
2
  return 4;
392
2
}
393
394
void FreeBSD::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
395
2
                                    llvm::opt::ArgStringList &CC1Args) const {
396
2
  addSystemInclude(DriverArgs, CC1Args,
397
2
                   getDriver().SysRoot + "/usr/include/c++/v1");
398
2
}
399
400
void FreeBSD::addLibStdCxxIncludePaths(
401
    const llvm::opt::ArgList &DriverArgs,
402
100
    llvm::opt::ArgStringList &CC1Args) const {
403
100
  addLibStdCXXIncludePaths(getDriver().SysRoot, "/usr/include/c++/4.2", "", "",
404
100
                           "", "", DriverArgs, CC1Args);
405
100
}
406
407
void FreeBSD::AddCXXStdlibLibArgs(const ArgList &Args,
408
4
                                  ArgStringList &CmdArgs) const {
409
4
  CXXStdlibType Type = GetCXXStdlibType(Args);
410
4
  bool Profiling = Args.hasArg(options::OPT_pg);
411
4
412
4
  switch (Type) {
413
2
  case ToolChain::CST_Libcxx:
414
1
    CmdArgs.push_back(Profiling ? "-lc++_p" : "-lc++");
415
2
    break;
416
0
417
2
  case ToolChain::CST_Libstdcxx:
418
1
    CmdArgs.push_back(Profiling ? "-lstdc++_p" : "-lstdc++");
419
2
    break;
420
4
  }
421
4
}
422
423
void FreeBSD::AddCudaIncludeArgs(const ArgList &DriverArgs,
424
17
                                 ArgStringList &CC1Args) const {
425
17
  CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
426
17
}
427
428
void FreeBSD::AddHIPIncludeArgs(const ArgList &DriverArgs,
429
0
                                ArgStringList &CC1Args) const {
430
0
  RocmInstallation.AddHIPIncludeArgs(DriverArgs, CC1Args);
431
0
}
432
433
24
Tool *FreeBSD::buildAssembler() const {
434
24
  return new tools::freebsd::Assembler(*this);
435
24
}
436
437
81
Tool *FreeBSD::buildLinker() const { return new tools::freebsd::Linker(*this); }
438
439
155
llvm::ExceptionHandling FreeBSD::GetExceptionModel(const ArgList &Args) const {
440
  // FreeBSD uses SjLj exceptions on ARM oabi.
441
155
  switch (getTriple().getEnvironment()) {
442
6
  case llvm::Triple::GNUEABIHF:
443
6
  case llvm::Triple::GNUEABI:
444
6
  case llvm::Triple::EABI:
445
6
    return llvm::ExceptionHandling::None;
446
149
  default:
447
149
    if (getTriple().getArch() == llvm::Triple::arm ||
448
147
        getTriple().getArch() == llvm::Triple::thumb)
449
2
      return llvm::ExceptionHandling::SjLj;
450
147
    return llvm::ExceptionHandling::None;
451
155
  }
452
155
}
453
454
89
bool FreeBSD::HasNativeLLVMSupport() const { return true; }
455
456
155
bool FreeBSD::IsUnwindTablesDefault(const ArgList &Args) const { return true; }
457
458
295
bool FreeBSD::isPIEDefault() const { return getSanitizerArgs().requiresPIE(); }
459
460
167
SanitizerMask FreeBSD::getSupportedSanitizers() const {
461
167
  const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
462
167
  const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
463
167
  const bool IsMIPS64 = getTriple().isMIPS64();
464
167
  SanitizerMask Res = ToolChain::getSupportedSanitizers();
465
167
  Res |= SanitizerKind::Address;
466
167
  Res |= SanitizerKind::PointerCompare;
467
167
  Res |= SanitizerKind::PointerSubtract;
468
167
  Res |= SanitizerKind::Vptr;
469
167
  if (IsX86_64 || 
IsMIPS6476
) {
470
106
    Res |= SanitizerKind::Leak;
471
106
    Res |= SanitizerKind::Thread;
472
106
  }
473
167
  if (IsX86 || 
IsX86_64155
) {
474
103
    Res |= SanitizerKind::Function;
475
103
    Res |= SanitizerKind::SafeStack;
476
103
    Res |= SanitizerKind::Fuzzer;
477
103
    Res |= SanitizerKind::FuzzerNoLink;
478
103
  }
479
167
  if (IsX86_64)
480
91
    Res |= SanitizerKind::Memory;
481
167
  return Res;
482
167
}
483
484
void FreeBSD::addClangTargetOptions(const ArgList &DriverArgs,
485
                                    ArgStringList &CC1Args,
486
189
                                    Action::OffloadKind) const {
487
189
  if (!DriverArgs.hasFlag(options::OPT_fuse_init_array,
488
189
                          options::OPT_fno_use_init_array,
489
189
                          getTriple().getOSMajorVersion() >= 12))
490
177
    CC1Args.push_back("-fno-use-init-array");
491
189
}