clang  9.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/Path.h"
19 
20 using namespace clang::driver;
21 using namespace clang::driver::toolchains;
22 using namespace clang::driver::tools;
23 using namespace clang;
24 using namespace llvm::opt;
25 
26 void fuchsia::Linker::ConstructJob(Compilation &C, const JobAction &JA,
27  const InputInfo &Output,
28  const InputInfoList &Inputs,
29  const ArgList &Args,
30  const char *LinkingOutput) const {
32  static_cast<const toolchains::Fuchsia &>(getToolChain());
33  const Driver &D = ToolChain.getDriver();
34 
35  ArgStringList CmdArgs;
36 
37  // Silence warning for "clang -g foo.o -o foo"
38  Args.ClaimAllArgs(options::OPT_g_Group);
39  // and "clang -emit-llvm foo.o -o foo"
40  Args.ClaimAllArgs(options::OPT_emit_llvm);
41  // and for "clang -w foo.o -o foo". Other warning options are already
42  // handled somewhere else.
43  Args.ClaimAllArgs(options::OPT_w);
44 
45  const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
46  if (llvm::sys::path::filename(Exec).equals_lower("ld.lld") ||
47  llvm::sys::path::stem(Exec).equals_lower("ld.lld")) {
48  CmdArgs.push_back("-z");
49  CmdArgs.push_back("rodynamic");
50  }
51 
52  if (!D.SysRoot.empty())
53  CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
54 
55  if (!Args.hasArg(options::OPT_shared) && !Args.hasArg(options::OPT_r))
56  CmdArgs.push_back("-pie");
57 
58  if (Args.hasArg(options::OPT_rdynamic))
59  CmdArgs.push_back("-export-dynamic");
60 
61  if (Args.hasArg(options::OPT_s))
62  CmdArgs.push_back("-s");
63 
64  if (Args.hasArg(options::OPT_r)) {
65  CmdArgs.push_back("-r");
66  } else {
67  CmdArgs.push_back("--build-id");
68  CmdArgs.push_back("--hash-style=gnu");
69  }
70 
71  CmdArgs.push_back("--eh-frame-hdr");
72 
73  if (Args.hasArg(options::OPT_static))
74  CmdArgs.push_back("-Bstatic");
75  else if (Args.hasArg(options::OPT_shared))
76  CmdArgs.push_back("-shared");
77 
78  const SanitizerArgs &SanArgs = ToolChain.getSanitizerArgs();
79 
80  if (!Args.hasArg(options::OPT_shared)) {
81  std::string Dyld = D.DyldPrefix;
82  if (SanArgs.needsAsanRt() && SanArgs.needsSharedRt())
83  Dyld += "asan/";
84  Dyld += "ld.so.1";
85  CmdArgs.push_back("-dynamic-linker");
86  CmdArgs.push_back(Args.MakeArgString(Dyld));
87  }
88 
89  CmdArgs.push_back("-o");
90  CmdArgs.push_back(Output.getFilename());
91 
92  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
93  if (!Args.hasArg(options::OPT_shared)) {
94  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("Scrt1.o")));
95  }
96  }
97 
98  Args.AddAllArgs(CmdArgs, options::OPT_L);
99  Args.AddAllArgs(CmdArgs, options::OPT_u);
100 
101  addSanitizerPathLibArgs(ToolChain, Args, CmdArgs);
102 
103  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
104 
105  if (D.isUsingLTO()) {
106  assert(!Inputs.empty() && "Must have at least one input.");
107  AddGoldPlugin(ToolChain, Args, CmdArgs, Output, Inputs[0],
108  D.getLTOMode() == LTOK_Thin);
109  }
110 
111  bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
112  bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
113  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
114  ToolChain.addProfileRTLibs(Args, CmdArgs);
115 
116  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
117  if (Args.hasArg(options::OPT_static))
118  CmdArgs.push_back("-Bdynamic");
119 
120  if (D.CCCIsCXX()) {
121  if (ToolChain.ShouldLinkCXXStdlib(Args)) {
122  bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
123  !Args.hasArg(options::OPT_static);
124  CmdArgs.push_back("--push-state");
125  CmdArgs.push_back("--as-needed");
126  if (OnlyLibstdcxxStatic)
127  CmdArgs.push_back("-Bstatic");
128  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
129  if (OnlyLibstdcxxStatic)
130  CmdArgs.push_back("-Bdynamic");
131  CmdArgs.push_back("-lm");
132  CmdArgs.push_back("--pop-state");
133  }
134  }
135 
136  if (NeedsSanitizerDeps)
137  linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
138 
139  if (NeedsXRayDeps)
140  linkXRayRuntimeDeps(ToolChain, CmdArgs);
141 
142  AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
143 
144  if (Args.hasArg(options::OPT_pthread) ||
145  Args.hasArg(options::OPT_pthreads))
146  CmdArgs.push_back("-lpthread");
147 
148  if (Args.hasArg(options::OPT_fsplit_stack))
149  CmdArgs.push_back("--wrap=pthread_create");
150 
151  if (!Args.hasArg(options::OPT_nolibc))
152  CmdArgs.push_back("-lc");
153  }
154 
155  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
156 }
157 
158 /// Fuchsia - Fuchsia tool chain which can call as(1) and ld(1) directly.
159 
160 Fuchsia::Fuchsia(const Driver &D, const llvm::Triple &Triple,
161  const ArgList &Args)
162  : ToolChain(D, Triple, Args) {
163  getProgramPaths().push_back(getDriver().getInstalledDir());
164  if (getDriver().getInstalledDir() != D.Dir)
165  getProgramPaths().push_back(D.Dir);
166 
167  if (!D.SysRoot.empty()) {
169  llvm::sys::path::append(P, "lib");
170  getFilePaths().push_back(P.str());
171  }
172 }
173 
174 std::string Fuchsia::ComputeEffectiveClangTriple(const ArgList &Args,
175  types::ID InputType) const {
176  llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
177  return (Triple.getArchName() + "-" + Triple.getOSName()).str();
178 }
179 
181  return new tools::fuchsia::Linker(*this);
182 }
183 
185  const ArgList &Args) const {
186  if (Arg *A = Args.getLastArg(clang::driver::options::OPT_rtlib_EQ)) {
187  StringRef Value = A->getValue();
188  if (Value != "compiler-rt")
189  getDriver().Diag(clang::diag::err_drv_invalid_rtlib_name)
190  << A->getAsString(Args);
191  }
192 
194 }
195 
197 Fuchsia::GetCXXStdlibType(const ArgList &Args) const {
198  if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
199  StringRef Value = A->getValue();
200  if (Value != "libc++")
201  getDriver().Diag(diag::err_drv_invalid_stdlib_name)
202  << A->getAsString(Args);
203  }
204 
205  return ToolChain::CST_Libcxx;
206 }
207 
208 void Fuchsia::addClangTargetOptions(const ArgList &DriverArgs,
209  ArgStringList &CC1Args,
210  Action::OffloadKind) const {
211  if (DriverArgs.hasFlag(options::OPT_fuse_init_array,
212  options::OPT_fno_use_init_array, true))
213  CC1Args.push_back("-fuse-init-array");
214 }
215 
216 void Fuchsia::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
217  ArgStringList &CC1Args) const {
218  const Driver &D = getDriver();
219 
220  if (DriverArgs.hasArg(options::OPT_nostdinc))
221  return;
222 
223  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
225  llvm::sys::path::append(P, "include");
226  addSystemInclude(DriverArgs, CC1Args, P);
227  }
228 
229  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
230  return;
231 
232  // Check for configure-time C include directories.
233  StringRef CIncludeDirs(C_INCLUDE_DIRS);
234  if (CIncludeDirs != "") {
236  CIncludeDirs.split(dirs, ":");
237  for (StringRef dir : dirs) {
238  StringRef Prefix =
239  llvm::sys::path::is_absolute(dir) ? StringRef(D.SysRoot) : "";
240  addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
241  }
242  return;
243  }
244 
245  if (!D.SysRoot.empty()) {
247  llvm::sys::path::append(P, "include");
248  addExternCSystemInclude(DriverArgs, CC1Args, P.str());
249  }
250 }
251 
252 void Fuchsia::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
253  ArgStringList &CC1Args) const {
254  if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
255  DriverArgs.hasArg(options::OPT_nostdincxx))
256  return;
257 
258  switch (GetCXXStdlibType(DriverArgs)) {
259  case ToolChain::CST_Libcxx: {
260  SmallString<128> P(getDriver().ResourceDir);
261  llvm::sys::path::append(P, "include", "c++", "v1");
262  addSystemInclude(DriverArgs, CC1Args, P.str());
263  break;
264  }
265 
266  default:
267  llvm_unreachable("invalid stdlib name");
268  }
269 }
270 
271 void Fuchsia::AddCXXStdlibLibArgs(const ArgList &Args,
272  ArgStringList &CmdArgs) const {
273  switch (GetCXXStdlibType(Args)) {
275  CmdArgs.push_back("-lc++");
276  break;
277 
279  llvm_unreachable("invalid stdlib name");
280  }
281 }
282 
285  Res |= SanitizerKind::Address;
286  Res |= SanitizerKind::Fuzzer;
287  Res |= SanitizerKind::FuzzerNoLink;
288  Res |= SanitizerKind::SafeStack;
289  Res |= SanitizerKind::Scudo;
290  return Res;
291 }
292 
294  return SanitizerKind::SafeStack;
295 }
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:757
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:533
bool addXRayRuntime(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
StringRef P
SanitizerMask getDefaultSanitizers() const override
Return sanitizers which are enabled by default.
Definition: Fuchsia.cpp:293
bool isUsingLTO() const
Returns true if we are performing any kind of LTO.
Definition: Driver.h:525
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)
LTOKind getLTOMode() const
Get the specific kind of LTO being performed.
Definition: Driver.h:528
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:252
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:670
std::string GetLinkerPath() const
Returns the linker path, respecting the -fuse-ld= argument to determine the linker suffix or name...
Definition: ToolChain.cpp:452
const char * getFilename() const
Definition: InputInfo.h:83
path_list & getProgramPaths()
Definition: ToolChain.h:234
void linkSanitizerRuntimeDeps(const ToolChain &TC, llvm::opt::ArgStringList &CmdArgs)
void addSanitizerPathLibArgs(const ToolChain &TC, const llvm::opt::ArgList &Args, 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)
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:444
path_list & getFilePaths()
Definition: ToolChain.h:231
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:57
void linkXRayRuntimeDeps(const ToolChain &TC, llvm::opt::ArgStringList &CmdArgs)
return Out str()
Tool * buildLinker() const override
Definition: Fuchsia.cpp:180
RuntimeLibType GetRuntimeLibType(const llvm::opt::ArgList &Args) const override
Definition: Fuchsia.cpp:184
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:205
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:271
SanitizerMask getSupportedSanitizers() const override
Return sanitizers which are available in this toolchain.
Definition: Fuchsia.cpp:283
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:818
const Driver & getDriver() const
Definition: ToolChain.h:193
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:216
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:742
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:795
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:115
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:858
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:174
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:208
CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const override
Definition: Fuchsia.cpp:197
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:160
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