LLVM API Documentation
00001 //===- lib/Linker/LinkArchives.cpp - Link LLVM objects and libraries ------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains routines to handle linking together LLVM bitcode 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/ADT/SetOperations.h" 00019 #include "llvm/Bitcode/Archive.h" 00020 #include "llvm/Config/config.h" 00021 #include <memory> 00022 #include <set> 00023 using namespace llvm; 00024 00025 /// GetAllUndefinedSymbols - calculates the set of undefined symbols that still 00026 /// exist in an LLVM module. This is a bit tricky because there may be two 00027 /// symbols with the same name but different LLVM types that will be resolved to 00028 /// each other but aren't currently (thus we need to treat it as resolved). 00029 /// 00030 /// Inputs: 00031 /// M - The module in which to find undefined symbols. 00032 /// 00033 /// Outputs: 00034 /// UndefinedSymbols - A set of C++ strings containing the name of all 00035 /// undefined symbols. 00036 /// 00037 static void 00038 GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) { 00039 std::set<std::string> DefinedSymbols; 00040 UndefinedSymbols.clear(); 00041 00042 // If the program doesn't define a main, try pulling one in from a .a file. 00043 // This is needed for programs where the main function is defined in an 00044 // archive, such f2c'd programs. 00045 Function *Main = M->getFunction("main"); 00046 if (Main == 0 || Main->isDeclaration()) 00047 UndefinedSymbols.insert("main"); 00048 00049 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) 00050 if (I->hasName()) { 00051 if (I->isDeclaration()) 00052 UndefinedSymbols.insert(I->getName()); 00053 else if (!I->hasLocalLinkage()) { 00054 assert(!I->hasDLLImportLinkage() 00055 && "Found dllimported non-external symbol!"); 00056 DefinedSymbols.insert(I->getName()); 00057 } 00058 } 00059 00060 for (Module::global_iterator I = M->global_begin(), E = M->global_end(); 00061 I != E; ++I) 00062 if (I->hasName()) { 00063 if (I->isDeclaration()) 00064 UndefinedSymbols.insert(I->getName()); 00065 else if (!I->hasLocalLinkage()) { 00066 assert(!I->hasDLLImportLinkage() 00067 && "Found dllimported non-external symbol!"); 00068 DefinedSymbols.insert(I->getName()); 00069 } 00070 } 00071 00072 for (Module::alias_iterator I = M->alias_begin(), E = M->alias_end(); 00073 I != E; ++I) 00074 if (I->hasName()) 00075 DefinedSymbols.insert(I->getName()); 00076 00077 // Prune out any defined symbols from the undefined symbols set... 00078 for (std::set<std::string>::iterator I = UndefinedSymbols.begin(); 00079 I != UndefinedSymbols.end(); ) 00080 if (DefinedSymbols.count(*I)) 00081 UndefinedSymbols.erase(I++); // This symbol really is defined! 00082 else 00083 ++I; // Keep this symbol in the undefined symbols list 00084 } 00085 00086 /// LinkInArchive - opens an archive library and link in all objects which 00087 /// provide symbols that are currently undefined. 00088 /// 00089 /// Inputs: 00090 /// Filename - The pathname of the archive. 00091 /// 00092 /// Return Value: 00093 /// TRUE - An error occurred. 00094 /// FALSE - No errors. 00095 bool 00096 Linker::LinkInArchive(const sys::Path &Filename, bool &is_native) { 00097 // Make sure this is an archive file we're dealing with 00098 if (!Filename.isArchive()) 00099 return error("File '" + Filename.toString() + "' is not an archive."); 00100 00101 // Open the archive file 00102 verbose("Linking archive file '" + Filename.toString() + "'"); 00103 00104 // Find all of the symbols currently undefined in the bitcode program. 00105 // If all the symbols are defined, the program is complete, and there is 00106 // no reason to link in any archive files. 00107 std::set<std::string> UndefinedSymbols; 00108 GetAllUndefinedSymbols(Composite, UndefinedSymbols); 00109 00110 if (UndefinedSymbols.empty()) { 00111 verbose("No symbols undefined, skipping library '" + 00112 Filename.toString() + "'"); 00113 return false; // No need to link anything in! 00114 } 00115 00116 std::string ErrMsg; 00117 std::auto_ptr<Archive> AutoArch ( 00118 Archive::OpenAndLoadSymbols(Filename,&ErrMsg)); 00119 00120 Archive* arch = AutoArch.get(); 00121 00122 if (!arch) 00123 return error("Cannot read archive '" + Filename.toString() + 00124 "': " + ErrMsg); 00125 if (!arch->isBitcodeArchive()) { 00126 is_native = true; 00127 return false; 00128 } 00129 is_native = false; 00130 00131 // Save a set of symbols that are not defined by the archive. Since we're 00132 // entering a loop, there's no point searching for these multiple times. This 00133 // variable is used to "set_subtract" from the set of undefined symbols. 00134 std::set<std::string> NotDefinedByArchive; 00135 00136 // Save the current set of undefined symbols, because we may have to make 00137 // multiple passes over the archive: 00138 std::set<std::string> CurrentlyUndefinedSymbols; 00139 00140 do { 00141 CurrentlyUndefinedSymbols = UndefinedSymbols; 00142 00143 // Find the modules we need to link into the target module 00144 std::set<ModuleProvider*> Modules; 00145 if (!arch->findModulesDefiningSymbols(UndefinedSymbols, Modules, &ErrMsg)) 00146 return error("Cannot find symbols in '" + Filename.toString() + 00147 "': " + ErrMsg); 00148 00149 // If we didn't find any more modules to link this time, we are done 00150 // searching this archive. 00151 if (Modules.empty()) 00152 break; 00153 00154 // Any symbols remaining in UndefinedSymbols after 00155 // findModulesDefiningSymbols are ones that the archive does not define. So 00156 // we add them to the NotDefinedByArchive variable now. 00157 NotDefinedByArchive.insert(UndefinedSymbols.begin(), 00158 UndefinedSymbols.end()); 00159 00160 // Loop over all the ModuleProviders that we got back from the archive 00161 for (std::set<ModuleProvider*>::iterator I=Modules.begin(), E=Modules.end(); 00162 I != E; ++I) { 00163 00164 // Get the module we must link in. 00165 std::string moduleErrorMsg; 00166 std::auto_ptr<Module> AutoModule((*I)->releaseModule( &moduleErrorMsg )); 00167 if (!moduleErrorMsg.empty()) 00168 return error("Could not load a module: " + moduleErrorMsg); 00169 00170 Module* aModule = AutoModule.get(); 00171 00172 if (aModule != NULL) { 00173 verbose(" Linking in module: " + aModule->getModuleIdentifier()); 00174 00175 // Link it in 00176 if (LinkInModule(aModule, &moduleErrorMsg)) { 00177 return error("Cannot link in module '" + 00178 aModule->getModuleIdentifier() + "': " + moduleErrorMsg); 00179 } 00180 } 00181 } 00182 00183 // Get the undefined symbols from the aggregate module. This recomputes the 00184 // symbols we still need after the new modules have been linked in. 00185 GetAllUndefinedSymbols(Composite, UndefinedSymbols); 00186 00187 // At this point we have two sets of undefined symbols: UndefinedSymbols 00188 // which holds the undefined symbols from all the modules, and 00189 // NotDefinedByArchive which holds symbols we know the archive doesn't 00190 // define. There's no point searching for symbols that we won't find in the 00191 // archive so we subtract these sets. 00192 set_subtract(UndefinedSymbols, NotDefinedByArchive); 00193 00194 // If there's no symbols left, no point in continuing to search the 00195 // archive. 00196 if (UndefinedSymbols.empty()) 00197 break; 00198 } while (CurrentlyUndefinedSymbols != UndefinedSymbols); 00199 00200 return false; 00201 }