xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-link/llvm-link.cpp (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
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 ExitOnError ExitOnErr;
133 
134 // Read the specified bitcode file in and return it. This routine searches the
135 // link path for the specified file to try to find it...
136 //
137 static std::unique_ptr<Module> loadFile(const char *argv0,
138                                         std::unique_ptr<MemoryBuffer> Buffer,
139                                         LLVMContext &Context,
140                                         bool MaterializeMetadata = true) {
141   SMDiagnostic Err;
142   if (Verbose)
143     errs() << "Loading '" << Buffer->getBufferIdentifier() << "'\n";
144   std::unique_ptr<Module> Result;
145   if (DisableLazyLoad)
146     Result = parseIR(*Buffer, Err, Context);
147   else
148     Result =
149         getLazyIRModule(std::move(Buffer), Err, Context, !MaterializeMetadata);
150 
151   if (!Result) {
152     Err.print(argv0, errs());
153     return nullptr;
154   }
155 
156   if (MaterializeMetadata) {
157     ExitOnErr(Result->materializeMetadata());
158     UpgradeDebugInfo(*Result);
159   }
160 
161   return Result;
162 }
163 
164 static std::unique_ptr<Module> loadArFile(const char *Argv0,
165                                           std::unique_ptr<MemoryBuffer> Buffer,
166                                           LLVMContext &Context) {
167   std::unique_ptr<Module> Result(new Module("ArchiveModule", Context));
168   StringRef ArchiveName = Buffer->getBufferIdentifier();
169   if (Verbose)
170     errs() << "Reading library archive file '" << ArchiveName
171            << "' to memory\n";
172   Expected<std::unique_ptr<object::Archive>> ArchiveOrError =
173       object::Archive::create(Buffer->getMemBufferRef());
174   if (!ArchiveOrError)
175     ExitOnErr(ArchiveOrError.takeError());
176 
177   std::unique_ptr<object::Archive> Archive = std::move(ArchiveOrError.get());
178 
179   Linker L(*Result);
180   Error Err = Error::success();
181   for (const object::Archive::Child &C : Archive->children(Err)) {
182     Expected<StringRef> Ename = C.getName();
183     if (Error E = Ename.takeError()) {
184       errs() << Argv0 << ": ";
185       WithColor::error() << " failed to read name of archive member"
186                          << ArchiveName << "'\n";
187       return nullptr;
188     }
189     std::string ChildName = Ename.get().str();
190     if (Verbose)
191       errs() << "Parsing member '" << ChildName
192              << "' of archive library to module.\n";
193     SMDiagnostic ParseErr;
194     Expected<MemoryBufferRef> MemBuf = C.getMemoryBufferRef();
195     if (Error E = MemBuf.takeError()) {
196       errs() << Argv0 << ": ";
197       WithColor::error() << " loading memory for member '" << ChildName
198                          << "' of archive library failed'" << ArchiveName
199                          << "'\n";
200       return nullptr;
201     };
202 
203     if (!isBitcode(reinterpret_cast<const unsigned char *>(
204                        MemBuf.get().getBufferStart()),
205                    reinterpret_cast<const unsigned char *>(
206                        MemBuf.get().getBufferEnd()))) {
207       if (IgnoreNonBitcode)
208         continue;
209       errs() << Argv0 << ": ";
210       WithColor::error() << "  member of archive is not a bitcode file: '"
211                          << ChildName << "'\n";
212       return nullptr;
213     }
214 
215     std::unique_ptr<Module> M;
216     if (DisableLazyLoad)
217       M = parseIR(MemBuf.get(), ParseErr, Context);
218     else
219       M = getLazyIRModule(MemoryBuffer::getMemBuffer(MemBuf.get(), false),
220                           ParseErr, Context);
221 
222     if (!M.get()) {
223       errs() << Argv0 << ": ";
224       WithColor::error() << " parsing member '" << ChildName
225                          << "' of archive library failed'" << ArchiveName
226                          << "'\n";
227       return nullptr;
228     }
229     if (Verbose)
230       errs() << "Linking member '" << ChildName << "' of archive library.\n";
231     if (L.linkInModule(std::move(M)))
232       return nullptr;
233   } // end for each child
234   ExitOnErr(std::move(Err));
235   return Result;
236 }
237 
238 namespace {
239 
240 /// Helper to load on demand a Module from file and cache it for subsequent
241 /// queries during function importing.
242 class ModuleLazyLoaderCache {
243   /// Cache of lazily loaded module for import.
244   StringMap<std::unique_ptr<Module>> ModuleMap;
245 
246   /// Retrieve a Module from the cache or lazily load it on demand.
247   std::function<std::unique_ptr<Module>(const char *argv0,
248                                         const std::string &FileName)>
249       createLazyModule;
250 
251 public:
252   /// Create the loader, Module will be initialized in \p Context.
253   ModuleLazyLoaderCache(std::function<std::unique_ptr<Module>(
254                             const char *argv0, const std::string &FileName)>
255                             createLazyModule)
256       : createLazyModule(std::move(createLazyModule)) {}
257 
258   /// Retrieve a Module from the cache or lazily load it on demand.
259   Module &operator()(const char *argv0, const std::string &FileName);
260 
261   std::unique_ptr<Module> takeModule(const std::string &FileName) {
262     auto I = ModuleMap.find(FileName);
263     assert(I != ModuleMap.end());
264     std::unique_ptr<Module> Ret = std::move(I->second);
265     ModuleMap.erase(I);
266     return Ret;
267   }
268 };
269 
270 // Get a Module for \p FileName from the cache, or load it lazily.
271 Module &ModuleLazyLoaderCache::operator()(const char *argv0,
272                                           const std::string &Identifier) {
273   auto &Module = ModuleMap[Identifier];
274   if (!Module) {
275     Module = createLazyModule(argv0, Identifier);
276     assert(Module && "Failed to create lazy module!");
277   }
278   return *Module;
279 }
280 } // anonymous namespace
281 
282 namespace {
283 struct LLVMLinkDiagnosticHandler : public DiagnosticHandler {
284   bool handleDiagnostics(const DiagnosticInfo &DI) override {
285     unsigned Severity = DI.getSeverity();
286     switch (Severity) {
287     case DS_Error:
288       WithColor::error();
289       break;
290     case DS_Warning:
291       if (SuppressWarnings)
292         return true;
293       WithColor::warning();
294       break;
295     case DS_Remark:
296     case DS_Note:
297       llvm_unreachable("Only expecting warnings and errors");
298     }
299 
300     DiagnosticPrinterRawOStream DP(errs());
301     DI.print(DP);
302     errs() << '\n';
303     return true;
304   }
305 };
306 } // namespace
307 
308 /// Import any functions requested via the -import option.
309 static bool importFunctions(const char *argv0, Module &DestModule) {
310   if (SummaryIndex.empty())
311     return true;
312   std::unique_ptr<ModuleSummaryIndex> Index =
313       ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex));
314 
315   // Map of Module -> List of globals to import from the Module
316   FunctionImporter::ImportMapTy ImportList;
317 
318   auto ModuleLoader = [&DestModule](const char *argv0,
319                                     const std::string &Identifier) {
320     std::unique_ptr<MemoryBuffer> Buffer =
321         ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(Identifier)));
322     return loadFile(argv0, std::move(Buffer), DestModule.getContext(), false);
323   };
324 
325   ModuleLazyLoaderCache ModuleLoaderCache(ModuleLoader);
326   for (const auto &Import : Imports) {
327     // Identify the requested function and its bitcode source file.
328     size_t Idx = Import.find(':');
329     if (Idx == std::string::npos) {
330       errs() << "Import parameter bad format: " << Import << "\n";
331       return false;
332     }
333     std::string FunctionName = Import.substr(0, Idx);
334     std::string FileName = Import.substr(Idx + 1, std::string::npos);
335 
336     // Load the specified source module.
337     auto &SrcModule = ModuleLoaderCache(argv0, FileName);
338 
339     if (!NoVerify && verifyModule(SrcModule, &errs())) {
340       errs() << argv0 << ": " << FileName;
341       WithColor::error() << "input module is broken!\n";
342       return false;
343     }
344 
345     Function *F = SrcModule.getFunction(FunctionName);
346     if (!F) {
347       errs() << "Ignoring import request for non-existent function "
348              << FunctionName << " from " << FileName << "\n";
349       continue;
350     }
351     // We cannot import weak_any functions without possibly affecting the
352     // order they are seen and selected by the linker, changing program
353     // semantics.
354     if (F->hasWeakAnyLinkage()) {
355       errs() << "Ignoring import request for weak-any function " << FunctionName
356              << " from " << FileName << "\n";
357       continue;
358     }
359 
360     if (Verbose)
361       errs() << "Importing " << FunctionName << " from " << FileName << "\n";
362 
363     auto &Entry = ImportList[FileName];
364     Entry.insert(F->getGUID());
365   }
366   auto CachedModuleLoader = [&](StringRef Identifier) {
367     return ModuleLoaderCache.takeModule(std::string(Identifier));
368   };
369   FunctionImporter Importer(*Index, CachedModuleLoader,
370                             /*ClearDSOLocalOnDeclarations=*/false);
371   ExitOnErr(Importer.importFunctions(DestModule, ImportList));
372 
373   return true;
374 }
375 
376 static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L,
377                       const cl::list<std::string> &Files, unsigned Flags) {
378   // Filter out flags that don't apply to the first file we load.
379   unsigned ApplicableFlags = Flags & Linker::Flags::OverrideFromSrc;
380   // Similar to some flags, internalization doesn't apply to the first file.
381   bool InternalizeLinkedSymbols = false;
382   for (const auto &File : Files) {
383     std::unique_ptr<MemoryBuffer> Buffer =
384         ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(File)));
385 
386     std::unique_ptr<Module> M =
387         identify_magic(Buffer->getBuffer()) == file_magic::archive
388             ? loadArFile(argv0, std::move(Buffer), Context)
389             : loadFile(argv0, std::move(Buffer), Context);
390     if (!M.get()) {
391       errs() << argv0 << ": ";
392       WithColor::error() << " loading file '" << File << "'\n";
393       return false;
394     }
395 
396     // Note that when ODR merging types cannot verify input files in here When
397     // doing that debug metadata in the src module might already be pointing to
398     // the destination.
399     if (DisableDITypeMap && !NoVerify && verifyModule(*M, &errs())) {
400       errs() << argv0 << ": " << File << ": ";
401       WithColor::error() << "input module is broken!\n";
402       return false;
403     }
404 
405     // If a module summary index is supplied, load it so linkInModule can treat
406     // local functions/variables as exported and promote if necessary.
407     if (!SummaryIndex.empty()) {
408       std::unique_ptr<ModuleSummaryIndex> Index =
409           ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex));
410 
411       // Conservatively mark all internal values as promoted, since this tool
412       // does not do the ThinLink that would normally determine what values to
413       // promote.
414       for (auto &I : *Index) {
415         for (auto &S : I.second.SummaryList) {
416           if (GlobalValue::isLocalLinkage(S->linkage()))
417             S->setLinkage(GlobalValue::ExternalLinkage);
418         }
419       }
420 
421       // Promotion
422       if (renameModuleForThinLTO(*M, *Index,
423                                  /*ClearDSOLocalOnDeclarations=*/false))
424         return true;
425     }
426 
427     if (Verbose)
428       errs() << "Linking in '" << File << "'\n";
429 
430     bool Err = false;
431     if (InternalizeLinkedSymbols) {
432       Err = L.linkInModule(
433           std::move(M), ApplicableFlags, [](Module &M, const StringSet<> &GVS) {
434             internalizeModule(M, [&GVS](const GlobalValue &GV) {
435               return !GV.hasName() || (GVS.count(GV.getName()) == 0);
436             });
437           });
438     } else {
439       Err = L.linkInModule(std::move(M), ApplicableFlags);
440     }
441 
442     if (Err)
443       return false;
444 
445     // Internalization applies to linking of subsequent files.
446     InternalizeLinkedSymbols = Internalize;
447 
448     // All linker flags apply to linking of subsequent files.
449     ApplicableFlags = Flags;
450   }
451 
452   return true;
453 }
454 
455 int main(int argc, char **argv) {
456   InitLLVM X(argc, argv);
457   ExitOnErr.setBanner(std::string(argv[0]) + ": ");
458 
459   cl::HideUnrelatedOptions({&LinkCategory, &getColorCategory()});
460   cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
461 
462   LLVMContext Context;
463   Context.setDiagnosticHandler(std::make_unique<LLVMLinkDiagnosticHandler>(),
464                                true);
465 
466   if (!DisableDITypeMap)
467     Context.enableDebugTypeODRUniquing();
468 
469   auto Composite = std::make_unique<Module>("llvm-link", Context);
470   Linker L(*Composite);
471 
472   unsigned Flags = Linker::Flags::None;
473   if (OnlyNeeded)
474     Flags |= Linker::Flags::LinkOnlyNeeded;
475 
476   // First add all the regular input files
477   if (!linkFiles(argv[0], Context, L, InputFilenames, Flags))
478     return 1;
479 
480   // Next the -override ones.
481   if (!linkFiles(argv[0], Context, L, OverridingInputs,
482                  Flags | Linker::Flags::OverrideFromSrc))
483     return 1;
484 
485   // Import any functions requested via -import
486   if (!importFunctions(argv[0], *Composite))
487     return 1;
488 
489   if (DumpAsm)
490     errs() << "Here's the assembly:\n" << *Composite;
491 
492   std::error_code EC;
493   ToolOutputFile Out(OutputFilename, EC,
494                      OutputAssembly ? sys::fs::OF_TextWithCRLF
495                                     : sys::fs::OF_None);
496   if (EC) {
497     WithColor::error() << EC.message() << '\n';
498     return 1;
499   }
500 
501   if (!NoVerify && verifyModule(*Composite, &errs())) {
502     errs() << argv[0] << ": ";
503     WithColor::error() << "linked module is broken!\n";
504     return 1;
505   }
506 
507   if (Verbose)
508     errs() << "Writing bitcode...\n";
509   if (OutputAssembly) {
510     Composite->print(Out.os(), nullptr, PreserveAssemblyUseListOrder);
511   } else if (Force || !CheckBitcodeOutputToConsole(Out.os()))
512     WriteBitcodeToFile(*Composite, Out.os(), PreserveBitcodeUseListOrder);
513 
514   // Declare success.
515   Out.keep();
516 
517   return 0;
518 }
519