llvm-link.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //===- llvm-link.cpp - Low-level LLVM linker ------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This utility may be invoked in the following manner:
  11. // llvm-link a.bc b.bc c.bc -o x.bc
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/Linker/Linker.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/Bitcode/ReaderWriter.h"
  17. #include "llvm/IR/AutoUpgrade.h"
  18. #include "llvm/IR/DiagnosticInfo.h"
  19. #include "llvm/IR/DiagnosticPrinter.h"
  20. #include "llvm/IR/LLVMContext.h"
  21. #include "llvm/IR/Module.h"
  22. #include "llvm/IR/Verifier.h"
  23. #include "llvm/IRReader/IRReader.h"
  24. #include "llvm/Support/CommandLine.h"
  25. #include "llvm/Support/FileSystem.h"
  26. #include "llvm/Support/ManagedStatic.h"
  27. #include "llvm/Support/Path.h"
  28. #include "llvm/Support/PrettyStackTrace.h"
  29. #include "llvm/Support/Signals.h"
  30. #include "llvm/Support/SourceMgr.h"
  31. #include "llvm/Support/SystemUtils.h"
  32. #include "llvm/Support/ToolOutputFile.h"
  33. #include <memory>
  34. using namespace llvm;
  35. static cl::list<std::string>
  36. InputFilenames(cl::Positional, cl::OneOrMore,
  37. cl::desc("<input bitcode files>"));
  38. static cl::list<std::string> OverridingInputs(
  39. "override", cl::ZeroOrMore, cl::value_desc("filename"),
  40. cl::desc(
  41. "input bitcode file which can override previously defined symbol(s)"));
  42. static cl::opt<std::string>
  43. OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
  44. cl::value_desc("filename"));
  45. static cl::opt<bool>
  46. Force("f", cl::desc("Enable binary output on terminals"));
  47. static cl::opt<bool>
  48. OutputAssembly("S",
  49. cl::desc("Write output as LLVM assembly"), cl::Hidden);
  50. static cl::opt<bool>
  51. Verbose("v", cl::desc("Print information about actions taken"));
  52. static cl::opt<bool>
  53. DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
  54. static cl::opt<bool>
  55. SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"),
  56. cl::init(false));
  57. static cl::opt<bool> PreserveBitcodeUseListOrder(
  58. "preserve-bc-uselistorder",
  59. cl::desc("Preserve use-list order when writing LLVM bitcode."),
  60. cl::init(true), cl::Hidden);
  61. static cl::opt<bool> PreserveAssemblyUseListOrder(
  62. "preserve-ll-uselistorder",
  63. cl::desc("Preserve use-list order when writing LLVM assembly."),
  64. cl::init(false), cl::Hidden);
  65. // Read the specified bitcode file in and return it. This routine searches the
  66. // link path for the specified file to try to find it...
  67. //
  68. static std::unique_ptr<Module>
  69. loadFile(const char *argv0, const std::string &FN, LLVMContext &Context) {
  70. SMDiagnostic Err;
  71. if (Verbose) errs() << "Loading '" << FN << "'\n";
  72. std::unique_ptr<Module> Result = getLazyIRFileModule(FN, Err, Context);
  73. if (!Result)
  74. Err.print(argv0, errs());
  75. Result->materializeMetadata();
  76. UpgradeDebugInfo(*Result);
  77. return Result;
  78. }
  79. static void diagnosticHandler(const DiagnosticInfo &DI) {
  80. unsigned Severity = DI.getSeverity();
  81. switch (Severity) {
  82. case DS_Error:
  83. errs() << "ERROR: ";
  84. break;
  85. case DS_Warning:
  86. if (SuppressWarnings)
  87. return;
  88. errs() << "WARNING: ";
  89. break;
  90. case DS_Remark:
  91. case DS_Note:
  92. llvm_unreachable("Only expecting warnings and errors");
  93. }
  94. DiagnosticPrinterRawOStream DP(errs());
  95. DI.print(DP);
  96. errs() << '\n';
  97. }
  98. static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L,
  99. const cl::list<std::string> &Files,
  100. bool OverrideDuplicateSymbols) {
  101. for (const auto &File : Files) {
  102. std::unique_ptr<Module> M = loadFile(argv0, File, Context);
  103. if (!M.get()) {
  104. errs() << argv0 << ": error loading file '" << File << "'\n";
  105. return false;
  106. }
  107. if (verifyModule(*M, &errs())) {
  108. errs() << argv0 << ": " << File << ": error: input module is broken!\n";
  109. return false;
  110. }
  111. if (Verbose)
  112. errs() << "Linking in '" << File << "'\n";
  113. if (L.linkInModule(M.get(), OverrideDuplicateSymbols))
  114. return false;
  115. }
  116. return true;
  117. }
  118. // HLSL Change: changed calling convention to __cdecl
  119. int __cdecl main(int argc, char **argv) {
  120. // Print a stack trace if we signal out.
  121. sys::PrintStackTraceOnErrorSignal();
  122. PrettyStackTraceProgram X(argc, argv);
  123. LLVMContext &Context = getGlobalContext();
  124. llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
  125. cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
  126. auto Composite = make_unique<Module>("llvm-link", Context);
  127. Linker L(Composite.get(), diagnosticHandler);
  128. // First add all the regular input files
  129. if (!linkFiles(argv[0], Context, L, InputFilenames, false))
  130. return 1;
  131. // Next the -override ones.
  132. if (!linkFiles(argv[0], Context, L, OverridingInputs, true))
  133. return 1;
  134. if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite;
  135. std::error_code EC;
  136. tool_output_file Out(OutputFilename, EC, sys::fs::F_None);
  137. if (EC) {
  138. errs() << EC.message() << '\n';
  139. return 1;
  140. }
  141. if (verifyModule(*Composite, &errs())) {
  142. errs() << argv[0] << ": error: linked module is broken!\n";
  143. return 1;
  144. }
  145. if (Verbose) errs() << "Writing bitcode...\n";
  146. if (OutputAssembly) {
  147. Composite->print(Out.os(), nullptr, PreserveAssemblyUseListOrder);
  148. } else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
  149. WriteBitcodeToFile(Composite.get(), Out.os(), PreserveBitcodeUseListOrder);
  150. // Declare success.
  151. Out.keep();
  152. return 0;
  153. }