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   ArgStringList CmdArgs;
85 
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.");
91 
92   // Force static linking when "-static" is present.
93   if (Args.hasArg(options::OPT_static))
94     CmdArgs.push_back("-bnso");
95 
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());
101   }
102 
103   // Set linking mode (i.e., 32/64-bit) and the address of
104   // text and data sections based on arch bit width.
105   if (IsArch32Bit) {
106     CmdArgs.push_back("-b32");
107     CmdArgs.push_back("-bpT:0x10000000");
108     CmdArgs.push_back("-bpD:0x20000000");
109   } else {
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");
114   }
115 
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";
123     else
124       return IsArch32Bit ? "crt0.o" : "crt0_64.o";
125   };
126 
127   if (!Args.hasArg(options::OPT_nostdlib)) {
128     CmdArgs.push_back(
129         Args.MakeArgString(ToolChain.GetFilePath(getCrt0Basename())));
130   }
131 
132   // Specify linker input file(s).
133   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
134 
135   // Add directory to library search path.
136   Args.AddAllArgs(CmdArgs, options::OPT_L);
137   ToolChain.AddFilePathLibArgs(Args, CmdArgs);
138 
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");
143 
144     CmdArgs.push_back("-lc");
145   }
146 
147   const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
148   C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
149 }
150 
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");
155 }
156 
157 auto AIX::buildAssembler() const -> Tool * { return new aix::Assembler(*this); }
158 
159 auto AIX::buildLinker() const -> Tool * { return new aix::Linker(*this); }
160