1 //===--- FreeBSD.cpp - FreeBSD 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 "FreeBSD.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/DriverDiagnostic.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 freebsd::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 const auto &D = getToolChain().getDriver(); 35 36 // When building 32-bit code on FreeBSD/amd64, we have to explicitly 37 // instruct as in the base system to assemble 32-bit code. 38 switch (getToolChain().getArch()) { 39 default: 40 break; 41 case llvm::Triple::x86: 42 CmdArgs.push_back("--32"); 43 break; 44 case llvm::Triple::ppc: 45 CmdArgs.push_back("-a32"); 46 break; 47 case llvm::Triple::mips: 48 case llvm::Triple::mipsel: 49 case llvm::Triple::mips64: 50 case llvm::Triple::mips64el: { 51 StringRef CPUName; 52 StringRef ABIName; 53 mips::getMipsCPUAndABI(Args, getToolChain().getTriple(), CPUName, ABIName); 54 55 CmdArgs.push_back("-march"); 56 CmdArgs.push_back(CPUName.data()); 57 58 CmdArgs.push_back("-mabi"); 59 CmdArgs.push_back(mips::getGnuCompatibleMipsABIName(ABIName).data()); 60 61 if (getToolChain().getTriple().isLittleEndian()) 62 CmdArgs.push_back("-EL"); 63 else 64 CmdArgs.push_back("-EB"); 65 66 if (Arg *A = Args.getLastArg(options::OPT_G)) { 67 StringRef v = A->getValue(); 68 CmdArgs.push_back(Args.MakeArgString("-G" + v)); 69 A->claim(); 70 } 71 72 AddAssemblerKPIC(getToolChain(), Args, CmdArgs); 73 break; 74 } 75 case llvm::Triple::arm: 76 case llvm::Triple::armeb: 77 case llvm::Triple::thumb: 78 case llvm::Triple::thumbeb: { 79 arm::FloatABI ABI = arm::getARMFloatABI(getToolChain(), Args); 80 81 if (ABI == arm::FloatABI::Hard) 82 CmdArgs.push_back("-mfpu=vfp"); 83 else 84 CmdArgs.push_back("-mfpu=softvfp"); 85 86 switch (getToolChain().getTriple().getEnvironment()) { 87 case llvm::Triple::GNUEABIHF: 88 case llvm::Triple::GNUEABI: 89 case llvm::Triple::EABI: 90 CmdArgs.push_back("-meabi=5"); 91 break; 92 93 default: 94 CmdArgs.push_back("-matpcs"); 95 } 96 break; 97 } 98 case llvm::Triple::sparc: 99 case llvm::Triple::sparcel: 100 case llvm::Triple::sparcv9: { 101 std::string CPU = getCPUName(Args, getToolChain().getTriple()); 102 CmdArgs.push_back( 103 sparc::getSparcAsmModeForCPU(CPU, getToolChain().getTriple())); 104 AddAssemblerKPIC(getToolChain(), Args, CmdArgs); 105 break; 106 } 107 } 108 109 for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ, 110 options::OPT_fdebug_prefix_map_EQ)) { 111 StringRef Map = A->getValue(); 112 if (Map.find('=') == StringRef::npos) 113 D.Diag(diag::err_drv_invalid_argument_to_option) 114 << Map << A->getOption().getName(); 115 else { 116 CmdArgs.push_back(Args.MakeArgString("--debug-prefix-map")); 117 CmdArgs.push_back(Args.MakeArgString(Map)); 118 } 119 A->claim(); 120 } 121 122 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler); 123 124 CmdArgs.push_back("-o"); 125 CmdArgs.push_back(Output.getFilename()); 126 127 for (const auto &II : Inputs) 128 CmdArgs.push_back(II.getFilename()); 129 130 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as")); 131 C.addCommand(std::make_unique<Command>( 132 JA, *this, ResponseFileSupport::AtFileCurCP(), Exec, CmdArgs, Inputs)); 133 } 134 135 void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA, 136 const InputInfo &Output, 137 const InputInfoList &Inputs, 138 const ArgList &Args, 139 const char *LinkingOutput) const { 140 const toolchains::FreeBSD &ToolChain = 141 static_cast<const toolchains::FreeBSD &>(getToolChain()); 142 const Driver &D = ToolChain.getDriver(); 143 const llvm::Triple::ArchType Arch = ToolChain.getArch(); 144 const bool IsPIE = 145 !Args.hasArg(options::OPT_shared) && 146 (Args.hasArg(options::OPT_pie) || ToolChain.isPIEDefault()); 147 ArgStringList CmdArgs; 148 149 // Silence warning for "clang -g foo.o -o foo" 150 Args.ClaimAllArgs(options::OPT_g_Group); 151 // and "clang -emit-llvm foo.o -o foo" 152 Args.ClaimAllArgs(options::OPT_emit_llvm); 153 // and for "clang -w foo.o -o foo". Other warning options are already 154 // handled somewhere else. 155 Args.ClaimAllArgs(options::OPT_w); 156 157 if (!D.SysRoot.empty()) 158 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot)); 159 160 if (IsPIE) 161 CmdArgs.push_back("-pie"); 162 163 CmdArgs.push_back("--eh-frame-hdr"); 164 if (Args.hasArg(options::OPT_static)) { 165 CmdArgs.push_back("-Bstatic"); 166 } else { 167 if (Args.hasArg(options::OPT_rdynamic)) 168 CmdArgs.push_back("-export-dynamic"); 169 if (Args.hasArg(options::OPT_shared)) { 170 CmdArgs.push_back("-Bshareable"); 171 } else { 172 CmdArgs.push_back("-dynamic-linker"); 173 CmdArgs.push_back("/libexec/ld-elf.so.1"); 174 } 175 const llvm::Triple &T = ToolChain.getTriple(); 176 if (T.getOSMajorVersion() >= 9) { 177 if (Arch == llvm::Triple::arm || Arch == llvm::Triple::sparc || T.isX86()) 178 CmdArgs.push_back("--hash-style=both"); 179 } 180 CmdArgs.push_back("--enable-new-dtags"); 181 } 182 183 // Explicitly set the linker emulation for platforms that might not 184 // be the default emulation for the linker. 185 switch (Arch) { 186 case llvm::Triple::x86: 187 CmdArgs.push_back("-m"); 188 CmdArgs.push_back("elf_i386_fbsd"); 189 break; 190 case llvm::Triple::ppc: 191 CmdArgs.push_back("-m"); 192 CmdArgs.push_back("elf32ppc_fbsd"); 193 break; 194 case llvm::Triple::mips: 195 CmdArgs.push_back("-m"); 196 CmdArgs.push_back("elf32btsmip_fbsd"); 197 break; 198 case llvm::Triple::mipsel: 199 CmdArgs.push_back("-m"); 200 CmdArgs.push_back("elf32ltsmip_fbsd"); 201 break; 202 case llvm::Triple::mips64: 203 CmdArgs.push_back("-m"); 204 if (tools::mips::hasMipsAbiArg(Args, "n32")) 205 CmdArgs.push_back("elf32btsmipn32_fbsd"); 206 else 207 CmdArgs.push_back("elf64btsmip_fbsd"); 208 break; 209 case llvm::Triple::mips64el: 210 CmdArgs.push_back("-m"); 211 if (tools::mips::hasMipsAbiArg(Args, "n32")) 212 CmdArgs.push_back("elf32ltsmipn32_fbsd"); 213 else 214 CmdArgs.push_back("elf64ltsmip_fbsd"); 215 break; 216 case llvm::Triple::riscv32: 217 CmdArgs.push_back("-m"); 218 CmdArgs.push_back("elf32lriscv"); 219 break; 220 case llvm::Triple::riscv64: 221 CmdArgs.push_back("-m"); 222 CmdArgs.push_back("elf64lriscv"); 223 break; 224 default: 225 break; 226 } 227 228 if (Arg *A = Args.getLastArg(options::OPT_G)) { 229 if (ToolChain.getTriple().isMIPS()) { 230 StringRef v = A->getValue(); 231 CmdArgs.push_back(Args.MakeArgString("-G" + v)); 232 A->claim(); 233 } 234 } 235 236 if (Output.isFilename()) { 237 CmdArgs.push_back("-o"); 238 CmdArgs.push_back(Output.getFilename()); 239 } else { 240 assert(Output.isNothing() && "Invalid output."); 241 } 242 243 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) { 244 const char *crt1 = nullptr; 245 if (!Args.hasArg(options::OPT_shared)) { 246 if (Args.hasArg(options::OPT_pg)) 247 crt1 = "gcrt1.o"; 248 else if (IsPIE) 249 crt1 = "Scrt1.o"; 250 else 251 crt1 = "crt1.o"; 252 } 253 if (crt1) 254 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1))); 255 256 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o"))); 257 258 const char *crtbegin = nullptr; 259 if (Args.hasArg(options::OPT_static)) 260 crtbegin = "crtbeginT.o"; 261 else if (Args.hasArg(options::OPT_shared) || IsPIE) 262 crtbegin = "crtbeginS.o"; 263 else 264 crtbegin = "crtbegin.o"; 265 266 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin))); 267 } 268 269 Args.AddAllArgs(CmdArgs, options::OPT_L); 270 ToolChain.AddFilePathLibArgs(Args, CmdArgs); 271 Args.AddAllArgs(CmdArgs, options::OPT_T_Group); 272 Args.AddAllArgs(CmdArgs, options::OPT_e); 273 Args.AddAllArgs(CmdArgs, options::OPT_s); 274 Args.AddAllArgs(CmdArgs, options::OPT_t); 275 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag); 276 Args.AddAllArgs(CmdArgs, options::OPT_r); 277 278 if (D.isUsingLTO()) { 279 assert(!Inputs.empty() && "Must have at least one input."); 280 addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs[0], 281 D.getLTOMode() == LTOK_Thin); 282 } 283 284 bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs); 285 bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs); 286 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); 287 288 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { 289 // Use the static OpenMP runtime with -static-openmp 290 bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) && 291 !Args.hasArg(options::OPT_static); 292 addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP); 293 294 if (D.CCCIsCXX()) { 295 if (ToolChain.ShouldLinkCXXStdlib(Args)) 296 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs); 297 if (Args.hasArg(options::OPT_pg)) 298 CmdArgs.push_back("-lm_p"); 299 else 300 CmdArgs.push_back("-lm"); 301 } 302 if (NeedsSanitizerDeps) 303 linkSanitizerRuntimeDeps(ToolChain, CmdArgs); 304 if (NeedsXRayDeps) 305 linkXRayRuntimeDeps(ToolChain, CmdArgs); 306 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding 307 // the default system libraries. Just mimic this for now. 308 if (Args.hasArg(options::OPT_pg)) 309 CmdArgs.push_back("-lgcc_p"); 310 else 311 CmdArgs.push_back("-lgcc"); 312 if (Args.hasArg(options::OPT_static)) { 313 CmdArgs.push_back("-lgcc_eh"); 314 } else if (Args.hasArg(options::OPT_pg)) { 315 CmdArgs.push_back("-lgcc_eh_p"); 316 } else { 317 CmdArgs.push_back("--as-needed"); 318 CmdArgs.push_back("-lgcc_s"); 319 CmdArgs.push_back("--no-as-needed"); 320 } 321 322 if (Args.hasArg(options::OPT_pthread)) { 323 if (Args.hasArg(options::OPT_pg)) 324 CmdArgs.push_back("-lpthread_p"); 325 else 326 CmdArgs.push_back("-lpthread"); 327 } 328 329 if (Args.hasArg(options::OPT_pg)) { 330 if (Args.hasArg(options::OPT_shared)) 331 CmdArgs.push_back("-lc"); 332 else 333 CmdArgs.push_back("-lc_p"); 334 CmdArgs.push_back("-lgcc_p"); 335 } else { 336 CmdArgs.push_back("-lc"); 337 CmdArgs.push_back("-lgcc"); 338 } 339 340 if (Args.hasArg(options::OPT_static)) { 341 CmdArgs.push_back("-lgcc_eh"); 342 } else if (Args.hasArg(options::OPT_pg)) { 343 CmdArgs.push_back("-lgcc_eh_p"); 344 } else { 345 CmdArgs.push_back("--as-needed"); 346 CmdArgs.push_back("-lgcc_s"); 347 CmdArgs.push_back("--no-as-needed"); 348 } 349 } 350 351 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) { 352 if (Args.hasArg(options::OPT_shared) || IsPIE) 353 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o"))); 354 else 355 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o"))); 356 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o"))); 357 } 358 359 ToolChain.addProfileRTLibs(Args, CmdArgs); 360 361 const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath()); 362 C.addCommand(std::make_unique<Command>( 363 JA, *this, ResponseFileSupport::AtFileCurCP(), Exec, CmdArgs, Inputs)); 364 } 365 366 /// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly. 367 368 FreeBSD::FreeBSD(const Driver &D, const llvm::Triple &Triple, 369 const ArgList &Args) 370 : Generic_ELF(D, Triple, Args) { 371 372 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall 373 // back to '/usr/lib' if it doesn't exist. 374 if ((Triple.getArch() == llvm::Triple::x86 || Triple.isMIPS32() || 375 Triple.getArch() == llvm::Triple::ppc) && 376 D.getVFS().exists(getDriver().SysRoot + "/usr/lib32/crt1.o")) 377 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32"); 378 else 379 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib"); 380 } 381 382 ToolChain::CXXStdlibType FreeBSD::GetDefaultCXXStdlibType() const { 383 if (getTriple().getOSMajorVersion() >= 10) 384 return ToolChain::CST_Libcxx; 385 return ToolChain::CST_Libstdcxx; 386 } 387 388 unsigned FreeBSD::GetDefaultDwarfVersion() const { 389 if (getTriple().getOSMajorVersion() < 12) 390 return 2; 391 return 4; 392 } 393 394 void FreeBSD::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, 395 llvm::opt::ArgStringList &CC1Args) const { 396 addSystemInclude(DriverArgs, CC1Args, 397 getDriver().SysRoot + "/usr/include/c++/v1"); 398 } 399 400 void FreeBSD::addLibStdCxxIncludePaths( 401 const llvm::opt::ArgList &DriverArgs, 402 llvm::opt::ArgStringList &CC1Args) const { 403 addLibStdCXXIncludePaths(getDriver().SysRoot, "/usr/include/c++/4.2", "", "", 404 "", "", DriverArgs, CC1Args); 405 } 406 407 void FreeBSD::AddCXXStdlibLibArgs(const ArgList &Args, 408 ArgStringList &CmdArgs) const { 409 CXXStdlibType Type = GetCXXStdlibType(Args); 410 bool Profiling = Args.hasArg(options::OPT_pg); 411 412 switch (Type) { 413 case ToolChain::CST_Libcxx: 414 CmdArgs.push_back(Profiling ? "-lc++_p" : "-lc++"); 415 break; 416 417 case ToolChain::CST_Libstdcxx: 418 CmdArgs.push_back(Profiling ? "-lstdc++_p" : "-lstdc++"); 419 break; 420 } 421 } 422 423 void FreeBSD::AddCudaIncludeArgs(const ArgList &DriverArgs, 424 ArgStringList &CC1Args) const { 425 CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args); 426 } 427 428 void FreeBSD::AddHIPIncludeArgs(const ArgList &DriverArgs, 429 ArgStringList &CC1Args) const { 430 RocmInstallation.AddHIPIncludeArgs(DriverArgs, CC1Args); 431 } 432 433 Tool *FreeBSD::buildAssembler() const { 434 return new tools::freebsd::Assembler(*this); 435 } 436 437 Tool *FreeBSD::buildLinker() const { return new tools::freebsd::Linker(*this); } 438 439 llvm::ExceptionHandling FreeBSD::GetExceptionModel(const ArgList &Args) const { 440 // FreeBSD uses SjLj exceptions on ARM oabi. 441 switch (getTriple().getEnvironment()) { 442 case llvm::Triple::GNUEABIHF: 443 case llvm::Triple::GNUEABI: 444 case llvm::Triple::EABI: 445 return llvm::ExceptionHandling::None; 446 default: 447 if (getTriple().getArch() == llvm::Triple::arm || 448 getTriple().getArch() == llvm::Triple::thumb) 449 return llvm::ExceptionHandling::SjLj; 450 return llvm::ExceptionHandling::None; 451 } 452 } 453 454 bool FreeBSD::HasNativeLLVMSupport() const { return true; } 455 456 bool FreeBSD::IsUnwindTablesDefault(const ArgList &Args) const { return true; } 457 458 bool FreeBSD::isPIEDefault() const { return getSanitizerArgs().requiresPIE(); } 459 460 SanitizerMask FreeBSD::getSupportedSanitizers() const { 461 const bool IsX86 = getTriple().getArch() == llvm::Triple::x86; 462 const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64; 463 const bool IsMIPS64 = getTriple().isMIPS64(); 464 SanitizerMask Res = ToolChain::getSupportedSanitizers(); 465 Res |= SanitizerKind::Address; 466 Res |= SanitizerKind::PointerCompare; 467 Res |= SanitizerKind::PointerSubtract; 468 Res |= SanitizerKind::Vptr; 469 if (IsX86_64 || IsMIPS64) { 470 Res |= SanitizerKind::Leak; 471 Res |= SanitizerKind::Thread; 472 } 473 if (IsX86 || IsX86_64) { 474 Res |= SanitizerKind::Function; 475 Res |= SanitizerKind::SafeStack; 476 Res |= SanitizerKind::Fuzzer; 477 Res |= SanitizerKind::FuzzerNoLink; 478 } 479 if (IsX86_64) 480 Res |= SanitizerKind::Memory; 481 return Res; 482 } 483 484 void FreeBSD::addClangTargetOptions(const ArgList &DriverArgs, 485 ArgStringList &CC1Args, 486 Action::OffloadKind) const { 487 if (!DriverArgs.hasFlag(options::OPT_fuse_init_array, 488 options::OPT_fno_use_init_array, 489 getTriple().getOSMajorVersion() >= 12)) 490 CC1Args.push_back("-fno-use-init-array"); 491 } 492