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/ProfileData/InstrProf.h"
17 #include "llvm/Support/Path.h"
18
19 using AIX = clang::driver::toolchains::AIX;
20 using namespace clang::driver;
21 using namespace clang::driver::tools;
22 using namespace clang::driver::toolchains;
23
24 using namespace llvm::opt;
25 using namespace llvm::sys;
26
ConstructJob(Compilation & C,const JobAction & JA,const InputInfo & Output,const InputInfoList & Inputs,const ArgList & Args,const char * LinkingOutput) const27 void aix::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
28 const InputInfo &Output,
29 const InputInfoList &Inputs,
30 const ArgList &Args,
31 const char *LinkingOutput) const {
32 ArgStringList CmdArgs;
33
34 const bool IsArch32Bit = getToolChain().getTriple().isArch32Bit();
35 const bool IsArch64Bit = getToolChain().getTriple().isArch64Bit();
36 // Only support 32 and 64 bit.
37 if (!IsArch32Bit && !IsArch64Bit)
38 llvm_unreachable("Unsupported bit width value.");
39
40 // Specify the mode in which the as(1) command operates.
41 if (IsArch32Bit) {
42 CmdArgs.push_back("-a32");
43 } else {
44 // Must be 64-bit, otherwise asserted already.
45 CmdArgs.push_back("-a64");
46 }
47
48 // Accept any mixture of instructions.
49 // On Power for AIX and Linux, this behaviour matches that of GCC for both the
50 // user-provided assembler source case and the compiler-produced assembler
51 // source case. Yet XL with user-provided assembler source would not add this.
52 CmdArgs.push_back("-many");
53
54 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
55
56 // Specify assembler output file.
57 assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
58 if (Output.isFilename()) {
59 CmdArgs.push_back("-o");
60 CmdArgs.push_back(Output.getFilename());
61 }
62
63 // Specify assembler input file.
64 // The system assembler on AIX takes exactly one input file. The driver is
65 // expected to invoke as(1) separately for each assembler source input file.
66 if (Inputs.size() != 1)
67 llvm_unreachable("Invalid number of input files.");
68 const InputInfo &II = Inputs[0];
69 assert((II.isFilename() || II.isNothing()) && "Invalid input.");
70 if (II.isFilename())
71 CmdArgs.push_back(II.getFilename());
72
73 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
74 C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
75 Exec, CmdArgs, Inputs, Output));
76 }
77
78 // Determine whether there are any linker options that supply an export list
79 // (or equivalent information about what to export) being sent to the linker.
hasExportListLinkerOpts(const ArgStringList & CmdArgs)80 static bool hasExportListLinkerOpts(const ArgStringList &CmdArgs) {
81 for (size_t i = 0, Size = CmdArgs.size(); i < Size; ++i) {
82 llvm::StringRef ArgString(CmdArgs[i]);
83
84 if (ArgString.startswith("-bE:") || ArgString.startswith("-bexport:") ||
85 ArgString == "-bexpall" || ArgString == "-bexpfull")
86 return true;
87
88 // If we split -b option, check the next opt.
89 if (ArgString == "-b" && i + 1 < Size) {
90 ++i;
91 llvm::StringRef ArgNextString(CmdArgs[i]);
92 if (ArgNextString.startswith("E:") ||
93 ArgNextString.startswith("export:") || ArgNextString == "expall" ||
94 ArgNextString == "expfull")
95 return true;
96 }
97 }
98 return false;
99 }
100
ConstructJob(Compilation & C,const JobAction & JA,const InputInfo & Output,const InputInfoList & Inputs,const ArgList & Args,const char * LinkingOutput) const101 void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA,
102 const InputInfo &Output,
103 const InputInfoList &Inputs, const ArgList &Args,
104 const char *LinkingOutput) const {
105 const AIX &ToolChain = static_cast<const AIX &>(getToolChain());
106 const Driver &D = ToolChain.getDriver();
107 ArgStringList CmdArgs;
108
109 const bool IsArch32Bit = ToolChain.getTriple().isArch32Bit();
110 const bool IsArch64Bit = ToolChain.getTriple().isArch64Bit();
111 // Only support 32 and 64 bit.
112 if (!(IsArch32Bit || IsArch64Bit))
113 llvm_unreachable("Unsupported bit width value.");
114
115 // Force static linking when "-static" is present.
116 if (Args.hasArg(options::OPT_static))
117 CmdArgs.push_back("-bnso");
118
119 // Add options for shared libraries.
120 if (Args.hasArg(options::OPT_shared)) {
121 CmdArgs.push_back("-bM:SRE");
122 CmdArgs.push_back("-bnoentry");
123 }
124
125 // PGO instrumentation generates symbols belonging to special sections, and
126 // the linker needs to place all symbols in a particular section together in
127 // memory; the AIX linker does that under an option.
128 if (Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
129 false) ||
130 Args.hasFlag(options::OPT_fprofile_generate,
131 options::OPT_fno_profile_generate, false) ||
132 Args.hasFlag(options::OPT_fprofile_generate_EQ,
133 options::OPT_fno_profile_generate, false) ||
134 Args.hasFlag(options::OPT_fprofile_instr_generate,
135 options::OPT_fno_profile_instr_generate, false) ||
136 Args.hasFlag(options::OPT_fprofile_instr_generate_EQ,
137 options::OPT_fno_profile_instr_generate, false) ||
138 Args.hasFlag(options::OPT_fcs_profile_generate,
139 options::OPT_fno_profile_generate, false) ||
140 Args.hasFlag(options::OPT_fcs_profile_generate_EQ,
141 options::OPT_fno_profile_generate, false) ||
142 Args.hasArg(options::OPT_fcreate_profile) ||
143 Args.hasArg(options::OPT_coverage))
144 CmdArgs.push_back("-bdbg:namedsects:ss");
145
146 // Specify linker output file.
147 assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
148 if (Output.isFilename()) {
149 CmdArgs.push_back("-o");
150 CmdArgs.push_back(Output.getFilename());
151 }
152
153 // Set linking mode (i.e., 32/64-bit) and the address of
154 // text and data sections based on arch bit width.
155 if (IsArch32Bit) {
156 CmdArgs.push_back("-b32");
157 CmdArgs.push_back("-bpT:0x10000000");
158 CmdArgs.push_back("-bpD:0x20000000");
159 } else {
160 // Must be 64-bit, otherwise asserted already.
161 CmdArgs.push_back("-b64");
162 CmdArgs.push_back("-bpT:0x100000000");
163 CmdArgs.push_back("-bpD:0x110000000");
164 }
165
166 auto getCrt0Basename = [&Args, IsArch32Bit] {
167 // Enable gprofiling when "-pg" is specified.
168 if (Args.hasArg(options::OPT_pg))
169 return IsArch32Bit ? "gcrt0.o" : "gcrt0_64.o";
170 // Enable profiling when "-p" is specified.
171 else if (Args.hasArg(options::OPT_p))
172 return IsArch32Bit ? "mcrt0.o" : "mcrt0_64.o";
173 else
174 return IsArch32Bit ? "crt0.o" : "crt0_64.o";
175 };
176
177 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
178 options::OPT_shared)) {
179 CmdArgs.push_back(
180 Args.MakeArgString(ToolChain.GetFilePath(getCrt0Basename())));
181
182 CmdArgs.push_back(Args.MakeArgString(
183 ToolChain.GetFilePath(IsArch32Bit ? "crti.o" : "crti_64.o")));
184 }
185
186 // Collect all static constructor and destructor functions in both C and CXX
187 // language link invocations. This has to come before AddLinkerInputs as the
188 // implied option needs to precede any other '-bcdtors' settings or
189 // '-bnocdtors' that '-Wl' might forward.
190 CmdArgs.push_back("-bcdtors:all:0:s");
191
192 // Specify linker input file(s).
193 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
194
195 if (D.isUsingLTO()) {
196 assert(!Inputs.empty() && "Must have at least one input.");
197 addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs[0],
198 D.getLTOMode() == LTOK_Thin);
199 }
200
201 if (Args.hasArg(options::OPT_shared) && !hasExportListLinkerOpts(CmdArgs)) {
202
203 const char *CreateExportListExec = Args.MakeArgString(
204 path::parent_path(ToolChain.getDriver().ClangExecutable) +
205 "/llvm-nm");
206 ArgStringList CreateExportCmdArgs;
207
208 std::string CreateExportListPath =
209 C.getDriver().GetTemporaryPath("CreateExportList", "exp");
210 const char *ExportList =
211 C.addTempFile(C.getArgs().MakeArgString(CreateExportListPath));
212
213 for (const auto &II : Inputs)
214 if (II.isFilename())
215 CreateExportCmdArgs.push_back(II.getFilename());
216
217 CreateExportCmdArgs.push_back("--export-symbols");
218 CreateExportCmdArgs.push_back("-X");
219 if (IsArch32Bit) {
220 CreateExportCmdArgs.push_back("32");
221 } else {
222 // Must be 64-bit, otherwise asserted already.
223 CreateExportCmdArgs.push_back("64");
224 }
225
226 auto ExpCommand = std::make_unique<Command>(
227 JA, *this, ResponseFileSupport::None(), CreateExportListExec,
228 CreateExportCmdArgs, Inputs, Output);
229 ExpCommand->setRedirectFiles(
230 {std::nullopt, std::string(ExportList), std::nullopt});
231 C.addCommand(std::move(ExpCommand));
232 CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-bE:") + ExportList));
233 }
234
235 // Add directory to library search path.
236 Args.AddAllArgs(CmdArgs, options::OPT_L);
237 ToolChain.AddFilePathLibArgs(Args, CmdArgs);
238 ToolChain.addProfileRTLibs(Args, CmdArgs);
239
240 if (getToolChain().ShouldLinkCXXStdlib(Args))
241 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
242
243 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
244 AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
245
246 // Add OpenMP runtime if -fopenmp is specified.
247 if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
248 options::OPT_fno_openmp, false)) {
249 switch (ToolChain.getDriver().getOpenMPRuntime(Args)) {
250 case Driver::OMPRT_OMP:
251 CmdArgs.push_back("-lomp");
252 break;
253 case Driver::OMPRT_IOMP5:
254 CmdArgs.push_back("-liomp5");
255 break;
256 case Driver::OMPRT_GOMP:
257 CmdArgs.push_back("-lgomp");
258 break;
259 case Driver::OMPRT_Unknown:
260 // Already diagnosed.
261 break;
262 }
263 }
264
265 // Support POSIX threads if "-pthreads" or "-pthread" is present.
266 if (Args.hasArg(options::OPT_pthreads, options::OPT_pthread))
267 CmdArgs.push_back("-lpthreads");
268
269 if (D.CCCIsCXX())
270 CmdArgs.push_back("-lm");
271
272 CmdArgs.push_back("-lc");
273
274 if (Args.hasArg(options::OPT_pg)) {
275 CmdArgs.push_back(Args.MakeArgString((llvm::Twine("-L") + D.SysRoot) +
276 "/lib/profiled"));
277 CmdArgs.push_back(Args.MakeArgString((llvm::Twine("-L") + D.SysRoot) +
278 "/usr/lib/profiled"));
279 }
280 }
281
282 const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
283 C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
284 Exec, CmdArgs, Inputs, Output));
285 }
286
287 /// AIX - AIX tool chain which can call as(1) and ld(1) directly.
AIX(const Driver & D,const llvm::Triple & Triple,const ArgList & Args)288 AIX::AIX(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
289 : ToolChain(D, Triple, Args) {
290 ParseInlineAsmUsingAsmParser = Args.hasFlag(
291 options::OPT_fintegrated_as, options::OPT_fno_integrated_as, true);
292 getLibraryPaths().push_back(getDriver().SysRoot + "/usr/lib");
293 }
294
295 // Returns the effective header sysroot path to use.
296 // This comes from either -isysroot or --sysroot.
297 llvm::StringRef
GetHeaderSysroot(const llvm::opt::ArgList & DriverArgs) const298 AIX::GetHeaderSysroot(const llvm::opt::ArgList &DriverArgs) const {
299 if (DriverArgs.hasArg(options::OPT_isysroot))
300 return DriverArgs.getLastArgValue(options::OPT_isysroot);
301 if (!getDriver().SysRoot.empty())
302 return getDriver().SysRoot;
303 return "/";
304 }
305
AddClangSystemIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const306 void AIX::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
307 ArgStringList &CC1Args) const {
308 // Return if -nostdinc is specified as a driver option.
309 if (DriverArgs.hasArg(options::OPT_nostdinc))
310 return;
311
312 llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs);
313 const Driver &D = getDriver();
314
315 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
316 SmallString<128> P(D.ResourceDir);
317 // Add the PowerPC intrinsic headers (<resource>/include/ppc_wrappers)
318 path::append(P, "include", "ppc_wrappers");
319 addSystemInclude(DriverArgs, CC1Args, P);
320 // Add the Clang builtin headers (<resource>/include)
321 addSystemInclude(DriverArgs, CC1Args, path::parent_path(P.str()));
322 }
323
324 // Return if -nostdlibinc is specified as a driver option.
325 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
326 return;
327
328 // Add <sysroot>/usr/include.
329 SmallString<128> UP(Sysroot);
330 path::append(UP, "/usr/include");
331 addSystemInclude(DriverArgs, CC1Args, UP.str());
332 }
333
AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList & DriverArgs,llvm::opt::ArgStringList & CC1Args) const334 void AIX::AddClangCXXStdlibIncludeArgs(
335 const llvm::opt::ArgList &DriverArgs,
336 llvm::opt::ArgStringList &CC1Args) const {
337
338 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
339 DriverArgs.hasArg(options::OPT_nostdincxx) ||
340 DriverArgs.hasArg(options::OPT_nostdlibinc))
341 return;
342
343 switch (GetCXXStdlibType(DriverArgs)) {
344 case ToolChain::CST_Libstdcxx:
345 llvm::report_fatal_error(
346 "picking up libstdc++ headers is unimplemented on AIX");
347 case ToolChain::CST_Libcxx: {
348 llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs);
349 SmallString<128> PathCPP(Sysroot);
350 llvm::sys::path::append(PathCPP, "opt/IBM/openxlCSDK", "include", "c++",
351 "v1");
352 addSystemInclude(DriverArgs, CC1Args, PathCPP.str());
353 // Required in order to suppress conflicting C++ overloads in the system
354 // libc headers that were used by XL C++.
355 CC1Args.push_back("-D__LIBC_NO_CPP_MATH_OVERLOADS__");
356 return;
357 }
358 }
359
360 llvm_unreachable("Unexpected C++ library type; only libc++ is supported.");
361 }
362
AddCXXStdlibLibArgs(const llvm::opt::ArgList & Args,llvm::opt::ArgStringList & CmdArgs) const363 void AIX::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
364 llvm::opt::ArgStringList &CmdArgs) const {
365 switch (GetCXXStdlibType(Args)) {
366 case ToolChain::CST_Libstdcxx:
367 llvm::report_fatal_error("linking libstdc++ unimplemented on AIX");
368 case ToolChain::CST_Libcxx:
369 CmdArgs.push_back("-lc++");
370 if (Args.hasArg(options::OPT_fexperimental_library))
371 CmdArgs.push_back("-lc++experimental");
372 CmdArgs.push_back("-lc++abi");
373 return;
374 }
375
376 llvm_unreachable("Unexpected C++ library type; only libc++ is supported.");
377 }
378
addProfileRTLibs(const llvm::opt::ArgList & Args,llvm::opt::ArgStringList & CmdArgs) const379 void AIX::addProfileRTLibs(const llvm::opt::ArgList &Args,
380 llvm::opt::ArgStringList &CmdArgs) const {
381 // Add linker option -u__llvm_profile_runtime to cause runtime
382 // initialization to occur.
383 if (needsProfileRT(Args))
384 CmdArgs.push_back(Args.MakeArgString(
385 Twine("-u", llvm::getInstrProfRuntimeHookVarName())));
386 ToolChain::addProfileRTLibs(Args, CmdArgs);
387 }
388
GetDefaultCXXStdlibType() const389 ToolChain::CXXStdlibType AIX::GetDefaultCXXStdlibType() const {
390 return ToolChain::CST_Libcxx;
391 }
392
GetDefaultRuntimeLibType() const393 ToolChain::RuntimeLibType AIX::GetDefaultRuntimeLibType() const {
394 return ToolChain::RLT_CompilerRT;
395 }
396
buildAssembler() const397 auto AIX::buildAssembler() const -> Tool * { return new aix::Assembler(*this); }
398
buildLinker() const399 auto AIX::buildLinker() const -> Tool * { return new aix::Linker(*this); }
400