[lld] r249962 - ELF2: Do not leak MemoryBuffers.
Rui Ueyama via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 10 18:53:05 PDT 2015
Author: ruiu
Date: Sat Oct 10 20:53:04 2015
New Revision: 249962
URL: http://llvm.org/viewvc/llvm-project?rev=249962&view=rev
Log:
ELF2: Do not leak MemoryBuffers.
Modified:
lld/trunk/ELF/Driver.cpp
lld/trunk/ELF/Driver.h
lld/trunk/ELF/LinkerScript.cpp
Modified: lld/trunk/ELF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=249962&r1=249961&r2=249962&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.cpp (original)
+++ lld/trunk/ELF/Driver.cpp Sat Oct 10 20:53:04 2015
@@ -136,7 +136,7 @@ void LinkerDriver::addFile(StringRef Pat
switch (identify_magic(MBRef.getBuffer())) {
case file_magic::unknown:
- readLinkerScript(MBRef);
+ readLinkerScript(&Alloc, MBRef);
return;
case file_magic::archive:
if (WholeArchive) {
Modified: lld/trunk/ELF/Driver.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.h?rev=249962&r1=249961&r2=249962&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.h (original)
+++ lld/trunk/ELF/Driver.h Sat Oct 10 20:53:04 2015
@@ -44,6 +44,7 @@ private:
template <template <class> class T>
std::unique_ptr<ELFFileBase> createELFInputFile(MemoryBufferRef MB);
+ llvm::BumpPtrAllocator Alloc;
ArgParser Parser;
bool WholeArchive = false;
std::vector<std::unique_ptr<InputFile>> Files;
@@ -60,7 +61,7 @@ enum {
};
// Parses a linker script. Calling this function updates the Symtab and Config.
-void readLinkerScript(MemoryBufferRef MB);
+void readLinkerScript(llvm::BumpPtrAllocator *A, MemoryBufferRef MB);
} // namespace elf2
} // namespace lld
Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=249962&r1=249961&r2=249962&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Sat Oct 10 20:53:04 2015
@@ -18,6 +18,7 @@
#include "SymbolTable.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/StringSaver.h"
using namespace llvm;
using namespace lld;
@@ -26,7 +27,8 @@ using namespace lld::elf2;
namespace {
class LinkerScript {
public:
- LinkerScript(StringRef S) : Tokens(tokenize(S)) {}
+ LinkerScript(BumpPtrAllocator *A, StringRef S)
+ : Saver(*A), Tokens(tokenize(S)) {}
void run();
private:
@@ -44,9 +46,9 @@ private:
void readOutputFormat();
void readSearchDir();
+ StringSaver Saver;
std::vector<StringRef> Tokens;
size_t Pos = 0;
- static std::vector<std::unique_ptr<MemoryBuffer>> OwningMBs;
};
}
@@ -169,9 +171,9 @@ void LinkerScript::readInclude() {
auto MBOrErr = MemoryBuffer::getFile(Tok);
error(MBOrErr, Twine("cannot open ") + Tok);
std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
- std::vector<StringRef> V = tokenize(MB->getMemBufferRef().getBuffer());
+ StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
+ std::vector<StringRef> V = tokenize(S);
Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
- OwningMBs.push_back(std::move(MB)); // keep ownership of MB
}
void LinkerScript::readOutput() {
@@ -196,9 +198,7 @@ void LinkerScript::readSearchDir() {
expect(")");
}
-std::vector<std::unique_ptr<MemoryBuffer>> LinkerScript::OwningMBs;
-
// Entry point. The other functions or classes are private to this file.
-void lld::elf2::readLinkerScript(MemoryBufferRef MB) {
- LinkerScript(MB.getBuffer()).run();
+void lld::elf2::readLinkerScript(BumpPtrAllocator *A, MemoryBufferRef MB) {
+ LinkerScript(A, MB.getBuffer()).run();
}
More information about the llvm-commits
mailing list