[lld] r274896 - Fix memory leak.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 8 10:58:54 PDT 2016


Author: ruiu
Date: Fri Jul  8 12:58:54 2016
New Revision: 274896

URL: http://llvm.org/viewvc/llvm-project?rev=274896&view=rev
Log:
Fix memory leak.

Symbol's dtors are not called because they are allocated using
BumpPtrAllocators. So, members of std::unique_ptr type are not
freed when symbols are deallocated.

This patch is to allocate Thunks using BumpPtrAllocators.

Modified:
    lld/trunk/ELF/InputFiles.cpp
    lld/trunk/ELF/InputFiles.h
    lld/trunk/ELF/Symbols.h
    lld/trunk/ELF/Thunks.cpp

Modified: lld/trunk/ELF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.cpp?rev=274896&r1=274895&r2=274896&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.cpp (original)
+++ lld/trunk/ELF/InputFiles.cpp Fri Jul  8 12:58:54 2016
@@ -353,8 +353,9 @@ SymbolBody *elf::ObjectFile<ELFT>::creat
   InputSectionBase<ELFT> *Sec = getSection(*Sym);
   if (Binding == STB_LOCAL) {
     if (Sym->st_shndx == SHN_UNDEF)
-      return new (Alloc) Undefined(Sym->st_name, Sym->st_other, Sym->getType());
-    return new (Alloc) DefinedRegular<ELFT>(*Sym, Sec);
+      return new (this->Alloc)
+          Undefined(Sym->st_name, Sym->st_other, Sym->getType());
+    return new (this->Alloc) DefinedRegular<ELFT>(*Sym, Sec);
   }
 
   StringRef Name = check(Sym->getName(this->StringTable));

Modified: lld/trunk/ELF/InputFiles.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.h?rev=274896&r1=274895&r2=274896&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.h (original)
+++ lld/trunk/ELF/InputFiles.h Fri Jul  8 12:58:54 2016
@@ -154,6 +154,10 @@ public:
   // st_name of the symbol.
   std::vector<std::pair<const DefinedRegular<ELFT> *, unsigned>> KeptLocalSyms;
 
+  // SymbolBodies and Thunks for sections in this file are allocated
+  // using this buffer.
+  llvm::BumpPtrAllocator Alloc;
+
 private:
   void initializeSections(llvm::DenseSet<StringRef> &ComdatGroups);
   void initializeSymbols();
@@ -173,7 +177,6 @@ private:
   // MIPS .MIPS.options section defined by this file.
   std::unique_ptr<MipsOptionsInputSection<ELFT>> MipsOptions;
 
-  llvm::BumpPtrAllocator Alloc;
   llvm::SpecificBumpPtrAllocator<InputSection<ELFT>> IAlloc;
   llvm::SpecificBumpPtrAllocator<MergeInputSection<ELFT>> MAlloc;
   llvm::SpecificBumpPtrAllocator<EhInputSection<ELFT>> EHAlloc;

Modified: lld/trunk/ELF/Symbols.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Symbols.h?rev=274896&r1=274895&r2=274896&view=diff
==============================================================================
--- lld/trunk/ELF/Symbols.h (original)
+++ lld/trunk/ELF/Symbols.h Fri Jul  8 12:58:54 2016
@@ -230,7 +230,7 @@ public:
 
   // If non-null the symbol has a Thunk that may be used as an alternative
   // destination for callers of this Symbol.
-  std::unique_ptr<Thunk<ELFT>> ThunkData;
+  Thunk<ELFT> *ThunkData;
 
 private:
   static InputSectionBase<ELFT> *NullInputSection;
@@ -306,7 +306,7 @@ public:
 
   // If non-null the symbol has a Thunk that may be used as an alternative
   // destination for callers of this Symbol.
-  std::unique_ptr<Thunk<ELFT>> ThunkData;
+  Thunk<ELFT> *ThunkData;
   bool needsCopy() const { return this->NeedsCopyOrPltAddr && !this->isFunc(); }
 };
 

Modified: lld/trunk/ELF/Thunks.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Thunks.cpp?rev=274896&r1=274895&r2=274896&view=diff
==============================================================================
--- lld/trunk/ELF/Thunks.cpp (original)
+++ lld/trunk/ELF/Thunks.cpp Fri Jul  8 12:58:54 2016
@@ -22,6 +22,7 @@
 #include "OutputSections.h"
 #include "Symbols.h"
 #include "Target.h"
