Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -907,21 +907,19 @@ LastRO = P; } + auto SetSection = [](ArrayRef V, OutputSection *OS) { + for (DefinedRegular *DR : V) + if (DR) + DR->Section = OS; + }; + // _end is the first location after the uninitialized data region. - if (Last) { - if (ElfSym::End1) - ElfSym::End1->Section = Last->LastSec; - if (ElfSym::End2) - ElfSym::End2->Section = Last->LastSec; - } + if (Last) + SetSection({ElfSym::End1, ElfSym::End2}, Last->LastSec); // _etext is the first location after the last read-only loadable segment. - if (LastRO) { - if (ElfSym::Etext1) - ElfSym::Etext1->Section = LastRO->LastSec; - if (ElfSym::Etext2) - ElfSym::Etext2->Section = LastRO->LastSec; - } + if (LastRO) + SetSection({ElfSym::Etext1, ElfSym::Etext2}, LastRO->LastSec); // _edata points to the end of the last non SHT_NOBITS section. if (LastRW) { @@ -934,10 +932,8 @@ if (OutputSections[I]->Type == SHT_NOBITS) break; - if (ElfSym::Edata1) - ElfSym::Edata1->Section = OutputSections[I - 1]; - if (ElfSym::Edata2) - ElfSym::Edata2->Section = OutputSections[I - 1]; + if (I > 0) + SetSection({ElfSym::Edata1, ElfSym::Edata2}, OutputSections[I - 1]); } if (ElfSym::Bss)