Reid Spencer | 4bdf1c9 | 2004-12-05 19:14:55 +0000 | [diff] [blame^] | 1 | //===- lib/Linker/LinkItems.cpp - Link LLVM objects and libraries ---------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Reid Spencer and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains routines to handle linking together LLVM bytecode files, |
| 11 | // and to handle annoying things like static libraries. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Linker.h" |
| 16 | #include "llvm/Module.h" |
| 17 | #include "llvm/ModuleProvider.h" |
| 18 | #include "llvm/PassManager.h" |
| 19 | #include "llvm/ADT/SetOperations.h" |
| 20 | #include "llvm/Bytecode/Reader.h" |
| 21 | #include "llvm/Bytecode/Archive.h" |
| 22 | #include "llvm/Bytecode/WriteBytecodePass.h" |
| 23 | #include "llvm/Target/TargetData.h" |
| 24 | #include "llvm/Transforms/IPO.h" |
| 25 | #include "llvm/Transforms/Scalar.h" |
| 26 | #include "llvm/Config/config.h" |
| 27 | #include "llvm/Support/CommandLine.h" |
| 28 | #include "llvm/Support/FileUtilities.h" |
| 29 | #include "llvm/Support/Timer.h" |
| 30 | #include "llvm/System/Signals.h" |
| 31 | #include "llvm/Support/SystemUtils.h" |
| 32 | #include <algorithm> |
| 33 | #include <fstream> |
| 34 | #include <memory> |
| 35 | #include <set> |
| 36 | using namespace llvm; |
| 37 | |
| 38 | static bool |
| 39 | LinkOneLibrary(const char*progname, Module* HeadModule, |
| 40 | const std::string& Lib, |
| 41 | const std::vector<std::string>& LibPaths, |
| 42 | bool Verbose, bool Native) { |
| 43 | |
| 44 | // String in which to receive error messages. |
| 45 | std::string ErrorMessage; |
| 46 | |
| 47 | // Determine where this library lives. |
| 48 | std::string Pathname = FindLib(Lib, LibPaths); |
| 49 | if (Pathname.empty()) { |
| 50 | // If the pathname does not exist, then simply return if we're doing a |
| 51 | // native link and give a warning if we're doing a bytecode link. |
| 52 | if (!Native) { |
| 53 | std::cerr << progname << ": error: Cannot find library '" |
| 54 | << Lib << "'\n"; |
| 55 | return true; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // A user may specify an ar archive without -l, perhaps because it |
| 60 | // is not installed as a library. Detect that and link the library. |
| 61 | if (IsArchive(Pathname)) { |
| 62 | if (Verbose) |
| 63 | std::cerr << "Trying to link archive '" << Pathname << "' (-l" |
| 64 | << Lib << ")\n"; |
| 65 | |
| 66 | if (LinkInArchive(HeadModule, Pathname, &ErrorMessage, Verbose)) { |
| 67 | std::cerr << progname << ": " << ErrorMessage |
| 68 | << ": Error linking in archive '" << Pathname << "' (-l" |
| 69 | << Lib << ")\n"; |
| 70 | return true; |
| 71 | } |
| 72 | } else { |
| 73 | std::cerr << progname << ": WARNING: Supposed library -l" |
| 74 | << Lib << " isn't a library.\n"; |
| 75 | } |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | // LinkItems - preserve link order for an arbitrary set of linkage items. |
| 80 | Module* |
| 81 | llvm::LinkItems(const char *progname, const LinkItemList& Items, |
| 82 | const std::vector<std::string>& LibPaths, |
| 83 | bool Verbose, bool Native) { |
| 84 | |
| 85 | // Construct the HeadModule to contain the result of the linkage |
| 86 | std::auto_ptr<Module> HeadModule(new Module(progname)); |
| 87 | |
| 88 | // Construct a mutable path list we can add paths to. This list will always |
| 89 | // have LLVM_LIB_SEARCH_PATH at the end so we place it there now. |
| 90 | std::vector<std::string> MyLibPaths(LibPaths); |
| 91 | MyLibPaths.insert(MyLibPaths.begin(),"."); |
| 92 | char* SearchPath = getenv("LLVM_LIB_SEARCH_PATH"); |
| 93 | if (SearchPath) |
| 94 | MyLibPaths.push_back(SearchPath); |
| 95 | |
| 96 | // For each linkage item ... |
| 97 | for (LinkItemList::const_iterator I = Items.begin(), E = Items.end(); |
| 98 | I != E; ++I) { |
| 99 | if (I->second) { |
| 100 | // Link in the library suggested. |
| 101 | if (LinkOneLibrary(progname,HeadModule.get(),I->first,MyLibPaths, |
| 102 | Verbose,Native)) |
| 103 | return 0; |
| 104 | } else { |
| 105 | std::vector<std::string> Files; |
| 106 | Files.push_back(I->first); |
| 107 | if (LinkFiles(progname,HeadModule.get(),Files,Verbose)) |
| 108 | return 0; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // At this point we have processed all the link items provided to us. Since |
| 113 | // we have an aggregated module at this point, the dependent libraries in |
| 114 | // that module should also be aggregated with duplicates eliminated. This is |
| 115 | // now the time to process the dependent libraries to resolve any remaining |
| 116 | // symbols. |
| 117 | const Module::LibraryListType& DepLibs = HeadModule->getLibraries(); |
| 118 | for (Module::LibraryListType::const_iterator I = DepLibs.begin(), |
| 119 | E = DepLibs.end(); I != E; ++I) { |
| 120 | if(LinkOneLibrary(progname,HeadModule.get(),*I,MyLibPaths,Verbose,Native)) |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | return HeadModule.release(); |
| 125 | } |
| 126 | |
| 127 | // BuildLinkItems -- This function |
| 128 | void llvm::BuildLinkItems( |
| 129 | LinkItemList& Items, |
| 130 | const cl::list<std::string>& Files, |
| 131 | const cl::list<std::string>& Libraries) { |
| 132 | |
| 133 | // Build the list of linkage items for LinkItems. |
| 134 | |
| 135 | cl::list<std::string>::const_iterator fileIt = Files.begin(); |
| 136 | cl::list<std::string>::const_iterator libIt = Libraries.begin(); |
| 137 | |
| 138 | int libPos = -1, filePos = -1; |
| 139 | while ( 1 ) { |
| 140 | if (libIt != Libraries.end()) |
| 141 | libPos = Libraries.getPosition(libIt - Libraries.begin()); |
| 142 | else |
| 143 | libPos = -1; |
| 144 | if (fileIt != Files.end()) |
| 145 | filePos = Files.getPosition(fileIt - Files.begin()); |
| 146 | else |
| 147 | filePos = -1; |
| 148 | |
| 149 | if (filePos != -1 && (libPos == -1 || filePos < libPos)) { |
| 150 | // Add a source file |
| 151 | Items.push_back(std::make_pair(*fileIt++, false)); |
| 152 | } else if (libPos != -1 && (filePos == -1 || libPos < filePos)) { |
| 153 | // Add a library |
| 154 | Items.push_back(std::make_pair(*libIt++, true)); |
| 155 | } else { |
| 156 | break; // we're done with the list |
| 157 | } |
| 158 | } |
| 159 | } |