+#include "llvm/Support/Allocator.h"
 
 #include "llvm/Object/ELF.h"
 #include "llvm/Support/ELF.h"
@@ -175,7 +176,9 @@ static void addThunkARM(uint32_t RelocTy
     return;
 
   bool NeedsPI = Config->Pic || Config->Pie || Config->Shared;
-  Thunk<ELFT> *thunk;
+  Thunk<ELFT> *T;
+  BumpPtrAllocator &Alloc = IS.getFile()->Alloc;
+
   // ARM relocations need ARM to Thumb interworking Thunks, Thumb relocations
   // need Thumb to ARM relocations. Use position independent Thunks if we
   // require position independent code.
@@ -184,43 +187,46 @@ static void addThunkARM(uint32_t RelocTy
   case R_ARM_PLT32:
   case R_ARM_JUMP24:
     if (NeedsPI)
-      thunk = new ARMToThumbV7PILongThunk<ELFT>(S, IS);
+      T = new (Alloc) ARMToThumbV7PILongThunk<ELFT>(S, IS);
     else
-      thunk = new ARMToThumbV7ABSLongThunk<ELFT>(S, IS);
+      T = new (Alloc) ARMToThumbV7ABSLongThunk<ELFT>(S, IS);
     break;
   case R_ARM_THM_JUMP19:
   case R_ARM_THM_JUMP24:
     if (NeedsPI)
-      thunk = new ThumbToARMV7PILongThunk<ELFT>(S, IS);
+      T = new (Alloc) ThumbToARMV7PILongThunk<ELFT>(S, IS);
     else
-      thunk = new ThumbToARMV7ABSLongThunk<ELFT>(S, IS);
+      T = new (Alloc) ThumbToARMV7ABSLongThunk<ELFT>(S, IS);
     break;
   default:
     fatal("Unrecognised Relocation type\n");
   }
+
   // ARM Thunks are added to the same InputSection as the relocation. This
   // isn't strictly necessary but it makes it more likely that a limited range
   // branch can reach the Thunk, and it makes Thunks to the PLT section easier
-  IS.addThunk(thunk);
+  IS.addThunk(T);
   if (DefinedRegular<ELFT> *DR = dyn_cast<DefinedRegular<ELFT>>(&S))
-    DR->ThunkData.reset(thunk);
+    DR->ThunkData = T;
   else if (SharedSymbol<ELFT> *SH = dyn_cast<SharedSymbol<ELFT>>(&S))
-    SH->ThunkData.reset(thunk);
+    SH->ThunkData = T;
   else
     fatal("symbol not DefinedRegular or Shared\n");
 }
 
 template <class ELFT>
-static void addThunkMips(uint32_t RelocType, SymbolBody &S) {
+static void addThunkMips(uint32_t RelocType, SymbolBody &S,
+                         InputSection<ELFT> &IS) {
   if (S.hasThunk<ELFT>())
     // only one Thunk supported per symbol
     return;
-  // Mips Thunks are added to the InputSection defining S
+
+  // Mips Thunks are added to the InputSection defining S.
   auto *R = cast<DefinedRegular<ELFT>>(&S);
   auto *Sec = cast<InputSection<ELFT>>(R->Section);
-  auto *T = new MipsThunk<ELFT>(S, *Sec);
+  auto *T = new (IS.getFile()->Alloc) MipsThunk<ELFT>(S, *Sec);
   Sec->addThunk(T);
-  R->ThunkData.reset(T);
+  R->ThunkData = T;
 }
 
 template <class ELFT>
@@ -228,7 +234,7 @@ void addThunk(uint32_t RelocType, Symbol
   if (Config->EMachine == EM_ARM)
     addThunkARM<ELFT>(RelocType, S, IS);
   else if (Config->EMachine == EM_MIPS)
-    addThunkMips<ELFT>(RelocType, S);
+    addThunkMips<ELFT>(RelocType, S, IS);
   else
     llvm_unreachable("add Thunk only supported for ARM and Mips");
 }




More information about the llvm-commits mailing list