Revert version 1.39. It breaks the ordering of the library processing.
[oota-llvm.git] / lib / Linker / LinkArchives.cpp
index 034a552aba631a54ada858bee931e5412a1ff295..af17ff31b884f3fdb6dc09e08c04a7694d5885f4 100644 (file)
@@ -1,85 +1,52 @@
-//===- Linker.cpp - Link together LLVM objects and libraries --------------===//
+//===- lib/Linker/LinkArchives.cpp - Link LLVM objects and libraries ------===//
+// 
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// 
+//===----------------------------------------------------------------------===//
 //
 // This file contains routines to handle linking together LLVM bytecode files,
 // and to handle annoying things like static libraries.
 //
 //===----------------------------------------------------------------------===//
 
-#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/Transforms/Utils/Linker.h"
-#include "Support/CommandLine.h"
-#include "Support/FileUtilities.h"
-#include "Support/Signals.h"
-#include "Support/SystemUtils.h"
-#include "Config/stdlib.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>
 #include <fstream>
 #include <memory>
 #include <set>
-
-/// FileExists - determines if the specified filename exists and is readable.
-///
-/// Inputs:
-///  FN - The name of the file.
-///
-/// Outputs:
-///  None.
-///
-/// Return Value:
-///  TRUE - The file exists and is readable.
-///  FALSE - The file does not exist or is unreadable.
-///
-static inline bool FileExists(const std::string &FN) {
-  return access(FN.c_str(), R_OK | F_OK) != -1;
-}
-
-/// IsArchive - determines if the specified file is an ar archive
-/// by checking the magic string at the beginning of the file.
-///
-/// Inputs:
-///  filename - A C++ string containing the name of the file.
-///
-/// Outputs:
-///  None.
-///
-/// Return value:
-///  TRUE  - The file is an archive.
-///  FALSE - The file is not an archive.
-///
-static inline bool IsArchive(const std::string &filename) {
-  std::string ArchiveMagic("!<arch>\012");
-  char buf[1 + ArchiveMagic.size()];
-  std::ifstream f(filename.c_str());
-  f.read(buf, ArchiveMagic.size());
-  buf[ArchiveMagic.size()] = '\0';
-  return ArchiveMagic == buf;
-}
-
-/// FindLib - locates a particular library.  It will prepend and append
-/// various directories, prefixes, and suffixes until it can find the library.
-///
-/// Inputs:
-///  Filename  - Name of the file to find.
-///  Paths     - List of directories to search.
-///
-/// Outputs:
-///  None.
-///
-/// Return value:
-///  The name of the file is returned.
-///  If the file is not found, an empty string is returned.
-///
-static std::string
-FindLib(const std::string &Filename, const std::vector<std::string> &Paths) {
+using namespace llvm;
+
+/// FindLib - Try to convert Filename into the name of a file that we can open,
+/// if it does not already name a file we can open, by first trying to open
+/// Filename, then libFilename.[suffix] for each of a set of several common
+/// library suffixes, in each of the directories in Paths and the directory
+/// named by the value of the environment variable LLVM_LIB_SEARCH_PATH. Returns
+/// an empty string if no matching file can be found.
+///
+std::string llvm::FindLib(const std::string &Filename,
+                          const std::vector<std::string> &Paths,
+                          bool SharedObjectOnly) {
   // Determine if the pathname can be found as it stands.
-  if (FileExists(Filename))
+  if (FileOpenable(Filename))
     return Filename;
 
   // If that doesn't work, convert the name into a library name.
@@ -90,13 +57,13 @@ FindLib(const std::string &Filename, const std::vector<std::string> &Paths) {
   for (unsigned Index = 0; Index != Paths.size(); ++Index) {
     std::string Directory = Paths[Index] + "/";
 
-    if (FileExists(Directory + LibName + ".bc"))
+    if (!SharedObjectOnly && FileOpenable(Directory + LibName + ".bc"))
       return Directory + LibName + ".bc";
 
-    if (FileExists(Directory + LibName + ".so"))
-      return Directory + LibName + ".so";
+    if (FileOpenable(Directory + LibName + LTDL_SHLIB_EXT))
+      return Directory + LibName + LTDL_SHLIB_EXT;
 
-    if (FileExists(Directory + LibName + ".a"))
+    if (!SharedObjectOnly && FileOpenable(Directory + LibName + ".a"))
       return Directory + LibName + ".a";
   }
 
@@ -106,26 +73,17 @@ FindLib(const std::string &Filename, const std::vector<std::string> &Paths) {
     return std::string();
 
   LibName = std::string(SearchPath) + "/" + LibName;
-  if (FileExists(LibName))
+  if (FileOpenable(LibName))
     return LibName;
 
   return std::string();
 }
 
-/// GetAllDefinedSymbols - finds all of the defined symbols in the specified 
-/// module.
-///
-/// Inputs:
-///  M - The module in which to find defined symbols.
+/// GetAllDefinedSymbols - Modifies its parameter DefinedSymbols to contain the
+/// name of each externally-visible symbol defined in M.
 ///
-/// Outputs:
-///  DefinedSymbols - A set of C++ strings that will contain the name of all
-///                   defined symbols.
-///
-/// Return value:
-///  None.
-///
-void GetAllDefinedSymbols(Module *M, std::set<std::string> &DefinedSymbols) {
+void llvm::GetAllDefinedSymbols(Module *M,
+                                std::set<std::string> &DefinedSymbols) {
   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
     if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
       DefinedSymbols.insert(I->getName());
@@ -146,13 +104,11 @@ void GetAllDefinedSymbols(Module *M, std::set<std::string> &DefinedSymbols) {
 ///  UndefinedSymbols - A set of C++ strings containing the name of all
 ///                     undefined symbols.
 ///
-/// Return value:
-///  None.
-///
 void
-GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) {
+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()) {
@@ -179,25 +135,17 @@ GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) {
 }
 
 
-/// LoadObject - reads the specified bytecode object file.
-///
-/// Inputs:
-///  FN - The name of the file to load.
+/// LoadObject - Read in and parse the bytecode file named by FN and return the
+/// module it contains (wrapped in an auto_ptr), or 0 and set ErrorMessage if an
+/// error occurs.
 ///
-/// Outputs:
-///  OutErrorMessage - The error message to give back to the caller.
-///
-/// Return Value:
-///  A pointer to a module represening the bytecode file is returned.
-///  If an error occurs, the pointer is 0.
-///
-std::auto_ptr<Module>
-LoadObject(const std::string & FN, std::string &OutErrorMessage) {
-  std::string ErrorMessage;
-  Module *Result = ParseBytecodeFile(FN, &ErrorMessage);
+static std::auto_ptr<Module> LoadObject(const std::string &FN,
+                                       std::string &ErrorMessage) {
+  std::string ParserErrorMessage;
+  Module *Result = ParseBytecodeFile(FN, &ParserErrorMessage);
   if (Result) return std::auto_ptr<Module>(Result);
-  OutErrorMessage = "Bytecode file '" + FN + "' corrupt!";
-  if (ErrorMessage.size()) OutErrorMessage += ": " + ErrorMessage;
+  ErrorMessage = "Bytecode file '" + FN + "' could not be loaded";
+  if (ParserErrorMessage.size()) ErrorMessage += ": " + ParserErrorMessage;
   return std::auto_ptr<Module>();
 }
 
@@ -216,84 +164,92 @@ LoadObject(const std::string & FN, std::string &OutErrorMessage) {
 ///  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.
-  if (Verbose) std::cerr << "  Loading '" << 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]);
-  }
+  // Open the archive file
+  if (Verbose) std::cerr << "  Loading archive file '" << Filename << "'\n";
+  std::auto_ptr<Archive> AutoArch (
+    Archive::OpenAndLoadSymbols(sys::Path(Filename)));
+
+  Archive* arch = AutoArch.get();
+
+  // 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.
-  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;
-      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 providing symbol '" << *I << "'...\n";
-          ObjectRequired = true;
-          break;
-        }
-      
-      // 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!
-      }
+  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;
 }
 
-/// LinkInFile - opens an archive library and link in all objects which
+/// LinkInFile - opens a bytecode file and links in all objects which
 /// provide symbols that are currently undefined.
 ///
 /// Inputs:
-///  HeadModule - The module in which to link the archives.
-///  Filename   - The pathname of the archive.
+///  HeadModule - The module in which to link the bytecode file.
+///  Filename   - The pathname of the bytecode file.
 ///  Verbose    - Flags whether verbose messages should be printed.
 ///
 /// Outputs:
@@ -310,8 +266,9 @@ static bool LinkInFile(Module *HeadModule,
 {
   std::auto_ptr<Module> M(LoadObject(Filename, ErrorMessage));
   if (M.get() == 0) return true;
-  if (Verbose) std::cerr << "Linking in '" << Filename << "'\n";
-  return LinkModules(HeadModule, M.get(), &ErrorMessage);
+  bool Result = LinkModules(HeadModule, M.get(), &ErrorMessage);
+  if (Verbose) std::cerr << "Linked in bytecode file '" << Filename << "'\n";
+  return Result;
 }
 
 /// LinkFiles - takes a module and a list of files and links them all together.
@@ -334,11 +291,8 @@ static bool LinkInFile(Module *HeadModule,
 ///  FALSE - No errors.
 ///  TRUE  - Some error occurred.
 ///
-bool LinkFiles(const char *progname,
-               Module *HeadModule,
-               const std::vector<std::string> &Files,
-               bool Verbose)
-{
+bool llvm::LinkFiles(const char *progname, Module *HeadModule,
+                     const std::vector<std::string> &Files, bool Verbose) {
   // String in which to receive error messages.
   std::string ErrorMessage;
 
@@ -348,19 +302,23 @@ bool LinkFiles(const char *progname,
   // Get the library search path from the environment
   char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH");
 
-  for (unsigned i = 1; i < Files.size(); ++i) {
+  for (unsigned i = 0; i < Files.size(); ++i) {
     // Determine where this file lives.
-    if (FileExists(Files[i])) {
+    if (FileOpenable(Files[i])) {
       Pathname = Files[i];
     } else {
       if (SearchPath == NULL) {
-        std::cerr << "Cannot find linker input file '" << Files[i] << "'";
+        std::cerr << progname << ": Cannot find linker input file '"
+                  << Files[i] << "'\n";
+        std::cerr << progname
+                  << ": Warning: Your LLVM_LIB_SEARCH_PATH is unset.\n";
         return true;
       }
 
       Pathname = std::string(SearchPath)+"/"+Files[i];
-      if (!FileExists(Pathname)) {
-        std::cerr << "Cannot find linker input file '" << Files[i] << "'";
+      if (!FileOpenable(Pathname)) {
+        std::cerr << progname << ": Cannot find linker input file '"
+                  << Files[i] << "'\n";
         return true;
       }
     }
@@ -369,22 +327,25 @@ bool LinkFiles(const char *progname,
     // is not installed as a library. Detect that and link the library.
     if (IsArchive(Pathname)) {
       if (Verbose)
-        std::cerr << "Linking archive '" << Files[i] << "'\n";
+        std::cerr << "Trying to link archive '" << Pathname << "'\n";
 
-      if (LinkInArchive(HeadModule, Pathname, ErrorMessage, Verbose)) {
-        PrintAndReturn(progname, ErrorMessage,
-                       ": Error linking in '" + Files[i] + "'");
+      if (LinkInArchive(HeadModule, Pathname, &ErrorMessage, Verbose)) {
+        std::cerr << progname << ": Error linking in archive '" << Pathname 
+                  << "': " << ErrorMessage << "\n";
         return true;
       }
-    } else {
+    } else if (IsBytecode(Pathname)) {
       if (Verbose)
-        std::cerr << "Linking file '" << Files[i] << "'\n";
+        std::cerr << "Trying to link bytecode file '" << Pathname << "'\n";
 
       if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) {
-        PrintAndReturn(progname, ErrorMessage,
-                       ": error linking in '" + Files[i] + "'");
+        std::cerr << progname << ": Error linking in bytecode file '"
+                  << Pathname << "': " << ErrorMessage << "\n";
         return true;
       }
+    } else {
+      std::cerr << progname << ": Warning: invalid file `" << Pathname 
+                << "' ignored.\n";
     }
   }
 
@@ -409,17 +370,14 @@ bool LinkFiles(const char *progname,
 ///  FALSE - No error.
 ///  TRUE  - Error.
 ///
-bool LinkLibraries(const char *progname,
-                   Module *HeadModule,
-                   const std::vector<std::string> &Libraries,
-                   const std::vector<std::string> &LibPaths,
-                   bool Verbose,
-                   bool Native)
-{
+void llvm::LinkLibraries(const char *progname, Module *HeadModule,
+                         const std::vector<std::string> &Libraries,
+                         const std::vector<std::string> &LibPaths,
+                         bool Verbose, bool Native) {
   // String in which to receive error messages.
   std::string ErrorMessage;
 
-  for (unsigned i = 1; i < Libraries.size(); ++i) {
+  for (unsigned i = 0; i < Libraries.size(); ++i) {
     // Determine where this library lives.
     std::string Pathname = FindLib(Libraries[i], LibPaths);
     if (Pathname.empty()) {
@@ -427,8 +385,9 @@ bool LinkLibraries(const char *progname,
       // we're doing a native link and give an error if we're doing a bytecode
       // link.
       if (!Native) {
-        PrintAndReturn(progname, "Cannot find " + Libraries[i]);
-        return true;
+        std::cerr << progname << ": WARNING: Cannot find library -l"
+                  << Libraries[i] << "\n";
+        continue;
       }
     }
 
@@ -436,24 +395,26 @@ bool LinkLibraries(const char *progname,
     // is not installed as a library. Detect that and link the library.
     if (IsArchive(Pathname)) {
       if (Verbose)
-        std::cerr << "Linking archive '" << Libraries[i] << "'\n";
-
-      if (LinkInArchive(HeadModule, Pathname, ErrorMessage, Verbose)) {
-        PrintAndReturn(progname, ErrorMessage,
-                       ": Error linking in '" + Libraries[i] + "'");
-        return true;
+        std::cerr << "Trying to link archive '" << Pathname << "' (-l"
+                  << Libraries[i] << ")\n";
+
+      if (LinkInArchive(HeadModule, Pathname, &ErrorMessage, Verbose)) {
+        std::cerr << progname << ": " << ErrorMessage
+                  << ": Error linking in archive '" << Pathname << "' (-l"
+                  << Libraries[i] << ")\n";
+        exit(1);
       }
-    } else {
+    } else if (IsBytecode(Pathname)) {
       if (Verbose)
-        std::cerr << "Linking file '" << Libraries[i] << "'\n";
+        std::cerr << "Trying to link bytecode file '" << Pathname
+                  << "' (-l" << Libraries[i] << ")\n";
 
       if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) {
-        PrintAndReturn(progname, ErrorMessage,
-                       ": error linking in '" + Libraries[i] + "'");
-        return true;
+        std::cerr << progname << ": " << ErrorMessage
+                  << ": error linking in bytecode file '" << Pathname << "' (-l"
+                  << Libraries[i] << ")\n";
+        exit(1);
       }
     }
   }
-
-  return false;
 }