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 "clang/Driver/CommonArgs.h"
11#include "clang/Driver/Compilation.h"
12#include "clang/Driver/SanitizerArgs.h"
13#include "clang/Options/Options.h"
14#include "llvm/ADT/StringExtras.h"
15#include "llvm/Option/ArgList.h"
16#include "llvm/ProfileData/InstrProf.h"
17#include "llvm/Support/Path.h"
18
19#include <set>
20
21using AIX = clang::driver::toolchains::AIX;
22using namespace clang;
23using namespace clang::driver;
24using namespace clang::driver::tools;
25using namespace clang::driver::toolchains;
26
27using namespace llvm::opt;
28using namespace llvm::sys;
29
30void aix::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
31 const InputInfo &Output,
32 const InputInfoList &Inputs,
33 const ArgList &Args,
34 const char *LinkingOutput) const {
35 const Driver &D = getToolChain().getDriver();
36 ArgStringList CmdArgs;
37
38 const bool IsArch32Bit = getToolChain().getTriple().isArch32Bit();
39 const bool IsArch64Bit = getToolChain().getTriple().isArch64Bit();
40 // Only support 32 and 64 bit.
41 if (!IsArch32Bit && !IsArch64Bit)
42 llvm_unreachable("Unsupported bit width value.");
43
44 if (Arg *A = C.getArgs().getLastArg(Ids: options::OPT_G)) {
45 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
46 << A->getSpelling() << D.getTargetTriple();
47 }
48
49 // Specify the mode in which the as(1) command operates.
50 if (IsArch32Bit) {
51 CmdArgs.push_back(Elt: "-a32");
52 } else {
53 // Must be 64-bit, otherwise asserted already.
54 CmdArgs.push_back(Elt: "-a64");
55 }
56
57 // Accept any mixture of instructions.
58 // On Power for AIX and Linux, this behaviour matches that of GCC for both the
59 // user-provided assembler source case and the compiler-produced assembler
60 // source case. Yet XL with user-provided assembler source would not add this.
61 CmdArgs.push_back(Elt: "-many");
62
63 Args.AddAllArgValues(Output&: CmdArgs, Id0: options::OPT_Wa_COMMA, Id1: options::OPT_Xassembler);
64
65 // Specify assembler output file.
66 assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
67 if (Output.isFilename()) {
68 CmdArgs.push_back(Elt: "-o");
69 CmdArgs.push_back(Elt: Output.getFilename());
70 }
71
72 // Specify assembler input file.
73 // The system assembler on AIX takes exactly one input file. The driver is
74 // expected to invoke as(1) separately for each assembler source input file.
75 if (Inputs.size() != 1)
76 llvm_unreachable("Invalid number of input files.");
77 const InputInfo &II = Inputs[0];
78 assert((II.isFilename() || II.isNothing()) && "Invalid input.");
79 if (II.isFilename())
80 CmdArgs.push_back(Elt: II.getFilename());
81
82 const char *Exec = Args.MakeArgString(Str: getToolChain().GetProgramPath(Name: "as"));
83 C.addCommand(C: std::make_unique<Command>(args: JA, args: *this, args: ResponseFileSupport::None(),
84 args&: Exec, args&: CmdArgs, args: Inputs, args: Output));
85}
86
87// Determine whether there are any linker options that supply an export list
88// (or equivalent information about what to export) being sent to the linker.
89static bool hasExportListLinkerOpts(const ArgStringList &CmdArgs) {
90 for (size_t i = 0, Size = CmdArgs.size(); i < Size; ++i) {
91 llvm::StringRef ArgString(CmdArgs[i]);
92
93 if (ArgString.starts_with(Prefix: "-bE:") || ArgString.starts_with(Prefix: "-bexport:") ||
94 ArgString == "-bexpall" || ArgString == "-bexpfull")
95 return true;
96
97 // If we split -b option, check the next opt.
98 if (ArgString == "-b" && i + 1 < Size) {
99 ++i;
100 llvm::StringRef ArgNextString(CmdArgs[i]);
101 if (ArgNextString.starts_with(Prefix: "E:") ||
102 ArgNextString.starts_with(Prefix: "export:") || ArgNextString == "expall" ||
103 ArgNextString == "expfull")
104 return true;
105 }
106 }
107 return false;
108}
109
110void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA,
111 const InputInfo &Output,
112 const InputInfoList &Inputs, const ArgList &Args,
113 const char *LinkingOutput) const {
114 const AIX &ToolChain = static_cast<const AIX &>(getToolChain());
115 const Driver &D = ToolChain.getDriver();
116 ArgStringList CmdArgs;
117
118 const bool IsArch32Bit = ToolChain.getTriple().isArch32Bit();
119 const bool IsArch64Bit = ToolChain.getTriple().isArch64Bit();
120 // Only support 32 and 64 bit.
121 if (!(IsArch32Bit || IsArch64Bit))
122 llvm_unreachable("Unsupported bit width value.");
123
124 if (Arg *A = C.getArgs().getLastArg(Ids: options::OPT_G)) {
125 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
126 << A->getSpelling() << D.getTargetTriple();
127 }
128
129 // Force static linking when "-static" is present.
130 if (Args.hasArg(Ids: options::OPT_static))
131 CmdArgs.push_back(Elt: "-bnso");
132
133 // Add options for shared libraries.
134 if (Args.hasArg(Ids: options::OPT_shared)) {
135 CmdArgs.push_back(Elt: "-bM:SRE");
136 CmdArgs.push_back(Elt: "-bnoentry");
137 }
138
139 if (Args.hasFlag(Pos: options::OPT_mxcoff_roptr, Neg: options::OPT_mno_xcoff_roptr,
140 Default: false)) {
141 if (Args.hasArg(Ids: options::OPT_shared))
142 D.Diag(DiagID: diag::err_roptr_cannot_build_shared);
143
144 // The `-mxcoff-roptr` option places constants in RO sections as much as
145 // possible. Then `-bforceimprw` changes such sections to RW if they contain
146 // imported symbols that need to be resolved.
147 CmdArgs.push_back(Elt: "-bforceimprw");
148 }
149
150 // PGO and ifunc support depends on the named sections linker feature that is
151 // available on AIX 7.2 TL5 SP5 onwards.
152 if (ToolChain.getTriple().getOSMajorVersion() == 0 ||
153 ToolChain.getTriple().getOSVersion() >= VersionTuple(7, 2))
154 CmdArgs.push_back(Elt: "-bdbg:namedsects:ss");
155
156 if (Arg *A = Args.getLastArg(Ids: options::OPT_mxcoff_build_id_EQ)) {
157 StringRef BuildId = A->getValue();
158 if (BuildId[0] != '0' || BuildId[1] != 'x' ||
159 BuildId.find_if_not(F: llvm::isHexDigit, From: 2) != StringRef::npos)
160 ToolChain.getDriver().Diag(DiagID: diag::err_drv_unsupported_option_argument)
161 << A->getSpelling() << BuildId;
162 else {
163 std::string LinkerFlag = "-bdbg:ldrinfo:xcoff_binary_id:0x";
164 if (BuildId.size() % 2) // Prepend a 0 if odd number of digits.
165 LinkerFlag += "0";
166 LinkerFlag += BuildId.drop_front(N: 2).lower();
167 CmdArgs.push_back(Elt: Args.MakeArgString(Str: LinkerFlag));
168 }
169 }
170
171 // Specify linker output file.
172 assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
173 if (Output.isFilename()) {
174 CmdArgs.push_back(Elt: "-o");
175 CmdArgs.push_back(Elt: Output.getFilename());
176 }
177
178 // Set linking mode (i.e., 32/64-bit) and the address of
179 // text and data sections based on arch bit width.
180 if (IsArch32Bit) {
181 CmdArgs.push_back(Elt: "-b32");
182 CmdArgs.push_back(Elt: "-bpT:0x10000000");
183 CmdArgs.push_back(Elt: "-bpD:0x20000000");
184 } else {
185 // Must be 64-bit, otherwise asserted already.
186 CmdArgs.push_back(Elt: "-b64");
187 CmdArgs.push_back(Elt: "-bpT:0x100000000");
188 CmdArgs.push_back(Elt: "-bpD:0x110000000");
189 }
190
191 if (!Args.hasArg(Ids: options::OPT_nostdlib, Ids: options::OPT_nostartfiles,
192 Ids: options::OPT_shared, Ids: options::OPT_r)) {
193 auto getCrt0Basename = [&Args, IsArch32Bit] {
194 if (Arg *A = Args.getLastArgNoClaim(Ids: options::OPT_p, Ids: options::OPT_pg)) {
195 // Enable gprofiling when "-pg" is specified.
196 if (A->getOption().matches(ID: options::OPT_pg))
197 return IsArch32Bit ? "gcrt0.o" : "gcrt0_64.o";
198 // Enable profiling when "-p" is specified.
199 return IsArch32Bit ? "mcrt0.o" : "mcrt0_64.o";
200 }
201 return IsArch32Bit ? "crt0.o" : "crt0_64.o";
202 };
203
204 CmdArgs.push_back(
205 Elt: Args.MakeArgString(Str: ToolChain.GetFilePath(Name: getCrt0Basename())));
206
207 CmdArgs.push_back(Elt: Args.MakeArgString(
208 Str: ToolChain.GetFilePath(Name: IsArch32Bit ? "crti.o" : "crti_64.o")));
209 }
210
211 // Collect all static constructor and destructor functions in both C and CXX
212 // language link invocations. This has to come before AddLinkerInputs as the
213 // implied option needs to precede any other '-bcdtors' settings or
214 // '-bnocdtors' that '-Wl' might forward.
215 CmdArgs.push_back(Elt: "-bcdtors:all:0:s");
216
217 if (Args.hasArg(Ids: options::OPT_rpath)) {
218 for (const auto &bopt : Args.getAllArgValues(Id: options::OPT_b))
219 // Check -b opts prefix for "libpath:" or exact match for "nolibpath"
220 if (!bopt.rfind(s: "libpath:", pos: 0) || bopt == "nolibpath")
221 D.Diag(DiagID: diag::err_drv_cannot_mix_options) << "-rpath" << "-b" + bopt;
222
223 for (const auto &wlopt : Args.getAllArgValues(Id: options::OPT_Wl_COMMA))
224 // Check -Wl, opts prefix for "-blibpath:" or exact match for
225 // "-bnolibpath"
226 if (!wlopt.rfind(s: "-blibpath:", pos: 0) || wlopt == "-bnolibpath")
227 D.Diag(DiagID: diag::err_drv_cannot_mix_options) << "-rpath" << "-Wl," + wlopt;
228
229 for (const auto &xopt : Args.getAllArgValues(Id: options::OPT_Xlinker))
230 // Check -Xlinker opts prefix for "-blibpath:" or exact match for
231 // "-bnolibpath"
232 if (!xopt.rfind(s: "-blibpath:", pos: 0) || xopt == "-bnolibpath")
233 D.Diag(DiagID: diag::err_drv_cannot_mix_options)
234 << "-rpath" << "-Xlinker " + xopt;
235
236 std::string BlibPathStr = "";
237 for (const auto &dir : Args.getAllArgValues(Id: options::OPT_rpath))
238 BlibPathStr += dir + ":";
239 BlibPathStr += "/usr/lib:/lib";
240 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine("-blibpath:") + BlibPathStr));
241 }
242
243 // Specify linker input file(s).
244 AddLinkerInputs(TC: ToolChain, Inputs, Args, CmdArgs, JA);
245
246 if (D.isUsingLTO())
247 addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs,
248 IsThinLTO: D.getLTOMode() == LTOK_Thin);
249
250 if (Args.hasArg(Ids: options::OPT_shared) && !hasExportListLinkerOpts(CmdArgs)) {
251
252 const char *CreateExportListExec = Args.MakeArgString(
253 Str: path::parent_path(path: ToolChain.getDriver().ClangExecutable) +
254 "/llvm-nm");
255 ArgStringList CreateExportCmdArgs;
256
257 std::string CreateExportListPath =
258 C.getDriver().GetTemporaryPath(Prefix: "CreateExportList", Suffix: "exp");
259 const char *ExportList =
260 C.addTempFile(Name: C.getArgs().MakeArgString(Str: CreateExportListPath));
261
262 for (const auto &II : Inputs)
263 if (II.isFilename())
264 CreateExportCmdArgs.push_back(Elt: II.getFilename());
265
266 CreateExportCmdArgs.push_back(Elt: "--export-symbols");
267 CreateExportCmdArgs.push_back(Elt: "-X");
268 if (IsArch32Bit) {
269 CreateExportCmdArgs.push_back(Elt: "32");
270 } else {
271 // Must be 64-bit, otherwise asserted already.
272 CreateExportCmdArgs.push_back(Elt: "64");
273 }
274
275 auto ExpCommand = std::make_unique<Command>(
276 args: JA, args: *this, args: ResponseFileSupport::None(), args&: CreateExportListExec,
277 args&: CreateExportCmdArgs, args: Inputs, args: Output);
278 ExpCommand->setRedirectFiles(
279 {std::nullopt, std::string(ExportList), std::nullopt});
280 C.addCommand(C: std::move(ExpCommand));
281 CmdArgs.push_back(Elt: Args.MakeArgString(Str: llvm::Twine("-bE:") + ExportList));
282 }
283
284 // Add directory to library search path.
285 Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_L);
286 if (!Args.hasArg(Ids: options::OPT_r)) {
287 ToolChain.AddFilePathLibArgs(Args, CmdArgs);
288 ToolChain.addProfileRTLibs(Args, CmdArgs);
289
290 if (getToolChain().ShouldLinkCXXStdlib(Args))
291 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
292
293 if (!Args.hasArg(Ids: options::OPT_nostdlib, Ids: options::OPT_nodefaultlibs)) {
294 AddRunTimeLibs(TC: ToolChain, D, CmdArgs, Args);
295
296 // Add OpenMP runtime if -fopenmp is specified.
297 if (Args.hasFlag(Pos: options::OPT_fopenmp, PosAlias: options::OPT_fopenmp_EQ,
298 Neg: options::OPT_fno_openmp, Default: false)) {
299 switch (ToolChain.getDriver().getOpenMPRuntime(Args)) {
300 case Driver::OMPRT_OMP:
301 CmdArgs.push_back(Elt: "-lomp");
302 break;
303 case Driver::OMPRT_IOMP5:
304 CmdArgs.push_back(Elt: "-liomp5");
305 break;
306 case Driver::OMPRT_GOMP:
307 CmdArgs.push_back(Elt: "-lgomp");
308 break;
309 case Driver::OMPRT_Unknown:
310 // Already diagnosed.
311 break;
312 }
313 // libpthreads is required for -fopenmp.
314 CmdArgs.push_back(Elt: "-lpthreads");
315 }
316
317 // Support POSIX threads if "-pthreads" or "-pthread" is present.
318 if (Args.hasArg(Ids: options::OPT_pthreads, Ids: options::OPT_pthread))
319 CmdArgs.push_back(Elt: "-lpthreads");
320
321 if (D.CCCIsCXX())
322 CmdArgs.push_back(Elt: "-lm");
323
324 CmdArgs.push_back(Elt: "-lc");
325
326 if (Args.hasArgNoClaim(Ids: options::OPT_p, Ids: options::OPT_pg)) {
327 CmdArgs.push_back(Elt: Args.MakeArgString(Str: (llvm::Twine("-L") + D.SysRoot) +
328 "/lib/profiled"));
329 CmdArgs.push_back(Elt: Args.MakeArgString(Str: (llvm::Twine("-L") + D.SysRoot) +
330 "/usr/lib/profiled"));
331 }
332 }
333 }
334
335 if (D.IsFlangMode() &&
336 !Args.hasArg(Ids: options::OPT_nostdlib, Ids: options::OPT_nodefaultlibs)) {
337 ToolChain.addFortranRuntimeLibraryPath(Args, CmdArgs);
338 ToolChain.addFortranRuntimeLibs(Args, CmdArgs);
339 CmdArgs.push_back(Elt: "-lm");
340 CmdArgs.push_back(Elt: "-lpthread");
341 }
342 const char *Exec = Args.MakeArgString(Str: ToolChain.GetLinkerPath());
343 C.addCommand(C: std::make_unique<Command>(args: JA, args: *this, args: ResponseFileSupport::None(),
344 args&: Exec, args&: CmdArgs, args: Inputs, args: Output));
345}
346
347/// AIX - AIX tool chain which can call as(1) and ld(1) directly.
348AIX::AIX(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
349 : ToolChain(D, Triple, Args) {
350 getProgramPaths().push_back(Elt: getDriver().Dir);
351
352 ParseInlineAsmUsingAsmParser = Args.hasFlag(
353 Pos: options::OPT_fintegrated_as, Neg: options::OPT_fno_integrated_as, Default: true);
354 getLibraryPaths().push_back(Elt: getDriver().SysRoot + "/usr/lib");
355
356 // FilePaths gets System Paths for -print-search-dirs
357 getFilePaths().push_back(Elt: getDriver().SysRoot + "/usr/lib");
358 getFilePaths().push_back(Elt: getDriver().SysRoot + "/lib");
359}
360
361// Returns the effective header sysroot path to use.
362// This comes from either -isysroot or --sysroot.
363llvm::StringRef
364AIX::GetHeaderSysroot(const llvm::opt::ArgList &DriverArgs) const {
365 if (DriverArgs.hasArg(Ids: options::OPT_isysroot))
366 return DriverArgs.getLastArgValue(Id: options::OPT_isysroot);
367 if (!getDriver().SysRoot.empty())
368 return getDriver().SysRoot;
369 return "/";
370}
371
372void AIX::AddOpenMPIncludeArgs(const ArgList &DriverArgs,
373 ArgStringList &CC1Args) const {
374 // Add OpenMP include paths if -fopenmp is specified.
375 if (DriverArgs.hasFlag(Pos: options::OPT_fopenmp, PosAlias: options::OPT_fopenmp_EQ,
376 Neg: options::OPT_fno_openmp, Default: false)) {
377 SmallString<128> PathOpenMP;
378 switch (getDriver().getOpenMPRuntime(Args: DriverArgs)) {
379 case Driver::OMPRT_OMP:
380 PathOpenMP = GetHeaderSysroot(DriverArgs);
381 llvm::sys::path::append(path&: PathOpenMP, a: "opt/IBM/openxlCSDK", b: "include",
382 c: "openmp");
383 addSystemInclude(DriverArgs, CC1Args, Path: PathOpenMP.str());
384 break;
385 case Driver::OMPRT_IOMP5:
386 case Driver::OMPRT_GOMP:
387 case Driver::OMPRT_Unknown:
388 // Unknown / unsupported include paths.
389 break;
390 }
391 }
392}
393
394void AIX::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
395 ArgStringList &CC1Args) const {
396 // Return if -nostdinc is specified as a driver option.
397 if (DriverArgs.hasArg(Ids: options::OPT_nostdinc))
398 return;
399
400 llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs);
401 const Driver &D = getDriver();
402
403 if (!DriverArgs.hasArg(Ids: options::OPT_nobuiltininc)) {
404 SmallString<128> P(D.ResourceDir);
405 // Add the PowerPC intrinsic headers (<resource>/include/ppc_wrappers)
406 path::append(path&: P, a: "include", b: "ppc_wrappers");
407 addSystemInclude(DriverArgs, CC1Args, Path: P);
408 // Add the Clang builtin headers (<resource>/include)
409 addSystemInclude(DriverArgs, CC1Args, Path: path::parent_path(path: P.str()));
410 }
411
412 // Add the include directory containing omp.h. This needs to be before
413 // adding the system include directory because other compilers put their
414 // omp.h in /usr/include.
415 AddOpenMPIncludeArgs(DriverArgs, CC1Args);
416
417 // Return if -nostdlibinc is specified as a driver option.
418 if (DriverArgs.hasArg(Ids: options::OPT_nostdlibinc))
419 return;
420
421 // Add <sysroot>/usr/include.
422 SmallString<128> UP(Sysroot);
423 path::append(path&: UP, a: "/usr/include");
424 addSystemInclude(DriverArgs, CC1Args, Path: UP.str());
425}
426
427void AIX::AddClangCXXStdlibIncludeArgs(
428 const llvm::opt::ArgList &DriverArgs,
429 llvm::opt::ArgStringList &CC1Args) const {
430
431 if (DriverArgs.hasArg(Ids: options::OPT_nostdinc) ||
432 DriverArgs.hasArg(Ids: options::OPT_nostdincxx) ||
433 DriverArgs.hasArg(Ids: options::OPT_nostdlibinc))
434 return;
435
436 switch (GetCXXStdlibType(Args: DriverArgs)) {
437 case ToolChain::CST_Libstdcxx:
438 llvm::report_fatal_error(
439 reason: "picking up libstdc++ headers is unimplemented on AIX");
440 case ToolChain::CST_Libcxx: {
441 llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs);
442 SmallString<128> PathCPP(Sysroot);
443 llvm::sys::path::append(path&: PathCPP, a: "opt/IBM/openxlCSDK", b: "include", c: "c++",
444 d: "v1");
445 addSystemInclude(DriverArgs, CC1Args, Path: PathCPP.str());
446 // Required in order to suppress conflicting C++ overloads in the system
447 // libc headers that were used by XL C++.
448 CC1Args.push_back(Elt: "-D__LIBC_NO_CPP_MATH_OVERLOADS__");
449 return;
450 }
451 }
452
453 llvm_unreachable("Unexpected C++ library type; only libc++ is supported.");
454}
455
456void AIX::AddFilePathLibArgs(const llvm::opt::ArgList &Args,
457 llvm::opt::ArgStringList &CmdArgs) const {
458 // AIX linker searches /usr/lib and /lib by default. Don't add them as -L
459 // flags to avoid duplicates. But keep them in FilePaths for
460 // -print-search-dirs
461 for (const auto &LibPath : getFilePaths()) {
462 if (LibPath.length() > 0 && LibPath != getDriver().SysRoot + "/usr/lib" &&
463 LibPath != getDriver().SysRoot + "/lib") {
464 CmdArgs.push_back(Elt: Args.MakeArgString(Str: StringRef("-L") + LibPath));
465 }
466 }
467}
468
469void AIX::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
470 llvm::opt::ArgStringList &CmdArgs) const {
471 switch (GetCXXStdlibType(Args)) {
472 case ToolChain::CST_Libstdcxx:
473 llvm::report_fatal_error(reason: "linking libstdc++ unimplemented on AIX");
474 case ToolChain::CST_Libcxx:
475 CmdArgs.push_back(Elt: "-lc++");
476 if (Args.hasArg(Ids: options::OPT_fexperimental_library))
477 CmdArgs.push_back(Elt: "-lc++experimental");
478 CmdArgs.push_back(Elt: "-lc++abi");
479 return;
480 }
481
482 llvm_unreachable("Unexpected C++ library type; only libc++ is supported.");
483}
484
485// This function processes all the mtocdata options to build the final
486// simplified toc data options to pass to CC1.
487static void addTocDataOptions(const llvm::opt::ArgList &Args,
488 llvm::opt::ArgStringList &CC1Args,
489 const Driver &D) {
490
491 // Check the global toc-data setting. The default is -mno-tocdata.
492 // To enable toc-data globally, -mtocdata must be specified.
493 // Additionally, it must be last to take effect.
494 const bool TOCDataGloballyinEffect = [&Args]() {
495 if (const Arg *LastArg =
496 Args.getLastArg(Ids: options::OPT_mtocdata, Ids: options::OPT_mno_tocdata))
497 return LastArg->getOption().matches(ID: options::OPT_mtocdata);
498 else
499 return false;
500 }();
501
502 enum TOCDataSetting {
503 AddressInTOC = 0, // Address of the symbol stored in the TOC.
504 DataInTOC = 1 // Symbol defined in the TOC.
505 };
506
507 const TOCDataSetting DefaultTocDataSetting =
508 TOCDataGloballyinEffect ? DataInTOC : AddressInTOC;
509
510 // Process the list of variables in the explicitly specified options
511 // -mtocdata= and -mno-tocdata= to see which variables are opposite to
512 // the global setting of tocdata in TOCDataGloballyinEffect.
513 // Those that have the opposite setting to TOCDataGloballyinEffect, are added
514 // to ExplicitlySpecifiedGlobals.
515 std::set<llvm::StringRef> ExplicitlySpecifiedGlobals;
516 for (const auto Arg :
517 Args.filtered(Ids: options::OPT_mtocdata_EQ, Ids: options::OPT_mno_tocdata_EQ)) {
518 TOCDataSetting ArgTocDataSetting =
519 Arg->getOption().matches(ID: options::OPT_mtocdata_EQ) ? DataInTOC
520 : AddressInTOC;
521
522 if (ArgTocDataSetting != DefaultTocDataSetting)
523 for (const char *Val : Arg->getValues())
524 ExplicitlySpecifiedGlobals.insert(x: Val);
525 else
526 for (const char *Val : Arg->getValues())
527 ExplicitlySpecifiedGlobals.erase(x: Val);
528 }
529
530 auto buildExceptionList = [](const std::set<llvm::StringRef> &ExplicitValues,
531 const char *OptionSpelling) {
532 std::string Option(OptionSpelling);
533 bool IsFirst = true;
534 for (const auto &E : ExplicitValues) {
535 if (!IsFirst)
536 Option += ",";
537
538 IsFirst = false;
539 Option += E.str();
540 }
541 return Option;
542 };
543
544 // Pass the final tocdata options to CC1 consisting of the default
545 // tocdata option (-mtocdata/-mno-tocdata) along with the list
546 // option (-mno-tocdata=/-mtocdata=) if there are any explicitly specified
547 // variables which would be exceptions to the default setting.
548 const char *TocDataGlobalOption =
549 TOCDataGloballyinEffect ? "-mtocdata" : "-mno-tocdata";
550 CC1Args.push_back(Elt: TocDataGlobalOption);
551
552 const char *TocDataListOption =
553 TOCDataGloballyinEffect ? "-mno-tocdata=" : "-mtocdata=";
554 if (!ExplicitlySpecifiedGlobals.empty())
555 CC1Args.push_back(Elt: Args.MakeArgString(Str: llvm::Twine(
556 buildExceptionList(ExplicitlySpecifiedGlobals, TocDataListOption))));
557}
558
559void AIX::addClangTargetOptions(
560 const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CC1Args,
561 Action::OffloadKind DeviceOffloadingKind) const {
562 Args.AddLastArg(Output&: CC1Args, Ids: options::OPT_mignore_xcoff_visibility);
563 Args.AddLastArg(Output&: CC1Args, Ids: options::OPT_mdefault_visibility_export_mapping_EQ);
564 Args.addOptInFlag(Output&: CC1Args, Pos: options::OPT_mxcoff_roptr, Neg: options::OPT_mno_xcoff_roptr);
565
566 // Forward last mtocdata/mno_tocdata options to -cc1.
567 if (Args.hasArg(Ids: options::OPT_mtocdata_EQ, Ids: options::OPT_mno_tocdata_EQ,
568 Ids: options::OPT_mtocdata))
569 addTocDataOptions(Args, CC1Args, D: getDriver());
570
571 if (Args.hasArg(Ids: options::OPT_msave_reg_params))
572 CC1Args.push_back(Elt: "-msave-reg-params");
573
574 if (Args.hasFlag(Pos: options::OPT_fxl_pragma_pack,
575 Neg: options::OPT_fno_xl_pragma_pack, Default: true))
576 CC1Args.push_back(Elt: "-fxl-pragma-pack");
577
578 // Pass "-fno-sized-deallocation" only when the user hasn't manually enabled
579 // or disabled sized deallocations.
580 if (!Args.getLastArgNoClaim(Ids: options::OPT_fsized_deallocation,
581 Ids: options::OPT_fno_sized_deallocation))
582 CC1Args.push_back(Elt: "-fno-sized-deallocation");
583}
584
585void AIX::addProfileRTLibs(const llvm::opt::ArgList &Args,
586 llvm::opt::ArgStringList &CmdArgs) const {
587 if (needsProfileRT(Args)) {
588 // Add linker option -u__llvm_profile_runtime to cause runtime
589 // initialization to occur.
590 CmdArgs.push_back(Elt: Args.MakeArgString(
591 Str: Twine("-u", llvm::getInstrProfRuntimeHookVarName())));
592
593 if (const auto *A =
594 Args.getLastArgNoClaim(Ids: options::OPT_fprofile_update_EQ)) {
595 StringRef Val = A->getValue();
596 if (Val == "atomic" || Val == "prefer-atomic")
597 CmdArgs.push_back(Elt: "-latomic");
598 }
599 }
600
601 ToolChain::addProfileRTLibs(Args, CmdArgs);
602}
603
604ToolChain::CXXStdlibType AIX::GetDefaultCXXStdlibType() const {
605 return ToolChain::CST_Libcxx;
606}
607
608ToolChain::RuntimeLibType AIX::GetDefaultRuntimeLibType() const {
609 return ToolChain::RLT_CompilerRT;
610}
611
612auto AIX::buildAssembler() const -> Tool * { return new aix::Assembler(*this); }
613
614auto AIX::buildLinker() const -> Tool * { return new aix::Linker(*this); }
615