CloudABI.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //===--- CloudABI.cpp - CloudABI 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 "CloudABI.h"
  9. #include "CommonArgs.h"
  10. #include "clang/Driver/Compilation.h"
  11. #include "clang/Driver/Driver.h"
  12. #include "clang/Driver/InputInfo.h"
  13. #include "clang/Driver/Options.h"
  14. #include "llvm/ADT/SmallString.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. void cloudabi::Linker::ConstructJob(Compilation &C, const JobAction &JA,
  23. const InputInfo &Output,
  24. const InputInfoList &Inputs,
  25. const ArgList &Args,
  26. const char *LinkingOutput) const {
  27. const ToolChain &ToolChain = getToolChain();
  28. const Driver &D = ToolChain.getDriver();
  29. ArgStringList CmdArgs;
  30. // Silence warning for "clang -g foo.o -o foo"
  31. Args.ClaimAllArgs(options::OPT_g_Group);
  32. // and "clang -emit-llvm foo.o -o foo"
  33. Args.ClaimAllArgs(options::OPT_emit_llvm);
  34. // and for "clang -w foo.o -o foo". Other warning options are already
  35. // handled somewhere else.
  36. Args.ClaimAllArgs(options::OPT_w);
  37. if (!D.SysRoot.empty())
  38. CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
  39. // CloudABI only supports static linkage.
  40. CmdArgs.push_back("-Bstatic");
  41. CmdArgs.push_back("--no-dynamic-linker");
  42. // Provide PIE linker flags in case PIE is default for the architecture.
  43. if (ToolChain.isPIEDefault(Args)) {
  44. CmdArgs.push_back("-pie");
  45. CmdArgs.push_back("-zrelro");
  46. }
  47. CmdArgs.push_back("--eh-frame-hdr");
  48. CmdArgs.push_back("--gc-sections");
  49. if (Output.isFilename()) {
  50. CmdArgs.push_back("-o");
  51. CmdArgs.push_back(Output.getFilename());
  52. } else {
  53. assert(Output.isNothing() && "Invalid output.");
  54. }
  55. if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
  56. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crt0.o")));
  57. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtbegin.o")));
  58. }
  59. Args.AddAllArgs(CmdArgs, options::OPT_L);
  60. ToolChain.AddFilePathLibArgs(Args, CmdArgs);
  61. Args.AddAllArgs(CmdArgs,
  62. {options::OPT_T_Group, options::OPT_e, options::OPT_s,
  63. options::OPT_t, options::OPT_Z_Flag, options::OPT_r});
  64. if (D.isUsingLTO()) {
  65. assert(!Inputs.empty() && "Must have at least one input.");
  66. addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs[0],
  67. D.getLTOMode() == LTOK_Thin);
  68. }
  69. AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
  70. if (ToolChain.ShouldLinkCXXStdlib(Args))
  71. ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
  72. if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
  73. CmdArgs.push_back("-lc");
  74. CmdArgs.push_back("-lcompiler_rt");
  75. }
  76. if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
  77. CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
  78. const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
  79. C.addCommand(std::make_unique<Command>(JA, *this,
  80. ResponseFileSupport::AtFileCurCP(),
  81. Exec, CmdArgs, Inputs, Output));
  82. }
  83. // CloudABI - CloudABI tool chain which can call ld(1) directly.
  84. CloudABI::CloudABI(const Driver &D, const llvm::Triple &Triple,
  85. const ArgList &Args)
  86. : Generic_ELF(D, Triple, Args) {
  87. SmallString<128> P(getDriver().Dir);
  88. llvm::sys::path::append(P, "..", getTriple().str(), "lib");
  89. getFilePaths().push_back(std::string(P.str()));
  90. }
  91. void CloudABI::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
  92. llvm::opt::ArgStringList &CC1Args) const {
  93. SmallString<128> P(getDriver().Dir);
  94. llvm::sys::path::append(P, "..", getTriple().str(), "include/c++/v1");
  95. addSystemInclude(DriverArgs, CC1Args, P.str());
  96. }
  97. void CloudABI::AddCXXStdlibLibArgs(const ArgList &Args,
  98. ArgStringList &CmdArgs) const {
  99. CmdArgs.push_back("-lc++");
  100. CmdArgs.push_back("-lc++abi");
  101. CmdArgs.push_back("-lunwind");
  102. }
  103. Tool *CloudABI::buildLinker() const {
  104. return new tools::cloudabi::Linker(*this);
  105. }
  106. bool CloudABI::isPIEDefault(const llvm::opt::ArgList &Args) const {
  107. // Only enable PIE on architectures that support PC-relative
  108. // addressing. PC-relative addressing is required, as the process
  109. // startup code must be able to relocate itself.
  110. switch (getTriple().getArch()) {
  111. case llvm::Triple::aarch64:
  112. case llvm::Triple::x86_64:
  113. return true;
  114. default:
  115. return false;
  116. }
  117. }
  118. SanitizerMask CloudABI::getSupportedSanitizers() const {
  119. SanitizerMask Res = ToolChain::getSupportedSanitizers();
  120. Res |= SanitizerKind::SafeStack;
  121. return Res;
  122. }
  123. SanitizerMask CloudABI::getDefaultSanitizers() const {
  124. return SanitizerKind::SafeStack;
  125. }