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