Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Driver/ToolChains/OpenBSD.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- OpenBSD.cpp - OpenBSD 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 "OpenBSD.h"
10
#include "Arch/ARM.h"
11
#include "Arch/Mips.h"
12
#include "Arch/Sparc.h"
13
#include "CommonArgs.h"
14
#include "clang/Config/config.h"
15
#include "clang/Driver/Compilation.h"
16
#include "clang/Driver/Options.h"
17
#include "clang/Driver/SanitizerArgs.h"
18
#include "llvm/Option/ArgList.h"
19
#include "llvm/Support/Path.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 openbsd::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
28
                                      const InputInfo &Output,
29
                                      const InputInfoList &Inputs,
30
                                      const ArgList &Args,
31
8
                                      const char *LinkingOutput) const {
32
8
  const toolchains::OpenBSD &ToolChain =
33
8
      static_cast<const toolchains::OpenBSD &>(getToolChain());
34
8
  const Driver &D = ToolChain.getDriver();
35
8
  const llvm::Triple &Triple = ToolChain.getTriple();
36
37
8
  claimNoWarnArgs(Args);
38
8
  ArgStringList CmdArgs;
39
40
8
  switch (ToolChain.getArch()) {
41
1
  case llvm::Triple::x86:
42
    // When building 32-bit code on OpenBSD/amd64, we have to explicitly
43
    // instruct as in the base system to assemble 32-bit code.
44
1
    CmdArgs.push_back("--32");
45
1
    break;
46
47
1
  case llvm::Triple::arm:
48
1
  case llvm::Triple::armeb: {
49
1
    StringRef MArch, MCPU;
50
1
    arm::getARMArchCPUFromArgs(Args, MArch, MCPU, /*FromAs*/ true);
51
1
    std::string Arch = arm::getARMTargetCPU(MCPU, MArch, Triple);
52
1
    CmdArgs.push_back(Args.MakeArgString("-mcpu=" + Arch));
53
1
    break;
54
1
  }
55
56
1
  case llvm::Triple::ppc:
57
1
    CmdArgs.push_back("-mppc");
58
1
    CmdArgs.push_back("-many");
59
1
    break;
60
61
1
  case llvm::Triple::sparcv9: {
62
1
    CmdArgs.push_back("-64");
63
1
    std::string CPU = getCPUName(D, Args, Triple);
64
1
    CmdArgs.push_back(sparc::getSparcAsmModeForCPU(CPU, Triple));
65
1
    AddAssemblerKPIC(ToolChain, Args, CmdArgs);
66
1
    break;
67
1
  }
68
69
2
  case llvm::Triple::mips64:
70
4
  case llvm::Triple::mips64el: {
71
4
    StringRef CPUName;
72
4
    StringRef ABIName;
73
4
    mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
74
75
4
    CmdArgs.push_back("-march");
76
4
    CmdArgs.push_back(CPUName.data());
77
78
4
    CmdArgs.push_back("-mabi");
79
4
    CmdArgs.push_back(mips::getGnuCompatibleMipsABIName(ABIName).data());
80
81
4
    if (Triple.isLittleEndian())
82
2
      CmdArgs.push_back("-EL");
83
2
    else
84
2
      CmdArgs.push_back("-EB");
85
86
4
    AddAssemblerKPIC(ToolChain, Args, CmdArgs);
87
4
    break;
88
2
  }
89
90
0
  default:
91
0
    break;
92
8
  }
93
94
8
  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
95
96
8
  CmdArgs.push_back("-o");
97
8
  CmdArgs.push_back(Output.getFilename());
98
99
8
  for (const auto &II : Inputs)
100
8
    CmdArgs.push_back(II.getFilename());
101
102
8
  const char *Exec = Args.MakeArgString(ToolChain.GetProgramPath("as"));
103
8
  C.addCommand(std::make_unique<Command>(JA, *this,
104
8
                                         ResponseFileSupport::AtFileCurCP(),
105
8
                                         Exec, CmdArgs, Inputs, Output));
106
8
}
107
108
void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
109
                                   const InputInfo &Output,
110
                                   const InputInfoList &Inputs,
111
                                   const ArgList &Args,
