comparison clang/lib/Driver/ToolChains/AIX.cpp @ 221:79ff65ed7e25

LLVM12 Original
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 15 Jun 2021 19:15:29 +0900
parents 1d019706d866
children 5f17cb93ff66
comparison
equal deleted inserted replaced
220:42394fc6a535 221:79ff65ed7e25
11 #include "CommonArgs.h" 11 #include "CommonArgs.h"
12 #include "clang/Driver/Compilation.h" 12 #include "clang/Driver/Compilation.h"
13 #include "clang/Driver/Options.h" 13 #include "clang/Driver/Options.h"
14 #include "clang/Driver/SanitizerArgs.h" 14 #include "clang/Driver/SanitizerArgs.h"
15 #include "llvm/Option/ArgList.h" 15 #include "llvm/Option/ArgList.h"
16 #include "llvm/Support/Path.h"
16 17
17 using AIX = clang::driver::toolchains::AIX; 18 using AIX = clang::driver::toolchains::AIX;
18 using namespace clang::driver; 19 using namespace clang::driver;
19 using namespace clang::driver::tools; 20 using namespace clang::driver::tools;
21 using namespace clang::driver::toolchains;
20 22
21 using namespace llvm::opt; 23 using namespace llvm::opt;
24 using namespace llvm::sys;
22 25
23 void aix::Assembler::ConstructJob(Compilation &C, const JobAction &JA, 26 void aix::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
24 const InputInfo &Output, 27 const InputInfo &Output,
25 const InputInfoList &Inputs, 28 const InputInfoList &Inputs,
26 const ArgList &Args, 29 const ArgList &Args,
38 CmdArgs.push_back("-a32"); 41 CmdArgs.push_back("-a32");
39 } else { 42 } else {
40 // Must be 64-bit, otherwise asserted already. 43 // Must be 64-bit, otherwise asserted already.
41 CmdArgs.push_back("-a64"); 44 CmdArgs.push_back("-a64");
42 } 45 }
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 46
50 // Accept any mixture of instructions. 47 // Accept any mixture of instructions.
51 // On Power for AIX and Linux, this behaviour matches that of GCC for both the 48 // 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 49 // user-provided assembler source case and the compiler-produced assembler
53 // source case. Yet XL with user-provided assembler source would not add this. 50 // source case. Yet XL with user-provided assembler source would not add this.
71 assert((II.isFilename() || II.isNothing()) && "Invalid input."); 68 assert((II.isFilename() || II.isNothing()) && "Invalid input.");
72 if (II.isFilename()) 69 if (II.isFilename())
73 CmdArgs.push_back(II.getFilename()); 70 CmdArgs.push_back(II.getFilename());
74 71
75 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as")); 72 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
76 C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs)); 73 C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
74 Exec, CmdArgs, Inputs, Output));
77 } 75 }
78 76
79 void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA, 77 void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA,
80 const InputInfo &Output, 78 const InputInfo &Output,
81 const InputInfoList &Inputs, const ArgList &Args, 79 const InputInfoList &Inputs, const ArgList &Args,
82 const char *LinkingOutput) const { 80 const char *LinkingOutput) const {
83 const AIX &ToolChain = static_cast<const AIX &>(getToolChain()); 81 const AIX &ToolChain = static_cast<const AIX &>(getToolChain());
82 const Driver &D = ToolChain.getDriver();
84 ArgStringList CmdArgs; 83 ArgStringList CmdArgs;
85 84
86 const bool IsArch32Bit = ToolChain.getTriple().isArch32Bit(); 85 const bool IsArch32Bit = ToolChain.getTriple().isArch32Bit();
87 const bool IsArch64Bit = ToolChain.getTriple().isArch64Bit(); 86 const bool IsArch64Bit = ToolChain.getTriple().isArch64Bit();
88 // Only support 32 and 64 bit. 87 // Only support 32 and 64 bit.
90 llvm_unreachable("Unsupported bit width value."); 89 llvm_unreachable("Unsupported bit width value.");
91 90
92 // Force static linking when "-static" is present. 91 // Force static linking when "-static" is present.
93 if (Args.hasArg(options::OPT_static)) 92 if (Args.hasArg(options::OPT_static))
94 CmdArgs.push_back("-bnso"); 93 CmdArgs.push_back("-bnso");
94
95 // Add options for shared libraries.
96 if (Args.hasArg(options::OPT_shared)) {
97 CmdArgs.push_back("-bM:SRE");
98 CmdArgs.push_back("-bnoentry");
99 }
95 100
96 // Specify linker output file. 101 // Specify linker output file.
97 assert((Output.isFilename() || Output.isNothing()) && "Invalid output."); 102 assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
98 if (Output.isFilename()) { 103 if (Output.isFilename()) {
99 CmdArgs.push_back("-o"); 104 CmdArgs.push_back("-o");
122 return IsArch32Bit ? "mcrt0.o" : "mcrt0_64.o"; 127 return IsArch32Bit ? "mcrt0.o" : "mcrt0_64.o";
123 else 128 else
124 return IsArch32Bit ? "crt0.o" : "crt0_64.o"; 129 return IsArch32Bit ? "crt0.o" : "crt0_64.o";
125 }; 130 };
126 131
127 if (!Args.hasArg(options::OPT_nostdlib)) { 132 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
133 options::OPT_shared)) {
128 CmdArgs.push_back( 134 CmdArgs.push_back(
129 Args.MakeArgString(ToolChain.GetFilePath(getCrt0Basename()))); 135 Args.MakeArgString(ToolChain.GetFilePath(getCrt0Basename())));
130 } 136
137 CmdArgs.push_back(Args.MakeArgString(
138 ToolChain.GetFilePath(IsArch32Bit ? "crti.o" : "crti_64.o")));
139 }
140
141 // Collect all static constructor and destructor functions in both C and CXX
142 // language link invocations. This has to come before AddLinkerInputs as the
143 // implied option needs to precede any other '-bcdtors' settings or
144 // '-bnocdtors' that '-Wl' might forward.
145 CmdArgs.push_back("-bcdtors:all:0:s");
131 146
132 // Specify linker input file(s). 147 // Specify linker input file(s).
133 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); 148 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
134 149
135 // Add directory to library search path. 150 // Add directory to library search path.
136 Args.AddAllArgs(CmdArgs, options::OPT_L); 151 Args.AddAllArgs(CmdArgs, options::OPT_L);
137 ToolChain.AddFilePathLibArgs(Args, CmdArgs); 152 ToolChain.AddFilePathLibArgs(Args, CmdArgs);
153 ToolChain.addProfileRTLibs(Args, CmdArgs);
154
155 if (getToolChain().ShouldLinkCXXStdlib(Args))
156 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
138 157
139 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { 158 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
159 AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
160
140 // Support POSIX threads if "-pthreads" or "-pthread" is present. 161 // Support POSIX threads if "-pthreads" or "-pthread" is present.
141 if (Args.hasArg(options::OPT_pthreads, options::OPT_pthread)) 162 if (Args.hasArg(options::OPT_pthreads, options::OPT_pthread))
142 CmdArgs.push_back("-lpthreads"); 163 CmdArgs.push_back("-lpthreads");
143 164
165 if (D.CCCIsCXX())
166 CmdArgs.push_back("-lm");
167
144 CmdArgs.push_back("-lc"); 168 CmdArgs.push_back("-lc");
145 } 169 }
146 170
147 const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath()); 171 const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
148 C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs)); 172 C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
173 Exec, CmdArgs, Inputs, Output));
149 } 174 }
150 175
151 /// AIX - AIX tool chain which can call as(1) and ld(1) directly. 176 /// 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) 177 AIX::AIX(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
153 : ToolChain(D, Triple, Args) { 178 : ToolChain(D, Triple, Args) {
154 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib"); 179 getLibraryPaths().push_back(getDriver().SysRoot + "/usr/lib");
180 }
181
182 // Returns the effective header sysroot path to use.
183 // This comes from either -isysroot or --sysroot.
184 llvm::StringRef
185 AIX::GetHeaderSysroot(const llvm::opt::ArgList &DriverArgs) const {
186 if (DriverArgs.hasArg(options::OPT_isysroot))
187 return DriverArgs.getLastArgValue(options::OPT_isysroot);
188 if (!getDriver().SysRoot.empty())
189 return getDriver().SysRoot;
190 return "/";
191 }
192
193 void AIX::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
194 ArgStringList &CC1Args) const {
195 // Return if -nostdinc is specified as a driver option.
196 if (DriverArgs.hasArg(options::OPT_nostdinc))
197 return;
198
199 llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs);
200 const Driver &D = getDriver();
201
202 // Add the Clang builtin headers (<resource>/include).
203 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
204 SmallString<128> P(D.ResourceDir);
205 path::append(P, "/include");
206 addSystemInclude(DriverArgs, CC1Args, P.str());
207 }
208
209 // Return if -nostdlibinc is specified as a driver option.
210 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
211 return;
212
213 // Add <sysroot>/usr/include.
214 SmallString<128> UP(Sysroot);
215 path::append(UP, "/usr/include");
216 addSystemInclude(DriverArgs, CC1Args, UP.str());
217 }
218
219 void AIX::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
220 llvm::opt::ArgStringList &CmdArgs) const {
221 switch (GetCXXStdlibType(Args)) {
222 case ToolChain::CST_Libcxx:
223 CmdArgs.push_back("-lc++");
224 CmdArgs.push_back("-lc++abi");
225 return;
226 case ToolChain::CST_Libstdcxx:
227 llvm::report_fatal_error("linking libstdc++ unimplemented on AIX");
228 }
229
230 llvm_unreachable("Unexpected C++ library type; only libc++ is supported.");
231 }
232
233 ToolChain::CXXStdlibType AIX::GetDefaultCXXStdlibType() const {
234 return ToolChain::CST_Libcxx;
235 }
236
237 ToolChain::RuntimeLibType AIX::GetDefaultRuntimeLibType() const {
238 return ToolChain::RLT_CompilerRT;
155 } 239 }
156 240
157 auto AIX::buildAssembler() const -> Tool * { return new aix::Assembler(*this); } 241 auto AIX::buildAssembler() const -> Tool * { return new aix::Assembler(*this); }
158 242
159 auto AIX::buildLinker() const -> Tool * { return new aix::Linker(*this); } 243 auto AIX::buildLinker() const -> Tool * { return new aix::Linker(*this); }