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