112
51
                                   const char *LinkingOutput) const {
113
51
  const toolchains::OpenBSD &ToolChain =
114
51
      static_cast<const toolchains::OpenBSD &>(getToolChain());
115
51
  const Driver &D = ToolChain.getDriver();
116
51
  ArgStringList CmdArgs;
117
118
  // Silence warning for "clang -g foo.o -o foo"
119
51
  Args.ClaimAllArgs(options::OPT_g_Group);
120
  // and "clang -emit-llvm foo.o -o foo"
121
51
  Args.ClaimAllArgs(options::OPT_emit_llvm);
122
  // and for "clang -w foo.o -o foo". Other warning options are already
123
  // handled somewhere else.
124
51
  Args.ClaimAllArgs(options::OPT_w);
125
126
51
  if (ToolChain.getArch() == llvm::Triple::mips64)
127
1
    CmdArgs.push_back("-EB");
128
50
  else if (ToolChain.getArch() == llvm::Triple::mips64el)
129
1
    CmdArgs.push_back("-EL");
130
131
51
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_shared)) {
132
48
    CmdArgs.push_back("-e");
133
48
    CmdArgs.push_back("__start");
134
48
  }
135
136
51
  CmdArgs.push_back("--eh-frame-hdr");
137
51
  if (Args.hasArg(options::OPT_static)) {
138
6
    CmdArgs.push_back("-Bstatic");
139
45
  } else {
140
45
    if (Args.hasArg(options::OPT_rdynamic))
141
0
      CmdArgs.push_back("-export-dynamic");
142
45
    CmdArgs.push_back("-Bdynamic");
143
45
    if (Args.hasArg(options::OPT_shared)) {
144
0
      CmdArgs.push_back("-shared");
145
45
    } else {
146
45
      CmdArgs.push_back("-dynamic-linker");
147
45
      CmdArgs.push_back("/usr/libexec/ld.so");
148
45
    }
149
45
  }
150
151
51
  if (Args.hasArg(options::OPT_pie))
152
1
    CmdArgs.push_back("-pie");
153
51
  if (Args.hasArg(options::OPT_nopie) || 
Args.hasArg(options::OPT_pg)45
)
154
11
    CmdArgs.push_back("-nopie");
155
156
51
  if (Output.isFilename()) {
157
51
    CmdArgs.push_back("-o");
158
51
    CmdArgs.push_back(Output.getFilename());
159
51
  } else {
160
0
    assert(Output.isNothing() && "Invalid output.");
161
0
  }
162
163
51
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
164
48
    const char *crt0 = nullptr;
165
48
    const char *crtbegin = nullptr;
166
48
    if (!Args.hasArg(options::OPT_shared)) {
167
48
      if (Args.hasArg(options::OPT_pg))
168
5
        crt0 = "gcrt0.o";
169
43
      else if (Args.hasArg(options::OPT_static) &&
170
43
               
!Args.hasArg(options::OPT_nopie)6
)
171
4
        crt0 = "rcrt0.o";
172
39
      else
173
39
        crt0 = "crt0.o";
174
48
      crtbegin = "crtbegin.o";
175
48
    } else {
176
0
      crtbegin = "crtbeginS.o";
177
0
    }
178
179
48
    if (crt0)
180
48
      CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt0)));
181
48
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
182
48
  }
183
184
51
  Args.AddAllArgs(CmdArgs, options::OPT_L);
185
51
  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
186
51
  Args.AddAllArgs(CmdArgs, {options::OPT_T_Group, options::OPT_e,
187
51
                            options::OPT_s, options::OPT_t,
188
51
                            options::OPT_Z_Flag, options::OPT_r});
189
190
51
  bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
191
51
  bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
192
51
  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
193
194
51
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
195
    // Use the static OpenMP runtime with -static-openmp
196
48
    bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) &&
197
48
                        
!Args.hasArg(options::OPT_static)4
;
198
48
    addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP);
199
200
48
    if (D.CCCIsCXX()) {
201
8
      if (ToolChain.ShouldLinkCXXStdlib(Args))
202
8
        ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
203
8
      if (Args.hasArg(options::OPT_pg))
204
4
        CmdArgs.push_back("-lm_p");
205
4
      else
206
4
        CmdArgs.push_back("-lm");
207
8
    }
208
48
    if (NeedsSanitizerDeps) {
209
0
      CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins"));
210
0
      linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
211
0
    }
212
48
    if (NeedsXRayDeps) {
213
0
      CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins"));
214
0
      linkXRayRuntimeDeps(ToolChain, CmdArgs);
215
0
    }
216
    // FIXME: For some reason GCC passes -lgcc before adding
217
    // the default system libraries. Just mimic this for now.
218
48
    CmdArgs.push_back("-lcompiler_rt");
219
220
48
    if (Args.hasArg(options::OPT_pthread)) {
221
1
      if (!Args.hasArg(options::OPT_shared) && Args.hasArg(options::OPT_pg))
222
1
        CmdArgs.push_back("-lpthread_p");
223
0
      else
224
0
        CmdArgs.push_back("-lpthread");
225
1
    }
