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/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"
36 static cl::list<std::string>
37 InputFilenames(cl::Positional, cl::OneOrMore,
38 cl::desc("<input bitcode files>"));
40 static cl::opt<std::string>
41 OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
42 cl::value_desc("filename"));
45 Force("f", cl::desc("Enable binary output on terminals"));
49 cl::desc("Write output as LLVM assembly"), cl::Hidden);
52 Verbose("v", cl::desc("Print information about actions taken"));
55 DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
58 SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"),
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...
64 static std::unique_ptr<Module>
65 loadFile(const char *argv0, const std::string &FN, LLVMContext &Context) {
67 if (Verbose) errs() << "Loading '" << FN << "'\n";
68 std::unique_ptr<Module> Result = getLazyIRFileModule(FN, Err, Context);
70 Err.print(argv0, errs());
72 // Fixme (pr23045). We would like to upgrade the metadata with something like
73 // Result->materializeMetadata();
74 // UpgradeDebugInfo(*Result);
75 // but that fails to drop old debug info from function bodies.
76 Result->materializeAllPermanently();
81 static void diagnosticHandler(const DiagnosticInfo &DI) {
82 unsigned Severity = DI.getSeverity();
90 errs() << "WARNING: ";
94 llvm_unreachable("Only expecting warnings and errors");
97 DiagnosticPrinterRawOStream DP(errs());
102 int main(int argc, char **argv) {
103 // Print a stack trace if we signal out.
104 sys::PrintStackTraceOnErrorSignal();
105 PrettyStackTraceProgram X(argc, argv);
107 LLVMContext &Context = getGlobalContext();
108 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
109 cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
111 auto Composite = make_unique<Module>("llvm-link", Context);
112 Linker L(Composite.get(), diagnosticHandler);
114 for (unsigned i = 0; i < InputFilenames.size(); ++i) {
115 std::unique_ptr<Module> M = loadFile(argv[0], InputFilenames[i], Context);
117 errs() << argv[0] << ": error loading file '" <<InputFilenames[i]<< "'\n";
121 if (verifyModule(*M)) {
122 errs() << argv[0] << ": input module '" << InputFilenames[i]
127 if (Verbose) errs() << "Linking in '" << InputFilenames[i] << "'\n";
129 if (L.linkInModule(M.get()))
133 if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite;
136 tool_output_file Out(OutputFilename, EC, sys::fs::F_None);
138 errs() << EC.message() << '\n';
142 if (verifyModule(*Composite)) {
143 errs() << argv[0] << ": linked module is broken!\n";
147 if (Verbose) errs() << "Writing bitcode...\n";
148 if (OutputAssembly) {
149 Out.os() << *Composite;
150 } else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
151 WriteBitcodeToFile(Composite.get(), Out.os());