DragonFly.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //===--- DragonFly.cpp - DragonFly 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 "DragonFly.h"
  9. #include "CommonArgs.h"
  10. #include "clang/Driver/Compilation.h"
  11. #include "clang/Driver/Driver.h"
  12. #include "clang/Driver/Options.h"
  13. #include "llvm/Option/ArgList.h"
  14. using namespace clang::driver;
  15. using namespace clang::driver::tools;
  16. using namespace clang::driver::toolchains;
  17. using namespace clang;
  18. using namespace llvm::opt;
  19. /// DragonFly Tools
  20. // For now, DragonFly Assemble does just about the same as for
  21. // FreeBSD, but this may change soon.
  22. void dragonfly::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
  23. const InputInfo &Output,
  24. const InputInfoList &Inputs,
  25. const ArgList &Args,
  26. const char *LinkingOutput) const {
  27. claimNoWarnArgs(Args);
  28. ArgStringList CmdArgs;
  29. // When building 32-bit code on DragonFly/pc64, we have to explicitly
  30. // instruct as in the base system to assemble 32-bit code.
  31. if (getToolChain().getArch() == llvm::Triple::x86)
  32. CmdArgs.push_back("--32");
  33. Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
  34. CmdArgs.push_back("-o");
  35. CmdArgs.push_back(Output.getFilename());
  36. for (const auto &II : Inputs)
  37. CmdArgs.push_back(II.getFilename());
  38. const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
  39. C.addCommand(std::make_unique<Command>(JA, *this,
  40. ResponseFileSupport::AtFileCurCP(),
  41. Exec, CmdArgs, Inputs, Output));
  42. }
  43. void dragonfly::Linker::ConstructJob(Compilation &C, const JobAction &JA,
  44. const InputInfo &Output,
  45. const InputInfoList &Inputs,
  46. const ArgList &Args,
  47. const char *LinkingOutput) const {
  48. const Driver &D = getToolChain().getDriver();
  49. ArgStringList CmdArgs;
  50. if (!D.SysRoot.empty())
  51. CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
  52. CmdArgs.push_back("--eh-frame-hdr");
  53. if (Args.hasArg(options::OPT_static)) {
  54. CmdArgs.push_back("-Bstatic");
  55. } else {
  56. if (Args.hasArg(options::OPT_rdynamic))
  57. CmdArgs.push_back("-export-dynamic");
  58. if (Args.hasArg(options::OPT_shared))
  59. CmdArgs.push_back("-Bshareable");
  60. else if (!Args.hasArg(options::OPT_r)) {
  61. CmdArgs.push_back("-dynamic-linker");
  62. CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
  63. }
  64. CmdArgs.push_back("--hash-style=gnu");
  65. CmdArgs.push_back("--enable-new-dtags");
  66. }
  67. // When building 32-bit code on DragonFly/pc64, we have to explicitly
  68. // instruct ld in the base system to link 32-bit code.
  69. if (getToolChain().getArch() == llvm::Triple::x86) {
  70. CmdArgs.push_back("-m");
  71. CmdArgs.push_back("elf_i386");
  72. }
  73. if (Output.isFilename()) {
  74. CmdArgs.push_back("-o");
  75. CmdArgs.push_back(Output.getFilename());
  76. } else {
  77. assert(Output.isNothing() && "Invalid output.");
  78. }
  79. if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
  80. options::OPT_r)) {
  81. if (!Args.hasArg(options::OPT_shared)) {
  82. if (Args.hasArg(options::OPT_pg))
  83. CmdArgs.push_back(
  84. Args.MakeArgString(getToolChain().GetFilePath("gcrt1.o")));
  85. else {
  86. if (Args.hasArg(options::OPT_pie))
  87. CmdArgs.push_back(
  88. Args.MakeArgString(getToolChain().GetFilePath("Scrt1.o")));
  89. else
  90. CmdArgs.push_back(
  91. Args.MakeArgString(getToolChain().GetFilePath("crt1.o")));
  92. }
  93. }
  94. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crti.o")));
  95. if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
  96. CmdArgs.push_back(
  97. Args.MakeArgString(getToolChain().GetFilePath("crtbeginS.o")));
  98. else
  99. CmdArgs.push_back(
  100. Args.MakeArgString(getToolChain().GetFilePath("crtbegin.o")));
  101. }
  102. Args.AddAllArgs(CmdArgs,
  103. {options::OPT_L, options::OPT_T_Group, options::OPT_e});
  104. AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
  105. if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
  106. options::OPT_r)) {
  107. CmdArgs.push_back("-L/usr/lib/gcc80");
  108. if (!Args.hasArg(options::OPT_static)) {
  109. CmdArgs.push_back("-rpath");
  110. CmdArgs.push_back("/usr/lib/gcc80");
  111. }
  112. if (D.CCCIsCXX()) {
  113. if (getToolChain().ShouldLinkCXXStdlib(Args))
  114. getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
  115. CmdArgs.push_back("-lm");
  116. }
  117. if (Args.hasArg(options::OPT_pthread))
  118. CmdArgs.push_back("-lpthread");
  119. if (!Args.hasArg(options::OPT_nolibc)) {
  120. CmdArgs.push_back("-lc");
  121. }
  122. if (Args.hasArg(options::OPT_static) ||
  123. Args.hasArg(options::OPT_static_libgcc)) {
  124. CmdArgs.push_back("-lgcc");
  125. CmdArgs.push_back("-lgcc_eh");
  126. } else {
  127. if (Args.hasArg(options::OPT_shared_libgcc)) {
  128. CmdArgs.push_back("-lgcc_pic");
  129. if (!Args.hasArg(options::OPT_shared))
  130. CmdArgs.push_back("-lgcc");
  131. } else {
  132. CmdArgs.push_back("-lgcc");
  133. CmdArgs.push_back("--as-needed");
  134. CmdArgs.push_back("-lgcc_pic");
  135. CmdArgs.push_back("--no-as-needed");
  136. }
  137. }
  138. }
  139. if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
  140. options::OPT_r)) {
  141. if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
  142. CmdArgs.push_back(
  143. Args.MakeArgString(getToolChain().GetFilePath("crtendS.o")));
  144. else
  145. CmdArgs.push_back(
  146. Args.MakeArgString(getToolChain().GetFilePath("crtend.o")));
  147. CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath("crtn.o")));
  148. }
  149. getToolChain().addProfileRTLibs(Args, CmdArgs);
  150. const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
  151. C.addCommand(std::make_unique<Command>(JA, *this,
  152. ResponseFileSupport::AtFileCurCP(),
  153. Exec, CmdArgs, Inputs, Output));
  154. }
  155. /// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
  156. DragonFly::DragonFly(const Driver &D, const llvm::Triple &Triple,
  157. const ArgList &Args)
  158. : Generic_ELF(D, Triple, Args) {
  159. // Path mangling to find libexec
  160. getProgramPaths().push_back(getDriver().getInstalledDir());
  161. if (getDriver().getInstalledDir() != getDriver().Dir)
  162. getProgramPaths().push_back(getDriver().Dir);
  163. getFilePaths().push_back(getDriver().Dir + "/../lib");
  164. getFilePaths().push_back("/usr/lib");
  165. getFilePaths().push_back("/usr/lib/gcc80");
  166. }
  167. Tool *DragonFly::buildAssembler() const {
  168. return new tools::dragonfly::Assembler(*this);
  169. }
  170. Tool *DragonFly::buildLinker() const {
  171. return new tools::dragonfly::Linker(*this);
  172. }