xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-link/llvm-link.cpp (revision 8bcb0991864975618c09697b1aca10683346d9f0)
1 //===- llvm-link.cpp - Low-level LLVM linker ------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This utility may be invoked in the following manner:
10 //  llvm-link a.bc b.bc c.bc -o x.bc
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/Bitcode/BitcodeReader.h"
16 #include "llvm/Bitcode/BitcodeWriter.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/Support/CommandLine.h"
27 #include "llvm/Support/FileSystem.h"
28 #include "llvm/Support/InitLLVM.h"
29 #include "llvm/Support/Path.h"
30 #include "llvm/Support/SourceMgr.h"
31 #include "llvm/Support/SystemUtils.h"
32 #include "llvm/Support/ToolOutputFile.h"
33 #include "llvm/Support/WithColor.h"
34 #include "llvm/Transforms/IPO/FunctionImport.h"
35 #include "llvm/Transforms/IPO/Internalize.h"
36 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
37 
38 #include <memory>
39 #include <utility>
40 using namespace llvm;
41 
42 static cl::list<std::string>
43 InputFilenames(cl::Positional, cl::OneOrMore,
44                cl::desc("<input bitcode files>"));
45 
46 static 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.
53 static 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.
63 static cl::opt<std::string>
64     SummaryIndex("summary-index", cl::desc("Module summary index filename"),
65                  cl::init(""), cl::value_desc("filename"));
66 
67 static cl::opt<std::string>
68 OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
69                cl::value_desc("filename"));
70 
71 static cl::opt<bool>
72 Internalize("internalize", cl::desc("Internalize linked symbols"));
73 
74 static cl::opt<bool>
75     DisableDITypeMap("disable-debug-info-type-map",
76                      cl::desc("Don't use a uniquing type map for debug info"));
77 
78 static cl::opt<bool>
79 OnlyNeeded("only-needed", cl::desc("Link only needed symbols"));
80 
81 static cl::opt<bool>
82 Force("f", cl::desc("Enable binary output on terminals"));
83 
84 static cl::opt<bool>
85     DisableLazyLoad("disable-lazy-loading",
86                     cl::desc("Disable lazy module loading"));
87 
88 static cl::opt<bool>
89     OutputAssembly("S", cl::desc("Write output as LLVM assembly"), cl::Hidden);
90 
91 static cl::opt<bool>
92 Verbose("v", cl::desc("Print information about actions taken"));
93 
94 static cl::opt<bool>
95 DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
96 
97 static cl::opt<bool>
98 SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"),
99                  cl::init(false));
100 
101 static cl::opt<bool> PreserveBitcodeUseListOrder(
102     "preserve-bc-uselistorder",
103     cl::desc("Preserve use-list order when writing LLVM bitcode."),
104     cl::init(true), cl::Hidden);
105 
106 static cl::opt<bool> PreserveAssemblyUseListOrder(
107     "preserve-ll-uselistorder",
108     cl::desc("Preserve use-list order when writing LLVM assembly."),
109     cl::init(false), cl::Hidden);
110 
111 static ExitOnError ExitOnErr;
112 
113 // Read the specified bitcode file in and return it. This routine searches the
114 // link path for the specified file to try to find it...
115 //
116 static std::unique_ptr<Module> loadFile(const char *argv0,
117                                         const std::string &FN,
118                                         LLVMContext &Context,
119                                         bool MaterializeMetadata = true) {
120   SMDiagnostic Err;
121   if (Verbose)
122     errs() << "Loading '" << FN << "'\n";
123   std::unique_ptr<Module> Result;
124   if (DisableLazyLoad)
125     Result = parseIRFile(FN, Err, Context);
126   else
127     Result = getLazyIRFileModule(FN, Err, Context, !MaterializeMetadata);
128 
129   if (!Result) {
130     Err.print(argv0, errs());
131     return nullptr;
132   }
133 
134   if (MaterializeMetadata) {
135     ExitOnErr(Result->materializeMetadata());
136     UpgradeDebugInfo(*Result);
137   }
138 
139   return Result;
140 }
141 
142 namespace {
143 
144 /// Helper to load on demand a Module from file and cache it for subsequent
145 /// queries during function importing.
146 class ModuleLazyLoaderCache {
147   /// Cache of lazily loaded module for import.
148   StringMap<std::unique_ptr<Module>> ModuleMap;
149 
150   /// Retrieve a Module from the cache or lazily load it on demand.
151   std::function<std::unique_ptr<Module>(const char *argv0,
152                                         const std::string &FileName)>
153       createLazyModule;
154 
155 public:
156   /// Create the loader, Module will be initialized in \p Context.
157   ModuleLazyLoaderCache(std::function<std::unique_ptr<Module>(
158                             const char *argv0, const std::string &FileName)>
159                             createLazyModule)
160       : createLazyModule(std::move(createLazyModule)) {}
161 
162   /// Retrieve a Module from the cache or lazily load it on demand.
163   Module &operator()(const char *argv0, const std::string &FileName);
164 
165   std::unique_ptr<Module> takeModule(const std::string &FileName) {
166     auto I = ModuleMap.find(FileName);
167     assert(I != ModuleMap.end());
168     std::unique_ptr<Module> Ret = std::move(I->second);
169     ModuleMap.erase(I);
170     return Ret;
171   }
172 };
173 
174 // Get a Module for \p FileName from the cache, or load it lazily.
175 Module &ModuleLazyLoaderCache::operator()(const char *argv0,
176                                           const std::string &Identifier) {
177   auto &Module = ModuleMap[Identifier];
178   if (!Module)
179     Module = createLazyModule(argv0, Identifier);
180   return *Module;
181 }
182 } // anonymous namespace
183 
184 namespace {
185 struct LLVMLinkDiagnosticHandler : public DiagnosticHandler {
186   bool handleDiagnostics(const DiagnosticInfo &DI) override {
187     unsigned Severity = DI.getSeverity();
188     switch (Severity) {
189     case DS_Error:
190       WithColor::error();
191       break;
192     case DS_Warning:
193       if (SuppressWarnings)
194         return true;
195       WithColor::warning();
196       break;
197     case DS_Remark:
198     case DS_Note:
199       llvm_unreachable("Only expecting warnings and errors");
200     }
201 
202     DiagnosticPrinterRawOStream DP(errs());
203     DI.print(DP);
204     errs() << '\n';
205     return true;
206   }
207 };
208 }
209 
210 /// Import any functions requested via the -import option.
211 static bool importFunctions(const char *argv0, Module &DestModule) {
212   if (SummaryIndex.empty())
213     return true;
214   std::unique_ptr<ModuleSummaryIndex> Index =
215       ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex));
216 
217   // Map of Module -> List of globals to import from the Module
218   FunctionImporter::ImportMapTy ImportList;
219 
220   auto ModuleLoader = [&DestModule](const char *argv0,
221                                     const std::string &Identifier) {
222     return loadFile(argv0, Identifier, DestModule.getContext(), false);
223   };
224 
225   ModuleLazyLoaderCache ModuleLoaderCache(ModuleLoader);
226   for (const auto &Import : Imports) {
227     // Identify the requested function and its bitcode source file.
228     size_t Idx = Import.find(':');
229     if (Idx == std::string::npos) {
230       errs() << "Import parameter bad format: " << Import << "\n";
231       return false;
232     }
233     std::string FunctionName = Import.substr(0, Idx);
234     std::string FileName = Import.substr(Idx + 1, std::string::npos);
235 
236     // Load the specified source module.
237     auto &SrcModule = ModuleLoaderCache(argv0, FileName);
238 
239     if (verifyModule(SrcModule, &errs())) {
240       errs() << argv0 << ": " << FileName;
241       WithColor::error() << "input module is broken!\n";
242       return false;
243     }
244 
245     Function *F = SrcModule.getFunction(FunctionName);
246     if (!F) {
247       errs() << "Ignoring import request for non-existent function "
248              << FunctionName << " from " << FileName << "\n";
249       continue;
250     }
251     // We cannot import weak_any functions without possibly affecting the
252     // order they are seen and selected by the linker, changing program
253     // semantics.
254     if (F->hasWeakAnyLinkage()) {
255       errs() << "Ignoring import request for weak-any function " << FunctionName
256              << " from " << FileName << "\n";
257       continue;
258     }
259 
260     if (Verbose)
261       errs() << "Importing " << FunctionName << " from " << FileName << "\n";
262 
263     auto &Entry = ImportList[FileName];
264     Entry.insert(F->getGUID());
265   }
266   auto CachedModuleLoader = [&](StringRef Identifier) {
267     return ModuleLoaderCache.takeModule(Identifier);
268   };
269   FunctionImporter Importer(*Index, CachedModuleLoader);
270   ExitOnErr(Importer.importFunctions(DestModule, ImportList));
271 
272   return true;
273 }
274 
275 static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L,
276                       const cl::list<std::string> &Files,
277                       unsigned Flags) {
278   // Filter out flags that don't apply to the first file we load.
279   unsigned ApplicableFlags = Flags & Linker::Flags::OverrideFromSrc;
280   // Similar to some flags, internalization doesn't apply to the first file.
281   bool InternalizeLinkedSymbols = false;
282   for (const auto &File : Files) {
283     std::unique_ptr<Module> M = loadFile(argv0, File, Context);
284     if (!M.get()) {
285       errs() << argv0 << ": ";
286       WithColor::error() << " loading file '" << File << "'\n";
287       return false;
288     }
289 
290     // Note that when ODR merging types cannot verify input files in here When
291     // doing that debug metadata in the src module might already be pointing to
292     // the destination.
293     if (DisableDITypeMap && verifyModule(*M, &errs())) {
294       errs() << argv0 << ": " << File << ": ";
295       WithColor::error() << "input module is broken!\n";
296       return false;
297     }
298 
299     // If a module summary index is supplied, load it so linkInModule can treat
300     // local functions/variables as exported and promote if necessary.
301     if (!SummaryIndex.empty()) {
302       std::unique_ptr<ModuleSummaryIndex> Index =
303           ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex));
304 
305       // Conservatively mark all internal values as promoted, since this tool
306       // does not do the ThinLink that would normally determine what values to
307       // promote.
308       for (auto &I : *Index) {
309         for (auto &S : I.second.SummaryList) {
310           if (GlobalValue::isLocalLinkage(S->linkage()))
311             S->setLinkage(GlobalValue::ExternalLinkage);
312         }
313       }
314 
315       // Promotion
316       if (renameModuleForThinLTO(*M, *Index))
317         return true;
318     }
319 
320     if (Verbose)
321       errs() << "Linking in '" << File << "'\n";
322 
323     bool Err = false;
324     if (InternalizeLinkedSymbols) {
325       Err = L.linkInModule(
326           std::move(M), ApplicableFlags, [](Module &M, const StringSet<> &GVS) {
327             internalizeModule(M, [&GVS](const GlobalValue &GV) {
328               return !GV.hasName() || (GVS.count(GV.getName()) == 0);
329             });
330           });
331     } else {
332       Err = L.linkInModule(std::move(M), ApplicableFlags);
333     }
334 
335     if (Err)
336       return false;
337 
338     // Internalization applies to linking of subsequent files.
339     InternalizeLinkedSymbols = Internalize;
340 
341     // All linker flags apply to linking of subsequent files.
342     ApplicableFlags = Flags;
343   }
344 
345   return true;
346 }
347 
348 int main(int argc, char **argv) {
349   InitLLVM X(argc, argv);
350   ExitOnErr.setBanner(std::string(argv[0]) + ": ");
351 
352   LLVMContext Context;
353   Context.setDiagnosticHandler(
354     std::make_unique<LLVMLinkDiagnosticHandler>(), true);
355   cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
356 
357   if (!DisableDITypeMap)
358     Context.enableDebugTypeODRUniquing();
359 
360   auto Composite = std::make_unique<Module>("llvm-link", Context);
361   Linker L(*Composite);
362 
363   unsigned Flags = Linker::Flags::None;
364   if (OnlyNeeded)
365     Flags |= Linker::Flags::LinkOnlyNeeded;
366 
367   // First add all the regular input files
368   if (!linkFiles(argv[0], Context, L, InputFilenames, Flags))
369     return 1;
370 
371   // Next the -override ones.
372   if (!linkFiles(argv[0], Context, L, OverridingInputs,
373                  Flags | Linker::Flags::OverrideFromSrc))
374     return 1;
375 
376   // Import any functions requested via -import
377   if (!importFunctions(argv[0], *Composite))
378     return 1;
379 
380   if (DumpAsm)
381     errs() << "Here's the assembly:\n" << *Composite;
382 
383   std::error_code EC;
384   ToolOutputFile Out(OutputFilename, EC, sys::fs::OF_None);
385   if (EC) {
386     WithColor::error() << EC.message() << '\n';
387     return 1;
388   }
389 
390   if (verifyModule(*Composite, &errs())) {
391     errs() << argv[0] << ": ";
392     WithColor::error() << "linked module is broken!\n";
393     return 1;
394   }
395 
396   if (Verbose)
397     errs() << "Writing bitcode...\n";
398   if (OutputAssembly) {
399     Composite->print(Out.os(), nullptr, PreserveAssemblyUseListOrder);
400   } else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
401     WriteBitcodeToFile(*Composite, Out.os(), PreserveBitcodeUseListOrder);
402 
403   // Declare success.
404   Out.keep();
405 
406   return 0;
407 }
408