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"
|
2010-11-29 18:16:10 +00:00
|
|
|
#include "llvm/Support/Path.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"
|
2009-08-23 22:45:37 +00:00
|
|
|
#include "llvm/Config/config.h"
|
2010-12-09 17:36:48 +00:00
|
|
|
#include "llvm/Support/system_error.h"
|
2004-12-13 03:00:28 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-11-06 10:58:06 +00:00
|
|
|
Linker::Linker(StringRef progname, StringRef modname,
|
|
|
|
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-11-06 10:58:06 +00:00
|
|
|
Linker::Linker(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-11-06 10:58:06 +00:00
|
|
|
Linker::error(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-11-06 10:58:06 +00:00
|
|
|
Linker::warning(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-11-06 10:58:06 +00:00
|
|
|
Linker::verbose(StringRef message) {
|
2006-11-27 10:09:12 +00:00
|
|
|
if (Flags&Verbose)
|
2010-01-05 01:55:22 +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) {
|
2009-08-23 22:45:37 +00:00
|
|
|
for (unsigned i = 0, e = paths.size(); i != e; ++i)
|
|
|
|
LibPaths.push_back(sys::Path(paths[i]));
|
2004-12-13 03:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2010-11-02 20:32:52 +00:00
|
|
|
|
2010-12-09 17:36:48 +00:00
|
|
|
error_code ec;
|
|
|
|
std::auto_ptr<MemoryBuffer> Buffer(
|
|
|
|
MemoryBuffer::getFileOrSTDIN(FN.c_str(), ec));
|
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
|
2010-12-09 17:36:48 +00:00
|
|
|
ParseErrorMessage = "Error reading file '" + FN.str() + "'" + ": "
|
|
|
|
+ ec.message();
|
2010-11-02 20:32:52 +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);
|
2009-08-23 22:45:37 +00:00
|
|
|
Error = "Bitcode file '" + FN.str() + "' 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-11-06 10:58:06 +00:00
|
|
|
static inline sys::Path IsLibrary(StringRef Name,
|
2009-07-25 06:02:13 +00:00
|
|
|
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();
|
2010-11-02 20:32:59 +00:00
|
|
|
FullPath.appendSuffix(sys::Path::GetDLLSuffix());
|
2006-07-28 22:52:11 +00:00
|
|
|
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-11-06 10:58:06 +00:00
|
|
|
Linker::FindLib(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();
|
|
|
|
}
|