LLVM API Documentation
00001 //===- lib/Linker/LinkItems.cpp - Link LLVM objects and libraries ---------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by Reid Spencer and is distributed under the 00006 // University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains routines to handle linking together LLVM bytecode files, 00011 // and to handle annoying things like static libraries. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/Linker.h" 00016 #include "llvm/Module.h" 00017 #include "llvm/ModuleProvider.h" 00018 #include "llvm/PassManager.h" 00019 #include "llvm/ADT/SetOperations.h" 00020 #include "llvm/Bytecode/Reader.h" 00021 #include "llvm/Bytecode/Archive.h" 00022 #include "llvm/Bytecode/WriteBytecodePass.h" 00023 #include "llvm/Target/TargetData.h" 00024 #include "llvm/Transforms/IPO.h" 00025 #include "llvm/Transforms/Scalar.h" 00026 #include "llvm/Config/config.h" 00027 #include "llvm/Support/CommandLine.h" 00028 #include "llvm/Support/FileUtilities.h" 00029 #include "llvm/Support/Timer.h" 00030 #include "llvm/System/Signals.h" 00031 #include "llvm/Support/SystemUtils.h" 00032 #include <algorithm> 00033 #include <fstream> 00034 #include <memory> 00035 #include <set> 00036 using namespace llvm; 00037 00038 static bool 00039 LinkOneLibrary(const char*progname, Module* HeadModule, 00040 const std::string& Lib, 00041 const std::vector<std::string>& LibPaths, 00042 bool Verbose, bool Native) { 00043 00044 // String in which to receive error messages. 00045 std::string ErrorMessage; 00046 00047 // Determine where this library lives. 00048 std::string Pathname = FindLib(Lib, LibPaths); 00049 if (Pathname.empty()) { 00050 // If the pathname does not exist, then simply return if we're doing a 00051 // native link and give a warning if we're doing a bytecode link. 00052 if (!Native) { 00053 std::cerr << progname << ": warning: Cannot find library '" 00054 << Lib << "'\n"; 00055 return false; 00056 } 00057 } 00058 00059 // A user may specify an ar archive without -l, perhaps because it 00060 // is not installed as a library. Detect that and link the library. 00061 if (IsArchive(Pathname)) { 00062 if (Verbose) 00063 std::cerr << "Trying to link archive '" << Pathname << "' (-l" 00064 << Lib << ")\n"; 00065 00066 if (LinkInArchive(HeadModule, Pathname, &ErrorMessage, Verbose)) { 00067 std::cerr << progname << ": " << ErrorMessage 00068 << ": Error linking in archive '" << Pathname << "' (-l" 00069 << Lib << ")\n"; 00070 return true; 00071 } 00072 } else { 00073 std::cerr << progname << ": WARNING: Supposed library -l" 00074 << Lib << " isn't a library.\n"; 00075 } 00076 return false; 00077 } 00078 00079 // LinkItems - preserve link order for an arbitrary set of linkage items. 00080 Module* 00081 llvm::LinkItems(const char *progname, const LinkItemList& Items, 00082 const std::vector<std::string>& LibPaths, 00083 bool Verbose, bool Native) { 00084 00085 // Construct the HeadModule to contain the result of the linkage 00086 std::auto_ptr<Module> HeadModule(new Module(progname)); 00087 00088 // Construct a mutable path list we can add paths to. This list will always 00089 // have LLVM_LIB_SEARCH_PATH at the end so we place it there now. 00090 std::vector<std::string> MyLibPaths(LibPaths); 00091 MyLibPaths.insert(MyLibPaths.begin(),"."); 00092 char* SearchPath = getenv("LLVM_LIB_SEARCH_PATH"); 00093 if (SearchPath) 00094 MyLibPaths.push_back(SearchPath); 00095 00096 // For each linkage item ... 00097 for (LinkItemList::const_iterator I = Items.begin(), E = Items.end(); 00098 I != E; ++I) { 00099 if (I->second) { 00100 // Link in the library suggested. 00101 if (LinkOneLibrary(progname,HeadModule.get(),I->first,MyLibPaths, 00102 Verbose,Native)) 00103 return 0; 00104 } else { 00105 std::vector<std::string> Files; 00106 Files.push_back(I->first); 00107 if (LinkFiles(progname,HeadModule.get(),Files,Verbose)) 00108 return 0; 00109 } 00110 } 00111 00112 // At this point we have processed all the link items provided to us. Since 00113 // we have an aggregated module at this point, the dependent libraries in 00114 // that module should also be aggregated with duplicates eliminated. This is 00115 // now the time to process the dependent libraries to resolve any remaining 00116 // symbols. 00117 const Module::LibraryListType& DepLibs = HeadModule->getLibraries(); 00118 for (Module::LibraryListType::const_iterator I = DepLibs.begin(), 00119 E = DepLibs.end(); I != E; ++I) { 00120 if(LinkOneLibrary(progname,HeadModule.get(),*I,MyLibPaths,Verbose,Native)) 00121 return 0; 00122 } 00123 00124 return HeadModule.release(); 00125 } 00126 00127 // BuildLinkItems -- This function 00128 void llvm::BuildLinkItems( 00129 LinkItemList& Items, 00130 const cl::list<std::string>& Files, 00131 const cl::list<std::string>& Libraries) { 00132 00133 // Build the list of linkage items for LinkItems. 00134 00135 cl::list<std::string>::const_iterator fileIt = Files.begin(); 00136 cl::list<std::string>::const_iterator libIt = Libraries.begin(); 00137 00138 int libPos = -1, filePos = -1; 00139 while ( 1 ) { 00140 if (libIt != Libraries.end()) 00141 libPos = Libraries.getPosition(libIt - Libraries.begin()); 00142 else 00143 libPos = -1; 00144 if (fileIt != Files.end()) 00145 filePos = Files.getPosition(fileIt - Files.begin()); 00146 else 00147 filePos = -1; 00148 00149 if (filePos != -1 && (libPos == -1 || filePos < libPos)) { 00150 // Add a source file 00151 Items.push_back(std::make_pair(*fileIt++, false)); 00152 } else if (libPos != -1 && (filePos == -1 || libPos < filePos)) { 00153 // Add a library 00154 Items.push_back(std::make_pair(*libIt++, true)); 00155 } else { 00156 break; // we're done with the list 00157 } 00158 } 00159 }