Index: lld/trunk/ELF/LinkerScript.cpp =================================================================== --- lld/trunk/ELF/LinkerScript.cpp +++ lld/trunk/ELF/LinkerScript.cpp @@ -135,7 +135,7 @@ // Update to location counter means update to section size. if (InSec) expandOutputSection(Val - Dot); - else + else if (Val > Dot) expandMemoryRegions(Val - Dot); Dot = Val; @@ -760,14 +760,15 @@ void LinkerScript::assignOffsets(OutputSection *Sec) { if (!(Sec->Flags & SHF_ALLOC)) Dot = 0; - else if (Sec->AddrExpr) - setDot(Sec->AddrExpr, Sec->Location, false); Ctx->MemRegion = Sec->MemRegion; Ctx->LMARegion = Sec->LMARegion; if (Ctx->MemRegion) Dot = Ctx->MemRegion->CurPos; + if ((Sec->Flags & SHF_ALLOC) && Sec->AddrExpr) + setDot(Sec->AddrExpr, Sec->Location, false); + switchTo(Sec); if (Sec->LMAExpr) Index: lld/trunk/test/ELF/linkerscript/out-of-order-section-in-region.s =================================================================== --- lld/trunk/test/ELF/linkerscript/out-of-order-section-in-region.s +++ lld/trunk/test/ELF/linkerscript/out-of-order-section-in-region.s @@ -0,0 +1,22 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t +# RUN: echo "MEMORY { \ +# RUN: REGION (rwx) : ORIGIN = 0x1000, LENGTH = 0x100 \ +# RUN: } \ +# RUN: \ +# RUN: SECTIONS { \ +# RUN: .aaa ORIGIN(REGION) + 0x8 : { *(.aaa) } > REGION \ +# RUN: _stext = .; \ +# RUN: .bbb ORIGIN(REGION) : { *(.bbb) } > REGION \ +# RUN: . = _stext; \ +# RUN: }" > %t.script +# RUN: ld.lld %t --script %t.script -o %t2 +# RUN: llvm-objdump -section-headers %t2 | FileCheck %s +# CHECK: .aaa 00000008 0000000000001008 DATA +# CHECK: .bbb 00000008 0000000000001000 DATA + +.section .aaa, "a" +.quad 0 + +.section .bbb, "a" +.quad 0