xref: /freebsd-src/contrib/llvm-project/clang/lib/Driver/ToolChains/AIX.cpp (revision 62987288060ff68c817b7056815aa9fb8ba8ecd7)
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 
20*0fca6ea1SDimitry Andric #include <set>
21*0fca6ea1SDimitry Andric 
22480093f4SDimitry Andric using AIX = clang::driver::toolchains::AIX;
23480093f4SDimitry Andric using namespace clang::driver;
24480093f4SDimitry Andric using namespace clang::driver::tools;
255ffd83dbSDimitry Andric using namespace clang::driver::toolchains;
26480093f4SDimitry Andric 
27480093f4SDimitry Andric using namespace llvm::opt;
285ffd83dbSDimitry Andric using namespace llvm::sys;
29480093f4SDimitry Andric 
30480093f4SDimitry Andric void aix::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
31480093f4SDimitry Andric                                   const InputInfo &Output,
32480093f4SDimitry Andric                                   const InputInfoList &Inputs,
33480093f4SDimitry Andric                                   const ArgList &Args,
34480093f4SDimitry Andric                                   const char *LinkingOutput) const {
358a4dda33SDimitry Andric   const Driver &D = getToolChain().getDriver();
36480093f4SDimitry Andric   ArgStringList CmdArgs;
37480093f4SDimitry Andric 
38480093f4SDimitry Andric   const bool IsArch32Bit = getToolChain().getTriple().isArch32Bit();
39480093f4SDimitry Andric   const bool IsArch64Bit = getToolChain().getTriple().isArch64Bit();
40480093f4SDimitry Andric   // Only support 32 and 64 bit.
41480093f4SDimitry Andric   if (!IsArch32Bit && !IsArch64Bit)
42480093f4SDimitry Andric     llvm_unreachable("Unsupported bit width value.");
43480093f4SDimitry Andric 
448a4dda33SDimitry Andric   if (Arg *A = C.getArgs().getLastArg(options::OPT_G)) {
458a4dda33SDimitry Andric     D.Diag(diag::err_drv_unsupported_opt_for_target)
468a4dda33SDimitry Andric         << A->getSpelling() << D.getTargetTriple();
478a4dda33SDimitry Andric   }
488a4dda33SDimitry Andric 
49480093f4SDimitry Andric   // Specify the mode in which the as(1) command operates.
50480093f4SDimitry Andric   if (IsArch32Bit) {
51480093f4SDimitry Andric     CmdArgs.push_back("-a32");
52480093f4SDimitry Andric   } else {
53480093f4SDimitry Andric     // Must be 64-bit, otherwise asserted already.
54480093f4SDimitry Andric     CmdArgs.push_back("-a64");
55480093f4SDimitry Andric   }
56480093f4SDimitry Andric 
57480093f4SDimitry Andric   // Accept any mixture of instructions.
58480093f4SDimitry Andric   // On Power for AIX and Linux, this behaviour matches that of GCC for both the
59480093f4SDimitry Andric   // user-provided assembler source case and the compiler-produced assembler
60480093f4SDimitry Andric   // source case. Yet XL with user-provided assembler source would not add this.
61480093f4SDimitry Andric   CmdArgs.push_back("-many");
62480093f4SDimitry Andric 
63480093f4SDimitry Andric   Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
64480093f4SDimitry Andric 
65480093f4SDimitry Andric   // Specify assembler output file.
66480093f4SDimitry Andric   assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
67480093f4SDimitry Andric   if (Output.isFilename()) {
68480093f4SDimitry Andric     CmdArgs.push_back("-o");
69480093f4SDimitry Andric     CmdArgs.push_back(Output.getFilename());
70480093f4SDimitry Andric   }
71480093f4SDimitry Andric 
72480093f4SDimitry Andric   // Specify assembler input file.
73480093f4SDimitry Andric   // The system assembler on AIX takes exactly one input file. The driver is
74480093f4SDimitry Andric   // expected to invoke as(1) separately for each assembler source input file.
75480093f4SDimitry Andric   if (Inputs.size() != 1)
76480093f4SDimitry Andric     llvm_unreachable("Invalid number of input files.");
77480093f4SDimitry Andric   const InputInfo &II = Inputs[0];
78480093f4SDimitry Andric   assert((II.isFilename() || II.isNothing()) && "Invalid input.");
79480093f4SDimitry Andric   if (II.isFilename())
80480093f4SDimitry Andric     CmdArgs.push_back(II.getFilename());
81480093f4SDimitry Andric 
82480093f4SDimitry Andric   const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
835ffd83dbSDimitry Andric   C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
84e8d8bef9SDimitry Andric                                          Exec, CmdArgs, Inputs, Output));
85480093f4SDimitry Andric }
86480093f4SDimitry Andric 
87bdd1243dSDimitry Andric // Determine whether there are any linker options that supply an export list
88bdd1243dSDimitry Andric // (or equivalent information about what to export) being sent to the linker.
89bdd1243dSDimitry Andric static bool hasExportListLinkerOpts(const ArgStringList &CmdArgs) {
90bdd1243dSDimitry Andric   for (size_t i = 0, Size = CmdArgs.size(); i < Size; ++i) {
91bdd1243dSDimitry Andric     llvm::StringRef ArgString(CmdArgs[i]);
92bdd1243dSDimitry Andric 
935f757f3fSDimitry Andric     if (ArgString.starts_with("-bE:") || ArgString.starts_with("-bexport:") ||
94bdd1243dSDimitry Andric         ArgString == "-bexpall" || ArgString == "-bexpfull")
95bdd1243dSDimitry Andric       return true;
96bdd1243dSDimitry Andric 
97bdd1243dSDimitry Andric     // If we split -b option, check the next opt.
98bdd1243dSDimitry Andric     if (ArgString == "-b" && i + 1 < Size) {
99bdd1243dSDimitry Andric       ++i;
100bdd1243dSDimitry Andric       llvm::StringRef ArgNextString(CmdArgs[i]);
1015f757f3fSDimitry Andric       if (ArgNextString.starts_with("E:") ||
1025f757f3fSDimitry Andric           ArgNextString.starts_with("export:") || ArgNextString == "expall" ||
103bdd1243dSDimitry Andric           ArgNextString == "expfull")
104bdd1243dSDimitry Andric         return true;
105bdd1243dSDimitry Andric     }
106bdd1243dSDimitry Andric   }
107bdd1243dSDimitry Andric   return false;
108bdd1243dSDimitry Andric }
109bdd1243dSDimitry Andric 
110480093f4SDimitry Andric void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA,
111480093f4SDimitry Andric                                const InputInfo &Output,
112480093f4SDimitry Andric                                const InputInfoList &Inputs, const ArgList &Args,
113480093f4SDimitry Andric                                const char *LinkingOutput) const {
114480093f4SDimitry Andric   const AIX &ToolChain = static_cast<const AIX &>(getToolChain());
1155ffd83dbSDimitry Andric   const Driver &D = ToolChain.getDriver();
116480093f4SDimitry Andric   ArgStringList CmdArgs;
117480093f4SDimitry Andric 
118480093f4SDimitry Andric   const bool IsArch32Bit = ToolChain.getTriple().isArch32Bit();
119480093f4SDimitry Andric   const bool IsArch64Bit = ToolChain.getTriple().isArch64Bit();
120480093f4SDimitry Andric   // Only support 32 and 64 bit.
121480093f4SDimitry Andric   if (!(IsArch32Bit || IsArch64Bit))
122480093f4SDimitry Andric     llvm_unreachable("Unsupported bit width value.");
123480093f4SDimitry Andric 
12406c3fb27SDimitry Andric   if (Arg *A = C.getArgs().getLastArg(options::OPT_G)) {
12506c3fb27SDimitry Andric     D.Diag(diag::err_drv_unsupported_opt_for_target)
12606c3fb27SDimitry Andric         << A->getSpelling() << D.getTargetTriple();
12706c3fb27SDimitry Andric   }
12806c3fb27SDimitry Andric 
129480093f4SDimitry Andric   // Force static linking when "-static" is present.
130480093f4SDimitry Andric   if (Args.hasArg(options::OPT_static))
131480093f4SDimitry Andric     CmdArgs.push_back("-bnso");
132480093f4SDimitry Andric 
133e8d8bef9SDimitry Andric   // Add options for shared libraries.
134e8d8bef9SDimitry Andric   if (Args.hasArg(options::OPT_shared)) {
135e8d8bef9SDimitry Andric     CmdArgs.push_back("-bM:SRE");
136e8d8bef9SDimitry Andric     CmdArgs.push_back("-bnoentry");
137e8d8bef9SDimitry Andric   }
138e8d8bef9SDimitry Andric 
13906c3fb27SDimitry Andric   if (Args.hasFlag(options::OPT_mxcoff_roptr, options::OPT_mno_xcoff_roptr,
14006c3fb27SDimitry Andric                    false)) {
14106c3fb27SDimitry Andric     if (Args.hasArg(options::OPT_shared))
14206c3fb27SDimitry Andric       D.Diag(diag::err_roptr_cannot_build_shared);
14306c3fb27SDimitry Andric 
14406c3fb27SDimitry Andric     // The `-mxcoff-roptr` option places constants in RO sections as much as
14506c3fb27SDimitry Andric     // possible. Then `-bforceimprw` changes such sections to RW if they contain
14606c3fb27SDimitry Andric     // imported symbols that need to be resolved.
14706c3fb27SDimitry Andric     CmdArgs.push_back("-bforceimprw");
14806c3fb27SDimitry Andric   }
14906c3fb27SDimitry Andric 
15081ad6265SDimitry Andric   // PGO instrumentation generates symbols belonging to special sections, and
15181ad6265SDimitry Andric   // the linker needs to place all symbols in a particular section together in
15281ad6265SDimitry Andric   // memory; the AIX linker does that under an option.
15381ad6265SDimitry Andric   if (Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
154349cc55cSDimitry Andric                     false) ||
155349cc55cSDimitry Andric        Args.hasFlag(options::OPT_fprofile_generate,
156349cc55cSDimitry Andric                     options::OPT_fno_profile_generate, false) ||
157349cc55cSDimitry Andric        Args.hasFlag(options::OPT_fprofile_generate_EQ,
158349cc55cSDimitry Andric                     options::OPT_fno_profile_generate, false) ||
159349cc55cSDimitry Andric        Args.hasFlag(options::OPT_fprofile_instr_generate,
160349cc55cSDimitry Andric                     options::OPT_fno_profile_instr_generate, false) ||
161349cc55cSDimitry Andric        Args.hasFlag(options::OPT_fprofile_instr_generate_EQ,
162349cc55cSDimitry Andric                     options::OPT_fno_profile_instr_generate, false) ||
163349cc55cSDimitry Andric        Args.hasFlag(options::OPT_fcs_profile_generate,
164349cc55cSDimitry Andric                     options::OPT_fno_profile_generate, false) ||
165349cc55cSDimitry Andric        Args.hasFlag(options::OPT_fcs_profile_generate_EQ,
166349cc55cSDimitry Andric                     options::OPT_fno_profile_generate, false) ||
167349cc55cSDimitry Andric        Args.hasArg(options::OPT_fcreate_profile) ||
16881ad6265SDimitry Andric        Args.hasArg(options::OPT_coverage))
16981ad6265SDimitry Andric     CmdArgs.push_back("-bdbg:namedsects:ss");
170349cc55cSDimitry Andric 
17106c3fb27SDimitry Andric   if (Arg *A =
17206c3fb27SDimitry Andric           Args.getLastArg(clang::driver::options::OPT_mxcoff_build_id_EQ)) {
17306c3fb27SDimitry Andric     StringRef BuildId = A->getValue();
17406c3fb27SDimitry Andric     if (BuildId[0] != '0' || BuildId[1] != 'x' ||
17506c3fb27SDimitry Andric         BuildId.find_if_not(llvm::isHexDigit, 2) != StringRef::npos)
17606c3fb27SDimitry Andric       ToolChain.getDriver().Diag(diag::err_drv_unsupported_option_argument)
17706c3fb27SDimitry Andric           << A->getSpelling() << BuildId;
17806c3fb27SDimitry Andric     else {
17906c3fb27SDimitry Andric       std::string LinkerFlag = "-bdbg:ldrinfo:xcoff_binary_id:0x";
18006c3fb27SDimitry Andric       if (BuildId.size() % 2) // Prepend a 0 if odd number of digits.
18106c3fb27SDimitry Andric         LinkerFlag += "0";
18206c3fb27SDimitry Andric       LinkerFlag += BuildId.drop_front(2).lower();
18306c3fb27SDimitry Andric       CmdArgs.push_back(Args.MakeArgString(LinkerFlag));
18406c3fb27SDimitry Andric     }
18506c3fb27SDimitry Andric   }
18606c3fb27SDimitry Andric 
187480093f4SDimitry Andric   // Specify linker output file.
188480093f4SDimitry Andric   assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
189480093f4SDimitry Andric   if (Output.isFilename()) {
190480093f4SDimitry Andric     CmdArgs.push_back("-o");
191480093f4SDimitry Andric     CmdArgs.push_back(Output.getFilename());
192480093f4SDimitry Andric   }
193480093f4SDimitry Andric 
194480093f4SDimitry Andric   // Set linking mode (i.e., 32/64-bit) and the address of
195480093f4SDimitry Andric   // text and data sections based on arch bit width.
196480093f4SDimitry Andric   if (IsArch32Bit) {
197480093f4SDimitry Andric     CmdArgs.push_back("-b32");
198480093f4SDimitry Andric     CmdArgs.push_back("-bpT:0x10000000");
199480093f4SDimitry Andric     CmdArgs.push_back("-bpD:0x20000000");
200480093f4SDimitry Andric   } else {
201480093f4SDimitry Andric     // Must be 64-bit, otherwise asserted already.
202480093f4SDimitry Andric     CmdArgs.push_back("-b64");
203480093f4SDimitry Andric     CmdArgs.push_back("-bpT:0x100000000");
204480093f4SDimitry Andric     CmdArgs.push_back("-bpD:0x110000000");
205480093f4SDimitry Andric   }
206480093f4SDimitry Andric 
20706c3fb27SDimitry Andric   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
20806c3fb27SDimitry Andric                    options::OPT_shared, options::OPT_r)) {
209480093f4SDimitry Andric     auto getCrt0Basename = [&Args, IsArch32Bit] {
21006c3fb27SDimitry Andric       if (Arg *A = Args.getLastArgNoClaim(options::OPT_p, options::OPT_pg)) {
211480093f4SDimitry Andric         // Enable gprofiling when "-pg" is specified.
21206c3fb27SDimitry Andric         if (A->getOption().matches(options::OPT_pg))
213480093f4SDimitry Andric           return IsArch32Bit ? "gcrt0.o" : "gcrt0_64.o";
214480093f4SDimitry Andric         // Enable profiling when "-p" is specified.
215480093f4SDimitry Andric         return IsArch32Bit ? "mcrt0.o" : "mcrt0_64.o";
21606c3fb27SDimitry Andric       }
217480093f4SDimitry Andric       return IsArch32Bit ? "crt0.o" : "crt0_64.o";
218480093f4SDimitry Andric     };
219480093f4SDimitry Andric 
220480093f4SDimitry Andric     CmdArgs.push_back(
221480093f4SDimitry Andric         Args.MakeArgString(ToolChain.GetFilePath(getCrt0Basename())));
222e8d8bef9SDimitry Andric 
223e8d8bef9SDimitry Andric     CmdArgs.push_back(Args.MakeArgString(
224e8d8bef9SDimitry Andric         ToolChain.GetFilePath(IsArch32Bit ? "crti.o" : "crti_64.o")));
225480093f4SDimitry Andric   }
226480093f4SDimitry Andric 
227e8d8bef9SDimitry Andric   // Collect all static constructor and destructor functions in both C and CXX
228e8d8bef9SDimitry Andric   // language link invocations. This has to come before AddLinkerInputs as the
229e8d8bef9SDimitry Andric   // implied option needs to precede any other '-bcdtors' settings or
230e8d8bef9SDimitry Andric   // '-bnocdtors' that '-Wl' might forward.
2315ffd83dbSDimitry Andric   CmdArgs.push_back("-bcdtors:all:0:s");
2325ffd83dbSDimitry Andric 
233480093f4SDimitry Andric   // Specify linker input file(s).
234480093f4SDimitry Andric   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
235480093f4SDimitry Andric 
236bdd1243dSDimitry Andric   if (D.isUsingLTO()) {
237bdd1243dSDimitry Andric     assert(!Inputs.empty() && "Must have at least one input.");
2385f757f3fSDimitry Andric     // Find the first filename InputInfo object.
2395f757f3fSDimitry Andric     auto Input = llvm::find_if(
2405f757f3fSDimitry Andric         Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
2415f757f3fSDimitry Andric     if (Input == Inputs.end())
2425f757f3fSDimitry Andric       // For a very rare case, all of the inputs to the linker are
2435f757f3fSDimitry Andric       // InputArg. If that happens, just use the first InputInfo.
2445f757f3fSDimitry Andric       Input = Inputs.begin();
2455f757f3fSDimitry Andric 
2465f757f3fSDimitry Andric     addLTOOptions(ToolChain, Args, CmdArgs, Output, *Input,
247bdd1243dSDimitry Andric                   D.getLTOMode() == LTOK_Thin);
248bdd1243dSDimitry Andric   }
249bdd1243dSDimitry Andric 
250bdd1243dSDimitry Andric   if (Args.hasArg(options::OPT_shared) && !hasExportListLinkerOpts(CmdArgs)) {
251bdd1243dSDimitry Andric 
252bdd1243dSDimitry Andric     const char *CreateExportListExec = Args.MakeArgString(
253bdd1243dSDimitry Andric         path::parent_path(ToolChain.getDriver().ClangExecutable) +
254bdd1243dSDimitry Andric         "/llvm-nm");
255bdd1243dSDimitry Andric     ArgStringList CreateExportCmdArgs;
256bdd1243dSDimitry Andric 
257bdd1243dSDimitry Andric     std::string CreateExportListPath =
258bdd1243dSDimitry Andric         C.getDriver().GetTemporaryPath("CreateExportList", "exp");
259bdd1243dSDimitry Andric     const char *ExportList =
260bdd1243dSDimitry Andric         C.addTempFile(C.getArgs().MakeArgString(CreateExportListPath));
261bdd1243dSDimitry Andric 
262bdd1243dSDimitry Andric     for (const auto &II : Inputs)
263bdd1243dSDimitry Andric       if (II.isFilename())
264bdd1243dSDimitry Andric         CreateExportCmdArgs.push_back(II.getFilename());
265bdd1243dSDimitry Andric 
266bdd1243dSDimitry Andric     CreateExportCmdArgs.push_back("--export-symbols");
267bdd1243dSDimitry Andric     CreateExportCmdArgs.push_back("-X");
268bdd1243dSDimitry Andric     if (IsArch32Bit) {
269bdd1243dSDimitry Andric       CreateExportCmdArgs.push_back("32");
270bdd1243dSDimitry Andric     } else {
271bdd1243dSDimitry Andric       // Must be 64-bit, otherwise asserted already.
272bdd1243dSDimitry Andric       CreateExportCmdArgs.push_back("64");
273bdd1243dSDimitry Andric     }
274bdd1243dSDimitry Andric 
275bdd1243dSDimitry Andric     auto ExpCommand = std::make_unique<Command>(
276bdd1243dSDimitry Andric         JA, *this, ResponseFileSupport::None(), CreateExportListExec,
277bdd1243dSDimitry Andric         CreateExportCmdArgs, Inputs, Output);
278bdd1243dSDimitry Andric     ExpCommand->setRedirectFiles(
279bdd1243dSDimitry Andric         {std::nullopt, std::string(ExportList), std::nullopt});
280bdd1243dSDimitry Andric     C.addCommand(std::move(ExpCommand));
281bdd1243dSDimitry Andric     CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-bE:") + ExportList));
282bdd1243dSDimitry Andric   }
283bdd1243dSDimitry Andric 
284480093f4SDimitry Andric   // Add directory to library search path.
285480093f4SDimitry Andric   Args.AddAllArgs(CmdArgs, options::OPT_L);
28606c3fb27SDimitry Andric   if (!Args.hasArg(options::OPT_r)) {
287480093f4SDimitry Andric     ToolChain.AddFilePathLibArgs(Args, CmdArgs);
288e8d8bef9SDimitry Andric     ToolChain.addProfileRTLibs(Args, CmdArgs);
289e8d8bef9SDimitry Andric 
290e8d8bef9SDimitry Andric     if (getToolChain().ShouldLinkCXXStdlib(Args))
291e8d8bef9SDimitry Andric       getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
292480093f4SDimitry Andric 
293480093f4SDimitry Andric     if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
294e8d8bef9SDimitry Andric       AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
295e8d8bef9SDimitry Andric 
296bdd1243dSDimitry Andric       // Add OpenMP runtime if -fopenmp is specified.
297bdd1243dSDimitry Andric       if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
298bdd1243dSDimitry Andric                        options::OPT_fno_openmp, false)) {
299bdd1243dSDimitry Andric         switch (ToolChain.getDriver().getOpenMPRuntime(Args)) {
300bdd1243dSDimitry Andric         case Driver::OMPRT_OMP:
301bdd1243dSDimitry Andric           CmdArgs.push_back("-lomp");
302bdd1243dSDimitry Andric           break;
303bdd1243dSDimitry Andric         case Driver::OMPRT_IOMP5:
304bdd1243dSDimitry Andric           CmdArgs.push_back("-liomp5");
305bdd1243dSDimitry Andric           break;
306bdd1243dSDimitry Andric         case Driver::OMPRT_GOMP:
307bdd1243dSDimitry Andric           CmdArgs.push_back("-lgomp");
308bdd1243dSDimitry Andric           break;
309bdd1243dSDimitry Andric         case Driver::OMPRT_Unknown:
310bdd1243dSDimitry Andric           // Already diagnosed.
311bdd1243dSDimitry Andric           break;
312bdd1243dSDimitry Andric         }
313bdd1243dSDimitry Andric       }
314bdd1243dSDimitry Andric 
315480093f4SDimitry Andric       // Support POSIX threads if "-pthreads" or "-pthread" is present.
316480093f4SDimitry Andric       if (Args.hasArg(options::OPT_pthreads, options::OPT_pthread))
317480093f4SDimitry Andric         CmdArgs.push_back("-lpthreads");
318480093f4SDimitry Andric 
319e8d8bef9SDimitry Andric       if (D.CCCIsCXX())
320e8d8bef9SDimitry Andric         CmdArgs.push_back("-lm");
321e8d8bef9SDimitry Andric 
322480093f4SDimitry Andric       CmdArgs.push_back("-lc");
323bdd1243dSDimitry Andric 
32406c3fb27SDimitry Andric       if (Args.hasArgNoClaim(options::OPT_p, options::OPT_pg)) {
325bdd1243dSDimitry Andric         CmdArgs.push_back(Args.MakeArgString((llvm::Twine("-L") + D.SysRoot) +
326bdd1243dSDimitry Andric                                              "/lib/profiled"));
327bdd1243dSDimitry Andric         CmdArgs.push_back(Args.MakeArgString((llvm::Twine("-L") + D.SysRoot) +
328bdd1243dSDimitry Andric                                              "/usr/lib/profiled"));
329bdd1243dSDimitry Andric       }
330480093f4SDimitry Andric     }
33106c3fb27SDimitry Andric   }
332480093f4SDimitry Andric 
333cb14a3feSDimitry Andric   if (D.IsFlangMode()) {
334cb14a3feSDimitry Andric     addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
335cb14a3feSDimitry Andric     addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
336cb14a3feSDimitry Andric     CmdArgs.push_back("-lm");
337cb14a3feSDimitry Andric     CmdArgs.push_back("-lpthread");
338cb14a3feSDimitry Andric   }
339480093f4SDimitry Andric   const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
3405ffd83dbSDimitry Andric   C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
341e8d8bef9SDimitry Andric                                          Exec, CmdArgs, Inputs, Output));
342480093f4SDimitry Andric }
343480093f4SDimitry Andric 
344480093f4SDimitry Andric /// AIX - AIX tool chain which can call as(1) and ld(1) directly.
345480093f4SDimitry Andric AIX::AIX(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
346480093f4SDimitry Andric     : ToolChain(D, Triple, Args) {
34706c3fb27SDimitry Andric   getProgramPaths().push_back(getDriver().Dir);
34806c3fb27SDimitry Andric 
349fe6060f1SDimitry Andric   ParseInlineAsmUsingAsmParser = Args.hasFlag(
350fe6060f1SDimitry Andric       options::OPT_fintegrated_as, options::OPT_fno_integrated_as, true);
351fe6060f1SDimitry Andric   getLibraryPaths().push_back(getDriver().SysRoot + "/usr/lib");
352480093f4SDimitry Andric }
353480093f4SDimitry Andric 
3545ffd83dbSDimitry Andric // Returns the effective header sysroot path to use.
3555ffd83dbSDimitry Andric // This comes from either -isysroot or --sysroot.
3565ffd83dbSDimitry Andric llvm::StringRef
3575ffd83dbSDimitry Andric AIX::GetHeaderSysroot(const llvm::opt::ArgList &DriverArgs) const {
3585ffd83dbSDimitry Andric   if (DriverArgs.hasArg(options::OPT_isysroot))
3595ffd83dbSDimitry Andric     return DriverArgs.getLastArgValue(options::OPT_isysroot);
3605ffd83dbSDimitry Andric   if (!getDriver().SysRoot.empty())
3615ffd83dbSDimitry Andric     return getDriver().SysRoot;
3625ffd83dbSDimitry Andric   return "/";
3635ffd83dbSDimitry Andric }
3645ffd83dbSDimitry Andric 
365*0fca6ea1SDimitry Andric void AIX::AddOpenMPIncludeArgs(const ArgList &DriverArgs,
366*0fca6ea1SDimitry Andric                                ArgStringList &CC1Args) const {
367*0fca6ea1SDimitry Andric   // Add OpenMP include paths if -fopenmp is specified.
368*0fca6ea1SDimitry Andric   if (DriverArgs.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
369*0fca6ea1SDimitry Andric                          options::OPT_fno_openmp, false)) {
370*0fca6ea1SDimitry Andric     SmallString<128> PathOpenMP;
371*0fca6ea1SDimitry Andric     switch (getDriver().getOpenMPRuntime(DriverArgs)) {
372*0fca6ea1SDimitry Andric     case Driver::OMPRT_OMP:
373*0fca6ea1SDimitry Andric       PathOpenMP = GetHeaderSysroot(DriverArgs);
374*0fca6ea1SDimitry Andric       llvm::sys::path::append(PathOpenMP, "opt/IBM/openxlCSDK", "include",
375*0fca6ea1SDimitry Andric                               "openmp");
376*0fca6ea1SDimitry Andric       addSystemInclude(DriverArgs, CC1Args, PathOpenMP.str());
377*0fca6ea1SDimitry Andric       break;
378*0fca6ea1SDimitry Andric     case Driver::OMPRT_IOMP5:
379*0fca6ea1SDimitry Andric     case Driver::OMPRT_GOMP:
380*0fca6ea1SDimitry Andric     case Driver::OMPRT_Unknown:
381*0fca6ea1SDimitry Andric       // Unknown / unsupported include paths.
382*0fca6ea1SDimitry Andric       break;
383*0fca6ea1SDimitry Andric     }
384*0fca6ea1SDimitry Andric   }
385*0fca6ea1SDimitry Andric }
386*0fca6ea1SDimitry Andric 
3875ffd83dbSDimitry Andric void AIX::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
3885ffd83dbSDimitry Andric                                     ArgStringList &CC1Args) const {
3895ffd83dbSDimitry Andric   // Return if -nostdinc is specified as a driver option.
3905ffd83dbSDimitry Andric   if (DriverArgs.hasArg(options::OPT_nostdinc))
3915ffd83dbSDimitry Andric     return;
3925ffd83dbSDimitry Andric 
3935ffd83dbSDimitry Andric   llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs);
3945ffd83dbSDimitry Andric   const Driver &D = getDriver();
3955ffd83dbSDimitry Andric 
3965ffd83dbSDimitry Andric   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
3975ffd83dbSDimitry Andric     SmallString<128> P(D.ResourceDir);
398fcaf7f86SDimitry Andric     // Add the PowerPC intrinsic headers (<resource>/include/ppc_wrappers)
399fcaf7f86SDimitry Andric     path::append(P, "include", "ppc_wrappers");
400fcaf7f86SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, P);
401fcaf7f86SDimitry Andric     // Add the Clang builtin headers (<resource>/include)
402fcaf7f86SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, path::parent_path(P.str()));
4035ffd83dbSDimitry Andric   }
4045ffd83dbSDimitry Andric 
405*0fca6ea1SDimitry Andric   // Add the include directory containing omp.h. This needs to be before
406*0fca6ea1SDimitry Andric   // adding the system include directory because other compilers put their
407*0fca6ea1SDimitry Andric   // omp.h in /usr/include.
408*0fca6ea1SDimitry Andric   AddOpenMPIncludeArgs(DriverArgs, CC1Args);
409*0fca6ea1SDimitry Andric 
4105ffd83dbSDimitry Andric   // Return if -nostdlibinc is specified as a driver option.
4115ffd83dbSDimitry Andric   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
4125ffd83dbSDimitry Andric     return;
4135ffd83dbSDimitry Andric 
4145ffd83dbSDimitry Andric   // Add <sysroot>/usr/include.
4155ffd83dbSDimitry Andric   SmallString<128> UP(Sysroot);
4165ffd83dbSDimitry Andric   path::append(UP, "/usr/include");
4175ffd83dbSDimitry Andric   addSystemInclude(DriverArgs, CC1Args, UP.str());
4185ffd83dbSDimitry Andric }
4195ffd83dbSDimitry Andric 
420349cc55cSDimitry Andric void AIX::AddClangCXXStdlibIncludeArgs(
421349cc55cSDimitry Andric     const llvm::opt::ArgList &DriverArgs,
422349cc55cSDimitry Andric     llvm::opt::ArgStringList &CC1Args) const {
423349cc55cSDimitry Andric 
424349cc55cSDimitry Andric   if (DriverArgs.hasArg(options::OPT_nostdinc) ||
425349cc55cSDimitry Andric       DriverArgs.hasArg(options::OPT_nostdincxx) ||
426349cc55cSDimitry Andric       DriverArgs.hasArg(options::OPT_nostdlibinc))
427349cc55cSDimitry Andric     return;
428349cc55cSDimitry Andric 
429349cc55cSDimitry Andric   switch (GetCXXStdlibType(DriverArgs)) {
430349cc55cSDimitry Andric   case ToolChain::CST_Libstdcxx:
431349cc55cSDimitry Andric     llvm::report_fatal_error(
432349cc55cSDimitry Andric         "picking up libstdc++ headers is unimplemented on AIX");
433349cc55cSDimitry Andric   case ToolChain::CST_Libcxx: {
434349cc55cSDimitry Andric     llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs);
435349cc55cSDimitry Andric     SmallString<128> PathCPP(Sysroot);
436349cc55cSDimitry Andric     llvm::sys::path::append(PathCPP, "opt/IBM/openxlCSDK", "include", "c++",
437349cc55cSDimitry Andric                             "v1");
438349cc55cSDimitry Andric     addSystemInclude(DriverArgs, CC1Args, PathCPP.str());
439349cc55cSDimitry Andric     // Required in order to suppress conflicting C++ overloads in the system
440349cc55cSDimitry Andric     // libc headers that were used by XL C++.
441349cc55cSDimitry Andric     CC1Args.push_back("-D__LIBC_NO_CPP_MATH_OVERLOADS__");
442349cc55cSDimitry Andric     return;
443349cc55cSDimitry Andric   }
444349cc55cSDimitry Andric   }
445349cc55cSDimitry Andric 
446349cc55cSDimitry Andric   llvm_unreachable("Unexpected C++ library type; only libc++ is supported.");
447349cc55cSDimitry Andric }
448349cc55cSDimitry Andric 
449e8d8bef9SDimitry Andric void AIX::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
450e8d8bef9SDimitry Andric                               llvm::opt::ArgStringList &CmdArgs) const {
451e8d8bef9SDimitry Andric   switch (GetCXXStdlibType(Args)) {
452349cc55cSDimitry Andric   case ToolChain::CST_Libstdcxx:
453349cc55cSDimitry Andric     llvm::report_fatal_error("linking libstdc++ unimplemented on AIX");
454e8d8bef9SDimitry Andric   case ToolChain::CST_Libcxx:
455e8d8bef9SDimitry Andric     CmdArgs.push_back("-lc++");
456fcaf7f86SDimitry Andric     if (Args.hasArg(options::OPT_fexperimental_library))
457fcaf7f86SDimitry Andric       CmdArgs.push_back("-lc++experimental");
458fe6060f1SDimitry Andric     CmdArgs.push_back("-lc++abi");
459e8d8bef9SDimitry Andric     return;
460e8d8bef9SDimitry Andric   }
461e8d8bef9SDimitry Andric 
462e8d8bef9SDimitry Andric   llvm_unreachable("Unexpected C++ library type; only libc++ is supported.");
463e8d8bef9SDimitry Andric }
464e8d8bef9SDimitry Andric 
465*0fca6ea1SDimitry Andric // This function processes all the mtocdata options to build the final
466*0fca6ea1SDimitry Andric // simplified toc data options to pass to CC1.
467*0fca6ea1SDimitry Andric static void addTocDataOptions(const llvm::opt::ArgList &Args,
468*0fca6ea1SDimitry Andric                               llvm::opt::ArgStringList &CC1Args,
469*0fca6ea1SDimitry Andric                               const Driver &D) {
470*0fca6ea1SDimitry Andric 
471*0fca6ea1SDimitry Andric   // Check the global toc-data setting. The default is -mno-tocdata.
472*0fca6ea1SDimitry Andric   // To enable toc-data globally, -mtocdata must be specified.
473*0fca6ea1SDimitry Andric   // Additionally, it must be last to take effect.
474*0fca6ea1SDimitry Andric   const bool TOCDataGloballyinEffect = [&Args]() {
475*0fca6ea1SDimitry Andric     if (const Arg *LastArg =
476*0fca6ea1SDimitry Andric             Args.getLastArg(options::OPT_mtocdata, options::OPT_mno_tocdata))
477*0fca6ea1SDimitry Andric       return LastArg->getOption().matches(options::OPT_mtocdata);
478*0fca6ea1SDimitry Andric     else
479*0fca6ea1SDimitry Andric       return false;
480*0fca6ea1SDimitry Andric   }();
481*0fca6ea1SDimitry Andric 
482*0fca6ea1SDimitry Andric   enum TOCDataSetting {
483*0fca6ea1SDimitry Andric     AddressInTOC = 0, // Address of the symbol stored in the TOC.
484*0fca6ea1SDimitry Andric     DataInTOC = 1     // Symbol defined in the TOC.
485*0fca6ea1SDimitry Andric   };
486*0fca6ea1SDimitry Andric 
487*0fca6ea1SDimitry Andric   const TOCDataSetting DefaultTocDataSetting =
488*0fca6ea1SDimitry Andric       TOCDataGloballyinEffect ? DataInTOC : AddressInTOC;
489*0fca6ea1SDimitry Andric 
490*0fca6ea1SDimitry Andric   // Process the list of variables in the explicitly specified options
491*0fca6ea1SDimitry Andric   // -mtocdata= and -mno-tocdata= to see which variables are opposite to
492*0fca6ea1SDimitry Andric   // the global setting of tocdata in TOCDataGloballyinEffect.
493*0fca6ea1SDimitry Andric   // Those that have the opposite setting to TOCDataGloballyinEffect, are added
494*0fca6ea1SDimitry Andric   // to ExplicitlySpecifiedGlobals.
495*0fca6ea1SDimitry Andric   std::set<llvm::StringRef> ExplicitlySpecifiedGlobals;
496*0fca6ea1SDimitry Andric   for (const auto Arg :
497*0fca6ea1SDimitry Andric        Args.filtered(options::OPT_mtocdata_EQ, options::OPT_mno_tocdata_EQ)) {
498*0fca6ea1SDimitry Andric     TOCDataSetting ArgTocDataSetting =
499*0fca6ea1SDimitry Andric         Arg->getOption().matches(options::OPT_mtocdata_EQ) ? DataInTOC
500*0fca6ea1SDimitry Andric                                                            : AddressInTOC;
501*0fca6ea1SDimitry Andric 
502*0fca6ea1SDimitry Andric     if (ArgTocDataSetting != DefaultTocDataSetting)
503*0fca6ea1SDimitry Andric       for (const char *Val : Arg->getValues())
504*0fca6ea1SDimitry Andric         ExplicitlySpecifiedGlobals.insert(Val);
505*0fca6ea1SDimitry Andric     else
506*0fca6ea1SDimitry Andric       for (const char *Val : Arg->getValues())
507*0fca6ea1SDimitry Andric         ExplicitlySpecifiedGlobals.erase(Val);
508*0fca6ea1SDimitry Andric   }
509*0fca6ea1SDimitry Andric 
510*0fca6ea1SDimitry Andric   auto buildExceptionList = [](const std::set<llvm::StringRef> &ExplicitValues,
511*0fca6ea1SDimitry Andric                                const char *OptionSpelling) {
512*0fca6ea1SDimitry Andric     std::string Option(OptionSpelling);
513*0fca6ea1SDimitry Andric     bool IsFirst = true;
514*0fca6ea1SDimitry Andric     for (const auto &E : ExplicitValues) {
515*0fca6ea1SDimitry Andric       if (!IsFirst)
516*0fca6ea1SDimitry Andric         Option += ",";
517*0fca6ea1SDimitry Andric 
518*0fca6ea1SDimitry Andric       IsFirst = false;
519*0fca6ea1SDimitry Andric       Option += E.str();
520*0fca6ea1SDimitry Andric     }
521*0fca6ea1SDimitry Andric     return Option;
522*0fca6ea1SDimitry Andric   };
523*0fca6ea1SDimitry Andric 
524*0fca6ea1SDimitry Andric   // Pass the final tocdata options to CC1 consisting of the default
525*0fca6ea1SDimitry Andric   // tocdata option (-mtocdata/-mno-tocdata) along with the list
526*0fca6ea1SDimitry Andric   // option (-mno-tocdata=/-mtocdata=) if there are any explicitly specified
527*0fca6ea1SDimitry Andric   // variables which would be exceptions to the default setting.
528*0fca6ea1SDimitry Andric   const char *TocDataGlobalOption =
529*0fca6ea1SDimitry Andric       TOCDataGloballyinEffect ? "-mtocdata" : "-mno-tocdata";
530*0fca6ea1SDimitry Andric   CC1Args.push_back(TocDataGlobalOption);
531*0fca6ea1SDimitry Andric 
532*0fca6ea1SDimitry Andric   const char *TocDataListOption =
533*0fca6ea1SDimitry Andric       TOCDataGloballyinEffect ? "-mno-tocdata=" : "-mtocdata=";
534*0fca6ea1SDimitry Andric   if (!ExplicitlySpecifiedGlobals.empty())
535*0fca6ea1SDimitry Andric     CC1Args.push_back(Args.MakeArgString(llvm::Twine(
536*0fca6ea1SDimitry Andric         buildExceptionList(ExplicitlySpecifiedGlobals, TocDataListOption))));
537*0fca6ea1SDimitry Andric }
538*0fca6ea1SDimitry Andric 
53906c3fb27SDimitry Andric void AIX::addClangTargetOptions(
54006c3fb27SDimitry Andric     const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CC1Args,
54106c3fb27SDimitry Andric     Action::OffloadKind DeviceOffloadingKind) const {
54206c3fb27SDimitry Andric   Args.AddLastArg(CC1Args, options::OPT_mignore_xcoff_visibility);
54306c3fb27SDimitry Andric   Args.AddLastArg(CC1Args, options::OPT_mdefault_visibility_export_mapping_EQ);
54406c3fb27SDimitry Andric   Args.addOptInFlag(CC1Args, options::OPT_mxcoff_roptr, options::OPT_mno_xcoff_roptr);
54506c3fb27SDimitry Andric 
546*0fca6ea1SDimitry Andric   // Forward last mtocdata/mno_tocdata options to -cc1.
547*0fca6ea1SDimitry Andric   if (Args.hasArg(options::OPT_mtocdata_EQ, options::OPT_mno_tocdata_EQ,
548*0fca6ea1SDimitry Andric                   options::OPT_mtocdata))
549*0fca6ea1SDimitry Andric     addTocDataOptions(Args, CC1Args, getDriver());
550*0fca6ea1SDimitry Andric 
55106c3fb27SDimitry Andric   if (Args.hasFlag(options::OPT_fxl_pragma_pack,
55206c3fb27SDimitry Andric                    options::OPT_fno_xl_pragma_pack, true))
55306c3fb27SDimitry Andric     CC1Args.push_back("-fxl-pragma-pack");
554*0fca6ea1SDimitry Andric 
555*0fca6ea1SDimitry Andric   // Pass "-fno-sized-deallocation" only when the user hasn't manually enabled
556*0fca6ea1SDimitry Andric   // or disabled sized deallocations.
557*0fca6ea1SDimitry Andric   if (!Args.getLastArgNoClaim(options::OPT_fsized_deallocation,
558*0fca6ea1SDimitry Andric                               options::OPT_fno_sized_deallocation))
559*0fca6ea1SDimitry Andric     CC1Args.push_back("-fno-sized-deallocation");
56006c3fb27SDimitry Andric }
56106c3fb27SDimitry Andric 
562bdd1243dSDimitry Andric void AIX::addProfileRTLibs(const llvm::opt::ArgList &Args,
563bdd1243dSDimitry Andric                            llvm::opt::ArgStringList &CmdArgs) const {
5645f757f3fSDimitry Andric   if (needsProfileRT(Args)) {
565bdd1243dSDimitry Andric     // Add linker option -u__llvm_profile_runtime to cause runtime
566bdd1243dSDimitry Andric     // initialization to occur.
567bdd1243dSDimitry Andric     CmdArgs.push_back(Args.MakeArgString(
568bdd1243dSDimitry Andric         Twine("-u", llvm::getInstrProfRuntimeHookVarName())));
5695f757f3fSDimitry Andric 
5705f757f3fSDimitry Andric     if (const auto *A =
5715f757f3fSDimitry Andric             Args.getLastArgNoClaim(options::OPT_fprofile_update_EQ)) {
5725f757f3fSDimitry Andric       StringRef Val = A->getValue();
5735f757f3fSDimitry Andric       if (Val == "atomic" || Val == "prefer-atomic")
5745f757f3fSDimitry Andric         CmdArgs.push_back("-latomic");
5755f757f3fSDimitry Andric     }
5765f757f3fSDimitry Andric   }
5775f757f3fSDimitry Andric 
578bdd1243dSDimitry Andric   ToolChain::addProfileRTLibs(Args, CmdArgs);
579bdd1243dSDimitry Andric }
580bdd1243dSDimitry Andric 
581e8d8bef9SDimitry Andric ToolChain::CXXStdlibType AIX::GetDefaultCXXStdlibType() const {
582e8d8bef9SDimitry Andric   return ToolChain::CST_Libcxx;
583e8d8bef9SDimitry Andric }
584e8d8bef9SDimitry Andric 
585e8d8bef9SDimitry Andric ToolChain::RuntimeLibType AIX::GetDefaultRuntimeLibType() const {
586e8d8bef9SDimitry Andric   return ToolChain::RLT_CompilerRT;
587e8d8bef9SDimitry Andric }
588e8d8bef9SDimitry Andric 
589480093f4SDimitry Andric auto AIX::buildAssembler() const -> Tool * { return new aix::Assembler(*this); }
590480093f4SDimitry Andric 
591480093f4SDimitry Andric auto AIX::buildLinker() const -> Tool * { return new aix::Linker(*this); }
592