Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 1 | //===--- Fuchsia.cpp - Fuchsia ToolChain Implementations --------*- C++ -*-===// |
| 2 | // |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 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 |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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" |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 18 | #include "llvm/ProfileData/InstrProf.h" |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 19 | #include "llvm/Support/FileSystem.h" |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 20 | #include "llvm/Support/Path.h" |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 21 | #include "llvm/Support/VirtualFileSystem.h" |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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 | |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 29 | using tools::addMultilibFlag; |
| 30 | |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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 | |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 50 | CmdArgs.push_back("-z"); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 51 | CmdArgs.push_back("max-page-size=4096"); |
| 52 | |
| 53 | CmdArgs.push_back("-z"); |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 54 | CmdArgs.push_back("now"); |
| 55 | |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 56 | const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath()); |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 57 | if (llvm::sys::path::filename(Exec).equals_insensitive("ld.lld") || |
| 58 | llvm::sys::path::stem(Exec).equals_insensitive("ld.lld")) { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 59 | CmdArgs.push_back("-z"); |
| 60 | CmdArgs.push_back("rodynamic"); |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 61 | CmdArgs.push_back("-z"); |
| 62 | CmdArgs.push_back("separate-loadable-segments"); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 63 | CmdArgs.push_back("--pack-dyn-relocs=relr"); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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/"; |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 98 | if (SanArgs.needsHwasanRt() && SanArgs.needsSharedRt()) |
| 99 | Dyld += "hwasan/"; |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 100 | if (SanArgs.needsTsanRt() && SanArgs.needsSharedRt()) |
| 101 | Dyld += "tsan/"; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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 | |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 119 | ToolChain.AddFilePathLibArgs(Args, CmdArgs); |
| 120 | |
| 121 | if (D.isUsingLTO()) { |
| 122 | assert(!Inputs.empty() && "Must have at least one input."); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 123 | addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs[0], |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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 | |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 167 | if (!Args.hasArg(options::OPT_nolibc)) |
| 168 | CmdArgs.push_back("-lc"); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 171 | C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(), |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 172 | Exec, CmdArgs, Inputs, Output)); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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"); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 187 | getFilePaths().push_back(std::string(P.str())); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 188 | } |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 189 | |
| 190 | auto FilePaths = [&](const Multilib &M) -> std::vector<std::string> { |
| 191 | std::vector<std::string> FP; |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 192 | SmallString<128> P(getStdlibPath()); |
| 193 | llvm::sys::path::append(P, M.gccSuffix()); |
| 194 | FP.push_back(std::string(P.str())); |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 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")); |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 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")); |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 220 | // Use the relative vtables ABI. |
| 221 | // TODO: Remove these multilibs once relative vtables are enabled by default |
| 222 | // for Fuchsia. |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 223 | Multilibs.push_back(Multilib("relative-vtables", {}, {}, 6) |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 224 | .flag("+fexperimental-relative-c++-abi-vtables")); |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 225 | Multilibs.push_back(Multilib("relative-vtables+noexcept", {}, {}, 7) |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 226 | .flag("+fexperimental-relative-c++-abi-vtables") |
| 227 | .flag("-fexceptions") |
| 228 | .flag("+fno-exceptions")); |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 229 | Multilibs.push_back(Multilib("relative-vtables+asan", {}, {}, 8) |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 230 | .flag("+fexperimental-relative-c++-abi-vtables") |
| 231 | .flag("+fsanitize=address")); |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 232 | Multilibs.push_back(Multilib("relative-vtables+asan+noexcept", {}, {}, 9) |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 233 | .flag("+fexperimental-relative-c++-abi-vtables") |
| 234 | .flag("+fsanitize=address") |
| 235 | .flag("-fexceptions") |
| 236 | .flag("+fno-exceptions")); |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 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 | |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 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); |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 260 | addMultilibFlag(getSanitizerArgs().needsHwasanRt(), "fsanitize=hwaddress", |
| 261 | Flags); |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 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); |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 268 | addMultilibFlag(Args.getLastArgValue(options::OPT_fcxx_abi_EQ) == "itanium", |
| 269 | "fc++-abi=itanium", Flags); |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 270 | |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 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); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | std::string Fuchsia::ComputeEffectiveClangTriple(const ArgList &Args, |
| 282 | types::ID InputType) const { |
| 283 | llvm::Triple Triple(ComputeLLVMTriple(Args, InputType)); |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 284 | return Triple.str(); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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 { |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 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"); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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 = |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 346 | llvm::sys::path::is_absolute(dir) ? "" : StringRef(D.SysRoot); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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 | |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 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 | |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 385 | switch (GetCXXStdlibType(DriverArgs)) { |
| 386 | case ToolChain::CST_Libcxx: { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 387 | SmallString<128> P(D.Dir); |
| 388 | llvm::sys::path::append(P, "..", "include"); |
| 389 | AddCXXIncludePath(P); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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; |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 413 | Res |= SanitizerKind::HWAddress; |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 414 | Res |= SanitizerKind::PointerCompare; |
| 415 | Res |= SanitizerKind::PointerSubtract; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 416 | Res |= SanitizerKind::Fuzzer; |
| 417 | Res |= SanitizerKind::FuzzerNoLink; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 418 | Res |= SanitizerKind::Leak; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 419 | Res |= SanitizerKind::SafeStack; |
| 420 | Res |= SanitizerKind::Scudo; |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 421 | Res |= SanitizerKind::Thread; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 422 | return Res; |
| 423 | } |
| 424 | |
| 425 | SanitizerMask Fuchsia::getDefaultSanitizers() const { |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 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; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 439 | } |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 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 | } |