1 //===--- ZOS.cpp - z/OS 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 "ZOS.h" 10 #include "CommonArgs.h" 11 #include "clang/Driver/Compilation.h" 12 #include "clang/Driver/Options.h" 13 #include "llvm/Option/ArgList.h" 14 #include "llvm/Support/FileSystem.h" 15 #include "llvm/Support/VirtualFileSystem.h" 16 #include "llvm/Support/WithColor.h" 17 18 using namespace clang; 19 using namespace clang::driver; 20 using namespace clang::driver::tools; 21 using namespace clang::driver::toolchains; 22 using namespace llvm; 23 using namespace llvm::opt; 24 using namespace llvm::sys; 25 26 ZOS::ZOS(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) 27 : ToolChain(D, Triple, Args) {} 28 29 ZOS::~ZOS() {} 30 31 void ZOS::addClangTargetOptions(const ArgList &DriverArgs, 32 ArgStringList &CC1Args, 33 Action::OffloadKind DeviceOffloadKind) const { 34 // Pass "-faligned-alloc-unavailable" only when the user hasn't manually 35 // enabled or disabled aligned allocations. 36 if (!DriverArgs.hasArgNoClaim(options::OPT_faligned_allocation, 37 options::OPT_fno_aligned_allocation)) 38 CC1Args.push_back("-faligned-alloc-unavailable"); 39 40 // Pass "-fno-sized-deallocation" only when the user hasn't manually enabled 41 // or disabled sized deallocations. 42 if (!DriverArgs.hasArgNoClaim(options::OPT_fsized_deallocation, 43 options::OPT_fno_sized_deallocation)) 44 CC1Args.push_back("-fno-sized-deallocation"); 45 } 46 47 void zos::Assembler::ConstructJob(Compilation &C, const JobAction &JA, 48 const InputInfo &Output, 49 const InputInfoList &Inputs, 50 const ArgList &Args, 51 const char *LinkingOutput) const { 52 ArgStringList CmdArgs; 53 54 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler); 55 56 // Specify assembler output file. 57 assert((Output.isFilename() || Output.isNothing()) && "Invalid output."); 58 if (Output.isFilename()) { 59 CmdArgs.push_back("-o"); 60 CmdArgs.push_back(Output.getFilename()); 61 } 62 63 // Specify assembler input file. 64 // The system assembler on z/OS takes exactly one input file. The driver is 65 // expected to invoke as(1) separately for each assembler source input file. 66 if (Inputs.size() != 1) 67 llvm_unreachable("Invalid number of input files."); 68 const InputInfo &II = Inputs[0]; 69 assert((II.isFilename() || II.isNothing()) && "Invalid input."); 70 if (II.isFilename()) 71 CmdArgs.push_back(II.getFilename()); 72 73 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as")); 74 C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(), 75 Exec, CmdArgs, Inputs)); 76 } 77 78 static std::string getLEHLQ(const ArgList &Args) { 79 if (Args.hasArg(options::OPT_mzos_hlq_le_EQ)) { 80 Arg *LEHLQArg = Args.getLastArg(options::OPT_mzos_hlq_le_EQ); 81 StringRef HLQ = LEHLQArg->getValue(); 82 if (!HLQ.empty()) 83 return HLQ.str(); 84 } 85 return "CEE"; 86 } 87 88 static std::string getClangHLQ(const ArgList &Args) { 89 if (Args.hasArg(options::OPT_mzos_hlq_clang_EQ)) { 90 Arg *ClangHLQArg = Args.getLastArg(options::OPT_mzos_hlq_clang_EQ); 91 StringRef HLQ = ClangHLQArg->getValue(); 92 if (!HLQ.empty()) 93 return HLQ.str(); 94 } 95 return getLEHLQ(Args); 96 } 97 98 static std::string getCSSHLQ(const ArgList &Args) { 99 if (Args.hasArg(options::OPT_mzos_hlq_csslib_EQ)) { 100 Arg *CsslibHLQArg = Args.getLastArg(options::OPT_mzos_hlq_csslib_EQ); 101 StringRef HLQ = CsslibHLQArg->getValue(); 102 if (!HLQ.empty()) 103 return HLQ.str(); 104 } 105 return "SYS1"; 106 } 107 108 void zos::Linker::ConstructJob(Compilation &C, const JobAction &JA, 109 const InputInfo &Output, 110 const InputInfoList &Inputs, const ArgList &Args, 111 const char *LinkingOutput) const { 112 const ZOS &ToolChain = static_cast<const ZOS &>(getToolChain()); 113 ArgStringList CmdArgs; 114 115 const bool IsSharedLib = 116 Args.hasFlag(options::OPT_shared, options::OPT_static, false); 117 118 assert((Output.isFilename() || Output.isNothing()) && "Invalid output."); 119 if (Output.isFilename()) { 120 CmdArgs.push_back("-o"); 121 CmdArgs.push_back(Output.getFilename()); 122 } 123 124 SmallString<128> LinkerOptions; 125 LinkerOptions = "AMODE="; 126 LinkerOptions += "64"; 127 LinkerOptions += ",LIST"; 128 LinkerOptions += ",DYNAM=DLL"; 129 LinkerOptions += ",MSGLEVEL=4"; 130 LinkerOptions += ",CASE=MIXED"; 131 LinkerOptions += ",REUS=RENT"; 132 133 CmdArgs.push_back("-b"); 134 CmdArgs.push_back(Args.MakeArgString(LinkerOptions)); 135 136 if (!IsSharedLib) { 137 CmdArgs.push_back("-e"); 138 CmdArgs.push_back("CELQSTRT"); 139 140 CmdArgs.push_back("-O"); 141 CmdArgs.push_back("CELQSTRT"); 142 143 CmdArgs.push_back("-u"); 144 CmdArgs.push_back("CELQMAIN"); 145 } 146 147 // Generate side file if -shared option is present. 148 if (IsSharedLib) { 149 StringRef OutputName = Output.getFilename(); 150 // Strip away the last file suffix in presence from output name and add 151 // a new .x suffix. 152 size_t Suffix = OutputName.find_last_of('.'); 153 const char *SideDeckName = 154 Args.MakeArgString(OutputName.substr(0, Suffix) + ".x"); 155 CmdArgs.push_back("-x"); 156 CmdArgs.push_back(SideDeckName); 157 } else { 158 // We need to direct side file to /dev/null to suppress linker warning when 159 // the object file contains exported symbols, and -shared or 160 // -Wl,-x<sidedeck>.x is not specified. 161 CmdArgs.push_back("-x"); 162 CmdArgs.push_back("/dev/null"); 163 } 164 165 // Add archive library search paths. 166 Args.addAllArgs(CmdArgs, {options::OPT_L, options::OPT_u}); 167 168 ToolChain.AddFilePathLibArgs(Args, CmdArgs); 169 170 // Specify linker input file(s) 171 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); 172 173 // z/OS tool chain depends on LE data sets and the CSSLIB data set. 174 // These data sets can have different high level qualifiers (HLQs) 175 // as each installation can define them differently. 176 177 std::string LEHLQ = getLEHLQ(Args); 178 std::string CsslibHLQ = getCSSHLQ(Args); 179 180 StringRef ld_env_var = StringRef(getenv("_LD_SYSLIB")).trim(); 181 if (ld_env_var.empty()) { 182 CmdArgs.push_back("-S"); 183 CmdArgs.push_back(Args.MakeArgString("//'" + LEHLQ + ".SCEEBND2'")); 184 CmdArgs.push_back("-S"); 185 CmdArgs.push_back(Args.MakeArgString("//'" + CsslibHLQ + ".CSSLIB'")); 186 } 187 188 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { 189 ld_env_var = StringRef(getenv("_LD_SIDE_DECKS")).trim(); 190 if (ld_env_var.empty()) { 191 CmdArgs.push_back( 192 Args.MakeArgString("//'" + LEHLQ + ".SCEELIB(CELQS001)'")); 193 CmdArgs.push_back( 194 Args.MakeArgString("//'" + LEHLQ + ".SCEELIB(CELQS003)'")); 195 } else { 196 SmallVector<StringRef> ld_side_deck; 197 ld_env_var.split(ld_side_deck, ":"); 198 for (StringRef ld_loc : ld_side_deck) { 199 CmdArgs.push_back((ld_loc.str()).c_str()); 200 } 201 } 202 } 203 // Link libc++ library 204 if (ToolChain.ShouldLinkCXXStdlib(Args)) { 205 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs); 206 } 207 208 // Specify compiler-rt library path for linker 209 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) 210 AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args); 211 212 const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath()); 213 C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(), 214 Exec, CmdArgs, Inputs)); 215 } 216 217 ToolChain::RuntimeLibType ZOS::GetDefaultRuntimeLibType() const { 218 return ToolChain::RLT_CompilerRT; 219 } 220 221 ToolChain::CXXStdlibType ZOS::GetDefaultCXXStdlibType() const { 222 return ToolChain::CST_Libcxx; 223 } 224 225 void ZOS::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, 226 llvm::opt::ArgStringList &CmdArgs) const { 227 switch (GetCXXStdlibType(Args)) { 228 case ToolChain::CST_Libstdcxx: 229 llvm::report_fatal_error("linking libstdc++ is unimplemented on z/OS"); 230 break; 231 case ToolChain::CST_Libcxx: { 232 std::string ClangHLQ = getClangHLQ(Args); 233 CmdArgs.push_back( 234 Args.MakeArgString("//'" + ClangHLQ + ".SCEELIB(CRTDQCXE)'")); 235 CmdArgs.push_back( 236 Args.MakeArgString("//'" + ClangHLQ + ".SCEELIB(CRTDQCXS)'")); 237 CmdArgs.push_back( 238 Args.MakeArgString("//'" + ClangHLQ + ".SCEELIB(CRTDQCXP)'")); 239 CmdArgs.push_back( 240 Args.MakeArgString("//'" + ClangHLQ + ".SCEELIB(CRTDQCXA)'")); 241 CmdArgs.push_back( 242 Args.MakeArgString("//'" + ClangHLQ + ".SCEELIB(CRTDQXLA)'")); 243 CmdArgs.push_back( 244 Args.MakeArgString("//'" + ClangHLQ + ".SCEELIB(CRTDQUNW)'")); 245 } break; 246 } 247 } 248 249 auto ZOS::buildAssembler() const -> Tool * { return new zos::Assembler(*this); } 250 251 auto ZOS::buildLinker() const -> Tool * { return new zos::Linker(*this); } 252 253 void ZOS::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 254 ArgStringList &CC1Args) const { 255 if (DriverArgs.hasArg(options::OPT_nostdinc)) 256 return; 257 258 const Driver &D = getDriver(); 259 260 // resolve ResourceDir 261 std::string ResourceDir(D.ResourceDir); 262 263 // zos_wrappers must take highest precedence 264 265 // - <clang>/lib/clang/<ver>/include/zos_wrappers 266 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 267 SmallString<128> P(ResourceDir); 268 path::append(P, "include", "zos_wrappers"); 269 addSystemInclude(DriverArgs, CC1Args, P.str()); 270 271 // - <clang>/lib/clang/<ver>/include 272 SmallString<128> P2(ResourceDir); 273 path::append(P2, "include"); 274 addSystemInclude(DriverArgs, CC1Args, P2.str()); 275 } 276 277 // - /usr/include 278 if (Arg *SysIncludeArg = 279 DriverArgs.getLastArg(options::OPT_mzos_sys_include_EQ)) { 280 StringRef SysInclude = SysIncludeArg->getValue(); 281 282 // fall back to the default include path 283 if (!SysInclude.empty()) { 284 285 // -mzos-sys-include opton can have colon separated 286 // list of paths, so we need to parse the value. 287 StringRef PathLE(SysInclude); 288 size_t Colon = PathLE.find(':'); 289 if (Colon == StringRef::npos) { 290 addSystemInclude(DriverArgs, CC1Args, PathLE.str()); 291 return; 292 } 293 294 while (Colon != StringRef::npos) { 295 SmallString<128> P = PathLE.substr(0, Colon); 296 addSystemInclude(DriverArgs, CC1Args, P.str()); 297 PathLE = PathLE.substr(Colon + 1); 298 Colon = PathLE.find(':'); 299 } 300 if (PathLE.size()) 301 addSystemInclude(DriverArgs, CC1Args, PathLE.str()); 302 303 return; 304 } 305 } 306 307 addSystemInclude(DriverArgs, CC1Args, "/usr/include"); 308 } 309 310 void ZOS::TryAddIncludeFromPath(llvm::SmallString<128> Path, 311 const llvm::opt::ArgList &DriverArgs, 312 llvm::opt::ArgStringList &CC1Args) const { 313 if (!getVFS().exists(Path)) { 314 if (DriverArgs.hasArg(options::OPT_v)) 315 WithColor::warning(errs(), "Clang") 316 << "ignoring nonexistent directory \"" << Path << "\"\n"; 317 if (!DriverArgs.hasArg(options::OPT__HASH_HASH_HASH)) 318 return; 319 } 320 addSystemInclude(DriverArgs, CC1Args, Path); 321 } 322 323 void ZOS::AddClangCXXStdlibIncludeArgs( 324 const llvm::opt::ArgList &DriverArgs, 325 llvm::opt::ArgStringList &CC1Args) const { 326 if (DriverArgs.hasArg(options::OPT_nostdinc) || 327 DriverArgs.hasArg(options::OPT_nostdincxx) || 328 DriverArgs.hasArg(options::OPT_nostdlibinc)) 329 return; 330 331 switch (GetCXXStdlibType(DriverArgs)) { 332 case ToolChain::CST_Libcxx: { 333 // <install>/bin/../include/c++/v1 334 llvm::SmallString<128> InstallBin(getDriver().Dir); 335 llvm::sys::path::append(InstallBin, "..", "include", "c++", "v1"); 336 TryAddIncludeFromPath(InstallBin, DriverArgs, CC1Args); 337 break; 338 } 339 case ToolChain::CST_Libstdcxx: 340 llvm::report_fatal_error( 341 "picking up libstdc++ headers is unimplemented on z/OS"); 342 break; 343 } 344 } 345