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(); } @@ -1333,6 +1335,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 @@ -1372,7 +1377,6 @@ // header, do not create program headers. Also assign of section addresses // is very straightforward: we just put all sections sequentually to the file. template void Writer::assignAddressesRelocatable() { - Out::ElfHeader->setSize(sizeof(Elf_Ehdr)); uintX_t FileOff = 0; for (OutputSectionBase *Sec : OutputSections) FileOff = fixFileOff(FileOff, Sec->getAlign(), Sec); @@ -1383,10 +1387,6 @@ // Visits all headers in PhdrTable and assigns the adresses to // the output sections. Also creates common and special headers. 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(); uintX_t FileOff = 0;