2004-12-05 20:14:55 +01:00
|
|
|
//===- lib/Linker/LinkItems.cpp - Link LLVM objects and libraries ---------===//
|
2005-04-22 00:55:34 +02:00
|
|
|
//
|
2004-12-05 20:14:55 +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-05 20:14:55 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2007-07-05 19:07:56 +02:00
|
|
|
// This file contains routines to handle linking together LLVM bitcode files,
|
2004-12-05 20:14:55 +01:00
|
|
|
// and to handle annoying things like static libraries.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Linker.h"
|
|
|
|
#include "llvm/Module.h"
|
2007-08-08 21:52:29 +02:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2004-12-05 20:14:55 +01:00
|
|
|
|
2004-12-13 03:59:52 +01:00
|
|
|
using namespace llvm;
|
2004-12-05 20:14:55 +01:00
|
|
|
|
2006-01-10 04:14:40 +01: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 19:07:56 +02:00
|
|
|
// function only links bitcode and produces a result list of items that are
|
2006-01-10 04:14:40 +01:00
|
|
|
// native objects.
|
2004-12-13 03:59:52 +01:00
|
|
|
bool
|
2006-01-10 04:14:40 +01:00
|
|
|
Linker::LinkInItems(const ItemList& Items, ItemList& NativeItems) {
|
|
|
|
// Clear the NativeItems just in case
|
|
|
|
NativeItems.clear();
|
|
|
|
|
2004-12-05 20:14:55 +01:00
|
|
|
// For each linkage item ...
|
2005-04-22 00:55:34 +02:00
|
|
|
for (ItemList::const_iterator I = Items.begin(), E = Items.end();
|
2004-12-05 20:14:55 +01:00
|
|
|
I != E; ++I) {
|
|
|
|
if (I->second) {
|
|
|
|
// Link in the library suggested.
|
2007-04-04 08:44:18 +02:00
|
|
|
bool is_native = false;
|
|
|
|
if (LinkInLibrary(I->first, is_native))
|
2004-12-13 03:59:52 +01:00
|
|
|
return true;
|
2007-04-04 08:44:18 +02:00
|
|
|
if (is_native)
|
2006-01-10 04:14:40 +01:00
|
|
|
NativeItems.push_back(*I);
|
2004-12-05 20:14:55 +01:00
|
|
|
} else {
|
2006-01-10 04:14:40 +01:00
|
|
|
// Link in the file suggested
|
2007-04-04 08:33:17 +02:00
|
|
|
bool is_native = false;
|
|
|
|
if (LinkInFile(sys::Path(I->first), is_native))
|
2004-12-13 03:59:52 +01:00
|
|
|
return true;
|
2007-04-04 08:33:17 +02:00
|
|
|
if (is_native)
|
|
|
|
NativeItems.push_back(*I);
|
2004-12-05 20:14:55 +01: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 08:44:18 +02:00
|
|
|
bool is_native;
|
2005-04-22 00:55:34 +02:00
|
|
|
for (Module::lib_iterator I = Composite->lib_begin(),
|
2007-04-30 02:00:10 +02:00
|
|
|
E = Composite->lib_end(); I != E; ++I) {
|
2007-04-04 08:44:18 +02:00
|
|
|
if(LinkInLibrary(*I, is_native))
|
2004-12-13 03:59:52 +01:00
|
|
|
return true;
|
2007-04-30 02:00:10 +02:00
|
|
|
if (is_native)
|
|
|
|
NativeItems.push_back(std::make_pair(*I, true));
|
|
|
|
}
|
2004-12-05 20:14:55 +01:00
|
|
|
|
2004-12-13 03:59:52 +01:00
|
|
|
return false;
|
2004-12-05 20:14:55 +01:00
|
|
|
}
|
2005-03-15 23:51:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
/// LinkInLibrary - links one library into the HeadModule.
|
|
|
|
///
|
2007-04-04 08:33:17 +02:00
|
|
|
bool Linker::LinkInLibrary(const std::string& Lib, bool& is_native) {
|
|
|
|
is_native = false;
|
2005-03-15 23:51:40 +01:00
|
|
|
// Determine where this library lives.
|
|
|
|
sys::Path Pathname = FindLib(Lib);
|
|
|
|
if (Pathname.isEmpty())
|
2008-02-27 18:20:32 +01:00
|
|
|
return error("Cannot find library '" + Lib + "'");
|
2005-03-15 23:51:40 +01:00
|
|
|
|
|
|
|
// If its an archive, try to link it in
|
2006-01-10 04:14:40 +01:00
|
|
|
std::string Magic;
|
|
|
|
Pathname.getMagicNumber(Magic, 64);
|
|
|
|
switch (sys::IdentifyFileType(Magic.c_str(), 64)) {
|
2007-04-04 08:33:17 +02:00
|
|
|
default: assert(0 && "Bad file type identification");
|
|
|
|
case sys::Unknown_FileType:
|
|
|
|
return warning("Supposed library '" + Lib + "' isn't a library.");
|
|
|
|
|
2007-05-06 08:02:13 +02:00
|
|
|
case sys::Bitcode_FileType:
|
2006-01-10 04:14:40 +01:00
|
|
|
// LLVM ".so" file.
|
2007-04-04 08:33:17 +02:00
|
|
|
if (LinkInFile(Pathname, is_native))
|
2007-08-16 09:47:30 +02:00
|
|
|
return true;
|
2006-01-10 04:14:40 +01:00
|
|
|
break;
|
2007-04-04 08:33:17 +02:00
|
|
|
|
|
|
|
case sys::Archive_FileType:
|
2007-04-30 02:29:39 +02:00
|
|
|
if (LinkInArchive(Pathname, is_native))
|
2006-01-10 04:14:40 +01:00
|
|
|
return error("Cannot link archive '" + Pathname.toString() + "'");
|
|
|
|
break;
|
2007-04-04 08:33:17 +02:00
|
|
|
|
2007-04-11 04:44:20 +02: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 08:33:17 +02:00
|
|
|
case sys::COFF_FileType:
|
|
|
|
is_native = true;
|
|
|
|
break;
|
2005-03-15 23:51:40 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// LinkLibraries - takes the specified library files and links them into the
|
2007-07-05 19:07:56 +02:00
|
|
|
/// main bitcode object file.
|
2005-03-15 23:51:40 +01: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 08:44:18 +02:00
|
|
|
bool is_native = false;
|
2005-03-15 23:51:40 +01:00
|
|
|
for (unsigned i = 0; i < Libraries.size(); ++i)
|
2007-04-04 08:44:18 +02:00
|
|
|
if (LinkInLibrary(Libraries[i], is_native))
|
2005-03-15 23:51:40 +01: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-22 00:55:34 +02:00
|
|
|
for (Module::LibraryListType::const_iterator I = DepLibs.begin(),
|
|
|
|
E = DepLibs.end(); I != E; ++I)
|
2007-04-04 08:44:18 +02:00
|
|
|
if (LinkInLibrary(*I, is_native))
|
2005-03-15 23:51:40 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2005-03-15 23:55:17 +01:00
|
|
|
|
2007-07-05 19:07:56 +02:00
|
|
|
/// LinkInFile - opens a bitcode file and links in all objects which
|
2005-03-15 23:55:17 +01:00
|
|
|
/// provide symbols that are currently undefined.
|
|
|
|
///
|
|
|
|
/// Inputs:
|
2007-07-05 19:07:56 +02:00
|
|
|
/// File - The pathname of the bitcode file.
|
2005-03-15 23:55:17 +01:00
|
|
|
///
|
|
|
|
/// Outputs:
|
|
|
|
/// ErrorMessage - A C++ string detailing what error occurred, if any.
|
|
|
|
///
|
|
|
|
/// Return Value:
|
|
|
|
/// TRUE - An error occurred.
|
|
|
|
/// FALSE - No errors.
|
|
|
|
///
|
2007-04-04 08:33:17 +02:00
|
|
|
bool Linker::LinkInFile(const sys::Path &File, bool &is_native) {
|
|
|
|
is_native = false;
|
2007-08-08 21:52:29 +02:00
|
|
|
|
|
|
|
// Check for a file of name "-", which means "read standard input"
|
|
|
|
if (File.toString() == "-") {
|
|
|
|
std::auto_ptr<Module> M;
|
|
|
|
if (MemoryBuffer *Buffer = MemoryBuffer::getSTDIN()) {
|
|
|
|
M.reset(ParseBitcodeFile(Buffer, &Error));
|
|
|
|
delete Buffer;
|
2007-08-16 09:23:37 +02:00
|
|
|
if (M.get())
|
|
|
|
if (!LinkInModule(M.get(), &Error))
|
|
|
|
return false;
|
2007-08-08 21:52:29 +02:00
|
|
|
} else
|
|
|
|
Error = "standard input is empty";
|
|
|
|
return error("Cannot link stdin: " + Error);
|
|
|
|
}
|
|
|
|
|
2005-03-15 23:55:17 +01:00
|
|
|
// Make sure we can at least read the file
|
2005-07-07 20:21:42 +02:00
|
|
|
if (!File.canRead())
|
2005-03-15 23:55:17 +01:00
|
|
|
return error("Cannot find linker input '" + File.toString() + "'");
|
|
|
|
|
2007-04-04 08:33:17 +02:00
|
|
|
// If its an archive, try to link it in
|
|
|
|
std::string Magic;
|
|
|
|
File.getMagicNumber(Magic, 64);
|
|
|
|
switch (sys::IdentifyFileType(Magic.c_str(), 64)) {
|
|
|
|
default: assert(0 && "Bad file type identification");
|
|
|
|
case sys::Unknown_FileType:
|
2007-08-16 09:47:30 +02:00
|
|
|
return warning("Ignoring file '" + File.toString() +
|
|
|
|
"' because does not contain bitcode.");
|
2007-04-04 08:33:17 +02: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.
|
|
|
|
verbose("Linking archive file '" + File.toString() + "'");
|
2007-04-30 02:29:39 +02:00
|
|
|
if (LinkInArchive(File, is_native))
|
2007-08-16 09:47:30 +02:00
|
|
|
return true;
|
2007-04-04 08:33:17 +02:00
|
|
|
break;
|
|
|
|
|
2007-07-06 15:38:17 +02:00
|
|
|
case sys::Bitcode_FileType: {
|
2007-07-05 19:07:56 +02:00
|
|
|
verbose("Linking bitcode file '" + File.toString() + "'");
|
2007-04-04 08:33:17 +02:00
|
|
|
std::auto_ptr<Module> M(LoadObject(File));
|
|
|
|
if (M.get() == 0)
|
2007-08-16 09:47:30 +02:00
|
|
|
return error("Cannot load file '" + File.toString() + "': " + Error);
|
2007-08-16 09:23:37 +02:00
|
|
|
if (LinkInModule(M.get(), &Error))
|
2007-08-16 09:47:30 +02:00
|
|
|
return error("Cannot link file '" + File.toString() + "': " + Error);
|
2007-04-04 08:33:17 +02:00
|
|
|
|
|
|
|
verbose("Linked in file '" + File.toString() + "'");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-04-11 04:44:20 +02: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 08:33:17 +02:00
|
|
|
case sys::COFF_FileType:
|
|
|
|
is_native = true;
|
|
|
|
break;
|
2005-03-15 23:55:17 +01: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 19:07:56 +02:00
|
|
|
/// Files - A vector of sys::Path indicating the LLVM bitcode filenames
|
2005-03-15 23:55:17 +01:00
|
|
|
/// to be linked. The names can refer to a mixture of pure LLVM
|
2007-07-05 19:07:56 +02:00
|
|
|
/// bitcode files and archive (ar) formatted files.
|
2005-03-15 23:55:17 +01:00
|
|
|
///
|
|
|
|
/// Return value:
|
|
|
|
/// FALSE - No errors.
|
|
|
|
/// TRUE - Some error occurred.
|
|
|
|
///
|
|
|
|
bool Linker::LinkInFiles(const std::vector<sys::Path> &Files) {
|
2007-04-04 08:33:17 +02:00
|
|
|
bool is_native;
|
2005-03-15 23:55:17 +01:00
|
|
|
for (unsigned i = 0; i < Files.size(); ++i)
|
2007-04-04 08:33:17 +02:00
|
|
|
if (LinkInFile(Files[i], is_native))
|
2005-03-15 23:55:17 +01:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|