1 //===- llvm-link.cpp - Low-level LLVM linker ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This utility may be invoked in the following manner:
11 // llvm-link a.bc b.bc c.bc -o x.bc
13 //===----------------------------------------------------------------------===//
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"
37 static cl::list<std::string>
38 InputFilenames(cl::Positional, cl::OneOrMore,
39 cl::desc("<input bitcode files>"));
41 static cl::opt<std::string>
42 OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
43 cl::value_desc("filename"));
46 Force("f", cl::desc("Enable binary output on terminals"));
50 cl::desc("Write output as LLVM assembly"), cl::Hidden);
53 Verbose("v", cl::desc("Print information about actions taken"));
56 DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
59 SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"),
62 // Read the specified bitcode file in and return it. This routine searches the
63 // link path for the specified file to try to find it...
65 static std::unique_ptr<Module>
66 loadFile(const char *argv0, const std::string &FN, LLVMContext &Context) {
68 if (Verbose) errs() << "Loading '" << FN << "'\n";
69 std::unique_ptr<Module> Result = getLazyIRFileModule(FN, Err, Context);
71 Err.print(argv0, errs());
73 Result->materializeMetadata();
74 UpgradeDebugInfo(*Result);
79 static void diagnosticHandler(const DiagnosticInfo &DI) {
80 unsigned Severity = DI.getSeverity();
88 errs() << "WARNING: ";
92 llvm_unreachable("Only expecting warnings and errors");
95 DiagnosticPrinterRawOStream DP(errs());
100 int main(int argc, char **argv) {
101 // Print a stack trace if we signal out.
102 sys::PrintStackTraceOnErrorSignal();
103 PrettyStackTraceProgram X(argc, argv);
105 LLVMContext &Context = getGlobalContext();
106 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
107 cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
109 auto Composite = make_unique<Module>("llvm-link", Context);
110 Linker L(Composite.get(), diagnosticHandler);
112 for (unsigned i = 0; i < InputFilenames.size(); ++i) {
113 std::unique_ptr<Module> M = loadFile(argv[0], InputFilenames[i], Context);
115 errs() << argv[0] << ": error loading file '" <<InputFilenames[i]<< "'\n";
119 if (verifyModule(*M)) {
120 errs() << argv[0] << ": input module '" << InputFilenames[i]
125 if (Verbose) errs() << "Linking in '" << InputFilenames[i] << "'\n";
127 if (L.linkInModule(M.get()))
131 if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite;
134 tool_output_file Out(OutputFilename, EC, sys::fs::F_None);
136 errs() << EC.message() << '\n';
140 if (verifyModule(*Composite)) {
141 errs() << argv[0] << ": linked module is broken!\n";
145 if (Verbose) errs() << "Writing bitcode...\n";
146 if (OutputAssembly) {
147 Out.os() << *Composite;
148 } else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
149 WriteBitcodeToFile(Composite.get(), Out.os());