Solaris.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //===--- Solaris.cpp - Solaris 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 "Solaris.h"
  9. #include "CommonArgs.h"
  10. #include "clang/Basic/LangStandard.h"
  11. #include "clang/Config/config.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/FileSystem.h"
  18. #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;
  24. void solaris::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
  25. const InputInfo &Output,
  26. const InputInfoList &Inputs,
  27. const ArgList &Args,
  28. const char *LinkingOutput) const {
  29. claimNoWarnArgs(Args);
  30. ArgStringList CmdArgs;
  31. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
  32. CmdArgs.push_back("-o");
  33. CmdArgs.push_back(Output.getFilename());
  34. for (const auto &II : Inputs)
  35. CmdArgs.push_back(II.getFilename());
  36. const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
  37. C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
  38. Exec, CmdArgs, Inputs, Output));
  39. }
  40. void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
  41. const InputInfo &Output,
  42. const InputInfoList &Inputs,
  43. const ArgList &Args,
  44. const char *LinkingOutput) const {
  45. ArgStringList CmdArgs;
  46. // Demangle C++ names in errors
  47. CmdArgs.push_back("-C");
  48. if (!Args.hasArg(options::OPT_nostdlib, options::OPT_shared)) {
  49. CmdArgs.push_back("-e");
  50. CmdArgs.push_back("_start");
  51. }
  52. if (Args.hasArg(options::OPT_static)) {
  53. CmdArgs.push_back("-Bstatic");
  54. CmdArgs.push_back("-dn");
  55. } else {
  56. CmdArgs.push_back("-Bdynamic");
  57. if (Args.hasArg(options::OPT_shared)) {
  58. CmdArgs.push_back("-shared");
  59. }
  60. // libpthread has been folded into libc since Solaris 10, no need to do
  61. // anything for pthreads. Claim argument to avoid warning.
  62. Args.ClaimAllArgs(options::OPT_pthread);
  63. Args.ClaimAllArgs(options::OPT_pthreads);
  64. }
  65. if (Output.isFilename()) {
  66. CmdArgs.push_back("-o");
  67. CmdArgs.push_back(Output.getFilename());
  68. } else {
  69. assert(Output.isNothing() && "Invalid output.");
  70. }
  71. if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
  72. options::OPT_r)) {
  73. if (!Args.hasArg(options::OPT_shared))
  74. CmdArgs.push_back(
  75. Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
  76. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
  77. const Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi);
  78. bool HaveAnsi = false;
  79. const LangStandard *LangStd = nullptr;
  80. if (Std) {
  81. HaveAnsi = Std->getOption().matches(options::OPT_ansi);
  82. if (!HaveAnsi)
  83. LangStd = LangStandard::getLangStandardForName(Std->getValue());
  84. }
  85. const char *values_X = "values-Xa.o";
  86. // Use values-Xc.o for -ansi, -std=c*, -std=iso9899:199409.
  87. if (HaveAnsi || (LangStd && !LangStd->isGNUMode()))
  88. values_X = "values-Xc.o";
  89. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(values_X)));
  90. const char *values_xpg = "values-xpg6.o";
  91. // Use values-xpg4.o for -std=c90, -std=gnu90, -std=iso9899:199409.
  92. if (LangStd && LangStd->getLanguage() == Language::C && !LangStd->isC99())
  93. values_xpg = "values-xpg4.o";
  94. CmdArgs.push_back(
  95. Args.MakeArgString(getToolChain().GetFilePath(values_xpg)));
  96. CmdArgs.push_back(
  97. Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
  98. }
  99. getToolChain().AddFilePathLibArgs(Args, CmdArgs);
  100. Args.AddAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group,
  101. options::OPT_e, options::OPT_r});
  102. bool NeedsSanitizerDeps = addSanitizerRuntimes(getToolChain(), Args, CmdArgs);
  103. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
  104. if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
  105. options::OPT_r)) {
  106. if (getToolChain().ShouldLinkCXXStdlib(Args))
  107. getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
  108. if (Args.hasArg(options::OPT_fstack_protector) ||
  109. Args.hasArg(options::OPT_fstack_protector_strong) ||
  110. Args.hasArg(options::OPT_fstack_protector_all)) {
  111. // Explicitly link ssp libraries, not folded into Solaris libc.
  112. CmdArgs.push_back("-lssp_nonshared");
  113. CmdArgs.push_back("-lssp");
  114. }
  115. // LLVM support for atomics on 32-bit SPARC V8+ is incomplete, so
  116. // forcibly link with libatomic as a workaround.
  117. if (getToolChain().getTriple().getArch() == llvm::Triple::sparc) {
  118. CmdArgs.push_back(getAsNeededOption(getToolChain(), true));
  119. CmdArgs.push_back("-latomic");
  120. CmdArgs.push_back(getAsNeededOption(getToolChain(), false));
  121. }
  122. CmdArgs.push_back("-lgcc_s");
  123. CmdArgs.push_back("-lc");
  124. if (!Args.hasArg(options::OPT_shared)) {
  125. CmdArgs.push_back("-lgcc");
  126. CmdArgs.push_back("-lm");
  127. }
  128. if (NeedsSanitizerDeps)
  129. linkSanitizerRuntimeDeps(getToolChain(), CmdArgs);
  130. }
  131. if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
  132. options::OPT_r)) {
  133. CmdArgs.push_back(
  134. Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
  135. CmdArgs.push_back(
  136. Args.MakeArgString(getToolChain().GetFilePath("crtn.o")));
  137. }
  138. getToolChain().addProfileRTLibs(Args, CmdArgs);
  139. const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
  140. C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
  141. Exec, CmdArgs, Inputs, Output));
  142. }
  143. static StringRef getSolarisLibSuffix(const llvm::Triple &Triple) {
  144. switch (Triple.getArch()) {
  145. case llvm::Triple::x86:
  146. case llvm::Triple::sparc:
  147. break;
  148. case llvm::Triple::x86_64:
  149. return "/amd64";
  150. case llvm::Triple::sparcv9:
  151. return "/sparcv9";
  152. default:
  153. llvm_unreachable("Unsupported architecture");
  154. }
  155. return "";
  156. }
  157. /// Solaris - Solaris tool chain which can call as(1) and ld(1) directly.
  158. Solaris::Solaris(const Driver &D, const llvm::Triple &Triple,
  159. const ArgList &Args)
  160. : Generic_ELF(D, Triple, Args) {
  161. GCCInstallation.init(Triple, Args);
  162. StringRef LibSuffix = getSolarisLibSuffix(Triple);
  163. path_list &Paths = getFilePaths();
  164. if (GCCInstallation.isValid()) {
  165. // On Solaris gcc uses both an architecture-specific path with triple in it
  166. // as well as a more generic lib path (+arch suffix).
  167. addPathIfExists(D,
  168. GCCInstallation.getInstallPath() +
  169. GCCInstallation.getMultilib().gccSuffix(),
  170. Paths);
  171. addPathIfExists(D, GCCInstallation.getParentLibPath() + LibSuffix, Paths);
  172. }
  173. // If we are currently running Clang inside of the requested system root,
  174. // add its parent library path to those searched.
  175. if (StringRef(D.Dir).startswith(D.SysRoot))
  176. addPathIfExists(D, D.Dir + "/../lib", Paths);
  177. addPathIfExists(D, D.SysRoot + "/usr/lib" + LibSuffix, Paths);
  178. }
  179. SanitizerMask Solaris::getSupportedSanitizers() const {
  180. const bool IsX86 = getTriple().getArch() == llvm::Triple::x86;
  181. const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
  182. SanitizerMask Res = ToolChain::getSupportedSanitizers();
  183. // FIXME: Omit X86_64 until 64-bit support is figured out.
  184. if (IsX86) {
  185. Res |= SanitizerKind::Address;
  186. Res |= SanitizerKind::PointerCompare;
  187. Res |= SanitizerKind::PointerSubtract;
  188. }
  189. if (IsX86 || IsX86_64)
  190. Res |= SanitizerKind::Function;
  191. Res |= SanitizerKind::Vptr;
  192. return Res;
  193. }
  194. Tool *Solaris::buildAssembler() const {
  195. return new tools::solaris::Assembler(*this);
  196. }
  197. Tool *Solaris::buildLinker() const { return new tools::solaris::Linker(*this); }
  198. void Solaris::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
  199. ArgStringList &CC1Args) const {
  200. const Driver &D = getDriver();
  201. if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
  202. return;
  203. if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
  204. addSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/local/include");
  205. if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
  206. SmallString<128> P(D.ResourceDir);
  207. llvm::sys::path::append(P, "include");
  208. addSystemInclude(DriverArgs, CC1Args, P);
  209. }
  210. if (DriverArgs.hasArg(options::OPT_nostdlibinc))
  211. return;
  212. // Check for configure-time C include directories.
  213. StringRef CIncludeDirs(C_INCLUDE_DIRS);
  214. if (CIncludeDirs != "") {
  215. SmallVector<StringRef, 5> dirs;
  216. CIncludeDirs.split(dirs, ":");
  217. for (StringRef dir : dirs) {
  218. StringRef Prefix =
  219. llvm::sys::path::is_absolute(dir) ? "" : StringRef(D.SysRoot);
  220. addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
  221. }
  222. return;
  223. }
  224. // Add include directories specific to the selected multilib set and multilib.
  225. if (GCCInstallation.isValid()) {
  226. const MultilibSet::IncludeDirsFunc &Callback =
  227. Multilibs.includeDirsCallback();
  228. if (Callback) {
  229. for (const auto &Path : Callback(GCCInstallation.getMultilib()))
  230. addExternCSystemIncludeIfExists(
  231. DriverArgs, CC1Args, GCCInstallation.getInstallPath() + Path);
  232. }
  233. }
  234. addExternCSystemInclude(DriverArgs, CC1Args, D.SysRoot + "/usr/include");
  235. }
  236. void Solaris::addLibStdCxxIncludePaths(
  237. const llvm::opt::ArgList &DriverArgs,
  238. llvm::opt::ArgStringList &CC1Args) const {
  239. // We need a detected GCC installation on Solaris (similar to Linux)
  240. // to provide libstdc++'s headers.
  241. if (!GCCInstallation.isValid())
  242. return;
  243. // By default, look for the C++ headers in an include directory adjacent to
  244. // the lib directory of the GCC installation.
  245. // On Solaris this usually looks like /usr/gcc/X.Y/include/c++/X.Y.Z
  246. StringRef LibDir = GCCInstallation.getParentLibPath();
  247. StringRef TripleStr = GCCInstallation.getTriple().str();
  248. const Multilib &Multilib = GCCInstallation.getMultilib();
  249. const GCCVersion &Version = GCCInstallation.getVersion();
  250. // The primary search for libstdc++ supports multiarch variants.
  251. addLibStdCXXIncludePaths(LibDir.str() + "/../include/c++/" + Version.Text,
  252. TripleStr, Multilib.includeSuffix(), DriverArgs,
  253. CC1Args);
  254. }