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/ADT/STLExtras.h"
16#include "llvm/Bitcode/ReaderWriter.h"
17#include "llvm/IR/AutoUpgrade.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/ModuleSummaryIndex.h"
23#include "llvm/IR/Verifier.h"
24#include "llvm/IRReader/IRReader.h"
25#include "llvm/Linker/Linker.h"
26#include "llvm/Object/ModuleSummaryIndexObjectFile.h"
27#include "llvm/Support/CommandLine.h"
28#include "llvm/Support/FileSystem.h"
29#include "llvm/Support/ManagedStatic.h"
30#include "llvm/Support/Path.h"
31#include "llvm/Support/PrettyStackTrace.h"
32#include "llvm/Support/Signals.h"
33#include "llvm/Support/SourceMgr.h"
34#include "llvm/Support/SystemUtils.h"
35#include "llvm/Support/ToolOutputFile.h"
36#include "llvm/Transforms/Utils/FunctionImportUtils.h"
37
38#include <memory>
39#include <utility>
40using namespace llvm;
41
42static cl::list<std::string>
43InputFilenames(cl::Positional, cl::OneOrMore,
44               cl::desc("<input bitcode files>"));
45
46static cl::list<std::string> OverridingInputs(
47    "override", cl::ZeroOrMore, cl::value_desc("filename"),
48    cl::desc(
49        "input bitcode file which can override previously defined symbol(s)"));
50
51// Option to simulate function importing for testing. This enables using
52// llvm-link to simulate ThinLTO backend processes.
53static cl::list<std::string> Imports(
54    "import", cl::ZeroOrMore, cl::value_desc("function:filename"),
55    cl::desc("Pair of function name and filename, where function should be "
56             "imported from bitcode in filename"));
57
58// Option to support testing of function importing. The module summary
59// must be specified in the case were we request imports via the -import
60// option, as well as when compiling any module with functions that may be
61// exported (imported by a different llvm-link -import invocation), to ensure
62// consistent promotion and renaming of locals.
63static cl::opt<std::string>
64    SummaryIndex("summary-index", cl::desc("Module summary index filename"),
65                 cl::init(""), cl::value_desc("filename"));
66
67static cl::opt<std::string>
68OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
69               cl::value_desc("filename"));
70
71static cl::opt<bool>
72Internalize("internalize", cl::desc("Internalize linked symbols"));
73
74static cl::opt<bool>
75    DisableDITypeMap("disable-debug-info-type-map",
76                     cl::desc("Don't use a uniquing type map for debug info"));
77
78static cl::opt<bool>
79OnlyNeeded("only-needed", cl::desc("Link only needed symbols"));
80
81static cl::opt<bool>
82Force("f", cl::desc("Enable binary output on terminals"));
83
84static cl::opt<bool>
85OutputAssembly("S",
86         cl::desc("Write output as LLVM assembly"), cl::Hidden);
87
88static cl::opt<bool>
89Verbose("v", cl::desc("Print information about actions taken"));
90
91static cl::opt<bool>
92DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
93
94static cl::opt<bool>
95SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"),
96                 cl::init(false));
97
98static cl::opt<bool> PreserveBitcodeUseListOrder(
99    "preserve-bc-uselistorder",
100    cl::desc("Preserve use-list order when writing LLVM bitcode."),
101    cl::init(true), cl::Hidden);
102
103static cl::opt<bool> PreserveAssemblyUseListOrder(
104    "preserve-ll-uselistorder",
105    cl::desc("Preserve use-list order when writing LLVM assembly."),
106    cl::init(false), cl::Hidden);
107
108// Read the specified bitcode file in and return it. This routine searches the
109// link path for the specified file to try to find it...
110//
111static std::unique_ptr<Module> loadFile(const char *argv0,
112                                        const std::string &FN,
113                                        LLVMContext &Context,
114                                        bool MaterializeMetadata = true) {
115  SMDiagnostic Err;
116  if (Verbose) errs() << "Loading '" << FN << "'\n";
117  std::unique_ptr<Module> Result =
118      getLazyIRFileModule(FN, Err, Context, !MaterializeMetadata);
119  if (!Result) {
120    Err.print(argv0, errs());
121    return nullptr;
122  }
123
124  if (MaterializeMetadata) {
125    Result->materializeMetadata();
126    UpgradeDebugInfo(*Result);
127  }
128
129  return Result;
130}
131
132namespace {
133
134/// Helper to load on demand a Module from file and cache it for subsequent
135/// queries during function importing.
136class ModuleLazyLoaderCache {
137  /// Cache of lazily loaded module for import.
138  StringMap<std::unique_ptr<Module>> ModuleMap;
139
140  /// Retrieve a Module from the cache or lazily load it on demand.
141  std::function<std::unique_ptr<Module>(const char *argv0,
142                                        const std::string &FileName)>
143      createLazyModule;
144
145public:
146  /// Create the loader, Module will be initialized in \p Context.
147  ModuleLazyLoaderCache(std::function<std::unique_ptr<Module>(
148                            const char *argv0, const std::string &FileName)>
149                            createLazyModule)
150      : createLazyModule(std::move(createLazyModule)) {}
151
152  /// Retrieve a Module from the cache or lazily load it on demand.
153  Module &operator()(const char *argv0, const std::string &FileName);
154
155  std::unique_ptr<Module> takeModule(const std::string &FileName) {
156    auto I = ModuleMap.find(FileName);
157    assert(I != ModuleMap.end());
158    std::unique_ptr<Module> Ret = std::move(I->second);
159    ModuleMap.erase(I);
160    return Ret;
161  }
162};
163
164// Get a Module for \p FileName from the cache, or load it lazily.
165Module &ModuleLazyLoaderCache::operator()(const char *argv0,
166                                          const std::string &Identifier) {
167  auto &Module = ModuleMap[Identifier];
168  if (!Module)
169    Module = createLazyModule(argv0, Identifier);
170  return *Module;
171}
172} // anonymous namespace
173
174static void diagnosticHandler(const DiagnosticInfo &DI) {
175  unsigned Severity = DI.getSeverity();
176  switch (Severity) {
177  case DS_Error:
178    errs() << "ERROR: ";
179    break;
180  case DS_Warning:
181    if (SuppressWarnings)
182      return;
183    errs() << "WARNING: ";
184    break;
185  case DS_Remark:
186  case DS_Note:
187    llvm_unreachable("Only expecting warnings and errors");
188  }
189
190  DiagnosticPrinterRawOStream DP(errs());
191  DI.print(DP);
192  errs() << '\n';
193}
194
195static void diagnosticHandlerWithContext(const DiagnosticInfo &DI, void *C) {
196  diagnosticHandler(DI);
197}
198
199/// Import any functions requested via the -import option.
200static bool importFunctions(const char *argv0, LLVMContext &Context,
201                            Linker &L) {
202  if (SummaryIndex.empty())
203    return true;
204  ErrorOr<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr =
205      llvm::getModuleSummaryIndexForFile(SummaryIndex, diagnosticHandler);
206  std::error_code EC = IndexOrErr.getError();
207  if (EC) {
208    errs() << EC.message() << '\n';
209    return false;
210  }
211  auto Index = std::move(IndexOrErr.get());
212
213  // Map of Module -> List of globals to import from the Module
214  std::map<StringRef, DenseSet<const GlobalValue *>> ModuleToGlobalsToImportMap;
215  auto ModuleLoader = [&Context](const char *argv0,
216                                 const std::string &Identifier) {
217    return loadFile(argv0, Identifier, Context, false);
218  };
219  ModuleLazyLoaderCache ModuleLoaderCache(ModuleLoader);
220  for (const auto &Import : Imports) {
221    // Identify the requested function and its bitcode source file.
222    size_t Idx = Import.find(':');
223    if (Idx == std::string::npos) {
224      errs() << "Import parameter bad format: " << Import << "\n";
225      return false;
226    }
227    std::string FunctionName = Import.substr(0, Idx);
228    std::string FileName = Import.substr(Idx + 1, std::string::npos);
229
230    // Load the specified source module.
231    auto &SrcModule = ModuleLoaderCache(argv0, FileName);
232
233    if (verifyModule(SrcModule, &errs())) {
234      errs() << argv0 << ": " << FileName
235             << ": error: input module is broken!\n";
236      return false;
237    }
238
239    Function *F = SrcModule.getFunction(FunctionName);
240    if (!F) {
241      errs() << "Ignoring import request for non-existent function "
242             << FunctionName << " from " << FileName << "\n";
243      continue;
244    }
245    // We cannot import weak_any functions without possibly affecting the
246    // order they are seen and selected by the linker, changing program
247    // semantics.
248    if (F->hasWeakAnyLinkage()) {
249      errs() << "Ignoring import request for weak-any function " << FunctionName
250             << " from " << FileName << "\n";
251      continue;
252    }
253
254    if (Verbose)
255      errs() << "Importing " << FunctionName << " from " << FileName << "\n";
256
257    auto &Entry = ModuleToGlobalsToImportMap[SrcModule.getModuleIdentifier()];
258    Entry.insert(F);
259
260    F->materialize();
261  }
262
263  // Do the actual import of globals now, one Module at a time
264  for (auto &GlobalsToImportPerModule : ModuleToGlobalsToImportMap) {
265    // Get the module for the import
266    auto &GlobalsToImport = GlobalsToImportPerModule.second;
267    std::unique_ptr<Module> SrcModule =
268        ModuleLoaderCache.takeModule(GlobalsToImportPerModule.first);
269    assert(&Context == &SrcModule->getContext() && "Context mismatch");
270
271    // If modules were created with lazy metadata loading, materialize it
272    // now, before linking it (otherwise this will be a noop).
273    SrcModule->materializeMetadata();
274    UpgradeDebugInfo(*SrcModule);
275
276    // Linkage Promotion and renaming
277    if (renameModuleForThinLTO(*SrcModule, *Index, &GlobalsToImport))
278      return true;
279
280    // Instruct the linker to not automatically import linkonce defintion.
281    unsigned Flags = Linker::Flags::DontForceLinkLinkonceODR;
282
283    if (L.linkInModule(std::move(SrcModule), Flags, &GlobalsToImport))
284      return false;
285  }
286
287  return true;
288}
289
290static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L,
291                      const cl::list<std::string> &Files,
292                      unsigned Flags) {
293  // Filter out flags that don't apply to the first file we load.
294  unsigned ApplicableFlags = Flags & Linker::Flags::OverrideFromSrc;
295  for (const auto &File : Files) {
296    std::unique_ptr<Module> M = loadFile(argv0, File, Context);
297    if (!M.get()) {
298      errs() << argv0 << ": error loading file '" << File << "'\n";
299      return false;
300    }
301
302    // Note that when ODR merging types cannot verify input files in here When
303    // doing that debug metadata in the src module might already be pointing to
304    // the destination.
305    if (DisableDITypeMap && verifyModule(*M, &errs())) {
306      errs() << argv0 << ": " << File << ": error: input module is broken!\n";
307      return false;
308    }
309
310    // If a module summary index is supplied, load it so linkInModule can treat
311    // local functions/variables as exported and promote if necessary.
312    if (!SummaryIndex.empty()) {
313      ErrorOr<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr =
314          llvm::getModuleSummaryIndexForFile(SummaryIndex, diagnosticHandler);
315      std::error_code EC = IndexOrErr.getError();
316      if (EC) {
317        errs() << EC.message() << '\n';
318        return false;
319      }
320      auto Index = std::move(IndexOrErr.get());
321
322      // Promotion
323      if (renameModuleForThinLTO(*M, *Index))
324        return true;
325    }
326
327    if (Verbose)
328      errs() << "Linking in '" << File << "'\n";
329
330    if (L.linkInModule(std::move(M), ApplicableFlags))
331      return false;
332    // All linker flags apply to linking of subsequent files.
333    ApplicableFlags = Flags;
334  }
335
336  return true;
337}
338
339int main(int argc, char **argv) {
340  // Print a stack trace if we signal out.
341  sys::PrintStackTraceOnErrorSignal(argv[0]);
342  PrettyStackTraceProgram X(argc, argv);
343
344  LLVMContext Context;
345  Context.setDiagnosticHandler(diagnosticHandlerWithContext, nullptr, true);
346
347  llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
348  cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
349
350  if (!DisableDITypeMap)
351    Context.enableDebugTypeODRUniquing();
352
353  auto Composite = make_unique<Module>("llvm-link", Context);
354  Linker L(*Composite);
355
356  unsigned Flags = Linker::Flags::None;
357  if (Internalize)
358    Flags |= Linker::Flags::InternalizeLinkedSymbols;
359  if (OnlyNeeded)
360    Flags |= Linker::Flags::LinkOnlyNeeded;
361
362  // First add all the regular input files
363  if (!linkFiles(argv[0], Context, L, InputFilenames, Flags))
364    return 1;
365
366  // Next the -override ones.
367  if (!linkFiles(argv[0], Context, L, OverridingInputs,
368                 Flags | Linker::Flags::OverrideFromSrc))
369    return 1;
370
371  // Import any functions requested via -import
372  if (!importFunctions(argv[0], Context, L))
373    return 1;
374
375  if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite;
376
377  std::error_code EC;
378  tool_output_file Out(OutputFilename, EC, sys::fs::F_None);
379  if (EC) {
380    errs() << EC.message() << '\n';
381    return 1;
382  }
383
384  if (verifyModule(*Composite, &errs())) {
385    errs() << argv[0] << ": error: linked module is broken!\n";
386    return 1;
387  }
388
389  if (Verbose) errs() << "Writing bitcode...\n";
390  if (OutputAssembly) {
391    Composite->print(Out.os(), nullptr, PreserveAssemblyUseListOrder);
392  } else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
393    WriteBitcodeToFile(Composite.get(), Out.os(), PreserveBitcodeUseListOrder);
394
395  // Declare success.
396  Out.keep();
397
398  return 0;
399}
400