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