llvm-link: Verify input modules
[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/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
36 static cl::list<std::string>
37 InputFilenames(cl::Positional, cl::OneOrMore,
38                cl::desc("<input bitcode files>"));
39
40 static cl::opt<std::string>
41 OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
42                cl::value_desc("filename"));
43
44 static cl::opt<bool>
45 Force("f", cl::desc("Enable binary output on terminals"));
46
47 static cl::opt<bool>
48 OutputAssembly("S",
49          cl::desc("Write output as LLVM assembly"), cl::Hidden);
50
51 static cl::opt<bool>
52 Verbose("v", cl::desc("Print information about actions taken"));
53
54 static cl::opt<bool>
55 DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
56
57 static cl::opt<bool>
58 SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"),
59                  cl::init(false));
60
61 // Read the specified bitcode file in and return it. This routine searches the
62 // link path for the specified file to try to find it...
63 //
64 static std::unique_ptr<Module>
65 loadFile(const char *argv0, const std::string &FN, LLVMContext &Context) {
66   SMDiagnostic Err;
67   if (Verbose) errs() << "Loading '" << FN << "'\n";
68   std::unique_ptr<Module> Result = getLazyIRFileModule(FN, Err, Context);
69   if (!Result)
70     Err.print(argv0, errs());
71
72   return Result;
73 }
74
75 static void diagnosticHandler(const DiagnosticInfo &DI) {
76   unsigned Severity = DI.getSeverity();
77   switch (Severity) {
78   case DS_Error:
79     errs() << "ERROR: ";
80     break;
81   case DS_Warning:
82     if (SuppressWarnings)
83       return;
84     errs() << "WARNING: ";
85     break;
86   case DS_Remark:
87   case DS_Note:
88     llvm_unreachable("Only expecting warnings and errors");
89   }
90
91   DiagnosticPrinterRawOStream DP(errs());
92   DI.print(DP);
93   errs() << '\n';
94 }
95
96 int main(int argc, char **argv) {
97   // Print a stack trace if we signal out.
98   sys::PrintStackTraceOnErrorSignal();
99   PrettyStackTraceProgram X(argc, argv);
100
101   LLVMContext &Context = getGlobalContext();
102   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
103   cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
104
105   auto Composite = make_unique<Module>("llvm-link", Context);
106   Linker L(Composite.get(), diagnosticHandler);
107
108   for (unsigned i = 0; i < InputFilenames.size(); ++i) {
109     std::unique_ptr<Module> M = loadFile(argv[0], InputFilenames[i], Context);
110     if (!M.get()) {
111       errs() << argv[0] << ": error loading file '" <<InputFilenames[i]<< "'\n";
112       return 1;
113     }
114
115     if (verifyModule(*M)) {
116       errs() << argv[0] << ": input module '" << InputFilenames[i]
117              << "' is broken!\n";
118       return 1;
119     }
120
121     if (Verbose) errs() << "Linking in '" << InputFilenames[i] << "'\n";
122
123     if (L.linkInModule(M.get()))
124       return 1;
125   }
126
127   if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite;
128
129   std::error_code EC;
130   tool_output_file Out(OutputFilename, EC, sys::fs::F_None);
131   if (EC) {
132     errs() << EC.message() << '\n';
133     return 1;
134   }
135
136   if (verifyModule(*Composite)) {
137     errs() << argv[0] << ": linked module is broken!\n";
138     return 1;
139   }
140
141   if (Verbose) errs() << "Writing bitcode...\n";
142   if (OutputAssembly) {
143     Out.os() << *Composite;
144   } else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
145     WriteBitcodeToFile(Composite.get(), Out.os());
146
147   // Declare success.
148   Out.keep();
149
150   return 0;
151 }