diff options
author | Eli Bendersky <eliben@chromium.org> | 2013-03-11 15:38:11 -0700 |
---|---|---|
committer | Eli Bendersky <eliben@chromium.org> | 2013-03-20 14:49:21 -0700 |
commit | d41567d2ffd3413600162653c08b2365bd5bcbbf (patch) | |
tree | aa1c212bcf816f4011315b80826acfb85ae7d9f3 /lib/Linker | |
parent | 23c00401dad33ca247d2818e71540079bed63c5b (diff) |
Apply after-merge fixes to return to working state.
Diffstat (limited to 'lib/Linker')
-rw-r--r-- | lib/Linker/LinkArchives.cpp | 213 | ||||
-rw-r--r-- | lib/Linker/LinkItems.cpp | 242 |
2 files changed, 0 insertions, 455 deletions
diff --git a/lib/Linker/LinkArchives.cpp b/lib/Linker/LinkArchives.cpp deleted file mode 100644 index e5ec0b83b8..0000000000 --- a/lib/Linker/LinkArchives.cpp +++ /dev/null @@ -1,213 +0,0 @@ -//===- lib/Linker/LinkArchives.cpp - Link LLVM objects and libraries ------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file contains routines to handle linking together LLVM bitcode files, -// and to handle annoying things like static libraries. -// -//===----------------------------------------------------------------------===// - -#include "llvm/Linker.h" -#include "llvm/ADT/SetOperations.h" -#include "llvm/Bitcode/Archive.h" -#include "llvm/IR/Module.h" -#include "llvm/Support/CommandLine.h" // @LOCALMOD - -#include <memory> -#include <set> -using namespace llvm; - -// @LOCALMOD-START -// NOTE: this has a similar effect as -// tools/llvm/llvm-preserve.ll -// which in turn is similar to the GNUS's attribute((used)) -// TODO(robertm): This is a little hackish for now -static cl::list<std::string> -UndefList("referenced-list", cl::value_desc("list"), - cl::desc("A list of symbols assumed to be referenced externally"), - cl::CommaSeparated); -// @LOCALMOD-END - -/// 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. -/// -static void -GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) { - std::set<std::string> DefinedSymbols; - UndefinedSymbols.clear(); - // @LOCALMOD-START - UndefinedSymbols.insert(UndefList.begin(), UndefList.end()); - // @LOCALMOD-END - - // If the program doesn't define a main, try pulling one in from a .a file. - // This is needed for programs where the main function is defined in an - // archive, such f2c'd programs. - Function *Main = M->getFunction("main"); - if (Main == 0 || Main->isDeclaration()) - UndefinedSymbols.insert("main"); - - for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) - if (I->hasName()) { - if (I->isDeclaration()) - UndefinedSymbols.insert(I->getName()); - else if (!I->hasLocalLinkage()) { - assert(!I->hasDLLImportLinkage() - && "Found dllimported non-external symbol!"); - DefinedSymbols.insert(I->getName()); - } - } - - for (Module::global_iterator I = M->global_begin(), E = M->global_end(); - I != E; ++I) - if (I->hasName()) { - if (I->isDeclaration()) - UndefinedSymbols.insert(I->getName()); - else if (!I->hasLocalLinkage()) { - assert(!I->hasDLLImportLinkage() - && "Found dllimported non-external symbol!"); - DefinedSymbols.insert(I->getName()); - } - } - - for (Module::alias_iterator I = M->alias_begin(), E = M->alias_end(); - I != E; ++I) - if (I->hasName()) - 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 -} - -/// LinkInArchive - opens an archive library and link in all objects which -/// provide symbols that are currently undefined. -/// -/// Inputs: -/// Filename - The pathname of the archive. -/// -/// Return Value: -/// TRUE - An error occurred. -/// FALSE - No errors. -bool -Linker::LinkInArchive(const sys::Path &Filename, bool &is_native) { - // Make sure this is an archive file we're dealing with - if (!Filename.isArchive()) - return error("File '" + Filename.str() + "' is not an archive."); - - // Open the archive file - verbose("Linking archive file '" + Filename.str() + "'"); - - // Find all of the symbols currently undefined in the bitcode 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; - GetAllUndefinedSymbols(Composite, UndefinedSymbols); - - if (UndefinedSymbols.empty()) { - verbose("No symbols undefined, skipping library '" + Filename.str() + "'"); - return false; // No need to link anything in! - } - - std::string ErrMsg; - std::auto_ptr<Archive> AutoArch ( - Archive::OpenAndLoadSymbols(Filename, Context, &ErrMsg)); - - Archive* arch = AutoArch.get(); - - if (!arch) - return error("Cannot read archive '" + Filename.str() + - "': " + ErrMsg); - if (!arch->isBitcodeArchive()) { - is_native = true; - return false; - } - is_native = false; - - // 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; - - // Save the current set of undefined symbols, because we may have to make - // multiple passes over the archive: - std::set<std::string> CurrentlyUndefinedSymbols; - - do { - CurrentlyUndefinedSymbols = UndefinedSymbols; - - // Find the modules we need to link into the target module. Note that arch - // keeps ownership of these modules and may return the same Module* from a - // subsequent call. - SmallVector<Module*, 16> Modules; - if (!arch->findModulesDefiningSymbols(UndefinedSymbols, Modules, &ErrMsg)) - return error("Cannot find symbols in '" + Filename.str() + - "': " + ErrMsg); - - // If we didn't find any more modules to link this time, we are done - // searching this archive. - if (Modules.empty()) - break; - - // 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(), - UndefinedSymbols.end()); - - // Loop over all the Modules that we got back from the archive - for (SmallVectorImpl<Module*>::iterator I=Modules.begin(), E=Modules.end(); - I != E; ++I) { - - // Get the module we must link in. - std::string moduleErrorMsg; - Module* aModule = *I; - if (aModule != NULL) { - if (aModule->MaterializeAll(&moduleErrorMsg)) - return error("Could not load a module: " + moduleErrorMsg); - - verbose(" Linking in module: " + aModule->getModuleIdentifier()); - - // Link it in - if (LinkInModule(aModule, &moduleErrorMsg)) - return error("Cannot link in module '" + - aModule->getModuleIdentifier() + "': " + moduleErrorMsg); - } - } - - // Get the undefined symbols from the aggregate module. This recomputes the - // symbols we still need after the new modules have been linked in. - GetAllUndefinedSymbols(Composite, UndefinedSymbols); - - // 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(UndefinedSymbols, NotDefinedByArchive); - - // If there's no symbols left, no point in continuing to search the - // archive. - if (UndefinedSymbols.empty()) - break; - } while (CurrentlyUndefinedSymbols != UndefinedSymbols); - - return false; -} diff --git a/lib/Linker/LinkItems.cpp b/lib/Linker/LinkItems.cpp deleted file mode 100644 index 0ab551d14d..0000000000 --- a/lib/Linker/LinkItems.cpp +++ /dev/null @@ -1,242 +0,0 @@ -//===- lib/Linker/LinkItems.cpp - Link LLVM objects and libraries ---------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file contains routines to handle linking together LLVM bitcode files, -// and to handle annoying things like static libraries. -// -//===----------------------------------------------------------------------===// - -#include "llvm/Linker.h" -#include "llvm/Bitcode/ReaderWriter.h" -#include "llvm/IR/Module.h" -#include "llvm/Support/ErrorHandling.h" -#include "llvm/Support/MemoryBuffer.h" -#include "llvm/Support/Path.h" -#include "llvm/Support/system_error.h" -using namespace llvm; - -// 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 -// function only links bitcode and produces a result list of items that are -// native objects. -bool -Linker::LinkInItems(const ItemList& Items, ItemList& NativeItems) { - // Clear the NativeItems just in case - NativeItems.clear(); - - // For each linkage item ... - for (ItemList::const_iterator I = Items.begin(), E = Items.end(); - I != E; ++I) { - if (I->second) { - // Link in the library suggested. - bool is_native = false; - if (LinkInLibrary(I->first, is_native)) - return true; - if (is_native) - NativeItems.push_back(*I); - } else { - // Link in the file suggested - bool is_native = false; - if (LinkInFile(sys::Path(I->first), is_native)) - return true; - if (is_native) - NativeItems.push_back(*I); - } - } - - // @LOCALMOD-BEGIN - // 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. - bool is_native; - for (Module::lib_iterator I = Composite->lib_begin(), - E = Composite->lib_end(); I != E; ++I) { - if(LinkInLibrary(*I, is_native)) - return true; - if (is_native) - NativeItems.push_back(std::make_pair(*I, true)); - } - // @LOCALMOD-END - return false; -} - - -/// LinkInLibrary - links one library into the HeadModule. -/// -bool Linker::LinkInLibrary(StringRef Lib, bool& is_native) { - is_native = false; - // Determine where this library lives. - sys::Path Pathname = FindLib(Lib); - if (Pathname.isEmpty()) - return error("Cannot find library '" + Lib.str() + "'"); - - // If its an archive, try to link it in - std::string Magic; - Pathname.getMagicNumber(Magic, 64); - switch (sys::IdentifyFileType(Magic.c_str(), 64)) { - default: llvm_unreachable("Bad file type identification"); - case sys::Unknown_FileType: - return warning("Supposed library '" + Lib.str() + "' isn't a library."); - - case sys::Bitcode_FileType: - // LLVM ".so" file. - if (LinkInFile(Pathname, is_native)) - return true; - break; - - case sys::Archive_FileType: - if (LinkInArchive(Pathname, is_native)) - return error("Cannot link archive '" + Pathname.str() + "'"); - break; - - 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: - case sys::COFF_FileType: - is_native = true; - break; - } - return false; -} - -/// LinkLibraries - takes the specified library files and links them into the -/// main bitcode object file. -/// -/// 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. - bool is_native = false; - for (unsigned i = 0; i < Libraries.size(); ++i) - if (LinkInLibrary(Libraries[i], is_native)) - return true; - // @LOCALMOD-BEGIN - // 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(); - for (Module::LibraryListType::const_iterator I = DepLibs.begin(), - E = DepLibs.end(); I != E; ++I) - if (LinkInLibrary(*I, is_native)) - return true; - // @LOCALMOD-END - return false; -} - -/// LinkInFile - opens a bitcode file and links in all objects which -/// provide symbols that are currently undefined. -/// -/// Inputs: -/// File - The pathname of the bitcode file. -/// -/// Outputs: -/// ErrorMessage - A C++ string detailing what error occurred, if any. -/// -/// Return Value: -/// TRUE - An error occurred. -/// FALSE - No errors. -/// -bool Linker::LinkInFile(const sys::Path &File, bool &is_native) { - is_native = false; - - // Check for a file of name "-", which means "read standard input" - if (File.str() == "-") { - std::auto_ptr<Module> M; - OwningPtr<MemoryBuffer> Buffer; - error_code ec; - if (!(ec = MemoryBuffer::getSTDIN(Buffer))) { - if (!Buffer->getBufferSize()) { - Error = "standard input is empty"; - } else { - M.reset(ParseBitcodeFile(Buffer.get(), Context, &Error)); - if (M.get()) - if (!LinkInModule(M.get(), &Error)) - return false; - } - } - return error("Cannot link stdin: " + ec.message()); - } - - // Determine what variety of file it is. - std::string Magic; - if (!File.getMagicNumber(Magic, 64)) - return error("Cannot find linker input '" + File.str() + "'"); - - switch (sys::IdentifyFileType(Magic.c_str(), 64)) { - default: llvm_unreachable("Bad file type identification"); - case sys::Unknown_FileType: - return warning("Ignoring file '" + File.str() + - "' because does not contain bitcode."); - - 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. - if (LinkInArchive(File, is_native)) - return true; - break; - - case sys::Bitcode_FileType: { - verbose("Linking bitcode file '" + File.str() + "'"); - std::auto_ptr<Module> M(LoadObject(File)); - if (M.get() == 0) - return error("Cannot load file '" + File.str() + "': " + Error); - if (LinkInModule(M.get(), &Error)) - return error("Cannot link file '" + File.str() + "': " + Error); - - verbose("Linked in file '" + File.str() + "'"); - break; - } - - 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: - case sys::COFF_FileType: - is_native = true; - break; - } - 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: -/// Files - A vector of sys::Path indicating the LLVM bitcode filenames -/// to be linked. The names can refer to a mixture of pure LLVM -/// bitcode files and archive (ar) formatted files. -/// -/// Return value: -/// FALSE - No errors. -/// TRUE - Some error occurred. -/// -bool Linker::LinkInFiles(const std::vector<sys::Path> &Files) { - bool is_native; - for (unsigned i = 0; i < Files.size(); ++i) - if (LinkInFile(Files[i], is_native)) - return true; - return false; -} |