[lld] r256893 - Simplify --whole-archive handling.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 5 16:51:36 PST 2016


Author: ruiu
Date: Tue Jan  5 18:51:35 2016
New Revision: 256893

URL: http://llvm.org/viewvc/llvm-project?rev=256893&view=rev
Log:
Simplify --whole-archive handling.

Previously, we handle archive files with --whole-archive this way:
create instances of ArchiveFile, call getMembers to obtain memory
buffers of archive members, and create ObjectFiles for the members.
We didn't call anything except getMembers if --whole-archive was
specified.

I noticed that we didn't actually have to create ArchiveFile instaces
at all for that case. All we need is to get a list of memory buffers
for members, which can be done by a non-member function.

This patch removes getMembers member function from ArchiveFile.
Also removed unnecessary code for memory management.

Modified:
    lld/trunk/ELF/Driver.cpp
    lld/trunk/ELF/Driver.h
    lld/trunk/ELF/InputFiles.cpp
    lld/trunk/ELF/InputFiles.h

Modified: lld/trunk/ELF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=256893&r1=256892&r2=256893&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.cpp (original)
+++ lld/trunk/ELF/Driver.cpp Tue Jan  5 18:51:35 2016
@@ -57,6 +57,24 @@ static std::pair<ELFKind, uint16_t> pars
   error("Unknown emulation: " + S);
 }
 
+// Returns slices of MB by parsing MB as an archive file.
+// Each slice consists of a member file in the archive.
+static std::vector<MemoryBufferRef> getArchiveMembers(MemoryBufferRef MB) {
+  ErrorOr<std::unique_ptr<Archive>> FileOrErr = Archive::create(MB);
+  error(FileOrErr, "Failed to parse archive");
+  std::unique_ptr<Archive> File = std::move(*FileOrErr);
+
+  std::vector<MemoryBufferRef> V;
+  for (const ErrorOr<Archive::Child> &C : File->children()) {
+    error(C, "Could not get the child of the archive " + File->getFileName());
+    ErrorOr<MemoryBufferRef> MbOrErr = C->getMemoryBufferRef();
+    error(MbOrErr, "Could not get the buffer for a child of the archive " +
+                       File->getFileName());
+    V.push_back(*MbOrErr);
+  }
+  return V;
+}
+
 // Opens and parses a file. Path has to be resolved already.
 // Newly created memory buffers are owned by this driver.
 void LinkerDriver::addFile(StringRef Path) {
@@ -75,10 +93,8 @@ void LinkerDriver::addFile(StringRef Pat
     return;
   case file_magic::archive:
     if (WholeArchive) {
-      auto File = make_unique<ArchiveFile>(MBRef);
-      for (MemoryBufferRef &MB : File->getMembers())
+      for (MemoryBufferRef MB : getArchiveMembers(MBRef))
         Files.push_back(createObjectFile(MB));
-      OwningArchives.emplace_back(std::move(File));
       return;
     }
     Files.push_back(make_unique<ArchiveFile>(MBRef));

Modified: lld/trunk/ELF/Driver.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.h?rev=256893&r1=256892&r2=256893&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.h (original)
+++ lld/trunk/ELF/Driver.h Tue Jan  5 18:51:35 2016
@@ -38,7 +38,6 @@ private:
   llvm::BumpPtrAllocator Alloc;
   bool WholeArchive = false;
   std::vector<std::unique_ptr<InputFile>> Files;
-  std::vector<std::unique_ptr<ArchiveFile>> OwningArchives;
   std::vector<std::unique_ptr<MemoryBuffer>> OwningMBs;
 };
 

Modified: lld/trunk/ELF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.cpp?rev=256893&r1=256892&r2=256893&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.cpp (original)
+++ lld/trunk/ELF/InputFiles.cpp Tue Jan  5 18:51:35 2016
@@ -310,14 +310,10 @@ SymbolBody *ObjectFile<ELFT>::createSymb
   }
 }
 
-static std::unique_ptr<Archive> openArchive(MemoryBufferRef MB) {
-  ErrorOr<std::unique_ptr<Archive>> ArchiveOrErr = Archive::create(MB);
-  error(ArchiveOrErr, "Failed to parse archive");
-  return std::move(*ArchiveOrErr);
-}
-
 void ArchiveFile::parse() {
-  File = openArchive(MB);
+  ErrorOr<std::unique_ptr<Archive>> FileOrErr = Archive::create(MB);
+  error(FileOrErr, "Failed to parse archive");
+  File = std::move(*FileOrErr);
 
   // Allocate a buffer for Lazy objects.
   size_t NumSyms = File->getNumberOfSymbols();
@@ -344,23 +340,6 @@ MemoryBufferRef ArchiveFile::getMember(c
   return *RefOrErr;
 }
 
-std::vector<MemoryBufferRef> ArchiveFile::getMembers() {
-  File = openArchive(MB);
-
-  std::vector<MemoryBufferRef> Result;
-  for (auto &ChildOrErr : File->children()) {
-    error(ChildOrErr,
-          "Could not get the child of the archive " + File->getFileName());
-    const Archive::Child Child(*ChildOrErr);
-    ErrorOr<MemoryBufferRef> MbOrErr = Child.getMemoryBufferRef();
-    if (!MbOrErr)
-      error(MbOrErr, "Could not get the buffer for a child of the archive " +
-            File->getFileName());
-    Result.push_back(MbOrErr.get());
-  }
-  return Result;
-}
-
 template <class ELFT>
 SharedFile<ELFT>::SharedFile(MemoryBufferRef M)
     : ELFFileBase<ELFT>(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {}

Modified: lld/trunk/ELF/InputFiles.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.h?rev=256893&r1=256892&r2=256893&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.h (original)
+++ lld/trunk/ELF/InputFiles.h Tue Jan  5 18:51:35 2016
@@ -159,7 +159,6 @@ public:
   MemoryBufferRef getMember(const Archive::Symbol *Sym);
 
   llvm::MutableArrayRef<Lazy> getLazySymbols() { return LazySymbols; }
-  std::vector<MemoryBufferRef> getMembers();
 
 private:
   std::unique_ptr<Archive> File;




More information about the llvm-commits mailing list