xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-link/llvm-link.cpp (revision 8bcb0991864975618c09697b1aca10683346d9f0)
10b57cec5SDimitry Andric //===- llvm-link.cpp - Low-level LLVM linker ------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This utility may be invoked in the following manner:
100b57cec5SDimitry Andric //  llvm-link a.bc b.bc c.bc -o x.bc
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
150b57cec5SDimitry Andric #include "llvm/Bitcode/BitcodeReader.h"
160b57cec5SDimitry Andric #include "llvm/Bitcode/BitcodeWriter.h"
170b57cec5SDimitry Andric #include "llvm/IR/AutoUpgrade.h"
180b57cec5SDimitry Andric #include "llvm/IR/DiagnosticInfo.h"
190b57cec5SDimitry Andric #include "llvm/IR/DiagnosticPrinter.h"
200b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h"
210b57cec5SDimitry Andric #include "llvm/IR/Module.h"
220b57cec5SDimitry Andric #include "llvm/IR/ModuleSummaryIndex.h"
230b57cec5SDimitry Andric #include "llvm/IR/Verifier.h"
240b57cec5SDimitry Andric #include "llvm/IRReader/IRReader.h"
250b57cec5SDimitry Andric #include "llvm/Linker/Linker.h"
260b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
270b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
280b57cec5SDimitry Andric #include "llvm/Support/InitLLVM.h"
290b57cec5SDimitry Andric #include "llvm/Support/Path.h"
300b57cec5SDimitry Andric #include "llvm/Support/SourceMgr.h"
310b57cec5SDimitry Andric #include "llvm/Support/SystemUtils.h"
320b57cec5SDimitry Andric #include "llvm/Support/ToolOutputFile.h"
330b57cec5SDimitry Andric #include "llvm/Support/WithColor.h"
340b57cec5SDimitry Andric #include "llvm/Transforms/IPO/FunctionImport.h"
350b57cec5SDimitry Andric #include "llvm/Transforms/IPO/Internalize.h"
360b57cec5SDimitry Andric #include "llvm/Transforms/Utils/FunctionImportUtils.h"
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric #include <memory>
390b57cec5SDimitry Andric #include <utility>
400b57cec5SDimitry Andric using namespace llvm;
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric static cl::list<std::string>
430b57cec5SDimitry Andric InputFilenames(cl::Positional, cl::OneOrMore,
440b57cec5SDimitry Andric                cl::desc("<input bitcode files>"));
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric static cl::list<std::string> OverridingInputs(
470b57cec5SDimitry Andric     "override", cl::ZeroOrMore, cl::value_desc("filename"),
480b57cec5SDimitry Andric     cl::desc(
490b57cec5SDimitry Andric         "input bitcode file which can override previously defined symbol(s)"));
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric // Option to simulate function importing for testing. This enables using
520b57cec5SDimitry Andric // llvm-link to simulate ThinLTO backend processes.
530b57cec5SDimitry Andric static cl::list<std::string> Imports(
540b57cec5SDimitry Andric     "import", cl::ZeroOrMore, cl::value_desc("function:filename"),
550b57cec5SDimitry Andric     cl::desc("Pair of function name and filename, where function should be "
560b57cec5SDimitry Andric              "imported from bitcode in filename"));
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric // Option to support testing of function importing. The module summary
590b57cec5SDimitry Andric // must be specified in the case were we request imports via the -import
600b57cec5SDimitry Andric // option, as well as when compiling any module with functions that may be
610b57cec5SDimitry Andric // exported (imported by a different llvm-link -import invocation), to ensure
620b57cec5SDimitry Andric // consistent promotion and renaming of locals.
630b57cec5SDimitry Andric static cl::opt<std::string>
640b57cec5SDimitry Andric     SummaryIndex("summary-index", cl::desc("Module summary index filename"),
650b57cec5SDimitry Andric                  cl::init(""), cl::value_desc("filename"));
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric static cl::opt<std::string>
680b57cec5SDimitry Andric OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
690b57cec5SDimitry Andric                cl::value_desc("filename"));
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric static cl::opt<bool>
720b57cec5SDimitry Andric Internalize("internalize", cl::desc("Internalize linked symbols"));
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric static cl::opt<bool>
750b57cec5SDimitry Andric     DisableDITypeMap("disable-debug-info-type-map",
760b57cec5SDimitry Andric                      cl::desc("Don't use a uniquing type map for debug info"));
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric static cl::opt<bool>
790b57cec5SDimitry Andric OnlyNeeded("only-needed", cl::desc("Link only needed symbols"));
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric static cl::opt<bool>
820b57cec5SDimitry Andric Force("f", cl::desc("Enable binary output on terminals"));
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric static cl::opt<bool>
850b57cec5SDimitry Andric     DisableLazyLoad("disable-lazy-loading",
860b57cec5SDimitry Andric                     cl::desc("Disable lazy module loading"));
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric static cl::opt<bool>
890b57cec5SDimitry Andric     OutputAssembly("S", cl::desc("Write output as LLVM assembly"), cl::Hidden);
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric static cl::opt<bool>
920b57cec5SDimitry Andric Verbose("v", cl::desc("Print information about actions taken"));
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric static cl::opt<bool>
950b57cec5SDimitry Andric DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric static cl::opt<bool>
980b57cec5SDimitry Andric SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"),
990b57cec5SDimitry Andric                  cl::init(false));
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric static cl::opt<bool> PreserveBitcodeUseListOrder(
1020b57cec5SDimitry Andric     "preserve-bc-uselistorder",
1030b57cec5SDimitry Andric     cl::desc("Preserve use-list order when writing LLVM bitcode."),
1040b57cec5SDimitry Andric     cl::init(true), cl::Hidden);
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric static cl::opt<bool> PreserveAssemblyUseListOrder(
1070b57cec5SDimitry Andric     "preserve-ll-uselistorder",
1080b57cec5SDimitry Andric     cl::desc("Preserve use-list order when writing LLVM assembly."),
1090b57cec5SDimitry Andric     cl::init(false), cl::Hidden);
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric static ExitOnError ExitOnErr;
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric // Read the specified bitcode file in and return it. This routine searches the
1140b57cec5SDimitry Andric // link path for the specified file to try to find it...
1150b57cec5SDimitry Andric //
1160b57cec5SDimitry Andric static std::unique_ptr<Module> loadFile(const char *argv0,
1170b57cec5SDimitry Andric                                         const std::string &FN,
1180b57cec5SDimitry Andric                                         LLVMContext &Context,
1190b57cec5SDimitry Andric                                         bool MaterializeMetadata = true) {
1200b57cec5SDimitry Andric   SMDiagnostic Err;
1210b57cec5SDimitry Andric   if (Verbose)
1220b57cec5SDimitry Andric     errs() << "Loading '" << FN << "'\n";
1230b57cec5SDimitry Andric   std::unique_ptr<Module> Result;
1240b57cec5SDimitry Andric   if (DisableLazyLoad)
1250b57cec5SDimitry Andric     Result = parseIRFile(FN, Err, Context);
1260b57cec5SDimitry Andric   else
1270b57cec5SDimitry Andric     Result = getLazyIRFileModule(FN, Err, Context, !MaterializeMetadata);
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric   if (!Result) {
1300b57cec5SDimitry Andric     Err.print(argv0, errs());
1310b57cec5SDimitry Andric     return nullptr;
1320b57cec5SDimitry Andric   }
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric   if (MaterializeMetadata) {
1350b57cec5SDimitry Andric     ExitOnErr(Result->materializeMetadata());
1360b57cec5SDimitry Andric     UpgradeDebugInfo(*Result);
1370b57cec5SDimitry Andric   }
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric   return Result;
1400b57cec5SDimitry Andric }
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric namespace {
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric /// Helper to load on demand a Module from file and cache it for subsequent
1450b57cec5SDimitry Andric /// queries during function importing.
1460b57cec5SDimitry Andric class ModuleLazyLoaderCache {
1470b57cec5SDimitry Andric   /// Cache of lazily loaded module for import.
1480b57cec5SDimitry Andric   StringMap<std::unique_ptr<Module>> ModuleMap;
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric   /// Retrieve a Module from the cache or lazily load it on demand.
1510b57cec5SDimitry Andric   std::function<std::unique_ptr<Module>(const char *argv0,
1520b57cec5SDimitry Andric                                         const std::string &FileName)>
1530b57cec5SDimitry Andric       createLazyModule;
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric public:
1560b57cec5SDimitry Andric   /// Create the loader, Module will be initialized in \p Context.
1570b57cec5SDimitry Andric   ModuleLazyLoaderCache(std::function<std::unique_ptr<Module>(
1580b57cec5SDimitry Andric                             const char *argv0, const std::string &FileName)>
1590b57cec5SDimitry Andric                             createLazyModule)
1600b57cec5SDimitry Andric       : createLazyModule(std::move(createLazyModule)) {}
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric   /// Retrieve a Module from the cache or lazily load it on demand.
1630b57cec5SDimitry Andric   Module &operator()(const char *argv0, const std::string &FileName);
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric   std::unique_ptr<Module> takeModule(const std::string &FileName) {
1660b57cec5SDimitry Andric     auto I = ModuleMap.find(FileName);
1670b57cec5SDimitry Andric     assert(I != ModuleMap.end());
1680b57cec5SDimitry Andric     std::unique_ptr<Module> Ret = std::move(I->second);
1690b57cec5SDimitry Andric     ModuleMap.erase(I);
1700b57cec5SDimitry Andric     return Ret;
1710b57cec5SDimitry Andric   }
1720b57cec5SDimitry Andric };
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric // Get a Module for \p FileName from the cache, or load it lazily.
1750b57cec5SDimitry Andric Module &ModuleLazyLoaderCache::operator()(const char *argv0,
1760b57cec5SDimitry Andric                                           const std::string &Identifier) {
1770b57cec5SDimitry Andric   auto &Module = ModuleMap[Identifier];
1780b57cec5SDimitry Andric   if (!Module)
1790b57cec5SDimitry Andric     Module = createLazyModule(argv0, Identifier);
1800b57cec5SDimitry Andric   return *Module;
1810b57cec5SDimitry Andric }
1820b57cec5SDimitry Andric } // anonymous namespace
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric namespace {
1850b57cec5SDimitry Andric struct LLVMLinkDiagnosticHandler : public DiagnosticHandler {
1860b57cec5SDimitry Andric   bool handleDiagnostics(const DiagnosticInfo &DI) override {
1870b57cec5SDimitry Andric     unsigned Severity = DI.getSeverity();
1880b57cec5SDimitry Andric     switch (Severity) {
1890b57cec5SDimitry Andric     case DS_Error:
1900b57cec5SDimitry Andric       WithColor::error();
1910b57cec5SDimitry Andric       break;
1920b57cec5SDimitry Andric     case DS_Warning:
1930b57cec5SDimitry Andric       if (SuppressWarnings)
1940b57cec5SDimitry Andric         return true;
1950b57cec5SDimitry Andric       WithColor::warning();
1960b57cec5SDimitry Andric       break;
1970b57cec5SDimitry Andric     case DS_Remark:
1980b57cec5SDimitry Andric     case DS_Note:
1990b57cec5SDimitry Andric       llvm_unreachable("Only expecting warnings and errors");
2000b57cec5SDimitry Andric     }
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric     DiagnosticPrinterRawOStream DP(errs());
2030b57cec5SDimitry Andric     DI.print(DP);
2040b57cec5SDimitry Andric     errs() << '\n';
2050b57cec5SDimitry Andric     return true;
2060b57cec5SDimitry Andric   }
2070b57cec5SDimitry Andric };
2080b57cec5SDimitry Andric }
2090b57cec5SDimitry Andric 
2100b57cec5SDimitry Andric /// Import any functions requested via the -import option.
2110b57cec5SDimitry Andric static bool importFunctions(const char *argv0, Module &DestModule) {
2120b57cec5SDimitry Andric   if (SummaryIndex.empty())
2130b57cec5SDimitry Andric     return true;
2140b57cec5SDimitry Andric   std::unique_ptr<ModuleSummaryIndex> Index =
2150b57cec5SDimitry Andric       ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex));
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric   // Map of Module -> List of globals to import from the Module
2180b57cec5SDimitry Andric   FunctionImporter::ImportMapTy ImportList;
2190b57cec5SDimitry Andric 
2200b57cec5SDimitry Andric   auto ModuleLoader = [&DestModule](const char *argv0,
2210b57cec5SDimitry Andric                                     const std::string &Identifier) {
2220b57cec5SDimitry Andric     return loadFile(argv0, Identifier, DestModule.getContext(), false);
2230b57cec5SDimitry Andric   };
2240b57cec5SDimitry Andric 
2250b57cec5SDimitry Andric   ModuleLazyLoaderCache ModuleLoaderCache(ModuleLoader);
2260b57cec5SDimitry Andric   for (const auto &Import : Imports) {
2270b57cec5SDimitry Andric     // Identify the requested function and its bitcode source file.
2280b57cec5SDimitry Andric     size_t Idx = Import.find(':');
2290b57cec5SDimitry Andric     if (Idx == std::string::npos) {
2300b57cec5SDimitry Andric       errs() << "Import parameter bad format: " << Import << "\n";
2310b57cec5SDimitry Andric       return false;
2320b57cec5SDimitry Andric     }
2330b57cec5SDimitry Andric     std::string FunctionName = Import.substr(0, Idx);
2340b57cec5SDimitry Andric     std::string FileName = Import.substr(Idx + 1, std::string::npos);
2350b57cec5SDimitry Andric 
2360b57cec5SDimitry Andric     // Load the specified source module.
2370b57cec5SDimitry Andric     auto &SrcModule = ModuleLoaderCache(argv0, FileName);
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric     if (verifyModule(SrcModule, &errs())) {
2400b57cec5SDimitry Andric       errs() << argv0 << ": " << FileName;
2410b57cec5SDimitry Andric       WithColor::error() << "input module is broken!\n";
2420b57cec5SDimitry Andric       return false;
2430b57cec5SDimitry Andric     }
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric     Function *F = SrcModule.getFunction(FunctionName);
2460b57cec5SDimitry Andric     if (!F) {
2470b57cec5SDimitry Andric       errs() << "Ignoring import request for non-existent function "
2480b57cec5SDimitry Andric              << FunctionName << " from " << FileName << "\n";
2490b57cec5SDimitry Andric       continue;
2500b57cec5SDimitry Andric     }
2510b57cec5SDimitry Andric     // We cannot import weak_any functions without possibly affecting the
2520b57cec5SDimitry Andric     // order they are seen and selected by the linker, changing program
2530b57cec5SDimitry Andric     // semantics.
2540b57cec5SDimitry Andric     if (F->hasWeakAnyLinkage()) {
2550b57cec5SDimitry Andric       errs() << "Ignoring import request for weak-any function " << FunctionName
2560b57cec5SDimitry Andric              << " from " << FileName << "\n";
2570b57cec5SDimitry Andric       continue;
2580b57cec5SDimitry Andric     }
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric     if (Verbose)
2610b57cec5SDimitry Andric       errs() << "Importing " << FunctionName << " from " << FileName << "\n";
2620b57cec5SDimitry Andric 
2630b57cec5SDimitry Andric     auto &Entry = ImportList[FileName];
2640b57cec5SDimitry Andric     Entry.insert(F->getGUID());
2650b57cec5SDimitry Andric   }
2660b57cec5SDimitry Andric   auto CachedModuleLoader = [&](StringRef Identifier) {
2670b57cec5SDimitry Andric     return ModuleLoaderCache.takeModule(Identifier);
2680b57cec5SDimitry Andric   };
2690b57cec5SDimitry Andric   FunctionImporter Importer(*Index, CachedModuleLoader);
2700b57cec5SDimitry Andric   ExitOnErr(Importer.importFunctions(DestModule, ImportList));
2710b57cec5SDimitry Andric 
2720b57cec5SDimitry Andric   return true;
2730b57cec5SDimitry Andric }
2740b57cec5SDimitry Andric 
2750b57cec5SDimitry Andric static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L,
2760b57cec5SDimitry Andric                       const cl::list<std::string> &Files,
2770b57cec5SDimitry Andric                       unsigned Flags) {
2780b57cec5SDimitry Andric   // Filter out flags that don't apply to the first file we load.
2790b57cec5SDimitry Andric   unsigned ApplicableFlags = Flags & Linker::Flags::OverrideFromSrc;
2800b57cec5SDimitry Andric   // Similar to some flags, internalization doesn't apply to the first file.
2810b57cec5SDimitry Andric   bool InternalizeLinkedSymbols = false;
2820b57cec5SDimitry Andric   for (const auto &File : Files) {
2830b57cec5SDimitry Andric     std::unique_ptr<Module> M = loadFile(argv0, File, Context);
2840b57cec5SDimitry Andric     if (!M.get()) {
2850b57cec5SDimitry Andric       errs() << argv0 << ": ";
2860b57cec5SDimitry Andric       WithColor::error() << " loading file '" << File << "'\n";
2870b57cec5SDimitry Andric       return false;
2880b57cec5SDimitry Andric     }
2890b57cec5SDimitry Andric 
2900b57cec5SDimitry Andric     // Note that when ODR merging types cannot verify input files in here When
2910b57cec5SDimitry Andric     // doing that debug metadata in the src module might already be pointing to
2920b57cec5SDimitry Andric     // the destination.
2930b57cec5SDimitry Andric     if (DisableDITypeMap && verifyModule(*M, &errs())) {
2940b57cec5SDimitry Andric       errs() << argv0 << ": " << File << ": ";
2950b57cec5SDimitry Andric       WithColor::error() << "input module is broken!\n";
2960b57cec5SDimitry Andric       return false;
2970b57cec5SDimitry Andric     }
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric     // If a module summary index is supplied, load it so linkInModule can treat
3000b57cec5SDimitry Andric     // local functions/variables as exported and promote if necessary.
3010b57cec5SDimitry Andric     if (!SummaryIndex.empty()) {
3020b57cec5SDimitry Andric       std::unique_ptr<ModuleSummaryIndex> Index =
3030b57cec5SDimitry Andric           ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex));
3040b57cec5SDimitry Andric 
3050b57cec5SDimitry Andric       // Conservatively mark all internal values as promoted, since this tool
3060b57cec5SDimitry Andric       // does not do the ThinLink that would normally determine what values to
3070b57cec5SDimitry Andric       // promote.
3080b57cec5SDimitry Andric       for (auto &I : *Index) {
3090b57cec5SDimitry Andric         for (auto &S : I.second.SummaryList) {
3100b57cec5SDimitry Andric           if (GlobalValue::isLocalLinkage(S->linkage()))
3110b57cec5SDimitry Andric             S->setLinkage(GlobalValue::ExternalLinkage);
3120b57cec5SDimitry Andric         }
3130b57cec5SDimitry Andric       }
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric       // Promotion
3160b57cec5SDimitry Andric       if (renameModuleForThinLTO(*M, *Index))
3170b57cec5SDimitry Andric         return true;
3180b57cec5SDimitry Andric     }
3190b57cec5SDimitry Andric 
3200b57cec5SDimitry Andric     if (Verbose)
3210b57cec5SDimitry Andric       errs() << "Linking in '" << File << "'\n";
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric     bool Err = false;
3240b57cec5SDimitry Andric     if (InternalizeLinkedSymbols) {
3250b57cec5SDimitry Andric       Err = L.linkInModule(
3260b57cec5SDimitry Andric           std::move(M), ApplicableFlags, [](Module &M, const StringSet<> &GVS) {
3270b57cec5SDimitry Andric             internalizeModule(M, [&GVS](const GlobalValue &GV) {
3280b57cec5SDimitry Andric               return !GV.hasName() || (GVS.count(GV.getName()) == 0);
3290b57cec5SDimitry Andric             });
3300b57cec5SDimitry Andric           });
3310b57cec5SDimitry Andric     } else {
3320b57cec5SDimitry Andric       Err = L.linkInModule(std::move(M), ApplicableFlags);
3330b57cec5SDimitry Andric     }
3340b57cec5SDimitry Andric 
3350b57cec5SDimitry Andric     if (Err)
3360b57cec5SDimitry Andric       return false;
3370b57cec5SDimitry Andric 
3380b57cec5SDimitry Andric     // Internalization applies to linking of subsequent files.
3390b57cec5SDimitry Andric     InternalizeLinkedSymbols = Internalize;
3400b57cec5SDimitry Andric 
3410b57cec5SDimitry Andric     // All linker flags apply to linking of subsequent files.
3420b57cec5SDimitry Andric     ApplicableFlags = Flags;
3430b57cec5SDimitry Andric   }
3440b57cec5SDimitry Andric 
3450b57cec5SDimitry Andric   return true;
3460b57cec5SDimitry Andric }
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric int main(int argc, char **argv) {
3490b57cec5SDimitry Andric   InitLLVM X(argc, argv);
3500b57cec5SDimitry Andric   ExitOnErr.setBanner(std::string(argv[0]) + ": ");
3510b57cec5SDimitry Andric 
3520b57cec5SDimitry Andric   LLVMContext Context;
3530b57cec5SDimitry Andric   Context.setDiagnosticHandler(
354*8bcb0991SDimitry Andric     std::make_unique<LLVMLinkDiagnosticHandler>(), true);
3550b57cec5SDimitry Andric   cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
3560b57cec5SDimitry Andric 
3570b57cec5SDimitry Andric   if (!DisableDITypeMap)
3580b57cec5SDimitry Andric     Context.enableDebugTypeODRUniquing();
3590b57cec5SDimitry Andric 
360*8bcb0991SDimitry Andric   auto Composite = std::make_unique<Module>("llvm-link", Context);
3610b57cec5SDimitry Andric   Linker L(*Composite);
3620b57cec5SDimitry Andric 
3630b57cec5SDimitry Andric   unsigned Flags = Linker::Flags::None;
3640b57cec5SDimitry Andric   if (OnlyNeeded)
3650b57cec5SDimitry Andric     Flags |= Linker::Flags::LinkOnlyNeeded;
3660b57cec5SDimitry Andric 
3670b57cec5SDimitry Andric   // First add all the regular input files
3680b57cec5SDimitry Andric   if (!linkFiles(argv[0], Context, L, InputFilenames, Flags))
3690b57cec5SDimitry Andric     return 1;
3700b57cec5SDimitry Andric 
3710b57cec5SDimitry Andric   // Next the -override ones.
3720b57cec5SDimitry Andric   if (!linkFiles(argv[0], Context, L, OverridingInputs,
3730b57cec5SDimitry Andric                  Flags | Linker::Flags::OverrideFromSrc))
3740b57cec5SDimitry Andric     return 1;
3750b57cec5SDimitry Andric 
3760b57cec5SDimitry Andric   // Import any functions requested via -import
3770b57cec5SDimitry Andric   if (!importFunctions(argv[0], *Composite))
3780b57cec5SDimitry Andric     return 1;
3790b57cec5SDimitry Andric 
3800b57cec5SDimitry Andric   if (DumpAsm)
3810b57cec5SDimitry Andric     errs() << "Here's the assembly:\n" << *Composite;
3820b57cec5SDimitry Andric 
3830b57cec5SDimitry Andric   std::error_code EC;
384*8bcb0991SDimitry Andric   ToolOutputFile Out(OutputFilename, EC, sys::fs::OF_None);
3850b57cec5SDimitry Andric   if (EC) {
3860b57cec5SDimitry Andric     WithColor::error() << EC.message() << '\n';
3870b57cec5SDimitry Andric     return 1;
3880b57cec5SDimitry Andric   }
3890b57cec5SDimitry Andric 
3900b57cec5SDimitry Andric   if (verifyModule(*Composite, &errs())) {
3910b57cec5SDimitry Andric     errs() << argv[0] << ": ";
3920b57cec5SDimitry Andric     WithColor::error() << "linked module is broken!\n";
3930b57cec5SDimitry Andric     return 1;
3940b57cec5SDimitry Andric   }
3950b57cec5SDimitry Andric 
3960b57cec5SDimitry Andric   if (Verbose)
3970b57cec5SDimitry Andric     errs() << "Writing bitcode...\n";
3980b57cec5SDimitry Andric   if (OutputAssembly) {
3990b57cec5SDimitry Andric     Composite->print(Out.os(), nullptr, PreserveAssemblyUseListOrder);
4000b57cec5SDimitry Andric   } else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
4010b57cec5SDimitry Andric     WriteBitcodeToFile(*Composite, Out.os(), PreserveBitcodeUseListOrder);
4020b57cec5SDimitry Andric 
4030b57cec5SDimitry Andric   // Declare success.
4040b57cec5SDimitry Andric   Out.keep();
4050b57cec5SDimitry Andric 
4060b57cec5SDimitry Andric   return 0;
4070b57cec5SDimitry Andric }
408