[llvm-commits] CVS: llvm/lib/Linker/LinkArchives.cpp

Reid Spencer reid at x10sys.com
Thu Dec 2 01:52:21 PST 2004



Changes in directory llvm/lib/Linker:

LinkArchives.cpp updated: 1.41 -> 1.42
---
Log message:

PR466: http://llvm.cs.uiuc.edu/PR466 :
* Make the linker find lib*.bca files now instead of lib*.bc since those
  are what the makefiles now generate for bytecode archives.
* Make sure the linker only links archives when LinkLibraries is called. 
  Previously if it found a lib*.bc file and that file was a bytecode file,
  it would link in the entire bytecode. This could make -lc -lc fail with
  duplicate symbols error but it shouldn't as searching multiple libraries,
  even the same one more than once, is permitted.
* Now that the above problems are corrected, implement the dependent libs
  feature. After the module is linked with all specified libraries, the
  LinkLibraries function will obtain the set of dependent libraries from
  the linked modules and attemp to find and link against those libraries.


---
Diffs of the changes:  (+54 -41)

Index: llvm/lib/Linker/LinkArchives.cpp
diff -u llvm/lib/Linker/LinkArchives.cpp:1.41 llvm/lib/Linker/LinkArchives.cpp:1.42
--- llvm/lib/Linker/LinkArchives.cpp:1.41	Tue Nov 30 16:54:48 2004
+++ llvm/lib/Linker/LinkArchives.cpp	Thu Dec  2 03:52:10 2004
@@ -57,8 +57,8 @@
   for (unsigned Index = 0; Index != Paths.size(); ++Index) {
     std::string Directory = Paths[Index] + "/";
 
-    if (!SharedObjectOnly && FileOpenable(Directory + LibName + ".bc"))
-      return Directory + LibName + ".bc";
+    if (!SharedObjectOnly && FileOpenable(Directory + LibName + ".bca"))
+      return Directory + LibName + ".bca";
 
     if (FileOpenable(Directory + LibName + LTDL_SHLIB_EXT))
       return Directory + LibName + LTDL_SHLIB_EXT;
@@ -352,6 +352,46 @@
   return false;
 }
 
+/// LinkOneLibrary - links one library of any kind into the HeadModule
+static inline void LinkOneLibrary(const char*progname, Module* HeadModule, 
+                                  const std::string& Lib, 
+                                  const std::vector<std::string>& LibPaths,
+                                  bool Verbose, bool Native) {
+
+  // String in which to receive error messages.
+  std::string ErrorMessage;
+
+  // Determine where this library lives.
+  std::string Pathname = FindLib(Lib, LibPaths);
+  if (Pathname.empty()) {
+    // If the pathname does not exist, then simply return if we're doing a 
+    // native link and give a warning if we're doing a bytecode link.
+    if (!Native) {
+      std::cerr << progname << ": WARNING: Cannot find library -l"
+                << Lib << "\n";
+      return;
+    }
+  }
+
+  // A user may specify an ar archive without -l, perhaps because it
+  // is not installed as a library. Detect that and link the library.
+  if (IsArchive(Pathname)) {
+    if (Verbose)
+      std::cerr << "Trying to link archive '" << Pathname << "' (-l"
+                << Lib << ")\n";
+
+    if (LinkInArchive(HeadModule, Pathname, &ErrorMessage, Verbose)) {
+      std::cerr << progname << ": " << ErrorMessage
+                << ": Error linking in archive '" << Pathname << "' (-l"
+                << Lib << ")\n";
+      exit(1);
+    }
+  } else {
+      std::cerr << progname << ": WARNING: Supposed library -l"
+                << Lib << " isn't a library.\n";
+  }
+}
+
 /// LinkLibraries - takes the specified library files and links them into the
 /// main bytecode object file.
 ///
@@ -374,47 +414,20 @@
                          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;
 
+  // Process the set of libraries we've been provided
   for (unsigned i = 0; i < Libraries.size(); ++i) {
-    // Determine where this library lives.
-    std::string Pathname = FindLib(Libraries[i], LibPaths);
-    if (Pathname.empty()) {
-      // If the pathname does not exist, then continue to the next one if
-      // we're doing a native link and give an error if we're doing a bytecode
-      // link.
-      if (!Native) {
-        std::cerr << progname << ": WARNING: Cannot find library -l"
-                  << Libraries[i] << "\n";
-        continue;
-      }
-    }
-
-    // A user may specify an ar archive without -l, perhaps because it
-    // is not installed as a library. Detect that and link the library.
-    if (IsArchive(Pathname)) {
-      if (Verbose)
-        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 if (IsBytecode(Pathname)) {
-      if (Verbose)
-        std::cerr << "Trying to link bytecode file '" << Pathname
-                  << "' (-l" << Libraries[i] << ")\n";
+    LinkOneLibrary(progname,HeadModule,Libraries[i],LibPaths,Verbose,Native);
+  }
 
-      if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) {
-        std::cerr << progname << ": " << ErrorMessage
-                  << ": error linking in bytecode file '" << Pathname << "' (-l"
-                  << Libraries[i] << ")\n";
-        exit(1);
-      }
-    }
+  // At this point we have processed all the libraries provided to us. Since
+  // we have an aggregated module at this point, the dependent libraries in
+  // that module should also be aggregated with duplicates eliminated. This is
+  // now the time to process the dependent libraries to resolve any remaining
+  // symbols.
+  const Module::LibraryListType& DepLibs = HeadModule->getLibraries();
+  for (Module::LibraryListType::const_iterator I = DepLibs.begin(), 
+      E = DepLibs.end(); I != E; ++I) {
+    LinkOneLibrary(progname,HeadModule,*I,LibPaths,Verbose,Native);
   }
 }






More information about the llvm-commits mailing list