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.

llvm-svn: 256893
This commit is contained in:
Rui Ueyama 2016-01-06 00:51:35 +00:00
parent fb2a9c4209
commit 9b09369b3d
4 changed files with 22 additions and 29 deletions

View File

@ -57,6 +57,24 @@ static std::pair<ELFKind, uint16_t> parseEmulation(StringRef S) {
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 Path) {
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));

View File

@ -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;
};

View File

@ -310,14 +310,10 @@ SymbolBody *ObjectFile<ELFT>::createSymbolBody(StringRef StringTable,
}
}
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(const Archive::Symbol *Sym) {
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) {}

View File

@ -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;