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_lower("ld.lld") || 58 llvm::sys::path::stem(Exec).equals_lower("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 Dyld += "ld.so.1"; 99 CmdArgs.push_back("-dynamic-linker"); 100 CmdArgs.push_back(Args.MakeArgString(Dyld)); 101 } 102 103 CmdArgs.push_back("-o"); 104 CmdArgs.push_back(Output.getFilename()); 105 106 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) { 107 if (!Args.hasArg(options::OPT_shared)) { 108 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("Scrt1.o"))); 109 } 110 } 111 112 Args.AddAllArgs(CmdArgs, options::OPT_L); 113 Args.AddAllArgs(CmdArgs, options::OPT_u); 114 115 ToolChain.AddFilePathLibArgs(Args, CmdArgs); 116 117 if (D.isUsingLTO()) { 118 assert(!Inputs.empty() && "Must have at least one input."); 119 addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs[0], 120 D.getLTOMode() == LTOK_Thin); 121 } 122 123 bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs); 124 bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs); 125 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); 126 ToolChain.addProfileRTLibs(Args, CmdArgs); 127 128 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) { 129 if (Args.hasArg(options::OPT_static)) 130 CmdArgs.push_back("-Bdynamic"); 131 132 if (D.CCCIsCXX()) { 133 if (ToolChain.ShouldLinkCXXStdlib(Args)) { 134 bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) && 135 !Args.hasArg(options::OPT_static); 136 CmdArgs.push_back("--push-state"); 137 CmdArgs.push_back("--as-needed"); 138 if (OnlyLibstdcxxStatic) 139 CmdArgs.push_back("-Bstatic"); 140 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs); 141 if (OnlyLibstdcxxStatic) 142 CmdArgs.push_back("-Bdynamic"); 143 CmdArgs.push_back("-lm"); 144 CmdArgs.push_back("--pop-state"); 145 } 146 } 147 148 if (NeedsSanitizerDeps) 149 linkSanitizerRuntimeDeps(ToolChain, CmdArgs); 150 151 if (NeedsXRayDeps) 152 linkXRayRuntimeDeps(ToolChain, CmdArgs); 153 154 AddRunTimeLibs(ToolChain, D, CmdArgs, Args); 155 156 if (Args.hasArg(options::OPT_pthread) || 157 Args.hasArg(options::OPT_pthreads)) 158 CmdArgs.push_back("-lpthread"); 159 160 if (Args.hasArg(options::OPT_fsplit_stack)) 161 CmdArgs.push_back("--wrap=pthread_create"); 162 163 if (!Args.hasArg(options::OPT_nolibc)) 164 CmdArgs.push_back("-lc"); 165 } 166 167 C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(), 168 Exec, CmdArgs, Inputs)); 169 } 170 171 /// Fuchsia - Fuchsia tool chain which can call as(1) and ld(1) directly. 172 173 Fuchsia::Fuchsia(const Driver &D, const llvm::Triple &Triple, 174 const ArgList &Args) 175 : ToolChain(D, Triple, Args) { 176 getProgramPaths().push_back(getDriver().getInstalledDir()); 177 if (getDriver().getInstalledDir() != D.Dir) 178 getProgramPaths().push_back(D.Dir); 179 180 if (!D.SysRoot.empty()) { 181 SmallString<128> P(D.SysRoot); 182 llvm::sys::path::append(P, "lib"); 183 getFilePaths().push_back(std::string(P.str())); 184 } 185 186 auto FilePaths = [&](const Multilib &M) -> std::vector<std::string> { 187 std::vector<std::string> FP; 188 if (D.CCCIsCXX()) { 189 if (auto CXXStdlibPath = getCXXStdlibPath()) { 190 SmallString<128> P(*CXXStdlibPath); 191 llvm::sys::path::append(P, M.gccSuffix()); 192 FP.push_back(std::string(P.str())); 193 } 194 } 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 Multilibs.FilterOut([&](const Multilib &M) { 212 std::vector<std::string> RD = FilePaths(M); 213 return std::all_of(RD.begin(), RD.end(), [&](std::string P) { 214 return !getVFS().exists(P); 215 }); 216 }); 217 218 Multilib::flags_list Flags; 219 addMultilibFlag( 220 Args.hasFlag(options::OPT_fexceptions, options::OPT_fno_exceptions, true), 221 "fexceptions", Flags); 222 addMultilibFlag(getSanitizerArgs().needsAsanRt(), "fsanitize=address", Flags); 223 Multilibs.setFilePathsCallback(FilePaths); 224 225 if (Multilibs.select(Flags, SelectedMultilib)) 226 if (!SelectedMultilib.isDefault()) 227 if (const auto &PathsCallback = Multilibs.filePathsCallback()) 228 for (const auto &Path : PathsCallback(SelectedMultilib)) 229 // Prepend the multilib path to ensure it takes the precedence. 230 getFilePaths().insert(getFilePaths().begin(), Path); 231 } 232 233 std::string Fuchsia::ComputeEffectiveClangTriple(const ArgList &Args, 234 types::ID InputType) const { 235 llvm::Triple Triple(ComputeLLVMTriple(Args, InputType)); 236 return Triple.str(); 237 } 238 239 Tool *Fuchsia::buildLinker() const { 240 return new tools::fuchsia::Linker(*this); 241 } 242 243 ToolChain::RuntimeLibType Fuchsia::GetRuntimeLibType( 244 const ArgList &Args) const { 245 if (Arg *A = Args.getLastArg(clang::driver::options::OPT_rtlib_EQ)) { 246 StringRef Value = A->getValue(); 247 if (Value != "compiler-rt") 248 getDriver().Diag(clang::diag::err_drv_invalid_rtlib_name) 249 << A->getAsString(Args); 250 } 251 252 return ToolChain::RLT_CompilerRT; 253 } 254 255 ToolChain::CXXStdlibType 256 Fuchsia::GetCXXStdlibType(const ArgList &Args) const { 257 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) { 258 StringRef Value = A->getValue(); 259 if (Value != "libc++") 260 getDriver().Diag(diag::err_drv_invalid_stdlib_name) 261 << A->getAsString(Args); 262 } 263 264 return ToolChain::CST_Libcxx; 265 } 266 267 void Fuchsia::addClangTargetOptions(const ArgList &DriverArgs, 268 ArgStringList &CC1Args, 269 Action::OffloadKind) const { 270 if (!DriverArgs.hasFlag(options::OPT_fuse_init_array, 271 options::OPT_fno_use_init_array, true)) 272 CC1Args.push_back("-fno-use-init-array"); 273 } 274 275 void Fuchsia::AddClangSystemIncludeArgs(const ArgList &DriverArgs, 276 ArgStringList &CC1Args) const { 277 const Driver &D = getDriver(); 278 279 if (DriverArgs.hasArg(options::OPT_nostdinc)) 280 return; 281 282 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { 283 SmallString<128> P(D.ResourceDir); 284 llvm::sys::path::append(P, "include"); 285 addSystemInclude(DriverArgs, CC1Args, P); 286 } 287 288 if (DriverArgs.hasArg(options::OPT_nostdlibinc)) 289 return; 290 291 // Check for configure-time C include directories. 292 StringRef CIncludeDirs(C_INCLUDE_DIRS); 293 if (CIncludeDirs != "") { 294 SmallVector<StringRef, 5> dirs; 295 CIncludeDirs.split(dirs, ":"); 296 for (StringRef dir : dirs) { 297 StringRef Prefix = 298 llvm::sys::path::is_absolute(dir) ? "" : StringRef(D.SysRoot); 299 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir); 300 } 301 return; 302 } 303 304 if (!D.SysRoot.empty()) { 305 SmallString<128> P(D.SysRoot); 306 llvm::sys::path::append(P, "include"); 307 addExternCSystemInclude(DriverArgs, CC1Args, P.str()); 308 } 309 } 310 311 void Fuchsia::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, 312 ArgStringList &CC1Args) const { 313 if (DriverArgs.hasArg(options::OPT_nostdlibinc) || 314 DriverArgs.hasArg(options::OPT_nostdincxx)) 315 return; 316 317 switch (GetCXXStdlibType(DriverArgs)) { 318 case ToolChain::CST_Libcxx: { 319 SmallString<128> P(getDriver().Dir); 320 llvm::sys::path::append(P, "..", "include", "c++", "v1"); 321 addSystemInclude(DriverArgs, CC1Args, P.str()); 322 break; 323 } 324 325 default: 326 llvm_unreachable("invalid stdlib name"); 327 } 328 } 329 330 void Fuchsia::AddCXXStdlibLibArgs(const ArgList &Args, 331 ArgStringList &CmdArgs) const { 332 switch (GetCXXStdlibType(Args)) { 333 case ToolChain::CST_Libcxx: 334 CmdArgs.push_back("-lc++"); 335 break; 336 337 case ToolChain::CST_Libstdcxx: 338 llvm_unreachable("invalid stdlib name"); 339 } 340 } 341 342 SanitizerMask Fuchsia::getSupportedSanitizers() const { 343 SanitizerMask Res = ToolChain::getSupportedSanitizers(); 344 Res |= SanitizerKind::Address; 345 Res |= SanitizerKind::PointerCompare; 346 Res |= SanitizerKind::PointerSubtract; 347 Res |= SanitizerKind::Fuzzer; 348 Res |= SanitizerKind::FuzzerNoLink; 349 Res |= SanitizerKind::Leak; 350 Res |= SanitizerKind::SafeStack; 351 Res |= SanitizerKind::Scudo; 352 return Res; 353 } 354 355 SanitizerMask Fuchsia::getDefaultSanitizers() const { 356 SanitizerMask Res; 357 switch (getTriple().getArch()) { 358 case llvm::Triple::aarch64: 359 Res |= SanitizerKind::ShadowCallStack; 360 break; 361 case llvm::Triple::x86_64: 362 Res |= SanitizerKind::SafeStack; 363 break; 364 default: 365 // TODO: Enable SafeStack on RISC-V once tested. 366 break; 367 } 368 return Res; 369 } 370 371 void Fuchsia::addProfileRTLibs(const llvm::opt::ArgList &Args, 372 llvm::opt::ArgStringList &CmdArgs) const { 373 // Add linker option -u__llvm_profile_runtime to cause runtime 374 // initialization module to be linked in. 375 if (needsProfileRT(Args)) 376 CmdArgs.push_back(Args.MakeArgString( 377 Twine("-u", llvm::getInstrProfRuntimeHookVarName()))); 378 ToolChain::addProfileRTLibs(Args, CmdArgs); 379 } 380