2003-09-20 10:42:54 +08:00
|
|
|
//===- llvm-link.cpp - Low-level LLVM linker ------------------------------===//
|
2003-10-21 01:47:21 +08: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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-08-29 00:25:34 +08:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Analysis/Verifier.h"
|
2001-10-13 15:06:23 +08:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
|
|
|
#include "llvm/Bytecode/Writer.h"
|
2004-06-24 01:33:09 +08:00
|
|
|
#include "llvm/Support/Linker.h"
|
2001-11-27 08:03:19 +08:00
|
|
|
#include "Support/CommandLine.h"
|
2003-12-30 15:48:17 +08:00
|
|
|
#include "Support/FileUtilities.h"
|
2004-05-27 13:41:36 +08:00
|
|
|
#include "llvm/System/Signals.h"
|
2001-11-27 08:03:19 +08:00
|
|
|
#include <fstream>
|
2004-07-04 20:20:55 +08:00
|
|
|
#include <iostream>
|
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,
|
2002-07-22 10:18:09 +08:00
|
|
|
cl::desc("<input bytecode 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"));
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
static cl::list<std::string>
|
|
|
|
LibPaths("L", cl::desc("Specify a library search path"), cl::ZeroOrMore,
|
|
|
|
cl::value_desc("directory"), cl::Prefix);
|
2001-12-09 04:31:32 +08: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...
|
|
|
|
//
|
2002-01-21 06:54:45 +08:00
|
|
|
static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
|
|
|
|
std::string Filename = FN;
|
|
|
|
std::string ErrorMessage;
|
2001-10-24 14:23:00 +08:00
|
|
|
|
|
|
|
unsigned NextLibPathIdx = 0;
|
2001-12-09 04:31:32 +08:00
|
|
|
bool FoundAFile = false;
|
2001-10-24 14:23:00 +08:00
|
|
|
|
|
|
|
while (1) {
|
2003-05-23 04:13:16 +08:00
|
|
|
if (Verbose) std::cerr << "Loading '" << Filename << "'\n";
|
2003-12-30 15:48:17 +08:00
|
|
|
if (getFileSize(Filename) != -1) FoundAFile = true;
|
2001-10-24 14:23:00 +08:00
|
|
|
Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
|
|
|
|
if (Result) return std::auto_ptr<Module>(Result); // Load successful!
|
|
|
|
|
|
|
|
if (Verbose) {
|
2003-05-23 04:13:16 +08:00
|
|
|
std::cerr << "Error opening bytecode file: '" << Filename << "'";
|
|
|
|
if (ErrorMessage.size()) std::cerr << ": " << ErrorMessage;
|
|
|
|
std::cerr << "\n";
|
2001-10-24 14:23:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NextLibPathIdx == LibPaths.size()) break;
|
|
|
|
Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
|
|
|
|
}
|
|
|
|
|
2001-12-09 04:31:32 +08:00
|
|
|
if (FoundAFile)
|
2003-05-23 04:13:16 +08:00
|
|
|
std::cerr << "Bytecode file '" << FN << "' corrupt! "
|
2003-09-16 02:34:34 +08:00
|
|
|
<< "Use 'llvm-link -v ...' for more info.\n";
|
2001-12-09 04:31:32 +08:00
|
|
|
else
|
2003-05-23 04:13:16 +08:00
|
|
|
std::cerr << "Could not locate bytecode file: '" << FN << "'\n";
|
2001-10-24 14:23:00 +08:00
|
|
|
return std::auto_ptr<Module>();
|
|
|
|
}
|
2001-10-13 15:06:23 +08:00
|
|
|
|
2001-12-09 04:31:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2001-10-13 15:06:23 +08:00
|
|
|
int main(int argc, char **argv) {
|
2002-07-22 10:10:13 +08:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
|
2004-08-30 03:28:55 +08:00
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
2001-10-13 15:06:23 +08:00
|
|
|
assert(InputFilenames.size() > 0 && "OneOrMore is not working");
|
|
|
|
|
2001-10-24 14:23:00 +08:00
|
|
|
unsigned BaseArg = 0;
|
2002-01-21 06:54:45 +08:00
|
|
|
std::string ErrorMessage;
|
2001-10-24 14:23:00 +08:00
|
|
|
|
|
|
|
std::auto_ptr<Module> Composite(LoadFile(InputFilenames[BaseArg]));
|
|
|
|
if (Composite.get() == 0) return 1;
|
|
|
|
|
|
|
|
for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
|
2002-01-21 06:54:45 +08:00
|
|
|
std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
|
2001-10-24 14:23:00 +08:00
|
|
|
if (M.get() == 0) return 1;
|
2001-10-24 04:44:55 +08:00
|
|
|
|
2003-05-23 04:13:16 +08:00
|
|
|
if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n";
|
2001-10-24 04:44:55 +08:00
|
|
|
|
2001-10-13 15:06:23 +08:00
|
|
|
if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
|
2003-05-23 04:13:16 +08:00
|
|
|
std::cerr << argv[0] << ": error linking in '" << InputFilenames[i]
|
|
|
|
<< "': " << ErrorMessage << "\n";
|
2001-10-13 15:06:23 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-23 04:13:16 +08:00
|
|
|
if (DumpAsm) std::cerr << "Here's the assembly:\n" << Composite.get();
|
2001-10-15 07:23:33 +08:00
|
|
|
|
2002-06-26 05:57:48 +08:00
|
|
|
std::ostream *Out = &std::cout; // Default to printing to stdout...
|
2003-06-14 00:10:26 +08:00
|
|
|
if (OutputFilename != "-") {
|
2002-01-23 05:07:24 +08:00
|
|
|
if (!Force && std::ifstream(OutputFilename.c_str())) {
|
2002-01-21 06:54:45 +08:00
|
|
|
// If force is not specified, make sure not to overwrite a file!
|
2003-05-23 04:13:16 +08:00
|
|
|
std::cerr << argv[0] << ": error opening '" << OutputFilename
|
|
|
|
<< "': file exists!\n"
|
|
|
|
<< "Use -f command line argument to force output\n";
|
2002-01-21 06:54:45 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
Out = new std::ofstream(OutputFilename.c_str());
|
2001-10-13 15:06:23 +08:00
|
|
|
if (!Out->good()) {
|
2003-05-23 04:13:16 +08:00
|
|
|
std::cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
|
2001-10-13 15:06:23 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2002-04-19 03:55:25 +08:00
|
|
|
|
2003-10-11 01:56:49 +08:00
|
|
|
// Make sure that the Out file gets unlinked from the disk if we get a
|
2002-04-19 03:55:25 +08:00
|
|
|
// SIGINT
|
2004-08-30 03:28:55 +08:00
|
|
|
sys::RemoveFileOnSignal(OutputFilename);
|
2001-10-13 15:06:23 +08:00
|
|
|
}
|
|
|
|
|
2003-08-29 00:25:34 +08:00
|
|
|
if (verifyModule(*Composite.get())) {
|
|
|
|
std::cerr << argv[0] << ": linked module is broken!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-05-23 04:13:16 +08:00
|
|
|
if (Verbose) std::cerr << "Writing bytecode...\n";
|
2001-10-13 15:06:23 +08:00
|
|
|
WriteBytecodeToFile(Composite.get(), *Out);
|
|
|
|
|
2002-01-21 06:54:45 +08:00
|
|
|
if (Out != &std::cout) delete Out;
|
2001-10-13 15:06:23 +08:00
|
|
|
return 0;
|
|
|
|
}
|