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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-11-15 07:25:32 +08:00
|
|
|
#include "llvm/Linker.h"
|
2007-05-06 13:13:17 +08:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.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"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/CommandLine.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"
|
2001-10-13 15:06:23 +08:00
|
|
|
#include <memory>
|
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
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
|
|
|
|
cl::value_desc("filename"));
|
|
|
|
|
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>
|
|
|
|
OutputAssembly("S",
|
|
|
|
cl::desc("Write output as LLVM assembly"), cl::Hidden);
|
|
|
|
|
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);
|
|
|
|
|
2007-07-06 01:07:56 +08:00
|
|
|
// LoadFile - Read the specified bitcode file in and return it. This routine
|
2004-09-13 07:39:42 +08:00
|
|
|
// searches the link path for the specified file to try to find it...
|
|
|
|
//
|
2013-04-15 20:06:32 +08:00
|
|
|
static inline Module *LoadFile(const char *argv0, const std::string &FN,
|
|
|
|
LLVMContext& Context) {
|
2009-09-13 05:55:12 +08:00
|
|
|
SMDiagnostic Err;
|
2013-06-18 01:32:19 +08:00
|
|
|
if (Verbose) errs() << "Loading '" << FN << "'\n";
|
2010-05-28 04:51:54 +08:00
|
|
|
Module* Result = 0;
|
2013-06-18 01:32:19 +08:00
|
|
|
|
|
|
|
Result = ParseIRFile(FN, Err, Context);
|
2013-04-15 20:06:32 +08:00
|
|
|
if (Result) return Result; // Load successful!
|
2001-10-24 14:23:00 +08:00
|
|
|
|
2011-10-16 12:47:35 +08:00
|
|
|
Err.print(argv0, errs());
|
2013-04-15 20:06:32 +08:00
|
|
|
return NULL;
|
2001-10-24 14:23:00 +08:00
|
|
|
}
|
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.
|
2007-05-06 13:13:17 +08:00
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
2009-03-06 13:34:10 +08:00
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
2013-09-19 07:31:10 +08:00
|
|
|
|
2009-07-16 06:16:10 +08:00
|
|
|
LLVMContext &Context = getGlobalContext();
|
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
|
|
|
|
2007-05-06 13:13:17 +08:00
|
|
|
unsigned BaseArg = 0;
|
|
|
|
std::string ErrorMessage;
|
2004-12-30 13:36:08 +08:00
|
|
|
|
2013-04-15 20:06:32 +08:00
|
|
|
OwningPtr<Module> Composite(LoadFile(argv[0],
|
|
|
|
InputFilenames[BaseArg], Context));
|
2007-05-06 13:13:17 +08:00
|
|
|
if (Composite.get() == 0) {
|
2009-07-16 23:30:09 +08:00
|
|
|
errs() << argv[0] << ": error loading file '"
|
|
|
|
<< InputFilenames[BaseArg] << "'\n";
|
2007-05-06 13:13:17 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2004-12-30 13:36:08 +08:00
|
|
|
|
2013-05-04 13:30:49 +08:00
|
|
|
Linker L(Composite.get());
|
2007-05-06 13:13:17 +08:00
|
|
|
for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
|
2013-04-15 20:06:32 +08:00
|
|
|
OwningPtr<Module> M(LoadFile(argv[0], InputFilenames[i], Context));
|
2007-05-06 13:13:17 +08:00
|
|
|
if (M.get() == 0) {
|
2009-07-16 23:30:09 +08:00
|
|
|
errs() << argv[0] << ": error loading file '" <<InputFilenames[i]<< "'\n";
|
2007-05-06 13:13:17 +08:00
|
|
|
return 1;
|
2001-10-13 15:06:23 +08:00
|
|
|
}
|
2004-09-11 12:32:42 +08:00
|
|
|
|
2009-07-16 23:30:09 +08:00
|
|
|
if (Verbose) errs() << "Linking in '" << InputFilenames[i] << "'\n";
|
2007-05-06 13:13:17 +08:00
|
|
|
|
2013-05-04 13:30:49 +08:00
|
|
|
if (L.linkInModule(M.get(), &ErrorMessage)) {
|
2009-07-16 23:30:09 +08:00
|
|
|
errs() << argv[0] << ": link error in '" << InputFilenames[i]
|
|
|
|
<< "': " << ErrorMessage << "\n";
|
2007-05-06 13:13:17 +08:00
|
|
|
return 1;
|
2002-01-21 06:54:45 +08:00
|
|
|
}
|
2007-05-06 13:13:17 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2009-08-23 10:56:05 +08:00
|
|
|
std::string ErrorInfo;
|
2013-07-17 03:44:17 +08:00
|
|
|
tool_output_file Out(OutputFilename.c_str(), ErrorInfo, sys::fs::F_Binary);
|
2009-08-23 10:56:05 +08:00
|
|
|
if (!ErrorInfo.empty()) {
|
|
|
|
errs() << ErrorInfo << '\n';
|
|
|
|
return 1;
|
|
|
|
}
|
2002-04-19 03:55:25 +08:00
|
|
|
|
2009-09-15 23:35:07 +08:00
|
|
|
if (verifyModule(*Composite)) {
|
2009-07-16 23:30:09 +08:00
|
|
|
errs() << argv[0] << ": 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) {
|
2010-09-01 22:20:41 +08:00
|
|
|
Out.os() << *Composite;
|
|
|
|
} else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
|
|
|
|
WriteBitcodeToFile(Composite.get(), Out.os());
|
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
|
|
|
}
|