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/Option/ArgList.h"
16 
17 using AIX = clang::driver::toolchains::AIX;
18 using namespace clang::driver;
19 using namespace clang::driver::tools;
20 
21 using namespace llvm::opt;
22 
23 void aix::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
24                                   const InputInfo &Output,
25                                   const InputInfoList &Inputs,
26                                   const ArgList &Args,
27                                   const char *LinkingOutput) const {
28   ArgStringList CmdArgs;
29 
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.");
35 
36   // Specify the mode in which the as(1) command operates.
37   if (IsArch32Bit) {
38     CmdArgs.push_back("-a32");
39   } else {
40     // Must be 64-bit, otherwise asserted already.
41     CmdArgs.push_back("-a64");
42   }
43 
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");
49 
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");
55 
56   Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
57 
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());
63   }
64 
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.");
72   if (II.isFilename())
73     CmdArgs.push_back(II.getFilename());
74 
75   const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
76   C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
77 }
78 
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   const Driver &D = ToolChain.getDriver();
85   ArgStringList CmdArgs;
86 
87   const bool IsArch32Bit = ToolChain.getTriple().isArch32Bit();
88   const bool IsArch64Bit = ToolChain.getTriple().isArch64Bit();
89   // Only support 32 and 64 bit.
90   if (!(IsArch32Bit || IsArch64Bit))
91     llvm_unreachable("Unsupported bit width value.");
92 
93   // Force static linking when "-static" is present.
94   if (Args.hasArg(options::OPT_static))
95     CmdArgs.push_back("-bnso");
96 
97   // Specify linker output file.
98   assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
99   if (Output.isFilename()) {
100     CmdArgs.push_back("-o");
101     CmdArgs.push_back(Output.getFilename());
102   }
103 
104   // Set linking mode (i.e., 32/64-bit) and the address of
105   // text and data sections based on arch bit width.
106   if (IsArch32Bit) {
107     CmdArgs.push_back("-b32");
108     CmdArgs.push_back("-bpT:0x10000000");
109     CmdArgs.push_back("-bpD:0x20000000");
110   } else {
111     // Must be 64-bit, otherwise asserted already.
112     CmdArgs.push_back("-b64");
113     CmdArgs.push_back("-bpT:0x100000000");
114     CmdArgs.push_back("-bpD:0x110000000");
115   }
116 
117   auto getCrt0Basename = [&Args, IsArch32Bit] {
118     // Enable gprofiling when "-pg" is specified.
119     if (Args.hasArg(options::OPT_pg))
120       return IsArch32Bit ? "gcrt0.o" : "gcrt0_64.o";
121     // Enable profiling when "-p" is specified.
122     else if (Args.hasArg(options::OPT_p))
123       return IsArch32Bit ? "mcrt0.o" : "mcrt0_64.o";
124     else
125       return IsArch32Bit ? "crt0.o" : "crt0_64.o";
126   };
127 
128   if (!Args.hasArg(options::OPT_nostdlib)) {
129     CmdArgs.push_back(
130         Args.MakeArgString(ToolChain.GetFilePath(getCrt0Basename())));
131   }
132 
133   // Collect all static constructor and destructor functions in CXX mode. This
134   // has to come before AddLinkerInputs as the implied option needs to precede
135   // any other '-bcdtors' settings or '-bnocdtors' that '-Wl' might forward.
136   if (D.CCCIsCXX())
137     CmdArgs.push_back("-bcdtors:all:0:s");
138 
139   // Specify linker input file(s).
140   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
141 
142   // Add directory to library search path.
143   Args.AddAllArgs(CmdArgs, options::OPT_L);
144   ToolChain.AddFilePathLibArgs(Args, CmdArgs);
145 
146   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
147     // Support POSIX threads if "-pthreads" or "-pthread" is present.
148     if (Args.hasArg(options::OPT_pthreads, options::OPT_pthread))
149       CmdArgs.push_back("-lpthreads");
150 
151     CmdArgs.push_back("-lc");
152   }
153 
154   const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
155   C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
156 }
157 
158 /// AIX - AIX tool chain which can call as(1) and ld(1) directly.
159 AIX::AIX(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
160     : ToolChain(D, Triple, Args) {
161   getFilePaths().push_back(getDriver().SysRoot + "/usr/lib");
162 }
163 
164 auto AIX::buildAssembler() const -> Tool * { return new aix::Assembler(*this); }
165 
166 auto AIX::buildLinker() const -> Tool * { return new aix::Linker(*this); }
167