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