1 //===--- OpenBSD.cpp - OpenBSD 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 "OpenBSD.h" 10 #include "Arch/ARM.h" 11 #include "Arch/Mips.h" 12 #include "Arch/Sparc.h" 13 #include "CommonArgs.h" 14 #include "clang/Config/config.h" 15 #include "clang/Driver/Compilation.h" 16 #include "clang/Driver/Options.h" 17 #include "clang/Driver/SanitizerArgs.h" 18 #include "llvm/Option/ArgList.h" 19 #include "llvm/Support/Path.h" 20 #include "llvm/Support/VirtualFileSystem.h" 21 22 using namespace clang::driver; 23 using namespace clang::driver::tools; 24 using namespace clang::driver::toolchains; 25 using namespace clang; 26 using namespace llvm::opt; 27 28 void openbsd::Assembler::ConstructJob(Compilation &C, const JobAction &JA, 29 const InputInfo &Output, 30 const InputInfoList &Inputs, 31 const ArgList &Args, 32 const char *LinkingOutput) const { 33 const toolchains::OpenBSD &ToolChain = 34 static_cast<const toolchains::OpenBSD &>(getToolChain()); 35 const Driver &D = ToolChain.getDriver(); 36 const llvm::Triple &Triple = ToolChain.getTriple(); 37 38 claimNoWarnArgs(Args); 39 ArgStringList CmdArgs; 40 41 switch (ToolChain.getArch()) { 42 case llvm::Triple::x86: 43 // When building 32-bit code on OpenBSD/amd64, we have to explicitly 44 // instruct as in the base system to assemble 32-bit code. 45 CmdArgs.push_back("--32"); 46 break; 47 48 case llvm::Triple::arm: 49 case llvm::Triple::armeb: { 50 StringRef MArch, MCPU; 51 arm::getARMArchCPUFromArgs(Args, MArch, MCPU, /*FromAs*/ true); 52 std::string Arch = arm::getARMTargetCPU(MCPU, MArch, Triple); 53 CmdArgs.push_back(Args.MakeArgString("-mcpu=" + Arch)); 54 break; 55 } 56 57 case llvm::Triple::ppc: 58 CmdArgs.push_back("-mppc"); 59 CmdArgs.push_back("-many"); 60 break; 61 62 case llvm::Triple::sparcv9: { 63 CmdArgs.push_back("-64"); 64 std::string CPU = getCPUName(D, Args, Triple); 65 CmdArgs.push_back(sparc::getSparcAsmModeForCPU(CPU, Triple)); 66 AddAssemblerKPIC(ToolChain, Args, CmdArgs); 67 break; 68 } 69 70 case llvm::Triple::mips64: 71 case llvm::Triple::mips64el: { 72 StringRef CPUName; 73 StringRef ABIName; 74 mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName); 75 76 CmdArgs.push_back("-march"); 77 CmdArgs.push_back(CPUName.data()); 78 79 CmdArgs.push_back("-mabi"); 80 CmdArgs.push_back(mips::getGnuCompatibleMipsABIName(ABIName).data()); 81 82 if (Triple.isLittleEndian()) 83 CmdArgs.push_back("-EL"); 84 else 85 CmdArgs.push_back("-EB"); 86 87 AddAssemblerKPIC(ToolChain, Args, CmdArgs); 88 break; 89 } 90 91 default: 92 break; 93 } 94 95 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler); 96 97 CmdArgs.push_back("-o"); 98 CmdArgs.push_back(Output.getFilename()); 99 100 for (const auto &II : Inputs) 101 CmdArgs.push_back(II.getFilename()); 102 103 const char *Exec = Args.MakeArgString(ToolChain.GetProgramPath("as")); 104 C.addCommand(std::make_unique<Command>(JA, *this, 105 ResponseFileSupport::AtFileCurCP(), 106 Exec, CmdArgs, Inputs, Output)); 107 } 108 109 void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA, 110 const InputInfo &Output, 111 const InputInfoList &Inputs, 112 const ArgList &Args, 113 const char *LinkingOutput) const { 114 const toolchains::OpenBSD &ToolChain = 115 static_cast<const toolchains::OpenBSD &>(getToolChain()); 116 const Driver &D = ToolChain.getDriver(); 117 const llvm::Triple::ArchType Arch = ToolChain.getArch(); 118 ArgStringList CmdArgs; 119 120 // Silence warning for "clang -g foo.o -o foo" 121 Args.ClaimAllArgs(options::OPT_g_Group); 122 // and "clang -emit-llvm foo.o -o foo" 123 Args.ClaimAllArgs(options::OPT_emit_llvm); 124 // and for "clang -w foo.o -o foo". Other warning options are already 125 // handled somewhere else. 126 Args.ClaimAllArgs(options::OPT_w); 127 128 if (!D.SysRoot.empty()) 129 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot)); 130 131 if (Arch == llvm::Triple::mips64) 132 CmdArgs.push_back("-EB"); 133 else if (Arch == llvm::Triple::mips64el) 134 CmdArgs.push_back("-EL"); 135 136 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_shared)) { 137 CmdArgs.push_back("-e"); 138 CmdArgs.push_back("__start"); 139 } 140 141 CmdArgs.push_back("--eh-frame-hdr"); 142 if (Args.hasArg(options::OPT_static)) { 143 CmdArgs.push_back("-Bstatic"); 144 } else { 145 if (Args.hasArg(options::OPT_rdynamic)) 146 CmdArgs.push_back("-export-dynamic"); 147 CmdArgs.push_back("-Bdynamic"); 148 if (Args.hasArg(options::OPT_shared)) { 149 CmdArgs.push_back("-shared"); 150 } else if (!Args.hasArg(options::OPT_r)) { 151 CmdArgs.push_back("-dynamic-linker"); 152 CmdArgs.push_back("/usr/libexec/ld.so"); 153 } 154 } 155 156 if (Args.hasArg(options::OPT_pie)) 157 CmdArgs.push_back("-pie"); 158 if (Args.hasArg(options::OPT_nopie) || Args.hasArg(options::OPT_pg)) 159 CmdArgs.push_back("-nopie"); 160 161 if (Arch == llvm::Triple::riscv64) 162 CmdArgs.push_back("-X"); 163 164 if (Output.isFilename()) { 165 CmdArgs.push_back("-o"); 166 CmdArgs.push_back(Output.getFilename()); 167 } else { 168 assert(Output.isNothing() && "Invalid output."); 169 } 170 171 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles, 172 options::OPT_r)) { 173 const char *crt0 = nullptr; 174 const char *crtbegin = nullptr; 175 if (!Args.hasArg(options::OPT_shared)) { 176 if (Args.hasArg(options::OPT_pg)) 177 crt0 = "gcrt0.o"; 178 else if (Args.hasArg(options::OPT_static) && 179 !Args.hasArg(options::OPT_nopie)) 180 crt0 = "rcrt0.o"; 181 else 182 crt0 = "crt0.o"; 183 crtbegin = "crtbegin.o"; 184 } else { 185 crtbegin = "crtbeginS.o"; 186 } 187 188 if (crt0) 189 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt0))); 190 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin))); 191 } 192 193 Args.AddAllArgs(CmdArgs, options::OPT_L); 194 ToolChain.AddFilePathLibArgs(Args, CmdArgs); 195 Args.AddAllArgs(CmdArgs, {options::OPT_T_Group, options::OPT_e, 196 options::OPT_s, options::OPT_t, 197 options::OPT_Z_Flag, options::OPT_r}); 198 199 bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs); 200 bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs); 201 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); 202 203 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs, 204 options::OPT_r)) { 205 // Use the static OpenMP runtime with -static-openmp 206 bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) && 207 !Args.hasArg(options::OPT_static); 208 addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP); 209 210 if (D.CCCIsCXX()) { 211 if (ToolChain.ShouldLinkCXXStdlib(Args)) 212 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs); 213 if (Args.hasArg(options::OPT_pg)) 214 CmdArgs.push_back("-lm_p"); 215 else 216 CmdArgs.push_back("-lm"); 217 } 218 if (NeedsSanitizerDeps) { 219 CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins")); 220 linkSanitizerRuntimeDeps(ToolChain, CmdArgs); 221 } 222 if (NeedsXRayDeps) { 223 CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins")); 224 linkXRayRuntimeDeps(ToolChain, CmdArgs); 225 } 226 // FIXME: For some reason GCC passes -lgcc before adding 227 // the default system libraries. Just mimic this for now. 228 CmdArgs.push_back("-lcompiler_rt"); 229 230 if (Args.hasArg(options::OPT_pthread)) { 231 if (!Args.hasArg(options::OPT_shared) && Args.hasArg(options::OPT_pg)) 232 CmdArgs.push_back("-lpthread_p"); 233 else 234 CmdArgs.push_back("-lpthread"); 235 } 236 237 if (!Args.hasArg(options::OPT_shared)) { 238 if (Args.hasArg(options::OPT_pg)) 239 CmdArgs.push_back("-lc_p"); 240 else 241 CmdArgs.push_back("-lc"); 242 } 243 244 CmdArgs.push_back("-lcompiler_rt"); 245 } 246 247 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles, 248 options::OPT_r)) { 249 const char *crtend = nullptr; 250 if (!Args.hasArg(options::OPT_shared)) 251 crtend = "crtend.o"; 252 else 253 crtend = "crtendS.o"; 254 255 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend))); 256 } 257 258 ToolChain.addProfileRTLibs(Args, CmdArgs); 259 260 const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath()); 261 C.addCommand(std::make_unique<Command>(JA, *this, 262 ResponseFileSupport::AtFileCurCP(), 263 Exec, CmdArgs, Inputs, Output)); 264 } 265 266 SanitizerMask OpenBSD::getSupportedSanitizers() const { 267 const bool IsX86 = getTriple().getArch() == llvm::Triple::x86; 268 const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64; 269 270 // For future use, only UBsan at the moment 271 SanitizerMask Res = ToolChain::getSupportedSanitizers(); 272 273 if (IsX86 || IsX86_64) { 274 Res |= SanitizerKind::Vptr; 275 Res |= SanitizerKind::Fuzzer; 276 Res |= SanitizerKind::FuzzerNoLink; 277 } 278 279 return Res; 280 } 281 282 /// OpenBSD - OpenBSD tool chain which can call as(1) and ld(1) directly. 283 284 OpenBSD::OpenBSD(const Driver &D, const llvm::Triple &Triple, 285 const ArgList &Args) 286 : Generic_ELF(D, Triple, Args) { 287 getFilePaths().push_back(concat(getDriver().SysRoot, "/usr/lib")); 288 } 289 290 void OpenBSD::AddClangSystemIncludeArgs( 291 const llvm::opt::ArgList &DriverArgs, 292 llvm::opt::ArgStringList &CC1Args) const { 293 const Driver &D = getDriver(); 294 295 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)) 296 return; 297 298 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 299 SmallString<128> Dir(D.ResourceDir); 300 llvm::sys::path::append(Dir, "include"); 301 addSystemInclude(DriverArgs, CC1Args, Dir.str()); 302 } 303 304 if (DriverArgs.hasArg(options::OPT_nostdlibinc)) 305 return; 306 307 // Check for configure-time C include directories. 308 StringRef CIncludeDirs(C_INCLUDE_DIRS); 309 if (CIncludeDirs != "") { 310 SmallVector<StringRef, 5> dirs; 311 CIncludeDirs.split(dirs, ":"); 312 for (StringRef dir : dirs) { 313 StringRef Prefix = 314 llvm::sys::path::is_absolute(dir) ? StringRef(D.SysRoot) : ""; 315 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir); 316 } 317 return; 318 } 319 320 addExternCSystemInclude(DriverArgs, CC1Args, 321 concat(D.SysRoot, "/usr/include")); 322 } 323 324 void OpenBSD::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, 325 llvm::opt::ArgStringList &CC1Args) const { 326 addSystemInclude(DriverArgs, CC1Args, 327 concat(getDriver().SysRoot, "/usr/include/c++/v1")); 328 } 329 330 void OpenBSD::AddCXXStdlibLibArgs(const ArgList &Args, 331 ArgStringList &CmdArgs) const { 332 bool Profiling = Args.hasArg(options::OPT_pg); 333 334 CmdArgs.push_back(Profiling ? "-lc++_p" : "-lc++"); 335 if (Args.hasArg(options::OPT_fexperimental_library)) 336 CmdArgs.push_back("-lc++experimental"); 337 CmdArgs.push_back(Profiling ? "-lc++abi_p" : "-lc++abi"); 338 CmdArgs.push_back(Profiling ? "-lpthread_p" : "-lpthread"); 339 } 340 341 std::string OpenBSD::getCompilerRT(const ArgList &Args, StringRef Component, 342 FileType Type) const { 343 if (Component == "builtins") { 344 SmallString<128> Path(getDriver().SysRoot); 345 llvm::sys::path::append(Path, "/usr/lib/libcompiler_rt.a"); 346 return std::string(Path.str()); 347 } 348 SmallString<128> P(getDriver().ResourceDir); 349 std::string CRTBasename = 350 buildCompilerRTBasename(Args, Component, Type, /*AddArch=*/false); 351 llvm::sys::path::append(P, "lib", CRTBasename); 352 // Checks if this is the base system case which uses a different location. 353 if (getVFS().exists(P)) 354 return std::string(P.str()); 355 return ToolChain::getCompilerRT(Args, Component, Type); 356 } 357 358 Tool *OpenBSD::buildAssembler() const { 359 return new tools::openbsd::Assembler(*this); 360 } 361 362 Tool *OpenBSD::buildLinker() const { return new tools::openbsd::Linker(*this); } 363 364 bool OpenBSD::HasNativeLLVMSupport() const { return true; } 365 366 bool OpenBSD::IsUnwindTablesDefault(const ArgList &Args) const { 367 switch (getArch()) { 368 case llvm::Triple::arm: 369 return false; 370 default: 371 return true; 372 } 373 } 374