clang  10.0.0svn
Fuchsia.cpp
Go to the documentation of this file.
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"
13 #include "clang/Driver/Driver.h"
15 #include "clang/Driver/Options.h"
17 #include "llvm/Option/ArgList.h"
18 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/Path.h"
20 #include "llvm/Support/VirtualFileSystem.h"
21 
22 using namespace clang::driver;
23 using namespace clang::driver::toolchains;
24 using namespace clang::driver::tools;
25 using namespace clang;
26 using namespace llvm::opt;
27 
29 
30 void fuchsia::Linker::ConstructJob(Compilation &C, const JobAction &JA,
31  const InputInfo &Output,
32  const InputInfoList &Inputs,
33  const ArgList &Args,
34  const char *LinkingOutput) const {
36  static_cast<const toolchains::Fuchsia &>(getToolChain());
37  const Driver &D = ToolChain.getDriver();
38 
39  ArgStringList CmdArgs;
40 
41  // Silence warning for "clang -g foo.o -o foo"
42  Args.ClaimAllArgs(options::OPT_g_Group);
43  // and "clang -emit-llvm foo.o -o foo"
44  Args.ClaimAllArgs(options::OPT_emit_llvm);
45  // and for "clang -w foo.o -o foo". Other warning options are already
46  // handled somewhere else.
47  Args.ClaimAllArgs(options::OPT_w);
48 
49  const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
50  if (llvm::sys::path::filename(Exec).equals_lower("ld.lld") ||
51  llvm::sys::path::stem(Exec).equals_lower("ld.lld")) {
52  CmdArgs.push_back("-z");
53  CmdArgs.push_back("rodynamic");
54  CmdArgs.push_back("-z");
55  CmdArgs.push_back("separate-loadable-segments");
56  }
57 
58  if (!D.SysRoot.empty())
59  CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
60 
61  if (!Args.hasArg(options::OPT_shared) && !Args.hasArg(options::OPT_r))
62  CmdArgs.push_back("-pie");
63 
64  if (Args.hasArg(options::OPT_rdynamic))
65  CmdArgs.push_back("-export-dynamic");
66 
67  if (Args.hasArg(options::OPT_s))
68  CmdArgs.push_back("-s");
69 
70  if (Args.hasArg(options::OPT_r)) {
71  CmdArgs.push_back("-r");
72  } else {
73  CmdArgs.push_back("--build-id");
74  CmdArgs.push_back("--hash-style=gnu");
75  }
76 
77  CmdArgs.push_back("--eh-frame-hdr");
78 
79  if (Args.hasArg(options::OPT_static))
80  CmdArgs.push_back("-Bstatic");
81  else if (Args.hasArg(options::OPT_shared))
82  CmdArgs.push_back("-shared");
83 
84  const SanitizerArgs &SanArgs = ToolChain.getSanitizerArgs();
85 
86  if (!Args.hasArg(options::OPT_shared)) {
87  std::string Dyld = D.DyldPrefix;
88  if (SanArgs.needsAsanRt() && SanArgs.needsSharedRt())
89  Dyld += "asan/";
90  Dyld += "ld.so.1";
91  CmdArgs.push_back("-dynamic-linker");
92  CmdArgs.push_back(Args.MakeArgString(Dyld));
93  }
94 
95  CmdArgs.push_back("-o");
96  CmdArgs.push_back(Output.getFilename());
97 
98  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
99  if (!Args.hasArg(options::OPT_shared)) {
100  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("Scrt1.o")));
101  }
102  }
103 
104  Args.AddAllArgs(CmdArgs, options::OPT_L);
105  Args.AddAllArgs(CmdArgs, options::OPT_u);
106 
107  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
108 
109  if (D.isUsingLTO()) {
110  assert(!Inputs.empty() && "Must have at least one input.");
111  AddGoldPlugin(ToolChain, Args, CmdArgs, Output, Inputs[0],
112  D.getLTOMode() == LTOK_Thin);
113  }
114 
115  bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
116  bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
117  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
118  ToolChain.addProfileRTLibs(Args, CmdArgs);
119 
120  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
121  if (Args.hasArg(options::OPT_static))
122  CmdArgs.push_back("-Bdynamic");
123 
124  if (D.CCCIsCXX()) {
125  if (ToolChain.ShouldLinkCXXStdlib(Args)) {
126  bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
127  !Args.hasArg(options::OPT_static);
128  CmdArgs.push_back("--push-state");
129  CmdArgs.push_back("--as-needed");
130  if (OnlyLibstdcxxStatic)
131  CmdArgs.push_back("-Bstatic");
132  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
133  if (OnlyLibstdcxxStatic)
134  CmdArgs.push_back("-Bdynamic");
135  CmdArgs.push_back("-lm");
136  CmdArgs.push_back("--pop-state");
137  }
138  }
139 
140  if (NeedsSanitizerDeps)
141  linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
142 
143  if (NeedsXRayDeps)
144  linkXRayRuntimeDeps(ToolChain, CmdArgs);
145 
146  AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
147 
148  if (Args.hasArg(options::OPT_pthread) ||
149  Args.hasArg(options::OPT_pthreads))
150  CmdArgs.push_back("-lpthread");
151 
152  if (Args.hasArg(options::OPT_fsplit_stack))
153  CmdArgs.push_back("--wrap=pthread_create");
154 
155  if (!Args.hasArg(options::OPT_nolibc))
156  CmdArgs.push_back("-lc");
157  }
158 
159  C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
160 }
161 
162 /// Fuchsia - Fuchsia tool chain which can call as(1) and ld(1) directly.
163 
164 Fuchsia::Fuchsia(const Driver &D, const llvm::Triple &Triple,
165  const ArgList &Args)
166  : ToolChain(D, Triple, Args) {
167  getProgramPaths().push_back(getDriver().getInstalledDir());
168  if (getDriver().getInstalledDir() != D.Dir)
169  getProgramPaths().push_back(D.Dir);
170 
171  if (!D.SysRoot.empty()) {
173  llvm::sys::path::append(P, "lib");
174  getFilePaths().push_back(P.str());
175  }
176 
177  auto FilePaths = [&](const Multilib &M) -> std::vector<std::string> {
178  std::vector<std::string> FP;
179  if (D.CCCIsCXX()) {
180  if (auto CXXStdlibPath = getCXXStdlibPath()) {
181  SmallString<128> P(*CXXStdlibPath);
182  llvm::sys::path::append(P, M.gccSuffix());
183  FP.push_back(P.str());
184  }
185  }
186  return FP;
187  };
188 
190  // Use the noexcept variant with -fno-exceptions to avoid the extra overhead.
191  Multilibs.push_back(Multilib("noexcept", {}, {}, 1)
192  .flag("-fexceptions")
193  .flag("+fno-exceptions"));
194  // ASan has higher priority because we always want the instrumentated version.
195  Multilibs.push_back(Multilib("asan", {}, {}, 2)
196  .flag("+fsanitize=address"));
197  // Use the asan+noexcept variant with ASan and -fno-exceptions.
198  Multilibs.push_back(Multilib("asan+noexcept", {}, {}, 3)
199  .flag("+fsanitize=address")
200  .flag("-fexceptions")
201  .flag("+fno-exceptions"));
202  Multilibs.FilterOut([&](const Multilib &M) {
203  std::vector<std::string> RD = FilePaths(M);
204  return std::all_of(RD.begin(), RD.end(), [&](std::string P) {
205  return !getVFS().exists(P);
206  });
207  });
208 
209  Multilib::flags_list Flags;
211  Args.hasFlag(options::OPT_fexceptions, options::OPT_fno_exceptions, true),
212  "fexceptions", Flags);
213  addMultilibFlag(getSanitizerArgs().needsAsanRt(), "fsanitize=address", Flags);
214  Multilibs.setFilePathsCallback(FilePaths);
215 
216  if (Multilibs.select(Flags, SelectedMultilib))
218  if (const auto &PathsCallback = Multilibs.filePathsCallback())
219  for (const auto &Path : PathsCallback(SelectedMultilib))
220  // Prepend the multilib path to ensure it takes the precedence.
221  getFilePaths().insert(getFilePaths().begin(), Path);
222 }
223 
224 std::string Fuchsia::ComputeEffectiveClangTriple(const ArgList &Args,
225  types::ID InputType) const {
226  llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
227  return (Triple.getArchName() + "-" + Triple.getOSName()).str();
228 }
229 
231  return new tools::fuchsia::Linker(*this);
232 }
233 
235  const ArgList &Args) const {
236  if (Arg *A = Args.getLastArg(clang::driver::options::OPT_rtlib_EQ)) {
237  StringRef Value = A->getValue();
238  if (Value != "compiler-rt")
239  getDriver().Diag(clang::diag::err_drv_invalid_rtlib_name)
240  << A->getAsString(Args);
241  }
242 
244 }
245 
247 Fuchsia::GetCXXStdlibType(const ArgList &Args) const {
248  if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
249  StringRef Value = A->getValue();
250  if (Value != "libc++")
251  getDriver().Diag(diag::err_drv_invalid_stdlib_name)
252  << A->getAsString(Args);
253  }
254 
255  return ToolChain::CST_Libcxx;
256 }
257 
258 void Fuchsia::addClangTargetOptions(const ArgList &DriverArgs,
259  ArgStringList &CC1Args,
260  Action::OffloadKind) const {
261  if (DriverArgs.hasFlag(options::OPT_fuse_init_array,
262  options::OPT_fno_use_init_array, true))
263  CC1Args.push_back("-fuse-init-array");
264 }
265 
266 void Fuchsia::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
267  ArgStringList &CC1Args) const {
268  const Driver &D = getDriver();
269 
270  if (DriverArgs.hasArg(options::OPT_nostdinc))
271  return;
272 
273  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
275  llvm::sys::path::append(P, "include");
276  addSystemInclude(DriverArgs, CC1Args, P);
277  }
278 
279  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
280  return;
281 
282  // Check for configure-time C include directories.
283  StringRef CIncludeDirs(C_INCLUDE_DIRS);
284  if (CIncludeDirs != "") {
286  CIncludeDirs.split(dirs, ":");
287  for (StringRef dir : dirs) {
288  StringRef Prefix =
289  llvm::sys::path::is_absolute(dir) ? StringRef(D.SysRoot) : "";
290  addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
291  }
292  return;
293  }
294 
295  if (!D.SysRoot.empty()) {
297  llvm::sys::path::append(P, "include");
298  addExternCSystemInclude(DriverArgs, CC1Args, P.str());
299  }
300 }
301 
302 void Fuchsia::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
303  ArgStringList &CC1Args) const {
304  if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
305  DriverArgs.hasArg(options::OPT_nostdincxx))
306  return;
307 
308  switch (GetCXXStdlibType(DriverArgs)) {
309  case ToolChain::CST_Libcxx: {
311  llvm::sys::path::append(P, "..", "include", "c++", "v1");
312  addSystemInclude(DriverArgs, CC1Args, P.str());
313  break;
314  }
315 
316  default:
317  llvm_unreachable("invalid stdlib name");
318  }
319 }
320 
321 void Fuchsia::AddCXXStdlibLibArgs(const ArgList &Args,
322  ArgStringList &CmdArgs) const {
323  switch (GetCXXStdlibType(Args)) {
325  CmdArgs.push_back("-lc++");
326  break;
327 
329  llvm_unreachable("invalid stdlib name");
330  }
331 }
332 
335  Res |= SanitizerKind::Address;
336  Res |= SanitizerKind::PointerCompare;
337  Res |= SanitizerKind::PointerSubtract;
338  Res |= SanitizerKind::Fuzzer;
339  Res |= SanitizerKind::FuzzerNoLink;
340  Res |= SanitizerKind::SafeStack;
341  Res |= SanitizerKind::Scudo;
342  return Res;
343 }
344 
346  return SanitizerKind::SafeStack;
347 }
static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory with extern "C" semantics to CC1 arguments...
Definition: ToolChain.cpp:816
virtual std::string ComputeLLVMTriple(const llvm::opt::ArgList &Args, types::ID InputType=types::TY_INVALID) const
ComputeLLVMTriple - Return the LLVM target triple to use, after taking command line arguments into ac...
Definition: ToolChain.cpp:592
bool addXRayRuntime(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
bool isDefault() const
Check whether the default is selected.
Definition: Multilib.h:106
StringRef P
SanitizerMask getDefaultSanitizers() const override
Return sanitizers which are enabled by default.
Definition: Fuchsia.cpp:345
bool isUsingLTO() const
Returns true if we are performing any kind of LTO.
Definition: Driver.h:538
std::string DyldPrefix
Dynamic loader prefix, if present.
Definition: Driver.h:150
void AddRunTimeLibs(const ToolChain &TC, const Driver &D, llvm::opt::ArgStringList &CmdArgs, const llvm::opt::ArgList &Args)
const IncludeDirsFunc & filePathsCallback() const
Definition: Multilib.h:186
LTOKind getLTOMode() const
Get the specific kind of LTO being performed.
Definition: Driver.h:541
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:108
void AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set the include paths to use for...
Definition: Fuchsia.cpp:302
virtual void addProfileRTLibs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
addProfileRTLibs - When -fprofile-instr-profile is specified, try to pass a suitable profile runtime ...
Definition: ToolChain.cpp:729
std::string GetLinkerPath() const
Returns the linker path, respecting the -fuse-ld= argument to determine the linker suffix or name...
Definition: ToolChain.cpp:511
bool select(const Multilib::flags_list &Flags, Multilib &M) const
Pick the best multilib in the set,.
Definition: Multilib.cpp:244
const char * getFilename() const
Definition: InputInfo.h:83
path_list & getProgramPaths()
Definition: ToolChain.h:238
void linkSanitizerRuntimeDeps(const ToolChain &TC, llvm::opt::ArgStringList &CmdArgs)
std::string Dir
The path the driver executable was in, as invoked from the command line.
Definition: Driver.h:119
void AddGoldPlugin(const ToolChain &ToolChain, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const InputInfo &Output, const InputInfo &Input, bool IsThinLTO)
bool addSanitizerRuntimes(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
void addMultilibFlag(bool Enabled, const char *const Flag, Multilib::flags_list &Flags)
Flag must be a flag accepted by the driver with its leading &#39;-&#39; removed,
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:503
path_list & getFilePaths()
Definition: ToolChain.h:235
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
void linkXRayRuntimeDeps(const ToolChain &TC, llvm::opt::ArgStringList &CmdArgs)
Tool * buildLinker() const override
Definition: Fuchsia.cpp:230
RuntimeLibType GetRuntimeLibType(const llvm::opt::ArgList &Args) const override
Definition: Fuchsia.cpp:234
virtual Optional< std::string > getCXXStdlibPath() const
Definition: ToolChain.cpp:446
std::vector< std::string > flags_list
Definition: Multilib.h:30
void push_back(const Multilib &M)
Add a completed Multilib to the set.
Definition: Multilib.cpp:232
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:205
This corresponds to a single GCC Multilib, or a segment of one controlled by a command line flag...
Definition: Multilib.h:28
void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const override
AddCXXStdlibLibArgs - Add the system specific linker arguments to use for the given C++ standard libr...
Definition: Fuchsia.cpp:321
SanitizerMask getSupportedSanitizers() const override
Return sanitizers which are available in this toolchain.
Definition: Fuchsia.cpp:333
void AddFilePathLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddFilePathLibArgs - Add each thing in getFilePaths() as a "-L" option.
Definition: ToolChain.cpp:887
const Driver & getDriver() const
Definition: ToolChain.h:197
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
Definition: Fuchsia.cpp:266
bool CCCIsCXX() const
Whether the driver should follow g++ like behavior.
Definition: Driver.h:172
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory to CC1 arguments.
Definition: ToolChain.cpp:801
llvm::vfs::FileSystem & getVFS() const
Definition: ToolChain.cpp:98
MultilibSet & setFilePathsCallback(IncludeDirsFunc F)
Definition: Multilib.h:181
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:864
Dataflow Directional Tag Classes.
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
std::string SysRoot
sysroot, if present
Definition: Driver.h:147
Tool - Information on a specific compilation tool.
Definition: Tool.h:33
const SanitizerArgs & getSanitizerArgs() const
Definition: ToolChain.cpp:116
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:923
MultilibSet & FilterOut(FilterCallback F)
Filter out some subset of the Multilibs using a user defined callback.
Definition: Multilib.cpp:212
std::string ComputeEffectiveClangTriple(const llvm::opt::ArgList &Args, types::ID InputType) const override
ComputeEffectiveClangTriple - Return the Clang triple to use for this target, which may take into acc...
Definition: Fuchsia.cpp:224
void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, Action::OffloadKind DeviceOffloadKind) const override
Add options that need to be passed to cc1 for this target.
Definition: Fuchsia.cpp:258
CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const override
Definition: Fuchsia.cpp:247
Fuchsia(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Fuchsia - Fuchsia tool chain which can call as(1) and ld(1) directly.
Definition: Fuchsia.cpp:164
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:88
std::string ResourceDir
The path to the compiler resource directory.
Definition: Driver.h:131