Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -701,6 +701,22 @@ Sec->assignOffsets(); } +template +static bool canSharePtLoad(const OutputSectionBase &S1, + const OutputSectionBase &S2) { + if (!(S1.getFlags() & SHF_ALLOC) || !(S2.getFlags() & SHF_ALLOC)) + return false; + + bool S1IsWrite = S1.getFlags() & SHF_WRITE; + bool S2IsWrite = S2.getFlags() & SHF_WRITE; + if (S1IsWrite != S2IsWrite) + return false; + + if (!S1IsWrite) + return true; // RO and RX share a PT_LOAD with linker scripts. + return (S1.getFlags() & SHF_EXECINSTR) == (S2.getFlags() & SHF_EXECINSTR); +} + template void Writer::sortSections() { if (!ScriptConfig->HasSections) { std::stable_sort(OutputSections.begin(), OutputSections.end(), @@ -735,19 +751,38 @@ std::stable_sort(OutputSections.begin(), OutputSections.end(), compareSections); - auto I = OutputSections.begin(); auto E = OutputSections.end(); - auto NonScriptI = std::find_if(I, E, [](OutputSectionBase *S) { - return Script::X->getSectionIndex(S->getName()) == INT_MAX; - }); + auto NonScriptI = + std::find_if(OutputSections.begin(), E, [](OutputSectionBase *S) { + return Script::X->getSectionIndex(S->getName()) == INT_MAX; + }); while (NonScriptI != E) { - auto FirstGreater = - std::find_if(I, NonScriptI, [&](OutputSectionBase *S) { - return compareSectionsNonScript(*NonScriptI, S); - }); + auto FirstGreater = NonScriptI; + + // Try to find best place to put orphan (non-scripted) section N before + // some scripted section S in order to minimize number of PT_LOAD segments. + // We'll try to use following places, according to their priorities (1,2,3): + // 1) Before section S with same access attributes, + // if compareSectionsNonScript(N,S) is true. + // 2) After the last section with same access attributes, if any + // 3) Before any section for which compareSectionsNonScript(N, S) is true. + // In such case there are no sections which can share segment with + // section N. + for (auto I = OutputSections.begin(); I != NonScriptI; ++I) { + bool Greater = compareSectionsNonScript(*NonScriptI, *I); + if (Greater && FirstGreater == NonScriptI) + FirstGreater = I; + + if (canSharePtLoad(**I, **NonScriptI)) { + if (Greater) { + FirstGreater = I; + break; + } + FirstGreater = I + 1; + } + } std::rotate(FirstGreater, NonScriptI, NonScriptI + 1); ++NonScriptI; - ++I; } } Index: test/ELF/linkerscript/orphan.s =================================================================== --- test/ELF/linkerscript/orphan.s +++ test/ELF/linkerscript/orphan.s @@ -0,0 +1,29 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t +# RUN: echo "SECTIONS { \ +# RUN: .text : { *(.text) } \ +# RUN: .rw1 : { *(.rw1) } \ +# RUN: .rw2 : { *(.rw2) } \ +# RUN: .rw3 : { *(.rw3) } \ +# RUN: }" > %t.script +# RUN: ld.lld -o %t1 --script %t.script %t +# RUN: llvm-objdump -section-headers %t1 | FileCheck %s + +# CHECK: 0 00000000 0000000000000000 +# CHECK-NEXT: 1 .text 00000000 0000000000000000 TEXT DATA +# CHECK-NEXT: 2 .jcr 00000008 0000000000000000 DATA +# CHECK-NEXT: 3 .rw1 00000008 0000000000000008 DATA +# CHECK-NEXT: 4 .rw2 00000008 0000000000000010 DATA +# CHECK-NEXT: 5 .rw3 00000008 0000000000000018 DATA + +.section .rw1, "aw" + .quad 0 + +.section .rw2, "aw" + .quad 0 + +.section .rw3, "aw" + .quad 0 + +.section .jcr, "aw" + .quad 0 Index: test/ELF/linkerscript/sections-constraint.s =================================================================== --- test/ELF/linkerscript/sections-constraint.s +++ test/ELF/linkerscript/sections-constraint.s @@ -23,9 +23,9 @@ # NO1-NEXT: Idx Name Size # NO1-NEXT: 0 00000000 # NO1: .writable 00000004 +# NO1: .foo.2 00000004 # NO1: .readable 00000004 # NO1: .foo.1 00000004 -# NO1: .foo.2 00000004 .global _start _start: