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::list<std::string> OverridingInputs(
42 "override", cl::ZeroOrMore, cl::value_desc("filename"),
44 "input bitcode file which can override previously defined symbol(s)"));
46 static cl::opt<std::string>
47 OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
48 cl::value_desc("filename"));
51 Internalize("internalize", cl::desc("Internalize linked symbols"));
54 OnlyNeeded("only-needed", cl::desc("Link only needed symbols"));
57 Force("f", cl::desc("Enable binary output on terminals"));
61 cl::desc("Write output as LLVM assembly"), cl::Hidden);
64 Verbose("v", cl::desc("Print information about actions taken"));
67 DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
70 SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"),
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);
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);
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...
86 static std::unique_ptr<Module>
87 loadFile(const char *argv0, const std::string &FN, LLVMContext &Context) {
89 if (Verbose) errs() << "Loading '" << FN << "'\n";
90 std::unique_ptr<Module> Result = getLazyIRFileModule(FN, Err, Context);
92 Err.print(argv0, errs());
94 Result->materializeMetadata();
95 UpgradeDebugInfo(*Result);
100 static void diagnosticHandler(const DiagnosticInfo &DI) {
101 unsigned Severity = DI.getSeverity();
107 if (SuppressWarnings)
109 errs() << "WARNING: ";
113 llvm_unreachable("Only expecting warnings and errors");
116 DiagnosticPrinterRawOStream DP(errs());
121 static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L,
122 const cl::list<std::string> &Files,
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);
129 errs() << argv0 << ": error loading file '" << File << "'\n";
133 if (verifyModule(*M, &errs())) {
134 errs() << argv0 << ": " << File << ": error: input module is broken!\n";
139 errs() << "Linking in '" << File << "'\n";
141 if (L.linkInModule(M.get(), ApplicableFlags))
143 // All linker flags apply to linking of subsequent files.
144 ApplicableFlags = Flags;
150 int main(int argc, char **argv) {
151 // Print a stack trace if we signal out.
152 sys::PrintStackTraceOnErrorSignal();
153 PrettyStackTraceProgram X(argc, argv);
155 LLVMContext &Context = getGlobalContext();
156 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
157 cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
159 auto Composite = make_unique<Module>("llvm-link", Context);
160 Linker L(Composite.get(), diagnosticHandler);
162 unsigned Flags = Linker::Flags::None;
164 Flags |= Linker::Flags::InternalizeLinkedSymbols;
166 Flags |= Linker::Flags::LinkOnlyNeeded;
168 // First add all the regular input files
169 if (!linkFiles(argv[0], Context, L, InputFilenames, Flags))
172 // Next the -override ones.
173 if (!linkFiles(argv[0], Context, L, OverridingInputs,
174 Flags | Linker::Flags::OverrideFromSrc))
177 if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite;
180 tool_output_file Out(OutputFilename, EC, sys::fs::F_None);
182 errs() << EC.message() << '\n';
186 if (verifyModule(*Composite, &errs())) {
187 errs() << argv[0] << ": error: linked module is broken!\n";
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);