1 //===--- AIX.cpp - AIX ToolChain Implementations ----------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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/Option/ArgList.h"
17 using AIX = clang::driver::toolchains::AIX;
18 using namespace clang::driver;
19 using namespace clang::driver::tools;
21 using namespace llvm::opt;
23 void aix::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
24 const InputInfo &Output,
25 const InputInfoList &Inputs,
27 const char *LinkingOutput) const {
28 ArgStringList CmdArgs;
30 const bool IsArch32Bit = getToolChain().getTriple().isArch32Bit();
31 const bool IsArch64Bit = getToolChain().getTriple().isArch64Bit();
32 // Only support 32 and 64 bit.
33 if (!IsArch32Bit && !IsArch64Bit)
34 llvm_unreachable("Unsupported bit width value.");
36 // Specify the mode in which the as(1) command operates.
38 CmdArgs.push_back("-a32");
40 // Must be 64-bit, otherwise asserted already.
41 CmdArgs.push_back("-a64");
44 // Accept an undefined symbol as an extern so that an error message is not
45 // displayed. Otherwise, undefined symbols are flagged with error messages.
46 // FIXME: This should be removed when the assembly generation from the
47 // compiler is able to write externs properly.
48 CmdArgs.push_back("-u");
50 // Accept any mixture of instructions.
51 // On Power for AIX and Linux, this behaviour matches that of GCC for both the
52 // user-provided assembler source case and the compiler-produced assembler
53 // source case. Yet XL with user-provided assembler source would not add this.
54 CmdArgs.push_back("-many");
56 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
58 // Specify assembler output file.
59 assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
60 if (Output.isFilename()) {
61 CmdArgs.push_back("-o");
62 CmdArgs.push_back(Output.getFilename());
65 // Specify assembler input file.
66 // The system assembler on AIX takes exactly one input file. The driver is
67 // expected to invoke as(1) separately for each assembler source input file.
68 if (Inputs.size() != 1)
69 llvm_unreachable("Invalid number of input files.");
70 const InputInfo &II = Inputs[0];
71 assert((II.isFilename() || II.isNothing()) && "Invalid input.");
73 CmdArgs.push_back(II.getFilename());
75 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
76 C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
79 void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA,
80 const InputInfo &Output,
81 const InputInfoList &Inputs, const ArgList &Args,
82 const char *LinkingOutput) const {
83 const AIX &ToolChain = static_cast<const AIX &>(getToolChain());
84 ArgStringList CmdArgs;
86 const bool IsArch32Bit = ToolChain.getTriple().isArch32Bit();
87 const bool IsArch64Bit = ToolChain.getTriple().isArch64Bit();
88 // Only support 32 and 64 bit.
89 if (!(IsArch32Bit || IsArch64Bit))
90 llvm_unreachable("Unsupported bit width value.");
92 // Force static linking when "-static" is present.
93 if (Args.hasArg(options::OPT_static))
94 CmdArgs.push_back("-bnso");
96 // Specify linker output file.
97 assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
98 if (Output.isFilename()) {
99 CmdArgs.push_back("-o");
100 CmdArgs.push_back(Output.getFilename());
103 // Set linking mode (i.e., 32/64-bit) and the address of
104 // text and data sections based on arch bit width.
106 CmdArgs.push_back("-b32");
107 CmdArgs.push_back("-bpT:0x10000000");
108 CmdArgs.push_back("-bpD:0x20000000");
110 // Must be 64-bit, otherwise asserted already.
111 CmdArgs.push_back("-b64");
112 CmdArgs.push_back("-bpT:0x100000000");
113 CmdArgs.push_back("-bpD:0x110000000");
116 auto getCrt0Basename = [&Args, IsArch32Bit] {
117 // Enable gprofiling when "-pg" is specified.
118 if (Args.hasArg(options::OPT_pg))
119 return IsArch32Bit ? "gcrt0.o" : "gcrt0_64.o";
120 // Enable profiling when "-p" is specified.
121 else if (Args.hasArg(options::OPT_p))
122 return IsArch32Bit ? "mcrt0.o" : "mcrt0_64.o";
124 return IsArch32Bit ? "crt0.o" : "crt0_64.o";
127 if (!Args.hasArg(options::OPT_nostdlib)) {
129 Args.MakeArgString(ToolChain.GetFilePath(getCrt0Basename())));
132 // Specify linker input file(s).
133 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
135 // Add directory to library search path.
136 Args.AddAllArgs(CmdArgs, options::OPT_L);
137 ToolChain.AddFilePathLibArgs(Args, CmdArgs);
139 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
140 // Support POSIX threads if "-pthreads" or "-pthread" is present.
141 if (Args.hasArg(options::OPT_pthreads, options::OPT_pthread))
142 CmdArgs.push_back("-lpthreads");
144 CmdArgs.push_back("-lc");
147 const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
148 C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
151 /// AIX - AIX tool chain which can call as(1) and ld(1) directly.
152 AIX::AIX(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
153 : ToolChain(D, Triple, Args) {
154 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
157 auto AIX::buildAssembler() const -> Tool * { return new aix::Assembler(*this); }
159 auto AIX::buildLinker() const -> Tool * { return new aix::Linker(*this); }