2004-12-13 03:50:50 +00:00
|
|
|
//===- lib/Linker/Linker.cpp - Basic Linker functionality ----------------===//
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2004-12-13 03:00:28 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2004-12-13 03:00:28 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2004-12-13 03:50:50 +00:00
|
|
|
// This file contains basic Linker functionality that all usages will need.
|
2004-12-13 03:00:28 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Linker.h"
|
|
|
|
#include "llvm/Module.h"
|
2007-05-06 06:02:13 +00:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2004-12-16 19:19:24 +00:00
|
|
|
#include "llvm/Config/config.h"
|
2007-05-06 06:02:13 +00:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2009-07-25 06:02:13 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2004-12-13 03:00:28 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-07-25 06:02:13 +00:00
|
|
|
Linker::Linker(const StringRef &progname, const StringRef &modname,
|
2009-07-01 23:13:44 +00:00
|
|
|
LLVMContext& C, unsigned flags):
|
2009-07-01 16:58:40 +00:00
|
|
|
Context(C),
|
|
|
|
Composite(new Module(modname, C)),
|
|
|
|
LibPaths(),
|
|
|
|
Flags(flags),
|
|
|
|
Error(),
|
|
|
|
ProgramName(progname) { }
|
2004-12-13 03:00:28 +00:00
|
|
|
|
2009-07-25 06:02:13 +00:00
|
|
|
Linker::Linker(const StringRef &progname, Module* aModule, unsigned flags) :
|
2009-07-01 16:58:40 +00:00
|
|
|
Context(aModule->getContext()),
|
|
|
|
Composite(aModule),
|
|
|
|
LibPaths(),
|
|
|
|
Flags(flags),
|
|
|
|
Error(),
|
|
|
|
ProgramName(progname) { }
|
2004-12-13 03:00:28 +00:00
|
|
|
|
|
|
|
Linker::~Linker() {
|
|
|
|
delete Composite;
|
|
|
|
}
|
|
|
|
|
2005-04-21 22:55:34 +00:00
|
|
|
bool
|
2009-07-25 06:02:13 +00:00
|
|
|
Linker::error(const StringRef &message) {
|
2004-12-13 03:00:28 +00:00
|
|
|
Error = message;
|
2006-11-27 10:09:12 +00:00
|
|
|
if (!(Flags&QuietErrors))
|
2009-07-25 06:02:13 +00:00
|
|
|
errs() << ProgramName << ": error: " << message << "\n";
|
2004-12-13 03:00:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2009-07-25 06:02:13 +00:00
|
|
|
Linker::warning(const StringRef &message) {
|
2004-12-13 03:00:28 +00:00
|
|
|
Error = message;
|
2008-10-25 17:57:20 +00:00
|
|
|
if (!(Flags&QuietWarnings))
|
2009-07-25 06:02:13 +00:00
|
|
|
errs() << ProgramName << ": warning: " << message << "\n";
|
2004-12-13 03:00:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-07-25 06:02:13 +00:00
|
|
|
Linker::verbose(const StringRef &message) {
|
2006-11-27 10:09:12 +00:00
|
|
|
if (Flags&Verbose)
|
2009-07-25 06:02:13 +00:00
|
|
|
errs() << " " << message << "\n";
|
2004-12-13 03:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Linker::addPath(const sys::Path& path) {
|
|
|
|
LibPaths.push_back(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Linker::addPaths(const std::vector<std::string>& paths) {
|
2004-12-13 03:50:50 +00:00
|
|
|
for (unsigned i = 0; i != paths.size(); ++i) {
|
2004-12-13 03:00:28 +00:00
|
|
|
sys::Path aPath;
|
2005-07-07 23:21:43 +00:00
|
|
|
aPath.set(paths[i]);
|
2004-12-13 03:00:28 +00:00
|
|
|
LibPaths.push_back(aPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Linker::addSystemPaths() {
|
2007-07-05 17:07:56 +00:00
|
|
|
sys::Path::GetBitcodeLibraryPaths(LibPaths);
|
2004-12-13 03:00:28 +00:00
|
|
|
LibPaths.insert(LibPaths.begin(),sys::Path("./"));
|
|
|
|
}
|
|
|
|
|
|
|
|
Module*
|
|
|
|
Linker::releaseModule() {
|
|
|
|
Module* result = Composite;
|
|
|
|
LibPaths.clear();
|
|
|
|
Error.clear();
|
2004-12-13 03:50:50 +00:00
|
|
|
Composite = 0;
|
2004-12-13 03:00:28 +00:00
|
|
|
Flags = 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-07-05 17:07:56 +00:00
|
|
|
// LoadObject - Read in and parse the bitcode file named by FN and return the
|
2005-04-21 22:55:34 +00:00
|
|
|
// module it contains (wrapped in an auto_ptr), or auto_ptr<Module>() and set
|
2004-12-13 03:00:28 +00:00
|
|
|
// Error if an error occurs.
|
2005-04-21 22:55:34 +00:00
|
|
|
std::auto_ptr<Module>
|
2004-12-13 03:00:28 +00:00
|
|
|
Linker::LoadObject(const sys::Path &FN) {
|
|
|
|
std::string ParseErrorMessage;
|
2007-05-06 06:02:13 +00:00
|
|
|
Module *Result = 0;
|
|
|
|
|
|
|
|
const std::string &FNS = FN.toString();
|
2008-04-01 18:04:03 +00:00
|
|
|
std::auto_ptr<MemoryBuffer> Buffer(MemoryBuffer::getFileOrSTDIN(FNS.c_str()));
|
2007-05-06 09:29:13 +00:00
|
|
|
if (Buffer.get())
|
2009-07-01 16:58:40 +00:00
|
|
|
Result = ParseBitcodeFile(Buffer.get(), Context, &ParseErrorMessage);
|
2007-05-06 09:29:13 +00:00
|
|
|
else
|
|
|
|
ParseErrorMessage = "Error reading file '" + FNS + "'";
|
2007-05-06 06:02:13 +00:00
|
|
|
|
2005-04-21 22:55:34 +00:00
|
|
|
if (Result)
|
2004-12-13 03:00:28 +00:00
|
|
|
return std::auto_ptr<Module>(Result);
|
2007-07-05 17:07:56 +00:00
|
|
|
Error = "Bitcode file '" + FN.toString() + "' could not be loaded";
|
2005-04-21 22:55:34 +00:00
|
|
|
if (ParseErrorMessage.size())
|
2004-12-13 03:00:28 +00:00
|
|
|
Error += ": " + ParseErrorMessage;
|
|
|
|
return std::auto_ptr<Module>();
|
|
|
|
}
|
|
|
|
|
2005-04-21 22:55:34 +00:00
|
|
|
// IsLibrary - Determine if "Name" is a library in "Directory". Return
|
2004-12-13 03:50:50 +00:00
|
|
|
// a non-empty sys::Path if its found, an empty one otherwise.
|
2009-07-25 06:02:13 +00:00
|
|
|
static inline sys::Path IsLibrary(const StringRef &Name,
|
|
|
|
const sys::Path &Directory) {
|
2004-12-13 03:00:28 +00:00
|
|
|
|
|
|
|
sys::Path FullPath(Directory);
|
|
|
|
|
2006-07-28 22:52:11 +00:00
|
|
|
// Try the libX.a form
|
2009-07-25 06:02:13 +00:00
|
|
|
FullPath.appendComponent(("lib" + Name).str());
|
2006-07-28 22:52:11 +00:00
|
|
|
FullPath.appendSuffix("a");
|
|
|
|
if (FullPath.isArchive())
|
|
|
|
return FullPath;
|
|
|
|
|
|
|
|
// Try the libX.bca form
|
|
|
|
FullPath.eraseSuffix();
|
|
|
|
FullPath.appendSuffix("bca");
|
|
|
|
if (FullPath.isArchive())
|
|
|
|
return FullPath;
|
|
|
|
|
|
|
|
// Try the libX.so (or .dylib) form
|
|
|
|
FullPath.eraseSuffix();
|
|
|
|
FullPath.appendSuffix(&(LTDL_SHLIB_EXT[1]));
|
|
|
|
if (FullPath.isDynamicLibrary()) // Native shared library?
|
|
|
|
return FullPath;
|
2007-05-06 06:02:13 +00:00
|
|
|
if (FullPath.isBitcodeFile()) // .so file containing bitcode?
|
|
|
|
return FullPath;
|
2006-07-28 22:52:11 +00:00
|
|
|
|
|
|
|
// Not found .. fall through
|
2004-12-13 03:00:28 +00:00
|
|
|
|
2005-07-07 23:21:43 +00:00
|
|
|
// Indicate that the library was not found in the directory.
|
2004-12-13 03:00:28 +00:00
|
|
|
FullPath.clear();
|
|
|
|
return FullPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// FindLib - Try to convert Filename into the name of a file that we can open,
|
|
|
|
/// if it does not already name a file we can open, by first trying to open
|
|
|
|
/// Filename, then libFilename.[suffix] for each of a set of several common
|
2005-04-21 22:55:34 +00:00
|
|
|
/// library suffixes, in each of the directories in LibPaths. Returns an empty
|
2004-12-13 03:50:50 +00:00
|
|
|
/// Path if no matching file can be found.
|
2004-12-13 03:00:28 +00:00
|
|
|
///
|
2005-04-21 22:55:34 +00:00
|
|
|
sys::Path
|
2009-07-25 06:02:13 +00:00
|
|
|
Linker::FindLib(const StringRef &Filename) {
|
2004-12-13 03:00:28 +00:00
|
|
|
// Determine if the pathname can be found as it stands.
|
|
|
|
sys::Path FilePath(Filename);
|
2005-07-07 18:21:42 +00:00
|
|
|
if (FilePath.canRead() &&
|
2004-12-13 03:00:28 +00:00
|
|
|
(FilePath.isArchive() || FilePath.isDynamicLibrary()))
|
|
|
|
return FilePath;
|
|
|
|
|
|
|
|
// Iterate over the directories in Paths to see if we can find the library
|
|
|
|
// there.
|
|
|
|
for (unsigned Index = 0; Index != LibPaths.size(); ++Index) {
|
|
|
|
sys::Path Directory(LibPaths[Index]);
|
2009-07-25 06:02:13 +00:00
|
|
|
sys::Path FullPath = IsLibrary(Filename, Directory);
|
2004-12-13 03:00:28 +00:00
|
|
|
if (!FullPath.isEmpty())
|
|
|
|
return FullPath;
|
|
|
|
}
|
|
|
|
return sys::Path();
|
|
|
|
}
|