Revert version 1.39. It breaks the ordering of the library processing.
[oota-llvm.git] / lib / Linker / LinkArchives.cpp
index fa9ec70306ceae63277afd9aa4d49eb1a9ad7a29..af17ff31b884f3fdb6dc09e08c04a7694d5885f4 100644 (file)
@@ -1,4 +1,4 @@
-//===- Linker.cpp - Link together LLVM objects and libraries --------------===//
+//===- lib/Linker/LinkArchives.cpp - Link LLVM objects and libraries ------===//
 // 
 //                     The LLVM Compiler Infrastructure
 //
 //
 //===----------------------------------------------------------------------===//
 
-#include "gccld.h"
+#include "llvm/Linker.h"
 #include "llvm/Module.h"
+#include "llvm/ModuleProvider.h"
 #include "llvm/PassManager.h"
+#include "llvm/ADT/SetOperations.h"
 #include "llvm/Bytecode/Reader.h"
+#include "llvm/Bytecode/Archive.h"
 #include "llvm/Bytecode/WriteBytecodePass.h"
 #include "llvm/Target/TargetData.h"
 #include "llvm/Transforms/IPO.h"
 #include "llvm/Transforms/Scalar.h"
-#include "llvm/Support/Linker.h"
 #include "llvm/Config/config.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FileUtilities.h"
+#include "llvm/Support/Timer.h"
 #include "llvm/System/Signals.h"
 #include "llvm/Support/SystemUtils.h"
 #include <algorithm>
