1 //===--- Fuchsia.cpp - Fuchsia 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 "Fuchsia.h" 10 #include "CommonArgs.h" 11 #include "clang/Config/config.h" 12 #include "clang/Driver/Compilation.h" 13 #include "clang/Driver/Driver.h" 14 #include "clang/Driver/DriverDiagnostic.h" 15 #include "clang/Driver/Options.h" 16 #include "clang/Driver/SanitizerArgs.h" 17 #include "llvm/Option/ArgList.h" 18 #include "llvm/ProfileData/InstrProf.h" 19 #include "llvm/Support/FileSystem.h" 20 #include "llvm/Support/Path.h" 21 #include "llvm/Support/VirtualFileSystem.h" 22 23 using namespace clang::driver; 24 using namespace clang::driver::toolchains; 25 using namespace clang::driver::tools; 26 using namespace clang; 27 using namespace llvm::opt; 28 29 using tools::addMultilibFlag; 30 31 void fuchsia::Linker::ConstructJob(Compilation &C, const JobAction &JA, 32 const InputInfo &Output, 33 const InputInfoList &Inputs, 34 const ArgList &Args, 35 const char *LinkingOutput) const { 36 const toolchains::Fuchsia &ToolChain = 37 static_cast<const toolchains::Fuchsia &>(getToolChain()); 38 const Driver &D = ToolChain.getDriver(); 39 40 const llvm::Triple &Triple = ToolChain.getEffectiveTriple(); 41 42 ArgStringList CmdArgs; 43 44 // Silence warning for "clang -g foo.o -o foo" 45 Args.ClaimAllArgs(options::OPT_g_Group); 46 // and "clang -emit-llvm foo.o -o foo" 47 Args.ClaimAllArgs(options::OPT_emit_llvm); 48 // and for "clang -w foo.o -o foo". Other warning options are already 49 // handled somewhere else. 50 Args.ClaimAllArgs(options::OPT_w); 51 52 CmdArgs.push_back("-z"); 53 CmdArgs.push_back("max-page-size=4096"); 54 55 CmdArgs.push_back("-z"); 56 CmdArgs.push_back("now"); 57 58 const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath()); 59 if (llvm::sys::path::filename(Exec).equals_insensitive("ld.lld") || 60 llvm::sys::path::stem(Exec).equals_insensitive("ld.lld")) { 61 CmdArgs.push_back("-z"); 62 CmdArgs.push_back("rodynamic"); 63 CmdArgs.push_back("-z"); 64 CmdArgs.push_back("separate-loadable-segments"); 65 CmdArgs.push_back("-z"); 66 CmdArgs.push_back("rel"); 67 CmdArgs.push_back("--pack-dyn-relocs=relr"); 68 } 69 70 if (!D.SysRoot.empty()) 71 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot)); 72 73 if (!Args.hasArg(options::OPT_shared) && !Args.hasArg(options::OPT_r)) 74 CmdArgs.push_back("-pie"); 75 76 if (Args.hasArg(options::OPT_rdynamic)) 77 CmdArgs.push_back("-export-dynamic"); 78 79 if (Args.hasArg(options::OPT_s)) 80 CmdArgs.push_back("-s"); 81 82 if (Args.hasArg(options::OPT_r)) { 83 CmdArgs.push_back("-r"); 84 } else { 85 CmdArgs.push_back("--build-id"); 86 CmdArgs.push_back("--hash-style=gnu"); 87 } 88 89 if (ToolChain.getArch() == llvm::Triple::aarch64) { 90 CmdArgs.push_back("--execute-only"); 91 92 std::string CPU = getCPUName(D, Args, Triple); 93 if (CPU.empty() || CPU == "generic" || CPU == "cortex-a53") 94 CmdArgs.push_back("--fix-cortex-a53-843419"); 95 } 96 97 CmdArgs.push_back("--eh-frame-hdr"); 98 99 if (Args.hasArg(options::OPT_static)) 100 CmdArgs.push_back("-Bstatic"); 101 else if (Args.hasArg(options::OPT_shared)) 102 CmdArgs.push_back("-shared"); 103 104 const SanitizerArgs &SanArgs = ToolChain.getSanitizerArgs(Args); 105 106 if (!Args.hasArg(options::OPT_shared) && !Args.hasArg(options::OPT_r)) { 107 std::string Dyld = D.DyldPrefix; 108 if (SanArgs.needsAsanRt() && SanArgs.needsSharedRt()) 109 Dyld += "asan/"; 110 if (SanArgs.needsHwasanRt() && SanArgs.needsSharedRt()) 111 Dyld += "hwasan/"; 112 if (SanArgs.needsTsanRt() && SanArgs.needsSharedRt()) 113 Dyld += "tsan/"; 114 Dyld += "ld.so.1"; 115 CmdArgs.push_back("-dynamic-linker"); 116 CmdArgs.push_back(Args.MakeArgString(Dyld)); 117 } 118 119 if (ToolChain.getArch() == llvm::Triple::riscv64) 120 CmdArgs.push_back("-X"); 121 122 CmdArgs.push_back("-o"); 123 CmdArgs.push_back(Output.getFilename()); 124 125 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles, 126 options::OPT_r)) { 127 if (!Args.hasArg(options::OPT_shared)) { 128 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("Scrt1.o"))); 129 } 130 } 131 132 Args.AddAllArgs(CmdArgs, options::OPT_L); 133 Args.AddAllArgs(CmdArgs, options::OPT_u); 134 135 ToolChain.AddFilePathLibArgs(Args, CmdArgs); 136 137 if (D.isUsingLTO()) { 138 assert(!Inputs.empty() && "Must have at least one input."); 139 addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs[0], 140 D.getLTOMode() == LTOK_Thin); 141 } 142 143 addLinkerCompressDebugSectionsOption(ToolChain, Args, CmdArgs); 144 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); 145 146 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs, 147 options::OPT_r)) { 148 if (Args.hasArg(options::OPT_static)) 149 CmdArgs.push_back("-Bdynamic"); 150 151 if (D.CCCIsCXX()) { 152 if (ToolChain.ShouldLinkCXXStdlib(Args)) { 153 bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) && 154 !Args.hasArg(options::OPT_static); 155 CmdArgs.push_back("--push-state"); 156 CmdArgs.push_back("--as-needed"); 157 if (OnlyLibstdcxxStatic) 158 CmdArgs.push_back("-Bstatic"); 159 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs); 160 if (OnlyLibstdcxxStatic) 161 CmdArgs.push_back("-Bdynamic"); 162 CmdArgs.push_back("-lm"); 163 CmdArgs.push_back("--pop-state"); 164 } 165 } 166 167 // Note that Fuchsia never needs to link in sanitizer runtime deps. Any 168 // sanitizer runtimes with system dependencies use the `.deplibs` feature 169 // instead. 170 addSanitizerRuntimes(ToolChain, Args, CmdArgs); 171 172 addXRayRuntime(ToolChain, Args, CmdArgs); 173 174 ToolChain.addProfileRTLibs(Args, CmdArgs); 175 176 AddRunTimeLibs(ToolChain, D, CmdArgs, Args); 177 178 if (Args.hasArg(options::OPT_pthread) || 179 Args.hasArg(options::OPT_pthreads)) 180 CmdArgs.push_back("-lpthread"); 181 182 if (Args.hasArg(options::OPT_fsplit_stack)) 183 CmdArgs.push_back("--wrap=pthread_create"); 184 185 if (!Args.hasArg(options::OPT_nolibc)) 186 CmdArgs.push_back("-lc"); 187 } 188 189 C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(), 190 Exec, CmdArgs, Inputs, Output)); 191 } 192 193 /// Fuchsia - Fuchsia tool chain which can call as(1) and ld(1) directly. 194 195 Fuchsia::Fuchsia(const Driver &D, const llvm::Triple &Triple, 196 const ArgList &Args) 197 : ToolChain(D, Triple, Args) { 198 getProgramPaths().push_back(getDriver().getInstalledDir()); 199 if (getDriver().getInstalledDir() != D.Dir) 200 getProgramPaths().push_back(D.Dir); 201 202 if (!D.SysRoot.empty()) { 203 SmallString<128> P(D.SysRoot); 204 llvm::sys::path::append(P, "lib"); 205 getFilePaths().push_back(std::string(P.str())); 206 } 207 208 auto FilePaths = [&](const Multilib &M) -> std::vector<std::string> { 209 std::vector<std::string> FP; 210 for (const std::string &Path : getStdlibPaths()) { 211 SmallString<128> P(Path); 212 llvm::sys::path::append(P, M.gccSuffix()); 213 FP.push_back(std::string(P.str())); 214 } 215 return FP; 216 }; 217 218 Multilibs.push_back(Multilib()); 219 // Use the noexcept variant with -fno-exceptions to avoid the extra overhead. 220 Multilibs.push_back(Multilib("noexcept", {}, {}, 1) 221 .flag("-fexceptions") 222 .flag("+fno-exceptions")); 223 // ASan has higher priority because we always want the instrumentated version. 224 Multilibs.push_back(Multilib("asan", {}, {}, 2) 225 .flag("+fsanitize=address")); 226 // Use the asan+noexcept variant with ASan and -fno-exceptions. 227 Multilibs.push_back(Multilib("asan+noexcept", {}, {}, 3) 228 .flag("+fsanitize=address") 229 .flag("-fexceptions") 230 .flag("+fno-exceptions")); 231 // HWASan has higher priority because we always want the instrumentated 232 // version. 233 Multilibs.push_back( 234 Multilib("hwasan", {}, {}, 4).flag("+fsanitize=hwaddress")); 235 // Use the hwasan+noexcept variant with HWASan and -fno-exceptions. 236 Multilibs.push_back(Multilib("hwasan+noexcept", {}, {}, 5) 237 .flag("+fsanitize=hwaddress") 238 .flag("-fexceptions") 239 .flag("+fno-exceptions")); 240 // Use the relative vtables ABI. 241 // TODO: Remove these multilibs once relative vtables are enabled by default 242 // for Fuchsia. 243 Multilibs.push_back(Multilib("relative-vtables", {}, {}, 6) 244 .flag("+fexperimental-relative-c++-abi-vtables")); 245 Multilibs.push_back(Multilib("relative-vtables+noexcept", {}, {}, 7) 246 .flag("+fexperimental-relative-c++-abi-vtables") 247 .flag("-fexceptions") 248 .flag("+fno-exceptions")); 249 Multilibs.push_back(Multilib("relative-vtables+asan", {}, {}, 8) 250 .flag("+fexperimental-relative-c++-abi-vtables") 251 .flag("+fsanitize=address")); 252 Multilibs.push_back(Multilib("relative-vtables+asan+noexcept", {}, {}, 9) 253 .flag("+fexperimental-relative-c++-abi-vtables") 254 .flag("+fsanitize=address") 255 .flag("-fexceptions") 256 .flag("+fno-exceptions")); 257 Multilibs.push_back(Multilib("relative-vtables+hwasan", {}, {}, 10) 258 .flag("+fexperimental-relative-c++-abi-vtables") 259 .flag("+fsanitize=hwaddress")); 260 Multilibs.push_back(Multilib("relative-vtables+hwasan+noexcept", {}, {}, 11) 261 .flag("+fexperimental-relative-c++-abi-vtables") 262 .flag("+fsanitize=hwaddress") 263 .flag("-fexceptions") 264 .flag("+fno-exceptions")); 265 // Use Itanium C++ ABI for the compat multilib. 266 Multilibs.push_back(Multilib("compat", {}, {}, 12).flag("+fc++-abi=itanium")); 267 268 Multilibs.FilterOut([&](const Multilib &M) { 269 std::vector<std::string> RD = FilePaths(M); 270 return llvm::all_of(RD, [&](std::string P) { return !getVFS().exists(P); }); 271 }); 272 273 Multilib::flags_list Flags; 274 addMultilibFlag( 275 Args.hasFlag(options::OPT_fexceptions, options::OPT_fno_exceptions, true), 276 "fexceptions", Flags); 277 addMultilibFlag(getSanitizerArgs(Args).needsAsanRt(), "fsanitize=address", 278 Flags); 279 addMultilibFlag(getSanitizerArgs(Args).needsHwasanRt(), "fsanitize=hwaddress", 280 Flags); 281 282 addMultilibFlag( 283 Args.hasFlag(options::OPT_fexperimental_relative_cxx_abi_vtables, 284 options::OPT_fno_experimental_relative_cxx_abi_vtables, 285 /*default=*/false), 286 "fexperimental-relative-c++-abi-vtables", Flags); 287 addMultilibFlag(Args.getLastArgValue(options::OPT_fcxx_abi_EQ) == "itanium", 288 "fc++-abi=itanium", Flags); 289 290 Multilibs.setFilePathsCallback(FilePaths); 291 292 if (Multilibs.select(Flags, SelectedMultilib)) 293 if (!SelectedMultilib.isDefault()) 294 if (const auto &PathsCallback = Multilibs.filePathsCallback()) 295 for (const auto &Path : PathsCallback(SelectedMultilib)) 296 // Prepend the multilib path to ensure it takes the precedence. 297 getFilePaths().insert(getFilePaths().begin(), Path); 298 } 299 300 std::string Fuchsia::ComputeEffectiveClangTriple(const ArgList &Args, 301 types::ID InputType) const { 302 llvm::Triple Triple(ComputeLLVMTriple(Args, InputType)); 303 return Triple.str(); 304 } 305 306 Tool *Fuchsia::buildLinker() const { 307 return new tools::fuchsia::Linker(*this); 308 } 309 310 ToolChain::RuntimeLibType Fuchsia::GetRuntimeLibType( 311 const ArgList &Args) const { 312 if (Arg *A = Args.getLastArg(clang::driver::options::OPT_rtlib_EQ)) { 313 StringRef Value = A->getValue(); 314 if (Value != "compiler-rt") 315 getDriver().Diag(clang::diag::err_drv_invalid_rtlib_name) 316 << A->getAsString(Args); 317 } 318 319 return ToolChain::RLT_CompilerRT; 320 } 321 322 ToolChain::CXXStdlibType 323 Fuchsia::GetCXXStdlibType(const ArgList &Args) const { 324 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) { 325 StringRef Value = A->getValue(); 326 if (Value != "libc++") 327 getDriver().Diag(diag::err_drv_invalid_stdlib_name) 328 << A->getAsString(Args); 329 } 330 331 return ToolChain::CST_Libcxx; 332 } 333 334 void Fuchsia::addClangTargetOptions(const ArgList &DriverArgs, 335 ArgStringList &CC1Args, 336 Action::OffloadKind) const { 337 if (!DriverArgs.hasFlag(options::OPT_fuse_init_array, 338 options::OPT_fno_use_init_array, true)) 339 CC1Args.push_back("-fno-use-init-array"); 340 } 341 342 void Fuchsia::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 343 ArgStringList &CC1Args) const { 344 const Driver &D = getDriver(); 345 346 if (DriverArgs.hasArg(options::OPT_nostdinc)) 347 return; 348 349 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 350 SmallString<128> P(D.ResourceDir); 351 llvm::sys::path::append(P, "include"); 352 addSystemInclude(DriverArgs, CC1Args, P); 353 } 354 355 if (DriverArgs.hasArg(options::OPT_nostdlibinc)) 356 return; 357 358 // Check for configure-time C include directories. 359 StringRef CIncludeDirs(C_INCLUDE_DIRS); 360 if (CIncludeDirs != "") { 361 SmallVector<StringRef, 5> dirs; 362 CIncludeDirs.split(dirs, ":"); 363 for (StringRef dir : dirs) { 364 StringRef Prefix = 365 llvm::sys::path::is_absolute(dir) ? "" : StringRef(D.SysRoot); 366 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir); 367 } 368 return; 369 } 370 371 if (!D.SysRoot.empty()) { 372 SmallString<128> P(D.SysRoot); 373 llvm::sys::path::append(P, "include"); 374 addExternCSystemInclude(DriverArgs, CC1Args, P.str()); 375 } 376 } 377 378 void Fuchsia::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 379 ArgStringList &CC1Args) const { 380 if (DriverArgs.hasArg(options::OPT_nostdinc, options::OPT_nostdlibinc, 381 options::OPT_nostdincxx)) 382 return; 383 384 const Driver &D = getDriver(); 385 std::string Target = getTripleString(); 386 387 auto AddCXXIncludePath = [&](StringRef Path) { 388 std::string Version = detectLibcxxVersion(Path); 389 if (Version.empty()) 390 return; 391 392 // First add the per-target include path. 393 SmallString<128> TargetDir(Path); 394 llvm::sys::path::append(TargetDir, Target, "c++", Version); 395 if (getVFS().exists(TargetDir)) 396 addSystemInclude(DriverArgs, CC1Args, TargetDir); 397 398 // Second add the generic one. 399 SmallString<128> Dir(Path); 400 llvm::sys::path::append(Dir, "c++", Version); 401 addSystemInclude(DriverArgs, CC1Args, Dir); 402 }; 403 404 switch (GetCXXStdlibType(DriverArgs)) { 405 case ToolChain::CST_Libcxx: { 406 SmallString<128> P(D.Dir); 407 llvm::sys::path::append(P, "..", "include"); 408 AddCXXIncludePath(P); 409 break; 410 } 411 412 default: 413 llvm_unreachable("invalid stdlib name"); 414 } 415 } 416 417 void Fuchsia::AddCXXStdlibLibArgs(const ArgList &Args, 418 ArgStringList &CmdArgs) const { 419 switch (GetCXXStdlibType(Args)) { 420 case ToolChain::CST_Libcxx: 421 CmdArgs.push_back("-lc++"); 422 if (Args.hasArg(options::OPT_fexperimental_library)) 423 CmdArgs.push_back("-lc++experimental"); 424 break; 425 426 case ToolChain::CST_Libstdcxx: 427 llvm_unreachable("invalid stdlib name"); 428 } 429 } 430 431 SanitizerMask Fuchsia::getSupportedSanitizers() const { 432 SanitizerMask Res = ToolChain::getSupportedSanitizers(); 433 Res |= SanitizerKind::Address; 434 Res |= SanitizerKind::HWAddress; 435 Res |= SanitizerKind::PointerCompare; 436 Res |= SanitizerKind::PointerSubtract; 437 Res |= SanitizerKind::Fuzzer; 438 Res |= SanitizerKind::FuzzerNoLink; 439 Res |= SanitizerKind::Leak; 440 Res |= SanitizerKind::SafeStack; 441 Res |= SanitizerKind::Scudo; 442 Res |= SanitizerKind::Thread; 443 return Res; 444 } 445 446 SanitizerMask Fuchsia::getDefaultSanitizers() const { 447 SanitizerMask Res; 448 switch (getTriple().getArch()) { 449 case llvm::Triple::aarch64: 450 case llvm::Triple::riscv64: 451 Res |= SanitizerKind::ShadowCallStack; 452 break; 453 case llvm::Triple::x86_64: 454 Res |= SanitizerKind::SafeStack; 455 break; 456 default: 457 break; 458 } 459 return Res; 460 } 461