xref: /freebsd-src/contrib/llvm-project/clang/lib/Driver/ToolChains/AIX.cpp (revision 8a4dda33d67586ca2624f2a38417baa03a533a7f)
1480093f4SDimitry Andric //===--- AIX.cpp - AIX ToolChain Implementations ----------------*- C++ -*-===//
2480093f4SDimitry Andric //
3480093f4SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4480093f4SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5480093f4SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6480093f4SDimitry Andric //
7480093f4SDimitry Andric //===----------------------------------------------------------------------===//
8480093f4SDimitry Andric 
9480093f4SDimitry Andric #include "AIX.h"
10480093f4SDimitry Andric #include "Arch/PPC.h"
11480093f4SDimitry Andric #include "CommonArgs.h"
12480093f4SDimitry Andric #include "clang/Driver/Compilation.h"
13480093f4SDimitry Andric #include "clang/Driver/Options.h"
14480093f4SDimitry Andric #include "clang/Driver/SanitizerArgs.h"
1506c3fb27SDimitry Andric #include "llvm/ADT/StringExtras.h"
16480093f4SDimitry Andric #include "llvm/Option/ArgList.h"
17bdd1243dSDimitry Andric #include "llvm/ProfileData/InstrProf.h"
185ffd83dbSDimitry Andric #include "llvm/Support/Path.h"
19480093f4SDimitry Andric 
20480093f4SDimitry Andric using AIX = clang::driver::toolchains::AIX;
21480093f4SDimitry Andric using namespace clang::driver;
22480093f4SDimitry Andric using namespace clang::driver::tools;
235ffd83dbSDimitry Andric using namespace clang::driver::toolchains;
24480093f4SDimitry Andric 
25480093f4SDimitry Andric using namespace llvm::opt;
265ffd83dbSDimitry Andric using namespace llvm::sys;
27480093f4SDimitry Andric 
28480093f4SDimitry Andric void aix::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
29480093f4SDimitry Andric                                   const InputInfo &Output,
30480093f4SDimitry Andric                                   const InputInfoList &Inputs,
31480093f4SDimitry Andric                                   const ArgList &Args,
32480093f4SDimitry Andric                                   const char *LinkingOutput) const {
33*8a4dda33SDimitry Andric   const Driver &D = getToolChain().getDriver();
34480093f4SDimitry Andric   ArgStringList CmdArgs;
35480093f4SDimitry Andric 
36480093f4SDimitry Andric   const bool IsArch32Bit = getToolChain().getTriple().isArch32Bit();
37480093f4SDimitry Andric   const bool IsArch64Bit = getToolChain().getTriple().isArch64Bit();
38480093f4SDimitry Andric   // Only support 32 and 64 bit.
39480093f4SDimitry Andric   if (!IsArch32Bit && !IsArch64Bit)
40480093f4SDimitry Andric     llvm_unreachable("Unsupported bit width value.");
41480093f4SDimitry Andric 
42*8a4dda33SDimitry Andric   if (Arg *A = C.getArgs().getLastArg(options::OPT_G)) {
43*8a4dda33SDimitry Andric     D.Diag(diag::err_drv_unsupported_opt_for_target)
44*8a4dda33SDimitry Andric         << A->getSpelling() << D.getTargetTriple();
45*8a4dda33SDimitry Andric   }
46*8a4dda33SDimitry Andric 
47480093f4SDimitry Andric   // Specify the mode in which the as(1) command operates.
48480093f4SDimitry Andric   if (IsArch32Bit) {
49480093f4SDimitry Andric     CmdArgs.push_back("-a32");
50480093f4SDimitry Andric   } else {
51480093f4SDimitry Andric     // Must be 64-bit, otherwise asserted already.
52480093f4SDimitry Andric     CmdArgs.push_back("-a64");
53480093f4SDimitry Andric   }
54480093f4SDimitry Andric 
55480093f4SDimitry Andric   // Accept any mixture of instructions.
56480093f4SDimitry Andric   // On Power for AIX and Linux, this behaviour matches that of GCC for both the
57480093f4SDimitry Andric   // user-provided assembler source case and the compiler-produced assembler
58480093f4SDimitry Andric   // source case. Yet XL with user-provided assembler source would not add this.
59480093f4SDimitry Andric   CmdArgs.push_back("-many");
60480093f4SDimitry Andric 
61480093f4SDimitry Andric   Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
62480093f4SDimitry Andric 
63480093f4SDimitry Andric   // Specify assembler output file.
64480093f4SDimitry Andric   assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
65480093f4SDimitry Andric   if (Output.isFilename()) {
66480093f4SDimitry Andric     CmdArgs.push_back("-o");
67480093f4SDimitry Andric     CmdArgs.push_back(Output.getFilename());
68480093f4SDimitry Andric   }
69480093f4SDimitry Andric 
70480093f4SDimitry Andric   // Specify assembler input file.
71480093f4SDimitry Andric   // The system assembler on AIX takes exactly one input file. The driver is
72480093f4SDimitry Andric   // expected to invoke as(1) separately for each assembler source input file.
73480093f4SDimitry Andric   if (Inputs.size() != 1)
74480093f4SDimitry Andric     llvm_unreachable("Invalid number of input files.");
75480093f4SDimitry Andric   const InputInfo &II = Inputs[0];
76480093f4SDimitry Andric   assert((II.isFilename() || II.isNothing()) && "Invalid input.");
77480093f4SDimitry Andric   if (II.isFilename())
78480093f4SDimitry Andric     CmdArgs.push_back(II.getFilename());
79480093f4SDimitry Andric 
80480093f4SDimitry Andric   const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
815ffd83dbSDimitry Andric   C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
82e8d8bef9SDimitry Andric                                          Exec, CmdArgs, Inputs, Output));
83480093f4SDimitry Andric }
84480093f4SDimitry Andric 
85bdd1243dSDimitry Andric // Determine whether there are any linker options that supply an export list
86bdd1243dSDimitry Andric // (or equivalent information about what to export) being sent to the linker.
87bdd1243dSDimitry Andric static bool hasExportListLinkerOpts(const ArgStringList &CmdArgs) {
88bdd1243dSDimitry Andric   for (size_t i = 0, Size = CmdArgs.size(); i < Size; ++i) {
89bdd1243dSDimitry Andric     llvm::StringRef ArgString(CmdArgs[i]);
90bdd1243dSDimitry Andric 
91bdd1243dSDimitry Andric     if (ArgString.startswith("-bE:") || ArgString.startswith("-bexport:") ||
92bdd1243dSDimitry Andric         ArgString == "-bexpall" || ArgString == "-bexpfull")
93bdd1243dSDimitry Andric       return true;
94bdd1243dSDimitry Andric 
95bdd1243dSDimitry Andric     // If we split -b option, check the next opt.
96bdd1243dSDimitry Andric     if (ArgString == "-b" && i + 1 < Size) {
97bdd1243dSDimitry Andric       ++i;
98bdd1243dSDimitry Andric       llvm::StringRef ArgNextString(CmdArgs[i]);
99bdd1243dSDimitry Andric       if (ArgNextString.startswith("E:") ||
100bdd1243dSDimitry Andric           ArgNextString.startswith("export:") || ArgNextString == "expall" ||
101bdd1243dSDimitry Andric           ArgNextString == "expfull")
102bdd1243dSDimitry Andric         return true;
103bdd1243dSDimitry Andric     }
104bdd1243dSDimitry Andric   }
105bdd1243dSDimitry Andric   return false;
106bdd1243dSDimitry Andric }
107bdd1243dSDimitry Andric 
108480093f4SDimitry Andric void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA,
109480093f4SDimitry Andric                                const InputInfo &Output,
110480093f4SDimitry Andric                                const InputInfoList &Inputs, const ArgList &Args,
111480093f4SDimitry Andric                                const char *LinkingOutput) const {
112480093f4SDimitry Andric   const AIX &ToolChain = static_cast<const AIX &>(getToolChain());
1135ffd83dbSDimitry Andric   const Driver &D = ToolChain.getDriver();
114480093f4SDimitry Andric   ArgStringList CmdArgs;
115480093f4SDimitry Andric 
116480093f4SDimitry Andric   const bool IsArch32Bit = ToolChain.getTriple().isArch32Bit();
117480093f4SDimitry Andric   const bool IsArch64Bit = ToolChain.getTriple().isArch64Bit();
118480093f4SDimitry Andric   // Only support 32 and 64 bit.
119480093f4SDimitry Andric   if (!(IsArch32Bit || IsArch64Bit))
120480093f4SDimitry Andric     llvm_unreachable("Unsupported bit width value.");
121480093f4SDimitry Andric 
12206c3fb27SDimitry Andric   if (Arg *A = C.getArgs().getLastArg(options::OPT_G)) {
12306c3fb27SDimitry Andric     D.Diag(diag::err_drv_unsupported_opt_for_target)
12406c3fb27SDimitry Andric         << A->getSpelling() << D.getTargetTriple();
12506c3fb27SDimitry Andric   }
12606c3fb27SDimitry Andric 
127480093f4SDimitry Andric   // Force static linking when "-static" is present.
128480093f4SDimitry Andric   if (Args.hasArg(options::OPT_static))
129480093f4SDimitry Andric     CmdArgs.push_back("-bnso");
130480093f4SDimitry Andric 
131e8d8bef9SDimitry Andric   // Add options for shared libraries.
132e8d8bef9SDimitry Andric   if (Args.hasArg(options::OPT_shared)) {
133e8d8bef9SDimitry Andric     CmdArgs.push_back("-bM:SRE");
134e8d8bef9SDimitry Andric     CmdArgs.push_back("-bnoentry");
135e8d8bef9SDimitry Andric   }
136e8d8bef9SDimitry Andric 
13706c3fb27SDimitry Andric   if (Args.hasFlag(options::OPT_mxcoff_roptr, options::OPT_mno_xcoff_roptr,
13806c3fb27SDimitry Andric                    false)) {
13906c3fb27SDimitry Andric     if (Args.hasArg(options::OPT_shared))
14006c3fb27SDimitry Andric       D.Diag(diag::err_roptr_cannot_build_shared);
14106c3fb27SDimitry Andric 
14206c3fb27SDimitry Andric     // The `-mxcoff-roptr` option places constants in RO sections as much as
14306c3fb27SDimitry Andric     // possible. Then `-bforceimprw` changes such sections to RW if they contain
14406c3fb27SDimitry Andric     // imported symbols that need to be resolved.
14506c3fb27SDimitry Andric     CmdArgs.push_back("-bforceimprw");
14606c3fb27SDimitry Andric   }
14706c3fb27SDimitry Andric 
14881ad6265SDimitry Andric   // PGO instrumentation generates symbols belonging to special sections, and
14981ad6265SDimitry Andric   // the linker needs to place all symbols in a particular section together in
15081ad6265SDimitry Andric   // memory; the AIX linker does that under an option.
15181ad6265SDimitry Andric   if (Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
152349cc55cSDimitry Andric                     false) ||
153349cc55cSDimitry Andric        Args.hasFlag(options::OPT_fprofile_generate,
154349cc55cSDimitry Andric                     options::OPT_fno_profile_generate, false) ||
155349cc55cSDimitry Andric        Args.hasFlag(options::OPT_fprofile_generate_EQ,
156349cc55cSDimitry Andric                     options::OPT_fno_profile_generate, false) ||
157349cc55cSDimitry Andric        Args.hasFlag(options::OPT_fprofile_instr_generate,
158349cc55cSDimitry Andric                     options::OPT_fno_profile_instr_generate, false) ||
159349cc55cSDimitry Andric        Args.hasFlag(options::OPT_fprofile_instr_generate_EQ,
160349cc55cSDimitry Andric                     options::OPT_fno_profile_instr_generate, false) ||
161349cc55cSDimitry Andric        Args.hasFlag(options::OPT_fcs_profile_generate,
162349cc55cSDimitry Andric                     options::OPT_fno_profile_generate, false) ||
163349cc55cSDimitry Andric        Args.hasFlag(options::OPT_fcs_profile_generate_EQ,
164349cc55cSDimitry Andric                     options::OPT_fno_profile_generate, false) ||
165349cc55cSDimitry Andric        Args.hasArg(options::OPT_fcreate_profile) ||
16681ad6265SDimitry Andric        Args.hasArg(options::OPT_coverage))
16781ad6265SDimitry Andric     CmdArgs.push_back("-bdbg:namedsects:ss");
168349cc55cSDimitry Andric 
16906c3fb27SDimitry Andric   if (Arg *A =
17006c3fb27SDimitry Andric           Args.getLastArg(clang::driver::options::OPT_mxcoff_build_id_EQ)) {
17106c3fb27SDimitry Andric     StringRef BuildId = A->getValue();
17206c3fb27SDimitry Andric     if (BuildId[0] != '0' || BuildId[1] != 'x' ||
17306c3fb27SDimitry Andric         BuildId.find_if_not(llvm::isHexDigit, 2) != StringRef::npos)
17406c3fb27SDimitry Andric       ToolChain.getDriver().Diag(diag::err_drv_unsupported_option_argument)
17506c3fb27SDimitry Andric           << A->getSpelling() << BuildId;
17606c3fb27SDimitry Andric     else {
17706c3fb27SDimitry Andric       std::string LinkerFlag = "-bdbg:ldrinfo:xcoff_binary_id:0x";
17806c3fb27SDimitry Andric       if (BuildId.size() % 2) // Prepend a 0 if odd number of digits.
17906c3fb27SDimitry Andric         LinkerFlag += "0";
18006c3fb27SDimitry Andric       LinkerFlag += BuildId.drop_front(2).lower();
18106c3fb27SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(LinkerFlag));
18206c3fb27SDimitry Andric     }
18306c3fb27SDimitry Andric   }
18406c3fb27SDimitry Andric 
185480093f4SDimitry Andric   // Specify linker output file.
186480093f4SDimitry Andric   assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
187480093f4SDimitry Andric   if (Output.isFilename()) {
188480093f4SDimitry Andric     CmdArgs.push_back("-o");
189480093f4SDimitry Andric     CmdArgs.push_back(Output.getFilename());
190480093f4SDimitry Andric   }
191480093f4SDimitry Andric 
192480093f4SDimitry Andric   // Set linking mode (i.e., 32/64-bit) and the address of
193480093f4SDimitry Andric   // text and data sections based on arch bit width.
194480093f4SDimitry Andric   if (IsArch32Bit) {
195480093f4SDimitry Andric     CmdArgs.push_back("-b32");
196480093f4SDimitry Andric     CmdArgs.push_back("-bpT:0x10000000");
197480093f4SDimitry Andric     CmdArgs.push_back("-bpD:0x20000000");
198480093f4SDimitry Andric   } else {
199480093f4SDimitry Andric     // Must be 64-bit, otherwise asserted already.
200480093f4SDimitry Andric     CmdArgs.push_back("-b64");
201480093f4SDimitry Andric     CmdArgs.push_back("-bpT:0x100000000");
202480093f4SDimitry Andric     CmdArgs.push_back("-bpD:0x110000000");
203480093f4SDimitry Andric   }
204480093f4SDimitry Andric 
20506c3fb27SDimitry Andric   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
20606c3fb27SDimitry Andric                    options::OPT_shared, options::OPT_r)) {
207480093f4SDimitry Andric     auto getCrt0Basename = [&Args, IsArch32Bit] {
20806c3fb27SDimitry Andric       if (Arg *A = Args.getLastArgNoClaim(options::OPT_p, options::OPT_pg)) {
209480093f4SDimitry Andric         // Enable gprofiling when "-pg" is specified.
21006c3fb27SDimitry Andric         if (A->getOption().matches(options::OPT_pg))
211480093f4SDimitry Andric           return IsArch32Bit ? "gcrt0.o" : "gcrt0_64.o";
212480093f4SDimitry Andric         // Enable profiling when "-p" is specified.
213480093f4SDimitry Andric         return IsArch32Bit ? "mcrt0.o" : "mcrt0_64.o";
21406c3fb27SDimitry Andric       }
215480093f4SDimitry Andric       return IsArch32Bit ? "crt0.o" : "crt0_64.o";
216480093f4SDimitry Andric     };
217480093f4SDimitry Andric 
218480093f4SDimitry Andric     CmdArgs.push_back(
219480093f4SDimitry Andric         Args.MakeArgString(ToolChain.GetFilePath(getCrt0Basename())));
220e8d8bef9SDimitry Andric 
221e8d8bef9SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(
222e8d8bef9SDimitry Andric         ToolChain.GetFilePath(IsArch32Bit ? "crti.o" : "crti_64.o")));
223480093f4SDimitry Andric   }
224480093f4SDimitry Andric 
225e8d8bef9SDimitry Andric   // Collect all static constructor and destructor functions in both C and CXX
226e8d8bef9SDimitry Andric   // language link invocations. This has to come before AddLinkerInputs as the
227e8d8bef9SDimitry Andric   // implied option needs to precede any other '-bcdtors' settings or
228e8d8bef9SDimitry Andric   // '-bnocdtors' that '-Wl' might forward.
2295ffd83dbSDimitry Andric   CmdArgs.push_back("-bcdtors:all:0:s");
2305ffd83dbSDimitry Andric 
231480093f4SDimitry Andric   // Specify linker input file(s).
232480093f4SDimitry Andric   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
233480093f4SDimitry Andric 
234bdd1243dSDimitry Andric   if (D.isUsingLTO()) {
235bdd1243dSDimitry Andric     assert(!Inputs.empty() && "Must have at least one input.");
236bdd1243dSDimitry Andric     addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs[0],
237bdd1243dSDimitry Andric                   D.getLTOMode() == LTOK_Thin);
238bdd1243dSDimitry Andric   }
239bdd1243dSDimitry Andric 
240bdd1243dSDimitry Andric   if (Args.hasArg(options::OPT_shared) && !hasExportListLinkerOpts(CmdArgs)) {
241bdd1243dSDimitry Andric 
242bdd1243dSDimitry Andric     const char *CreateExportListExec = Args.MakeArgString(
243bdd1243dSDimitry Andric         path::parent_path(ToolChain.getDriver().ClangExecutable) +
244bdd1243dSDimitry Andric         "/llvm-nm");
245bdd1243dSDimitry Andric     ArgStringList CreateExportCmdArgs;
246bdd1243dSDimitry Andric 
247bdd1243dSDimitry Andric     std::string CreateExportListPath =
248bdd1243dSDimitry Andric         C.getDriver().GetTemporaryPath("CreateExportList", "exp");
249bdd1243dSDimitry Andric     const char *ExportList =
250bdd1243dSDimitry Andric         C.addTempFile(C.getArgs().MakeArgString(CreateExportListPath));
251bdd1243dSDimitry Andric 
252bdd1243dSDimitry Andric     for (const auto &II : Inputs)
253bdd1243dSDimitry Andric       if (II.isFilename())
254bdd1243dSDimitry Andric         CreateExportCmdArgs.push_back(II.getFilename());
255bdd1243dSDimitry Andric 
256bdd1243dSDimitry Andric     CreateExportCmdArgs.push_back("--export-symbols");
257bdd1243dSDimitry Andric     CreateExportCmdArgs.push_back("-X");
258bdd1243dSDimitry Andric     if (IsArch32Bit) {
259bdd1243dSDimitry Andric       CreateExportCmdArgs.push_back("32");
260bdd1243dSDimitry Andric     } else {
261bdd1243dSDimitry Andric       // Must be 64-bit, otherwise asserted already.
262bdd1243dSDimitry Andric       CreateExportCmdArgs.push_back("64");
263bdd1243dSDimitry Andric     }
264bdd1243dSDimitry Andric 
265bdd1243dSDimitry Andric     auto ExpCommand = std::make_unique<Command>(
266bdd1243dSDimitry Andric         JA, *this, ResponseFileSupport::None(), CreateExportListExec,
267bdd1243dSDimitry Andric         CreateExportCmdArgs, Inputs, Output);
268bdd1243dSDimitry Andric     ExpCommand->setRedirectFiles(
269bdd1243dSDimitry Andric         {std::nullopt, std::string(ExportList), std::nullopt});
270bdd1243dSDimitry Andric     C.addCommand(std::move(ExpCommand));
271bdd1243dSDimitry Andric     CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-bE:") + ExportList));
272bdd1243dSDimitry Andric   }
273bdd1243dSDimitry Andric 
274480093f4SDimitry Andric   // Add directory to library search path.
275480093f4SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_L);
27606c3fb27SDimitry Andric   if (!Args.hasArg(options::OPT_r)) {
277480093f4SDimitry Andric     ToolChain.AddFilePathLibArgs(Args, CmdArgs);
278e8d8bef9SDimitry Andric     ToolChain.addProfileRTLibs(Args, CmdArgs);
279e8d8bef9SDimitry Andric 
280e8d8bef9SDimitry Andric     if (getToolChain().ShouldLinkCXXStdlib(Args))
281e8d8bef9SDimitry Andric       getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
282480093f4SDimitry Andric 
283480093f4SDimitry Andric     if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
284e8d8bef9SDimitry Andric       AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
285e8d8bef9SDimitry Andric 
286bdd1243dSDimitry Andric       // Add OpenMP runtime if -fopenmp is specified.
287bdd1243dSDimitry Andric       if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
288bdd1243dSDimitry Andric                        options::OPT_fno_openmp, false)) {
289bdd1243dSDimitry Andric         switch (ToolChain.getDriver().getOpenMPRuntime(Args)) {
290bdd1243dSDimitry Andric         case Driver::OMPRT_OMP:
291bdd1243dSDimitry Andric           CmdArgs.push_back("-lomp");
292bdd1243dSDimitry Andric           break;
293bdd1243dSDimitry Andric         case Driver::OMPRT_IOMP5:
294bdd1243dSDimitry Andric           CmdArgs.push_back("-liomp5");
295bdd1243dSDimitry Andric           break;
296bdd1243dSDimitry Andric         case Driver::OMPRT_GOMP:
297bdd1243dSDimitry Andric           CmdArgs.push_back("-lgomp");
298bdd1243dSDimitry Andric           break;
299bdd1243dSDimitry Andric         case Driver::OMPRT_Unknown:
300bdd1243dSDimitry Andric           // Already diagnosed.
301bdd1243dSDimitry Andric           break;
302bdd1243dSDimitry Andric         }
303bdd1243dSDimitry Andric       }
304bdd1243dSDimitry Andric 
305480093f4SDimitry Andric       // Support POSIX threads if "-pthreads" or "-pthread" is present.
306480093f4SDimitry Andric       if (Args.hasArg(options::OPT_pthreads, options::OPT_pthread))
307480093f4SDimitry Andric         CmdArgs.push_back("-lpthreads");
308480093f4SDimitry Andric 
309e8d8bef9SDimitry Andric       if (D.CCCIsCXX())
310e8d8bef9SDimitry Andric         CmdArgs.push_back("-lm");
311e8d8bef9SDimitry Andric 
312480093f4SDimitry Andric       CmdArgs.push_back("-lc");
313bdd1243dSDimitry Andric 
31406c3fb27SDimitry Andric       if (Args.hasArgNoClaim(options::OPT_p, options::OPT_pg)) {
315bdd1243dSDimitry Andric         CmdArgs.push_back(Args.MakeArgString((llvm::Twine("-L") + D.SysRoot) +
316bdd1243dSDimitry Andric                                              "/lib/profiled"));
317bdd1243dSDimitry Andric         CmdArgs.push_back(Args.MakeArgString((llvm::Twine("-L") + D.SysRoot) +
318bdd1243dSDimitry Andric                                              "/usr/lib/profiled"));
319bdd1243dSDimitry Andric       }
320480093f4SDimitry Andric     }
32106c3fb27SDimitry Andric   }
322480093f4SDimitry Andric 
323480093f4SDimitry Andric   const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
3245ffd83dbSDimitry Andric   C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
325e8d8bef9SDimitry Andric                                          Exec, CmdArgs, Inputs, Output));
326480093f4SDimitry Andric }
327480093f4SDimitry Andric 
328480093f4SDimitry Andric /// AIX - AIX tool chain which can call as(1) and ld(1) directly.
329480093f4SDimitry Andric AIX::AIX(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
330480093f4SDimitry Andric     : ToolChain(D, Triple, Args) {
33106c3fb27SDimitry Andric   getProgramPaths().push_back(getDriver().getInstalledDir());
33206c3fb27SDimitry Andric   if (getDriver().getInstalledDir() != getDriver().Dir)
33306c3fb27SDimitry Andric     getProgramPaths().push_back(getDriver().Dir);
33406c3fb27SDimitry Andric 
335fe6060f1SDimitry Andric   ParseInlineAsmUsingAsmParser = Args.hasFlag(
336fe6060f1SDimitry Andric       options::OPT_fintegrated_as, options::OPT_fno_integrated_as, true);
337fe6060f1SDimitry Andric   getLibraryPaths().push_back(getDriver().SysRoot + "/usr/lib");
338480093f4SDimitry Andric }
339480093f4SDimitry Andric 
3405ffd83dbSDimitry Andric // Returns the effective header sysroot path to use.
3415ffd83dbSDimitry Andric // This comes from either -isysroot or --sysroot.
3425ffd83dbSDimitry Andric llvm::StringRef
3435ffd83dbSDimitry Andric AIX::GetHeaderSysroot(const llvm::opt::ArgList &DriverArgs) const {
3445ffd83dbSDimitry Andric   if (DriverArgs.hasArg(options::OPT_isysroot))
3455ffd83dbSDimitry Andric     return DriverArgs.getLastArgValue(options::OPT_isysroot);
3465ffd83dbSDimitry Andric   if (!getDriver().SysRoot.empty())
3475ffd83dbSDimitry Andric     return getDriver().SysRoot;
3485ffd83dbSDimitry Andric   return "/";
3495ffd83dbSDimitry Andric }
3505ffd83dbSDimitry Andric 
3515ffd83dbSDimitry Andric void AIX::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
3525ffd83dbSDimitry Andric                                     ArgStringList &CC1Args) const {
3535ffd83dbSDimitry Andric   // Return if -nostdinc is specified as a driver option.
3545ffd83dbSDimitry Andric   if (DriverArgs.hasArg(options::OPT_nostdinc))
3555ffd83dbSDimitry Andric     return;
3565ffd83dbSDimitry Andric 
3575ffd83dbSDimitry Andric   llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs);
3585ffd83dbSDimitry Andric   const Driver &D = getDriver();
3595ffd83dbSDimitry Andric 
3605ffd83dbSDimitry Andric   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
3615ffd83dbSDimitry Andric     SmallString<128> P(D.ResourceDir);
362fcaf7f86SDimitry Andric     // Add the PowerPC intrinsic headers (<resource>/include/ppc_wrappers)
363fcaf7f86SDimitry Andric     path::append(P, "include", "ppc_wrappers");
364fcaf7f86SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, P);
365fcaf7f86SDimitry Andric     // Add the Clang builtin headers (<resource>/include)
366fcaf7f86SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, path::parent_path(P.str()));
3675ffd83dbSDimitry Andric   }
3685ffd83dbSDimitry Andric 
3695ffd83dbSDimitry Andric   // Return if -nostdlibinc is specified as a driver option.
3705ffd83dbSDimitry Andric   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
3715ffd83dbSDimitry Andric     return;
3725ffd83dbSDimitry Andric 
3735ffd83dbSDimitry Andric   // Add <sysroot>/usr/include.
3745ffd83dbSDimitry Andric   SmallString<128> UP(Sysroot);
3755ffd83dbSDimitry Andric   path::append(UP, "/usr/include");
3765ffd83dbSDimitry Andric   addSystemInclude(DriverArgs, CC1Args, UP.str());
3775ffd83dbSDimitry Andric }
3785ffd83dbSDimitry Andric 
379349cc55cSDimitry Andric void AIX::AddClangCXXStdlibIncludeArgs(
380349cc55cSDimitry Andric     const llvm::opt::ArgList &DriverArgs,
381349cc55cSDimitry Andric     llvm::opt::ArgStringList &CC1Args) const {
382349cc55cSDimitry Andric 
383349cc55cSDimitry Andric   if (DriverArgs.hasArg(options::OPT_nostdinc) ||
384349cc55cSDimitry Andric       DriverArgs.hasArg(options::OPT_nostdincxx) ||
385349cc55cSDimitry Andric       DriverArgs.hasArg(options::OPT_nostdlibinc))
386349cc55cSDimitry Andric     return;
387349cc55cSDimitry Andric 
388349cc55cSDimitry Andric   switch (GetCXXStdlibType(DriverArgs)) {
389349cc55cSDimitry Andric   case ToolChain::CST_Libstdcxx:
390349cc55cSDimitry Andric     llvm::report_fatal_error(
391349cc55cSDimitry Andric         "picking up libstdc++ headers is unimplemented on AIX");
392349cc55cSDimitry Andric   case ToolChain::CST_Libcxx: {
393349cc55cSDimitry Andric     llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs);
394349cc55cSDimitry Andric     SmallString<128> PathCPP(Sysroot);
395349cc55cSDimitry Andric     llvm::sys::path::append(PathCPP, "opt/IBM/openxlCSDK", "include", "c++",
396349cc55cSDimitry Andric                             "v1");
397349cc55cSDimitry Andric     addSystemInclude(DriverArgs, CC1Args, PathCPP.str());
398349cc55cSDimitry Andric     // Required in order to suppress conflicting C++ overloads in the system
399349cc55cSDimitry Andric     // libc headers that were used by XL C++.
400349cc55cSDimitry Andric     CC1Args.push_back("-D__LIBC_NO_CPP_MATH_OVERLOADS__");
401349cc55cSDimitry Andric     return;
402349cc55cSDimitry Andric   }
403349cc55cSDimitry Andric   }
404349cc55cSDimitry Andric 
405349cc55cSDimitry Andric   llvm_unreachable("Unexpected C++ library type; only libc++ is supported.");
406349cc55cSDimitry Andric }
407349cc55cSDimitry Andric 
408e8d8bef9SDimitry Andric void AIX::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
409e8d8bef9SDimitry Andric                               llvm::opt::ArgStringList &CmdArgs) const {
410e8d8bef9SDimitry Andric   switch (GetCXXStdlibType(Args)) {
411349cc55cSDimitry Andric   case ToolChain::CST_Libstdcxx:
412349cc55cSDimitry Andric     llvm::report_fatal_error("linking libstdc++ unimplemented on AIX");
413e8d8bef9SDimitry Andric   case ToolChain::CST_Libcxx:
414e8d8bef9SDimitry Andric     CmdArgs.push_back("-lc++");
415fcaf7f86SDimitry Andric     if (Args.hasArg(options::OPT_fexperimental_library))
416fcaf7f86SDimitry Andric       CmdArgs.push_back("-lc++experimental");
417fe6060f1SDimitry Andric     CmdArgs.push_back("-lc++abi");
418e8d8bef9SDimitry Andric     return;
419e8d8bef9SDimitry Andric   }
420e8d8bef9SDimitry Andric 
421e8d8bef9SDimitry Andric   llvm_unreachable("Unexpected C++ library type; only libc++ is supported.");
422e8d8bef9SDimitry Andric }
423e8d8bef9SDimitry Andric 
42406c3fb27SDimitry Andric void AIX::addClangTargetOptions(
42506c3fb27SDimitry Andric     const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CC1Args,
42606c3fb27SDimitry Andric     Action::OffloadKind DeviceOffloadingKind) const {
42706c3fb27SDimitry Andric   Args.AddLastArg(CC1Args, options::OPT_mignore_xcoff_visibility);
42806c3fb27SDimitry Andric   Args.AddLastArg(CC1Args, options::OPT_mdefault_visibility_export_mapping_EQ);
42906c3fb27SDimitry Andric   Args.addOptInFlag(CC1Args, options::OPT_mxcoff_roptr, options::OPT_mno_xcoff_roptr);
43006c3fb27SDimitry Andric 
43106c3fb27SDimitry Andric   if (Args.hasFlag(options::OPT_fxl_pragma_pack,
43206c3fb27SDimitry Andric                    options::OPT_fno_xl_pragma_pack, true))
43306c3fb27SDimitry Andric     CC1Args.push_back("-fxl-pragma-pack");
43406c3fb27SDimitry Andric }
43506c3fb27SDimitry Andric 
436bdd1243dSDimitry Andric void AIX::addProfileRTLibs(const llvm::opt::ArgList &Args,
437bdd1243dSDimitry Andric                            llvm::opt::ArgStringList &CmdArgs) const {
438bdd1243dSDimitry Andric   // Add linker option -u__llvm_profile_runtime to cause runtime
439bdd1243dSDimitry Andric   // initialization to occur.
440bdd1243dSDimitry Andric   if (needsProfileRT(Args))
441bdd1243dSDimitry Andric     CmdArgs.push_back(Args.MakeArgString(
442bdd1243dSDimitry Andric         Twine("-u", llvm::getInstrProfRuntimeHookVarName())));
443bdd1243dSDimitry Andric   ToolChain::addProfileRTLibs(Args, CmdArgs);
444bdd1243dSDimitry Andric }
445bdd1243dSDimitry Andric 
446e8d8bef9SDimitry Andric ToolChain::CXXStdlibType AIX::GetDefaultCXXStdlibType() const {
447e8d8bef9SDimitry Andric   return ToolChain::CST_Libcxx;
448e8d8bef9SDimitry Andric }
449e8d8bef9SDimitry Andric 
450e8d8bef9SDimitry Andric ToolChain::RuntimeLibType AIX::GetDefaultRuntimeLibType() const {
451e8d8bef9SDimitry Andric   return ToolChain::RLT_CompilerRT;
452e8d8bef9SDimitry Andric }
453e8d8bef9SDimitry Andric 
454480093f4SDimitry Andric auto AIX::buildAssembler() const -> Tool * { return new aix::Assembler(*this); }
455480093f4SDimitry Andric 
456480093f4SDimitry Andric auto AIX::buildLinker() const -> Tool * { return new aix::Linker(*this); }
457