Coverage Report

Created: 2020-09-15 12:33

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Driver/ToolChains/AIX.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- AIX.cpp - AIX 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 "AIX.h"
10
#include "Arch/PPC.h"
11
#include "CommonArgs.h"
12
#include "clang/Driver/Compilation.h"
13
#include "clang/Driver/Options.h"
14
#include "clang/Driver/SanitizerArgs.h"
15
#include "llvm/Option/ArgList.h"
16
#include "llvm/Support/Path.h"
17
18
using AIX = clang::driver::toolchains::AIX;
19
using namespace clang::driver;
20
using namespace clang::driver::tools;
21
using namespace clang::driver::toolchains;
22
23
using namespace llvm::opt;
24
using namespace llvm::sys;
25
26
void aix::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
27
                                  const InputInfo &Output,
28
                                  const InputInfoList &Inputs,
29
                                  const ArgList &Args,
30
54
                                  const char *LinkingOutput) const {
31
54
  ArgStringList CmdArgs;
32
54
33
54
  const bool IsArch32Bit = getToolChain().getTriple().isArch32Bit();
34
54
  const bool IsArch64Bit = getToolChain().getTriple().isArch64Bit();
35
  // Only support 32 and 64 bit.
36
54
  if (!IsArch32Bit && 
!IsArch64Bit23
)
37
0
    llvm_unreachable("Unsupported bit width value.");
38
54
39
  // Specify the mode in which the as(1) command operates.
40
54
  if (IsArch32Bit) {
41
31
    CmdArgs.push_back("-a32");
42
23
  } else {
43
    // Must be 64-bit, otherwise asserted already.
44
23
    CmdArgs.push_back("-a64");
45
23
  }
46
54
47
  // Accept any mixture of instructions.
48
  // On Power for AIX and Linux, this behaviour matches that of GCC for both the
49
  // user-provided assembler source case and the compiler-produced assembler
50
  // source case. Yet XL with user-provided assembler source would not add this.
51
54
  CmdArgs.push_back("-many");
52
54
53
54
  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
54
54
55
  // Specify assembler output file.
56
54
  assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
57
54
  if (Output.isFilename()) {
58
54
    CmdArgs.push_back("-o");
59
54
    CmdArgs.push_back(Output.getFilename());
60
54
  }
61
54
62
  // Specify assembler input file.
63
  // The system assembler on AIX takes exactly one input file. The driver is
64
  // expected to invoke as(1) separately for each assembler source input file.
65
54
  if (Inputs.size() != 1)
66
0
    llvm_unreachable("Invalid number of input files.");
67
54
  const InputInfo &II = Inputs[0];
68
54
  assert((II.isFilename() || II.isNothing()) && "Invalid input.");
69
54
  if (II.isFilename())
70
54
    CmdArgs.push_back(II.getFilename());
71
54
72
54
  const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
73
54
  C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
74
54
                                         Exec, CmdArgs, Inputs));
75
54
}
76
77
void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA,
78
                               const InputInfo &Output,
79
                               const InputInfoList &Inputs, const ArgList &Args,
