clang  6.0.0svn
NaCl.cpp
Go to the documentation of this file.
1 //===--- NaCl.cpp - Native Client ToolChain Implementations -----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "NaCl.h"
11 #include "InputInfo.h"
12 #include "CommonArgs.h"
14 #include "clang/Driver/Driver.h"
16 #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::tools;
22 using namespace clang::driver::toolchains;
23 using namespace clang;
24 using namespace llvm::opt;
25 
26 // NaCl ARM assembly (inline or standalone) can be written with a set of macros
27 // for the various SFI requirements like register masking. The assembly tool
28 // inserts the file containing the macros as an input into all the assembly
29 // jobs.
31  const InputInfo &Output,
32  const InputInfoList &Inputs,
33  const ArgList &Args,
34  const char *LinkingOutput) const {
36  static_cast<const toolchains::NaClToolChain &>(getToolChain());
37  InputInfo NaClMacros(types::TY_PP_Asm, ToolChain.GetNaClArmMacrosPath(),
38  "nacl-arm-macros.s");
39  InputInfoList NewInputs;
40  NewInputs.push_back(NaClMacros);
41  NewInputs.append(Inputs.begin(), Inputs.end());
42  gnutools::Assembler::ConstructJob(C, JA, Output, NewInputs, Args,
43  LinkingOutput);
44 }
45 
46 // This is quite similar to gnutools::Linker::ConstructJob with changes that
47 // we use static by default, do not yet support sanitizers or LTO, and a few
48 // others. Eventually we can support more of that and hopefully migrate back
49 // to gnutools::Linker.
51  const InputInfo &Output,
52  const InputInfoList &Inputs,
53  const ArgList &Args,
54  const char *LinkingOutput) const {
55 
57  static_cast<const toolchains::NaClToolChain &>(getToolChain());
58  const Driver &D = ToolChain.getDriver();
59  const llvm::Triple::ArchType Arch = ToolChain.getArch();
60  const bool IsStatic =
61  !Args.hasArg(options::OPT_dynamic) && !Args.hasArg(options::OPT_shared);
62 
63  ArgStringList CmdArgs;
64 
65  // Silence warning for "clang -g foo.o -o foo"
66  Args.ClaimAllArgs(options::OPT_g_Group);
67  // and "clang -emit-llvm foo.o -o foo"
68  Args.ClaimAllArgs(options::OPT_emit_llvm);
69  // and for "clang -w foo.o -o foo". Other warning options are already
70  // handled somewhere else.
71  Args.ClaimAllArgs(options::OPT_w);
72 
73  if (!D.SysRoot.empty())
74  CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
75 
76  if (Args.hasArg(options::OPT_rdynamic))
77  CmdArgs.push_back("-export-dynamic");
78 
79  if (Args.hasArg(options::OPT_s))
80  CmdArgs.push_back("-s");
81 
82  // NaClToolChain doesn't have ExtraOpts like Linux; the only relevant flag
83  // from there is --build-id, which we do want.
84  CmdArgs.push_back("--build-id");
85 
86  if (!IsStatic)
87  CmdArgs.push_back("--eh-frame-hdr");
88 
89  CmdArgs.push_back("-m");
90  if (Arch == llvm::Triple::x86)
91  CmdArgs.push_back("elf_i386_nacl");
92  else if (Arch == llvm::Triple::arm)
93  CmdArgs.push_back("armelf_nacl");
94  else if (Arch == llvm::Triple::x86_64)
95  CmdArgs.push_back("elf_x86_64_nacl");
96  else if (Arch == llvm::Triple::mipsel)
97  CmdArgs.push_back("mipselelf_nacl");
98  else
99  D.Diag(diag::err_target_unsupported_arch) << ToolChain.getArchName()
100  << "Native Client";
101 
102  if (IsStatic)
103  CmdArgs.push_back("-static");
104  else if (Args.hasArg(options::OPT_shared))
105  CmdArgs.push_back("-shared");
106 
107  CmdArgs.push_back("-o");
108  CmdArgs.push_back(Output.getFilename());
109  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
110  if (!Args.hasArg(options::OPT_shared))
111  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt1.o")));
112  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
113 
114  const char *crtbegin;
115  if (IsStatic)
116  crtbegin = "crtbeginT.o";
117  else if (Args.hasArg(options::OPT_shared))
118  crtbegin = "crtbeginS.o";
119  else
120  crtbegin = "crtbegin.o";
121  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
122  }
123 
124  Args.AddAllArgs(CmdArgs, options::OPT_L);
125  Args.AddAllArgs(CmdArgs, options::OPT_u);
126 
127  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
128 
129  if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
130  CmdArgs.push_back("--no-demangle");
131 
132  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
133 
134  if (D.CCCIsCXX() &&
135  !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
136  if (ToolChain.ShouldLinkCXXStdlib(Args)) {
137  bool OnlyLibstdcxxStatic =
138  Args.hasArg(options::OPT_static_libstdcxx) && !IsStatic;
139  if (OnlyLibstdcxxStatic)
140  CmdArgs.push_back("-Bstatic");
141  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
142  if (OnlyLibstdcxxStatic)
143  CmdArgs.push_back("-Bdynamic");
144  }
145  CmdArgs.push_back("-lm");
146  }
147 
148  if (!Args.hasArg(options::OPT_nostdlib)) {
149  if (!Args.hasArg(options::OPT_nodefaultlibs)) {
150  // Always use groups, since it has no effect on dynamic libraries.
151  CmdArgs.push_back("--start-group");
152  CmdArgs.push_back("-lc");
153  // NaCl's libc++ currently requires libpthread, so just always include it
154  // in the group for C++.
155  if (Args.hasArg(options::OPT_pthread) ||
156  Args.hasArg(options::OPT_pthreads) || D.CCCIsCXX()) {
157  // Gold, used by Mips, handles nested groups differently than ld, and
158  // without '-lnacl' it prefers symbols from libpthread.a over libnacl.a,
159  // which is not a desired behaviour here.
160  // See https://sourceware.org/ml/binutils/2015-03/msg00034.html
161  if (getToolChain().getArch() == llvm::Triple::mipsel)
162  CmdArgs.push_back("-lnacl");
163 
164  CmdArgs.push_back("-lpthread");
165  }
166 
167  CmdArgs.push_back("-lgcc");
168  CmdArgs.push_back("--as-needed");
169  if (IsStatic)
170  CmdArgs.push_back("-lgcc_eh");
171  else
172  CmdArgs.push_back("-lgcc_s");
173  CmdArgs.push_back("--no-as-needed");
174 
175  // Mips needs to create and use pnacl_legacy library that contains
176  // definitions from bitcode/pnaclmm.c and definitions for
177  // __nacl_tp_tls_offset() and __nacl_tp_tdb_offset().
178  if (getToolChain().getArch() == llvm::Triple::mipsel)
179  CmdArgs.push_back("-lpnacl_legacy");
180 
181  CmdArgs.push_back("--end-group");
182  }
183 
184  if (!Args.hasArg(options::OPT_nostartfiles)) {
185  const char *crtend;
186  if (Args.hasArg(options::OPT_shared))
187  crtend = "crtendS.o";
188  else
189  crtend = "crtend.o";
190 
191  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
192  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
193  }
194  }
195 
196  const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
197  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
198 }
199 
200 /// NaCl Toolchain
201 NaClToolChain::NaClToolChain(const Driver &D, const llvm::Triple &Triple,
202  const ArgList &Args)
203  : Generic_ELF(D, Triple, Args) {
204 
205  // Remove paths added by Generic_GCC. NaCl Toolchain cannot use the
206  // default paths, and must instead only use the paths provided
207  // with this toolchain based on architecture.
208  path_list &file_paths = getFilePaths();
209  path_list &prog_paths = getProgramPaths();
210 
211  file_paths.clear();
212  prog_paths.clear();
213 
214  // Path for library files (libc.a, ...)
215  std::string FilePath(getDriver().Dir + "/../");
216 
217  // Path for tools (clang, ld, etc..)
218  std::string ProgPath(getDriver().Dir + "/../");
219 
220  // Path for toolchain libraries (libgcc.a, ...)
221  std::string ToolPath(getDriver().ResourceDir + "/lib/");
222 
223  switch (Triple.getArch()) {
224  case llvm::Triple::x86:
225  file_paths.push_back(FilePath + "x86_64-nacl/lib32");
226  file_paths.push_back(FilePath + "i686-nacl/usr/lib");
227  prog_paths.push_back(ProgPath + "x86_64-nacl/bin");
228  file_paths.push_back(ToolPath + "i686-nacl");
229  break;
230  case llvm::Triple::x86_64:
231  file_paths.push_back(FilePath + "x86_64-nacl/lib");
232  file_paths.push_back(FilePath + "x86_64-nacl/usr/lib");
233  prog_paths.push_back(ProgPath + "x86_64-nacl/bin");
234  file_paths.push_back(ToolPath + "x86_64-nacl");
235  break;
236  case llvm::Triple::arm:
237  file_paths.push_back(FilePath + "arm-nacl/lib");
238  file_paths.push_back(FilePath + "arm-nacl/usr/lib");
239  prog_paths.push_back(ProgPath + "arm-nacl/bin");
240  file_paths.push_back(ToolPath + "arm-nacl");
241  break;
242  case llvm::Triple::mipsel:
243  file_paths.push_back(FilePath + "mipsel-nacl/lib");
244  file_paths.push_back(FilePath + "mipsel-nacl/usr/lib");
245  prog_paths.push_back(ProgPath + "bin");
246  file_paths.push_back(ToolPath + "mipsel-nacl");
247  break;
248  default:
249  break;
250  }
251 
252  NaClArmMacrosPath = GetFilePath("nacl-arm-macros.s");
253 }
254 
255 void NaClToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
256  ArgStringList &CC1Args) const {
257  const Driver &D = getDriver();
258  if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
259  return;
260 
261  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
263  llvm::sys::path::append(P, "include");
264  addSystemInclude(DriverArgs, CC1Args, P.str());
265  }
266 
267  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
268  return;
269 
270  SmallString<128> P(D.Dir + "/../");
271  switch (getTriple().getArch()) {
272  case llvm::Triple::x86:
273  // x86 is special because multilib style uses x86_64-nacl/include for libc
274  // headers but the SDK wants i686-nacl/usr/include. The other architectures
275  // have the same substring.
276  llvm::sys::path::append(P, "i686-nacl/usr/include");
277  addSystemInclude(DriverArgs, CC1Args, P.str());
278  llvm::sys::path::remove_filename(P);
279  llvm::sys::path::remove_filename(P);
280  llvm::sys::path::remove_filename(P);
281  llvm::sys::path::append(P, "x86_64-nacl/include");
282  addSystemInclude(DriverArgs, CC1Args, P.str());
283  return;
284  case llvm::Triple::arm:
285  llvm::sys::path::append(P, "arm-nacl/usr/include");
286  break;
287  case llvm::Triple::x86_64:
288  llvm::sys::path::append(P, "x86_64-nacl/usr/include");
289  break;
290  case llvm::Triple::mipsel:
291  llvm::sys::path::append(P, "mipsel-nacl/usr/include");
292  break;
293  default:
294  return;
295  }
296 
297  addSystemInclude(DriverArgs, CC1Args, P.str());
298  llvm::sys::path::remove_filename(P);
299  llvm::sys::path::remove_filename(P);
300  llvm::sys::path::append(P, "include");
301  addSystemInclude(DriverArgs, CC1Args, P.str());
302 }
303 
304 void NaClToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
305  ArgStringList &CmdArgs) const {
306  // Check for -stdlib= flags. We only support libc++ but this consumes the arg
307  // if the value is libc++, and emits an error for other values.
308  GetCXXStdlibType(Args);
309  CmdArgs.push_back("-lc++");
310 }
311 
313  const Driver &D = getDriver();
314 
315  SmallString<128> P(D.Dir + "/../");
316  switch (getTriple().getArch()) {
317  case llvm::Triple::arm:
318  llvm::sys::path::append(P, "arm-nacl/include/c++/v1");
319  return P.str();
320  case llvm::Triple::x86:
321  llvm::sys::path::append(P, "x86_64-nacl/include/c++/v1");
322  return P.str();
323  case llvm::Triple::x86_64:
324  llvm::sys::path::append(P, "x86_64-nacl/include/c++/v1");
325  return P.str();
326  case llvm::Triple::mipsel:
327  llvm::sys::path::append(P, "mipsel-nacl/include/c++/v1");
328  return P.str();
329  default:
330  return "";
331  }
332 }
333 
335 NaClToolChain::GetCXXStdlibType(const ArgList &Args) const {
336  if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
337  StringRef Value = A->getValue();
338  if (Value == "libc++")
339  return ToolChain::CST_Libcxx;
340  getDriver().Diag(clang::diag::err_drv_invalid_stdlib_name)
341  << A->getAsString(Args);
342  }
343 
344  return ToolChain::CST_Libcxx;
345 }
346 
347 std::string
349  types::ID InputType) const {
350  llvm::Triple TheTriple(ComputeLLVMTriple(Args, InputType));
351  if (TheTriple.getArch() == llvm::Triple::arm &&
352  TheTriple.getEnvironment() == llvm::Triple::UnknownEnvironment)
353  TheTriple.setEnvironment(llvm::Triple::GNUEABIHF);
354  return TheTriple.getTriple();
355 }
356 
358  return new tools::nacltools::Linker(*this);
359 }
360 
362  if (getTriple().getArch() == llvm::Triple::arm)
363  return new tools::nacltools::AssemblerARM(*this);
364  return new tools::gnutools::Assembler(*this);
365 }
CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const override
Definition: NaCl.cpp:335
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: NaCl.cpp:304
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:482
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs...
Definition: NaCl.cpp:30
Tool * buildAssembler() const override
Definition: NaCl.cpp:361
StringRef getArchName() const
Definition: ToolChain.h:184
StringRef P
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:116
std::string GetLinkerPath() const
Returns the linker path, respecting the -fuse-ld= argument to determine the linker suffix or name...
Definition: ToolChain.cpp:399
std::string findLibCxxIncludePath() const override
Definition: NaCl.cpp:312
const char * getFilename() const
Definition: InputInfo.h:84
path_list & getProgramPaths()
Definition: ToolChain.h:205
std::string Dir
The path the driver executable was in, as invoked from the command line.
Definition: Driver.h:127
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
Definition: NaCl.cpp:255
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:23
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:391
path_list & getFilePaths()
Definition: ToolChain.h:202
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:65
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs...
Definition: NaCl.cpp:50
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:189
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:183
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:740
const Driver & getDriver() const
Definition: ToolChain.h:167
bool CCCIsCXX() const
Whether the driver should follow g++ like behavior.
Definition: Driver.h:174
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:664
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs...
Definition: Gnu.cpp:556
const char * GetNaClArmMacrosPath() const
Definition: NaCl.h:70
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:717
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:149
Tool - Information on a specific compilation tool.
Definition: Tool.h:34
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: NaCl.cpp:348
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:34
const llvm::Triple & getTriple() const
Definition: ToolChain.h:169
Tool * buildLinker() const override
Definition: NaCl.cpp:357
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:73
std::string ResourceDir
The path to the compiler resource directory.
Definition: Driver.h:139