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
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and 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"
|
2003-08-28 16:25:34 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Analysis/Verifier.h"
|
2001-10-13 07:06:23 +00:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
|
|
|
#include "llvm/Bytecode/Writer.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/CommandLine.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-11-27 00:03:19 +00:00
|
|
|
#include <fstream>
|
2004-07-04 12:20:55 +00:00
|
|
|
#include <iostream>
|
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,
|
2002-07-22 02:18:09 +00:00
|
|
|
cl::desc("<input bytecode 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);
|
|
|
|
|
2004-11-07 05:50:16 +00:00
|
|
|
static cl::opt<bool> NoCompress("disable-compression", cl::init(false),
|
2005-01-01 22:10:32 +00:00
|
|
|
cl::desc("Don't compress the generated bytecode"));
|
2004-11-07 05:41:32 +00:00
|
|
|
|
2004-09-12 23:39:42 +00:00
|
|
|
// LoadFile - Read the specified bytecode file in and return it. This routine
|
|
|
|
// searches the link path for the specified file to try to find it...
|
|
|
|
//
|
|
|
|
static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
|
|
|
|
sys::Path Filename;
|
2005-07-07 23:21:43 +00:00
|
|
|
if (!Filename.set(FN)) {
|
2004-09-12 23:39:42 +00:00
|
|
|
std::cerr << "Invalid file name: '" << FN << "'\n";
|
|
|
|
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()) {
|
2004-09-12 23:39:42 +00:00
|
|
|
if (Verbose) std::cerr << "Loading '" << Filename.c_str() << "'\n";
|
2004-12-11 00:14:15 +00:00
|
|
|
Module* Result = ParseBytecodeFile(Filename.toString(), &ErrorMessage);
|
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) {
|
|
|
|
std::cerr << "Error opening bytecode file: '" << Filename.c_str() << "'";
|
|
|
|
if (ErrorMessage.size()) std::cerr << ": " << ErrorMessage;
|
|
|
|
std::cerr << "\n";
|
|
|
|
}
|
|
|
|
} else {
|
2005-04-22 00:00:37 +00:00
|
|
|
std::cerr << "Bytecode file: '" << Filename.c_str()
|
2004-09-11 04:32:42 +00:00
|
|
|
<< "' does not exist.\n";
|
|
|
|
}
|
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) {
|
2004-12-30 05:36:08 +00:00
|
|
|
try {
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
|
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
|
|
|
assert(InputFilenames.size() > 0 && "OneOrMore is not working");
|
2001-10-13 07:06:23 +00:00
|
|
|
|
2004-12-30 05:36:08 +00:00
|
|
|
unsigned BaseArg = 0;
|
|
|
|
std::string ErrorMessage;
|
2001-10-24 06:23:00 +00:00
|
|
|
|
2004-12-30 05:36:08 +00:00
|
|
|
std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
|
|
|
|
if (Composite.get() == 0) {
|
2004-09-27 16:41:01 +00:00
|
|
|
std::cerr << argv[0] << ": error loading file '"
|
2004-12-30 05:36:08 +00:00
|
|
|
<< InputFilenames[BaseArg] << "'\n";
|
2004-09-27 16:41:01 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2001-10-23 20:44:55 +00:00
|
|
|
|
2004-12-30 05:36:08 +00:00
|
|
|
for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
|
|
|
|
std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
|
|
|
|
if (M.get() == 0) {
|
|
|
|
std::cerr << argv[0] << ": error loading file '"
|
|
|
|
<< InputFilenames[i] << "'\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n";
|
|
|
|
|
|
|
|
if (Linker::LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
|
|
|
|
std::cerr << argv[0] << ": link error in '" << InputFilenames[i]
|
|
|
|
<< "': " << ErrorMessage << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2001-10-13 07:06:23 +00:00
|
|
|
}
|
2004-09-11 04:32:42 +00:00
|
|
|
|
2004-12-30 05:36:08 +00:00
|
|
|
// TODO: Iterate over the -l list and link in any modules containing
|
|
|
|
// global symbols that have not been resolved so far.
|
|
|
|
|
2005-02-13 19:12:31 +00:00
|
|
|
if (DumpAsm) std::cerr << "Here's the assembly:\n" << *Composite.get();
|
2004-12-30 05:36:08 +00:00
|
|
|
|
2005-01-22 17:36:17 +00:00
|
|
|
// FIXME: cout is not binary!
|
2004-12-30 05:36:08 +00:00
|
|
|
std::ostream *Out = &std::cout; // Default to printing to stdout...
|
|
|
|
if (OutputFilename != "-") {
|
|
|
|
if (!Force && std::ifstream(OutputFilename.c_str())) {
|
|
|
|
// If force is not specified, make sure not to overwrite a file!
|
|
|
|
std::cerr << argv[0] << ": error opening '" << OutputFilename
|
|
|
|
<< "': file exists!\n"
|
|
|
|
<< "Use -f command line argument to force output\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2005-01-22 17:36:17 +00:00
|
|
|
std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
|
|
|
|
std::ios::binary;
|
|
|
|
Out = new std::ofstream(OutputFilename.c_str(), io_mode);
|
2004-12-30 05:36:08 +00:00
|
|
|
if (!Out->good()) {
|
|
|
|
std::cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that the Out file gets unlinked from the disk if we get a
|
|
|
|
// SIGINT
|
|
|
|
sys::RemoveFileOnSignal(sys::Path(OutputFilename));
|
2002-01-20 22:54:45 +00:00
|
|
|
}
|
2004-12-30 05:36:08 +00:00
|
|
|
|
|
|
|
if (verifyModule(*Composite.get())) {
|
|
|
|
std::cerr << argv[0] << ": linked module is broken!\n";
|
2001-10-13 07:06:23 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2002-04-18 19:55:25 +00:00
|
|
|
|
2004-12-30 05:36:08 +00:00
|
|
|
if (Verbose) std::cerr << "Writing bytecode...\n";
|
|
|
|
WriteBytecodeToFile(Composite.get(), *Out, !NoCompress);
|
2001-10-13 07:06:23 +00:00
|
|
|
|
2004-12-30 05:36:08 +00:00
|
|
|
if (Out != &std::cout) delete Out;
|
|
|
|
return 0;
|
|
|
|
} catch (const std::string& msg) {
|
|
|
|
std::cerr << argv[0] << ": " << msg << "\n";
|
|
|
|
} catch (...) {
|
|
|
|
std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
|
2003-08-28 16:25:34 +00:00
|
|
|
}
|
2004-12-30 05:36:08 +00:00
|
|
|
return 1;
|
2001-10-13 07:06:23 +00:00
|
|
|
}
|