2003-09-20 02:42:54 +00:00
|
|
|
//===- llvm-link.cpp - Low-level LLVM linker ------------------------------===//
|
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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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-06-23 17:33:09 +00:00
|
|
|
#include "llvm/Support/Linker.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/FileUtilities.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"
|
|
|
|
#include "llvm/ADT/SetVector.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);
|
|
|
|
|
|
|
|
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
|
|
|
|
2004-09-11 04:32:42 +00:00
|
|
|
static cl::list<std::string>
|
|
|
|
Libraries("l", cl::desc("Specify library names to link with"), cl::ZeroOrMore,
|
|
|
|
cl::Prefix, cl::value_desc("library name"));
|
|
|
|
|
|
|
|
// GetModule - This function is just factored out of the functions below
|
|
|
|
static inline Module* GetModule(const sys::Path& Filename) {
|
|
|
|
if (Verbose) std::cerr << "Loading '" << Filename.c_str() << "'\n";
|
|
|
|
std::string ErrorMessage;
|
|
|
|
if (Filename.exists()) {
|
|
|
|
Module* Result = ParseBytecodeFile(Filename.get(), &ErrorMessage);
|
|
|
|
if (Result) return Result; // Load successful!
|
|
|
|
|
|
|
|
if (Verbose) {
|
|
|
|
std::cerr << "Error opening bytecode file: '" << Filename.c_str() << "'";
|
|
|
|
if (ErrorMessage.size()) std::cerr << ": " << ErrorMessage;
|
|
|
|
std::cerr << "\n";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
std::cerr << "Bytecode file: '" << Filename.c_str()
|
|
|
|
<< "' does not exist.\n";
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-12-08 20:31:32 +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...
|
|
|
|
//
|
2002-01-20 22:54:45 +00:00
|
|
|
static inline std::auto_ptr<Module> LoadFile(const std::string &FN) {
|
2004-09-11 04:32:42 +00:00
|
|
|
sys::Path Filename;
|
|
|
|
if (!Filename.set_file(FN)) {
|
|
|
|
std::cerr << "Invalid file name: '" << Filename.c_str() << "'\n";
|
|
|
|
return std::auto_ptr<Module>();
|
|
|
|
}
|
2001-10-24 06:23:00 +00:00
|
|
|
|
2004-09-11 04:32:42 +00:00
|
|
|
if (Module* Result = GetModule(Filename))
|
|
|
|
return std::auto_ptr<Module>(Result);
|
2001-10-24 06:23:00 +00:00
|
|
|
|
2004-09-11 04:32:42 +00:00
|
|
|
bool FoundAFile = false;
|
2001-10-24 06:23:00 +00:00
|
|
|
|
2004-09-11 04:32:42 +00:00
|
|
|
for (unsigned i = 0; i < LibPaths.size(); i++) {
|
|
|
|
if (!Filename.set_directory(LibPaths[i])) {
|
|
|
|
std::cerr << "Invalid library path: '" << LibPaths[i] << "'\n";
|
|
|
|
} else if (!Filename.append_file(FN)) {
|
|
|
|
std::cerr << "Invalid library path: '" << LibPaths[i]
|
|
|
|
<< "/" << FN.c_str() << "'\n";
|
|
|
|
} else if (Filename.exists()) {
|
|
|
|
FoundAFile = true;
|
|
|
|
if (Module *Result = GetModule(Filename))
|
|
|
|
return std::auto_ptr<Module>(Result); // Load successful!
|
2001-10-24 06:23:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-08 20:31:32 +00:00
|
|
|
if (FoundAFile)
|
2003-05-22 20:13:16 +00:00
|
|
|
std::cerr << "Bytecode file '" << FN << "' corrupt! "
|
2003-09-15 18:34:34 +00:00
|
|
|
<< "Use 'llvm-link -v ...' for more info.\n";
|
2001-12-08 20:31:32 +00:00
|
|
|
else
|
2003-05-22 20:13:16 +00:00
|
|
|
std::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
|
|
|
|
2004-09-11 04:32:42 +00:00
|
|
|
sys::Path GetPathForLinkageItem(const std::string& link_item,
|
|
|
|
const std::string& dir) {
|
|
|
|
sys::Path fullpath;
|
|
|
|
fullpath.set_directory(dir);
|
|
|
|
|
|
|
|
// Try *.o
|
|
|
|
fullpath.append_file(link_item);
|
|
|
|
fullpath.append_suffix("o");
|
|
|
|
if (fullpath.readable())
|
|
|
|
return fullpath;
|
|
|
|
|
|
|
|
// Try *.bc
|
|
|
|
fullpath.elide_suffix();
|
|
|
|
fullpath.append_suffix("bc");
|
|
|
|
if (fullpath.readable())
|
|
|
|
return fullpath;
|
|
|
|
|
|
|
|
// Try *.so
|
|
|
|
fullpath.elide_suffix();
|
|
|
|
fullpath.append_suffix(sys::Path::GetDLLSuffix());
|
|
|
|
if (fullpath.readable())
|
|
|
|
return fullpath;
|
|
|
|
|
|
|
|
// Try lib*.a
|
|
|
|
fullpath.set_directory(dir);
|
|
|
|
fullpath.append_file(std::string("lib") + link_item);
|
|
|
|
fullpath.append_suffix("a");
|
|
|
|
if (fullpath.readable())
|
|
|
|
return fullpath;
|
|
|
|
|
|
|
|
// Didn't find one.
|
|
|
|
fullpath.clear();
|
|
|
|
return fullpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool LoadLibrary(const std::string &FN, Module*& Result) {
|
|
|
|
Result = 0;
|
|
|
|
sys::Path Filename;
|
|
|
|
if (!Filename.set_file(FN)) {
|
|
|
|
return false;
|
|
|
|
}
|
2001-12-08 20:31:32 +00:00
|
|
|
|
2004-09-11 04:32:42 +00:00
|
|
|
if (Filename.readable() && Filename.is_bytecode_file()) {
|
2004-09-12 23:01:24 +00:00
|
|
|
if ((Result = GetModule(Filename)))
|
2004-09-11 04:32:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
2001-12-08 20:31:32 +00:00
|
|
|
|
2004-09-11 04:32:42 +00:00
|
|
|
bool foundAFile = false;
|
|
|
|
|
|
|
|
for (unsigned I = 0; I < LibPaths.size(); I++) {
|
|
|
|
sys::Path path = GetPathForLinkageItem(FN,LibPaths[I]);
|
|
|
|
if (!path.is_empty()) {
|
|
|
|
if (path.is_bytecode_file()) {
|
2004-09-12 23:01:24 +00:00
|
|
|
if ((Result = GetModule(path))) {
|
2004-09-11 04:32:42 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// We found file but its not a valid bytecode file so we
|
|
|
|
// return false and leave Result null.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We found a file, but its not a bytecode file so we return
|
|
|
|
// false and leave Result null.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We didn't find a file so we leave Result null and return
|
|
|
|
// false to indicate that the library should be just left in the
|
|
|
|
// emitted module as resolvable at runtime.
|
|
|
|
return false;
|
|
|
|
}
|
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");
|
2004-08-29 19:28:55 +00:00
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
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
|
|
|
|
|
|
|
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
|
|
|
|
2003-05-22 20:13:16 +00:00
|
|
|
if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n";
|
2001-10-23 20:44:55 +00:00
|
|
|
|
2001-10-13 07:06:23 +00:00
|
|
|
if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
|
2004-09-11 04:32:42 +00:00
|
|
|
std::cerr << argv[0] << ": link error in '" << InputFilenames[i]
|
2003-05-22 20:13:16 +00:00
|
|
|
<< "': " << ErrorMessage << "\n";
|
2001-10-13 07:06:23 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-11 04:32:42 +00:00
|
|
|
// Get the list of dependent libraries from the composite module
|
|
|
|
const Module::LibraryListType& libs = Composite.get()->getLibraries();
|
|
|
|
|
|
|
|
// Iterate over the list of dependent libraries, linking them in as we
|
|
|
|
// find them
|
|
|
|
Module::LibraryListType::const_iterator I = libs.begin();
|
|
|
|
while (I != libs.end()) {
|
|
|
|
Module* Mod = 0;
|
|
|
|
if (LoadLibrary(*I,Mod)) {
|
|
|
|
if (Mod != 0) {
|
|
|
|
std::auto_ptr<Module> M(Mod);
|
|
|
|
if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
|
|
|
|
std::cerr << argv[0] << ": link error in '" << *I
|
|
|
|
<< "': " << ErrorMessage << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
std::cerr << argv[0] << ": confused loading library '" << *I
|
|
|
|
<< "'. Aborting\n";
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Iterate over the -l list and link in any modules containing
|
|
|
|
// global symbols that have not been resolved so far.
|
|
|
|
|
2003-05-22 20:13:16 +00:00
|
|
|
if (DumpAsm) std::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...
|
2003-06-13 16:10:26 +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!
|
2003-05-22 20:13:16 +00:00
|
|
|
std::cerr << argv[0] << ": error opening '" << OutputFilename
|
|
|
|
<< "': file exists!\n"
|
|
|
|
<< "Use -f command line argument to force output\n";
|
2002-01-20 22:54:45 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
Out = new std::ofstream(OutputFilename.c_str());
|
2001-10-13 07:06:23 +00:00
|
|
|
if (!Out->good()) {
|
2003-05-22 20:13:16 +00:00
|
|
|
std::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
|
|
|
|
2003-10-10 17:56:49 +00:00
|
|
|
// Make sure that the Out file gets unlinked from the disk if we get a
|
2002-04-18 19:55:25 +00:00
|
|
|
// SIGINT
|
2004-08-29 19:28:55 +00:00
|
|
|
sys::RemoveFileOnSignal(OutputFilename);
|
2001-10-13 07:06:23 +00:00
|
|
|
}
|
|
|
|
|
2003-08-28 16:25:34 +00:00
|
|
|
if (verifyModule(*Composite.get())) {
|
|
|
|
std::cerr << argv[0] << ": linked module is broken!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-05-22 20:13:16 +00:00
|
|
|
if (Verbose) std::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;
|
|
|
|
}
|