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
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Linker.h"
|
|
|
|
#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"
|
|
|
|
#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
|
|
|
|
|
|
|
|
|
|
|
cl::StringList InputFilenames("", "Load <arg> files, linking them together",
|
|
|
|
cl::OneOrMore);
|
|
|
|
cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "-");
|
|
|
|
cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
|
2001-10-23 20:44:55 +00:00
|
|
|
cl::Flag Verbose ("v", "Print information about actions taken");
|
2001-10-14 23:23:33 +00:00
|
|
|
cl::Flag DumpAsm ("d", "Print assembly as linked", cl::Hidden, false);
|
2001-10-24 06:23:00 +00:00
|
|
|
cl::StringList LibPaths ("L", "Specify a library search path", cl::ZeroOrMore);
|
2002-01-25 03:59:39 +00:00
|
|
|
cl::StringList Libraries ("l", "Specify libraries to link to", cl::ZeroOrMore);
|
2001-10-13 07:06:23 +00:00
|
|
|
|
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) {
|
2001-11-26 19:18:30 +00:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, " llvm linker\n",
|
|
|
|
cl::EnableSingleLetterArgValue |
|
|
|
|
cl::DisableSingleLetterArgGrouping);
|
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
|
|
|
}
|
|
|
|
|
2002-01-25 03:59:39 +00:00
|
|
|
if (!Libraries.empty())
|
|
|
|
cerr << "LLVM Linker Warning: Linking to libraries is unimplemented!\n";
|
|
|
|
|
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)) {
|
|
|
|
cerr << "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-07 22:34:44 +00:00
|
|
|
if (DumpAsm) {
|
|
|
|
cerr << "Here's the assembly:\n";
|
|
|
|
Composite.get()->dump();
|
|
|
|
}
|
2001-10-14 23:23:33 +00:00
|
|
|
|
2001-10-13 07:06:23 +00:00
|
|
|
ostream *Out = &cout; // Default to printing to stdout...
|
|
|
|
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!
|
|
|
|
cerr << "Error opening '" << OutputFilename << "': File exists!\n"
|
|
|
|
<< "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()) {
|
|
|
|
cerr << "Error opening '" << OutputFilename << "'!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|