2003-09-20 04:42:54 +02:00
|
|
|
//===- llvm-link.cpp - Low-level LLVM linker ------------------------------===//
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:44:31 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-10-13 09:06:23 +02:00
|
|
|
//
|
|
|
|
// This utility may be invoked in the following manner:
|
2003-09-15 20:34:34 +02:00
|
|
|
// llvm-link a.bc b.bc c.bc -o x.bc
|
2001-10-13 09:06:23 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-03-06 04:42:23 +01:00
|
|
|
#include "llvm/Linker/Linker.h"
|
2015-03-01 22:28:53 +01:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2007-05-06 07:13:17 +02:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2015-03-30 23:36:43 +02:00
|
|
|
#include "llvm/IR/AutoUpgrade.h"
|
2014-10-25 06:06:10 +02:00
|
|
|
#include "llvm/IR/DiagnosticInfo.h"
|
|
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2014-01-13 10:26:24 +01:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2013-03-26 03:25:37 +01:00
|
|
|
#include "llvm/IRReader/IRReader.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2014-04-30 01:26:49 +02:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2006-12-06 02:18:01 +01:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2012-12-04 11:44:52 +01:00
|
|
|
#include "llvm/Support/Path.h"
|
2009-03-06 06:34:10 +01:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2010-11-29 19:16:10 +01:00
|
|
|
#include "llvm/Support/Signals.h"
|
2013-03-26 03:25:37 +01:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2012-12-04 11:44:52 +01:00
|
|
|
#include "llvm/Support/SystemUtils.h"
|
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2001-10-13 09:06:23 +02:00
|
|
|
#include <memory>
|
2003-11-11 23:41:34 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
2002-07-22 04:10:13 +02:00
|
|
|
static cl::list<std::string>
|
|
|
|
InputFilenames(cl::Positional, cl::OneOrMore,
|
2007-07-05 19:07:56 +02:00
|
|
|
cl::desc("<input bitcode files>"));
|
2002-07-22 04:10:13 +02:00
|
|
|
|
2015-04-22 06:11:00 +02: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)"));
|
|
|
|
|
2002-07-22 04:10:13 +02:00
|
|
|
static cl::opt<std::string>
|
|
|
|
OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
|
|
|
|
cl::value_desc("filename"));
|
|
|
|
|
2015-09-01 19:55:55 +02:00
|
|
|
static cl::opt<bool>
|
|
|
|
Internalize("internalize", cl::desc("Internalize linked symbols"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
OnlyNeeded("only-needed", cl::desc("Link only needed symbols"));
|
|
|
|
|
2009-08-25 17:34:52 +02:00
|
|
|
static cl::opt<bool>
|
|
|
|
Force("f", cl::desc("Enable binary output on terminals"));
|
2002-07-22 04:10:13 +02:00
|
|
|
|
2009-09-15 17:35:07 +02:00
|
|
|
static cl::opt<bool>
|
|
|
|
OutputAssembly("S",
|
|
|
|
cl::desc("Write output as LLVM assembly"), cl::Hidden);
|
|
|
|
|
2002-07-22 04:10:13 +02: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-20 23:19:24 +01:00
|
|
|
static cl::opt<bool>
|
|
|
|
SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"),
|
|
|
|
cl::init(false));
|
|
|
|
|
2015-04-15 05:14:06 +02: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);
|
|
|
|
|
2014-08-26 19:29:46 +02: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 01:39:42 +02:00
|
|
|
//
|
2014-08-26 19:29:46 +02:00
|
|
|
static std::unique_ptr<Module>
|
|
|
|
loadFile(const char *argv0, const std::string &FN, LLVMContext &Context) {
|
2009-09-12 23:55:12 +02:00
|
|
|
SMDiagnostic Err;
|
2013-06-17 19:32:19 +02:00
|
|
|
if (Verbose) errs() << "Loading '" << FN << "'\n";
|
2014-10-23 21:40:45 +02:00
|
|
|
std::unique_ptr<Module> Result = getLazyIRFileModule(FN, Err, Context);
|
2014-08-26 20:03:35 +02:00
|
|
|
if (!Result)
|
|
|
|
Err.print(argv0, errs());
|
2001-10-24 08:23:00 +02:00
|
|
|
|
2015-03-30 23:36:43 +02:00
|
|
|
Result->materializeMetadata();
|
|
|
|
UpgradeDebugInfo(*Result);
|
2015-03-27 16:55:06 +01:00
|
|
|
|
2014-08-26 20:03:35 +02:00
|
|
|
return Result;
|
2001-10-24 08:23:00 +02:00
|
|
|
}
|
2001-10-13 09:06:23 +02:00
|
|
|
|
2014-10-28 00:02:10 +01:00
|
|
|
static void diagnosticHandler(const DiagnosticInfo &DI) {
|
2014-10-25 06:06:10 +02:00
|
|
|
unsigned Severity = DI.getSeverity();
|
|
|
|
switch (Severity) {
|
|
|
|
case DS_Error:
|
|
|
|
errs() << "ERROR: ";
|
2014-10-28 00:02:10 +01:00
|
|
|
break;
|
2014-10-25 06:06:10 +02:00
|
|
|
case DS_Warning:
|
|
|
|
if (SuppressWarnings)
|
|
|
|
return;
|
|
|
|
errs() << "WARNING: ";
|
|
|
|
break;
|
|
|
|
case DS_Remark:
|
|
|
|
case DS_Note:
|
|
|
|
llvm_unreachable("Only expecting warnings and errors");
|
|
|
|
}
|
|
|
|
|
|
|
|
DiagnosticPrinterRawOStream DP(errs());
|
|
|
|
DI.print(DP);
|
2014-10-28 00:02:10 +01:00
|
|
|
errs() << '\n';
|
2014-10-25 06:06:10 +02:00
|
|
|
}
|
|
|
|
|
2015-04-22 06:08:22 +02:00
|
|
|
static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L,
|
2015-04-22 06:11:00 +02:00
|
|
|
const cl::list<std::string> &Files,
|
2015-09-01 19:55:55 +02:00
|
|
|
unsigned Flags) {
|
|
|
|
// Filter out flags that don't apply to the first file we load.
|
|
|
|
unsigned ApplicableFlags = Flags & Linker::Flags::OverrideFromSrc;
|
2015-04-22 06:08:22 +02: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verifyModule(*M, &errs())) {
|
|
|
|
errs() << argv0 << ": " << File << ": error: input module is broken!\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Verbose)
|
|
|
|
errs() << "Linking in '" << File << "'\n";
|
|
|
|
|
2015-11-02 23:17:32 +01:00
|
|
|
if (L.linkInModule(M.get(), ApplicableFlags))
|
2015-04-22 06:08:22 +02:00
|
|
|
return false;
|
2015-09-01 19:55:55 +02:00
|
|
|
// All linker flags apply to linking of subsequent files.
|
|
|
|
ApplicableFlags = Flags;
|
2015-04-22 06:08:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2001-10-13 09:06:23 +02:00
|
|
|
int main(int argc, char **argv) {
|
2009-03-06 06:34:10 +01:00
|
|
|
// Print a stack trace if we signal out.
|
2007-05-06 07:13:17 +02:00
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
2009-03-06 06:34:10 +01:00
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
2013-09-19 01:31:10 +02:00
|
|
|
|
2009-07-16 00:16:10 +02:00
|
|
|
LLVMContext &Context = getGlobalContext();
|
2009-03-06 06:34:10 +01:00
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
|
2001-10-23 22:44:55 +02:00
|
|
|
|
2014-10-23 21:40:45 +02:00
|
|
|
auto Composite = make_unique<Module>("llvm-link", Context);
|
2014-10-28 00:02:10 +01:00
|
|
|
Linker L(Composite.get(), diagnosticHandler);
|
2014-10-23 21:40:45 +02:00
|
|
|
|
2015-09-01 19:55:55 +02:00
|
|
|
unsigned Flags = Linker::Flags::None;
|
|
|
|
if (Internalize)
|
|
|
|
Flags |= Linker::Flags::InternalizeLinkedSymbols;
|
|
|
|
if (OnlyNeeded)
|
|
|
|
Flags |= Linker::Flags::LinkOnlyNeeded;
|
|
|
|
|
2015-04-22 06:11:00 +02:00
|
|
|
// First add all the regular input files
|
2015-09-01 19:55:55 +02:00
|
|
|
if (!linkFiles(argv[0], Context, L, InputFilenames, Flags))
|
2015-04-22 06:11:00 +02:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
// Next the -override ones.
|
2015-09-01 19:55:55 +02:00
|
|
|
if (!linkFiles(argv[0], Context, L, OverridingInputs,
|
|
|
|
Flags | Linker::Flags::OverrideFromSrc))
|
2015-04-22 06:08:22 +02:00
|
|
|
return 1;
|
2007-05-06 07:13:17 +02:00
|
|
|
|
2009-09-15 17:35:07 +02:00
|
|
|
if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite;
|
2007-05-06 07:13:17 +02:00
|
|
|
|
2014-08-25 20:16:47 +02:00
|
|
|
std::error_code EC;
|
|
|
|
tool_output_file Out(OutputFilename, EC, sys::fs::F_None);
|
|
|
|
if (EC) {
|
|
|
|
errs() << EC.message() << '\n';
|
2009-08-23 04:56:05 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2002-04-18 21:55:25 +02:00
|
|
|
|
2015-03-31 05:07:23 +02:00
|
|
|
if (verifyModule(*Composite, &errs())) {
|
|
|
|
errs() << argv[0] << ": error: linked module is broken!\n";
|
2007-05-06 07:13:17 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-07-16 17:30:09 +02:00
|
|
|
if (Verbose) errs() << "Writing bitcode...\n";
|
2009-09-15 17:35:07 +02:00
|
|
|
if (OutputAssembly) {
|
2015-04-15 05:14:06 +02:00
|
|
|
Composite->print(Out.os(), nullptr, PreserveAssemblyUseListOrder);
|
2010-09-01 16:20:41 +02:00
|
|
|
} else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
|
2015-04-15 05:14:06 +02:00
|
|
|
WriteBitcodeToFile(Composite.get(), Out.os(), PreserveBitcodeUseListOrder);
|
2010-08-20 03:12:13 +02:00
|
|
|
|
|
|
|
// Declare success.
|
|
|
|
Out.keep();
|
2007-05-06 07:13:17 +02:00
|
|
|
|
|
|
|
return 0;
|
2001-10-13 09:06:23 +02:00
|
|
|
}
|