2003-09-20 02:42:54 +00:00
|
|
|
//===- llvm-link.cpp - Low-level LLVM linker ------------------------------===//
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-20 17:47:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:44:31 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-20 17:47:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-10-13 07:06:23 +00:00
|
|
|
//
|
|
|
|
// This utility may be invoked in the following manner:
|
2003-09-15 18:34:34 +00:00
|
|
|
// llvm-link a.bc b.bc c.bc -o x.bc
|
2001-10-13 07:06:23 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-11-14 23:25:32 +00:00
|
|
|
#include "llvm/Linker.h"
|
2009-07-01 16:58:40 +00:00
|
|
|
#include "llvm/LLVMContext.h"
|
2003-08-28 16:25:34 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Analysis/Verifier.h"
|
2007-05-06 05:13:17 +00:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2006-12-06 01:18:01 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2007-05-06 05:13:17 +00:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2009-03-06 05:34:10 +00:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2009-08-23 22:45:37 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2004-05-27 05:41:36 +00:00
|
|
|
#include "llvm/System/Signals.h"
|
2004-09-11 04:32:42 +00:00
|
|
|
#include "llvm/System/Path.h"
|
2001-10-13 07:06:23 +00:00
|
|
|
#include <memory>
|
2003-11-11 22:41:34 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2002-07-22 02:10:13 +00:00
|
|
|
static cl::list<std::string>
|
|
|
|
InputFilenames(cl::Positional, cl::OneOrMore,
|
2007-07-05 17:07:56 +00:00
|
|
|
cl::desc("<input bitcode files>"));
|
2002-07-22 02:10:13 +00:00
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
|
|
|
|
cl::value_desc("filename"));
|
|
|
|
|
|
|
|
static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
|
|
|
|
|
|
|
|
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 17:07:56 +00:00
|
|
|
// LoadFile - Read the specified bitcode file in and return it. This routine
|
2004-09-12 23:39:42 +00:00
|
|
|
// searches the link path for the specified file to try to find it...
|
|
|
|
//
|
2009-07-01 16:58:40 +00:00
|
|
|
static inline std::auto_ptr<Module> LoadFile(const std::string &FN,
|
2009-07-01 23:13:44 +00:00
|
|
|
LLVMContext& Context) {
|
2004-09-12 23:39:42 +00:00
|
|
|
sys::Path Filename;
|
2005-07-07 23:21:43 +00:00
|
|
|
if (!Filename.set(FN)) {
|
2009-07-16 15:30:09 +00:00
|
|
|
errs() << "Invalid file name: '" << FN << "'\n";
|
2004-09-12 23:39:42 +00:00
|
|
|
return std::auto_ptr<Module>();
|
|
|
|
}
|
2001-12-08 20:31:32 +00:00
|
|
|
|
2004-09-11 04:32:42 +00:00
|
|
|
std::string ErrorMessage;
|
|
|
|
if (Filename.exists()) {
|
2009-07-16 15:30:09 +00:00
|
|
|
if (Verbose) errs() << "Loading '" << Filename.c_str() << "'\n";
|
2007-05-06 05:13:17 +00:00
|
|
|
Module* Result = 0;
|
|
|
|
|
2009-08-23 22:45:37 +00:00
|
|
|
const std::string &FNStr = Filename.str();
|
2007-05-06 23:45:49 +00:00
|
|
|
if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(FNStr,
|
|
|
|
&ErrorMessage)) {
|
2009-07-01 16:58:40 +00:00
|
|
|
Result = ParseBitcodeFile(Buffer, Context, &ErrorMessage);
|
2007-05-06 23:45:49 +00:00
|
|
|
delete Buffer;
|
|
|
|
}
|
2004-09-12 23:39:42 +00:00
|
|
|
if (Result) return std::auto_ptr<Module>(Result); // Load successful!
|
2004-09-11 04:32:42 +00:00
|
|
|
|
|
|
|
if (Verbose) {
|
2009-07-16 15:30:09 +00:00
|
|
|
errs() << "Error opening bitcode file: '" << Filename.c_str() << "'";
|
|
|
|
if (ErrorMessage.size()) errs() << ": " << ErrorMessage;
|
|
|
|
errs() << "\n";
|
2004-09-11 04:32:42 +00:00
|
|
|
}
|
|
|
|
} else {
|
2009-07-16 15:30:09 +00:00
|
|
|
errs() << "Bitcode file: '" << Filename.c_str() << "' does not exist.\n";
|
2004-09-11 04:32:42 +00:00
|
|
|
}
|
2001-10-24 06:23:00 +00:00
|
|
|
|
|
|
|
return std::auto_ptr<Module>();
|
|
|
|
}
|
2001-10-13 07:06:23 +00:00
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2009-03-06 05:34:10 +00:00
|
|
|
// Print a stack trace if we signal out.
|
2007-05-06 05:13:17 +00:00
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
2009-03-06 05:34:10 +00:00
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
|
|
|
|
2009-07-15 22:16:10 +00:00
|
|
|
LLVMContext &Context = getGlobalContext();
|
2009-03-06 05:34:10 +00:00
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
|
2001-10-23 20:44:55 +00:00
|
|
|
|
2007-05-06 05:13:17 +00:00
|
|
|
unsigned BaseArg = 0;
|
|
|
|
std::string ErrorMessage;
|
2004-12-30 05:36:08 +00:00
|
|
|
|
2009-07-01 21:22:36 +00:00
|
|
|
std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg], Context));
|
2007-05-06 05:13:17 +00:00
|
|
|
if (Composite.get() == 0) {
|
2009-07-16 15:30:09 +00:00
|
|
|
errs() << argv[0] << ": error loading file '"
|
|
|
|
<< InputFilenames[BaseArg] << "'\n";
|
2007-05-06 05:13:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2004-12-30 05:36:08 +00:00
|
|
|
|
2007-05-06 05:13:17 +00:00
|
|
|
for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
|
2009-07-01 21:22:36 +00:00
|
|
|
std::auto_ptr<Module> M(LoadFile(InputFilenames[i], Context));
|
2007-05-06 05:13:17 +00:00
|
|
|
if (M.get() == 0) {
|
2009-07-16 15:30:09 +00:00
|
|
|
errs() << argv[0] << ": error loading file '" <<InputFilenames[i]<< "'\n";
|
2007-05-06 05:13:17 +00:00
|
|
|
return 1;
|
2001-10-13 07:06:23 +00:00
|
|
|
}
|
2004-09-11 04:32:42 +00:00
|
|
|
|
2009-07-16 15:30:09 +00:00
|
|
|
if (Verbose) errs() << "Linking in '" << InputFilenames[i] << "'\n";
|
2007-05-06 05:13:17 +00:00
|
|
|
|
|
|
|
if (Linker::LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
|
2009-07-16 15:30:09 +00:00
|
|
|
errs() << argv[0] << ": link error in '" << InputFilenames[i]
|
|
|
|
<< "': " << ErrorMessage << "\n";
|
2007-05-06 05:13:17 +00:00
|
|
|
return 1;
|
2002-01-20 22:54:45 +00:00
|
|
|
}
|
2007-05-06 05:13:17 +00: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 05:36:08 +00:00
|
|
|
|
2009-07-16 15:30:09 +00:00
|
|
|
if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite.get();
|
2007-05-06 05:13:17 +00:00
|
|
|
|
2009-08-23 02:56:05 +00:00
|
|
|
std::string ErrorInfo;
|
|
|
|
std::auto_ptr<raw_ostream>
|
|
|
|
Out(new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
|
|
|
|
raw_fd_ostream::F_Binary |
|
|
|
|
(Force ? raw_fd_ostream::F_Force : 0)));
|
|
|
|
if (!ErrorInfo.empty()) {
|
|
|
|
errs() << ErrorInfo << '\n';
|
|
|
|
if (!Force)
|
|
|
|
errs() << "Use -f command line argument to force output\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2002-04-18 19:55:25 +00:00
|
|
|
|
2007-05-06 05:13:17 +00:00
|
|
|
// Make sure that the Out file gets unlinked from the disk if we get a
|
|
|
|
// SIGINT
|
2009-08-23 02:56:05 +00:00
|
|
|
if (OutputFilename != "-")
|
2007-05-06 05:13:17 +00:00
|
|
|
sys::RemoveFileOnSignal(sys::Path(OutputFilename));
|
|
|
|
|
|
|
|
if (verifyModule(*Composite.get())) {
|
2009-07-16 15:30:09 +00:00
|
|
|
errs() << argv[0] << ": linked module is broken!\n";
|
2007-05-06 05:13:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-07-16 15:30:09 +00:00
|
|
|
if (Verbose) errs() << "Writing bitcode...\n";
|
2007-05-06 09:29:57 +00:00
|
|
|
WriteBitcodeToFile(Composite.get(), *Out);
|
2007-05-06 05:13:17 +00:00
|
|
|
|
|
|
|
return 0;
|
2001-10-13 07:06:23 +00:00
|
|
|
}
|