/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Driver/ToolChains/NaCl.cpp
Line | Count | Source (jump to first uncovered line) |
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" |
11 | | #include "clang/Driver/Compilation.h" |
12 | | #include "clang/Driver/Driver.h" |
13 | | #include "clang/Driver/DriverDiagnostic.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. |
29 | | void nacltools::AssemblerARM::ConstructJob(Compilation &C, const JobAction &JA, |
30 | | const InputInfo &Output, |
31 | | const InputInfoList &Inputs, |
32 | | const ArgList &Args, |
33 | 5 | const char *LinkingOutput) const { |
34 | 5 | const toolchains::NaClToolChain &ToolChain = |
35 | 5 | static_cast<const toolchains::NaClToolChain &>(getToolChain()); |
36 | 5 | InputInfo NaClMacros(types::TY_PP_Asm, ToolChain.GetNaClArmMacrosPath(), |
37 | 5 | "nacl-arm-macros.s"); |
38 | 5 | InputInfoList NewInputs; |
39 | 5 | NewInputs.push_back(NaClMacros); |
40 | 5 | NewInputs.append(Inputs.begin(), Inputs.end()); |
41 | 5 | gnutools::Assembler::ConstructJob(C, JA, Output, NewInputs, Args, |
42 | 5 | LinkingOutput); |
43 | 5 | } |
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. |
49 | | void nacltools::Linker::ConstructJob(Compilation &C, const JobAction &JA, |
50 | | const InputInfo &Output, |
51 | | const InputInfoList &Inputs, |
52 | | const ArgList &Args, |
53 | 12 | const char *LinkingOutput) const { |
54 | | |
55 | 12 | const toolchains::NaClToolChain &ToolChain = |
56 | 12 | static_cast<const toolchains::NaClToolChain &>(getToolChain()); |
57 | 12 | const Driver &D = ToolChain.getDriver(); |
58 | 12 | const llvm::Triple::ArchType Arch = ToolChain.getArch(); |
59 | 12 | const bool IsStatic = |
60 | 12 | !Args.hasArg(options::OPT_dynamic) && !Args.hasArg(options::OPT_shared); |
61 | | |
62 | 12 | ArgStringList CmdArgs; |
63 | | |
64 | | // Silence warning for "clang -g foo.o -o foo" |
65 | 12 | Args.ClaimAllArgs(options::OPT_g_Group); |
66 | | // and "clang -emit-llvm foo.o -o foo" |
67 | 12 | 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 | 12 | Args.ClaimAllArgs(options::OPT_w); |
71 | | |
72 | 12 | if (!D.SysRoot.empty()) |
73 | 0 | CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot)); |
74 | | |
75 | 12 | if (Args.hasArg(options::OPT_rdynamic)) |
76 | 0 | CmdArgs.push_back("-export-dynamic"); |
77 | | |
78 | 12 | if (Args.hasArg(options::OPT_s)) |
79 | 0 | 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 | 12 | CmdArgs.push_back("--build-id"); |
84 | | |
85 | 12 | if (!IsStatic) |
86 | 0 | CmdArgs.push_back("--eh-frame-hdr"); |
87 | | |
88 | 12 | CmdArgs.push_back("-m"); |
89 | 12 | if (Arch == llvm::Triple::x86) |
90 | 2 | CmdArgs.push_back("elf_i386_nacl"); |
91 | 10 | else if (Arch == llvm::Triple::arm) |
92 | 5 | CmdArgs.push_back("armelf_nacl"); |
93 | 5 | else if (Arch == llvm::Triple::x86_64) |
94 | 2 | CmdArgs.push_back("elf_x86_64_nacl"); |
95 | 3 | else if (Arch == llvm::Triple::mipsel) |
96 | 2 | CmdArgs.push_back("mipselelf_nacl"); |
97 | 1 | else |
98 | 1 | D.Diag(diag::err_target_unsupported_arch) << ToolChain.getArchName() |
99 | 1 | << "Native Client"; |
100 | | |
101 | 12 | if (IsStatic) |
102 | 12 | CmdArgs.push_back("-static"); |
103 | 0 | else if (Args.hasArg(options::OPT_shared)) |
104 | 0 | CmdArgs.push_back("-shared"); |
105 | | |
106 | 12 | CmdArgs.push_back("-o"); |
107 | 12 | CmdArgs.push_back(Output.getFilename()); |
108 | 12 | if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) { |
109 | 12 | if (!Args.hasArg(options::OPT_shared)) |
110 | 12 | CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt1.o"))); |
111 | 12 | CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o"))); |
112 | | |
113 | 12 | const char *crtbegin; |
114 | 12 | if (IsStatic) |
115 | 12 | crtbegin = "crtbeginT.o"; |
116 | 0 | else if (Args.hasArg(options::OPT_shared)) |
117 | 0 | crtbegin = "crtbeginS.o"; |
118 | 0 | else |
119 | 0 | crtbegin = "crtbegin.o"; |
120 | 12 | CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin))); |
121 | 12 | } |
122 | | |
123 | 12 | Args.AddAllArgs(CmdArgs, options::OPT_L); |
124 | 12 | Args.AddAllArgs(CmdArgs, options::OPT_u); |
125 | | |
126 | 12 | ToolChain.AddFilePathLibArgs(Args, CmdArgs); |
127 | | |
128 | 12 | if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle)) |
129 | 0 | CmdArgs.push_back("--no-demangle"); |
130 | | |
131 | 12 | AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA); |
132 | | |
133 | 12 | if (D.CCCIsCXX() && |
134 | 12 | !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)4 ) { |
135 | 4 | if (ToolChain.ShouldLinkCXXStdlib(Args)) { |
136 | 4 | bool OnlyLibstdcxxStatic = |
137 | 4 | Args.hasArg(options::OPT_static_libstdcxx) && !IsStatic0 ; |
138 | 4 | if (OnlyLibstdcxxStatic) |
139 | 0 | CmdArgs.push_back("-Bstatic"); |
140 | 4 | ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs); |
141 | 4 | if (OnlyLibstdcxxStatic) |
142 | 0 | CmdArgs.push_back("-Bdynamic"); |
143 | 4 | } |
144 | 4 | CmdArgs.push_back("-lm"); |
145 | 4 | } |
146 | | |
147 | 12 | if (!Args.hasArg(options::OPT_nostdlib)) { |
148 | 12 | if (!Args.hasArg(options::OPT_nodefaultlibs)) { |
149 | | // Always use groups, since it has no effect on dynamic libraries. |
150 | 12 | CmdArgs.push_back("--start-group"); |
151 | 12 | CmdArgs.push_back("-lc"); |
152 | | // NaCl's libc++ currently requires libpthread, so just always include it |
153 | | // in the group for C++. |
154 | 12 | if (Args.hasArg(options::OPT_pthread) || |
155 | 12 | 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 | 4 | if (getToolChain().getArch() == llvm::Triple::mipsel) |
161 | 1 | CmdArgs.push_back("-lnacl"); |
162 | | |
163 | 4 | CmdArgs.push_back("-lpthread"); |
164 | 4 | } |
165 | | |
166 | 12 | CmdArgs.push_back("-lgcc"); |
167 | 12 | CmdArgs.push_back("--as-needed"); |
168 | 12 | if (IsStatic) |
169 | 12 | CmdArgs.push_back("-lgcc_eh"); |
170 | 0 | else |
171 | 0 | CmdArgs.push_back("-lgcc_s"); |
172 | 12 | 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 | 12 | if (getToolChain().getArch() == llvm::Triple::mipsel) |
178 | 2 | CmdArgs.push_back("-lpnacl_legacy"); |
179 | | |
180 | 12 | CmdArgs.push_back("--end-group"); |
181 | 12 | } |
182 | | |
183 | 12 | if (!Args.hasArg(options::OPT_nostartfiles)) { |
184 | 12 | const char *crtend; |
185 | 12 | if (Args.hasArg(options::OPT_shared)) |
186 | 0 | crtend = "crtendS.o"; |
187 | 12 | else |
188 | 12 | crtend = "crtend.o"; |
189 | | |
190 | 12 | CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend))); |
191 | 12 | CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o"))); |
192 | 12 | } |
193 | 12 | } |
194 | | |
195 | 12 | const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath()); |
196 | 12 | C.addCommand(std::make_unique<Command>(JA, *this, |
197 | 12 | ResponseFileSupport::AtFileCurCP(), |
198 | 12 | Exec, CmdArgs, Inputs, Output)); |
199 | 12 | } |
200 | | |
201 | | /// NaCl Toolchain |
202 | | NaClToolChain::NaClToolChain(const Driver &D, const llvm::Triple &Triple, |
203 | | const ArgList &Args) |
204 | 18 | : 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 | 18 | path_list &file_paths = getFilePaths(); |
210 | 18 | path_list &prog_paths = getProgramPaths(); |
211 | | |
212 | 18 | file_paths.clear(); |
213 | 18 | prog_paths.clear(); |
214 | | |
215 | | // Path for library files (libc.a, ...) |
216 | 18 | std::string FilePath(getDriver().Dir + "/../"); |
217 | | |
218 | | // Path for tools (clang, ld, etc..) |
219 | 18 | std::string ProgPath(getDriver().Dir + "/../"); |
220 | | |
221 | | // Path for toolchain libraries (libgcc.a, ...) |
222 | 18 | std::string ToolPath(getDriver().ResourceDir + "/lib/"); |
223 | | |
224 | 18 | switch (Triple.getArch()) { |
225 | 2 | case llvm::Triple::x86: |
226 | 2 | file_paths.push_back(FilePath + "x86_64-nacl/lib32"); |
227 | 2 | file_paths.push_back(FilePath + "i686-nacl/usr/lib"); |
228 | 2 | prog_paths.push_back(ProgPath + "x86_64-nacl/bin"); |
229 | 2 | file_paths.push_back(ToolPath + "i686-nacl"); |
230 | 2 | break; |
231 | 5 | case llvm::Triple::x86_64: |
232 | 5 | file_paths.push_back(FilePath + "x86_64-nacl/lib"); |
233 | 5 | file_paths.push_back(FilePath + "x86_64-nacl/usr/lib"); |
234 | 5 | prog_paths.push_back(ProgPath + "x86_64-nacl/bin"); |
235 | 5 | file_paths.push_back(ToolPath + "x86_64-nacl"); |
236 | 5 | break; |
237 | 5 | case llvm::Triple::arm: |
238 | 5 | file_paths.push_back(FilePath + "arm-nacl/lib"); |
239 | 5 | file_paths.push_back(FilePath + "arm-nacl/usr/lib"); |
240 | 5 | prog_paths.push_back(ProgPath + "arm-nacl/bin"); |
241 | 5 | file_paths.push_back(ToolPath + "arm-nacl"); |
242 | 5 | break; |
243 | 5 | case llvm::Triple::mipsel: |
244 | 5 | file_paths.push_back(FilePath + "mipsel-nacl/lib"); |
245 | 5 | file_paths.push_back(FilePath + "mipsel-nacl/usr/lib"); |
246 | 5 | prog_paths.push_back(ProgPath + "bin"); |
247 | 5 | file_paths.push_back(ToolPath + "mipsel-nacl"); |
248 | 5 | break; |
249 | 1 | default: |
250 | 1 | break; |
251 | 18 | } |
252 | | |
253 | 18 | NaClArmMacrosPath = GetFilePath("nacl-arm-macros.s"); |
254 | 18 | } |
255 | | |
256 | | void NaClToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, |
257 | 18 | ArgStringList &CC1Args) const { |
258 | 18 | const Driver &D = getDriver(); |
259 | 18 | if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)) |
260 | 0 | return; |
261 | | |
262 | 18 | if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { |
263 | 18 | SmallString<128> P(D.ResourceDir); |
264 | 18 | llvm::sys::path::append(P, "include"); |
265 | 18 | addSystemInclude(DriverArgs, CC1Args, P.str()); |
266 | 18 | } |
267 | | |
268 | 18 | if (DriverArgs.hasArg(options::OPT_nostdlibinc)) |
269 | 0 | return; |
270 | | |
271 | 18 | SmallString<128> P(D.Dir + "/../"); |
272 | 18 | switch (getTriple().getArch()) { |
273 | 2 | 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 | 2 | llvm::sys::path::append(P, "i686-nacl/usr/include"); |
278 | 2 | addSystemInclude(DriverArgs, CC1Args, P.str()); |
279 | 2 | llvm::sys::path::remove_filename(P); |
280 | 2 | llvm::sys::path::remove_filename(P); |
281 | 2 | llvm::sys::path::remove_filename(P); |
282 | 2 | llvm::sys::path::append(P, "x86_64-nacl/include"); |
283 | 2 | addSystemInclude(DriverArgs, CC1Args, P.str()); |
284 | 2 | return; |
285 | 5 | case llvm::Triple::arm: |
286 | 5 | llvm::sys::path::append(P, "arm-nacl/usr/include"); |
287 | 5 | break; |
288 | 5 | case llvm::Triple::x86_64: |
289 | 5 | llvm::sys::path::append(P, "x86_64-nacl/usr/include"); |
290 | 5 | break; |
291 | 5 | case llvm::Triple::mipsel: |
292 | 5 | llvm::sys::path::append(P, "mipsel-nacl/usr/include"); |
293 | 5 | break; |
294 | 1 | default: |
295 | 1 | return; |
296 | 18 | } |
297 | | |
298 | 15 | addSystemInclude(DriverArgs, CC1Args, P.str()); |
299 | 15 | llvm::sys::path::remove_filename(P); |
300 | 15 | llvm::sys::path::remove_filename(P); |
301 | 15 | llvm::sys::path::append(P, "include"); |
302 | 15 | addSystemInclude(DriverArgs, CC1Args, P.str()); |
303 | 15 | } |
304 | | |
305 | | void NaClToolChain::AddCXXStdlibLibArgs(const ArgList &Args, |
306 | 4 | 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 | 4 | GetCXXStdlibType(Args); |
310 | 4 | CmdArgs.push_back("-lc++"); |
311 | 4 | if (Args.hasArg(options::OPT_fexperimental_library)) |
312 | 0 | CmdArgs.push_back("-lc++experimental"); |
313 | 4 | } |
314 | | |
315 | | void NaClToolChain::addLibCxxIncludePaths( |
316 | | const llvm::opt::ArgList &DriverArgs, |
317 | 10 | llvm::opt::ArgStringList &CC1Args) const { |
318 | 10 | const Driver &D = getDriver(); |
319 | | |
320 | 10 | SmallString<128> P(D.Dir + "/../"); |
321 | 10 | switch (getTriple().getArch()) { |
322 | 0 | default: |
323 | 0 | break; |
324 | 1 | case llvm::Triple::arm: |
325 | 1 | llvm::sys::path::append(P, "arm-nacl/include/c++/v1"); |
326 | 1 | addSystemInclude(DriverArgs, CC1Args, P.str()); |
327 | 1 | break; |
328 | 1 | case llvm::Triple::x86: |
329 | 1 | llvm::sys::path::append(P, "x86_64-nacl/include/c++/v1"); |
330 | 1 | addSystemInclude(DriverArgs, CC1Args, P.str()); |
331 | 1 | break; |
332 | 4 | case llvm::Triple::x86_64: |
333 | 4 | llvm::sys::path::append(P, "x86_64-nacl/include/c++/v1"); |
334 | 4 | addSystemInclude(DriverArgs, CC1Args, P.str()); |
335 | 4 | break; |
336 | 4 | case llvm::Triple::mipsel: |
337 | 4 | llvm::sys::path::append(P, "mipsel-nacl/include/c++/v1"); |
338 | 4 | addSystemInclude(DriverArgs, CC1Args, P.str()); |
339 | 4 | break; |
340 | 10 | } |
341 | 10 | } |
342 | | |
343 | | ToolChain::CXXStdlibType |
344 | 14 | NaClToolChain::GetCXXStdlibType(const ArgList &Args) const { |
345 | 14 | if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) { |
346 | 0 | StringRef Value = A->getValue(); |
347 | 0 | if (Value == "libc++") |
348 | 0 | return ToolChain::CST_Libcxx; |
349 | 0 | getDriver().Diag(clang::diag::err_drv_invalid_stdlib_name) |
350 | 0 | << A->getAsString(Args); |
351 | 0 | } |
352 | | |
353 | 14 | return ToolChain::CST_Libcxx; |
354 | 14 | } |
355 | | |
356 | | std::string |
357 | | NaClToolChain::ComputeEffectiveClangTriple(const ArgList &Args, |
358 | 41 | types::ID InputType) const { |
359 | 41 | llvm::Triple TheTriple(ComputeLLVMTriple(Args, InputType)); |
360 | 41 | if (TheTriple.getArch() == llvm::Triple::arm && |
361 | 41 | TheTriple.getEnvironment() == llvm::Triple::UnknownEnvironment15 ) |
362 | 3 | TheTriple.setEnvironment(llvm::Triple::GNUEABIHF); |
363 | 41 | return TheTriple.getTriple(); |
364 | 41 | } |
365 | | |
366 | 12 | Tool *NaClToolChain::buildLinker() const { |
367 | 12 | return new tools::nacltools::Linker(*this); |
368 | 12 | } |
369 | | |
370 | 11 | Tool *NaClToolChain::buildAssembler() const { |
371 | 11 | if (getTriple().getArch() == llvm::Triple::arm) |
372 | 5 | return new tools::nacltools::AssemblerARM(*this); |
373 | 6 | return new tools::gnutools::Assembler(*this); |
374 | 11 | } |