Coverage Report

Created: 2020-02-25 14:32

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