xref: /freebsd/contrib/llvm-project/clang/lib/Driver/ToolChains/AIX.cpp (revision 5b56413d04e608379c9a306373554a8e4d321bc0)
1 //===--- AIX.cpp - AIX ToolChain Implementations ----------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "AIX.h"
10 #include "Arch/PPC.h"
11 #include "CommonArgs.h"
12 #include "clang/Driver/Compilation.h"
13 #include "clang/Driver/Options.h"
14 #include "clang/Driver/SanitizerArgs.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/Option/ArgList.h"
17 #include "llvm/ProfileData/InstrProf.h"
18 #include "llvm/Support/Path.h"
19 
20 using AIX = clang::driver::toolchains::AIX;
21 using namespace clang::driver;
22 using namespace clang::driver::tools;
23 using namespace clang::driver::toolchains;
24 
25 using namespace llvm::opt;
26 using namespace llvm::sys;
27 
28 void aix::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
29                                   const InputInfo &Output,
30                                   const InputInfoList &Inputs,
31                                   const ArgList &Args,
32                                   const char *LinkingOutput) const {
33   const Driver &D = getToolChain().getDriver();
34   ArgStringList CmdArgs;
35 
36   const bool IsArch32Bit = getToolChain().getTriple().isArch32Bit();
37   const bool IsArch64Bit = getToolChain().getTriple().isArch64Bit();
38   // Only support 32 and 64 bit.
39   if (!IsArch32Bit && !IsArch64Bit)
40     llvm_unreachable("Unsupported bit width value.");
41 
42   if (Arg *A = C.getArgs().getLastArg(options::OPT_G)) {
43     D.Diag(diag::err_drv_unsupported_opt_for_target)
44         << A->getSpelling() << D.getTargetTriple();
45   }
46 
47   // Specify the mode in which the as(1) command operates.
48   if (IsArch32Bit) {
49     CmdArgs.push_back("-a32");
50   } else {
51     // Must be 64-bit, otherwise asserted already.
52     CmdArgs.push_back("-a64");
53   }
54 
55   // Accept any mixture of instructions.
56   // On Power for AIX and Linux, this behaviour matches that of GCC for both the
57   // user-provided assembler source case and the compiler-produced assembler
58   // source case. Yet XL with user-provided assembler source would not add this.
59   CmdArgs.push_back("-many");
60 
61   Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
62 
63   // Specify assembler output file.
64   assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
65   if (Output.isFilename()) {
66     CmdArgs.push_back("-o");
67     CmdArgs.push_back(Output.getFilename());
68   }
69 
70   // Specify assembler input file.
71   // The system assembler on AIX takes exactly one input file. The driver is
72   // expected to invoke as(1) separately for each assembler source input file.
73   if (Inputs.size() != 1)
74     llvm_unreachable("Invalid number of input files.");
75   const InputInfo &II = Inputs[0];
76   assert((II.isFilename() || II.isNothing()) && "Invalid input.");
77   if (II.isFilename())
78     CmdArgs.push_back(II.getFilename());
79 
80   const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
81   C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
82                                          Exec, CmdArgs, Inputs, Output));
83 }
84 
85 // Determine whether there are any linker options that supply an export list
86 // (or equivalent information about what to export) being sent to the linker.
87 static bool hasExportListLinkerOpts(const ArgStringList &CmdArgs) {
88   for (size_t i = 0, Size = CmdArgs.size(); i < Size; ++i) {
89     llvm::StringRef ArgString(CmdArgs[i]);
90 
91     if (ArgString.starts_with("-bE:") || ArgString.starts_with("-bexport:") ||
92         ArgString == "-bexpall" || ArgString == "-bexpfull")
93       return true;
94 
95     // If we split -b option, check the next opt.
96     if (ArgString == "-b" && i + 1 < Size) {
97       ++i;
98       llvm::StringRef ArgNextString(CmdArgs[i]);
99       if (ArgNextString.starts_with("E:") ||
100           ArgNextString.starts_with("export:") || ArgNextString == "expall" ||
101           ArgNextString == "expfull")
102         return true;
103     }
104   }
105   return false;
106 }
107 
108 void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA,
109                                const InputInfo &Output,
110                                const InputInfoList &Inputs, const ArgList &Args,
111                                const char *LinkingOutput) const {
112   const AIX &ToolChain = static_cast<const AIX &>(getToolChain());
113   const Driver &D = ToolChain.getDriver();
114   ArgStringList CmdArgs;
115 
116   const bool IsArch32Bit = ToolChain.getTriple().isArch32Bit();
117   const bool IsArch64Bit = ToolChain.getTriple().isArch64Bit();
118   // Only support 32 and 64 bit.
119   if (!(IsArch32Bit || IsArch64Bit))
120     llvm_unreachable("Unsupported bit width value.");
121 
122   if (Arg *A = C.getArgs().getLastArg(options::OPT_G)) {
123     D.Diag(diag::err_drv_unsupported_opt_for_target)
124         << A->getSpelling() << D.getTargetTriple();
125   }
126 
127   // Force static linking when "-static" is present.
128   if (Args.hasArg(options::OPT_static))
129     CmdArgs.push_back("-bnso");
130 
131   // Add options for shared libraries.
132   if (Args.hasArg(options::OPT_shared)) {
133     CmdArgs.push_back("-bM:SRE");
134     CmdArgs.push_back("-bnoentry");
135   }
136 
137   if (Args.hasFlag(options::OPT_mxcoff_roptr, options::OPT_mno_xcoff_roptr,
138                    false)) {
139     if (Args.hasArg(options::OPT_shared))
140       D.Diag(diag::err_roptr_cannot_build_shared);
141 
142     // The `-mxcoff-roptr` option places constants in RO sections as much as
143     // possible. Then `-bforceimprw` changes such sections to RW if they contain
144     // imported symbols that need to be resolved.
145     CmdArgs.push_back("-bforceimprw");
146   }
147 
148   // PGO instrumentation generates symbols belonging to special sections, and
149   // the linker needs to place all symbols in a particular section together in
150   // memory; the AIX linker does that under an option.
151   if (Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
152                     false) ||
153        Args.hasFlag(options::OPT_fprofile_generate,
154                     options::OPT_fno_profile_generate, false) ||
155        Args.hasFlag(options::OPT_fprofile_generate_EQ,
156                     options::OPT_fno_profile_generate, false) ||
157        Args.hasFlag(options::OPT_fprofile_instr_generate,
158                     options::OPT_fno_profile_instr_generate, false) ||
159        Args.hasFlag(options::OPT_fprofile_instr_generate_EQ,
160                     options::OPT_fno_profile_instr_generate, false) ||
161        Args.hasFlag(options::OPT_fcs_profile_generate,
162                     options::OPT_fno_profile_generate, false) ||
163        Args.hasFlag(options::OPT_fcs_profile_generate_EQ,
164                     options::OPT_fno_profile_generate, false) ||
165        Args.hasArg(options::OPT_fcreate_profile) ||
166        Args.hasArg(options::OPT_coverage))
167     CmdArgs.push_back("-bdbg:namedsects:ss");
168 
169   if (Arg *A =
170           Args.getLastArg(clang::driver::options::OPT_mxcoff_build_id_EQ)) {
171     StringRef BuildId = A->getValue();
172     if (BuildId[0] != '0' || BuildId[1] != 'x' ||
173         BuildId.find_if_not(llvm::isHexDigit, 2) != StringRef::npos)
174       ToolChain.getDriver().Diag(diag::err_drv_unsupported_option_argument)
175           << A->getSpelling() << BuildId;
176     else {
177       std::string LinkerFlag = "-bdbg:ldrinfo:xcoff_binary_id:0x";
178       if (BuildId.size() % 2) // Prepend a 0 if odd number of digits.
179         LinkerFlag += "0";
180       LinkerFlag += BuildId.drop_front(2).lower();
181       CmdArgs.push_back(Args.MakeArgString(LinkerFlag));
182     }
183   }
184 
185   // Specify linker output file.
186   assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
187   if (Output.isFilename()) {
188     CmdArgs.push_back("-o");
189     CmdArgs.push_back(Output.getFilename());
190   }
191 
192   // Set linking mode (i.e., 32/64-bit) and the address of
193   // text and data sections based on arch bit width.
194   if (IsArch32Bit) {
195     CmdArgs.push_back("-b32");
196     CmdArgs.push_back("-bpT:0x10000000");
197     CmdArgs.push_back("-bpD:0x20000000");
198   } else {
199     // Must be 64-bit, otherwise asserted already.
200     CmdArgs.push_back("-b64");
201     CmdArgs.push_back("-bpT:0x100000000");
202     CmdArgs.push_back("-bpD:0x110000000");
203   }
204 
205   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
206                    options::OPT_shared, options::OPT_r)) {
207     auto getCrt0Basename = [&Args, IsArch32Bit] {
208       if (Arg *A = Args.getLastArgNoClaim(options::OPT_p, options::OPT_pg)) {
209         // Enable gprofiling when "-pg" is specified.
210         if (A->getOption().matches(options::OPT_pg))
211           return IsArch32Bit ? "gcrt0.o" : "gcrt0_64.o";
212         // Enable profiling when "-p" is specified.
213         return IsArch32Bit ? "mcrt0.o" : "mcrt0_64.o";
214       }
215       return IsArch32Bit ? "crt0.o" : "crt0_64.o";
216     };
217 
218     CmdArgs.push_back(
219         Args.MakeArgString(ToolChain.GetFilePath(getCrt0Basename())));
220 
221     CmdArgs.push_back(Args.MakeArgString(
222         ToolChain.GetFilePath(IsArch32Bit ? "crti.o" : "crti_64.o")));
223   }
224 
225   // Collect all static constructor and destructor functions in both C and CXX
226   // language link invocations. This has to come before AddLinkerInputs as the
227   // implied option needs to precede any other '-bcdtors' settings or
228   // '-bnocdtors' that '-Wl' might forward.
229   CmdArgs.push_back("-bcdtors:all:0:s");
230 
231   // Specify linker input file(s).
232   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
233 
234   if (D.isUsingLTO()) {
235     assert(!Inputs.empty() && "Must have at least one input.");
236     // Find the first filename InputInfo object.
237     auto Input = llvm::find_if(
238         Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
239     if (Input == Inputs.end())
240       // For a very rare case, all of the inputs to the linker are
241       // InputArg. If that happens, just use the first InputInfo.
242       Input = Inputs.begin();
243 
244     addLTOOptions(ToolChain, Args, CmdArgs, Output, *Input,
245                   D.getLTOMode() == LTOK_Thin);
246   }
247 
248   if (Args.hasArg(options::OPT_shared) && !hasExportListLinkerOpts(CmdArgs)) {
249 
250     const char *CreateExportListExec = Args.MakeArgString(
251         path::parent_path(ToolChain.getDriver().ClangExecutable) +
252         "/llvm-nm");
253     ArgStringList CreateExportCmdArgs;
254 
255     std::string CreateExportListPath =
256         C.getDriver().GetTemporaryPath("CreateExportList", "exp");
257     const char *ExportList =
258         C.addTempFile(C.getArgs().MakeArgString(CreateExportListPath));
259 
260     for (const auto &II : Inputs)
261       if (II.isFilename())
262         CreateExportCmdArgs.push_back(II.getFilename());
263 
264     CreateExportCmdArgs.push_back("--export-symbols");
265     CreateExportCmdArgs.push_back("-X");
266     if (IsArch32Bit) {
267       CreateExportCmdArgs.push_back("32");
268     } else {
269       // Must be 64-bit, otherwise asserted already.
270       CreateExportCmdArgs.push_back("64");
271     }
272 
273     auto ExpCommand = std::make_unique<Command>(
274         JA, *this, ResponseFileSupport::None(), CreateExportListExec,
275         CreateExportCmdArgs, Inputs, Output);
276     ExpCommand->setRedirectFiles(
277         {std::nullopt, std::string(ExportList), std::nullopt});
278     C.addCommand(std::move(ExpCommand));
279     CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-bE:") + ExportList));
280   }
281 
282   // Add directory to library search path.
283   Args.AddAllArgs(CmdArgs, options::OPT_L);
284   if (!Args.hasArg(options::OPT_r)) {
285     ToolChain.AddFilePathLibArgs(Args, CmdArgs);
286     ToolChain.addProfileRTLibs(Args, CmdArgs);
287 
288     if (getToolChain().ShouldLinkCXXStdlib(Args))
289       getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
290 
291     if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
292       AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
293 
294       // Add OpenMP runtime if -fopenmp is specified.
295       if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
296                        options::OPT_fno_openmp, false)) {
297         switch (ToolChain.getDriver().getOpenMPRuntime(Args)) {
298         case Driver::OMPRT_OMP:
299           CmdArgs.push_back("-lomp");
300           break;
301         case Driver::OMPRT_IOMP5:
302           CmdArgs.push_back("-liomp5");
303           break;
304         case Driver::OMPRT_GOMP:
305           CmdArgs.push_back("-lgomp");
306           break;
307         case Driver::OMPRT_Unknown:
308           // Already diagnosed.
309           break;
310         }
311       }
312 
313       // Support POSIX threads if "-pthreads" or "-pthread" is present.
314       if (Args.hasArg(options::OPT_pthreads, options::OPT_pthread))
315         CmdArgs.push_back("-lpthreads");
316 
317       if (D.CCCIsCXX())
318         CmdArgs.push_back("-lm");
319 
320       CmdArgs.push_back("-lc");
321 
322       if (Args.hasArgNoClaim(options::OPT_p, options::OPT_pg)) {
323         CmdArgs.push_back(Args.MakeArgString((llvm::Twine("-L") + D.SysRoot) +
324                                              "/lib/profiled"));
325         CmdArgs.push_back(Args.MakeArgString((llvm::Twine("-L") + D.SysRoot) +
326                                              "/usr/lib/profiled"));
327       }
328     }
329   }
330 
331   if (D.IsFlangMode()) {
332     addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
333     addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
334     CmdArgs.push_back("-lm");
335     CmdArgs.push_back("-lpthread");
336   }
337   const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
338   C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
339                                          Exec, CmdArgs, Inputs, Output));
340 }
341 
342 /// AIX - AIX tool chain which can call as(1) and ld(1) directly.
343 AIX::AIX(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
344     : ToolChain(D, Triple, Args) {
345   getProgramPaths().push_back(getDriver().getInstalledDir());
346   if (getDriver().getInstalledDir() != getDriver().Dir)
347     getProgramPaths().push_back(getDriver().Dir);
348 
349   ParseInlineAsmUsingAsmParser = Args.hasFlag(
350       options::OPT_fintegrated_as, options::OPT_fno_integrated_as, true);
351   getLibraryPaths().push_back(getDriver().SysRoot + "/usr/lib");
352 }
353 
354 // Returns the effective header sysroot path to use.
355 // This comes from either -isysroot or --sysroot.
356 llvm::StringRef
357 AIX::GetHeaderSysroot(const llvm::opt::ArgList &DriverArgs) const {
358   if (DriverArgs.hasArg(options::OPT_isysroot))
359     return DriverArgs.getLastArgValue(options::OPT_isysroot);
360   if (!getDriver().SysRoot.empty())
361     return getDriver().SysRoot;
362   return "/";
363 }
364 
365 void AIX::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
366                                     ArgStringList &CC1Args) const {
367   // Return if -nostdinc is specified as a driver option.
368   if (DriverArgs.hasArg(options::OPT_nostdinc))
369     return;
370 
371   llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs);
372   const Driver &D = getDriver();
373 
374   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
375     SmallString<128> P(D.ResourceDir);
376     // Add the PowerPC intrinsic headers (<resource>/include/ppc_wrappers)
377     path::append(P, "include", "ppc_wrappers");
378     addSystemInclude(DriverArgs, CC1Args, P);
379     // Add the Clang builtin headers (<resource>/include)
380     addSystemInclude(DriverArgs, CC1Args, path::parent_path(P.str()));
381   }
382 
383   // Return if -nostdlibinc is specified as a driver option.
384   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
385     return;
386 
387   // Add <sysroot>/usr/include.
388   SmallString<128> UP(Sysroot);
389   path::append(UP, "/usr/include");
390   addSystemInclude(DriverArgs, CC1Args, UP.str());
391 }
392 
393 void AIX::AddClangCXXStdlibIncludeArgs(
394     const llvm::opt::ArgList &DriverArgs,
395     llvm::opt::ArgStringList &CC1Args) const {
396 
397   if (DriverArgs.hasArg(options::OPT_nostdinc) ||
398       DriverArgs.hasArg(options::OPT_nostdincxx) ||
399       DriverArgs.hasArg(options::OPT_nostdlibinc))
400     return;
401 
402   switch (GetCXXStdlibType(DriverArgs)) {
403   case ToolChain::CST_Libstdcxx:
404     llvm::report_fatal_error(
405         "picking up libstdc++ headers is unimplemented on AIX");
406   case ToolChain::CST_Libcxx: {
407     llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs);
408     SmallString<128> PathCPP(Sysroot);
409     llvm::sys::path::append(PathCPP, "opt/IBM/openxlCSDK", "include", "c++",
410                             "v1");
411     addSystemInclude(DriverArgs, CC1Args, PathCPP.str());
412     // Required in order to suppress conflicting C++ overloads in the system
413     // libc headers that were used by XL C++.
414     CC1Args.push_back("-D__LIBC_NO_CPP_MATH_OVERLOADS__");
415     return;
416   }
417   }
418 
419   llvm_unreachable("Unexpected C++ library type; only libc++ is supported.");
420 }
421 
422 void AIX::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
423                               llvm::opt::ArgStringList &CmdArgs) const {
424   switch (GetCXXStdlibType(Args)) {
425   case ToolChain::CST_Libstdcxx:
426     llvm::report_fatal_error("linking libstdc++ unimplemented on AIX");
427   case ToolChain::CST_Libcxx:
428     CmdArgs.push_back("-lc++");
429     if (Args.hasArg(options::OPT_fexperimental_library))
430       CmdArgs.push_back("-lc++experimental");
431     CmdArgs.push_back("-lc++abi");
432     return;
433   }
434 
435   llvm_unreachable("Unexpected C++ library type; only libc++ is supported.");
436 }
437 
438 void AIX::addClangTargetOptions(
439     const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CC1Args,
440     Action::OffloadKind DeviceOffloadingKind) const {
441   Args.AddLastArg(CC1Args, options::OPT_mignore_xcoff_visibility);
442   Args.AddLastArg(CC1Args, options::OPT_mdefault_visibility_export_mapping_EQ);
443   Args.addOptInFlag(CC1Args, options::OPT_mxcoff_roptr, options::OPT_mno_xcoff_roptr);
444 
445   if (Args.hasFlag(options::OPT_fxl_pragma_pack,
446                    options::OPT_fno_xl_pragma_pack, true))
447     CC1Args.push_back("-fxl-pragma-pack");
448 }
449 
450 void AIX::addProfileRTLibs(const llvm::opt::ArgList &Args,
451                            llvm::opt::ArgStringList &CmdArgs) const {
452   if (needsProfileRT(Args)) {
453     // Add linker option -u__llvm_profile_runtime to cause runtime
454     // initialization to occur.
455     CmdArgs.push_back(Args.MakeArgString(
456         Twine("-u", llvm::getInstrProfRuntimeHookVarName())));
457 
458     if (const auto *A =
459             Args.getLastArgNoClaim(options::OPT_fprofile_update_EQ)) {
460       StringRef Val = A->getValue();
461       if (Val == "atomic" || Val == "prefer-atomic")
462         CmdArgs.push_back("-latomic");
463     }
464   }
465 
466   ToolChain::addProfileRTLibs(Args, CmdArgs);
467 }
468 
469 ToolChain::CXXStdlibType AIX::GetDefaultCXXStdlibType() const {
470   return ToolChain::CST_Libcxx;
471 }
472 
473 ToolChain::RuntimeLibType AIX::GetDefaultRuntimeLibType() const {
474   return ToolChain::RLT_CompilerRT;
475 }
476 
477 auto AIX::buildAssembler() const -> Tool * { return new aix::Assembler(*this); }
478 
479 auto AIX::buildLinker() const -> Tool * { return new aix::Linker(*this); }
480