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