Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -59,7 +59,7 @@ }; void copyLocalSymbols(); - void addReservedSymbols(); + void addReservedSymbols(ArrayRef *> Sections); void createSections(); void addPredefinedSections(); bool needsGot(); @@ -210,7 +210,6 @@ template void Writer::run() { if (!Config->DiscardAll) copyLocalSymbols(); - addReservedSymbols(); createSections(); if (HasError) return; @@ -879,6 +878,17 @@ if (Comp != 0) return Comp < 0; + // Add .interp first because some loaders want to see that section + // on the first page of the executable file when loaded into memory. + if (A == Out::Interp || B == Out::Interp) + return A == Out::Interp; + + // A core file does not usually contain unmodified segments except + // the first page of the executable. Add the build ID section now + // so that the section is included in the first page. + if (A == Out::BuildId || B == Out::BuildId) + return A == Out::BuildId; + uintX_t AFlags = A->getFlags(); uintX_t BFlags = B->getFlags(); @@ -1163,7 +1173,9 @@ // The linker is expected to define some symbols depending on // the linking result. This function defines such symbols. -template void Writer::addReservedSymbols() { +template +void Writer::addReservedSymbols( + ArrayRef *> Sections) { if (Config->EMachine == EM_MIPS) { // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer // so that it points to an absolute address which is relative to GOT. @@ -1222,6 +1234,25 @@ Define("_end", ElfSym::End, ElfSym::End2); Define("_etext", ElfSym::Etext, ElfSym::Etext2); Define("_edata", ElfSym::Edata, ElfSym::Edata2); + + // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop + // symbols for sections, so that the runtime can get the start and end + // addresses of each section by section name. Add such symbols. + if (!Config->Relocatable) { + addStartEndSymbols(); + for (OutputSectionBase *Sec : Sections) + addStartStopSymbols(Sec); + } + + // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type. + // It should be okay as no one seems to care about the type. + // Even the author of gold doesn't remember why gold behaves that way. + // https://sourceware.org/ml/binutils/2002-03/msg00360.html + if (isOutputDynamic()) + Symtab.addSynthetic("_DYNAMIC", *Out::Dynamic, 0); + + // Define __rel[a]_iplt_{start,end} symbols if needed. + addRelIpltSymbols(); } // Sort input sections by section name suffixes for @@ -1239,17 +1270,6 @@ // Create output section objects and add them to OutputSections. template void Writer::createSections() { - // Add .interp first because some loaders want to see that section - // on the first page of the executable file when loaded into memory. - if (needsInterpSection()) - OutputSections.push_back(Out::Interp); - - // A core file does not usually contain unmodified segments except - // the first page of the executable. Add the build ID section now - // so that the section is included in the first page. - if (Out::BuildId) - OutputSections.push_back(Out::BuildId); - // Create output sections for input object file sections. std::vector *> RegularSections; OutputSectionFactory Factory; @@ -1293,24 +1313,7 @@ sortCtorsDtors(Factory.lookup(".ctors", SHT_PROGBITS, SHF_WRITE | SHF_ALLOC)); sortCtorsDtors(Factory.lookup(".dtors", SHT_PROGBITS, SHF_WRITE | SHF_ALLOC)); - // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop - // symbols for sections, so that the runtime can get the start and end - // addresses of each section by section name. Add such symbols. - if (!Config->Relocatable) { - addStartEndSymbols(); - for (OutputSectionBase *Sec : RegularSections) - addStartStopSymbols(Sec); - } - - // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type. - // It should be okay as no one seems to care about the type. - // Even the author of gold doesn't remember why gold behaves that way. - // https://sourceware.org/ml/binutils/2002-03/msg00360.html - if (isOutputDynamic()) - Symtab.addSynthetic("_DYNAMIC", *Out::Dynamic, 0); - - // Define __rel[a]_iplt_{start,end} symbols if needed. - addRelIpltSymbols(); + addReservedSymbols(RegularSections); if (Out::EhFrameHdr->Sec) Out::EhFrameHdr->Sec->finalize(); @@ -1420,6 +1423,10 @@ // This order is not the same as the final output order // because we sort the sections using their attributes below. + if (needsInterpSection()) + Add(Out::Interp); + if (Out::BuildId) + Add(Out::BuildId); Add(Out::SymTab); Add(Out::ShStrTab); Add(Out::StrTab);