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/Object/Archive.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/Bitcode/BitcodeReader.h"
17 #include "llvm/Bitcode/BitcodeWriter.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/ModuleSummaryIndex.h"
24 #include "llvm/IR/Verifier.h"
25 #include "llvm/IRReader/IRReader.h"
26 #include "llvm/Linker/Linker.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/FileSystem.h"
29 #include "llvm/Support/InitLLVM.h"
30 #include "llvm/Support/Path.h"
31 #include "llvm/Support/SourceMgr.h"
32 #include "llvm/Support/SystemUtils.h"
33 #include "llvm/Support/ToolOutputFile.h"
34 #include "llvm/Support/WithColor.h"
35 #include "llvm/Transforms/IPO/FunctionImport.h"
36 #include "llvm/Transforms/IPO/Internalize.h"
37 #include "llvm/Transforms/Utils/FunctionImportUtils.h"
38 
39 #include <memory>
40 #include <utility>
41 using namespace llvm;
42 
43 static cl::list<std::string>
44 InputFilenames(cl::Positional, cl::OneOrMore,
45                cl::desc("<input bitcode files>"));
46 
47 static cl::list<std::string> OverridingInputs(
48     "override", cl::ZeroOrMore, cl::value_desc("filename"),
49     cl::desc(
50         "input bitcode file which can override previously defined symbol(s)"));
51 
52 // Option to simulate function importing for testing. This enables using
53 // llvm-link to simulate ThinLTO backend processes.
54 static cl::list<std::string> Imports(
55     "import", cl::ZeroOrMore, cl::value_desc("function:filename"),
56     cl::desc("Pair of function name and filename, where function should be "
57              "imported from bitcode in filename"));
58 
59 // Option to support testing of function importing. The module summary
60 // must be specified in the case were we request imports via the -import
61 // option, as well as when compiling any module with functions that may be
62 // exported (imported by a different llvm-link -import invocation), to ensure
63 // consistent promotion and renaming of locals.
64 static cl::opt<std::string>
65     SummaryIndex("summary-index", cl::desc("Module summary index filename"),
66                  cl::init(""), cl::value_desc("filename"));
67 
68 static cl::opt<std::string>
69 OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
70                cl::value_desc("filename"));
71 
72 static cl::opt<bool>
73 Internalize("internalize", cl::desc("Internalize linked symbols"));
74 
75 static cl::opt<bool>
76     DisableDITypeMap("disable-debug-info-type-map",
77                      cl::desc("Don't use a uniquing type map for debug info"));
78 
79 static cl::opt<bool>
80 OnlyNeeded("only-needed", cl::desc("Link only needed symbols"));
81 
82 static cl::opt<bool>
83 Force("f", cl::desc("Enable binary output on terminals"));
84 
85 static cl::opt<bool>
86     DisableLazyLoad("disable-lazy-loading",
87                     cl::desc("Disable lazy module loading"));
88 
89 static cl::opt<bool>
90     OutputAssembly("S", cl::desc("Write output as LLVM assembly"), cl::Hidden);
91 
92 static cl::opt<bool>
93 Verbose("v", cl::desc("Print information about actions taken"));
94 
95 static cl::opt<bool>
96 DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
97 
98 static cl::opt<bool>
99 SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"),
100                  cl::init(false));
101 
102 static cl::opt<bool> PreserveBitcodeUseListOrder(
103     "preserve-bc-uselistorder",
104     cl::desc("Preserve use-list order when writing LLVM bitcode."),
105     cl::init(true), cl::Hidden);
106 
107 static cl::opt<bool> PreserveAssemblyUseListOrder(
108     "preserve-ll-uselistorder",
109     cl::desc("Preserve use-list order when writing LLVM assembly."),
110     cl::init(false), cl::Hidden);
111 
112 static ExitOnError ExitOnErr;
113 
114 // Read the specified bitcode file in and return it. This routine searches the
115 // link path for the specified file to try to find it...
116 //
117 static std::unique_ptr<Module> loadFile(const char *argv0,
118                                         const std::string &FN,
119                                         LLVMContext &Context,
120                                         bool MaterializeMetadata = true) {
121   SMDiagnostic Err;
122   if (Verbose)
123     errs() << "Loading '" << FN << "'\n";
124   std::unique_ptr<Module> Result;
125   if (DisableLazyLoad)
126     Result = parseIRFile(FN, Err, Context);
127   else
128     Result = getLazyIRFileModule(FN, Err, Context, !MaterializeMetadata);
129 
130   if (!Result) {
131     Err.print(argv0, errs());
132     return nullptr;
133   }
134 
135   if (MaterializeMetadata) {
136     ExitOnErr(Result->materializeMetadata());
137     UpgradeDebugInfo(*Result);
138   }
139 
140   return Result;
141 }
142 
143 static std::unique_ptr<Module> loadArFile(const char *Argv0,
144                                           const std::string &ArchiveName,
145                                           LLVMContext &Context, Linker &L,
146                                           unsigned OrigFlags,
147                                           unsigned ApplicableFlags) {
148   std::unique_ptr<Module> Result(new Module("ArchiveModule", Context));
149   if (Verbose)
150     errs() << "Reading library archive file '" << ArchiveName
151            << "' to memory\n";
152   ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
153     MemoryBuffer::getFile(ArchiveName, -1, false);
154   ExitOnErr(errorCodeToError(Buf.getError()));
155   Error Err = Error::success();
156   object::Archive Archive(Buf.get()->getMemBufferRef(), Err);
157   ExitOnErr(std::move(Err));
158   for (const object::Archive::Child &C : Archive.children(Err)) {
159     Expected<StringRef> Ename = C.getName();
160     if (Error E = Ename.takeError()) {
161       errs() << Argv0 << ": ";
162       WithColor::error()
163           << " failed to read name of archive member"
164           << ArchiveName << "'\n";
165       return nullptr;
166     };
167     std::string ChildName = Ename.get().str();
168     if (Verbose)
169       errs() << "Parsing member '" << ChildName
170              << "' of archive library to module.\n";
171     SMDiagnostic ParseErr;
172     Expected<MemoryBufferRef> MemBuf = C.getMemoryBufferRef();
173     if (Error E = MemBuf.takeError()) {
174       errs() << Argv0 << ": ";
175       WithColor::error() << " loading memory for member '" << ChildName
176                          << "' of archive library failed'" << ArchiveName
177                          << "'\n";
178       return nullptr;
179     };
180 
181     if (!isBitcode(reinterpret_cast<const unsigned char *>
182                    (MemBuf.get().getBufferStart()),
183                    reinterpret_cast<const unsigned char *>
184                    (MemBuf.get().getBufferEnd()))) {
185       errs() << Argv0 << ": ";
186       WithColor::error() << "  member of archive is not a bitcode file: '"
187                          << ChildName << "'\n";
188       return nullptr;
189     }
190 
191     std::unique_ptr<Module> M = parseIR(MemBuf.get(), ParseErr, Context);
192 
193     if (!M.get()) {
194       errs() << Argv0 << ": ";
195       WithColor::error() << " parsing member '" << ChildName
196                          << "' of archive library failed'" << ArchiveName
197                          << "'\n";
198       return nullptr;
199     }
200     if (Verbose)
201       errs() << "Linking member '" << ChildName << "' of archive library.\n";
202     if (L.linkModules(*Result, std::move(M), ApplicableFlags))
203       return nullptr;
204     ApplicableFlags = OrigFlags;
205   } // end for each child
206   ExitOnErr(std::move(Err));
207   return Result;
208 }
209 
210 namespace {
211 
212 /// Helper to load on demand a Module from file and cache it for subsequent
213 /// queries during function importing.
214 class ModuleLazyLoaderCache {
215   /// Cache of lazily loaded module for import.
216   StringMap<std::unique_ptr<Module>> ModuleMap;
217 
218   /// Retrieve a Module from the cache or lazily load it on demand.
219   std::function<std::unique_ptr<Module>(const char *argv0,
220                                         const std::string &FileName)>
221       createLazyModule;
222 
223 public:
224   /// Create the loader, Module will be initialized in \p Context.
225   ModuleLazyLoaderCache(std::function<std::unique_ptr<Module>(
226                             const char *argv0, const std::string &FileName)>
227                             createLazyModule)
228       : createLazyModule(std::move(createLazyModule)) {}
229 
230   /// Retrieve a Module from the cache or lazily load it on demand.
231   Module &operator()(const char *argv0, const std::string &FileName);
232 
233   std::unique_ptr<Module> takeModule(const std::string &FileName) {
234     auto I = ModuleMap.find(FileName);
235     assert(I != ModuleMap.end());
236     std::unique_ptr<Module> Ret = std::move(I->second);
237     ModuleMap.erase(I);
238     return Ret;
239   }
240 };
241 
242 // Get a Module for \p FileName from the cache, or load it lazily.
243 Module &ModuleLazyLoaderCache::operator()(const char *argv0,
244                                           const std::string &Identifier) {
245   auto &Module = ModuleMap[Identifier];
246   if (!Module)
247     Module = createLazyModule(argv0, Identifier);
248   return *Module;
249 }
250 } // anonymous namespace
251 
252 namespace {
253 struct LLVMLinkDiagnosticHandler : public DiagnosticHandler {
254   bool handleDiagnostics(const DiagnosticInfo &DI) override {
255     unsigned Severity = DI.getSeverity();
256     switch (Severity) {
257     case DS_Error:
258       WithColor::error();
259       break;
260     case DS_Warning:
261       if (SuppressWarnings)
262         return true;
263       WithColor::warning();
264       break;
265     case DS_Remark:
266     case DS_Note:
267       llvm_unreachable("Only expecting warnings and errors");
268     }
269 
270     DiagnosticPrinterRawOStream DP(errs());
271     DI.print(DP);
272     errs() << '\n';
273     return true;
274   }
275 };
276 }
277 
278 /// Import any functions requested via the -import option.
279 static bool importFunctions(const char *argv0, Module &DestModule) {
280   if (SummaryIndex.empty())
281     return true;
282   std::unique_ptr<ModuleSummaryIndex> Index =
283       ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex));
284 
285   // Map of Module -> List of globals to import from the Module
286   FunctionImporter::ImportMapTy ImportList;
287 
288   auto ModuleLoader = [&DestModule](const char *argv0,
289                                     const std::string &Identifier) {
290     return loadFile(argv0, Identifier, DestModule.getContext(), false);
291   };
292 
293   ModuleLazyLoaderCache ModuleLoaderCache(ModuleLoader);
294   for (const auto &Import : Imports) {
295     // Identify the requested function and its bitcode source file.
296     size_t Idx = Import.find(':');
297     if (Idx == std::string::npos) {
298       errs() << "Import parameter bad format: " << Import << "\n";
299       return false;
300     }
301     std::string FunctionName = Import.substr(0, Idx);
302     std::string FileName = Import.substr(Idx + 1, std::string::npos);
303 
304     // Load the specified source module.
305     auto &SrcModule = ModuleLoaderCache(argv0, FileName);
306 
307     if (verifyModule(SrcModule, &errs())) {
308       errs() << argv0 << ": " << FileName;
309       WithColor::error() << "input module is broken!\n";
310       return false;
311     }
312 
313     Function *F = SrcModule.getFunction(FunctionName);
314     if (!F) {
315       errs() << "Ignoring import request for non-existent function "
316              << FunctionName << " from " << FileName << "\n";
317       continue;
318     }
319     // We cannot import weak_any functions without possibly affecting the
320     // order they are seen and selected by the linker, changing program
321     // semantics.
322     if (F->hasWeakAnyLinkage()) {
323       errs() << "Ignoring import request for weak-any function " << FunctionName
324              << " from " << FileName << "\n";
325       continue;
326     }
327 
328     if (Verbose)
329       errs() << "Importing " << FunctionName << " from " << FileName << "\n";
330 
331     auto &Entry = ImportList[FileName];
332     Entry.insert(F->getGUID());
333   }
334   auto CachedModuleLoader = [&](StringRef Identifier) {
335     return ModuleLoaderCache.takeModule(std::string(Identifier));
336   };
337   FunctionImporter Importer(*Index, CachedModuleLoader,
338                             /*ClearDSOLocalOnDeclarations=*/false);
339   ExitOnErr(Importer.importFunctions(DestModule, ImportList));
340 
341   return true;
342 }
343 
344 static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L,
345                       const cl::list<std::string> &Files,
346                       unsigned Flags) {
347   // Filter out flags that don't apply to the first file we load.
348   unsigned ApplicableFlags = Flags & Linker::Flags::OverrideFromSrc;
349   // Similar to some flags, internalization doesn't apply to the first file.
350   bool InternalizeLinkedSymbols = false;
351   for (const auto &File : Files) {
352     std::unique_ptr<Module> M =
353       (llvm::sys::path::extension(File) == ".a")
354           ? loadArFile(argv0, File, Context, L, Flags, ApplicableFlags)
355           : loadFile(argv0, File, Context);
356     if (!M.get()) {
357       errs() << argv0 << ": ";
358       WithColor::error() << " loading file '" << File << "'\n";
359       return false;
360     }
361 
362     // Note that when ODR merging types cannot verify input files in here When
363     // doing that debug metadata in the src module might already be pointing to
364     // the destination.
365     if (DisableDITypeMap && verifyModule(*M, &errs())) {
366       errs() << argv0 << ": " << File << ": ";
367       WithColor::error() << "input module is broken!\n";
368       return false;
369     }
370 
371     // If a module summary index is supplied, load it so linkInModule can treat
372     // local functions/variables as exported and promote if necessary.
373     if (!SummaryIndex.empty()) {
374       std::unique_ptr<ModuleSummaryIndex> Index =
375           ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex));
376 
377       // Conservatively mark all internal values as promoted, since this tool
378       // does not do the ThinLink that would normally determine what values to
379       // promote.
380       for (auto &I : *Index) {
381         for (auto &S : I.second.SummaryList) {
382           if (GlobalValue::isLocalLinkage(S->linkage()))
383             S->setLinkage(GlobalValue::ExternalLinkage);
384         }
385       }
386 
387       // Promotion
388       if (renameModuleForThinLTO(*M, *Index,
389                                  /*ClearDSOLocalOnDeclarations=*/false))
390         return true;
391     }
392 
393     if (Verbose)
394       errs() << "Linking in '" << File << "'\n";
395 
396     bool Err = false;
397     if (InternalizeLinkedSymbols) {
398       Err = L.linkInModule(
399           std::move(M), ApplicableFlags, [](Module &M, const StringSet<> &GVS) {
400             internalizeModule(M, [&GVS](const GlobalValue &GV) {
401               return !GV.hasName() || (GVS.count(GV.getName()) == 0);
402             });
403           });
404     } else {
405       Err = L.linkInModule(std::move(M), ApplicableFlags);
406     }
407 
408     if (Err)
409       return false;
410 
411     // Internalization applies to linking of subsequent files.
412     InternalizeLinkedSymbols = Internalize;
413 
414     // All linker flags apply to linking of subsequent files.
415     ApplicableFlags = Flags;
416   }
417 
418   return true;
419 }
420 
421 int main(int argc, char **argv) {
422   InitLLVM X(argc, argv);
423   ExitOnErr.setBanner(std::string(argv[0]) + ": ");
424 
425   LLVMContext Context;
426   Context.setDiagnosticHandler(
427     std::make_unique<LLVMLinkDiagnosticHandler>(), true);
428   cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
429 
430   if (!DisableDITypeMap)
431     Context.enableDebugTypeODRUniquing();
432 
433   auto Composite = std::make_unique<Module>("llvm-link", Context);
434   Linker L(*Composite);
435 
436   unsigned Flags = Linker::Flags::None;
437   if (OnlyNeeded)
438     Flags |= Linker::Flags::LinkOnlyNeeded;
439 
440   // First add all the regular input files
441   if (!linkFiles(argv[0], Context, L, InputFilenames, Flags))
442     return 1;
443 
444   // Next the -override ones.
445   if (!linkFiles(argv[0], Context, L, OverridingInputs,
446                  Flags | Linker::Flags::OverrideFromSrc))
447     return 1;
448 
449   // Import any functions requested via -import
450   if (!importFunctions(argv[0], *Composite))
451     return 1;
452 
453   if (DumpAsm)
454     errs() << "Here's the assembly:\n" << *Composite;
455 
456   std::error_code EC;
457   ToolOutputFile Out(OutputFilename, EC, sys::fs::OF_None);
458   if (EC) {
459     WithColor::error() << EC.message() << '\n';
460     return 1;
461   }
462 
463   if (verifyModule(*Composite, &errs())) {
464     errs() << argv[0] << ": ";
465     WithColor::error() << "linked module is broken!\n";
466     return 1;
467   }
468 
469   if (Verbose)
470     errs() << "Writing bitcode...\n";
471   if (OutputAssembly) {
472     Composite->print(Out.os(), nullptr, PreserveAssemblyUseListOrder);
473   } else if (Force || !CheckBitcodeOutputToConsole(Out.os()))
474     WriteBitcodeToFile(*Composite, Out.os(), PreserveBitcodeUseListOrder);
475 
476   // Declare success.
477   Out.keep();
478 
479   return 0;
480 }
481