Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -130,6 +130,7 @@ 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); @@ -205,6 +206,7 @@ Out::TlsPhdr = nullptr; Out::ElfHeader = &ElfHeader; Out::ProgramHeaders = &ProgramHeaders; + Out::ElfHeader->setSize(sizeof(Elf_Ehdr)); Writer(*Symtab).run(); } @@ -1328,6 +1330,9 @@ if (Note.First) Phdrs.push_back(std::move(Note)); + + size_t PhdrSize = sizeof(Elf_Phdr) * Phdrs.size(); + Out::ProgramHeaders->setSize(PhdrSize); } // The first section of each PT_LOAD and the first section after PT_GNU_RELRO @@ -1359,10 +1364,6 @@ // 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(); @@ -1387,7 +1388,6 @@ // 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) {