2001-10-13 07:06:23 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// LLVM 'LINK' UTILITY
|
|
|
|
//
|
|
|
|
// This utility may be invoked in the following manner:
|
|
|
|
// link a.bc b.bc c.bc -o x.bc
|
|
|
|
//
|
2001-10-23 20:44:55 +00:00
|
|
|
// Alternatively, this can be used as an 'ar' tool as well. If invoked as
|
2001-10-24 06:23:00 +00:00
|
|
|
// either 'ar' or 'llvm-ar', it accepts a 'rc' parameter as well.
|
2001-10-23 20:44:55 +00:00
|
|
|
//
|
2001-10-13 07:06:23 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-05-07 18:36:35 +00:00
|
|
|
#include "llvm/Transforms/Utils/Linker.h"
|
2001-10-13 07:06:23 +00:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
|
|
|
#include "llvm/Bytecode/Writer.h"
|
|
|
|
#include "llvm/Module.h"
|
2001-11-27 00:03:19 +00:00
|
|
|
#include "Support/CommandLine.h"
|
2002-04-18 19:55:25 +00:00
|
|
|
#include "Support/Signals.h"
|
2001-11-27 00:03:19 +00:00
|
|
|
#include <fstream>
|
2001-10-13 07:06:23 +00:00
|
|
|
#include <memory>
|
2001-12-08 20:31:32 +00:00
|
|
|
#include <sys/types.h> // For FileExists
|
|
|
|
#include <sys/stat.h>
|
2001-10-13 07:06:23 +00:00
|
|
|
|
2002-06-25 21:57:48 +00:00
|
|
|
using std::cerr;
|
2001-10-13 07:06:23 +00:00
|
|
|
|
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);
|
|
|
|
|
|
|
|
static cl::list<std::string>
|
|
|
|
LibPaths("L", cl::desc("Specify a library search path"), cl::ZeroOrMore,
|
|
|
|
cl::value_desc("directory"), cl::Prefix);
|
2001-12-08 20:31:32 +00:00
|
|
|
|
|
|
|
// FileExists - Return true if the specified string is an openable file...
|
2002-01-20 22:54:45 +00:00
|
|
|
static inline bool FileExists(const std::string &FN) {
|
2001-12-08 20:31:32 +00:00
|
|
|
struct stat StatBuf;
|
|
|
|
return stat(FN.c_str(), &StatBuf) != -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-20 22:54:45 +00:00
|
|
|
static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
|
|
|
|
std::string Filename = FN;
|
|
|
|
std::string ErrorMessage;
|
2001-10-24 06:23:00 +00:00
|
|
|
|
|
|
|
unsigned NextLibPathIdx = 0;
|
2001-12-08 20:31:32 +00:00
|
|
|
bool FoundAFile = false;
|
2001-10-24 06:23:00 +00:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (Verbose) cerr << "Loading '" << Filename << "'\n";
|
2001-12-08 20:31:32 +00:00
|
|
|
if (FileExists(Filename)) FoundAFile = true;
|
2001-10-24 06:23:00 +00:00
|
|
|
Module *Result = ParseBytecodeFile(Filename, &ErrorMessage);
|
|
|
|
if (Result) return std::auto_ptr<Module>(Result); // Load successful!
|
|
|
|
|
|
|
|
if (Verbose) {
|
|
|
|
cerr << "Error opening bytecode file: '" << Filename << "'";
|
|
|
|
if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
|
2002-04-07 22:34:44 +00:00
|
|
|
cerr << "\n";
|
2001-10-24 06:23:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NextLibPathIdx == LibPaths.size()) break;
|
|
|
|
Filename = LibPaths[NextLibPathIdx++] + "/" + FN;
|
|
|
|
}
|
|
|
|
|
2001-12-08 20:31:32 +00:00
|
|
|
if (FoundAFile)
|
|
|
|
cerr << "Bytecode file '" << FN << "' corrupt! "
|
|
|
|
<< "Use 'link -v ...' for more info.\n";
|
|
|
|
else
|
|
|
|
cerr << "Could not locate bytecode file: '" << FN << "'\n";
|
2001-10-24 06:23:00 +00:00
|
|
|
return std::auto_ptr<Module>();
|
|
|
|
}
|
2001-10-13 07:06:23 +00:00
|
|
|
|
2001-12-08 20:31:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2001-10-13 07:06:23 +00:00
|
|
|
int main(int argc, char **argv) {
|
2002-07-22 02:10:13 +00:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, " llvm linker\n");
|
2001-10-13 07:06:23 +00:00
|
|
|
assert(InputFilenames.size() > 0 && "OneOrMore is not working");
|
|
|
|
|
2001-10-24 06:23:00 +00:00
|
|
|
unsigned BaseArg = 0;
|
2002-01-20 22:54:45 +00:00
|
|
|
std::string ErrorMessage;
|
2001-10-24 06:23:00 +00:00
|
|
|
|
|
|
|
// TODO: TEST argv[0] for llvm-ar forms... for now, this is a huge hack.
|
|
|
|
if (InputFilenames.size() >= 3 && InputFilenames[0] == "rc" &&
|
|
|
|
OutputFilename == "-") {
|
|
|
|
BaseArg = 2;
|
|
|
|
OutputFilename = InputFilenames[1];
|
2001-10-13 07:06:23 +00:00
|
|
|
}
|
|
|
|
|
2001-10-24 06:23:00 +00: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-20 22:54:45 +00:00
|
|
|
std::auto_ptr<Module> M(LoadFile(InputFilenames[i]));
|
2001-10-24 06:23:00 +00:00
|
|
|
if (M.get() == 0) return 1;
|
2001-10-23 20:44:55 +00:00
|
|
|
|
|
|
|
if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
|
|
|
|
|
2001-10-13 07:06:23 +00:00
|
|
|
if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
|
2002-07-30 21:43:25 +00:00
|
|
|
cerr << argv[0] << ": error linking in '" << InputFilenames[i] << "': "
|
2002-04-07 22:34:44 +00:00
|
|
|
<< ErrorMessage << "\n";
|
2001-10-13 07:06:23 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-28 05:13:45 +00:00
|
|
|
if (DumpAsm) cerr << "Here's the assembly:\n" << Composite.get();
|
2001-10-14 23:23:33 +00:00
|
|
|
|
2002-06-25 21:57:48 +00:00
|
|
|
std::ostream *Out = &std::cout; // Default to printing to stdout...
|
2001-10-13 07:06:23 +00:00
|
|
|
if (OutputFilename != "-") {
|
2002-01-22 21:07:24 +00:00
|
|
|
if (!Force && std::ifstream(OutputFilename.c_str())) {
|
2002-01-20 22:54:45 +00:00
|
|
|
// If force is not specified, make sure not to overwrite a file!
|
2002-07-30 21:43:25 +00:00
|
|
|
cerr << argv[0] << ": error opening '" << OutputFilename
|
|
|
|
<< "': file exists!\n"
|
2002-01-20 22:54:45 +00:00
|
|
|
<< "Use -f command line argument to force output\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
Out = new std::ofstream(OutputFilename.c_str());
|
2001-10-13 07:06:23 +00:00
|
|
|
if (!Out->good()) {
|
2002-07-30 21:43:25 +00:00
|
|
|
cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
|
2001-10-13 07:06:23 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2002-04-18 19:55:25 +00:00
|
|
|
|
|
|
|
// Make sure that the Out file gets unlink'd from the disk if we get a
|
|
|
|
// SIGINT
|
|
|
|
RemoveFileOnSignal(OutputFilename);
|
2001-10-13 07:06:23 +00:00
|
|
|
}
|
|
|
|
|
2001-10-23 20:44:55 +00:00
|
|
|
if (Verbose) cerr << "Writing bytecode...\n";
|
2001-10-13 07:06:23 +00:00
|
|
|
WriteBytecodeToFile(Composite.get(), *Out);
|
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
if (Out != &std::cout) delete Out;
|
2001-10-13 07:06:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|