Index: ELF/InputSection.h =================================================================== --- ELF/InputSection.h +++ ELF/InputSection.h @@ -324,6 +324,11 @@ // to. The writer sets a value. uint64_t OutSecOff = 0; + // The order in which the section was added to its output section. This is + // used when ordering SHF_LINK_ORDER sections. It is not set for sections + // inserted late, such as thunk sections. + llvm::Optional OutSecPos; + static bool classof(const SectionBase *S); InputSectionBase *getRelocatedSection(); Index: ELF/OutputSections.h =================================================================== --- ELF/OutputSections.h +++ ELF/OutputSections.h @@ -110,6 +110,10 @@ std::vector ZDebugHeader; llvm::SmallVector CompressedData; + // The number of input sections assigned to this section excluding + // late-inserted sections, such as thunk sections. + size_t InputSectionCount = 0; + uint32_t getFiller(); }; Index: ELF/OutputSections.cpp =================================================================== --- ELF/OutputSections.cpp +++ ELF/OutputSections.cpp @@ -116,13 +116,7 @@ IS->Parent = this; Flags |= IS->Flags; Alignment = std::max(Alignment, IS->Alignment); - - // The actual offsets will be computed by assignAddresses. For now, use - // crude approximation so that it is at least easy for other code to know the - // section order. It is also used to calculate the output section size early - // for compressed debug sections. - IS->OutSecOff = alignTo(Size, IS->Alignment); - this->Size = IS->OutSecOff + IS->getSize(); + IS->OutSecPos = InputSectionCount++; // If this section contains a table of fixed-size entries, sh_entsize // holds the element size. If it contains elements of different size we @@ -189,6 +183,14 @@ !Name.startswith(".debug_")) return; + // Calculate the section offsets and size pre-compression. + for (BaseCommand * Cmd : SectionCommands) + if (auto *ISD = dyn_cast(Cmd)) + for (InputSection *IS : ISD->Sections) { + IS->OutSecOff = alignTo(Size, IS->Alignment); + this->Size = IS->OutSecOff + IS->getSize(); + } + // Create a section header. ZDebugHeader.resize(sizeof(Elf_Chdr)); auto *Hdr = reinterpret_cast(ZDebugHeader.data()); @@ -281,7 +283,9 @@ OutputSection *BOut = LB->getParent(); if (AOut != BOut) return AOut->SectionIndex < BOut->SectionIndex; - return LA->OutSecOff < LB->OutSecOff; + assert(LA->OutSecPos && LB->OutSecPos && + "Cannot compare late-inserted section positions."); + return LA->OutSecPos < LB->OutSecPos; } template Index: test/ELF/linkerscript/unused-synthetic.s =================================================================== --- test/ELF/linkerscript/unused-synthetic.s +++ test/ELF/linkerscript/unused-synthetic.s @@ -13,6 +13,16 @@ # CHECK: .text # CHECK-NEXT: .dynsym +# Test that the size of a removed unused synthetic input section does not get +# added to the output section size, if the output section has symbol +# assignments, preventing removal of the output section. +# RUN: echo "SECTIONS { \ +# RUN: .got.plt : { a_sym = .; *(.got.plt) } \ +# RUN: }" > %t2.script +# RUN: ld.lld -shared -o %t2.so --script %t2.script %t.o +# RUN: llvm-objdump -section-headers %t2.so | FileCheck %s --check-prefix=CHECK2 +# CHECK2: .got.plt 00000000 + .global _start _start: nop