Revert "Support for ThinLTO function importing and symbol linking."
[oota-llvm.git] / tools / llvm-link / llvm-link.cpp
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
15 #include "llvm/Linker/Linker.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/Bitcode/ReaderWriter.h"
18 #include "llvm/IR/AutoUpgrade.h"
19 #include "llvm/IR/DiagnosticInfo.h"
20 #include "llvm/IR/DiagnosticPrinter.h"
21 #include "llvm/IR/LLVMContext.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/Verifier.h"
24 #include "llvm/IRReader/IRReader.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/FileSystem.h"
27 #include "llvm/Support/ManagedStatic.h"
28 #include "llvm/Support/Path.h"
29 #include "llvm/Support/PrettyStackTrace.h"
30 #include "llvm/Support/Signals.h"
31 #include "llvm/Support/SourceMgr.h"
32 #include "llvm/Support/SystemUtils.h"
33 #include "llvm/Support/ToolOutputFile.h"
34 #include <memory>
35 using namespace llvm;
36
37 static cl::list<std::string>
38 InputFilenames(cl::Positional, cl::OneOrMore,
39                cl::desc("<input bitcode files>"));
40
41 static cl::list<std::string> OverridingInputs(
42     "override", cl::ZeroOrMore, cl::value_desc("filename"),
43     cl::desc(
44         "input bitcode file which can override previously defined symbol(s)"));
45
46 static cl::opt<std::string>
47 OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
48                cl::value_desc("filename"));
49
50 static cl::opt<bool>
51 Internalize("internalize", cl::desc("Internalize linked symbols"));
52
53 static cl::opt<bool>
54 OnlyNeeded("only-needed", cl::desc("Link only needed symbols"));
55
56 static cl::opt<bool>
57 Force("f", cl::desc("Enable binary output on terminals"));
58
59 static cl::opt<bool>
60 OutputAssembly("S",
61          cl::desc("Write output as LLVM assembly"), cl::Hidden);
62
63 static cl::opt<bool>
64 Verbose("v", cl::desc("Print information about actions taken"));
65
66 static cl::opt<bool>
67 DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
68
69 static cl::opt<bool>
70 SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"),
71                  cl::init(false));
72
73 static cl::opt<bool> PreserveBitcodeUseListOrder(
74     "preserve-bc-uselistorder",
75     cl::desc("Preserve use-list order when writing LLVM bitcode."),
76     cl::init(true), cl::Hidden);
77
78 static cl::opt<bool> PreserveAssemblyUseListOrder(
79     "preserve-ll-uselistorder",
80     cl::desc("Preserve use-list order when writing LLVM assembly."),
81     cl::init(false), cl::Hidden);
82
83 // Read the specified bitcode file in and return it. This routine searches the
84 // link path for the specified file to try to find it...
85 //
86 static std::unique_ptr<Module>
87 loadFile(const char *argv0, const std::string &FN, LLVMContext &Context) {
88   SMDiagnostic Err;
89   if (Verbose) errs() << "Loading '" << FN << "'\n";
90   std::unique_ptr<Module> Result = getLazyIRFileModule(FN, Err, Context);
91   if (!Result)
92     Err.print(argv0, errs());
93
94   Result->materializeMetadata();
95   UpgradeDebugInfo(*Result);
96
97   return Result;
98 }
99
100 static void diagnosticHandler(const DiagnosticInfo &DI) {
101   unsigned Severity = DI.getSeverity();
102   switch (Severity) {
103   case DS_Error:
104     errs() << "ERROR: ";
105     break;
106   case DS_Warning:
107     if (SuppressWarnings)
108       return;
109     errs() << "WARNING: ";
110     break;
111   case DS_Remark:
112   case DS_Note:
113     llvm_unreachable("Only expecting warnings and errors");
114   }
115
116   DiagnosticPrinterRawOStream DP(errs());
117   DI.print(DP);
118   errs() << '\n';
119 }
120
121 static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L,
122                       const cl::list<std::string> &Files,
123                       unsigned Flags) {
124   // Filter out flags that don't apply to the first file we load.
125   unsigned ApplicableFlags = Flags & Linker::Flags::OverrideFromSrc;
126   for (const auto &File : Files) {
127     std::unique_ptr<Module> M = loadFile(argv0, File, Context);
128     if (!M.get()) {
129       errs() << argv0 << ": error loading file '" << File << "'\n";
130       return false;
131     }
132
133     if (verifyModule(*M, &errs())) {
134       errs() << argv0 << ": " << File << ": error: input module is broken!\n";
135       return false;
136     }
137
138     if (Verbose)
139       errs() << "Linking in '" << File << "'\n";
140
141     if (L.linkInModule(M.get(), ApplicableFlags))
142       return false;
143     // All linker flags apply to linking of subsequent files.
144     ApplicableFlags = Flags;
145   }
146
147   return true;
148 }
149
150 int main(int argc, char **argv) {
151   // Print a stack trace if we signal out.
152   sys::PrintStackTraceOnErrorSignal();
153   PrettyStackTraceProgram X(argc, argv);
154
155   LLVMContext &Context = getGlobalContext();
156   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
157   cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
158
159   auto Composite = make_unique<Module>("llvm-link", Context);
160   Linker L(Composite.get(), diagnosticHandler);
161
162   unsigned Flags = Linker::Flags::None;
163   if (Internalize)
164     Flags |= Linker::Flags::InternalizeLinkedSymbols;
165   if (OnlyNeeded)
166     Flags |= Linker::Flags::LinkOnlyNeeded;
167
168   // First add all the regular input files
169   if (!linkFiles(argv[0], Context, L, InputFilenames, Flags))
170     return 1;
171
172   // Next the -override ones.
173   if (!linkFiles(argv[0], Context, L, OverridingInputs,
174                  Flags | Linker::Flags::OverrideFromSrc))
175     return 1;
176
177   if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite;
178
179   std::error_code EC;
180   tool_output_file Out(OutputFilename, EC, sys::fs::F_None);
181   if (EC) {
182     errs() << EC.message() << '\n';
183     return 1;
184   }
185
186   if (verifyModule(*Composite, &errs())) {
187     errs() << argv[0] << ": error: linked module is broken!\n";
188     return 1;
189   }
190
191   if (Verbose) errs() << "Writing bitcode...\n";
192   if (OutputAssembly) {
193     Composite->print(Out.os(), nullptr, PreserveAssemblyUseListOrder);
194   } else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
195     WriteBitcodeToFile(Composite.get(), Out.os(), PreserveBitcodeUseListOrder);
196
197   // Declare success.
198   Out.keep();
199
200   return 0;
201 }