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 #include "llvm/Support/Path.h" 17 18 using AIX = clang::driver::toolchains::AIX; 19 using namespace clang::driver; 20 using namespace clang::driver::tools; 21 using namespace clang::driver::toolchains; 22 23 using namespace llvm::opt; 24 using namespace llvm::sys; 25 26 void aix::Assembler::ConstructJob(Compilation &C, const JobAction &JA, 27 const InputInfo &Output, 28 const InputInfoList &Inputs, 29 const ArgList &Args, 30 const char *LinkingOutput) const { 31 ArgStringList CmdArgs; 32 33 const bool IsArch32Bit = getToolChain().getTriple().isArch32Bit(); 34 const bool IsArch64Bit = getToolChain().getTriple().isArch64Bit(); 35 // Only support 32 and 64 bit. 36 if (!IsArch32Bit && !IsArch64Bit) 37 llvm_unreachable("Unsupported bit width value."); 38 39 // Specify the mode in which the as(1) command operates. 40 if (IsArch32Bit) { 41 CmdArgs.push_back("-a32"); 42 } else { 43 // Must be 64-bit, otherwise asserted already. 44 CmdArgs.push_back("-a64"); 45 } 46 47 // Accept any mixture of instructions. 48 // On Power for AIX and Linux, this behaviour matches that of GCC for both the 49 // user-provided assembler source case and the compiler-produced assembler 50 // source case. Yet XL with user-provided assembler source would not add this. 51 CmdArgs.push_back("-many"); 52 53 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler); 54 55 // Specify assembler output file. 56 assert((Output.isFilename() || Output.isNothing()) && "Invalid output."); 57 if (Output.isFilename()) { 58 CmdArgs.push_back("-o"); 59 CmdArgs.push_back(Output.getFilename()); 60 } 61 62 // Specify assembler input file. 63 // The system assembler on AIX takes exactly one input file. The driver is 64 // expected to invoke as(1) separately for each assembler source input file. 65 if (Inputs.size() != 1) 66 llvm_unreachable("Invalid number of input files."); 67 const InputInfo &II = Inputs[0]; 68 assert((II.isFilename() || II.isNothing()) && "Invalid input."); 69 if (II.isFilename()) 70 CmdArgs.push_back(II.getFilename()); 71 72 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as")); 73 C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(), 74 Exec, CmdArgs, Inputs, Output)); 75 } 76 77 void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA, 78 const InputInfo &Output, 79 const InputInfoList &Inputs, const ArgList &Args, 80 const char *LinkingOutput) const { 81 const AIX &ToolChain = static_cast<const AIX &>(getToolChain()); 82 const Driver &D = ToolChain.getDriver(); 83 ArgStringList CmdArgs; 84 85 const bool IsArch32Bit = ToolChain.getTriple().isArch32Bit(); 86 const bool IsArch64Bit = ToolChain.getTriple().isArch64Bit(); 87 // Only support 32 and 64 bit. 88 if (!(IsArch32Bit || IsArch64Bit)) 89 llvm_unreachable("Unsupported bit width value."); 90 91 // Force static linking when "-static" is present. 92 if (Args.hasArg(options::OPT_static)) 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 } 100 101 // PGO instrumentation generates symbols belonging to special sections, and 102 // the linker needs to place all symbols in a particular section together in 103 // memory; the AIX linker does that under an option. 104 if (Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs, 105 false) || 106 Args.hasFlag(options::OPT_fprofile_generate, 107 options::OPT_fno_profile_generate, false) || 108 Args.hasFlag(options::OPT_fprofile_generate_EQ, 109 options::OPT_fno_profile_generate, false) || 110 Args.hasFlag(options::OPT_fprofile_instr_generate, 111 options::OPT_fno_profile_instr_generate, false) || 112 Args.hasFlag(options::OPT_fprofile_instr_generate_EQ, 113 options::OPT_fno_profile_instr_generate, false) || 114 Args.hasFlag(options::OPT_fcs_profile_generate, 115 options::OPT_fno_profile_generate, false) || 116 Args.hasFlag(options::OPT_fcs_profile_generate_EQ, 117 options::OPT_fno_profile_generate, false) || 118 Args.hasArg(options::OPT_fcreate_profile) || 119 Args.hasArg(options::OPT_coverage)) 120 CmdArgs.push_back("-bdbg:namedsects:ss"); 121 122 // Specify linker output file. 123 assert((Output.isFilename() || Output.isNothing()) && "Invalid output."); 124 if (Output.isFilename()) { 125 CmdArgs.push_back("-o"); 126 CmdArgs.push_back(Output.getFilename()); 127 } 128 129 // Set linking mode (i.e., 32/64-bit) and the address of 130 // text and data sections based on arch bit width. 131 if (IsArch32Bit) { 132 CmdArgs.push_back("-b32"); 133 CmdArgs.push_back("-bpT:0x10000000"); 134 CmdArgs.push_back("-bpD:0x20000000"); 135 } else { 136 // Must be 64-bit, otherwise asserted already. 137 CmdArgs.push_back("-b64"); 138 CmdArgs.push_back("-bpT:0x100000000"); 139 CmdArgs.push_back("-bpD:0x110000000"); 140 } 141 142 auto getCrt0Basename = [&Args, IsArch32Bit] { 143 // Enable gprofiling when "-pg" is specified. 144 if (Args.hasArg(options::OPT_pg)) 145 return IsArch32Bit ? "gcrt0.o" : "gcrt0_64.o"; 146 // Enable profiling when "-p" is specified. 147 else if (Args.hasArg(options::OPT_p)) 148 return IsArch32Bit ? "mcrt0.o" : "mcrt0_64.o"; 149 else 150 return IsArch32Bit ? "crt0.o" : "crt0_64.o"; 151 }; 152 153 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles, 154 options::OPT_shared)) { 155 CmdArgs.push_back( 156 Args.MakeArgString(ToolChain.GetFilePath(getCrt0Basename()))); 157 158 CmdArgs.push_back(Args.MakeArgString( 159 ToolChain.GetFilePath(IsArch32Bit ? "crti.o" : "crti_64.o"))); 160 } 161 162 // Collect all static constructor and destructor functions in both C and CXX 163 // language link invocations. This has to come before AddLinkerInputs as the 164 // implied option needs to precede any other '-bcdtors' settings or 165 // '-bnocdtors' that '-Wl' might forward. 166 CmdArgs.push_back("-bcdtors:all:0:s"); 167 168 // Specify linker input file(s). 169 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); 170 171 // Add directory to library search path. 172 Args.AddAllArgs(CmdArgs, options::OPT_L); 173 ToolChain.AddFilePathLibArgs(Args, CmdArgs); 174 ToolChain.addProfileRTLibs(Args, CmdArgs); 175 176 if (getToolChain().ShouldLinkCXXStdlib(Args)) 177 getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs); 178 179 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { 180 AddRunTimeLibs(ToolChain, D, CmdArgs, Args); 181 182 // Support POSIX threads if "-pthreads" or "-pthread" is present. 183 if (Args.hasArg(options::OPT_pthreads, options::OPT_pthread)) 184 CmdArgs.push_back("-lpthreads"); 185 186 if (D.CCCIsCXX()) 187 CmdArgs.push_back("-lm"); 188 189 CmdArgs.push_back("-lc"); 190 } 191 192 const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath()); 193 C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(), 194 Exec, CmdArgs, Inputs, Output)); 195 } 196 197 /// AIX - AIX tool chain which can call as(1) and ld(1) directly. 198 AIX::AIX(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) 199 : ToolChain(D, Triple, Args) { 200 ParseInlineAsmUsingAsmParser = Args.hasFlag( 201 options::OPT_fintegrated_as, options::OPT_fno_integrated_as, true); 202 getLibraryPaths().push_back(getDriver().SysRoot + "/usr/lib"); 203 } 204 205 // Returns the effective header sysroot path to use. 206 // This comes from either -isysroot or --sysroot. 207 llvm::StringRef 208 AIX::GetHeaderSysroot(const llvm::opt::ArgList &DriverArgs) const { 209 if (DriverArgs.hasArg(options::OPT_isysroot)) 210 return DriverArgs.getLastArgValue(options::OPT_isysroot); 211 if (!getDriver().SysRoot.empty()) 212 return getDriver().SysRoot; 213 return "/"; 214 } 215 216 void AIX::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 217 ArgStringList &CC1Args) const { 218 // Return if -nostdinc is specified as a driver option. 219 if (DriverArgs.hasArg(options::OPT_nostdinc)) 220 return; 221 222 llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs); 223 const Driver &D = getDriver(); 224 225 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 226 SmallString<128> P(D.ResourceDir); 227 // Add the PowerPC intrinsic headers (<resource>/include/ppc_wrappers) 228 path::append(P, "include", "ppc_wrappers"); 229 addSystemInclude(DriverArgs, CC1Args, P); 230 // Add the Clang builtin headers (<resource>/include) 231 addSystemInclude(DriverArgs, CC1Args, path::parent_path(P.str())); 232 } 233 234 // Return if -nostdlibinc is specified as a driver option. 235 if (DriverArgs.hasArg(options::OPT_nostdlibinc)) 236 return; 237 238 // Add <sysroot>/usr/include. 239 SmallString<128> UP(Sysroot); 240 path::append(UP, "/usr/include"); 241 addSystemInclude(DriverArgs, CC1Args, UP.str()); 242 } 243 244 void AIX::AddClangCXXStdlibIncludeArgs( 245 const llvm::opt::ArgList &DriverArgs, 246 llvm::opt::ArgStringList &CC1Args) const { 247 248 if (DriverArgs.hasArg(options::OPT_nostdinc) || 249 DriverArgs.hasArg(options::OPT_nostdincxx) || 250 DriverArgs.hasArg(options::OPT_nostdlibinc)) 251 return; 252 253 switch (GetCXXStdlibType(DriverArgs)) { 254 case ToolChain::CST_Libstdcxx: 255 llvm::report_fatal_error( 256 "picking up libstdc++ headers is unimplemented on AIX"); 257 case ToolChain::CST_Libcxx: { 258 llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs); 259 SmallString<128> PathCPP(Sysroot); 260 llvm::sys::path::append(PathCPP, "opt/IBM/openxlCSDK", "include", "c++", 261 "v1"); 262 addSystemInclude(DriverArgs, CC1Args, PathCPP.str()); 263 // Required in order to suppress conflicting C++ overloads in the system 264 // libc headers that were used by XL C++. 265 CC1Args.push_back("-D__LIBC_NO_CPP_MATH_OVERLOADS__"); 266 return; 267 } 268 } 269 270 llvm_unreachable("Unexpected C++ library type; only libc++ is supported."); 271 } 272 273 void AIX::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, 274 llvm::opt::ArgStringList &CmdArgs) const { 275 switch (GetCXXStdlibType(Args)) { 276 case ToolChain::CST_Libstdcxx: 277 llvm::report_fatal_error("linking libstdc++ unimplemented on AIX"); 278 case ToolChain::CST_Libcxx: 279 CmdArgs.push_back("-lc++"); 280 if (Args.hasArg(options::OPT_fexperimental_library)) 281 CmdArgs.push_back("-lc++experimental"); 282 CmdArgs.push_back("-lc++abi"); 283 return; 284 } 285 286 llvm_unreachable("Unexpected C++ library type; only libc++ is supported."); 287 } 288 289 ToolChain::CXXStdlibType AIX::GetDefaultCXXStdlibType() const { 290 return ToolChain::CST_Libcxx; 291 } 292 293 ToolChain::RuntimeLibType AIX::GetDefaultRuntimeLibType() const { 294 return ToolChain::RLT_CompilerRT; 295 } 296 297 auto AIX::buildAssembler() const -> Tool * { return new aix::Assembler(*this); } 298 299 auto AIX::buildLinker() const -> Tool * { return new aix::Linker(*this); } 300