ScriptConfig = ≻
Driver->main(Args, CanExitEarly);
- InputFile::freePool();
freeArena();
return !HasError;
}
MemoryBufferRef MBRef = *Buffer;
if (InBinary) {
- Files.push_back(new BinaryFile(MBRef));
+ Files.push_back(new (alloc<BinaryFile>()) BinaryFile(MBRef));
return;
}
Files.push_back(createObjectFile(MB, Path));
return;
}
- Files.push_back(new ArchiveFile(MBRef));
+ Files.push_back(new (alloc<ArchiveFile>()) ArchiveFile(MBRef));
return;
case file_magic::elf_shared_object:
if (Config->Relocatable) {
return;
default:
if (InLib)
- Files.push_back(new LazyObjectFile(MBRef));
+ Files.push_back(new (alloc<LazyObjectFile>()) LazyObjectFile(MBRef));
else
Files.push_back(createObjectFile(MBRef));
}
using namespace lld;
using namespace lld::elf;
-std::vector<InputFile *> InputFile::Pool;
namespace {
// In ELF object file all section addresses are zero. If we have multiple
: "";
}
-// Deletes all InputFile instances created so far.
-void InputFile::freePool() {
- // Files are freed in reverse order so that files created
- // from other files (e.g. object files extracted from archives)
- // are freed in the proper order.
- for (int I = Pool.size() - 1; I >= 0; --I)
- delete Pool[I];
-}
-
// Returns "(internal)", "foo.a(bar.o)" or "baz.o".
std::string elf::getFilename(const InputFile *F) {
if (!F)
// If -r is given, we do not interpret or apply relocation
// but just copy relocation sections to output.
if (Config->Relocatable)
- return new (GAlloc<ELFT>::IAlloc.Allocate())
+ return new (alloc<InputSection<ELFT>>())
InputSection<ELFT>(this, &Sec, Name);
// Find the relocation target section and associate this
// .eh_frame_hdr section for runtime. So we handle them with a special
// class. For relocatable outputs, they are just passed through.
if (Name == ".eh_frame" && !Config->Relocatable)
- return new (GAlloc<ELFT>::EHAlloc.Allocate())
+ return new (alloc<EhInputSection<ELFT>>())
EhInputSection<ELFT>(this, &Sec, Name);
if (shouldMerge(Sec))
- return new (GAlloc<ELFT>::MAlloc.Allocate())
+ return new (alloc<MergeInputSection<ELFT>>())
MergeInputSection<ELFT>(this, &Sec, Name);
- return new (GAlloc<ELFT>::IAlloc.Allocate())
- InputSection<ELFT>(this, &Sec, Name);
+ return new (alloc<InputSection<ELFT>>()) InputSection<ELFT>(this, &Sec, Name);
}
template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() {
InputFile *Obj;
if (Size == ELFCLASS32 && Endian == ELFDATA2LSB)
- Obj = new T<ELF32LE>(MB);
+ Obj = new (alloc<T<ELF32LE>>()) T<ELF32LE>(MB);
else if (Size == ELFCLASS32 && Endian == ELFDATA2MSB)
- Obj = new T<ELF32BE>(MB);
+ Obj = new (alloc<T<ELF32BE>>()) T<ELF32BE>(MB);
else if (Size == ELFCLASS64 && Endian == ELFDATA2LSB)
- Obj = new T<ELF64LE>(MB);
+ Obj = new (alloc<T<ELF64LE>>()) T<ELF64LE>(MB);
else if (Size == ELFCLASS64 && Endian == ELFDATA2MSB)
- Obj = new T<ELF64BE>(MB);
+ Obj = new (alloc<T<ELF64BE>>()) T<ELF64BE>(MB);
else
fatal("invalid file class: " + MB.getBufferIdentifier());
namespace lld {
namespace elf {
-template <class ELFT> struct GAlloc {
- static llvm::SpecificBumpPtrAllocator<InputSection<ELFT>> IAlloc;
- static llvm::SpecificBumpPtrAllocator<MergeInputSection<ELFT>> MAlloc;
- static llvm::SpecificBumpPtrAllocator<EhInputSection<ELFT>> EHAlloc;
-};
-
-template <class ELFT>
-llvm::SpecificBumpPtrAllocator<InputSection<ELFT>> GAlloc<ELFT>::IAlloc;
-
-template <class ELFT>
-llvm::SpecificBumpPtrAllocator<MergeInputSection<ELFT>> GAlloc<ELFT>::MAlloc;
-
-template <class ELFT>
-llvm::SpecificBumpPtrAllocator<EhInputSection<ELFT>> GAlloc<ELFT>::EHAlloc;
-
using llvm::object::Archive;
class InputFile;
// The root class of input files.
class InputFile {
public:
- virtual ~InputFile() = default;
-
enum Kind {
ObjectKind,
SharedKind,
uint16_t EMachine = llvm::ELF::EM_NONE;
uint8_t OSABI = 0;
- static void freePool();
-
protected:
- InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {
- Pool.push_back(this);
- }
+ InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
private:
const Kind FileKind;
-
- // All InputFile instances are added to the pool
- // and freed all at once on exit by freePool().
- static std::vector<InputFile *> Pool;
};
// Returns "(internal)", "foo.a(bar.o)" or "baz.o".
#include "Memory.h"
using namespace llvm;
+using namespace lld;
+using namespace lld::elf;
namespace lld {
BumpPtrAllocator elf::BAlloc;
StringSaver elf::Saver{elf::BAlloc};
-void elf::freeArena() { elf::BAlloc.Reset(); }
+SpecificAllocBase::SpecificAllocBase() { Instances.push_back(this); }
+
+std::vector<SpecificAllocBase *> SpecificAllocBase::Instances;
+
+void elf::freeArena() {
+ for (SpecificAllocBase *Alloc : SpecificAllocBase::Instances)
+ Alloc->reset();
+ BAlloc.Reset();
+}
}
#include "llvm/Support/Allocator.h"
#include "llvm/Support/StringSaver.h"
+#include <vector>
namespace lld {
namespace elf {
+
+// Use this arena if your object doesn't have a destructor.
extern llvm::BumpPtrAllocator BAlloc;
extern llvm::StringSaver Saver;
+// These two classes are hack to keep track of all
+// SpecificBumpPtrAllocator instances.
+struct SpecificAllocBase {
+ SpecificAllocBase();
+ virtual ~SpecificAllocBase() = default;
+ virtual void reset() = 0;
+ static std::vector<SpecificAllocBase *> Instances;
+};
+
+template <class T> struct SpecificAlloc : public SpecificAllocBase {
+ void reset() override { Alloc.DestroyAll(); }
+ llvm::SpecificBumpPtrAllocator<T> Alloc;
+};
+
+// Use this arean if your object have a destructor.
+// Your destructor will be invoked from freeArena().
+template <class T> static T *alloc() {
+ static SpecificAlloc<T> Alloc;
+ return Alloc.Alloc.Allocate();
+}
+
void freeArena();
}
}