Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -681,8 +681,9 @@ RF_NOT_INTERP = 1 << 17, RF_NOT_ALLOC = 1 << 16, RF_WRITE = 1 << 15, - RF_EXEC_WRITE = 1 << 13, - RF_EXEC = 1 << 12, + RF_EXEC_WRITE = 1 << 14, + RF_EXEC = 1 << 13, + RF_PROGBITS = 1 << 12, RF_NON_TLS_BSS = 1 << 11, RF_NON_TLS_BSS_RO = 1 << 10, RF_NOT_TLS = 1 << 9, @@ -717,6 +718,10 @@ if (!(Sec->Flags & SHF_ALLOC)) return Rank | RF_NOT_ALLOC; + // Place sections with PROGBITS closer. + if (Sec->Type == SHT_PROGBITS && !isRelroSection(Sec)) + Rank |= RF_PROGBITS; + // Sort sections based on their access permission in the following // order: R, RX, RWX, RW. This order is based on the following // considerations: @@ -1675,7 +1680,9 @@ // symbols for a few sections. This function defines them. template void Writer::addStartEndSymbols() { auto Define = [&](StringRef Start, StringRef End, OutputSection *OS) { - // These symbols resolve to the image base if the section does not exist. + // These symbols resolve to the image base or ".text" if the section + // does not exist. Set symbol value to ".text" mitigates the possibilities + // that an relocation from .text section to these symbols overflows. // A special value -1 indicates end of the section. if (OS) { addOptionalRegular(Start, OS, 0); @@ -1683,6 +1690,8 @@ } else { if (Config->Pic) OS = Out::ElfHeader; + OutputSection* p = findSection(".text"); + if (p) OS = p; addOptionalRegular(Start, OS, 0); addOptionalRegular(End, OS, 0); }