Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Driver/ToolChains/CloudABI.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- CloudABI.cpp - CloudABI 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 "CloudABI.h"
10
#include "InputInfo.h"
11
#include "CommonArgs.h"
12
#include "clang/Driver/Compilation.h"
13
#include "clang/Driver/Driver.h"
14
#include "clang/Driver/Options.h"
15
#include "llvm/ADT/SmallString.h"
16
#include "llvm/Option/ArgList.h"
17
#include "llvm/Support/Path.h"
18
19
using namespace clang::driver;
20
using namespace clang::driver::tools;
21
using namespace clang::driver::toolchains;
22
using namespace clang;
23
using namespace llvm::opt;
24
25
void cloudabi::Linker::ConstructJob(Compilation &C, const JobAction &JA,
26
                                    const InputInfo &Output,
27
                                    const InputInfoList &Inputs,
28
                                    const ArgList &Args,
29
8
                                    const char *LinkingOutput) const {
30
8
  const ToolChain &ToolChain = getToolChain();
31
8
  const Driver &D = ToolChain.getDriver();
32
8
  ArgStringList CmdArgs;
33
8
34
8
  // Silence warning for "clang -g foo.o -o foo"
35
8
  Args.ClaimAllArgs(options::OPT_g_Group);
36
8
  // and "clang -emit-llvm foo.o -o foo"
37
8
  Args.ClaimAllArgs(options::OPT_emit_llvm);
38
8
  // and for "clang -w foo.o -o foo". Other warning options are already
39
8
  // handled somewhere else.
40
8
  Args.ClaimAllArgs(options::OPT_w);
41
8
42
8
  if (!D.SysRoot.empty())
43
0
    CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
44
8
45
8
  // CloudABI only supports static linkage.
46
8
  CmdArgs.push_back("-Bstatic");
47
8
  CmdArgs.push_back("--no-dynamic-linker");
48
8
49
8
  // Provide PIE linker flags in case PIE is default for the architecture.
50
8
  if (ToolChain.isPIEDefault()) {
51
6
    CmdArgs.push_back("-pie");
52
6
    CmdArgs.push_back("-zrelro");
53
6
  }
54
8
55
8
  CmdArgs.push_back("--eh-frame-hdr");
56
8
  CmdArgs.push_back("--gc-sections");
57
8
58
8
  if (Output.isFilename()) {
59
8
    CmdArgs.push_back("-o");
60
8
    CmdArgs.push_back(Output.getFilename());
61
8
  } else {
62
0
    assert(Output.isNothing() && "Invalid output.");
63
0
  }
64
8
65
8
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
66
8
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
67
8
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtbegin.o")));
68
8
  }
69
8
70
8
  Args.AddAllArgs(CmdArgs, options::OPT_L);
71
8
  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
72
8
  Args.AddAllArgs(CmdArgs,
73
8
                  {options::OPT_T_Group, options::OPT_e, options::OPT_s,
74
8
                   options::OPT_t, options::OPT_Z_Flag, options::OPT_r});
75
8
76
8
  if (D.isUsingLTO()) {
77
0
    assert(!Inputs.empty() && "Must have at least one input.");
78
0
    AddGoldPlugin(ToolChain, Args, CmdArgs, Output, Inputs[0],
79
0
                  D.getLTOMode() == LTOK_Thin);
80
0
  }
81
8
82
8
  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
83
8
84
8
  if (ToolChain.ShouldLinkCXXStdlib(Args))
85
3
    ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
86
8
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
87
8
    CmdArgs.push_back("-lc");
88
8
    CmdArgs.push_back("-lcompiler_rt");
89
8
  }
90
8
91
8
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
92
8
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
93
8
94
8
  const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
95
8
  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
96
8
}
97
98
// CloudABI - CloudABI tool chain which can call ld(1) directly.
99
100
CloudABI::CloudABI(const Driver &D, const llvm::Triple &Triple,
101
                   const ArgList &Args)
102
14
    : Generic_ELF(D, Triple, Args) {
103
14
  SmallString<128> P(getDriver().Dir);
104
14
  llvm::sys::path::append(P, "..", getTriple().str(), "lib");
105
14
  getFilePaths().push_back(P.str());
106
14
}
107
108
void CloudABI::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
109
3
                                     llvm::opt::ArgStringList &CC1Args) const {
110
3
  SmallString<128> P(getDriver().Dir);
111
3
  llvm::sys::path::append(P, "..", getTriple().str(), "include/c++/v1");
112
3
  addSystemInclude(DriverArgs, CC1Args, P.str());
113
3
}
114
115
void CloudABI::AddCXXStdlibLibArgs(const ArgList &Args,
116
3
                                   ArgStringList &CmdArgs) const {
117
3
  CmdArgs.push_back("-lc++");
118
3
  CmdArgs.push_back("-lc++abi");
119
3
  CmdArgs.push_back("-lunwind");
120
3
}
121
122
8
Tool *CloudABI::buildLinker() const {
123
8
  return new tools::cloudabi::Linker(*this);
124
8
}
125
126
22
bool CloudABI::isPIEDefault() const {
127
22
  // Only enable PIE on architectures that support PC-relative
128
22
  // addressing. PC-relative addressing is required, as the process
129
22
  // startup code must be able to relocate itself.
130
22
  switch (getTriple().getArch()) {
131
22
  case llvm::Triple::aarch64:
132
16
  case llvm::Triple::x86_64:
133
16
    return true;
134
16
  default:
135
6
    return false;
136
22
  }
137
22
}
138
139
14
SanitizerMask CloudABI::getSupportedSanitizers() const {
140
14
  SanitizerMask Res = ToolChain::getSupportedSanitizers();
141
14
  Res |= SanitizerKind::SafeStack;
142
14
  return Res;
143
14
}
144
145
14
SanitizerMask CloudABI::getDefaultSanitizers() const {
146
14
  return SanitizerKind::SafeStack;
147
14
}