diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 5d4ee887ea46..d10771abfd2d 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -130,6 +130,7 @@ private: template void elf::writeResult(SymbolTable *Symtab) { typedef typename ELFT::uint uintX_t; + typedef typename ELFT::Ehdr Elf_Ehdr; // Create singleton output sections. DynamicSection Dynamic(*Symtab); @@ -143,6 +144,7 @@ template void elf::writeResult(SymbolTable *Symtab) { SymbolTableSection DynSymTab(*Symtab, DynStrTab); OutputSectionBase ElfHeader("", 0, SHF_ALLOC); + ElfHeader.setSize(sizeof(Elf_Ehdr)); OutputSectionBase ProgramHeaders("", 0, SHF_ALLOC); ProgramHeaders.updateAlign(sizeof(uintX_t)); @@ -1329,6 +1331,8 @@ template void Writer::createPhdrs() { if (Note.First) Phdrs.push_back(std::move(Note)); + + Out::ProgramHeaders->setSize(sizeof(Elf_Phdr) * Phdrs.size()); } // The first section of each PT_LOAD and the first section after PT_GNU_RELRO @@ -1355,10 +1359,6 @@ template void Writer::fixSectionAlignments() { // Assign VAs (addresses at run-time) to output sections. template void Writer::assignAddresses() { - Out::ElfHeader->setSize(sizeof(Elf_Ehdr)); - size_t PhdrSize = sizeof(Elf_Phdr) * Phdrs.size(); - Out::ProgramHeaders->setSize(PhdrSize); - uintX_t ThreadBssOffset = 0; uintX_t VA = Target->getVAStart(); @@ -1383,7 +1383,6 @@ template void Writer::assignAddresses() { // Assign file offsets to output sections. template void Writer::assignFileOffsets() { - Out::ElfHeader->setSize(sizeof(Elf_Ehdr)); uintX_t Off = 0; for (OutputSectionBase *Sec : OutputSections) { if (Sec->getType() == SHT_NOBITS) {