Index: ELF/InputSection.h =================================================================== --- ELF/InputSection.h +++ ELF/InputSection.h @@ -319,6 +319,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 @@ -115,13 +115,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. Consequently, if this contains two or more @@ -320,6 +314,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()); @@ -412,7 +414,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: ELF/SyntheticSections.h =================================================================== --- ELF/SyntheticSections.h +++ ELF/SyntheticSections.h @@ -170,7 +170,6 @@ void writeTo(uint8_t *Buf) override; size_t getSize() const override { return Size; } bool updateAllocSize() override; - void finalizeContents() override; bool empty() const override; void addEntry(SymbolBody &Sym, int64_t Addend, RelExpr Expr); bool addDynTlsEntry(SymbolBody &Sym); Index: ELF/SyntheticSections.cpp =================================================================== --- ELF/SyntheticSections.cpp +++ ELF/SyntheticSections.cpp @@ -816,8 +816,6 @@ LocalEntries32.size(); } -void MipsGotSection::finalizeContents() { updateAllocSize(); } - bool MipsGotSection::updateAllocSize() { PageEntriesNum = 0; for (std::pair &P : PageIndexMap) { @@ -1147,7 +1145,11 @@ add({DT_MIPS_FLAGS, RHF_NOTPOT}); add({DT_MIPS_BASE_ADDRESS, Target->getImageBase()}); add({DT_MIPS_SYMTABNO, InX::DynSymTab->getNumSymbols()}); - add({DT_MIPS_LOCAL_GOTNO, InX::MipsGot->getLocalEntriesNum()}); + + // The number of local got entries has not yet been determined. This value + // will be set in writeTo(). + add({DT_MIPS_LOCAL_GOTNO, uint64_t(0)}); + if (const SymbolBody *B = InX::MipsGot->getFirstGlobalEntry()) add({DT_MIPS_GOTSYM, B->DynsymIndex}); else @@ -1182,7 +1184,10 @@ P->d_un.d_ptr = E.Sym->getVA(); break; case Entry::PlainInt: - P->d_un.d_val = E.Val; + if (E.Tag == DT_MIPS_LOCAL_GOTNO) + P->d_un.d_val = InX::MipsGot->getLocalEntriesNum(); + else + P->d_un.d_val = E.Val; break; } ++P; 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