xref: /freebsd-src/contrib/llvm-project/clang/lib/Driver/ToolChains/AIX.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
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 {
338a4dda33SDimitry 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 
428a4dda33SDimitry Andric   if (Arg *A = C.getArgs().getLastArg(options::OPT_G)) {
438a4dda33SDimitry Andric     D.Diag(diag::err_drv_unsupported_opt_for_target)
448a4dda33SDimitry Andric         << A->getSpelling() << D.getTargetTriple();
458a4dda33SDimitry Andric   }
468a4dda33SDimitry 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 
91*5f757f3fSDimitry Andric     if (ArgString.starts_with("-bE:") || ArgString.starts_with("-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]);
99*5f757f3fSDimitry Andric       if (ArgNextString.starts_with("E:") ||
100*5f757f3fSDimitry Andric           ArgNextString.starts_with("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.");
236*5f757f3fSDimitry Andric     // Find the first filename InputInfo object.
237*5f757f3fSDimitry Andric     auto Input = llvm::find_if(
238*5f757f3fSDimitry Andric         Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
239*5f757f3fSDimitry Andric     if (Input == Inputs.end())
240*5f757f3fSDimitry Andric       // For a very rare case, all of the inputs to the linker are
241*5f757f3fSDimitry Andric       // InputArg. If that happens, just use the first InputInfo.
242*5f757f3fSDimitry Andric       Input = Inputs.begin();
243*5f757f3fSDimitry Andric 
244*5f757f3fSDimitry Andric     addLTOOptions(ToolChain, Args, CmdArgs, Output, *Input,
245bdd1243dSDimitry Andric                   D.getLTOMode() == LTOK_Thin);
246bdd1243dSDimitry Andric   }
247bdd1243dSDimitry Andric 
248bdd1243dSDimitry Andric   if (Args.hasArg(options::OPT_shared) && !hasExportListLinkerOpts(CmdArgs)) {
249bdd1243dSDimitry Andric 
250bdd1243dSDimitry Andric     const char *CreateExportListExec = Args.MakeArgString(
251bdd1243dSDimitry Andric         path::parent_path(ToolChain.getDriver().ClangExecutable) +
252bdd1243dSDimitry Andric         "/llvm-nm");
253bdd1243dSDimitry Andric     ArgStringList CreateExportCmdArgs;
254bdd1243dSDimitry Andric 
255bdd1243dSDimitry Andric     std::string CreateExportListPath =
256bdd1243dSDimitry Andric         C.getDriver().GetTemporaryPath("CreateExportList", "exp");
257bdd1243dSDimitry Andric     const char *ExportList =
258bdd1243dSDimitry Andric         C.addTempFile(C.getArgs().MakeArgString(CreateExportListPath));
259bdd1243dSDimitry Andric 
260bdd1243dSDimitry Andric     for (const auto &II : Inputs)
261bdd1243dSDimitry Andric       if (II.isFilename())
262bdd1243dSDimitry Andric         CreateExportCmdArgs.push_back(II.getFilename());
263bdd1243dSDimitry Andric 
264bdd1243dSDimitry Andric     CreateExportCmdArgs.push_back("--export-symbols");
265bdd1243dSDimitry Andric     CreateExportCmdArgs.push_back("-X");
266bdd1243dSDimitry Andric     if (IsArch32Bit) {
267bdd1243dSDimitry Andric       CreateExportCmdArgs.push_back("32");
268bdd1243dSDimitry Andric     } else {
269bdd1243dSDimitry Andric       // Must be 64-bit, otherwise asserted already.
270bdd1243dSDimitry Andric       CreateExportCmdArgs.push_back("64");
271bdd1243dSDimitry Andric     }
272bdd1243dSDimitry Andric 
273bdd1243dSDimitry Andric     auto ExpCommand = std::make_unique<Command>(
274bdd1243dSDimitry Andric         JA, *this, ResponseFileSupport::None(), CreateExportListExec,
275bdd1243dSDimitry Andric         CreateExportCmdArgs, Inputs, Output);
276bdd1243dSDimitry Andric     ExpCommand->setRedirectFiles(
277bdd1243dSDimitry Andric         {std::nullopt, std::string(ExportList), std::nullopt});
278bdd1243dSDimitry Andric     C.addCommand(std::move(ExpCommand));
279bdd1243dSDimitry Andric     CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-bE:") + ExportList));
280bdd1243dSDimitry Andric   }
281bdd1243dSDimitry Andric 
282480093f4SDimitry Andric   // Add directory to library search path.
283480093f4SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_L);
28406c3fb27SDimitry Andric   if (!Args.hasArg(options::OPT_r)) {
285480093f4SDimitry Andric     ToolChain.AddFilePathLibArgs(Args, CmdArgs);
286e8d8bef9SDimitry Andric     ToolChain.addProfileRTLibs(Args, CmdArgs);
287e8d8bef9SDimitry Andric 
288e8d8bef9SDimitry Andric     if (getToolChain().ShouldLinkCXXStdlib(Args))
289e8d8bef9SDimitry Andric       getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
290480093f4SDimitry Andric 
291480093f4SDimitry Andric     if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
292e8d8bef9SDimitry Andric       AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
293e8d8bef9SDimitry Andric 
294bdd1243dSDimitry Andric       // Add OpenMP runtime if -fopenmp is specified.
295bdd1243dSDimitry Andric       if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
296bdd1243dSDimitry Andric                        options::OPT_fno_openmp, false)) {
297bdd1243dSDimitry Andric         switch (ToolChain.getDriver().getOpenMPRuntime(Args)) {
298bdd1243dSDimitry Andric         case Driver::OMPRT_OMP:
299bdd1243dSDimitry Andric           CmdArgs.push_back("-lomp");
300bdd1243dSDimitry Andric           break;
301bdd1243dSDimitry Andric         case Driver::OMPRT_IOMP5:
302bdd1243dSDimitry Andric           CmdArgs.push_back("-liomp5");
303bdd1243dSDimitry Andric           break;
304bdd1243dSDimitry Andric         case Driver::OMPRT_GOMP:
305bdd1243dSDimitry Andric           CmdArgs.push_back("-lgomp");
306bdd1243dSDimitry Andric           break;
307bdd1243dSDimitry Andric         case Driver::OMPRT_Unknown:
308bdd1243dSDimitry Andric           // Already diagnosed.
309bdd1243dSDimitry Andric           break;
310bdd1243dSDimitry Andric         }
311bdd1243dSDimitry Andric       }
312bdd1243dSDimitry Andric 
313480093f4SDimitry Andric       // Support POSIX threads if "-pthreads" or "-pthread" is present.
314480093f4SDimitry Andric       if (Args.hasArg(options::OPT_pthreads, options::OPT_pthread))
315480093f4SDimitry Andric         CmdArgs.push_back("-lpthreads");
316480093f4SDimitry Andric 
317e8d8bef9SDimitry Andric       if (D.CCCIsCXX())
318e8d8bef9SDimitry Andric         CmdArgs.push_back("-lm");
319e8d8bef9SDimitry Andric 
320480093f4SDimitry Andric       CmdArgs.push_back("-lc");
321bdd1243dSDimitry Andric 
32206c3fb27SDimitry Andric       if (Args.hasArgNoClaim(options::OPT_p, options::OPT_pg)) {
323bdd1243dSDimitry Andric         CmdArgs.push_back(Args.MakeArgString((llvm::Twine("-L") + D.SysRoot) +
324bdd1243dSDimitry Andric                                              "/lib/profiled"));
325bdd1243dSDimitry Andric         CmdArgs.push_back(Args.MakeArgString((llvm::Twine("-L") + D.SysRoot) +
326bdd1243dSDimitry Andric                                              "/usr/lib/profiled"));
327bdd1243dSDimitry Andric       }
328480093f4SDimitry Andric     }
32906c3fb27SDimitry Andric   }
330480093f4SDimitry Andric 
331480093f4SDimitry Andric   const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
3325ffd83dbSDimitry Andric   C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
333e8d8bef9SDimitry Andric                                          Exec, CmdArgs, Inputs, Output));
334480093f4SDimitry Andric }
335480093f4SDimitry Andric 
336480093f4SDimitry Andric /// AIX - AIX tool chain which can call as(1) and ld(1) directly.
337480093f4SDimitry Andric AIX::AIX(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
338480093f4SDimitry Andric     : ToolChain(D, Triple, Args) {
33906c3fb27SDimitry Andric   getProgramPaths().push_back(getDriver().getInstalledDir());
34006c3fb27SDimitry Andric   if (getDriver().getInstalledDir() != getDriver().Dir)
34106c3fb27SDimitry Andric     getProgramPaths().push_back(getDriver().Dir);
34206c3fb27SDimitry Andric 
343fe6060f1SDimitry Andric   ParseInlineAsmUsingAsmParser = Args.hasFlag(
344fe6060f1SDimitry Andric       options::OPT_fintegrated_as, options::OPT_fno_integrated_as, true);
345fe6060f1SDimitry Andric   getLibraryPaths().push_back(getDriver().SysRoot + "/usr/lib");
346480093f4SDimitry Andric }
347480093f4SDimitry Andric 
3485ffd83dbSDimitry Andric // Returns the effective header sysroot path to use.
3495ffd83dbSDimitry Andric // This comes from either -isysroot or --sysroot.
3505ffd83dbSDimitry Andric llvm::StringRef
3515ffd83dbSDimitry Andric AIX::GetHeaderSysroot(const llvm::opt::ArgList &DriverArgs) const {
3525ffd83dbSDimitry Andric   if (DriverArgs.hasArg(options::OPT_isysroot))
3535ffd83dbSDimitry Andric     return DriverArgs.getLastArgValue(options::OPT_isysroot);
3545ffd83dbSDimitry Andric   if (!getDriver().SysRoot.empty())
3555ffd83dbSDimitry Andric     return getDriver().SysRoot;
3565ffd83dbSDimitry Andric   return "/";
3575ffd83dbSDimitry Andric }
3585ffd83dbSDimitry Andric 
3595ffd83dbSDimitry Andric void AIX::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
3605ffd83dbSDimitry Andric                                     ArgStringList &CC1Args) const {
3615ffd83dbSDimitry Andric   // Return if -nostdinc is specified as a driver option.
3625ffd83dbSDimitry Andric   if (DriverArgs.hasArg(options::OPT_nostdinc))
3635ffd83dbSDimitry Andric     return;
3645ffd83dbSDimitry Andric 
3655ffd83dbSDimitry Andric   llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs);
3665ffd83dbSDimitry Andric   const Driver &D = getDriver();
3675ffd83dbSDimitry Andric 
3685ffd83dbSDimitry Andric   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
3695ffd83dbSDimitry Andric     SmallString<128> P(D.ResourceDir);
370fcaf7f86SDimitry Andric     // Add the PowerPC intrinsic headers (<resource>/include/ppc_wrappers)
371fcaf7f86SDimitry Andric     path::append(P, "include", "ppc_wrappers");
372fcaf7f86SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, P);
373fcaf7f86SDimitry Andric     // Add the Clang builtin headers (<resource>/include)
374fcaf7f86SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, path::parent_path(P.str()));
3755ffd83dbSDimitry Andric   }
3765ffd83dbSDimitry Andric 
3775ffd83dbSDimitry Andric   // Return if -nostdlibinc is specified as a driver option.
3785ffd83dbSDimitry Andric   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
3795ffd83dbSDimitry Andric     return;
3805ffd83dbSDimitry Andric 
3815ffd83dbSDimitry Andric   // Add <sysroot>/usr/include.
3825ffd83dbSDimitry Andric   SmallString<128> UP(Sysroot);
3835ffd83dbSDimitry Andric   path::append(UP, "/usr/include");
3845ffd83dbSDimitry Andric   addSystemInclude(DriverArgs, CC1Args, UP.str());
3855ffd83dbSDimitry Andric }
3865ffd83dbSDimitry Andric 
387349cc55cSDimitry Andric void AIX::AddClangCXXStdlibIncludeArgs(
388349cc55cSDimitry Andric     const llvm::opt::ArgList &DriverArgs,
389349cc55cSDimitry Andric     llvm::opt::ArgStringList &CC1Args) const {
390349cc55cSDimitry Andric 
391349cc55cSDimitry Andric   if (DriverArgs.hasArg(options::OPT_nostdinc) ||
392349cc55cSDimitry Andric       DriverArgs.hasArg(options::OPT_nostdincxx) ||
393349cc55cSDimitry Andric       DriverArgs.hasArg(options::OPT_nostdlibinc))
394349cc55cSDimitry Andric     return;
395349cc55cSDimitry Andric 
396349cc55cSDimitry Andric   switch (GetCXXStdlibType(DriverArgs)) {
397349cc55cSDimitry Andric   case ToolChain::CST_Libstdcxx:
398349cc55cSDimitry Andric     llvm::report_fatal_error(
399349cc55cSDimitry Andric         "picking up libstdc++ headers is unimplemented on AIX");
400349cc55cSDimitry Andric   case ToolChain::CST_Libcxx: {
401349cc55cSDimitry Andric     llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs);
402349cc55cSDimitry Andric     SmallString<128> PathCPP(Sysroot);
403349cc55cSDimitry Andric     llvm::sys::path::append(PathCPP, "opt/IBM/openxlCSDK", "include", "c++",
404349cc55cSDimitry Andric                             "v1");
405349cc55cSDimitry Andric     addSystemInclude(DriverArgs, CC1Args, PathCPP.str());
406349cc55cSDimitry Andric     // Required in order to suppress conflicting C++ overloads in the system
407349cc55cSDimitry Andric     // libc headers that were used by XL C++.
408349cc55cSDimitry Andric     CC1Args.push_back("-D__LIBC_NO_CPP_MATH_OVERLOADS__");
409349cc55cSDimitry Andric     return;
410349cc55cSDimitry Andric   }
411349cc55cSDimitry Andric   }
412349cc55cSDimitry Andric 
413349cc55cSDimitry Andric   llvm_unreachable("Unexpected C++ library type; only libc++ is supported.");
414349cc55cSDimitry Andric }
415349cc55cSDimitry Andric 
416e8d8bef9SDimitry Andric void AIX::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
417e8d8bef9SDimitry Andric                               llvm::opt::ArgStringList &CmdArgs) const {
418e8d8bef9SDimitry Andric   switch (GetCXXStdlibType(Args)) {
419349cc55cSDimitry Andric   case ToolChain::CST_Libstdcxx:
420349cc55cSDimitry Andric     llvm::report_fatal_error("linking libstdc++ unimplemented on AIX");
421e8d8bef9SDimitry Andric   case ToolChain::CST_Libcxx:
422e8d8bef9SDimitry Andric     CmdArgs.push_back("-lc++");
423fcaf7f86SDimitry Andric     if (Args.hasArg(options::OPT_fexperimental_library))
424fcaf7f86SDimitry Andric       CmdArgs.push_back("-lc++experimental");
425fe6060f1SDimitry Andric     CmdArgs.push_back("-lc++abi");
426e8d8bef9SDimitry Andric     return;
427e8d8bef9SDimitry Andric   }
428e8d8bef9SDimitry Andric 
429e8d8bef9SDimitry Andric   llvm_unreachable("Unexpected C++ library type; only libc++ is supported.");
430e8d8bef9SDimitry Andric }
431e8d8bef9SDimitry Andric 
43206c3fb27SDimitry Andric void AIX::addClangTargetOptions(
43306c3fb27SDimitry Andric     const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CC1Args,
43406c3fb27SDimitry Andric     Action::OffloadKind DeviceOffloadingKind) const {
43506c3fb27SDimitry Andric   Args.AddLastArg(CC1Args, options::OPT_mignore_xcoff_visibility);
43606c3fb27SDimitry Andric   Args.AddLastArg(CC1Args, options::OPT_mdefault_visibility_export_mapping_EQ);
43706c3fb27SDimitry Andric   Args.addOptInFlag(CC1Args, options::OPT_mxcoff_roptr, options::OPT_mno_xcoff_roptr);
43806c3fb27SDimitry Andric 
43906c3fb27SDimitry Andric   if (Args.hasFlag(options::OPT_fxl_pragma_pack,
44006c3fb27SDimitry Andric                    options::OPT_fno_xl_pragma_pack, true))
44106c3fb27SDimitry Andric     CC1Args.push_back("-fxl-pragma-pack");
44206c3fb27SDimitry Andric }
44306c3fb27SDimitry Andric 
444bdd1243dSDimitry Andric void AIX::addProfileRTLibs(const llvm::opt::ArgList &Args,
445bdd1243dSDimitry Andric                            llvm::opt::ArgStringList &CmdArgs) const {
446*5f757f3fSDimitry Andric   if (needsProfileRT(Args)) {
447bdd1243dSDimitry Andric     // Add linker option -u__llvm_profile_runtime to cause runtime
448bdd1243dSDimitry Andric     // initialization to occur.
449bdd1243dSDimitry Andric     CmdArgs.push_back(Args.MakeArgString(
450bdd1243dSDimitry Andric         Twine("-u", llvm::getInstrProfRuntimeHookVarName())));
451*5f757f3fSDimitry Andric 
452*5f757f3fSDimitry Andric     if (const auto *A =
453*5f757f3fSDimitry Andric             Args.getLastArgNoClaim(options::OPT_fprofile_update_EQ)) {
454*5f757f3fSDimitry Andric       StringRef Val = A->getValue();
455*5f757f3fSDimitry Andric       if (Val == "atomic" || Val == "prefer-atomic")
456*5f757f3fSDimitry Andric         CmdArgs.push_back("-latomic");
457*5f757f3fSDimitry Andric     }
458*5f757f3fSDimitry Andric   }
459*5f757f3fSDimitry Andric 
460bdd1243dSDimitry Andric   ToolChain::addProfileRTLibs(Args, CmdArgs);
461bdd1243dSDimitry Andric }
462bdd1243dSDimitry Andric 
463e8d8bef9SDimitry Andric ToolChain::CXXStdlibType AIX::GetDefaultCXXStdlibType() const {
464e8d8bef9SDimitry Andric   return ToolChain::CST_Libcxx;
465e8d8bef9SDimitry Andric }
466e8d8bef9SDimitry Andric 
467e8d8bef9SDimitry Andric ToolChain::RuntimeLibType AIX::GetDefaultRuntimeLibType() const {
468e8d8bef9SDimitry Andric   return ToolChain::RLT_CompilerRT;
469e8d8bef9SDimitry Andric }
470e8d8bef9SDimitry Andric 
471480093f4SDimitry Andric auto AIX::buildAssembler() const -> Tool * { return new aix::Assembler(*this); }
472480093f4SDimitry Andric 
473480093f4SDimitry Andric auto AIX::buildLinker() const -> Tool * { return new aix::Linker(*this); }
474