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