LLVM API Documentation

LinkArchives.cpp

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