1 //===--- NaCl.cpp - Native Client ToolChain Implementations -----*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 #include "InputInfo.h"
11 #include "CommonArgs.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 "llvm/Option/ArgList.h"
17 #include "llvm/Support/Path.h"
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;
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
29 void nacltools::AssemblerARM::ConstructJob(Compilation &C, const JobAction &JA,
30 const InputInfo &Output,
31 const InputInfoList &Inputs,
33 const char *LinkingOutput) const {
34 const toolchains::NaClToolChain &ToolChain =
35 static_cast<const toolchains::NaClToolChain &>(getToolChain());
36 InputInfo NaClMacros(types::TY_PP_Asm, ToolChain.GetNaClArmMacrosPath(),
38 InputInfoList NewInputs;
39 NewInputs.push_back(NaClMacros);
40 NewInputs.append(Inputs.begin(), Inputs.end());
41 gnutools::Assembler::ConstructJob(C, JA, Output, NewInputs, Args,
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.
49 void nacltools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
50 const InputInfo &Output,
51 const InputInfoList &Inputs,
53 const char *LinkingOutput) const {
55 const toolchains::NaClToolChain &ToolChain =
56 static_cast<const toolchains::NaClToolChain &>(getToolChain());
57 const Driver &D = ToolChain.getDriver();
58 const llvm::Triple::ArchType Arch = ToolChain.getArch();
60 !Args.hasArg(options::OPT_dynamic) && !Args.hasArg(options::OPT_shared);
62 ArgStringList CmdArgs;
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);
72 if (!D.SysRoot.empty())
73 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
75 if (Args.hasArg(options::OPT_rdynamic))
76 CmdArgs.push_back("-export-dynamic");
78 if (Args.hasArg(options::OPT_s))
79 CmdArgs.push_back("-s");
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");
86 CmdArgs.push_back("--eh-frame-hdr");
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");
98 D.Diag(diag::err_target_unsupported_arch) << ToolChain.getArchName()
102 CmdArgs.push_back("-static");
103 else if (Args.hasArg(options::OPT_shared))
104 CmdArgs.push_back("-shared");
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")));
113 const char *crtbegin;
115 crtbegin = "crtbeginT.o";
116 else if (Args.hasArg(options::OPT_shared))
117 crtbegin = "crtbeginS.o";
119 crtbegin = "crtbegin.o";
120 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
123 Args.AddAllArgs(CmdArgs, options::OPT_L);
124 Args.AddAllArgs(CmdArgs, options::OPT_u);
126 ToolChain.AddFilePathLibArgs(Args, CmdArgs);
128 if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
129 CmdArgs.push_back("--no-demangle");
131 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
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");
144 CmdArgs.push_back("-lm");
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");
163 CmdArgs.push_back("-lpthread");
166 CmdArgs.push_back("-lgcc");
167 CmdArgs.push_back("--as-needed");
169 CmdArgs.push_back("-lgcc_eh");
171 CmdArgs.push_back("-lgcc_s");
172 CmdArgs.push_back("--no-as-needed");
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");
180 CmdArgs.push_back("--end-group");
183 if (!Args.hasArg(options::OPT_nostartfiles)) {
185 if (Args.hasArg(options::OPT_shared))
186 crtend = "crtendS.o";
190 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
191 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
195 const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
196 C.addCommand(std::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
200 NaClToolChain::NaClToolChain(const Driver &D, const llvm::Triple &Triple,
202 : Generic_ELF(D, Triple, Args) {
204 // Remove paths added by Generic_GCC. NaCl Toolchain cannot use the
205 // default paths, and must instead only use the paths provided
206 // with this toolchain based on architecture.
207 path_list &file_paths = getFilePaths();
208 path_list &prog_paths = getProgramPaths();
213 // Path for library files (libc.a, ...)
214 std::string FilePath(getDriver().Dir + "/../");
216 // Path for tools (clang, ld, etc..)
217 std::string ProgPath(getDriver().Dir + "/../");
219 // Path for toolchain libraries (libgcc.a, ...)
220 std::string ToolPath(getDriver().ResourceDir + "/lib/");
222 switch (Triple.getArch()) {
223 case llvm::Triple::x86:
224 file_paths.push_back(FilePath + "x86_64-nacl/lib32");
225 file_paths.push_back(FilePath + "i686-nacl/usr/lib");
226 prog_paths.push_back(ProgPath + "x86_64-nacl/bin");
227 file_paths.push_back(ToolPath + "i686-nacl");
229 case llvm::Triple::x86_64:
230 file_paths.push_back(FilePath + "x86_64-nacl/lib");
231 file_paths.push_back(FilePath + "x86_64-nacl/usr/lib");
232 prog_paths.push_back(ProgPath + "x86_64-nacl/bin");
233 file_paths.push_back(ToolPath + "x86_64-nacl");
235 case llvm::Triple::arm:
236 file_paths.push_back(FilePath + "arm-nacl/lib");
237 file_paths.push_back(FilePath + "arm-nacl/usr/lib");
238 prog_paths.push_back(ProgPath + "arm-nacl/bin");
239 file_paths.push_back(ToolPath + "arm-nacl");
241 case llvm::Triple::mipsel:
242 file_paths.push_back(FilePath + "mipsel-nacl/lib");
243 file_paths.push_back(FilePath + "mipsel-nacl/usr/lib");
244 prog_paths.push_back(ProgPath + "bin");
245 file_paths.push_back(ToolPath + "mipsel-nacl");
251 NaClArmMacrosPath = GetFilePath("nacl-arm-macros.s");
254 void NaClToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
255 ArgStringList &CC1Args) const {
256 const Driver &D = getDriver();
257 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
260 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
261 SmallString<128> P(D.ResourceDir);
262 llvm::sys::path::append(P, "include");
263 addSystemInclude(DriverArgs, CC1Args, P.str());
266 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
269 SmallString<128> P(D.Dir + "/../");
270 switch (getTriple().getArch()) {
271 case llvm::Triple::x86:
272 // x86 is special because multilib style uses x86_64-nacl/include for libc
273 // headers but the SDK wants i686-nacl/usr/include. The other architectures
274 // have the same substring.
275 llvm::sys::path::append(P, "i686-nacl/usr/include");
276 addSystemInclude(DriverArgs, CC1Args, P.str());
277 llvm::sys::path::remove_filename(P);
278 llvm::sys::path::remove_filename(P);
279 llvm::sys::path::remove_filename(P);
280 llvm::sys::path::append(P, "x86_64-nacl/include");
281 addSystemInclude(DriverArgs, CC1Args, P.str());
283 case llvm::Triple::arm:
284 llvm::sys::path::append(P, "arm-nacl/usr/include");
286 case llvm::Triple::x86_64:
287 llvm::sys::path::append(P, "x86_64-nacl/usr/include");
289 case llvm::Triple::mipsel:
290 llvm::sys::path::append(P, "mipsel-nacl/usr/include");
296 addSystemInclude(DriverArgs, CC1Args, P.str());
297 llvm::sys::path::remove_filename(P);
298 llvm::sys::path::remove_filename(P);
299 llvm::sys::path::append(P, "include");
300 addSystemInclude(DriverArgs, CC1Args, P.str());
303 void NaClToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
304 ArgStringList &CmdArgs) const {
305 // Check for -stdlib= flags. We only support libc++ but this consumes the arg
306 // if the value is libc++, and emits an error for other values.
307 GetCXXStdlibType(Args);
308 CmdArgs.push_back("-lc++");
311 void NaClToolChain::addLibCxxIncludePaths(
312 const llvm::opt::ArgList &DriverArgs,
313 llvm::opt::ArgStringList &CC1Args) const {
314 const Driver &D = getDriver();
316 SmallString<128> P(D.Dir + "/../");
317 switch (getTriple().getArch()) {
320 case llvm::Triple::arm:
321 llvm::sys::path::append(P, "arm-nacl/include/c++/v1");
322 addSystemInclude(DriverArgs, CC1Args, P.str());
324 case llvm::Triple::x86:
325 llvm::sys::path::append(P, "x86_64-nacl/include/c++/v1");
326 addSystemInclude(DriverArgs, CC1Args, P.str());
328 case llvm::Triple::x86_64:
329 llvm::sys::path::append(P, "x86_64-nacl/include/c++/v1");
330 addSystemInclude(DriverArgs, CC1Args, P.str());
332 case llvm::Triple::mipsel:
333 llvm::sys::path::append(P, "mipsel-nacl/include/c++/v1");
334 addSystemInclude(DriverArgs, CC1Args, P.str());
339 ToolChain::CXXStdlibType
340 NaClToolChain::GetCXXStdlibType(const ArgList &Args) const {
341 if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
342 StringRef Value = A->getValue();
343 if (Value == "libc++")
344 return ToolChain::CST_Libcxx;
345 getDriver().Diag(clang::diag::err_drv_invalid_stdlib_name)
346 << A->getAsString(Args);
349 return ToolChain::CST_Libcxx;
353 NaClToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
354 types::ID InputType) const {
355 llvm::Triple TheTriple(ComputeLLVMTriple(Args, InputType));
356 if (TheTriple.getArch() == llvm::Triple::arm &&
357 TheTriple.getEnvironment() == llvm::Triple::UnknownEnvironment)
358 TheTriple.setEnvironment(llvm::Triple::GNUEABIHF);
359 return TheTriple.getTriple();
362 Tool *NaClToolChain::buildLinker() const {
363 return new tools::nacltools::Linker(*this);
366 Tool *NaClToolChain::buildAssembler() const {
367 if (getTriple().getArch() == llvm::Triple::arm)
368 return new tools::nacltools::AssemblerARM(*this);
369 return new tools::gnutools::Assembler(*this);