Index: ELF/LinkerScript.h =================================================================== --- ELF/LinkerScript.h +++ ELF/LinkerScript.h @@ -206,6 +206,7 @@ uint64_t ThreadBssOffset = 0; OutputSection *OutSec = nullptr; MemoryRegion *MemRegion = nullptr; + MemoryRegion *LMARegion = nullptr; uint64_t LMAOffset = 0; }; Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -589,6 +589,10 @@ // If there is a memory region associated with this input section, then // place the section in that region and update the region index. + if (Ctx->LMARegion) + Ctx->LMARegion->CurPos += Pos - Before; + // FIXME: should we also produce overflow errors for LMARegion? + if (Ctx->MemRegion) { uint64_t &CurOffset = Ctx->MemRegion->CurPos; CurOffset += Pos - Before; @@ -651,6 +655,7 @@ setDot(Sec->AddrExpr, Sec->Location, false); Ctx->MemRegion = Sec->MemRegion; + Ctx->LMARegion = Sec->LMARegion; if (Ctx->MemRegion) Dot = Ctx->MemRegion->CurPos; @@ -660,7 +665,7 @@ Ctx->LMAOffset = Sec->LMAExpr().getValue() - Dot; if (MemoryRegion *MR = Sec->LMARegion) - Ctx->LMAOffset = MR->Origin - Dot; + Ctx->LMAOffset = MR->CurPos - Dot; // If neither AT nor AT> is specified for an allocatable section, the linker // will set the LMA such that the difference between VMA and LMA for the @@ -690,6 +695,8 @@ Dot += Cmd->Size; if (Ctx->MemRegion) Ctx->MemRegion->CurPos += Cmd->Size; + if (Ctx->LMARegion) + Ctx->LMARegion->CurPos += Cmd->Size; Ctx->OutSec->Size = Dot - Ctx->OutSec->Addr; continue; } Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -1626,7 +1626,8 @@ // different flags or is loaded at a discontiguous address using AT linker // script command. uint64_t NewFlags = computeFlags(Sec->getPhdrFlags()); - if (Sec->LMAExpr || Flags != NewFlags) { + if (Sec->LMAExpr || Sec->MemRegion != Load->FirstSec->MemRegion || + Flags != NewFlags) { Load = AddHdr(PT_LOAD, NewFlags); Flags = NewFlags; } Index: test/ELF/linkerscript/at3.s =================================================================== --- /dev/null +++ test/ELF/linkerscript/at3.s @@ -0,0 +1,38 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o +# RUN: echo "MEMORY { \ +# RUN: FOO (ax) : ORIGIN = 0x1000, LENGTH = 0x100 \ +# RUN: BAR (ax) : ORIGIN = 0x2000, LENGTH = 0x100 \ +# RUN: ZED (ax) : ORIGIN = 0x3000, LENGTH = 0x100 \ +# RUN: FLASH (ax) : ORIGIN = 0x6000, LENGTH = 0x200 \ +# RUN: } \ +# RUN: SECTIONS { \ +# RUN: .foo1 : { *(.foo1) } > FOO AT>FLASH \ +# RUN: .foo2 : { *(.foo2) BYTE(0x42) } > BAR AT>FLASH \ +# RUN: .foo3 : { *(.foo3) } > ZED AT>FLASH \ +# RUN: }" > %t.script +# RUN: ld.lld %t.o --script %t.script -o %t +# RUN: llvm-readelf -sections -program-headers %t | FileCheck %s + + +# CHECK: .foo1 PROGBITS 0000000000001000 001000 +# CHECK: .foo2 PROGBITS 0000000000002000 002000 +# CHECK: .foo3 PROGBITS 0000000000003000 003000 + +# CHECK: Program Headers: +# CHECK-NOT: LOAD + +# CHECK: LOAD 0x001000 0x0000000000001000 0x0000000000006000 +# CHECK: LOAD 0x002000 0x0000000000002000 0x0000000000006008 +# CHECK: LOAD 0x003000 0x0000000000003000 0x0000000000006011 + +# CHECK-NOT: LOAD + +.section .foo1, "a" +.quad 0 + +.section .foo2, "ax" +.quad 0 + +.section .foo3, "ax" +.quad 0