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 00018 using namespace llvm; 00019 00020 // LinkItems - This function is the main entry point into linking. It takes a 00021 // list of LinkItem which indicates the order the files should be linked and 00022 // how each file should be treated (plain file or with library search). The 00023 // function only links bytecode and produces a result list of items that are 00024 // native objects. 00025 bool 00026 Linker::LinkInItems(const ItemList& Items, ItemList& NativeItems) { 00027 // Clear the NativeItems just in case 00028 NativeItems.clear(); 00029 00030 // For each linkage item ... 00031 for (ItemList::const_iterator I = Items.begin(), E = Items.end(); 00032 I != E; ++I) { 00033 if (I->second) { 00034 // Link in the library suggested. 00035 bool is_file = true; 00036 if (LinkInLibrary(I->first,is_file)) 00037 return true; 00038 if (!is_file) 00039 NativeItems.push_back(*I); 00040 } else { 00041 // Link in the file suggested 00042 if (LinkInFile(sys::Path(I->first))) 00043 return true; 00044 } 00045 } 00046 00047 // At this point we have processed all the link items provided to us. Since 00048 // we have an aggregated module at this point, the dependent libraries in 00049 // that module should also be aggregated with duplicates eliminated. This is 00050 // now the time to process the dependent libraries to resolve any remaining 00051 // symbols. 00052 bool is_bytecode; 00053 for (Module::lib_iterator I = Composite->lib_begin(), 00054 E = Composite->lib_end(); I != E; ++I) 00055 if(LinkInLibrary(*I, is_bytecode)) 00056 return true; 00057 00058 return false; 00059 } 00060 00061 00062 /// LinkInLibrary - links one library into the HeadModule. 00063 /// 00064 bool Linker::LinkInLibrary(const std::string& Lib, bool& is_file) { 00065 is_file = false; 00066 // Determine where this library lives. 00067 sys::Path Pathname = FindLib(Lib); 00068 if (Pathname.isEmpty()) 00069 return warning("Cannot find library '" + Lib + "'"); 00070 00071 // If its an archive, try to link it in 00072 std::string Magic; 00073 Pathname.getMagicNumber(Magic, 64); 00074 switch (sys::IdentifyFileType(Magic.c_str(), 64)) { 00075 case sys::BytecodeFileType: 00076 case sys::CompressedBytecodeFileType: 00077 // LLVM ".so" file. 00078 if (LinkInFile(Pathname)) 00079 return error("Cannot link file '" + Pathname.toString() + "'"); 00080 is_file = true; 00081 break; 00082 case sys::ArchiveFileType: 00083 if (LinkInArchive(Pathname)) 00084 return error("Cannot link archive '" + Pathname.toString() + "'"); 00085 break; 00086 default: 00087 return warning("Supposed library '" + Lib + "' isn't a library."); 00088 } 00089 return false; 00090 } 00091 00092 /// LinkLibraries - takes the specified library files and links them into the 00093 /// main bytecode object file. 00094 /// 00095 /// Inputs: 00096 /// Libraries - The list of libraries to link into the module. 00097 /// 00098 /// Return value: 00099 /// FALSE - No error. 00100 /// TRUE - Error. 00101 /// 00102 bool Linker::LinkInLibraries(const std::vector<std::string> &Libraries) { 00103 00104 // Process the set of libraries we've been provided. 00105 bool is_bytecode; 00106 for (unsigned i = 0; i < Libraries.size(); ++i) 00107 if (LinkInLibrary(Libraries[i], is_bytecode)) 00108 return true; 00109 00110 // At this point we have processed all the libraries provided to us. Since 00111 // we have an aggregated module at this point, the dependent libraries in 00112 // that module should also be aggregated with duplicates eliminated. This is 00113 // now the time to process the dependent libraries to resolve any remaining 00114 // symbols. 00115 const Module::LibraryListType& DepLibs = Composite->getLibraries(); 00116 for (Module::LibraryListType::const_iterator I = DepLibs.begin(), 00117 E = DepLibs.end(); I != E; ++I) 00118 if (LinkInLibrary(*I, is_bytecode)) 00119 return true; 00120 00121 return false; 00122 } 00123 00124 /// LinkInFile - opens a bytecode file and links in all objects which 00125 /// provide symbols that are currently undefined. 00126 /// 00127 /// Inputs: 00128 /// File - The pathname of the bytecode file. 00129 /// 00130 /// Outputs: 00131 /// ErrorMessage - A C++ string detailing what error occurred, if any. 00132 /// 00133 /// Return Value: 00134 /// TRUE - An error occurred. 00135 /// FALSE - No errors. 00136 /// 00137 bool Linker::LinkInFile(const sys::Path &File) { 00138 // Make sure we can at least read the file 00139 if (!File.canRead()) 00140 return error("Cannot find linker input '" + File.toString() + "'"); 00141 00142 // A user may specify an ar archive without -l, perhaps because it 00143 // is not installed as a library. Detect that and link the library. 00144 if (File.isArchive()) { 00145 if (LinkInArchive(File)) 00146 return error("Cannot link archive '" + File.toString() + "'"); 00147 } else if (File.isBytecodeFile()) { 00148 verbose("Linking bytecode file '" + File.toString() + "'"); 00149 00150 std::auto_ptr<Module> M(LoadObject(File)); 00151 if (M.get() == 0) 00152 return error("Cannot load file '" + File.toString() + "'" + Error); 00153 if (LinkInModule(M.get())) 00154 return error("Cannot link file '" + File.toString() + "'" + Error); 00155 00156 verbose("Linked in file '" + File.toString() + "'"); 00157 } else { 00158 return warning("File of unknown type '" + File.toString() + "' ignored."); 00159 } 00160 return false; 00161 } 00162 00163 /// LinkFiles - takes a module and a list of files and links them all together. 00164 /// It locates the file either in the current directory, as its absolute 00165 /// or relative pathname, or as a file somewhere in LLVM_LIB_SEARCH_PATH. 00166 /// 00167 /// Inputs: 00168 /// Files - A vector of sys::Path indicating the LLVM bytecode filenames 00169 /// to be linked. The names can refer to a mixture of pure LLVM 00170 /// bytecode files and archive (ar) formatted files. 00171 /// 00172 /// Return value: 00173 /// FALSE - No errors. 00174 /// TRUE - Some error occurred. 00175 /// 00176 bool Linker::LinkInFiles(const std::vector<sys::Path> &Files) { 00177 for (unsigned i = 0; i < Files.size(); ++i) 00178 if (LinkInFile(Files[i])) 00179 return true; 00180 return false; 00181 }