Coverage Report

Created: 2019-07-24 05:18

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