clang  12.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 linker output file.
102  assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
103  if (Output.isFilename()) {
104  CmdArgs.push_back("-o");
105  CmdArgs.push_back(Output.getFilename());
106  }
107 
108  // Set linking mode (i.e., 32/64-bit) and the address of
109  // text and data sections based on arch bit width.
110  if (IsArch32Bit) {
111  CmdArgs.push_back("-b32");
112  CmdArgs.push_back("-bpT:0x10000000");
113  CmdArgs.push_back("-bpD:0x20000000");
114  } else {
115  // Must be 64-bit, otherwise asserted already.
116  CmdArgs.push_back("-b64");
117  CmdArgs.push_back("-bpT:0x100000000");
118  CmdArgs.push_back("-bpD:0x110000000");
119  }
120 
121  auto getCrt0Basename = [&Args, IsArch32Bit] {
122  // Enable gprofiling when "-pg" is specified.
123  if (Args.hasArg(options::OPT_pg))
124  return IsArch32Bit ? "gcrt0.o" : "gcrt0_64.o";
125  // Enable profiling when "-p" is specified.
126  else if (Args.hasArg(options::OPT_p))
127  return IsArch32Bit ? "mcrt0.o" : "mcrt0_64.o";
128  else
129  return IsArch32Bit ? "crt0.o" : "crt0_64.o";
130  };
131 
132  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
133  options::OPT_shared)) {
134  CmdArgs.push_back(
135  Args.MakeArgString(ToolChain.GetFilePath(getCrt0Basename())));
136 
137  CmdArgs.push_back(Args.MakeArgString(
138  ToolChain.GetFilePath(IsArch32Bit ? "crti.o" : "crti_64.o")));
139  }
140 
141  // Collect all static constructor and destructor functions in both C and CXX
142  // language link invocations. This has to come before AddLinkerInputs as the
143  // implied option needs to precede any other '-bcdtors' settings or
144  // '-bnocdtors' that '-Wl' might forward.
145  CmdArgs.push_back("-bcdtors:all:0:s");
146 
147  // Specify linker input file(s).
148  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
149 
150  // Add directory to library search path.
151  Args.AddAllArgs(CmdArgs, options::OPT_L);
152  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
153  ToolChain.addProfileRTLibs(Args, CmdArgs);
154 
155  if (getToolChain().ShouldLinkCXXStdlib(Args))
156  getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
157 
158  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
159  AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
160 
161  // Support POSIX threads if "-pthreads" or "-pthread" is present.
162  if (Args.hasArg(options::OPT_pthreads, options::OPT_pthread))
163  CmdArgs.push_back("-lpthreads");
164 
165  if (D.CCCIsCXX())
166  CmdArgs.push_back("-lm");
167 
168  CmdArgs.push_back("-lc");
169  }
170 
171  const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
172  C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
173  Exec, CmdArgs, Inputs, Output));
174 }
175 
176 /// AIX - AIX tool chain which can call as(1) and ld(1) directly.
177 AIX::AIX(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
178  : ToolChain(D, Triple, Args) {
179  getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
180 }
181 
182 // Returns the effective header sysroot path to use.
183 // This comes from either -isysroot or --sysroot.
184 llvm::StringRef
185 AIX::GetHeaderSysroot(const llvm::opt::ArgList &DriverArgs) const {
186  if (DriverArgs.hasArg(options::OPT_isysroot))
187  return DriverArgs.getLastArgValue(options::OPT_isysroot);
188  if (!getDriver().SysRoot.empty())
189  return getDriver().SysRoot;
190  return "/";
191 }
192 
193 void AIX::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
194  ArgStringList &CC1Args) const {
195  // Return if -nostdinc is specified as a driver option.
196  if (DriverArgs.hasArg(options::OPT_nostdinc))
197  return;
198 
199  llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs);
200  const Driver &D = getDriver();
201 
202  // Add the Clang builtin headers (<resource>/include).
203  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
205  path::append(P, "/include");
206  addSystemInclude(DriverArgs, CC1Args, P.str());
207  }
208 
209  // Return if -nostdlibinc is specified as a driver option.
210  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
211  return;
212 
213  // Add <sysroot>/usr/include.
214  SmallString<128> UP(Sysroot);
215  path::append(UP, "/usr/include");
216  addSystemInclude(DriverArgs, CC1Args, UP.str());
217 }
218 
219 void AIX::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
220  llvm::opt::ArgStringList &CmdArgs) const {
221  switch (GetCXXStdlibType(Args)) {
223  CmdArgs.push_back("-lc++");
224  return;
226  llvm::report_fatal_error("linking libstdc++ unimplemented on AIX");
227  }
228 
229  llvm_unreachable("Unexpected C++ library type; only libc++ is supported.");
230 }
231 
233  return ToolChain::CST_Libcxx;
234 }
235 
238 }
239 
240 auto AIX::buildAssembler() const -> Tool * { return new aix::Assembler(*this); }
241 
242 auto AIX::buildLinker() const -> Tool * { return new aix::Linker(*this); }
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
StringRef P
void AddRunTimeLibs(const ToolChain &TC, const Driver &D, llvm::opt::ArgStringList &CmdArgs, const llvm::opt::ArgList &Args)
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:864
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:219
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:177
const char * getFilename() const
Definition: InputInfo.h:83
Tool * buildLinker() const override
Definition: AIX.cpp:242
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:546
path_list & getFilePaths()
Definition: ToolChain.h:242
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
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:1023
const Driver & getDriver() const
Definition: ToolChain.h:204
CXXStdlibType GetDefaultCXXStdlibType() const override
Definition: AIX.cpp:232
bool CCCIsCXX() const
Whether the driver should follow g++ like behavior.
Definition: Driver.h:173
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
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:937
RuntimeLibType GetDefaultRuntimeLibType() const override
GetDefaultRuntimeLibType - Get the default runtime library variant to use.
Definition: AIX.cpp:236
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
std::string SysRoot
sysroot, if present
Definition: Driver.h:148
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
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:554
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
const llvm::Triple & getTriple() const
Definition: ToolChain.h:206
clang::driver::toolchains::AIX AIX
Definition: AIX.cpp:18
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:193
bool isNothing() const
Definition: InputInfo.h:74
static constexpr ResponseFileSupport None()
Returns a ResponseFileSupport indicating that response files are not supported.
Definition: Job.h:77
bool isFilename() const
Definition: InputInfo.h:75
virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:918
Tool * buildAssembler() const override
Definition: AIX.cpp:240
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:90
std::string ResourceDir
The path to the compiler resource directory.
Definition: Driver.h:132