From 953c2c4b2d811b4f21ec8115e2533320bd460900 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Sat, 10 Oct 2015 23:59:57 +0000 Subject: [PATCH] ELF2: Remove Writer member varaibles that are used only by one function. llvm-svn: 249958 --- lld/ELF/Writer.cpp | 64 ++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index f54feff7d19d..14d2bc76e11f 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -77,13 +77,8 @@ private: uintX_t VA, uintX_t Align); void phdrCopy(Elf_Phdr *PH, OutputSectionBase *From); - llvm::BumpPtrAllocator PAlloc; SymbolTable &Symtab; - std::vector Phdrs; - Elf_Phdr PhdrPhdr; - Elf_Phdr FileHeaderPhdr; - Elf_Phdr InterpPhdr; - Elf_Phdr DynamicPhdr; + std::vector Phdrs; uintX_t FileSize; uintX_t SectionHeaderOff; @@ -462,10 +457,6 @@ template void Writer::assignAddresses() { uintX_t VA = getVAStart() + sizeof(Elf_Ehdr); uintX_t FileOff = sizeof(Elf_Ehdr); - // The first Phdr entry is PT_PHDR which describes the program header itself. - Phdrs.push_back(&PhdrPhdr); - phdrSet(&PhdrPhdr, PT_PHDR, PF_R, FileOff, VA, /*Align=*/8); - // Reserve space for Phdrs. int NumPhdrs = 2; // 2 for PhdrPhdr and FileHeaderPhdr if (needsInterpSection()) @@ -482,22 +473,32 @@ template void Writer::assignAddresses() { ++NumPhdrs; } } + Phdrs.reserve(NumPhdrs); + + // The first Phdr entry is PT_PHDR which describes the program header itself. + Phdrs.emplace_back(); + Elf_Phdr *PhdrPhdr = &Phdrs.back(); + phdrSet(PhdrPhdr, PT_PHDR, PF_R, FileOff, VA, /*Align=*/8); + FileOff += sizeof(Elf_Phdr) * NumPhdrs; VA += sizeof(Elf_Phdr) * NumPhdrs; - if (needsInterpSection()) - Phdrs.push_back(&InterpPhdr); + Elf_Phdr *Interp = nullptr; + if (needsInterpSection()) { + Phdrs.emplace_back(); + Interp = &Phdrs.back(); + } // Create a Phdr for the file header. - Phdrs.push_back(&FileHeaderPhdr); - phdrSet(&FileHeaderPhdr, PT_LOAD, PF_R, 0, getVAStart(), - Target->getPageSize()); + Phdrs.emplace_back(); + Elf_Phdr *FileHeader = &Phdrs.back(); + phdrSet(FileHeader, PT_LOAD, PF_R, 0, getVAStart(), Target->getPageSize()); std::unordered_set Closed; for (OutputSectionBase *Sec : OutputSections) { if (Sec->getSize()) { uintX_t Flags = toPhdrFlags(Sec->getFlags()); - Elf_Phdr *Last = Phdrs.back(); + Elf_Phdr *Last = &Phdrs.back(); if (Last->p_flags != Flags || !needsPhdr(Sec)) { // Flags changed. End current Phdr and potentially create a new one. if (Closed.insert(Last).second) { @@ -508,9 +509,9 @@ template void Writer::assignAddresses() { if (needsPhdr(Sec)) { VA = RoundUpToAlignment(VA, Target->getPageSize()); FileOff = RoundUpToAlignment(FileOff, Target->getPageSize()); - auto *PH = new (PAlloc) Elf_Phdr; + Phdrs.emplace_back(); + Elf_Phdr *PH = &Phdrs.back(); phdrSet(PH, PT_LOAD, Flags, FileOff, VA, Target->getPageSize()); - Phdrs.push_back(PH); } } } @@ -528,25 +529,26 @@ template void Writer::assignAddresses() { FileOff += Size; } - if (needsInterpSection()) { - InterpPhdr.p_type = PT_INTERP; - phdrCopy(&InterpPhdr, Out::Interp); + if (Interp) { + Interp->p_type = PT_INTERP; + phdrCopy(Interp, Out::Interp); } if (needsDynamicSections()) { - Phdrs.push_back(&DynamicPhdr); - DynamicPhdr.p_type = PT_DYNAMIC; - phdrCopy(&DynamicPhdr, Out::Dynamic); + Phdrs.emplace_back(); + Elf_Phdr *PH = &Phdrs.back(); + PH->p_type = PT_DYNAMIC; + phdrCopy(PH, Out::Dynamic); } // Fix up the first entry's size. - PhdrPhdr.p_filesz = sizeof(Elf_Phdr) * Phdrs.size(); - PhdrPhdr.p_memsz = sizeof(Elf_Phdr) * Phdrs.size(); + PhdrPhdr->p_filesz = sizeof(Elf_Phdr) * Phdrs.size(); + PhdrPhdr->p_memsz = sizeof(Elf_Phdr) * Phdrs.size(); // If nothing was merged into the file header PT_LOAD, set the size correctly. - if (FileHeaderPhdr.p_filesz == Target->getPageSize()) { + if (FileHeader->p_filesz == Target->getPageSize()) { uint64_t Size = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * Phdrs.size(); - FileHeaderPhdr.p_filesz = Size; - FileHeaderPhdr.p_memsz = Size; + FileHeader->p_filesz = Size; + FileHeader->p_memsz = Size; } // Add space for section headers. @@ -591,8 +593,8 @@ template void Writer::writeHeader() { EHdr->e_shstrndx = Out::StrTab->getSectionIndex(); auto PHdrs = reinterpret_cast(Buf + EHdr->e_phoff); - for (Elf_Phdr *PH : Phdrs) - *PHdrs++ = *PH; + for (Elf_Phdr &PH : Phdrs) + *PHdrs++ = PH; auto SHdrs = reinterpret_cast(Buf + EHdr->e_shoff); // First entry is null.