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