2004-12-05 19:14:55 +00:00
|
|
|
//===- lib/Linker/LinkItems.cpp - Link LLVM objects and libraries ---------===//
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2004-12-05 19:14:55 +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-05 19:14:55 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2007-07-05 17:07:56 +00:00
|
|
|
// This file contains routines to handle linking together LLVM bitcode files,
|
2004-12-05 19:14:55 +00:00
|
|
|
// and to handle annoying things like static libraries.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Linker.h"
|
|
|
|
#include "llvm/Module.h"
|
2009-08-23 22:45:37 +00:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
|
|
|
#include "llvm/System/Path.h"
|
2009-07-11 20:10:48 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2007-08-08 19:52:29 +00:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2004-12-13 02:59:52 +00:00
|
|
|
using namespace llvm;
|
2004-12-05 19:14:55 +00:00
|
|
|
|
2006-01-10 03:14:40 +00:00
|
|
|
// LinkItems - This function is the main entry point into linking. It takes a
|
|
|
|
// list of LinkItem which indicates the order the files should be linked and
|
|
|
|
// how each file should be treated (plain file or with library search). The
|
2007-07-05 17:07:56 +00:00
|
|
|
// function only links bitcode and produces a result list of items that are
|
2006-01-10 03:14:40 +00:00
|
|
|
// native objects.
|
2004-12-13 02:59:52 +00:00
|
|
|
bool
|
2006-01-10 03:14:40 +00:00
|
|
|
Linker::LinkInItems(const ItemList& Items, ItemList& NativeItems) {
|
|
|
|
// Clear the NativeItems just in case
|
|
|
|
NativeItems.clear();
|
|
|
|
|
2004-12-05 19:14:55 +00:00
|
|
|
// For each linkage item ...
|
2005-04-21 22:55:34 +00:00
|
|
|
for (ItemList::const_iterator I = Items.begin(), E = Items.end();
|
2004-12-05 19:14:55 +00:00
|
|
|
I != E; ++I) {
|
|
|
|
if (I->second) {
|
|
|
|
// Link in the library suggested.
|
2007-04-04 06:44:18 +00:00
|
|
|
bool is_native = false;
|
|
|
|
if (LinkInLibrary(I->first, is_native))
|
2004-12-13 02:59:52 +00:00
|
|
|
return true;
|
2007-04-04 06:44:18 +00:00
|
|
|
if (is_native)
|
2006-01-10 03:14:40 +00:00
|
|
|
NativeItems.push_back(*I);
|
2004-12-05 19:14:55 +00:00
|
|
|
} else {
|
2006-01-10 03:14:40 +00:00
|
|
|
// Link in the file suggested
|
2007-04-04 06:33:17 +00:00
|
|
|
bool is_native = false;
|
|
|
|
if (LinkInFile(sys::Path(I->first), is_native))
|
2004-12-13 02:59:52 +00:00
|
|
|
return true;
|
2007-04-04 06:33:17 +00:00
|
|
|
if (is_native)
|
|
|
|
NativeItems.push_back(*I);
|
2004-12-05 19:14:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point we have processed all the link items provided to us. Since
|
|
|
|
// we have an aggregated module at this point, the dependent libraries in
|
|
|
|
// that module should also be aggregated with duplicates eliminated. This is
|
|
|
|
// now the time to process the dependent libraries to resolve any remaining
|
|
|
|
// symbols.
|
2007-04-04 06:44:18 +00:00
|
|
|
bool is_native;
|
2005-04-21 22:55:34 +00:00
|
|
|
for (Module::lib_iterator I = Composite->lib_begin(),
|
2007-04-30 00:00:10 +00:00
|
|
|
E = Composite->lib_end(); I != E; ++I) {
|
2007-04-04 06:44:18 +00:00
|
|
|
if(LinkInLibrary(*I, is_native))
|
2004-12-13 02:59:52 +00:00
|
|
|
return true;
|
2007-04-30 00:00:10 +00:00
|
|
|
if (is_native)
|
|
|
|
NativeItems.push_back(std::make_pair(*I, true));
|
|
|
|
}
|
2004-12-05 19:14:55 +00:00
|
|
|
|
2004-12-13 02:59:52 +00:00
|
|
|
return false;
|
2004-12-05 19:14:55 +00:00
|
|
|
}
|
2005-03-15 22:51:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// LinkInLibrary - links one library into the HeadModule.
|
|
|
|
///
|
2009-07-25 06:02:13 +00:00
|
|
|
bool Linker::LinkInLibrary(const StringRef &Lib, bool& is_native) {
|
2007-04-04 06:33:17 +00:00
|
|
|
is_native = false;
|
2005-03-15 22:51:40 +00:00
|
|
|
// Determine where this library lives.
|
|
|
|
sys::Path Pathname = FindLib(Lib);
|
|
|
|
if (Pathname.isEmpty())
|
2009-07-25 06:02:13 +00:00
|
|
|
return error("Cannot find library '" + Lib.str() + "'");
|
2005-03-15 22:51:40 +00:00
|
|
|
|
|
|
|
// If its an archive, try to link it in
|
2006-01-10 03:14:40 +00:00
|
|
|
std::string Magic;
|
|
|
|
Pathname.getMagicNumber(Magic, 64);
|
|
|
|
switch (sys::IdentifyFileType(Magic.c_str(), 64)) {
|
2009-07-14 16:55:14 +00:00
|
|
|
default: llvm_unreachable("Bad file type identification");
|
2007-04-04 06:33:17 +00:00
|
|
|
case sys::Unknown_FileType:
|
2009-07-25 06:02:13 +00:00
|
|
|
return warning("Supposed library '" + Lib.str() + "' isn't a library.");
|
2007-04-04 06:33:17 +00:00
|
|
|
|
2007-05-06 06:02:13 +00:00
|
|
|
case sys::Bitcode_FileType:
|
2006-01-10 03:14:40 +00:00
|
|
|
// LLVM ".so" file.
|
2007-04-04 06:33:17 +00:00
|
|
|
if (LinkInFile(Pathname, is_native))
|
2007-08-16 07:47:30 +00:00
|
|
|
return true;
|
2006-01-10 03:14:40 +00:00
|
|
|
break;
|
2007-04-04 06:33:17 +00:00
|
|
|
|
|
|
|
case sys::Archive_FileType:
|
2007-04-30 00:29:39 +00:00
|
|
|
if (LinkInArchive(Pathname, is_native))
|
2009-08-23 22:45:37 +00:00
|
|
|
return error("Cannot link archive '" + Pathname.str() + "'");
|
2006-01-10 03:14:40 +00:00
|
|
|
break;
|
2007-04-04 06:33:17 +00:00
|
|
|
|
2007-04-11 02:44:20 +00:00
|
|
|
case sys::ELF_Relocatable_FileType:
|
|
|
|
case sys::ELF_SharedObject_FileType:
|
|
|
|
case sys::Mach_O_Object_FileType:
|
|
|
|
case sys::Mach_O_FixedVirtualMemorySharedLib_FileType:
|
|
|
|
case sys::Mach_O_DynamicallyLinkedSharedLib_FileType:
|
|
|
|
case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType:
|
2007-04-04 06:33:17 +00:00
|
|
|
case sys::COFF_FileType:
|
|
|
|
is_native = true;
|
|
|
|
break;
|
2005-03-15 22:51:40 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// LinkLibraries - takes the specified library files and links them into the
|
2007-07-05 17:07:56 +00:00
|
|
|
/// main bitcode object file.
|
2005-03-15 22:51:40 +00:00
|
|
|
///
|
|
|
|
/// Inputs:
|
|
|
|
/// Libraries - The list of libraries to link into the module.
|
|
|
|
///
|
|
|
|
/// Return value:
|
|
|
|
/// FALSE - No error.
|
|
|
|
/// TRUE - Error.
|
|
|
|
///
|
|
|
|
bool Linker::LinkInLibraries(const std::vector<std::string> &Libraries) {
|
|
|
|
|
|
|
|
// Process the set of libraries we've been provided.
|
2007-04-04 06:44:18 +00:00
|
|
|
bool is_native = false;
|
2005-03-15 22:51:40 +00:00
|
|
|
for (unsigned i = 0; i < Libraries.size(); ++i)
|
2007-04-04 06:44:18 +00:00
|
|
|
if (LinkInLibrary(Libraries[i], is_native))
|
2005-03-15 22:51:40 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// At this point we have processed all the libraries provided to us. Since
|
|
|
|
// we have an aggregated module at this point, the dependent libraries in
|
|
|
|
// that module should also be aggregated with duplicates eliminated. This is
|
|
|
|
// now the time to process the dependent libraries to resolve any remaining
|
|
|
|
// symbols.
|
|
|
|
const Module::LibraryListType& DepLibs = Composite->getLibraries();
|
2005-04-21 22:55:34 +00:00
|
|
|
for (Module::LibraryListType::const_iterator I = DepLibs.begin(),
|
|
|
|
E = DepLibs.end(); I != E; ++I)
|
2007-04-04 06:44:18 +00:00
|
|
|
if (LinkInLibrary(*I, is_native))
|
2005-03-15 22:51:40 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2005-03-15 22:55:17 +00:00
|
|
|
|
2007-07-05 17:07:56 +00:00
|
|
|
/// LinkInFile - opens a bitcode file and links in all objects which
|
2005-03-15 22:55:17 +00:00
|
|
|
/// provide symbols that are currently undefined.
|
|
|
|
///
|
|
|
|
/// Inputs:
|
2007-07-05 17:07:56 +00:00
|
|
|
/// File - The pathname of the bitcode file.
|
2005-03-15 22:55:17 +00:00
|
|
|
///
|
|
|
|
/// Outputs:
|
|
|
|
/// ErrorMessage - A C++ string detailing what error occurred, if any.
|
|
|
|
///
|
|
|
|
/// Return Value:
|
|
|
|
/// TRUE - An error occurred.
|
|
|
|
/// FALSE - No errors.
|
|
|
|
///
|
2007-04-04 06:33:17 +00:00
|
|
|
bool Linker::LinkInFile(const sys::Path &File, bool &is_native) {
|
|
|
|
is_native = false;
|
2007-08-08 19:52:29 +00:00
|
|
|
|
|
|
|
// Check for a file of name "-", which means "read standard input"
|
2009-08-23 22:45:37 +00:00
|
|
|
if (File.str() == "-") {
|
2007-08-08 19:52:29 +00:00
|
|
|
std::auto_ptr<Module> M;
|
|
|
|
if (MemoryBuffer *Buffer = MemoryBuffer::getSTDIN()) {
|
2009-07-01 16:58:40 +00:00
|
|
|
M.reset(ParseBitcodeFile(Buffer, Context, &Error));
|
2007-08-08 19:52:29 +00:00
|
|
|
delete Buffer;
|
2007-08-16 07:23:37 +00:00
|
|
|
if (M.get())
|
|
|
|
if (!LinkInModule(M.get(), &Error))
|
|
|
|
return false;
|
2007-08-08 19:52:29 +00:00
|
|
|
} else
|
|
|
|
Error = "standard input is empty";
|
|
|
|
return error("Cannot link stdin: " + Error);
|
|
|
|
}
|
|
|
|
|
2005-03-15 22:55:17 +00:00
|
|
|
// Make sure we can at least read the file
|
2005-07-07 18:21:42 +00:00
|
|
|
if (!File.canRead())
|
2009-08-23 22:45:37 +00:00
|
|
|
return error("Cannot find linker input '" + File.str() + "'");
|
2005-03-15 22:55:17 +00:00
|
|
|
|
2007-04-04 06:33:17 +00:00
|
|
|
// If its an archive, try to link it in
|
|
|
|
std::string Magic;
|
|
|
|
File.getMagicNumber(Magic, 64);
|
|
|
|
switch (sys::IdentifyFileType(Magic.c_str(), 64)) {
|
2009-07-14 16:55:14 +00:00
|
|
|
default: llvm_unreachable("Bad file type identification");
|
2007-04-04 06:33:17 +00:00
|
|
|
case sys::Unknown_FileType:
|
2009-08-23 22:45:37 +00:00
|
|
|
return warning("Ignoring file '" + File.str() +
|
2007-08-16 07:47:30 +00:00
|
|
|
"' because does not contain bitcode.");
|
2007-04-04 06:33:17 +00:00
|
|
|
|
|
|
|
case sys::Archive_FileType:
|
|
|
|
// A user may specify an ar archive without -l, perhaps because it
|
|
|
|
// is not installed as a library. Detect that and link the archive.
|
2009-08-23 22:45:37 +00:00
|
|
|
verbose("Linking archive file '" + File.str() + "'");
|
2007-04-30 00:29:39 +00:00
|
|
|
if (LinkInArchive(File, is_native))
|
2007-08-16 07:47:30 +00:00
|
|
|
return true;
|
2007-04-04 06:33:17 +00:00
|
|
|
break;
|
|
|
|
|
2007-07-06 13:38:17 +00:00
|
|
|
case sys::Bitcode_FileType: {
|
2009-08-23 22:45:37 +00:00
|
|
|
verbose("Linking bitcode file '" + File.str() + "'");
|
2007-04-04 06:33:17 +00:00
|
|
|
std::auto_ptr<Module> M(LoadObject(File));
|
|
|
|
if (M.get() == 0)
|
2009-08-23 22:45:37 +00:00
|
|
|
return error("Cannot load file '" + File.str() + "': " + Error);
|
2007-08-16 07:23:37 +00:00
|
|
|
if (LinkInModule(M.get(), &Error))
|
2009-08-23 22:45:37 +00:00
|
|
|
return error("Cannot link file '" + File.str() + "': " + Error);
|
2007-04-04 06:33:17 +00:00
|
|
|
|
2009-08-23 22:45:37 +00:00
|
|
|
verbose("Linked in file '" + File.str() + "'");
|
2007-04-04 06:33:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-04-11 02:44:20 +00:00
|
|
|
case sys::ELF_Relocatable_FileType:
|
|
|
|
case sys::ELF_SharedObject_FileType:
|
|
|
|
case sys::Mach_O_Object_FileType:
|
|
|
|
case sys::Mach_O_FixedVirtualMemorySharedLib_FileType:
|
|
|
|
case sys::Mach_O_DynamicallyLinkedSharedLib_FileType:
|
|
|
|
case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType:
|
2007-04-04 06:33:17 +00:00
|
|
|
case sys::COFF_FileType:
|
|
|
|
is_native = true;
|
|
|
|
break;
|
2005-03-15 22:55:17 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// LinkFiles - takes a module and a list of files and links them all together.
|
|
|
|
/// It locates the file either in the current directory, as its absolute
|
|
|
|
/// or relative pathname, or as a file somewhere in LLVM_LIB_SEARCH_PATH.
|
|
|
|
///
|
|
|
|
/// Inputs:
|
2007-07-05 17:07:56 +00:00
|
|
|
/// Files - A vector of sys::Path indicating the LLVM bitcode filenames
|
2005-03-15 22:55:17 +00:00
|
|
|
/// to be linked. The names can refer to a mixture of pure LLVM
|
2007-07-05 17:07:56 +00:00
|
|
|
/// bitcode files and archive (ar) formatted files.
|
2005-03-15 22:55:17 +00:00
|
|
|
///
|
|
|
|
/// Return value:
|
|
|
|
/// FALSE - No errors.
|
|
|
|
/// TRUE - Some error occurred.
|
|
|
|
///
|
|
|
|
bool Linker::LinkInFiles(const std::vector<sys::Path> &Files) {
|
2007-04-04 06:33:17 +00:00
|
|
|
bool is_native;
|
2005-03-15 22:55:17 +00:00
|
|
|
for (unsigned i = 0; i < Files.size(); ++i)
|
2007-04-04 06:33:17 +00:00
|
|
|
if (LinkInFile(Files[i], is_native))
|
2005-03-15 22:55:17 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|