Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -207,15 +207,6 @@ } template -static OutputSectionBase * -findSection(ArrayRef *> V, StringRef Name) { - for (OutputSectionBase *Sec : V) - if (Sec->getName() == Name) - return Sec; - return nullptr; -} - -template void LinkerScript::assignAddresses( ArrayRef *> Sections) { // Orphan sections are sections present in the input files which @@ -239,23 +230,27 @@ continue; } - OutputSectionBase *Sec = findSection(Sections, Cmd.SectionName); - if (!Sec) - continue; + // Find all the sections with required name. There can be more than + // ont section with such name, if the alignment, flags or type + // attribute differs. + for (OutputSectionBase *Sec : Sections) { + if (Sec->getName() != Cmd.SectionName) + continue; - if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) { - uintX_t TVA = Dot + ThreadBssOffset; - TVA = alignTo(TVA, Sec->getAlign()); - Sec->setVA(TVA); - ThreadBssOffset = TVA - Dot + Sec->getSize(); - continue; - } + if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) { + uintX_t TVA = Dot + ThreadBssOffset; + TVA = alignTo(TVA, Sec->getAlign()); + Sec->setVA(TVA); + ThreadBssOffset = TVA - Dot + Sec->getSize(); + continue; + } - if (Sec->getFlags() & SHF_ALLOC) { - Dot = alignTo(Dot, Sec->getAlign()); - Sec->setVA(Dot); - Dot += Sec->getSize(); - continue; + if (Sec->getFlags() & SHF_ALLOC) { + Dot = alignTo(Dot, Sec->getAlign()); + Sec->setVA(Dot); + Dot += Sec->getSize(); + continue; + } } } } Index: test/ELF/linkerscript-repsection-va.s =================================================================== --- test/ELF/linkerscript-repsection-va.s +++ test/ELF/linkerscript-repsection-va.s @@ -0,0 +1,24 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t + +# RUN: echo "SECTIONS {.foo : {*(.foo.*)} }" > %t.script +# RUN: ld.lld -o %t1 --script %t.script %t +# RUN: llvm-objdump -section-headers %t1 | FileCheck %s +# CHECK: Sections: +# CHECK-NEXT: Idx Name Size Address Type +# CHECK-NEXT: 0 00000000 0000000000000000 +# CHECK-NEXT: 1 .foo 00000004 0000000000000158 DATA +# CHECK-NEXT: 2 .foo 00000004 000000000000015c DATA +# CHECK-NEXT: 3 .text 00000001 0000000000000160 TEXT DATA + +.global _start +_start: + nop + +.section .foo.1,"a" +foo1: + .long 0 + +.section .foo.2,"aw" +foo2: + .long 0