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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-11-15 00:25:32 +01:00
|
|
|
#include "llvm/Linker.h"
|
2003-08-28 18:25:34 +02:00
|
|
|
#include "llvm/Analysis/Verifier.h"
|
2007-05-06 07:13:17 +02:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2012-12-04 11:44:52 +01:00
|
|
|
#include "llvm/Support/IRReader.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"
|
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
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
|
|
|
|
cl::value_desc("filename"));
|
|
|
|
|
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);
|
|
|
|
|
2007-07-05 19:07:56 +02:00
|
|
|
// LoadFile - Read the specified bitcode file in and return it. This routine
|
2004-09-13 01:39:42 +02:00
|
|
|
// searches the link path for the specified file to try to find it...
|
|
|
|
//
|
2009-09-12 23:55:12 +02:00
|
|
|
static inline std::auto_ptr<Module> LoadFile(const char *argv0,
|
|
|
|
const std::string &FN,
|
2009-07-02 01:13:44 +02:00
|
|
|
LLVMContext& Context) {
|
2004-09-13 01:39:42 +02:00
|
|
|
sys::Path Filename;
|
2005-07-08 01:21:43 +02:00
|
|
|
if (!Filename.set(FN)) {
|
2009-07-16 17:30:09 +02:00
|
|
|
errs() << "Invalid file name: '" << FN << "'\n";
|
2004-09-13 01:39:42 +02:00
|
|
|
return std::auto_ptr<Module>();
|
|
|
|
}
|
2001-12-08 21:31:32 +01:00
|
|
|
|
2009-09-12 23:55:12 +02:00
|
|
|
SMDiagnostic Err;
|
2010-05-27 22:51:54 +02:00
|
|
|
if (Verbose) errs() << "Loading '" << Filename.c_str() << "'\n";
|
|
|
|
Module* Result = 0;
|
|
|
|
|
|
|
|
const std::string &FNStr = Filename.str();
|
|
|
|
Result = ParseIRFile(FNStr, Err, Context);
|
|
|
|
if (Result) return std::auto_ptr<Module>(Result); // Load successful!
|
2001-10-24 08:23:00 +02:00
|
|
|
|
2011-10-16 06:47:35 +02:00
|
|
|
Err.print(argv0, errs());
|
2001-10-24 08:23:00 +02:00
|
|
|
return std::auto_ptr<Module>();
|
|
|
|
}
|
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);
|
|
|
|
|
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
|
|
|
|
2007-05-06 07:13:17 +02:00
|
|
|
unsigned BaseArg = 0;
|
|
|
|
std::string ErrorMessage;
|
2004-12-30 06:36:08 +01:00
|
|
|
|
2009-09-12 23:55:12 +02:00
|
|
|
std::auto_ptr<Module> Composite(LoadFile(argv[0],
|
|
|
|
InputFilenames[BaseArg], Context));
|
2007-05-06 07:13:17 +02:00
|
|
|
if (Composite.get() == 0) {
|
2009-07-16 17:30:09 +02:00
|
|
|
errs() << argv[0] << ": error loading file '"
|
|
|
|
<< InputFilenames[BaseArg] << "'\n";
|
2007-05-06 07:13:17 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2004-12-30 06:36:08 +01:00
|
|
|
|
2007-05-06 07:13:17 +02:00
|
|
|
for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
|
2009-09-12 23:55:12 +02:00
|
|
|
std::auto_ptr<Module> M(LoadFile(argv[0],
|
|
|
|
InputFilenames[i], Context));
|
2007-05-06 07:13:17 +02:00
|
|
|
if (M.get() == 0) {
|
2009-07-16 17:30:09 +02:00
|
|
|
errs() << argv[0] << ": error loading file '" <<InputFilenames[i]<< "'\n";
|
2007-05-06 07:13:17 +02:00
|
|
|
return 1;
|
2001-10-13 09:06:23 +02:00
|
|
|
}
|
2004-09-11 06:32:42 +02:00
|
|
|
|
2009-07-16 17:30:09 +02:00
|
|
|
if (Verbose) errs() << "Linking in '" << InputFilenames[i] << "'\n";
|
2007-05-06 07:13:17 +02:00
|
|
|
|
2011-10-11 02:24:54 +02:00
|
|
|
if (Linker::LinkModules(Composite.get(), M.get(), Linker::DestroySource,
|
|
|
|
&ErrorMessage)) {
|
2009-07-16 17:30:09 +02:00
|
|
|
errs() << argv[0] << ": link error in '" << InputFilenames[i]
|
|
|
|
<< "': " << ErrorMessage << "\n";
|
2007-05-06 07:13:17 +02:00
|
|
|
return 1;
|
2002-01-20 23:54:45 +01:00
|
|
|
}
|
2007-05-06 07:13:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Iterate over the -l list and link in any modules containing
|
|
|
|
// global symbols that have not been resolved so far.
|
2004-12-30 06:36:08 +01: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
|
|
|
|
2009-08-23 04:56:05 +02:00
|
|
|
std::string ErrorInfo;
|
2010-08-20 03:12:13 +02:00
|
|
|
tool_output_file Out(OutputFilename.c_str(), ErrorInfo,
|
|
|
|
raw_fd_ostream::F_Binary);
|
2009-08-23 04:56:05 +02:00
|
|
|
if (!ErrorInfo.empty()) {
|
|
|
|
errs() << ErrorInfo << '\n';
|
|
|
|
return 1;
|
|
|
|
}
|
2002-04-18 21:55:25 +02:00
|
|
|
|
2009-09-15 17:35:07 +02:00
|
|
|
if (verifyModule(*Composite)) {
|
2009-07-16 17:30:09 +02:00
|
|
|
errs() << argv[0] << ": 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) {
|
2010-09-01 16:20:41 +02:00
|
|
|
Out.os() << *Composite;
|
|
|
|
} else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
|
|
|
|
WriteBitcodeToFile(Composite.get(), Out.os());
|
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
|
|
|
}
|