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