80
35
                               const char *LinkingOutput) const {
81
35
  const AIX &ToolChain = static_cast<const AIX &>(getToolChain());
82
35
  const Driver &D = ToolChain.getDriver();
83
35
  ArgStringList CmdArgs;
84
35
85
35
  const bool IsArch32Bit = ToolChain.getTriple().isArch32Bit();
86
35
  const bool IsArch64Bit = ToolChain.getTriple().isArch64Bit();
87
  // Only support 32 and 64 bit.
88
35
  if (!(IsArch32Bit || 
IsArch64Bit16
))
89
0
    llvm_unreachable("Unsupported bit width value.");
90
35
91
  // Force static linking when "-static" is present.
92
35
  if (Args.hasArg(options::OPT_static))
93
1
    CmdArgs.push_back("-bnso");
94
35
95
  // Specify linker output file.
96
35
  assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
97
35
  if (Output.isFilename()) {
98
35
    CmdArgs.push_back("-o");
99
35
    CmdArgs.push_back(Output.getFilename());
100
35
  }
101
35
102
  // Set linking mode (i.e., 32/64-bit) and the address of
103
  // text and data sections based on arch bit width.
104
35
  if (IsArch32Bit) {
105
19
    CmdArgs.push_back("-b32");
106
19
    CmdArgs.push_back("-bpT:0x10000000");
107
19
    CmdArgs.push_back("-bpD:0x20000000");
108
16
  } else {
109
    // Must be 64-bit, otherwise asserted already.
110
16
    CmdArgs.push_back("-b64");
111
16
    CmdArgs.push_back("-bpT:0x100000000");
112
16
    CmdArgs.push_back("-bpD:0x110000000");
113
16
  }
114
35
115
32
  auto getCrt0Basename = [&Args, IsArch32Bit] {
116
    // Enable gprofiling when "-pg" is specified.
117
32
    if (Args.hasArg(options::OPT_pg))
118
1
      return IsArch32Bit ? 
"gcrt0.o"0
: "gcrt0_64.o";
119
    // Enable profiling when "-p" is specified.
120
31
    else if (Args.hasArg(options::OPT_p))
121
1
      return IsArch32Bit ? "mcrt0.o" : 
"mcrt0_64.o"0
;
122
30
    else
123
30
      return IsArch32Bit ? 
"crt0.o"16
:
"crt0_64.o"14
;
124
32
  };
125
35
126
35
  if (!Args.hasArg(options::OPT_nostdlib)) {
127
32
    CmdArgs.push_back(
128
32
        Args.MakeArgString(ToolChain.GetFilePath(getCrt0Basename())));
129
32
  }
130
35
131
  // Collect all static constructor and destructor functions in CXX mode. This
132
  // has to come before AddLinkerInputs as the implied option needs to precede
133
  // any other '-bcdtors' settings or '-bnocdtors' that '-Wl' might forward.
134
35
  if (D.CCCIsCXX())
135
17
    CmdArgs.push_back("-bcdtors:all:0:s");
136
35
137
  // Specify linker input file(s).
138
35
  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
139
35
140
  // Add directory to library search path.
141
35
  Args.AddAllArgs(CmdArgs, options::OPT_L);
142
35
  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
143
35
144
35
  if (getToolChain().ShouldLinkCXXStdlib(Args))
145
11
    getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
146
35
147
35
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
148
    // Support POSIX threads if "-pthreads" or "-pthread" is present.
149
29
    if (Args.hasArg(options::OPT_pthreads, options::OPT_pthread))
150
2
      CmdArgs.push_back("-lpthreads");
151
29
152
29
    CmdArgs.push_back("-lc");
153
29
  }
154
35
155
35
  const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
156
35
  C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
157
35
                                         Exec, CmdArgs, Inputs));
158
35
}
159
160
/// AIX - AIX tool chain which can call as(1) and ld(1) directly.
161
AIX::AIX(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
162
59
    : ToolChain(D, Triple, Args) {
163
59
  getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
164
59
}
165
166
// Returns the effective header sysroot path to use.
167
// This comes from either -isysroot or --sysroot.
168
llvm::StringRef
169
49
AIX::GetHeaderSysroot(const llvm::opt::ArgList &DriverArgs) const {
170
49
  if (DriverArgs.hasArg(options::OPT_isysroot))
171
0
    return DriverArgs.getLastArgValue(options::OPT_isysroot);
172
49
  if (!getDriver().SysRoot.empty())
173
31
    return getDriver().SysRoot;
174
18
  return "/";
175
18
}
176
177
void AIX::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
178
53
                                    ArgStringList &CC1Args) const {
179
  // Return if -nostdinc is specified as a driver option.
180
53
  if (DriverArgs.hasArg(options::OPT_nostdinc))
181
4
    return;
182
49
183
49
  llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs);
184
49
  const Driver &D = getDriver();
185
49
186
  // Add the Clang builtin headers (<resource>/include).
187
49
  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
188
45
    SmallString<128> P(D.ResourceDir);
189
45
    path::append(P, "/include");
190
45
    addSystemInclude(DriverArgs, CC1Args, P.str());
191
45
  }
192
49
193
  // Return if -nostdlibinc is specified as a driver option.
194
49
  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
195
4
    return;
196
45
197
  // Add <sysroot>/usr/include.
198
45
  SmallString<128> UP(Sysroot);
199
45
  path::append(UP, "/usr/include");
200
45
  addSystemInclude(DriverArgs, CC1Args, UP.str());
201
45
}
202
203
void AIX::AddCXXStdlibLibArgs(const llvm::opt::ArgList &DriverArgs,
204
11
                              llvm::opt::ArgStringList &CC1Args) const {
205
11
  switch (GetCXXStdlibType(DriverArgs)) {
206
11
  case ToolChain::CST_Libcxx:
207
11
    CC1Args.push_back("-lc++");
208
11
    return;
209
0
  case ToolChain::CST_Libstdcxx:
210
0
    llvm::report_fatal_error("linking libstdc++ unimplemented on AIX");
211
0
  }
212
0
213
0
  llvm_unreachable("Unexpected C++ library type; only libc++ is supported.");
214
0
}
215
216
11
ToolChain::CXXStdlibType AIX::GetDefaultCXXStdlibType() const {
217
11
  return ToolChain::CST_Libcxx;
218
11
}
219
220
52
auto AIX::buildAssembler() const -> Tool * { return new aix::Assembler(*this); }
221
222
35
auto AIX::buildLinker() const -> Tool * { return new aix::Linker(*this); }