NaCl.cpp 13 KB

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