clang  14.0.0git
AIX.cpp
Go to the documentation of this file.
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"
13 #include "clang/Driver/Options.h"
15 #include "llvm/Option/ArgList.h"
16 #include "llvm/Support/Path.h"
17 
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 
27  const InputInfo &Output,
28  const InputInfoList &Inputs,
29  const ArgList &Args,
30  const char *LinkingOutput) const {
31  ArgStringList CmdArgs;
32 
33  const bool IsArch32Bit = getToolChain().getTriple().isArch32Bit();
34  const bool IsArch64Bit = getToolChain().getTriple().isArch64Bit();
35  // Only support 32 and 64 bit.
36  if (!IsArch32Bit && !IsArch64Bit)
37  llvm_unreachable("Unsupported bit width value.");
38 
39  // Specify the mode in which the as(1) command operates.
40  if (IsArch32Bit) {
41  CmdArgs.push_back("-a32");
42  } else {
43  // Must be 64-bit, otherwise asserted already.
44  CmdArgs.push_back("-a64");
45  }
46 
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  CmdArgs.push_back("-many");
52 
53  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
54 
55  // Specify assembler output file.
56  assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
57  if (Output.isFilename()) {
58  CmdArgs.push_back("-o");
59  CmdArgs.push_back(Output.getFilename());
60  }
61 
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  if (Inputs.size() != 1)
66  llvm_unreachable("Invalid number of input files.");
67  const InputInfo &II = Inputs[0];
68  assert((II.isFilename() || II.isNothing()) && "Invalid input.");
69  if (II.isFilename())
70  CmdArgs.push_back(II.getFilename());
71 
72  const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
73  C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
74  Exec, CmdArgs, Inputs, Output));
75 }
76 
78  const InputInfo &Output,
79  const InputInfoList &Inputs, const ArgList &Args,
80  const char *LinkingOutput) const {
81  const AIX &ToolChain = static_cast<const AIX &>(getToolChain());
82  const Driver &D = ToolChain.getDriver();
83  ArgStringList CmdArgs;
84 
85  const bool IsArch32Bit = ToolChain.getTriple().isArch32Bit();
86  const bool IsArch64Bit = ToolChain.getTriple().isArch64Bit();
87  // Only support 32 and 64 bit.
88  if (!(IsArch32Bit || IsArch64Bit))
89  llvm_unreachable("Unsupported bit width value.");
90 
91  // Force static linking when "-static" is present.
92  if (Args.hasArg(options::OPT_static))
93  CmdArgs.push_back("-bnso");
94 
95  // Add options for shared libraries.
96  if (Args.hasArg(options::OPT_shared)) {
97  CmdArgs.push_back("-bM:SRE");
98  CmdArgs.push_back("-bnoentry");
99  }
100 
101  // Specify PGO linker option without LTO
102  if (!D.isUsingLTO() &&
103  (Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
104  false) ||
105  Args.hasFlag(options::OPT_fprofile_generate,
106  options::OPT_fno_profile_generate, false) ||
107  Args.hasFlag(options::OPT_fprofile_generate_EQ,
108  options::OPT_fno_profile_generate, false) ||
109  Args.hasFlag(options::OPT_fprofile_instr_generate,
110  options::OPT_fno_profile_instr_generate, false) ||
111  Args.hasFlag(options::OPT_fprofile_instr_generate_EQ,
112  options::OPT_fno_profile_instr_generate, false) ||
113  Args.hasFlag(options::OPT_fcs_profile_generate,
114  options::OPT_fno_profile_generate, false) ||
115  Args.hasFlag(options::OPT_fcs_profile_generate_EQ,
116  options::OPT_fno_profile_generate, false) ||
117  Args.hasArg(options::OPT_fcreate_profile) ||
118  Args.hasArg(options::OPT_coverage)))
119  CmdArgs.push_back("-bdbg:namedsects");
120 
121  // Specify linker output file.
122  assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
123  if (Output.isFilename()) {
124  CmdArgs.push_back("-o");
125  CmdArgs.push_back(Output.getFilename());
126  }
127 
128  // Set linking mode (i.e., 32/64-bit) and the address of
129  // text and data sections based on arch bit width.
130  if (IsArch32Bit) {
131  CmdArgs.push_back("-b32");
132  CmdArgs.push_back("-bpT:0x10000000");
133  CmdArgs.push_back("-bpD:0x20000000");
134  } else {
135  // Must be 64-bit, otherwise asserted already.
136  CmdArgs.push_back("-b64");
137  CmdArgs.push_back("-bpT:0x100000000");
138  CmdArgs.push_back("-bpD:0x110000000");
139  }
140 
141  auto getCrt0Basename = [&Args, IsArch32Bit] {
142  // Enable gprofiling when "-pg" is specified.
143  if (Args.hasArg(options::OPT_pg))
144  return IsArch32Bit ? "gcrt0.o" : "gcrt0_64.o";
145  // Enable profiling when "-p" is specified.
146  else if (Args.hasArg(options::OPT_p))
147  return IsArch32Bit ? "mcrt0.o" : "mcrt0_64.o";
148  else
149  return IsArch32Bit ? "crt0.o" : "crt0_64.o";
150  };
151 
152  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
153  options::OPT_shared)) {
154  CmdArgs.push_back(
155  Args.MakeArgString(ToolChain.GetFilePath(getCrt0Basename())));
156 
157  CmdArgs.push_back(Args.MakeArgString(
158  ToolChain.GetFilePath(IsArch32Bit ? "crti.o" : "crti_64.o")));
159  }
160 
161  // Collect all static constructor and destructor functions in both C and CXX
162  // language link invocations. This has to come before AddLinkerInputs as the
163  // implied option needs to precede any other '-bcdtors' settings or
164  // '-bnocdtors' that '-Wl' might forward.
165  CmdArgs.push_back("-bcdtors:all:0:s");
166 
167  // Specify linker input file(s).
168  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
169 
170  // Add directory to library search path.
171  Args.AddAllArgs(CmdArgs, options::OPT_L);
172  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
173  ToolChain.addProfileRTLibs(Args, CmdArgs);
174 
175  if (getToolChain().ShouldLinkCXXStdlib(Args))
176  getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
177 
178  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
179  AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
180 
181  // Support POSIX threads if "-pthreads" or "-pthread" is present.
182  if (Args.hasArg(options::OPT_pthreads, options::OPT_pthread))
183  CmdArgs.push_back("-lpthreads");
184 
185  if (D.CCCIsCXX())
186  CmdArgs.push_back("-lm");
187 
188  CmdArgs.push_back("-lc");
189  }
190 
191  const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
192  C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
193  Exec, CmdArgs, Inputs, Output));
194 }
195 
196 /// AIX - AIX tool chain which can call as(1) and ld(1) directly.
197 AIX::AIX(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
198  : ToolChain(D, Triple, Args) {
199  ParseInlineAsmUsingAsmParser = Args.hasFlag(
200  options::OPT_fintegrated_as, options::OPT_fno_integrated_as, true);
201  getLibraryPaths().push_back(getDriver().SysRoot + "/usr/lib");
202 }
203 
204 // Returns the effective header sysroot path to use.
205 // This comes from either -isysroot or --sysroot.
206 llvm::StringRef
207 AIX::GetHeaderSysroot(const llvm::opt::ArgList &DriverArgs) const {
208  if (DriverArgs.hasArg(options::OPT_isysroot))
209  return DriverArgs.getLastArgValue(options::OPT_isysroot);
210  if (!getDriver().SysRoot.empty())
211  return getDriver().SysRoot;
212  return "/";
213 }
214 
215 void AIX::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
216  ArgStringList &CC1Args) const {
217  // Return if -nostdinc is specified as a driver option.
218  if (DriverArgs.hasArg(options::OPT_nostdinc))
219  return;
220 
221  llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs);
222  const Driver &D = getDriver();
223 
224  // Add the Clang builtin headers (<resource>/include).
225  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
227  path::append(P, "/include");
228  addSystemInclude(DriverArgs, CC1Args, P.str());
229  }
230 
231  // Return if -nostdlibinc is specified as a driver option.
232  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
233  return;
234 
235  // Add <sysroot>/usr/include.
236  SmallString<128> UP(Sysroot);
237  path::append(UP, "/usr/include");
238  addSystemInclude(DriverArgs, CC1Args, UP.str());
239 }
240 
242  const llvm::opt::ArgList &DriverArgs,
243  llvm::opt::ArgStringList &CC1Args) const {
244 
245  if (DriverArgs.hasArg(options::OPT_nostdinc) ||
246  DriverArgs.hasArg(options::OPT_nostdincxx) ||
247  DriverArgs.hasArg(options::OPT_nostdlibinc))
248  return;
249 
250  switch (GetCXXStdlibType(DriverArgs)) {
252  llvm::report_fatal_error(
253  "picking up libstdc++ headers is unimplemented on AIX");
254  case ToolChain::CST_Libcxx: {
255  llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs);
256  SmallString<128> PathCPP(Sysroot);
257  llvm::sys::path::append(PathCPP, "opt/IBM/openxlCSDK", "include", "c++",
258  "v1");
259  addSystemInclude(DriverArgs, CC1Args, PathCPP.str());
260  // Required in order to suppress conflicting C++ overloads in the system
261  // libc headers that were used by XL C++.
262  CC1Args.push_back("-D__LIBC_NO_CPP_MATH_OVERLOADS__");
263  return;
264  }
265  }
266 
267  llvm_unreachable("Unexpected C++ library type; only libc++ is supported.");
268 }
269 
270 void AIX::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
271  llvm::opt::ArgStringList &CmdArgs) const {
272  switch (GetCXXStdlibType(Args)) {
274  llvm::report_fatal_error("linking libstdc++ unimplemented on AIX");
276  CmdArgs.push_back("-lc++");
277  CmdArgs.push_back("-lc++abi");
278  return;
279  }
280 
281  llvm_unreachable("Unexpected C++ library type; only libc++ is supported.");
282 }
283 
285  return ToolChain::CST_Libcxx;
286 }
287 
290 }
291 
292 auto AIX::buildAssembler() const -> Tool * { return new aix::Assembler(*this); }
293 
294 auto AIX::buildLinker() const -> Tool * { return new aix::Linker(*this); }
clang::driver::toolchains::AIX::buildAssembler
Tool * buildAssembler() const override
Definition: AIX.cpp:292
clang::driver::toolchains
Definition: AIX.h:55
clang::driver::toolchains::AIX::AddCXXStdlibLibArgs
void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const override
AddCXXStdlibLibArgs - Add the system specific linker arguments to use for the given C++ standard libr...
Definition: AIX.cpp:270
llvm::SmallVector
Definition: LLVM.h:38
clang::driver::tools
Definition: AIX.h:17
clang::driver::ToolChain::getDriver
const Driver & getDriver() const
Definition: ToolChain.h:221
clang::driver::tools::aix::Assembler::ConstructJob
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs,...
Definition: AIX.cpp:26
clang::driver::ToolChain::GetFilePath
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:536
clang::driver::tools::aix::Linker::ConstructJob
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs,...
Definition: AIX.cpp:77
clang::driver::ToolChain::GetLinkerPath
std::string GetLinkerPath(bool *LinkerIsLLD=nullptr, bool *LinkerIsLLDDarwinNew=nullptr) const
Returns the linker path, respecting the -fuse-ld= argument to determine the linker suffix or name.
Definition: ToolChain.cpp:544
clang::driver::InputInfo
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
clang::driver::ToolChain::RuntimeLibType
RuntimeLibType
Definition: ToolChain.h:100
clang::driver::tools::AddLinkerInputs
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
clang::driver::Tool
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
clang::driver::tools::aix::Linker
Definition: AIX.h:34
Options.h
clang::driver::InputInfo::isFilename
bool isFilename() const
Definition: InputInfo.h:75
clang::driver::tools::aix::Assembler
Definition: AIX.h:22
llvm::opt
Definition: DiagnosticOptions.h:19
clang::driver::InputInfo::isNothing
bool isNothing() const
Definition: InputInfo.h:74
clang::driver::ToolChain::CXXStdlibType
CXXStdlibType
Definition: ToolChain.h:95
llvm::SmallString< 128 >
clang::driver::ToolChain::getTriple
const llvm::Triple & getTriple() const
Definition: ToolChain.h:223
clang::driver::Driver::SysRoot
std::string SysRoot
sysroot, if present
Definition: Driver.h:151
clang::driver::ToolChain::CST_Libcxx
@ CST_Libcxx
Definition: ToolChain.h:96
clang::driver::Driver::CCCIsCXX
bool CCCIsCXX() const
Whether the driver should follow g++ like behavior.
Definition: Driver.h:179
SanitizerArgs.h
clang::driver::ToolChain::CST_Libstdcxx
@ CST_Libstdcxx
Definition: ToolChain.h:97
clang::driver::Driver::ResourceDir
std::string ResourceDir
The path to the compiler resource directory.
Definition: Driver.h:135
clang::driver::ToolChain::AddFilePathLibArgs
void AddFilePathLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddFilePathLibArgs - Add each thing in getFilePaths() as a "-L" option.
Definition: ToolChain.cpp:953
clang::driver::toolchains::AIX::AIX
AIX(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
AIX - AIX tool chain which can call as(1) and ld(1) directly.
Definition: AIX.cpp:197
clang::driver::toolchains::AIX::GetDefaultRuntimeLibType
RuntimeLibType GetDefaultRuntimeLibType() const override
GetDefaultRuntimeLibType - Get the default runtime library variant to use.
Definition: AIX.cpp:288
P
StringRef P
Definition: ASTMatchersInternal.cpp:563
clang::driver::Driver::isUsingLTO
bool isUsingLTO(bool IsOffload=false) const
Returns true if we are performing any kind of LTO.
Definition: Driver.h:559
Compilation.h
clang::driver::toolchains::AIX::GetDefaultCXXStdlibType
CXXStdlibType GetDefaultCXXStdlibType() const override
Definition: AIX.cpp:284
clang::driver::ToolChain::addProfileRTLibs
virtual void addProfileRTLibs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
addProfileRTLibs - When -fprofile-instr-profile is specified, try to pass a suitable profile runtime ...
Definition: ToolChain.cpp:747
clang::driver::ToolChain
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:91
clang::driver::ToolChain::RLT_CompilerRT
@ RLT_CompilerRT
Definition: ToolChain.h:101
clang::driver::toolchains::AIX::buildLinker
Tool * buildLinker() const override
Definition: AIX.cpp:294
clang::driver::tools::AddRunTimeLibs
void AddRunTimeLibs(const ToolChain &TC, const Driver &D, llvm::opt::ArgStringList &CmdArgs, const llvm::opt::ArgList &Args)
clang::driver::ToolChain::getLibraryPaths
path_list & getLibraryPaths()
Definition: ToolChain.h:256
clang::driver::Compilation
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
clang::driver::ToolChain::GetCXXStdlibType
virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:817
clang::driver::InputInfo::getFilename
const char * getFilename() const
Definition: InputInfo.h:83
CommonArgs.h
clang::driver::Driver
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
clang::driver::toolchains::AIX::AddClangCXXStdlibIncludeArgs
void AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set the include paths to use for...
Definition: AIX.cpp:241
clang::driver::toolchains::AIX::AddClangSystemIncludeArgs
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
Definition: AIX.cpp:215
clang::driver
Definition: Action.h:31
clang::driver::ResponseFileSupport::None
static constexpr ResponseFileSupport None()
Returns a ResponseFileSupport indicating that response files are not supported.
Definition: Job.h:78
clang::driver::ToolChain::addSystemInclude
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory to CC1 arguments.
Definition: ToolChain.cpp:843
AIX
clang::driver::toolchains::AIX AIX
Definition: AIX.cpp:18
clang::driver::toolchains::AIX
Definition: AIX.h:57
clang::driver::JobAction
Definition: Action.h:380
PPC.h
AIX.h