NaCl.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 "InputInfo.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/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, Exec, CmdArgs, Inputs));
  168. }
  169. /// NaCl Toolchain
  170. NaClToolChain::NaClToolChain(const Driver &D, const llvm::Triple &Triple,
  171. const ArgList &Args)
  172. : Generic_ELF(D, Triple, Args) {
  173. // Remove paths added by Generic_GCC. NaCl Toolchain cannot use the
  174. // default paths, and must instead only use the paths provided
  175. // with this toolchain based on architecture.
  176. path_list &file_paths = getFilePaths();
  177. path_list &prog_paths = getProgramPaths();
  178. file_paths.clear();
  179. prog_paths.clear();
  180. // Path for library files (libc.a, ...)
  181. std::string FilePath(getDriver().Dir + "/../");
  182. // Path for tools (clang, ld, etc..)
  183. std::string ProgPath(getDriver().Dir + "/../");
  184. // Path for toolchain libraries (libgcc.a, ...)
  185. std::string ToolPath(getDriver().ResourceDir + "/lib/");
  186. switch (Triple.getArch()) {
  187. case llvm::Triple::x86:
  188. file_paths.push_back(FilePath + "x86_64-nacl/lib32");
  189. file_paths.push_back(FilePath + "i686-nacl/usr/lib");
  190. prog_paths.push_back(ProgPath + "x86_64-nacl/bin");
  191. file_paths.push_back(ToolPath + "i686-nacl");
  192. break;
  193. case llvm::Triple::x86_64:
  194. file_paths.push_back(FilePath + "x86_64-nacl/lib");
  195. file_paths.push_back(FilePath + "x86_64-nacl/usr/lib");
  196. prog_paths.push_back(ProgPath + "x86_64-nacl/bin");
  197. file_paths.push_back(ToolPath + "x86_64-nacl");
  198. break;
  199. case llvm::Triple::arm:
  200. file_paths.push_back(FilePath + "arm-nacl/lib");
  201. file_paths.push_back(FilePath + "arm-nacl/usr/lib");
  202. prog_paths.push_back(ProgPath + "arm-nacl/bin");
  203. file_paths.push_back(ToolPath + "arm-nacl");
  204. break;
  205. case llvm::Triple::mipsel:
  206. file_paths.push_back(FilePath + "mipsel-nacl/lib");
  207. file_paths.push_back(FilePath + "mipsel-nacl/usr/lib");
  208. prog_paths.push_back(ProgPath + "bin");
  209. file_paths.push_back(ToolPath + "mipsel-nacl");
  210. break;
  211. default:
  212. break;
  213. }
  214. NaClArmMacrosPath = GetFilePath("nacl-arm-macros.s");
  215. }
  216. void NaClToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
  217. ArgStringList &CC1Args) const {
  218. const Driver &D = getDriver();
  219. if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
  220. return;
  221. if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
  222. SmallString<128> P(D.ResourceDir);
  223. llvm::sys::path::append(P, "include");
  224. addSystemInclude(DriverArgs, CC1Args, P.str());
  225. }
  226. if (DriverArgs.hasArg(options::OPT_nostdlibinc))
  227. return;
  228. SmallString<128> P(D.Dir + "/../");
  229. switch (getTriple().getArch()) {
  230. case llvm::Triple::x86:
  231. // x86 is special because multilib style uses x86_64-nacl/include for libc
  232. // headers but the SDK wants i686-nacl/usr/include. The other architectures
  233. // have the same substring.
  234. llvm::sys::path::append(P, "i686-nacl/usr/include");
  235. addSystemInclude(DriverArgs, CC1Args, P.str());
  236. llvm::sys::path::remove_filename(P);
  237. llvm::sys::path::remove_filename(P);
  238. llvm::sys::path::remove_filename(P);
  239. llvm::sys::path::append(P, "x86_64-nacl/include");
  240. addSystemInclude(DriverArgs, CC1Args, P.str());
  241. return;
  242. case llvm::Triple::arm:
  243. llvm::sys::path::append(P, "arm-nacl/usr/include");
  244. break;
  245. case llvm::Triple::x86_64:
  246. llvm::sys::path::append(P, "x86_64-nacl/usr/include");
  247. break;
  248. case llvm::Triple::mipsel:
  249. llvm::sys::path::append(P, "mipsel-nacl/usr/include");
  250. break;
  251. default:
  252. return;
  253. }
  254. addSystemInclude(DriverArgs, CC1Args, P.str());
  255. llvm::sys::path::remove_filename(P);
  256. llvm::sys::path::remove_filename(P);
  257. llvm::sys::path::append(P, "include");
  258. addSystemInclude(DriverArgs, CC1Args, P.str());
  259. }
  260. void NaClToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
  261. ArgStringList &CmdArgs) const {
  262. // Check for -stdlib= flags. We only support libc++ but this consumes the arg
  263. // if the value is libc++, and emits an error for other values.
  264. GetCXXStdlibType(Args);
  265. CmdArgs.push_back("-lc++");
  266. }
  267. void NaClToolChain::addLibCxxIncludePaths(
  268. const llvm::opt::ArgList &DriverArgs,
  269. llvm::opt::ArgStringList &CC1Args) const {
  270. const Driver &D = getDriver();
  271. SmallString<128> P(D.Dir + "/../");
  272. switch (getTriple().getArch()) {
  273. default:
  274. break;
  275. case llvm::Triple::arm:
  276. llvm::sys::path::append(P, "arm-nacl/include/c++/v1");
  277. addSystemInclude(DriverArgs, CC1Args, P.str());
  278. break;
  279. case llvm::Triple::x86:
  280. llvm::sys::path::append(P, "x86_64-nacl/include/c++/v1");
  281. addSystemInclude(DriverArgs, CC1Args, P.str());
  282. break;
  283. case llvm::Triple::x86_64:
  284. llvm::sys::path::append(P, "x86_64-nacl/include/c++/v1");
  285. addSystemInclude(DriverArgs, CC1Args, P.str());
  286. break;
  287. case llvm::Triple::mipsel:
  288. llvm::sys::path::append(P, "mipsel-nacl/include/c++/v1");
  289. addSystemInclude(DriverArgs, CC1Args, P.str());
  290. break;
  291. }
  292. }
  293. ToolChain::CXXStdlibType
  294. NaClToolChain::GetCXXStdlibType(const ArgList &Args) const {
  295. if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
  296. StringRef Value = A->getValue();
  297. if (Value == "libc++")
  298. return ToolChain::CST_Libcxx;
  299. getDriver().Diag(clang::diag::err_drv_invalid_stdlib_name)
  300. << A->getAsString(Args);
  301. }
  302. return ToolChain::CST_Libcxx;
  303. }
  304. std::string
  305. NaClToolChain::ComputeEffectiveClangTriple(const ArgList &Args,
  306. types::ID InputType) const {
  307. llvm::Triple TheTriple(ComputeLLVMTriple(Args, InputType));
  308. if (TheTriple.getArch() == llvm::Triple::arm &&
  309. TheTriple.getEnvironment() == llvm::Triple::UnknownEnvironment)
  310. TheTriple.setEnvironment(llvm::Triple::GNUEABIHF);
  311. return TheTriple.getTriple();
  312. }
  313. Tool *NaClToolChain::buildLinker() const {
  314. return new tools::nacltools::Linker(*this);
  315. }
  316. Tool *NaClToolChain::buildAssembler() const {
  317. if (getTriple().getArch() == llvm::Triple::arm)
  318. return new tools::nacltools::AssemblerARM(*this);
  319. return new tools::gnutools::Assembler(*this);
  320. }