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