2004-11-12 20:34:32 +00:00
|
|
|
//===- lib/Linker/LinkArchives.cpp - Link LLVM objects and libraries ------===//
|
2003-10-20 17:47:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2003-09-19 20:24:23 +00:00
|
|
|
//
|
2003-09-30 03:24:28 +00:00
|
|
|
// This file contains routines to handle linking together LLVM bytecode files,
|
|
|
|
// and to handle annoying things like static libraries.
|
2003-09-19 20:24:23 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-11-14 22:02:27 +00:00
|
|
|
#include "llvm/Linker.h"
|
2003-09-19 20:24:23 +00:00
|
|
|
#include "llvm/Module.h"
|
2004-11-14 22:02:27 +00:00
|
|
|
#include "llvm/ModuleProvider.h"
|
2003-09-19 20:24:23 +00:00
|
|
|
#include "llvm/PassManager.h"
|
2004-11-19 03:13:25 +00:00
|
|
|
#include "llvm/ADT/SetOperations.h"
|
2003-09-19 20:24:23 +00:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
2004-11-14 22:02:27 +00:00
|
|
|
#include "llvm/Bytecode/Archive.h"
|
2003-09-19 20:24:23 +00:00
|
|
|
#include "llvm/Bytecode/WriteBytecodePass.h"
|
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Config/config.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/FileUtilities.h"
|
2004-11-19 03:13:25 +00:00
|
|
|
#include "llvm/Support/Timer.h"
|
2004-05-27 05:41:36 +00:00
|
|
|
#include "llvm/System/Signals.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/SystemUtils.h"
|
2003-09-29 22:16:43 +00:00
|
|
|
#include <algorithm>
|
2003-09-19 20:24:23 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <memory>
|
|
|
|
#include <set>
|
2003-11-28 07:44:09 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2003-11-11 21:54:01 +00:00
|
|
|
/// 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
|
2004-04-15 15:23:45 +00:00
|
|
|
/// Filename, then libFilename.[suffix] for each of a set of several common
|
2003-11-11 21:54:01 +00:00
|
|
|
/// library suffixes, in each of the directories in Paths and the directory
|
|
|
|
/// named by the value of the environment variable LLVM_LIB_SEARCH_PATH. Returns
|
|
|
|
/// an empty string if no matching file can be found.
|
|
|
|
///
|
2003-11-28 07:44:09 +00:00
|
|
|
std::string llvm::FindLib(const std::string &Filename,
|
|
|
|
const std::vector<std::string> &Paths,
|
|
|
|
bool SharedObjectOnly) {
|
2003-09-19 20:24:23 +00:00
|
|
|
// Determine if the pathname can be found as it stands.
|
2003-11-11 18:27:37 +00:00
|
|
|
if (FileOpenable(Filename))
|
2003-09-19 20:24:23 +00:00
|
|
|
return Filename;
|
|
|
|
|
|
|
|
// If that doesn't work, convert the name into a library name.
|
|
|
|
std::string LibName = "lib" + Filename;
|
|
|
|
|
|
|
|
// Iterate over the directories in Paths to see if we can find the library
|
|
|
|
// there.
|
2003-09-29 22:16:43 +00:00
|
|
|
for (unsigned Index = 0; Index != Paths.size(); ++Index) {
|
2003-09-19 20:24:23 +00:00
|
|
|
std::string Directory = Paths[Index] + "/";
|
|
|
|
|
2003-11-20 19:08:06 +00:00
|
|
|
if (!SharedObjectOnly && FileOpenable(Directory + LibName + ".bc"))
|
2003-09-19 20:24:23 +00:00
|
|
|
return Directory + LibName + ".bc";
|
|
|
|
|
2004-01-26 20:59:41 +00:00
|
|
|
if (FileOpenable(Directory + LibName + SHLIBEXT))
|
|
|
|
return Directory + LibName + SHLIBEXT;
|
2003-09-19 20:24:23 +00:00
|
|
|
|
2003-11-20 19:08:06 +00:00
|
|
|
if (!SharedObjectOnly && FileOpenable(Directory + LibName + ".a"))
|
2003-09-19 20:24:23 +00:00
|
|
|
return Directory + LibName + ".a";
|
|
|
|
}
|
|
|
|
|
|
|
|
// One last hope: Check LLVM_LIB_SEARCH_PATH.
|
|
|
|
char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH");
|
|
|
|
if (SearchPath == NULL)
|
2003-09-29 22:16:43 +00:00
|
|
|
return std::string();
|
2003-09-19 20:24:23 +00:00
|
|
|
|
|
|
|
LibName = std::string(SearchPath) + "/" + LibName;
|
2003-11-11 18:27:37 +00:00
|
|
|
if (FileOpenable(LibName))
|
2003-09-19 20:24:23 +00:00
|
|
|
return LibName;
|
|
|
|
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
2003-11-05 22:12:52 +00:00
|
|
|
/// GetAllDefinedSymbols - Modifies its parameter DefinedSymbols to contain the
|
|
|
|
/// name of each externally-visible symbol defined in M.
|
2003-09-30 18:09:32 +00:00
|
|
|
///
|
2003-11-28 07:44:09 +00:00
|
|
|
void llvm::GetAllDefinedSymbols(Module *M,
|
|
|
|
std::set<std::string> &DefinedSymbols) {
|
2003-09-19 20:24:23 +00:00
|
|
|
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
|
|
|
|
if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
|
|
|
|
DefinedSymbols.insert(I->getName());
|
|
|
|
for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
|
|
|
|
if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
|
|
|
|
DefinedSymbols.insert(I->getName());
|
|
|
|
}
|
|
|
|
|
2003-09-30 18:09:32 +00:00
|
|
|
/// GetAllUndefinedSymbols - calculates the set of undefined symbols that still
|
|
|
|
/// exist in an LLVM module. This is a bit tricky because there may be two
|
|
|
|
/// symbols with the same name but different LLVM types that will be resolved to
|
|
|
|
/// each other but aren't currently (thus we need to treat it as resolved).
|
|
|
|
///
|
|
|
|
/// Inputs:
|
|
|
|
/// M - The module in which to find undefined symbols.
|
|
|
|
///
|
|
|
|
/// Outputs:
|
|
|
|
/// UndefinedSymbols - A set of C++ strings containing the name of all
|
|
|
|
/// undefined symbols.
|
|
|
|
///
|
2003-09-19 20:24:23 +00:00
|
|
|
void
|
2003-11-28 07:44:09 +00:00
|
|
|
llvm::GetAllUndefinedSymbols(Module *M,
|
|
|
|
std::set<std::string> &UndefinedSymbols) {
|
2003-09-19 20:24:23 +00:00
|
|
|
std::set<std::string> DefinedSymbols;
|
2004-11-19 03:13:25 +00:00
|
|
|
UndefinedSymbols.clear();
|
2003-09-19 20:24:23 +00:00
|
|
|
|
|
|
|
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
|
|
|
|
if (I->hasName()) {
|
|
|
|
if (I->isExternal())
|
|
|
|
UndefinedSymbols.insert(I->getName());
|
|
|
|
else if (!I->hasInternalLinkage())
|
|
|
|
DefinedSymbols.insert(I->getName());
|
|
|
|
}
|
|
|
|
for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
|
|
|
|
if (I->hasName()) {
|
|
|
|
if (I->isExternal())
|
|
|
|
UndefinedSymbols.insert(I->getName());
|
|
|
|
else if (!I->hasInternalLinkage())
|
|
|
|
DefinedSymbols.insert(I->getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prune out any defined symbols from the undefined symbols set...
|
|
|
|
for (std::set<std::string>::iterator I = UndefinedSymbols.begin();
|
|
|
|
I != UndefinedSymbols.end(); )
|
|
|
|
if (DefinedSymbols.count(*I))
|
|
|
|
UndefinedSymbols.erase(I++); // This symbol really is defined!
|
|
|
|
else
|
|
|
|
++I; // Keep this symbol in the undefined symbols list
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-11 21:54:01 +00:00
|
|
|
/// LoadObject - Read in and parse the bytecode file named by FN and return the
|
|
|
|
/// module it contains (wrapped in an auto_ptr), or 0 and set ErrorMessage if an
|
|
|
|
/// error occurs.
|
2003-09-30 18:09:32 +00:00
|
|
|
///
|
2004-11-14 22:02:27 +00:00
|
|
|
static std::auto_ptr<Module> LoadObject(const std::string &FN,
|
2003-11-28 07:44:09 +00:00
|
|
|
std::string &ErrorMessage) {
|
2003-11-11 21:54:01 +00:00
|
|
|
std::string ParserErrorMessage;
|
|
|
|
Module *Result = ParseBytecodeFile(FN, &ParserErrorMessage);
|
2003-09-19 20:24:23 +00:00
|
|
|
if (Result) return std::auto_ptr<Module>(Result);
|
2003-11-11 21:54:01 +00:00
|
|
|
ErrorMessage = "Bytecode file '" + FN + "' could not be loaded";
|
|
|
|
if (ParserErrorMessage.size()) ErrorMessage += ": " + ParserErrorMessage;
|
2003-09-19 20:24:23 +00:00
|
|
|
return std::auto_ptr<Module>();
|
|
|
|
}
|
|
|
|
|
2003-09-30 18:09:32 +00:00
|
|
|
/// LinkInArchive - opens an archive library and link in all objects which
|
|
|
|
/// provide symbols that are currently undefined.
|
|
|
|
///
|
|
|
|
/// Inputs:
|
|
|
|
/// M - The module in which to link the archives.
|
|
|
|
/// Filename - The pathname of the archive.
|
|
|
|
/// Verbose - Flags whether verbose messages should be printed.
|
|
|
|
///
|
|
|
|
/// Outputs:
|
|
|
|
/// ErrorMessage - A C++ string detailing what error occurred, if any.
|
|
|
|
///
|
|
|
|
/// Return Value:
|
|
|
|
/// TRUE - An error occurred.
|
|
|
|
/// FALSE - No errors.
|
|
|
|
///
|
2004-11-14 22:02:27 +00:00
|
|
|
bool llvm::LinkInArchive(Module *M,
|
2004-11-16 06:47:41 +00:00
|
|
|
const std::string &Filename,
|
|
|
|
std::string* ErrorMessage,
|
|
|
|
bool Verbose)
|
2003-09-19 20:24:23 +00:00
|
|
|
{
|
|
|
|
// Find all of the symbols currently undefined in the bytecode program.
|
|
|
|
// If all the symbols are defined, the program is complete, and there is
|
|
|
|
// no reason to link in any archive files.
|
|
|
|
std::set<std::string> UndefinedSymbols;
|
2003-09-29 22:26:24 +00:00
|
|
|
GetAllUndefinedSymbols(M, UndefinedSymbols);
|
2004-11-19 03:13:25 +00:00
|
|
|
|
2003-09-29 22:16:43 +00:00
|
|
|
if (UndefinedSymbols.empty()) {
|
2003-09-19 20:24:23 +00:00
|
|
|
if (Verbose) std::cerr << " No symbols undefined, don't link library!\n";
|
|
|
|
return false; // No need to link anything in!
|
|
|
|
}
|
|
|
|
|
2004-11-14 22:02:27 +00:00
|
|
|
// Open the archive file
|
2003-11-05 22:12:52 +00:00
|
|
|
if (Verbose) std::cerr << " Loading archive file '" << Filename << "'\n";
|
2004-11-16 06:47:41 +00:00
|
|
|
std::auto_ptr<Archive> AutoArch (
|
|
|
|
Archive::OpenAndLoadSymbols(sys::Path(Filename)));
|
|
|
|
|
|
|
|
Archive* arch = AutoArch.get();
|
2003-09-19 20:24:23 +00:00
|
|
|
|
2004-11-19 03:13:25 +00:00
|
|
|
// Save a set of symbols that are not defined by the archive. Since we're
|
|
|
|
// entering a loop, there's no point searching for these multiple times. This
|
|
|
|
// variable is used to "set_subtract" from the set of undefined symbols.
|
|
|
|
std::set<std::string> NotDefinedByArchive;
|
|
|
|
|
2003-09-19 20:24:23 +00:00
|
|
|
// While we are linking in object files, loop.
|
2004-11-14 22:02:27 +00:00
|
|
|
while (true) {
|
2004-11-19 03:13:25 +00:00
|
|
|
|
|
|
|
// Find the modules we need to link into the target module
|
2004-11-14 22:02:27 +00:00
|
|
|
std::set<ModuleProvider*> Modules;
|
2004-11-16 06:47:41 +00:00
|
|
|
arch->findModulesDefiningSymbols(UndefinedSymbols, Modules);
|
2004-11-14 22:02:27 +00:00
|
|
|
|
2004-11-19 03:13:25 +00:00
|
|
|
// If we didn't find any more modules to link this time, we are done
|
|
|
|
// searching this archive.
|
2004-11-14 22:02:27 +00:00
|
|
|
if (Modules.empty())
|
|
|
|
break;
|
|
|
|
|
2004-11-19 03:13:25 +00:00
|
|
|
// Any symbols remaining in UndefinedSymbols after
|
|
|
|
// findModulesDefiningSymbols are ones that the archive does not define. So
|
|
|
|
// we add them to the NotDefinedByArchive variable now.
|
|
|
|
NotDefinedByArchive.insert(UndefinedSymbols.begin(),
|
2004-11-19 03:27:05 +00:00
|
|
|
UndefinedSymbols.end());
|
2004-11-19 03:13:25 +00:00
|
|
|
|
2004-11-14 22:02:27 +00:00
|
|
|
// Loop over all the ModuleProviders that we got back from the archive
|
|
|
|
for (std::set<ModuleProvider*>::iterator I=Modules.begin(), E=Modules.end();
|
|
|
|
I != E; ++I) {
|
2004-11-19 03:13:25 +00:00
|
|
|
|
2004-11-14 22:02:27 +00:00
|
|
|
// Get the module we must link in.
|
2004-11-16 06:47:41 +00:00
|
|
|
std::auto_ptr<Module> AutoModule( (*I)->releaseModule() );
|
|
|
|
Module* aModule = AutoModule.get();
|
2004-11-14 22:02:27 +00:00
|
|
|
|
2004-11-16 06:47:41 +00:00
|
|
|
// Link it in
|
|
|
|
if (LinkModules(M, aModule, ErrorMessage))
|
2004-11-19 03:13:25 +00:00
|
|
|
return true; // Couldn't link in the module
|
2003-09-19 20:24:23 +00:00
|
|
|
}
|
2004-11-14 22:02:27 +00:00
|
|
|
|
2004-11-19 03:13:25 +00:00
|
|
|
// Get the undefined symbols from the aggregate module. This recomputes the
|
|
|
|
// symbols we still need after the new modules have been linked in.
|
2004-11-14 22:02:27 +00:00
|
|
|
GetAllUndefinedSymbols(M, UndefinedSymbols);
|
2004-11-19 03:13:25 +00:00
|
|
|
|
|
|
|
// At this point we have two sets of undefined symbols: UndefinedSymbols
|
|
|
|
// which holds the undefined symbols from all the modules, and
|
|
|
|
// NotDefinedByArchive which holds symbols we know the archive doesn't
|
|
|
|
// define. There's no point searching for symbols that we won't find in the
|
|
|
|
// archive so we subtract these sets.
|
|
|
|
set_subtract<std::set<std::string>,std::set<std::string> >(
|
|
|
|
UndefinedSymbols,NotDefinedByArchive);
|
|
|
|
|
|
|
|
// If there's no symbols left, no point in continuing to search the
|
|
|
|
// archive.
|
|
|
|
if (UndefinedSymbols.empty())
|
|
|
|
break;
|
2003-09-19 20:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-11-05 22:12:52 +00:00
|
|
|
/// LinkInFile - opens a bytecode file and links in all objects which
|
2003-09-30 18:09:32 +00:00
|
|
|
/// provide symbols that are currently undefined.
|
|
|
|
///
|
|
|
|
/// Inputs:
|
2003-11-05 22:12:52 +00:00
|
|
|
/// HeadModule - The module in which to link the bytecode file.
|
|
|
|
/// Filename - The pathname of the bytecode file.
|
2003-09-30 18:09:32 +00:00
|
|
|
/// Verbose - Flags whether verbose messages should be printed.
|
|
|
|
///
|
|
|
|
/// Outputs:
|
|
|
|
/// ErrorMessage - A C++ string detailing what error occurred, if any.
|
|
|
|
///
|
|
|
|
/// Return Value:
|
|
|
|
/// TRUE - An error occurred.
|
|
|
|
/// FALSE - No errors.
|
|
|
|
///
|
2003-09-29 22:26:24 +00:00
|
|
|
static bool LinkInFile(Module *HeadModule,
|
|
|
|
const std::string &Filename,
|
|
|
|
std::string &ErrorMessage,
|
|
|
|
bool Verbose)
|
2003-09-19 20:24:23 +00:00
|
|
|
{
|
|
|
|
std::auto_ptr<Module> M(LoadObject(Filename, ErrorMessage));
|
2003-09-29 22:16:43 +00:00
|
|
|
if (M.get() == 0) return true;
|
2003-11-05 22:12:52 +00:00
|
|
|
bool Result = LinkModules(HeadModule, M.get(), &ErrorMessage);
|
|
|
|
if (Verbose) std::cerr << "Linked in bytecode file '" << Filename << "'\n";
|
|
|
|
return Result;
|
2003-09-19 20:24:23 +00:00
|
|
|
}
|
|
|
|
|
2003-09-30 18:09:32 +00:00
|
|
|
/// 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:
|
|
|
|
/// progname - The name of the program (infamous argv[0]).
|
|
|
|
/// HeadModule - The module under which all files will be linked.
|
|
|
|
/// Files - A vector of C++ strings indicating the LLVM bytecode filenames
|
|
|
|
/// to be linked. The names can refer to a mixture of pure LLVM
|
|
|
|
/// bytecode files and archive (ar) formatted files.
|
|
|
|
/// Verbose - Flags whether verbose output should be printed while linking.
|
|
|
|
///
|
|
|
|
/// Outputs:
|
|
|
|
/// HeadModule - The module will have the specified LLVM bytecode files linked
|
|
|
|
/// in.
|
|
|
|
///
|
|
|
|
/// Return value:
|
|
|
|
/// FALSE - No errors.
|
|
|
|
/// TRUE - Some error occurred.
|
|
|
|
///
|
2003-11-28 07:44:09 +00:00
|
|
|
bool llvm::LinkFiles(const char *progname, Module *HeadModule,
|
|
|
|
const std::vector<std::string> &Files, bool Verbose) {
|
2003-09-19 20:24:23 +00:00
|
|
|
// String in which to receive error messages.
|
|
|
|
std::string ErrorMessage;
|
|
|
|
|
|
|
|
// Full pathname of the file
|
|
|
|
std::string Pathname;
|
|
|
|
|
|
|
|
// Get the library search path from the environment
|
|
|
|
char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH");
|
|
|
|
|
2003-11-05 22:12:52 +00:00
|
|
|
for (unsigned i = 0; i < Files.size(); ++i) {
|
2003-09-19 20:24:23 +00:00
|
|
|
// Determine where this file lives.
|
2003-11-11 18:27:37 +00:00
|
|
|
if (FileOpenable(Files[i])) {
|
2003-09-19 20:24:23 +00:00
|
|
|
Pathname = Files[i];
|
2003-09-29 22:16:43 +00:00
|
|
|
} else {
|
|
|
|
if (SearchPath == NULL) {
|
2003-10-08 19:09:30 +00:00
|
|
|
std::cerr << progname << ": Cannot find linker input file '"
|
|
|
|
<< Files[i] << "'\n";
|
2003-11-05 22:12:52 +00:00
|
|
|
std::cerr << progname
|
|
|
|
<< ": Warning: Your LLVM_LIB_SEARCH_PATH is unset.\n";
|
2003-09-19 20:24:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Pathname = std::string(SearchPath)+"/"+Files[i];
|
2003-11-11 18:27:37 +00:00
|
|
|
if (!FileOpenable(Pathname)) {
|
2003-10-08 19:09:30 +00:00
|
|
|
std::cerr << progname << ": Cannot find linker input file '"
|
|
|
|
<< Files[i] << "'\n";
|
2003-09-19 20:24:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A user may specify an ar archive without -l, perhaps because it
|
|
|
|
// is not installed as a library. Detect that and link the library.
|
2003-09-29 22:16:43 +00:00
|
|
|
if (IsArchive(Pathname)) {
|
2003-09-19 20:24:23 +00:00
|
|
|
if (Verbose)
|
2003-11-05 22:12:52 +00:00
|
|
|
std::cerr << "Trying to link archive '" << Pathname << "'\n";
|
2003-09-19 20:24:23 +00:00
|
|
|
|
2004-11-14 22:02:27 +00:00
|
|
|
if (LinkInArchive(HeadModule, Pathname, &ErrorMessage, Verbose)) {
|
2004-06-02 00:22:24 +00:00
|
|
|
std::cerr << progname << ": Error linking in archive '" << Pathname
|
|
|
|
<< "': " << ErrorMessage << "\n";
|
2003-09-19 20:24:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
2003-11-11 18:27:37 +00:00
|
|
|
} else if (IsBytecode(Pathname)) {
|
2003-09-19 20:24:23 +00:00
|
|
|
if (Verbose)
|
2003-11-05 22:12:52 +00:00
|
|
|
std::cerr << "Trying to link bytecode file '" << Pathname << "'\n";
|
2003-09-19 20:24:23 +00:00
|
|
|
|
2003-09-29 22:26:24 +00:00
|
|
|
if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) {
|
2004-06-02 00:22:24 +00:00
|
|
|
std::cerr << progname << ": Error linking in bytecode file '"
|
|
|
|
<< Pathname << "': " << ErrorMessage << "\n";
|
2003-09-19 20:24:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
2004-11-08 22:03:10 +00:00
|
|
|
} else {
|
2004-11-09 04:24:59 +00:00
|
|
|
std::cerr << progname << ": Warning: invalid file `" << Pathname
|
|
|
|
<< "' ignored.\n";
|
2003-09-19 20:24:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-09-30 18:09:32 +00:00
|
|
|
/// LinkLibraries - takes the specified library files and links them into the
|
|
|
|
/// main bytecode object file.
|
|
|
|
///
|
|
|
|
/// Inputs:
|
|
|
|
/// progname - The name of the program (infamous argv[0]).
|
|
|
|
/// HeadModule - The module into which all necessary libraries will be linked.
|
|
|
|
/// Libraries - The list of libraries to link into the module.
|
|
|
|
/// LibPaths - The list of library paths in which to find libraries.
|
|
|
|
/// Verbose - Flags whether verbose messages should be printed.
|
|
|
|
/// Native - Flags whether native code is being generated.
|
|
|
|
///
|
|
|
|
/// Outputs:
|
|
|
|
/// HeadModule - The module will have all necessary libraries linked in.
|
|
|
|
///
|
|
|
|
/// Return value:
|
|
|
|
/// FALSE - No error.
|
|
|
|
/// TRUE - Error.
|
|
|
|
///
|
2003-11-28 07:44:09 +00:00
|
|
|
void llvm::LinkLibraries(const char *progname, Module *HeadModule,
|
2004-11-25 09:32:08 +00:00
|
|
|
const std::vector<std::string> &Libs,
|
2003-11-28 07:44:09 +00:00
|
|
|
const std::vector<std::string> &LibPaths,
|
|
|
|
bool Verbose, bool Native) {
|
2003-09-19 20:24:23 +00:00
|
|
|
// String in which to receive error messages.
|
|
|
|
std::string ErrorMessage;
|
|
|
|
|
2004-11-25 09:32:08 +00:00
|
|
|
// Build a set of library names that we should try, including the
|
|
|
|
// HeadModule's dependent libraries. We use a set here to eliminate
|
|
|
|
// duplicates between the module's libraries and the argument Libs.
|
|
|
|
Module::LibraryListType Libraries(HeadModule->getLibraries());
|
|
|
|
Libraries.insert(Libs.begin(),Libs.end());
|
|
|
|
|
|
|
|
// For each library
|
2003-10-21 21:07:12 +00:00
|
|
|
for (unsigned i = 0; i < Libraries.size(); ++i) {
|
2003-09-19 20:24:23 +00:00
|
|
|
// Determine where this library lives.
|
2003-09-29 22:16:43 +00:00
|
|
|
std::string Pathname = FindLib(Libraries[i], LibPaths);
|
|
|
|
if (Pathname.empty()) {
|
2003-09-19 20:24:23 +00:00
|
|
|
// If the pathname does not exist, then continue to the next one if
|
|
|
|
// we're doing a native link and give an error if we're doing a bytecode
|
|
|
|
// link.
|
2003-09-29 22:16:43 +00:00
|
|
|
if (!Native) {
|
2003-11-28 07:44:09 +00:00
|
|
|
std::cerr << progname << ": WARNING: Cannot find library -l"
|
|
|
|
<< Libraries[i] << "\n";
|
|
|
|
continue;
|
2003-09-19 20:24:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A user may specify an ar archive without -l, perhaps because it
|
|
|
|
// is not installed as a library. Detect that and link the library.
|
2003-09-29 22:16:43 +00:00
|
|
|
if (IsArchive(Pathname)) {
|
2003-09-19 20:24:23 +00:00
|
|
|
if (Verbose)
|
2003-11-16 23:07:13 +00:00
|
|
|
std::cerr << "Trying to link archive '" << Pathname << "' (-l"
|
|
|
|
<< Libraries[i] << ")\n";
|
2003-09-19 20:24:23 +00:00
|
|
|
|
2004-11-14 22:02:27 +00:00
|
|
|
if (LinkInArchive(HeadModule, Pathname, &ErrorMessage, Verbose)) {
|
2003-11-28 07:44:09 +00:00
|
|
|
std::cerr << progname << ": " << ErrorMessage
|
|
|
|
<< ": Error linking in archive '" << Pathname << "' (-l"
|
|
|
|
<< Libraries[i] << ")\n";
|
|
|
|
exit(1);
|
2003-09-19 20:24:23 +00:00
|
|
|
}
|
2003-11-11 18:27:37 +00:00
|
|
|
} else if (IsBytecode(Pathname)) {
|
2003-09-19 20:24:23 +00:00
|
|
|
if (Verbose)
|
2003-11-16 23:07:13 +00:00
|
|
|
std::cerr << "Trying to link bytecode file '" << Pathname
|
|
|
|
<< "' (-l" << Libraries[i] << ")\n";
|
2003-09-19 20:24:23 +00:00
|
|
|
|
2003-09-29 22:16:43 +00:00
|
|
|
if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) {
|
2003-11-28 07:44:09 +00:00
|
|
|
std::cerr << progname << ": " << ErrorMessage
|
|
|
|
<< ": error linking in bytecode file '" << Pathname << "' (-l"
|
|
|
|
<< Libraries[i] << ")\n";
|
|
|
|
exit(1);
|
2003-09-19 20:24:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|