Coverage Report

Created: 2021-01-16 07:00

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