226
227
48
    if (!Args.hasArg(options::OPT_shared)) {
228
48
      if (Args.hasArg(options::OPT_pg))
229
5
        CmdArgs.push_back("-lc_p");
230
43
      else
231
43
        CmdArgs.push_back("-lc");
232
48
    }
233
234
48
    CmdArgs.push_back("-lcompiler_rt");
235
48
  }
236
237
51
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
238
48
    const char *crtend = nullptr;
239
48
    if (!Args.hasArg(options::OPT_shared))
240
48
      crtend = "crtend.o";
241
0
    else
242
0
      crtend = "crtendS.o";
243
244
48
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
245
48
  }
246
247
51
  ToolChain.addProfileRTLibs(Args, CmdArgs);
248
249
51
  const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
250
51
  C.addCommand(std::make_unique<Command>(JA, *this,
251
51
                                         ResponseFileSupport::AtFileCurCP(),
252
51
                                         Exec, CmdArgs, Inputs, Output));
253
51
}
254
255
214
SanitizerMask OpenBSD::getSupportedSanitizers() const {
256
214
  const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
257
214
  const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
258
259
  // For future use, only UBsan at the moment
260
214
  SanitizerMask Res = ToolChain::getSupportedSanitizers();
261
262
214
  if (IsX86 || 
IsX86_64117
) {
263
142
    Res |= SanitizerKind::Vptr;
264
142
    Res |= SanitizerKind::Fuzzer;
265
142
    Res |= SanitizerKind::FuzzerNoLink;
266
142
  }
267
268
214
  return Res;
269
214
}
270
271
/// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly.
272
273
OpenBSD::OpenBSD(const Driver &D, const llvm::Triple &Triple,
274
                 const ArgList &Args)
275
112
    : Generic_ELF(D, Triple, Args) {
276
112
  getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
277
112
}
278
279
void OpenBSD::AddClangSystemIncludeArgs(
280
    const llvm::opt::ArgList &DriverArgs,
281
112
    llvm::opt::ArgStringList &CC1Args) const {
282
112
  const Driver &D = getDriver();
283
284
112
  if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
285
0
    return;
286
287
112
  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
288
112
    SmallString<128> Dir(D.ResourceDir);
289
112
    llvm::sys::path::append(Dir, "include");
290
112
    addSystemInclude(DriverArgs, CC1Args, Dir.str());
291
112
  }
292
293
112
  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
294
0
    return;
295
296
  // Check for configure-time C include directories.
297
112
  StringRef CIncludeDirs(C_INCLUDE_DIRS);
298
112
  if (CIncludeDirs != "") {
299
0
    SmallVector<StringRef, 5> dirs;
300
0
    CIncludeDirs.split(dirs, ":");
301
0
    for (StringRef dir : dirs) {
302
0
      StringRef Prefix =
303
0
          llvm::sys::path::is_absolute(dir) ? StringRef(D.SysRoot) : "";
304
0
      addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
305
0
    }
306
0
    return;
307
0
  }
308
309
112
  addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include");
310
112
}
311
312
void OpenBSD::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
313
20
                                    llvm::opt::ArgStringList &CC1Args) const {
314
20
  addSystemInclude(DriverArgs, CC1Args,
315
20
                   getDriver().SysRoot + "/usr/include/c++/v1");
316
20
}
317
318
void OpenBSD::AddCXXStdlibLibArgs(const ArgList &Args,
319
8
                                  ArgStringList &CmdArgs) const {
320
8
  bool Profiling = Args.hasArg(options::OPT_pg);
321
322
8
  CmdArgs.push_back(Profiling ? 
"-lc++_p"4
:
"-lc++"4
);
323
8
  CmdArgs.push_back(Profiling ? 
"-lc++abi_p"4
:
"-lc++abi"4
);
324
8
  CmdArgs.push_back(Profiling ? 
"-lpthread_p"4
:
"-lpthread"4
);
325
8
}
326
327
std::string OpenBSD::getCompilerRT(const ArgList &Args,
328
                                   StringRef Component,
329
0
                                   FileType Type) const {
330
0
  SmallString<128> Path(getDriver().SysRoot);
331
0
  llvm::sys::path::append(Path, "/usr/lib/libcompiler_rt.a");
332
0
  return std::string(Path.str());
333
0
}
334
335
8
Tool *OpenBSD::buildAssembler() const {
336
8
  return new tools::openbsd::Assembler(*this);
337
8
}
338
339
51
Tool *OpenBSD::buildLinker() const { return new tools::openbsd::Linker(*this); }
340
341
52
bool OpenBSD::HasNativeLLVMSupport() const { return true; }