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