@@ -57,8 +60,8 @@ std::string llvm::FindLib(const std::string &Filename,
     if (!SharedObjectOnly && FileOpenable(Directory + LibName + ".bc"))
       return Directory + LibName + ".bc";
 
-    if (FileOpenable(Directory + LibName + SHLIBEXT))
-      return Directory + LibName + SHLIBEXT;
+    if (FileOpenable(Directory + LibName + LTDL_SHLIB_EXT))
+      return Directory + LibName + LTDL_SHLIB_EXT;
 
     if (!SharedObjectOnly && FileOpenable(Directory + LibName + ".a"))
       return Directory + LibName + ".a";
@@ -105,7 +108,7 @@ void
 llvm::GetAllUndefinedSymbols(Module *M,
                              std::set<std::string> &UndefinedSymbols) {
   std::set<std::string> DefinedSymbols;
-  UndefinedSymbols.clear();   // Start out empty
+  UndefinedSymbols.clear();
   
   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
     if (I->hasName()) {
@@ -136,7 +139,7 @@ llvm::GetAllUndefinedSymbols(Module *M,
 /// module it contains (wrapped in an auto_ptr), or 0 and set ErrorMessage if an
 /// error occurs.
 ///
-std::auto_ptr<Module> llvm::LoadObject(const std::string &FN,
+static std::auto_ptr<Module> LoadObject(const std::string &FN,
                                        std::string &ErrorMessage) {
   std::string ParserErrorMessage;
   Module *Result = ParseBytecodeFile(FN, &ParserErrorMessage);
@@ -161,86 +164,81 @@ std::auto_ptr<Module> llvm::LoadObject(const std::string &FN,
 ///  TRUE  - An error occurred.
 ///  FALSE - No errors.
 ///
-static bool LinkInArchive(Module *M,
-                          const std::string &Filename,
-                          std::string &ErrorMessage,
-                          bool Verbose)
+bool llvm::LinkInArchive(Module *M,
+                         const std::string &Filename,
+                         std::string* ErrorMessage,
+                         bool Verbose)
 {
   // Find all of the symbols currently undefined in the bytecode program.
   // If all the symbols are defined, the program is complete, and there is
   // no reason to link in any archive files.
   std::set<std::string> UndefinedSymbols;
   GetAllUndefinedSymbols(M, UndefinedSymbols);
+  
   if (UndefinedSymbols.empty()) {
     if (Verbose) std::cerr << "  No symbols undefined, don't link library!\n";
     return false;  // No need to link anything in!
   }
 
-  // Load in the archive objects.
+  // Open the archive file
   if (Verbose) std::cerr << "  Loading archive file '" << Filename << "'\n";
-  std::vector<Module*> Objects;
-  if (ReadArchiveFile(Filename, Objects, &ErrorMessage))
-    return true;
-
-  // Figure out which symbols are defined by all of the modules in the archive.
-  std::vector<std::set<std::string> > DefinedSymbols;
-  DefinedSymbols.resize(Objects.size());
-  for (unsigned i = 0; i != Objects.size(); ++i) {
-    GetAllDefinedSymbols(Objects[i], DefinedSymbols[i]);
-  }
+  std::auto_ptr<Archive> AutoArch (
+    Archive::OpenAndLoadSymbols(sys::Path(Filename)));
 
-  // While we are linking in object files, loop.
-  bool Linked = true;
-  while (Linked) {     
-    Linked = false;
-
-    for (unsigned i = 0; i != Objects.size(); ++i) {
-      // Consider whether we need to link in this module...  we only need to
-      // link it in if it defines some symbol which is so far undefined.
-      //
-      const std::set<std::string> &DefSymbols = DefinedSymbols[i];
-
-      bool ObjectRequired = false;
-
-      //
-      // If the object defines main() and the program currently has main()
-      // undefined, then automatically link in the module.  Otherwise, look to
-      // see if it defines a symbol that is currently undefined.
-      //
-      if ((M->getMainFunction() == NULL) &&
-          ((DefSymbols.find ("main")) != DefSymbols.end())) {
-        ObjectRequired = true;
-      } else {
-        for (std::set<std::string>::iterator I = UndefinedSymbols.begin(),
-               E = UndefinedSymbols.end(); I != E; ++I)
-          if (DefSymbols.count(*I)) {
-            if (Verbose)
-              std::cerr << "  Found object '"
-                        << Objects[i]->getModuleIdentifier ()
-                        << "' providing symbol '" << *I << "'...\n";
-            ObjectRequired = true;
-            break;
-          }
-      }
+  Archive* arch = AutoArch.get();
 
-      // We DO need to link this object into the program...
-      if (ObjectRequired) {
-        if (LinkModules(M, Objects[i], &ErrorMessage))
-          return true;   // Couldn't link in the right object file...        
-        
-        // Since we have linked in this object, delete it from the list of
-        // objects to consider in this archive file.
-        std::swap(Objects[i], Objects.back());
-        std::swap(DefinedSymbols[i], DefinedSymbols.back());
-        Objects.pop_back();
-        DefinedSymbols.pop_back();
-        --i;   // Do not skip an entry
-        
-        // The undefined symbols set should have shrunk.
-        GetAllUndefinedSymbols(M, UndefinedSymbols);
-        Linked = true;  // We have linked something in!
-      }
+  // Save a set of symbols that are not defined by the archive. Since we're
+  // entering a loop, there's no point searching for these multiple times. This
+  // variable is used to "set_subtract" from the set of undefined symbols.
+  std::set<std::string> NotDefinedByArchive;
+
+  // While we are linking in object files, loop.
+  while (true) {     
+
+    // Find the modules we need to link into the target module
+    std::set<ModuleProvider*> Modules;
+    arch->findModulesDefiningSymbols(UndefinedSymbols, Modules);
+
+    // If we didn't find any more modules to link this time, we are done 
+    // searching this archive.
+    if (Modules.empty())
+      break;
+
+    // Any symbols remaining in UndefinedSymbols after
+    // findModulesDefiningSymbols are ones that the archive does not define. So
+    // we add them to the NotDefinedByArchive variable now.
+    NotDefinedByArchive.insert(UndefinedSymbols.begin(),
+        UndefinedSymbols.end());
+
+    // Loop over all the ModuleProviders that we got back from the archive
+    for (std::set<ModuleProvider*>::iterator I=Modules.begin(), E=Modules.end();
+         I != E; ++I) {
+
+      // Get the module we must link in.
+      std::auto_ptr<Module> AutoModule( (*I)->releaseModule() );
+      Module* aModule = AutoModule.get();
+
+      // Link it in
+      if (LinkModules(M, aModule, ErrorMessage))
+        return true;   // Couldn't link in the module
     }
+
+    // Get the undefined symbols from the aggregate module. This recomputes the
+    // symbols we still need after the new modules have been linked in.
+    GetAllUndefinedSymbols(M, UndefinedSymbols);
+
+    // At this point we have two sets of undefined symbols: UndefinedSymbols
+    // which holds the undefined symbols from all the modules, and 
+    // NotDefinedByArchive which holds symbols we know the archive doesn't
+    // define. There's no point searching for symbols that we won't find in the
+    // archive so we subtract these sets.
+    set_subtract<std::set<std::string>,std::set<std::string> >(
+        UndefinedSymbols,NotDefinedByArchive);
+    
+    // If there's no symbols left, no point in continuing to search the
+    // archive.
+    if (UndefinedSymbols.empty())
+      break;
   }
   
   return false;
@@ -331,7 +329,7 @@ bool llvm::LinkFiles(const char *progname, Module *HeadModule,
       if (Verbose)
         std::cerr << "Trying to link archive '" << Pathname << "'\n";
 
-      if (LinkInArchive(HeadModule, Pathname, ErrorMessage, Verbose)) {
+      if (LinkInArchive(HeadModule, Pathname, &ErrorMessage, Verbose)) {
         std::cerr << progname << ": Error linking in archive '" << Pathname 
                   << "': " << ErrorMessage << "\n";
         return true;
@@ -345,6 +343,9 @@ bool llvm::LinkFiles(const char *progname, Module *HeadModule,
                   << Pathname << "': " << ErrorMessage << "\n";
         return true;
       }
+    } else {
+      std::cerr << progname << ": Warning: invalid file `" << Pathname 
+                << "' ignored.\n";
     }
   }
 
@@ -397,7 +398,7 @@ void llvm::LinkLibraries(const char *progname, Module *HeadModule,
         std::cerr << "Trying to link archive '" << Pathname << "' (-l"
                   << Libraries[i] << ")\n";
 
-      if (LinkInArchive(HeadModule, Pathname, ErrorMessage, Verbose)) {
+      if (LinkInArchive(HeadModule, Pathname, &ErrorMessage, Verbose)) {
         std::cerr << progname << ": " << ErrorMessage
                   << ": Error linking in archive '" << Pathname << "' (-l"
                   << Libraries[i] << ")\n";