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(llvm::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 addOpenMPRuntime(CmdArgs, ToolChain, Args); 274 if (D.CCCIsCXX()) { 275 if (ToolChain.ShouldLinkCXXStdlib(Args)) 276 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs); 277 if (Args.hasArg(options::OPT_pg)) 278 CmdArgs.push_back("-lm_p"); 279 else 280 CmdArgs.push_back("-lm"); 281 } 282 if (NeedsSanitizerDeps) 283 linkSanitizerRuntimeDeps(ToolChain, CmdArgs); 284 if (NeedsXRayDeps) 285 linkXRayRuntimeDeps(ToolChain, CmdArgs); 286 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding 287 // the default system libraries. Just mimic this for now. 288 if (Args.hasArg(options::OPT_pg)) 289 CmdArgs.push_back("-lgcc_p"); 290 else 291 CmdArgs.push_back("-lgcc"); 292 if (Args.hasArg(options::OPT_static)) { 293 CmdArgs.push_back("-lgcc_eh"); 294 } else if (Args.hasArg(options::OPT_pg)) { 295 CmdArgs.push_back("-lgcc_eh_p"); 296 } else { 297 CmdArgs.push_back("--as-needed"); 298 CmdArgs.push_back("-lgcc_s"); 299 CmdArgs.push_back("--no-as-needed"); 300 } 301 302 if (Args.hasArg(options::OPT_pthread)) { 303 if (Args.hasArg(options::OPT_pg)) 304 CmdArgs.push_back("-lpthread_p"); 305 else 306 CmdArgs.push_back("-lpthread"); 307 } 308 309 if (Args.hasArg(options::OPT_pg)) { 310 if (Args.hasArg(options::OPT_shared)) 311 CmdArgs.push_back("-lc"); 312 else 313 CmdArgs.push_back("-lc_p"); 314 CmdArgs.push_back("-lgcc_p"); 315 } else { 316 CmdArgs.push_back("-lc"); 317 CmdArgs.push_back("-lgcc"); 318 } 319 320 if (Args.hasArg(options::OPT_static)) { 321 CmdArgs.push_back("-lgcc_eh"); 322 } else if (Args.hasArg(options::OPT_pg)) { 323 CmdArgs.push_back("-lgcc_eh_p"); 324 } else { 325 CmdArgs.push_back("--as-needed"); 326 CmdArgs.push_back("-lgcc_s"); 327 CmdArgs.push_back("--no-as-needed"); 328 } 329 } 330 331 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) { 332 if (Args.hasArg(options::OPT_shared) || IsPIE) 333 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o"))); 334 else 335 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o"))); 336 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o"))); 337 } 338 339 ToolChain.addProfileRTLibs(Args, CmdArgs); 340 341 const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath()); 342 C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs)); 343 } 344 345 /// FreeBSD - FreeBSD tool chain which can call as(1) and ld(1) directly. 346 347 FreeBSD::FreeBSD(const Driver &D, const llvm::Triple &Triple, 348 const ArgList &Args) 349 : Generic_ELF(D, Triple, Args) { 350 351 // When targeting 32-bit platforms, look for '/usr/lib32/crt1.o' and fall 352 // back to '/usr/lib' if it doesn't exist. 353 if ((Triple.getArch() == llvm::Triple::x86 || Triple.isMIPS32() || 354 Triple.getArch() == llvm::Triple::ppc) && 355 D.getVFS().exists(getDriver().SysRoot + "/usr/lib32/crt1.o")) 356 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib32"); 357 else 358 getFilePaths().push_back(getDriver().SysRoot + "/usr/lib"); 359 } 360 361 ToolChain::CXXStdlibType FreeBSD::GetDefaultCXXStdlibType() const { 362 if (getTriple().getOSMajorVersion() >= 10) 363 return ToolChain::CST_Libcxx; 364 return ToolChain::CST_Libstdcxx; 365 } 366 367 unsigned FreeBSD::GetDefaultDwarfVersion() const { 368 // Default to use DWARF 2 before FreeBSD 13. 369 if (getTriple().getOSMajorVersion() < 13) 370 return 2; 371 return 4; 372 } 373 374 void FreeBSD::addLibStdCxxIncludePaths( 375 const llvm::opt::ArgList &DriverArgs, 376 llvm::opt::ArgStringList &CC1Args) const { 377 addLibStdCXXIncludePaths(getDriver().SysRoot, "/usr/include/c++/4.2", "", "", 378 "", "", DriverArgs, CC1Args); 379 } 380 381 void FreeBSD::AddCXXStdlibLibArgs(const ArgList &Args, 382 ArgStringList &CmdArgs) const { 383 CXXStdlibType Type = GetCXXStdlibType(Args); 384 bool Profiling = Args.hasArg(options::OPT_pg); 385 386 switch (Type) { 387 case ToolChain::CST_Libcxx: 388 CmdArgs.push_back(Profiling ? "-lc++_p" : "-lc++"); 389 break; 390 391 case ToolChain::CST_Libstdcxx: 392 CmdArgs.push_back(Profiling ? "-lstdc++_p" : "-lstdc++"); 393 break; 394 } 395 } 396 397 Tool *FreeBSD::buildAssembler() const { 398 return new tools::freebsd::Assembler(*this); 399 } 400 401 Tool *FreeBSD::buildLinker() const { return new tools::freebsd::Linker(*this); } 402 403 llvm::ExceptionHandling FreeBSD::GetExceptionModel(const ArgList &Args) const { 404 // FreeBSD uses SjLj exceptions on ARM oabi. 405 switch (getTriple().getEnvironment()) { 406 case llvm::Triple::GNUEABIHF: 407 case llvm::Triple::GNUEABI: 408 case llvm::Triple::EABI: 409 return llvm::ExceptionHandling::None; 410 default: 411 if (getTriple().getArch() == llvm::Triple::arm || 412 getTriple().getArch() == llvm::Triple::thumb) 413 return llvm::ExceptionHandling::SjLj; 414 return llvm::ExceptionHandling::None; 415 } 416 } 417 418 bool FreeBSD::HasNativeLLVMSupport() const { return true; } 419 420 bool FreeBSD::IsUnwindTablesDefault(const ArgList &Args) const { return true; } 421 422 bool FreeBSD::isPIEDefault() const { return getSanitizerArgs().requiresPIE(); } 423 424 SanitizerMask FreeBSD::getSupportedSanitizers() const { 425 const bool IsX86 = getTriple().getArch() == llvm::Triple::x86; 426 const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64; 427 const bool IsMIPS64 = getTriple().isMIPS64(); 428 SanitizerMask Res = ToolChain::getSupportedSanitizers(); 429 Res |= SanitizerKind::Address; 430 Res |= SanitizerKind::PointerCompare; 431 Res |= SanitizerKind::PointerSubtract; 432 Res |= SanitizerKind::Vptr; 433 if (IsX86_64 || IsMIPS64) { 434 Res |= SanitizerKind::Leak; 435 Res |= SanitizerKind::Thread; 436 } 437 if (IsX86 || IsX86_64) { 438 Res |= SanitizerKind::Function; 439 Res |= SanitizerKind::SafeStack; 440 Res |= SanitizerKind::Fuzzer; 441 Res |= SanitizerKind::FuzzerNoLink; 442 } 443 if (IsX86_64) 444 Res |= SanitizerKind::Memory; 445 return Res; 446 } 447