Coverage Report

Created: 2021-09-21 08:58

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