1 //===--- NetBSD.cpp - NetBSD 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 "NetBSD.h" 10 #include "Arch/ARM.h" 11 #include "Arch/Mips.h" 12 #include "Arch/Sparc.h" 13 #include "CommonArgs.h" 14 #include "clang/Driver/Compilation.h" 15 #include "clang/Driver/Driver.h" 16 #include "clang/Driver/Options.h" 17 #include "clang/Driver/SanitizerArgs.h" 18 #include "llvm/Option/ArgList.h" 19 #include "llvm/Support/VirtualFileSystem.h" 20 21 using namespace clang::driver; 22 using namespace clang::driver::tools; 23 using namespace clang::driver::toolchains; 24 using namespace clang; 25 using namespace llvm::opt; 26 27 void netbsd::Assembler::ConstructJob(Compilation &C, const JobAction &JA, 28 const InputInfo &Output, 29 const InputInfoList &Inputs, 30 const ArgList &Args, 31 const char *LinkingOutput) const { 32 claimNoWarnArgs(Args); 33 ArgStringList CmdArgs; 34 35 // GNU as needs different flags for creating the correct output format 36 // on architectures with different ABIs or optional feature sets. 37 switch (getToolChain().getArch()) { 38 case llvm::Triple::x86: 39 CmdArgs.push_back("--32"); 40 break; 41 case llvm::Triple::arm: 42 case llvm::Triple::armeb: 43 case llvm::Triple::thumb: 44 case llvm::Triple::thumbeb: { 45 StringRef MArch, MCPU; 46 arm::getARMArchCPUFromArgs(Args, MArch, MCPU, /*FromAs*/ true); 47 std::string Arch = 48 arm::getARMTargetCPU(MCPU, MArch, getToolChain().getTriple()); 49 CmdArgs.push_back(Args.MakeArgString("-mcpu=" + Arch)); 50 break; 51 } 52 53 case llvm::Triple::mips: 54 case llvm::Triple::mipsel: 55 case llvm::Triple::mips64: 56 case llvm::Triple::mips64el: { 57 StringRef CPUName; 58 StringRef ABIName; 59 mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName); 60 61 CmdArgs.push_back("-march"); 62 CmdArgs.push_back(CPUName.data()); 63 64 CmdArgs.push_back("-mabi"); 65 CmdArgs.push_back(mips::getGnuCompatibleMipsABIName(ABIName).data()); 66 67 if (getToolChain().getTriple().isLittleEndian()) 68 CmdArgs.push_back("-EL"); 69 else 70 CmdArgs.push_back("-EB"); 71 72 AddAssemblerKPIC(getToolChain(), Args, CmdArgs); 73 break; 74 } 75 76 case llvm::Triple::sparc: 77 case llvm::Triple::sparcel: { 78 CmdArgs.push_back("-32"); 79 std::string CPU = getCPUName(Args, getToolChain().getTriple()); 80 CmdArgs.push_back(sparc::getSparcAsmModeForCPU(CPU, getToolChain().getTriple())); 81 AddAssemblerKPIC(getToolChain(), Args, CmdArgs); 82 break; 83 } 84 85 case llvm::Triple::sparcv9: { 86 CmdArgs.push_back("-64"); 87 std::string CPU = getCPUName(Args, getToolChain().getTriple()); 88 CmdArgs.push_back(sparc::getSparcAsmModeForCPU(CPU, getToolChain().getTriple())); 89 AddAssemblerKPIC(getToolChain(), Args, CmdArgs); 90 break; 91 } 92 93 default: 94 break; 95 } 96 97 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler); 98 99 CmdArgs.push_back("-o"); 100 CmdArgs.push_back(Output.getFilename()); 101 102 for (const auto &II : Inputs) 103 CmdArgs.push_back(II.getFilename()); 104 105 const char *Exec = Args.MakeArgString((getToolChain().GetProgramPath("as"))); 106 C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs)); 107 } 108 109 void netbsd::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::NetBSD &ToolChain = 115 static_cast<const toolchains::NetBSD &>(getToolChain()); 116 const Driver &D = ToolChain.getDriver(); 117 ArgStringList CmdArgs; 118 119 if (!D.SysRoot.empty()) 120 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot)); 121 122 CmdArgs.push_back("--eh-frame-hdr"); 123 if (Args.hasArg(options::OPT_static)) { 124 CmdArgs.push_back("-Bstatic"); 125 if (Args.hasArg(options::OPT_pie)) { 126 Args.AddAllArgs(CmdArgs, options::OPT_pie); 127 CmdArgs.push_back("--no-dynamic-linker"); 128 } 129 } else { 130 if (Args.hasArg(options::OPT_rdynamic)) 131 CmdArgs.push_back("-export-dynamic"); 132 if (Args.hasArg(options::OPT_shared)) { 133 CmdArgs.push_back("-Bshareable"); 134 } else { 135 Args.AddAllArgs(CmdArgs, options::OPT_pie); 136 CmdArgs.push_back("-dynamic-linker"); 137 CmdArgs.push_back("/libexec/ld.elf_so"); 138 } 139 } 140 141 // Many NetBSD architectures support more than one ABI. 142 // Determine the correct emulation for ld. 143 switch (ToolChain.getArch()) { 144 case llvm::Triple::x86: 145 CmdArgs.push_back("-m"); 146 CmdArgs.push_back("elf_i386"); 147 break; 148 case llvm::Triple::arm: 149 case llvm::Triple::thumb: 150 CmdArgs.push_back("-m"); 151 switch (ToolChain.getTriple().getEnvironment()) { 152 case llvm::Triple::EABI: 153 case llvm::Triple::GNUEABI: 154 CmdArgs.push_back("armelf_nbsd_eabi"); 155 break; 156 case llvm::Triple::EABIHF: 157 case llvm::Triple::GNUEABIHF: 158 CmdArgs.push_back("armelf_nbsd_eabihf"); 159 break; 160 default: 161 CmdArgs.push_back("armelf_nbsd"); 162 break; 163 } 164 break; 165 case llvm::Triple::armeb: 166 case llvm::Triple::thumbeb: 167 arm::appendBE8LinkFlag(Args, CmdArgs, ToolChain.getEffectiveTriple()); 168 CmdArgs.push_back("-m"); 169 switch (ToolChain.getTriple().getEnvironment()) { 170 case llvm::Triple::EABI: 171 case llvm::Triple::GNUEABI: 172 CmdArgs.push_back("armelfb_nbsd_eabi"); 173 break; 174 case llvm::Triple::EABIHF: 175 case llvm::Triple::GNUEABIHF: 176 CmdArgs.push_back("armelfb_nbsd_eabihf"); 177 break; 178 default: 179 CmdArgs.push_back("armelfb_nbsd"); 180 break; 181 } 182 break; 183 case llvm::Triple::mips64: 184 case llvm::Triple::mips64el: 185 if (mips::hasMipsAbiArg(Args, "32")) { 186 CmdArgs.push_back("-m"); 187 if (ToolChain.getArch() == llvm::Triple::mips64) 188 CmdArgs.push_back("elf32btsmip"); 189 else 190 CmdArgs.push_back("elf32ltsmip"); 191 } else if (mips::hasMipsAbiArg(Args, "64")) { 192 CmdArgs.push_back("-m"); 193 if (ToolChain.getArch() == llvm::Triple::mips64) 194 CmdArgs.push_back("elf64btsmip"); 195 else 196 CmdArgs.push_back("elf64ltsmip"); 197 } 198 break; 199 case llvm::Triple::ppc: 200 CmdArgs.push_back("-m"); 201 CmdArgs.push_back("elf32ppc_nbsd"); 202 break; 203 204 case llvm::Triple::ppc64: 205 case llvm::Triple::ppc64le: 206 CmdArgs.push_back("-m"); 207 CmdArgs.push_back("elf64ppc"); 208 break; 209 210 case llvm::Triple::sparc: 211 CmdArgs.push_back("-m"); 212 CmdArgs.push_back("elf32_sparc"); 213 break; 214 215 case llvm::Triple::sparcv9: 216 CmdArgs.push_back("-m"); 217 CmdArgs.push_back("elf64_sparc"); 218 break; 219 220 default: 221 break; 222 } 223 224 if (Output.isFilename()) { 225 CmdArgs.push_back("-o"); 226 CmdArgs.push_back(Output.getFilename()); 227 } else { 228 assert(Output.isNothing() && "Invalid output."); 229 } 230 231 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) { 232 if (!Args.hasArg(options::OPT_shared)) { 233 CmdArgs.push_back( 234 Args.MakeArgString(ToolChain.GetFilePath("crt0.o"))); 235 } 236 CmdArgs.push_back( 237 Args.MakeArgString(ToolChain.GetFilePath("crti.o"))); 238 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie)) { 239 CmdArgs.push_back( 240 Args.MakeArgString(ToolChain.GetFilePath("crtbeginS.o"))); 241 } else { 242 CmdArgs.push_back( 243 Args.MakeArgString(ToolChain.GetFilePath("crtbegin.o"))); 244 } 245 } 246 247 Args.AddAllArgs(CmdArgs, options::OPT_L); 248 Args.AddAllArgs(CmdArgs, options::OPT_T_Group); 249 Args.AddAllArgs(CmdArgs, options::OPT_e); 250 Args.AddAllArgs(CmdArgs, options::OPT_s); 251 Args.AddAllArgs(CmdArgs, options::OPT_t); 252 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag); 253 Args.AddAllArgs(CmdArgs, options::OPT_r); 254 255 bool NeedsSanitizerDeps = addSanitizerRuntimes(getToolChain(), Args, CmdArgs); 256 bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs); 257 AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA); 258 259 const SanitizerArgs &SanArgs = ToolChain.getSanitizerArgs(); 260 if (SanArgs.needsSharedRt()) { 261 CmdArgs.push_back("-rpath"); 262 CmdArgs.push_back(Args.MakeArgString( 263 ToolChain.getCompilerRTPath().c_str())); 264 } 265 266 unsigned Major, Minor, Micro; 267 ToolChain.getTriple().getOSVersion(Major, Minor, Micro); 268 bool useLibgcc = true; 269 if (Major >= 7 || Major == 0) { 270 switch (ToolChain.getArch()) { 271 case llvm::Triple::aarch64: 272 case llvm::Triple::aarch64_be: 273 case llvm::Triple::arm: 274 case llvm::Triple::armeb: 275 case llvm::Triple::thumb: 276 case llvm::Triple::thumbeb: 277 case llvm::Triple::ppc: 278 case llvm::Triple::ppc64: 279 case llvm::Triple::ppc64le: 280 case llvm::Triple::sparc: 281 case llvm::Triple::sparcv9: 282 case llvm::Triple::x86: 283 case llvm::Triple::x86_64: 284 useLibgcc = false; 285 break; 286 default: 287 break; 288 } 289 } 290 291 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { 292 // Use the static OpenMP runtime with -static-openmp 293 bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) && 294 !Args.hasArg(options::OPT_static); 295 addOpenMPRuntime(CmdArgs, getToolChain(), Args, StaticOpenMP); 296 297 if (D.CCCIsCXX()) { 298 if (ToolChain.ShouldLinkCXXStdlib(Args)) 299 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs); 300 CmdArgs.push_back("-lm"); 301 } 302 if (NeedsSanitizerDeps) 303 linkSanitizerRuntimeDeps(getToolChain(), CmdArgs); 304 if (NeedsXRayDeps) 305 linkXRayRuntimeDeps(ToolChain, CmdArgs); 306 if (Args.hasArg(options::OPT_pthread)) 307 CmdArgs.push_back("-lpthread"); 308 CmdArgs.push_back("-lc"); 309 310 if (useLibgcc) { 311 if (Args.hasArg(options::OPT_static)) { 312 // libgcc_eh depends on libc, so resolve as much as possible, 313 // pull in any new requirements from libc and then get the rest 314 // of libgcc. 315 CmdArgs.push_back("-lgcc_eh"); 316 CmdArgs.push_back("-lc"); 317 CmdArgs.push_back("-lgcc"); 318 } else { 319 CmdArgs.push_back("-lgcc"); 320 CmdArgs.push_back("--as-needed"); 321 CmdArgs.push_back("-lgcc_s"); 322 CmdArgs.push_back("--no-as-needed"); 323 } 324 } 325 } 326 327 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) { 328 if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie)) 329 CmdArgs.push_back( 330 Args.MakeArgString(ToolChain.GetFilePath("crtendS.o"))); 331 else 332 CmdArgs.push_back( 333 Args.MakeArgString(ToolChain.GetFilePath("crtend.o"))); 334 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o"))); 335 } 336 337 ToolChain.addProfileRTLibs(Args, CmdArgs); 338 339 const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath()); 340 C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs)); 341 } 342 343 /// NetBSD - NetBSD tool chain which can call as(1) and ld(1) directly. 344 345 NetBSD::NetBSD(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) 346 : Generic_ELF(D, Triple, Args) { 347 if (!Args.hasArg(options::OPT_nostdlib)) { 348 // When targeting a 32-bit platform, try the special directory used on 349 // 64-bit hosts, and only fall back to the main library directory if that 350 // doesn't work. 351 // FIXME: It'd be nicer to test if this directory exists, but I'm not sure 352 // what all logic is needed to emulate the '=' prefix here. 353 switch (Triple.getArch()) { 354 case llvm::Triple::x86: 355 getFilePaths().push_back("=/usr/lib/i386"); 356 break; 357 case llvm::Triple::arm: 358 case llvm::Triple::armeb: 359 case llvm::Triple::thumb: 360 case llvm::Triple::thumbeb: 361 switch (Triple.getEnvironment()) { 362 case llvm::Triple::EABI: 363 case llvm::Triple::GNUEABI: 364 getFilePaths().push_back("=/usr/lib/eabi"); 365 break; 366 case llvm::Triple::EABIHF: 367 case llvm::Triple::GNUEABIHF: 368 getFilePaths().push_back("=/usr/lib/eabihf"); 369 break; 370 default: 371 getFilePaths().push_back("=/usr/lib/oabi"); 372 break; 373 } 374 break; 375 case llvm::Triple::mips64: 376 case llvm::Triple::mips64el: 377 if (tools::mips::hasMipsAbiArg(Args, "o32")) 378 getFilePaths().push_back("=/usr/lib/o32"); 379 else if (tools::mips::hasMipsAbiArg(Args, "64")) 380 getFilePaths().push_back("=/usr/lib/64"); 381 break; 382 case llvm::Triple::ppc: 383 getFilePaths().push_back("=/usr/lib/powerpc"); 384 break; 385 case llvm::Triple::sparc: 386 getFilePaths().push_back("=/usr/lib/sparc"); 387 break; 388 default: 389 break; 390 } 391 392 getFilePaths().push_back("=/usr/lib"); 393 } 394 } 395 396 Tool *NetBSD::buildAssembler() const { 397 return new tools::netbsd::Assembler(*this); 398 } 399 400 Tool *NetBSD::buildLinker() const { return new tools::netbsd::Linker(*this); } 401 402 ToolChain::CXXStdlibType NetBSD::GetDefaultCXXStdlibType() const { 403 unsigned Major, Minor, Micro; 404 getTriple().getOSVersion(Major, Minor, Micro); 405 if (Major >= 7 || Major == 0) { 406 switch (getArch()) { 407 case llvm::Triple::aarch64: 408 case llvm::Triple::aarch64_be: 409 case llvm::Triple::arm: 410 case llvm::Triple::armeb: 411 case llvm::Triple::thumb: 412 case llvm::Triple::thumbeb: 413 case llvm::Triple::ppc: 414 case llvm::Triple::ppc64: 415 case llvm::Triple::ppc64le: 416 case llvm::Triple::sparc: 417 case llvm::Triple::sparcv9: 418 case llvm::Triple::x86: 419 case llvm::Triple::x86_64: 420 return ToolChain::CST_Libcxx; 421 default: 422 break; 423 } 424 } 425 return ToolChain::CST_Libstdcxx; 426 } 427 428 void NetBSD::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, 429 llvm::opt::ArgStringList &CC1Args) const { 430 const std::string Candidates[] = { 431 // directory relative to build tree 432 getDriver().Dir + "/../include/c++/v1", 433 // system install with full upstream path 434 getDriver().SysRoot + "/usr/include/c++/v1", 435 // system install from src 436 getDriver().SysRoot + "/usr/include/c++", 437 }; 438 439 for (const auto &IncludePath : Candidates) { 440 if (!getVFS().exists(IncludePath + "/__config")) 441 continue; 442 443 // Use the first candidate that looks valid. 444 addSystemInclude(DriverArgs, CC1Args, IncludePath); 445 return; 446 } 447 } 448 449 void NetBSD::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, 450 llvm::opt::ArgStringList &CC1Args) const { 451 addLibStdCXXIncludePaths(getDriver().SysRoot, "/usr/include/g++", "", "", "", 452 "", DriverArgs, CC1Args); 453 } 454 455 llvm::ExceptionHandling NetBSD::GetExceptionModel(const ArgList &Args) const { 456 // NetBSD uses Dwarf exceptions on ARM. 457 llvm::Triple::ArchType TArch = getTriple().getArch(); 458 if (TArch == llvm::Triple::arm || TArch == llvm::Triple::armeb || 459 TArch == llvm::Triple::thumb || TArch == llvm::Triple::thumbeb) 460 return llvm::ExceptionHandling::DwarfCFI; 461 return llvm::ExceptionHandling::None; 462 } 463 464 SanitizerMask NetBSD::getSupportedSanitizers() const { 465 const bool IsX86 = getTriple().getArch() == llvm::Triple::x86; 466 const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64; 467 SanitizerMask Res = ToolChain::getSupportedSanitizers(); 468 if (IsX86 || IsX86_64) { 469 Res |= SanitizerKind::Address; 470 Res |= SanitizerKind::PointerCompare; 471 Res |= SanitizerKind::PointerSubtract; 472 Res |= SanitizerKind::Function; 473 Res |= SanitizerKind::Leak; 474 Res |= SanitizerKind::SafeStack; 475 Res |= SanitizerKind::Scudo; 476 Res |= SanitizerKind::Vptr; 477 } 478 if (IsX86_64) { 479 Res |= SanitizerKind::DataFlow; 480 Res |= SanitizerKind::Fuzzer; 481 Res |= SanitizerKind::FuzzerNoLink; 482 Res |= SanitizerKind::HWAddress; 483 Res |= SanitizerKind::KernelAddress; 484 Res |= SanitizerKind::KernelHWAddress; 485 Res |= SanitizerKind::KernelMemory; 486 Res |= SanitizerKind::Memory; 487 Res |= SanitizerKind::Thread; 488 } 489 return Res; 490 } 491 492 void NetBSD::addClangTargetOptions(const ArgList &DriverArgs, 493 ArgStringList &CC1Args, 494 Action::OffloadKind) const { 495 const SanitizerArgs &SanArgs = getSanitizerArgs(); 496 if (SanArgs.hasAnySanitizer()) 497 CC1Args.push_back("-D_REENTRANT"); 498 499 unsigned Major, Minor, Micro; 500 getTriple().getOSVersion(Major, Minor, Micro); 501 bool UseInitArrayDefault = 502 Major >= 9 || Major == 0 || 503 getTriple().getArch() == llvm::Triple::aarch64 || 504 getTriple().getArch() == llvm::Triple::aarch64_be || 505 getTriple().getArch() == llvm::Triple::arm || 506 getTriple().getArch() == llvm::Triple::armeb; 507 508 if (DriverArgs.hasFlag(options::OPT_fuse_init_array, 509 options::OPT_fno_use_init_array, UseInitArrayDefault)) 510 CC1Args.push_back("-fuse-init-array"); 511 } 512