2003-09-20 10:42:54 +08:00
|
|
|
//===- llvm-link.cpp - Low-level LLVM linker ------------------------------===//
|
2005-04-22 08:00:37 +08:00
|
|
|
//
|
2003-10-21 01:47:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:44:31 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 08:00:37 +08:00
|
|
|
//
|
2003-10-21 01:47:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-10-13 15:06:23 +08:00
|
|
|
//
|
|
|
|
// This utility may be invoked in the following manner:
|
2003-09-16 02:34:34 +08:00
|
|
|
// llvm-link a.bc b.bc c.bc -o x.bc
|
2001-10-13 15:06:23 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-03-02 05:28:53 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2017-05-02 04:42:32 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeReader.h"
|
2016-11-11 13:34:58 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeWriter.h"
|
2015-03-31 05:36:43 +08:00
|
|
|
#include "llvm/IR/AutoUpgrade.h"
|
2014-10-25 12:06:10 +08:00
|
|
|
#include "llvm/IR/DiagnosticInfo.h"
|
|
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2016-03-15 08:04:37 +08:00
|
|
|
#include "llvm/IR/ModuleSummaryIndex.h"
|
2014-01-13 17:26:24 +08:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2013-03-26 10:25:37 +08:00
|
|
|
#include "llvm/IRReader/IRReader.h"
|
2016-03-15 08:04:37 +08:00
|
|
|
#include "llvm/Linker/Linker.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2014-04-30 07:26:49 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2006-12-06 09:18:01 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2009-03-06 13:34:10 +08:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2010-11-30 02:16:10 +08:00
|
|
|
#include "llvm/Support/Signals.h"
|
2013-03-26 10:25:37 +08:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/Support/SystemUtils.h"
|
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2017-01-04 22:27:31 +08:00
|
|
|
#include "llvm/Transforms/IPO/FunctionImport.h"
|
2017-03-14 02:08:11 +08:00
|
|
|
#include "llvm/Transforms/IPO/Internalize.h"
|
2016-03-19 08:40:31 +08:00
|
|
|
#include "llvm/Transforms/Utils/FunctionImportUtils.h"
|
|
|
|
|
2001-10-13 15:06:23 +08:00
|
|
|
#include <memory>
|
2016-05-27 22:27:24 +08:00
|
|
|
#include <utility>
|
2003-11-12 06:41:34 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2002-07-22 10:10:13 +08:00
|
|
|
static cl::list<std::string>
|
|
|
|
InputFilenames(cl::Positional, cl::OneOrMore,
|
2007-07-06 01:07:56 +08:00
|
|
|
cl::desc("<input bitcode files>"));
|
2002-07-22 10:10:13 +08:00
|
|
|
|
2015-04-22 12:11:00 +08:00
|
|
|
static cl::list<std::string> OverridingInputs(
|
|
|
|
"override", cl::ZeroOrMore, cl::value_desc("filename"),
|
|
|
|
cl::desc(
|
|
|
|
"input bitcode file which can override previously defined symbol(s)"));
|
|
|
|
|
2015-11-03 08:14:15 +08:00
|
|
|
// Option to simulate function importing for testing. This enables using
|
|
|
|
// llvm-link to simulate ThinLTO backend processes.
|
|
|
|
static cl::list<std::string> Imports(
|
|
|
|
"import", cl::ZeroOrMore, cl::value_desc("function:filename"),
|
|
|
|
cl::desc("Pair of function name and filename, where function should be "
|
|
|
|
"imported from bitcode in filename"));
|
|
|
|
|
2016-03-15 08:04:37 +08:00
|
|
|
// Option to support testing of function importing. The module summary
|
2015-11-03 08:14:15 +08:00
|
|
|
// must be specified in the case were we request imports via the -import
|
|
|
|
// option, as well as when compiling any module with functions that may be
|
|
|
|
// exported (imported by a different llvm-link -import invocation), to ensure
|
|
|
|
// consistent promotion and renaming of locals.
|
2016-03-15 08:04:37 +08:00
|
|
|
static cl::opt<std::string>
|
|
|
|
SummaryIndex("summary-index", cl::desc("Module summary index filename"),
|
|
|
|
cl::init(""), cl::value_desc("filename"));
|
2015-11-03 08:14:15 +08:00
|
|
|
|
2002-07-22 10:10:13 +08:00
|
|
|
static cl::opt<std::string>
|
|
|
|
OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
|
|
|
|
cl::value_desc("filename"));
|
|
|
|
|
2015-09-02 01:55:55 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
Internalize("internalize", cl::desc("Internalize linked symbols"));
|
|
|
|
|
2016-04-17 11:58:21 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
DisableDITypeMap("disable-debug-info-type-map",
|
|
|
|
cl::desc("Don't use a uniquing type map for debug info"));
|
|
|
|
|
2015-09-02 01:55:55 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
OnlyNeeded("only-needed", cl::desc("Link only needed symbols"));
|
|
|
|
|
2009-08-25 23:34:52 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
Force("f", cl::desc("Enable binary output on terminals"));
|
2002-07-22 10:10:13 +08:00
|
|
|
|
2009-09-15 23:35:07 +08:00
|
|
|
static cl::opt<bool>
|
2016-09-15 06:29:59 +08:00
|
|
|
DisableLazyLoad("disable-lazy-loading",
|
2016-10-10 01:15:04 +08:00
|
|
|
cl::desc("Disable lazy module loading"));
|
2016-09-15 06:29:59 +08:00
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
OutputAssembly("S", cl::desc("Write output as LLVM assembly"), cl::Hidden);
|
2009-09-15 23:35:07 +08:00
|
|
|
|
2002-07-22 10:10:13 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
Verbose("v", cl::desc("Print information about actions taken"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
DumpAsm("d", cl::desc("Print assembly as linked"), cl::Hidden);
|
|
|
|
|
2014-02-21 06:19:24 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"),
|
|
|
|
cl::init(false));
|
|
|
|
|
2015-04-15 11:14:06 +08:00
|
|
|
static cl::opt<bool> PreserveBitcodeUseListOrder(
|
|
|
|
"preserve-bc-uselistorder",
|
|
|
|
cl::desc("Preserve use-list order when writing LLVM bitcode."),
|
|
|
|
cl::init(true), cl::Hidden);
|
|
|
|
|
|
|
|
static cl::opt<bool> PreserveAssemblyUseListOrder(
|
|
|
|
"preserve-ll-uselistorder",
|
|
|
|
cl::desc("Preserve use-list order when writing LLVM assembly."),
|
|
|
|
cl::init(false), cl::Hidden);
|
|
|
|
|
2016-11-10 01:49:19 +08:00
|
|
|
static ExitOnError ExitOnErr;
|
|
|
|
|
2014-08-27 01:29:46 +08:00
|
|
|
// Read the specified bitcode file in and return it. This routine searches the
|
|
|
|
// link path for the specified file to try to find it...
|
2004-09-13 07:39:42 +08:00
|
|
|
//
|
2015-12-18 01:14:09 +08:00
|
|
|
static std::unique_ptr<Module> loadFile(const char *argv0,
|
|
|
|
const std::string &FN,
|
|
|
|
LLVMContext &Context,
|
|
|
|
bool MaterializeMetadata = true) {
|
2009-09-13 05:55:12 +08:00
|
|
|
SMDiagnostic Err;
|
2013-06-18 01:32:19 +08:00
|
|
|
if (Verbose) errs() << "Loading '" << FN << "'\n";
|
2016-09-15 06:29:59 +08:00
|
|
|
std::unique_ptr<Module> Result;
|
|
|
|
if (DisableLazyLoad)
|
|
|
|
Result = parseIRFile(FN, Err, Context);
|
|
|
|
else
|
|
|
|
Result = getLazyIRFileModule(FN, Err, Context, !MaterializeMetadata);
|
|
|
|
|
2016-03-25 03:52:20 +08:00
|
|
|
if (!Result) {
|
2014-08-27 02:03:35 +08:00
|
|
|
Err.print(argv0, errs());
|
2016-03-25 03:52:20 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2001-10-24 14:23:00 +08:00
|
|
|
|
2015-12-18 01:14:09 +08:00
|
|
|
if (MaterializeMetadata) {
|
2016-11-10 01:49:19 +08:00
|
|
|
ExitOnErr(Result->materializeMetadata());
|
2015-12-18 01:14:09 +08:00
|
|
|
UpgradeDebugInfo(*Result);
|
|
|
|
}
|
2015-03-27 23:55:06 +08:00
|
|
|
|
2014-08-27 02:03:35 +08:00
|
|
|
return Result;
|
2001-10-24 14:23:00 +08:00
|
|
|
}
|
2001-10-13 15:06:23 +08:00
|
|
|
|
2016-03-25 03:52:20 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// Helper to load on demand a Module from file and cache it for subsequent
|
|
|
|
/// queries during function importing.
|
|
|
|
class ModuleLazyLoaderCache {
|
|
|
|
/// Cache of lazily loaded module for import.
|
|
|
|
StringMap<std::unique_ptr<Module>> ModuleMap;
|
|
|
|
|
|
|
|
/// Retrieve a Module from the cache or lazily load it on demand.
|
|
|
|
std::function<std::unique_ptr<Module>(const char *argv0,
|
|
|
|
const std::string &FileName)>
|
|
|
|
createLazyModule;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// Create the loader, Module will be initialized in \p Context.
|
|
|
|
ModuleLazyLoaderCache(std::function<std::unique_ptr<Module>(
|
|
|
|
const char *argv0, const std::string &FileName)>
|
|
|
|
createLazyModule)
|
2016-05-27 22:27:24 +08:00
|
|
|
: createLazyModule(std::move(createLazyModule)) {}
|
2016-03-25 03:52:20 +08:00
|
|
|
|
|
|
|
/// Retrieve a Module from the cache or lazily load it on demand.
|
|
|
|
Module &operator()(const char *argv0, const std::string &FileName);
|
|
|
|
|
|
|
|
std::unique_ptr<Module> takeModule(const std::string &FileName) {
|
|
|
|
auto I = ModuleMap.find(FileName);
|
|
|
|
assert(I != ModuleMap.end());
|
|
|
|
std::unique_ptr<Module> Ret = std::move(I->second);
|
|
|
|
ModuleMap.erase(I);
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Get a Module for \p FileName from the cache, or load it lazily.
|
|
|
|
Module &ModuleLazyLoaderCache::operator()(const char *argv0,
|
|
|
|
const std::string &Identifier) {
|
|
|
|
auto &Module = ModuleMap[Identifier];
|
|
|
|
if (!Module)
|
|
|
|
Module = createLazyModule(argv0, Identifier);
|
|
|
|
return *Module;
|
|
|
|
}
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2017-09-16 04:10:09 +08:00
|
|
|
namespace {
|
|
|
|
struct LLVMLinkDiagnosticHandler : public DiagnosticHandler {
|
|
|
|
bool handleDiagnostics(const DiagnosticInfo &DI) override {
|
|
|
|
unsigned Severity = DI.getSeverity();
|
|
|
|
switch (Severity) {
|
|
|
|
case DS_Error:
|
|
|
|
errs() << "ERROR: ";
|
|
|
|
break;
|
|
|
|
case DS_Warning:
|
|
|
|
if (SuppressWarnings)
|
|
|
|
return true;
|
|
|
|
errs() << "WARNING: ";
|
|
|
|
break;
|
|
|
|
case DS_Remark:
|
|
|
|
case DS_Note:
|
|
|
|
llvm_unreachable("Only expecting warnings and errors");
|
|
|
|
}
|
2017-09-16 03:53:54 +08:00
|
|
|
|
2017-09-16 04:10:09 +08:00
|
|
|
DiagnosticPrinterRawOStream DP(errs());
|
|
|
|
DI.print(DP);
|
|
|
|
errs() << '\n';
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2014-10-25 12:06:10 +08:00
|
|
|
}
|
|
|
|
|
2015-11-03 08:14:15 +08:00
|
|
|
/// Import any functions requested via the -import option.
|
2017-01-04 22:27:31 +08:00
|
|
|
static bool importFunctions(const char *argv0, Module &DestModule) {
|
2016-03-25 03:52:20 +08:00
|
|
|
if (SummaryIndex.empty())
|
|
|
|
return true;
|
2016-11-12 03:50:39 +08:00
|
|
|
std::unique_ptr<ModuleSummaryIndex> Index =
|
|
|
|
ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex));
|
2016-03-25 03:52:20 +08:00
|
|
|
|
|
|
|
// Map of Module -> List of globals to import from the Module
|
2017-01-04 22:27:31 +08:00
|
|
|
FunctionImporter::ImportMapTy ImportList;
|
|
|
|
|
|
|
|
auto ModuleLoader = [&DestModule](const char *argv0,
|
|
|
|
const std::string &Identifier) {
|
|
|
|
return loadFile(argv0, Identifier, DestModule.getContext(), false);
|
2016-03-25 03:52:20 +08:00
|
|
|
};
|
2017-01-04 22:27:31 +08:00
|
|
|
|
2016-03-25 03:52:20 +08:00
|
|
|
ModuleLazyLoaderCache ModuleLoaderCache(ModuleLoader);
|
2015-11-03 08:14:15 +08:00
|
|
|
for (const auto &Import : Imports) {
|
|
|
|
// Identify the requested function and its bitcode source file.
|
|
|
|
size_t Idx = Import.find(':');
|
|
|
|
if (Idx == std::string::npos) {
|
|
|
|
errs() << "Import parameter bad format: " << Import << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::string FunctionName = Import.substr(0, Idx);
|
|
|
|
std::string FileName = Import.substr(Idx + 1, std::string::npos);
|
|
|
|
|
|
|
|
// Load the specified source module.
|
2016-03-25 03:52:20 +08:00
|
|
|
auto &SrcModule = ModuleLoaderCache(argv0, FileName);
|
2015-11-03 08:14:15 +08:00
|
|
|
|
2016-03-25 03:52:20 +08:00
|
|
|
if (verifyModule(SrcModule, &errs())) {
|
2015-11-03 08:14:15 +08:00
|
|
|
errs() << argv0 << ": " << FileName
|
|
|
|
<< ": error: input module is broken!\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-25 03:52:20 +08:00
|
|
|
Function *F = SrcModule.getFunction(FunctionName);
|
2015-11-03 08:14:15 +08:00
|
|
|
if (!F) {
|
|
|
|
errs() << "Ignoring import request for non-existent function "
|
|
|
|
<< FunctionName << " from " << FileName << "\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// We cannot import weak_any functions without possibly affecting the
|
|
|
|
// order they are seen and selected by the linker, changing program
|
|
|
|
// semantics.
|
|
|
|
if (F->hasWeakAnyLinkage()) {
|
|
|
|
errs() << "Ignoring import request for weak-any function " << FunctionName
|
|
|
|
<< " from " << FileName << "\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Verbose)
|
|
|
|
errs() << "Importing " << FunctionName << " from " << FileName << "\n";
|
|
|
|
|
2017-01-04 22:27:31 +08:00
|
|
|
auto &Entry = ImportList[FileName];
|
|
|
|
Entry.insert(std::make_pair(F->getGUID(), /* (Unused) threshold */ 1.0));
|
2015-11-03 08:14:15 +08:00
|
|
|
}
|
2017-01-04 22:27:31 +08:00
|
|
|
auto CachedModuleLoader = [&](StringRef Identifier) {
|
|
|
|
return ModuleLoaderCache.takeModule(Identifier);
|
|
|
|
};
|
|
|
|
FunctionImporter Importer(*Index, CachedModuleLoader);
|
|
|
|
ExitOnErr(Importer.importFunctions(DestModule, ImportList));
|
2016-03-25 03:52:20 +08:00
|
|
|
|
2015-11-03 08:14:15 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-22 12:08:22 +08:00
|
|
|
static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L,
|
2015-04-22 12:11:00 +08:00
|
|
|
const cl::list<std::string> &Files,
|
2015-09-02 01:55:55 +08:00
|
|
|
unsigned Flags) {
|
|
|
|
// Filter out flags that don't apply to the first file we load.
|
|
|
|
unsigned ApplicableFlags = Flags & Linker::Flags::OverrideFromSrc;
|
2017-03-14 02:08:11 +08:00
|
|
|
// Similar to some flags, internalization doesn't apply to the first file.
|
|
|
|
bool InternalizeLinkedSymbols = false;
|
2015-04-22 12:08:22 +08:00
|
|
|
for (const auto &File : Files) {
|
|
|
|
std::unique_ptr<Module> M = loadFile(argv0, File, Context);
|
|
|
|
if (!M.get()) {
|
|
|
|
errs() << argv0 << ": error loading file '" << File << "'\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-30 02:31:48 +08:00
|
|
|
// Note that when ODR merging types cannot verify input files in here When
|
|
|
|
// doing that debug metadata in the src module might already be pointing to
|
|
|
|
// the destination.
|
|
|
|
if (DisableDITypeMap && verifyModule(*M, &errs())) {
|
2015-04-22 12:08:22 +08:00
|
|
|
errs() << argv0 << ": " << File << ": error: input module is broken!\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-15 08:04:37 +08:00
|
|
|
// If a module summary index is supplied, load it so linkInModule can treat
|
2015-11-03 08:14:15 +08:00
|
|
|
// local functions/variables as exported and promote if necessary.
|
2016-03-15 08:04:37 +08:00
|
|
|
if (!SummaryIndex.empty()) {
|
2016-11-12 03:50:39 +08:00
|
|
|
std::unique_ptr<ModuleSummaryIndex> Index =
|
|
|
|
ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex));
|
2016-03-19 08:40:31 +08:00
|
|
|
|
2016-11-15 03:21:41 +08:00
|
|
|
// Conservatively mark all internal values as promoted, since this tool
|
|
|
|
// does not do the ThinLink that would normally determine what values to
|
|
|
|
// promote.
|
|
|
|
for (auto &I : *Index) {
|
2017-05-05 02:03:25 +08:00
|
|
|
for (auto &S : I.second.SummaryList) {
|
2016-11-15 03:21:41 +08:00
|
|
|
if (GlobalValue::isLocalLinkage(S->linkage()))
|
|
|
|
S->setLinkage(GlobalValue::ExternalLinkage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-19 08:40:31 +08:00
|
|
|
// Promotion
|
|
|
|
if (renameModuleForThinLTO(*M, *Index))
|
|
|
|
return true;
|
2015-11-03 08:14:15 +08:00
|
|
|
}
|
|
|
|
|
2015-04-22 12:08:22 +08:00
|
|
|
if (Verbose)
|
|
|
|
errs() << "Linking in '" << File << "'\n";
|
|
|
|
|
2017-03-14 02:08:11 +08:00
|
|
|
bool Err = false;
|
|
|
|
if (InternalizeLinkedSymbols) {
|
|
|
|
Err = L.linkInModule(
|
|
|
|
std::move(M), ApplicableFlags, [](Module &M, const StringSet<> &GVS) {
|
2017-03-14 05:46:14 +08:00
|
|
|
internalizeModule(M, [&GVS](const GlobalValue &GV) {
|
2017-03-14 02:08:11 +08:00
|
|
|
return !GV.hasName() || (GVS.count(GV.getName()) == 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Err = L.linkInModule(std::move(M), ApplicableFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Err)
|
2015-04-22 12:08:22 +08:00
|
|
|
return false;
|
2017-03-14 02:08:11 +08:00
|
|
|
|
|
|
|
// Internalization applies to linking of subsequent files.
|
|
|
|
InternalizeLinkedSymbols = Internalize;
|
|
|
|
|
2015-09-02 01:55:55 +08:00
|
|
|
// All linker flags apply to linking of subsequent files.
|
|
|
|
ApplicableFlags = Flags;
|
2015-04-22 12:08:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2001-10-13 15:06:23 +08:00
|
|
|
int main(int argc, char **argv) {
|
2009-03-06 13:34:10 +08:00
|
|
|
// Print a stack trace if we signal out.
|
2016-06-09 08:53:21 +08:00
|
|
|
sys::PrintStackTraceOnErrorSignal(argv[0]);
|
2009-03-06 13:34:10 +08:00
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
2013-09-19 07:31:10 +08:00
|
|
|
|
2016-11-10 01:49:19 +08:00
|
|
|
ExitOnErr.setBanner(std::string(argv[0]) + ": ");
|
|
|
|
|
2016-04-15 05:59:01 +08:00
|
|
|
LLVMContext Context;
|
2017-09-16 04:10:09 +08:00
|
|
|
Context.setDiagnosticHandler(
|
|
|
|
llvm::make_unique<LLVMLinkDiagnosticHandler>(), true);
|
2009-03-06 13:34:10 +08:00
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
|
2001-10-24 04:44:55 +08:00
|
|
|
|
2016-04-17 11:58:21 +08:00
|
|
|
if (!DisableDITypeMap)
|
2016-04-19 12:55:25 +08:00
|
|
|
Context.enableDebugTypeODRUniquing();
|
2016-04-17 11:58:21 +08:00
|
|
|
|
2014-10-24 03:40:45 +08:00
|
|
|
auto Composite = make_unique<Module>("llvm-link", Context);
|
2015-12-15 07:17:03 +08:00
|
|
|
Linker L(*Composite);
|
2014-10-24 03:40:45 +08:00
|
|
|
|
2015-09-02 01:55:55 +08:00
|
|
|
unsigned Flags = Linker::Flags::None;
|
|
|
|
if (OnlyNeeded)
|
|
|
|
Flags |= Linker::Flags::LinkOnlyNeeded;
|
|
|
|
|
2015-04-22 12:11:00 +08:00
|
|
|
// First add all the regular input files
|
2015-09-02 01:55:55 +08:00
|
|
|
if (!linkFiles(argv[0], Context, L, InputFilenames, Flags))
|
2015-04-22 12:11:00 +08:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
// Next the -override ones.
|
2015-09-02 01:55:55 +08:00
|
|
|
if (!linkFiles(argv[0], Context, L, OverridingInputs,
|
|
|
|
Flags | Linker::Flags::OverrideFromSrc))
|
2015-04-22 12:08:22 +08:00
|
|
|
return 1;
|
2007-05-06 13:13:17 +08:00
|
|
|
|
2015-11-03 08:14:15 +08:00
|
|
|
// Import any functions requested via -import
|
2017-01-04 22:27:31 +08:00
|
|
|
if (!importFunctions(argv[0], *Composite))
|
2015-11-03 08:14:15 +08:00
|
|
|
return 1;
|
|
|
|
|
2009-09-15 23:35:07 +08:00
|
|
|
if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite;
|
2007-05-06 13:13:17 +08:00
|
|
|
|
2014-08-26 02:16:47 +08:00
|
|
|
std::error_code EC;
|
2017-09-23 09:03:17 +08:00
|
|
|
ToolOutputFile Out(OutputFilename, EC, sys::fs::F_None);
|
2014-08-26 02:16:47 +08:00
|
|
|
if (EC) {
|
|
|
|
errs() << EC.message() << '\n';
|
2009-08-23 10:56:05 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2002-04-19 03:55:25 +08:00
|
|
|
|
2015-03-31 11:07:23 +08:00
|
|
|
if (verifyModule(*Composite, &errs())) {
|
|
|
|
errs() << argv[0] << ": error: linked module is broken!\n";
|
2007-05-06 13:13:17 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-07-16 23:30:09 +08:00
|
|
|
if (Verbose) errs() << "Writing bitcode...\n";
|
2009-09-15 23:35:07 +08:00
|
|
|
if (OutputAssembly) {
|
2015-04-15 11:14:06 +08:00
|
|
|
Composite->print(Out.os(), nullptr, PreserveAssemblyUseListOrder);
|
2010-09-01 22:20:41 +08:00
|
|
|
} else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
|
2015-04-15 11:14:06 +08:00
|
|
|
WriteBitcodeToFile(Composite.get(), Out.os(), PreserveBitcodeUseListOrder);
|
2010-08-20 09:12:13 +08:00
|
|
|
|
|
|
|
// Declare success.
|
|
|
|
Out.keep();
|
2007-05-06 13:13:17 +08:00
|
|
|
|
|
|
|
return 0;
|
2001-10-13 15:06:23 +08:00
|
|
|
}
|