PageRenderTime 30ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/llvm/tools/lld/tools/lld/lld.cpp

https://bitbucket.org/freebsd/freebsd-base
C++ | 169 lines | 112 code | 16 blank | 41 comment | 33 complexity | 9a3884b1a10d99d082e4549bd0afc098 MD5 | raw file
  1. //===- tools/lld/lld.cpp - Linker Driver Dispatcher -----------------------===//
  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. //
  9. // This file contains the main function of the lld executable. The main
  10. // function is a thin wrapper which dispatches to the platform specific
  11. // driver.
  12. //
  13. // lld is a single executable that contains four different linkers for ELF,
  14. // COFF, WebAssembly and Mach-O. The main function dispatches according to
  15. // argv[0] (i.e. command name). The most common name for each target is shown
  16. // below:
  17. //
  18. // - ld.lld: ELF (Unix)
  19. // - ld64: Mach-O (macOS)
  20. // - lld-link: COFF (Windows)
  21. // - ld-wasm: WebAssembly
  22. //
  23. // lld can be invoked as "lld" along with "-flavor" option. This is for
  24. // backward compatibility and not recommended.
  25. //
  26. //===----------------------------------------------------------------------===//
  27. #include "lld/Common/Driver.h"
  28. #include "lld/Common/Memory.h"
  29. #include "llvm/ADT/STLExtras.h"
  30. #include "llvm/ADT/SmallVector.h"
  31. #include "llvm/ADT/StringSwitch.h"
  32. #include "llvm/ADT/Triple.h"
  33. #include "llvm/ADT/Twine.h"
  34. #include "llvm/Support/CommandLine.h"
  35. #include "llvm/Support/InitLLVM.h"
  36. #include "llvm/Support/Path.h"
  37. #include <cstdlib>
  38. using namespace lld;
  39. using namespace llvm;
  40. using namespace llvm::sys;
  41. enum Flavor {
  42. Invalid,
  43. Gnu, // -flavor gnu
  44. WinLink, // -flavor link
  45. Darwin, // -flavor darwin
  46. Wasm, // -flavor wasm
  47. };
  48. LLVM_ATTRIBUTE_NORETURN static void die(const Twine &s) {
  49. errs() << s << "\n";
  50. exit(1);
  51. }
  52. static Flavor getFlavor(StringRef s) {
  53. return StringSwitch<Flavor>(s)
  54. .CasesLower("ld", "ld.lld", "gnu", Gnu)
  55. .CasesLower("wasm", "ld-wasm", Wasm)
  56. .CaseLower("link", WinLink)
  57. .CasesLower("ld64", "ld64.lld", "darwin", Darwin)
  58. .Default(Invalid);
  59. }
  60. static cl::TokenizerCallback getDefaultQuotingStyle() {
  61. if (Triple(sys::getProcessTriple()).getOS() == Triple::Win32)
  62. return cl::TokenizeWindowsCommandLine;
  63. return cl::TokenizeGNUCommandLine;
  64. }
  65. static bool isPETargetName(StringRef s) {
  66. return s == "i386pe" || s == "i386pep" || s == "thumb2pe" || s == "arm64pe";
  67. }
  68. static bool isPETarget(std::vector<const char *> &v) {
  69. for (auto it = v.begin(); it + 1 != v.end(); ++it) {
  70. if (StringRef(*it) != "-m")
  71. continue;
  72. return isPETargetName(*(it + 1));
  73. }
  74. // Expand response files (arguments in the form of @<filename>)
  75. // to allow detecting the -m argument from arguments in them.
  76. SmallVector<const char *, 256> expandedArgs(v.data(), v.data() + v.size());
  77. cl::ExpandResponseFiles(saver, getDefaultQuotingStyle(), expandedArgs);
  78. for (auto it = expandedArgs.begin(); it + 1 != expandedArgs.end(); ++it) {
  79. if (StringRef(*it) != "-m")
  80. continue;
  81. return isPETargetName(*(it + 1));
  82. }
  83. return false;
  84. }
  85. static Flavor parseProgname(StringRef progname) {
  86. #if __APPLE__
  87. // Use Darwin driver for "ld" on Darwin.
  88. if (progname == "ld")
  89. return Darwin;
  90. #endif
  91. #if LLVM_ON_UNIX
  92. // Use GNU driver for "ld" on other Unix-like system.
  93. if (progname == "ld")
  94. return Gnu;
  95. #endif
  96. // Progname may be something like "lld-gnu". Parse it.
  97. SmallVector<StringRef, 3> v;
  98. progname.split(v, "-");
  99. for (StringRef s : v)
  100. if (Flavor f = getFlavor(s))
  101. return f;
  102. return Invalid;
  103. }
  104. static Flavor parseFlavor(std::vector<const char *> &v) {
  105. // Parse -flavor option.
  106. if (v.size() > 1 && v[1] == StringRef("-flavor")) {
  107. if (v.size() <= 2)
  108. die("missing arg value for '-flavor'");
  109. Flavor f = getFlavor(v[2]);
  110. if (f == Invalid)
  111. die("Unknown flavor: " + StringRef(v[2]));
  112. v.erase(v.begin() + 1, v.begin() + 3);
  113. return f;
  114. }
  115. // Deduct the flavor from argv[0].
  116. StringRef arg0 = path::filename(v[0]);
  117. if (arg0.endswith_lower(".exe"))
  118. arg0 = arg0.drop_back(4);
  119. return parseProgname(arg0);
  120. }
  121. // If this function returns true, lld calls _exit() so that it quickly
  122. // exits without invoking destructors of globally allocated objects.
  123. //
  124. // We don't want to do that if we are running tests though, because
  125. // doing that breaks leak sanitizer. So, lit sets this environment variable,
  126. // and we use it to detect whether we are running tests or not.
  127. static bool canExitEarly() { return StringRef(getenv("LLD_IN_TEST")) != "1"; }
  128. /// Universal linker main(). This linker emulates the gnu, darwin, or
  129. /// windows linker based on the argv[0] or -flavor option.
  130. int main(int argc, const char **argv) {
  131. InitLLVM x(argc, argv);
  132. std::vector<const char *> args(argv, argv + argc);
  133. #ifdef __FreeBSD__
  134. return !elf::link(args, true);
  135. #else
  136. switch (parseFlavor(args)) {
  137. case Gnu:
  138. if (isPETarget(args))
  139. return !mingw::link(args);
  140. return !elf::link(args, canExitEarly());
  141. case WinLink:
  142. return !coff::link(args, canExitEarly());
  143. case Darwin:
  144. return !mach_o::link(args, canExitEarly());
  145. case Wasm:
  146. return !wasm::link(args, canExitEarly());
  147. default:
  148. die("lld is a generic driver.\n"
  149. "Invoke ld.lld (Unix), ld64.lld (macOS), lld-link (Windows), wasm-ld"
  150. " (WebAssembly) instead");
  151. }
  152. #endif
  153. }