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 }
314
315 // Support POSIX threads if "-pthreads" or "-pthread" is present.
316 if (Args.hasArg(Ids: options::OPT_pthreads, Ids: options::OPT_pthread))
317 CmdArgs.push_back(Elt: "-lpthreads");
318
319 if (D.CCCIsCXX())
320 CmdArgs.push_back(Elt: "-lm");
321
322 CmdArgs.push_back(Elt: "-lc");
323
324 if (Args.hasArgNoClaim(Ids: options::OPT_p, Ids: options::OPT_pg)) {
325 CmdArgs.push_back(Elt: Args.MakeArgString(Str: (llvm::Twine("-L") + D.SysRoot) +
326 "/lib/profiled"));
327 CmdArgs.push_back(Elt: Args.MakeArgString(Str: (llvm::Twine("-L") + D.SysRoot) +
328 "/usr/lib/profiled"));
329 }
330 }
331 }
332
333 if (D.IsFlangMode() &&
334 !Args.hasArg(Ids: options::OPT_nostdlib, Ids: options::OPT_nodefaultlibs)) {
335 ToolChain.addFortranRuntimeLibraryPath(Args, CmdArgs);
336 ToolChain.addFortranRuntimeLibs(Args, CmdArgs);
337 CmdArgs.push_back(Elt: "-lm");
338 CmdArgs.push_back(Elt: "-lpthread");
339 }
340 const char *Exec = Args.MakeArgString(Str: ToolChain.GetLinkerPath());
341 C.addCommand(C: std::make_unique<Command>(args: JA, args: *this, args: ResponseFileSupport::None(),
342 args&: Exec, args&: CmdArgs, args: Inputs, args: Output));
343}
344
345/// AIX - AIX tool chain which can call as(1) and ld(1) directly.
346AIX::AIX(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
347 : ToolChain(D, Triple, Args) {
348 getProgramPaths().push_back(Elt: getDriver().Dir);
349
350 ParseInlineAsmUsingAsmParser = Args.hasFlag(
351 Pos: options::OPT_fintegrated_as, Neg: options::OPT_fno_integrated_as, Default: true);
352 getLibraryPaths().push_back(Elt: getDriver().SysRoot + "/usr/lib");
353
354 // FilePaths gets System Paths for -print-search-dirs
355 getFilePaths().push_back(Elt: getDriver().SysRoot + "/usr/lib");
356 getFilePaths().push_back(Elt: getDriver().SysRoot + "/lib");
357}
358
359// Returns the effective header sysroot path to use.
360// This comes from either -isysroot or --sysroot.
361llvm::StringRef
362AIX::GetHeaderSysroot(const llvm::opt::ArgList &DriverArgs) const {
363 if (DriverArgs.hasArg(Ids: options::OPT_isysroot))
364 return DriverArgs.getLastArgValue(Id: options::OPT_isysroot);
365 if (!getDriver().SysRoot.empty())
366 return getDriver().SysRoot;
367 return "/";
368}
369
370void AIX::AddOpenMPIncludeArgs(const ArgList &DriverArgs,
371 ArgStringList &CC1Args) const {
372 // Add OpenMP include paths if -fopenmp is specified.
373 if (DriverArgs.hasFlag(Pos: options::OPT_fopenmp, PosAlias: options::OPT_fopenmp_EQ,
374 Neg: options::OPT_fno_openmp, Default: false)) {
375 SmallString<128> PathOpenMP;
376 switch (getDriver().getOpenMPRuntime(Args: DriverArgs)) {
377 case Driver::OMPRT_OMP:
378 PathOpenMP = GetHeaderSysroot(DriverArgs);
379 llvm::sys::path::append(path&: PathOpenMP, a: "opt/IBM/openxlCSDK", b: "include",
380 c: "openmp");
381 addSystemInclude(DriverArgs, CC1Args, Path: PathOpenMP.str());
382 break;
383 case Driver::OMPRT_IOMP5:
384 case Driver::OMPRT_GOMP:
385 case Driver::OMPRT_Unknown:
386 // Unknown / unsupported include paths.
387 break;
388 }
389 }
390}
391
392void AIX::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
393 ArgStringList &CC1Args) const {
394 // Return if -nostdinc is specified as a driver option.
395 if (DriverArgs.hasArg(Ids: options::OPT_nostdinc))
396 return;
397
398 llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs);
399 const Driver &D = getDriver();
400
401 if (!DriverArgs.hasArg(Ids: options::OPT_nobuiltininc)) {
402 SmallString<128> P(D.ResourceDir);
403 // Add the PowerPC intrinsic headers (<resource>/include/ppc_wrappers)
404 path::append(path&: P, a: "include", b: "ppc_wrappers");
405 addSystemInclude(DriverArgs, CC1Args, Path: P);
406 // Add the Clang builtin headers (<resource>/include)
407 addSystemInclude(DriverArgs, CC1Args, Path: path::parent_path(path: P.str()));
408 }
409
410 // Add the include directory containing omp.h. This needs to be before
411 // adding the system include directory because other compilers put their
412 // omp.h in /usr/include.
413 AddOpenMPIncludeArgs(DriverArgs, CC1Args);
414
415 // Return if -nostdlibinc is specified as a driver option.
416 if (DriverArgs.hasArg(Ids: options::OPT_nostdlibinc))
417 return;
418
419 // Add <sysroot>/usr/include.
420 SmallString<128> UP(Sysroot);
421 path::append(path&: UP, a: "/usr/include");
422 addSystemInclude(DriverArgs, CC1Args, Path: UP.str());
423}
424
425void AIX::AddClangCXXStdlibIncludeArgs(
426 const llvm::opt::ArgList &DriverArgs,
427 llvm::opt::ArgStringList &CC1Args) const {
428
429 if (DriverArgs.hasArg(Ids: options::OPT_nostdinc) ||
430 DriverArgs.hasArg(Ids: options::OPT_nostdincxx) ||
431 DriverArgs.hasArg(Ids: options::OPT_nostdlibinc))
432 return;
433
434 switch (GetCXXStdlibType(Args: DriverArgs)) {
435 case ToolChain::CST_Libstdcxx:
436 llvm::report_fatal_error(
437 reason: "picking up libstdc++ headers is unimplemented on AIX");
438 case ToolChain::CST_Libcxx: {
439 llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs);
440 SmallString<128> PathCPP(Sysroot);
441 llvm::sys::path::append(path&: PathCPP, a: "opt/IBM/openxlCSDK", b: "include", c: "c++",
442 d: "v1");
443 addSystemInclude(DriverArgs, CC1Args, Path: PathCPP.str());
444 // Required in order to suppress conflicting C++ overloads in the system
445 // libc headers that were used by XL C++.
446 CC1Args.push_back(Elt: "-D__LIBC_NO_CPP_MATH_OVERLOADS__");
447 return;
448 }
449 }
450
451 llvm_unreachable("Unexpected C++ library type; only libc++ is supported.");
452}
453
454void AIX::AddFilePathLibArgs(const llvm::opt::ArgList &Args,
455 llvm::opt::ArgStringList &CmdArgs) const {
456 // AIX linker searches /usr/lib and /lib by default. Don't add them as -L
457 // flags to avoid duplicates. But keep them in FilePaths for
458 // -print-search-dirs
459 for (const auto &LibPath : getFilePaths()) {
460 if (LibPath.length() > 0 && LibPath != getDriver().SysRoot + "/usr/lib" &&
461 LibPath != getDriver().SysRoot + "/lib") {
462 CmdArgs.push_back(Elt: Args.MakeArgString(Str: StringRef("-L") + LibPath));
463 }
464 }
465}
466
467void AIX::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
468 llvm::opt::ArgStringList &CmdArgs) const {
469 switch (GetCXXStdlibType(Args)) {
470 case ToolChain::CST_Libstdcxx:
471 llvm::report_fatal_error(reason: "linking libstdc++ unimplemented on AIX");
472 case ToolChain::CST_Libcxx:
473 CmdArgs.push_back(Elt: "-lc++");
474 if (Args.hasArg(Ids: options::OPT_fexperimental_library))
475 CmdArgs.push_back(Elt: "-lc++experimental");
476 CmdArgs.push_back(Elt: "-lc++abi");
477 return;
478 }
479
480 llvm_unreachable("Unexpected C++ library type; only libc++ is supported.");
481}
482
483// This function processes all the mtocdata options to build the final
484// simplified toc data options to pass to CC1.
485static void addTocDataOptions(const llvm::opt::ArgList &Args,
486 llvm::opt::ArgStringList &CC1Args,
487 const Driver &D) {
488
489 // Check the global toc-data setting. The default is -mno-tocdata.
490 // To enable toc-data globally, -mtocdata must be specified.
491 // Additionally, it must be last to take effect.
492 const bool TOCDataGloballyinEffect = [&Args]() {
493 if (const Arg *LastArg =
494 Args.getLastArg(Ids: options::OPT_mtocdata, Ids: options::OPT_mno_tocdata))
495 return LastArg->getOption().matches(ID: options::OPT_mtocdata);
496 else
497 return false;
498 }();
499
500 enum TOCDataSetting {
501 AddressInTOC = 0, // Address of the symbol stored in the TOC.
502 DataInTOC = 1 // Symbol defined in the TOC.
503 };
504
505 const TOCDataSetting DefaultTocDataSetting =
506 TOCDataGloballyinEffect ? DataInTOC : AddressInTOC;
507
508 // Process the list of variables in the explicitly specified options
509 // -mtocdata= and -mno-tocdata= to see which variables are opposite to
510 // the global setting of tocdata in TOCDataGloballyinEffect.
511 // Those that have the opposite setting to TOCDataGloballyinEffect, are added
512 // to ExplicitlySpecifiedGlobals.
513 std::set<llvm::StringRef> ExplicitlySpecifiedGlobals;
514 for (const auto Arg :
515 Args.filtered(Ids: options::OPT_mtocdata_EQ, Ids: options::OPT_mno_tocdata_EQ)) {
516 TOCDataSetting ArgTocDataSetting =
517 Arg->getOption().matches(ID: options::OPT_mtocdata_EQ) ? DataInTOC
518 : AddressInTOC;
519
520 if (ArgTocDataSetting != DefaultTocDataSetting)
521 for (const char *Val : Arg->getValues())
522 ExplicitlySpecifiedGlobals.insert(x: Val);
523 else
524 for (const char *Val : Arg->getValues())
525 ExplicitlySpecifiedGlobals.erase(x: Val);
526 }
527
528 auto buildExceptionList = [](const std::set<llvm::StringRef> &ExplicitValues,
529 const char *OptionSpelling) {
530 std::string Option(OptionSpelling);
531 bool IsFirst = true;
532 for (const auto &E : ExplicitValues) {
533 if (!IsFirst)
534 Option += ",";
535
536 IsFirst = false;
537 Option += E.str();
538 }
539 return Option;
540 };
541
542 // Pass the final tocdata options to CC1 consisting of the default
543 // tocdata option (-mtocdata/-mno-tocdata) along with the list
544 // option (-mno-tocdata=/-mtocdata=) if there are any explicitly specified
545 // variables which would be exceptions to the default setting.
546 const char *TocDataGlobalOption =
547 TOCDataGloballyinEffect ? "-mtocdata" : "-mno-tocdata";
548 CC1Args.push_back(Elt: TocDataGlobalOption);
549
550 const char *TocDataListOption =
551 TOCDataGloballyinEffect ? "-mno-tocdata=" : "-mtocdata=";
552 if (!ExplicitlySpecifiedGlobals.empty())
553 CC1Args.push_back(Elt: Args.MakeArgString(Str: llvm::Twine(
554 buildExceptionList(ExplicitlySpecifiedGlobals, TocDataListOption))));
555}
556
557void AIX::addClangTargetOptions(
558 const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CC1Args,
559 Action::OffloadKind DeviceOffloadingKind) const {
560 Args.AddLastArg(Output&: CC1Args, Ids: options::OPT_mignore_xcoff_visibility);
561 Args.AddLastArg(Output&: CC1Args, Ids: options::OPT_mdefault_visibility_export_mapping_EQ);
562 Args.addOptInFlag(Output&: CC1Args, Pos: options::OPT_mxcoff_roptr, Neg: options::OPT_mno_xcoff_roptr);
563
564 // Forward last mtocdata/mno_tocdata options to -cc1.
565 if (Args.hasArg(Ids: options::OPT_mtocdata_EQ, Ids: options::OPT_mno_tocdata_EQ,
566 Ids: options::OPT_mtocdata))
567 addTocDataOptions(Args, CC1Args, D: getDriver());
568
569 if (Args.hasArg(Ids: options::OPT_msave_reg_params))
570 CC1Args.push_back(Elt: "-msave-reg-params");
571
572 if (Args.hasFlag(Pos: options::OPT_fxl_pragma_pack,
573 Neg: options::OPT_fno_xl_pragma_pack, Default: true))
574 CC1Args.push_back(Elt: "-fxl-pragma-pack");
575
576 // Pass "-fno-sized-deallocation" only when the user hasn't manually enabled
577 // or disabled sized deallocations.
578 if (!Args.getLastArgNoClaim(Ids: options::OPT_fsized_deallocation,
579 Ids: options::OPT_fno_sized_deallocation))
580 CC1Args.push_back(Elt: "-fno-sized-deallocation");
581}
582
583void AIX::addProfileRTLibs(const llvm::opt::ArgList &Args,
584 llvm::opt::ArgStringList &CmdArgs) const {
585 if (needsProfileRT(Args)) {
586 // Add linker option -u__llvm_profile_runtime to cause runtime
587 // initialization to occur.
588 CmdArgs.push_back(Elt: Args.MakeArgString(
589 Str: Twine("-u", llvm::getInstrProfRuntimeHookVarName())));
590
591 if (const auto *A =
592 Args.getLastArgNoClaim(Ids: options::OPT_fprofile_update_EQ)) {
593 StringRef Val = A->getValue();
594 if (Val == "atomic" || Val == "prefer-atomic")
595 CmdArgs.push_back(Elt: "-latomic");
596 }
597 }
598
599 ToolChain::addProfileRTLibs(Args, CmdArgs);
600}
601
602ToolChain::CXXStdlibType AIX::GetDefaultCXXStdlibType() const {
603 return ToolChain::CST_Libcxx;
604}
605
606ToolChain::RuntimeLibType AIX::GetDefaultRuntimeLibType() const {
607 return ToolChain::RLT_CompilerRT;
608}
609
610auto AIX::buildAssembler() const -> Tool * { return new aix::Assembler(*this); }
611
612auto AIX::buildLinker() const -> Tool * { return new aix::Linker(*this); }
613