Index: ELF/LinkerScript.h =================================================================== --- ELF/LinkerScript.h +++ ELF/LinkerScript.h @@ -232,6 +232,7 @@ void assignSymbol(SymbolAssignment *Cmd, bool InSec); void setDot(Expr E, const Twine &Loc, bool InSec); void expandOutputSection(uint64_t Size); + void expandMemoryRegions(uint64_t Size); std::vector computeInputSections(const InputSectionDescription *); Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -112,8 +112,7 @@ "': overflowed by " + Twine(NewSize - MemRegion->Length) + " bytes"); } -void LinkerScript::expandOutputSection(uint64_t Size) { - Ctx->OutSec->Size += Size; +void LinkerScript::expandMemoryRegions(uint64_t Size) { if (Ctx->MemRegion) expandMemoryRegion(Ctx->MemRegion, Size, Ctx->MemRegion->Name, Ctx->OutSec->Name); @@ -122,6 +121,11 @@ Ctx->OutSec->Name); } +void LinkerScript::expandOutputSection(uint64_t Size) { + Ctx->OutSec->Size += Size; + expandMemoryRegions(Size); +} + void LinkerScript::setDot(Expr E, const Twine &Loc, bool InSec) { uint64_t Val = E().getValue(); if (Val < Dot && InSec) @@ -691,9 +695,12 @@ void LinkerScript::switchTo(OutputSection *Sec) { if (Ctx->OutSec == Sec) return; - Ctx->OutSec = Sec; - Ctx->OutSec->Addr = advance(0, Ctx->OutSec->Alignment); + + uint64_t Before = advance(0, 1); + uint64_t Pos = advance(0, Ctx->OutSec->Alignment); + Ctx->OutSec->Addr = Pos; + expandMemoryRegions(Pos - Before); } // This function searches for a memory region to place the given output Index: test/ELF/linkerscript/memory-region-alignment.test =================================================================== --- test/ELF/linkerscript/memory-region-alignment.test +++ test/ELF/linkerscript/memory-region-alignment.test @@ -0,0 +1,36 @@ +# REQUIRES: x86 +# RUN: echo '.section .foo,"a"; .quad 0; .section .zed,"M",@progbits,1; .byte 0' > %t.s +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %t.s -o %t.o + +MEMORY { + ram (rwx): org = 0x1, len = 96K +} + +SECTIONS { + .foo : ALIGN(8) { + *(.foo) + } > ram +} + +# RUN: ld.lld %t.o -o %t --script %s +# RUN: llvm-readobj -sections %t | FileCheck %s + +# CHECK: Name: .foo +# CHECK: Address: 0x8 +# CHECK: Offset: 0x1008 +# CHECK: Size: 8 + +# CHECK: Name: .text +# CHECK: Address: 0x10 +# CHECK: Offset: 0x1010 +# CHECK: Size: 0 + +# CHECK: Name: .zed +# CHECK: Address: 0x0 +# CHECK: Offset: 0x1010 +# CHECK: Size: 1 + +# CHECK: Name: .comment +# CHECK: Address: 0x0 +# CHECK: Offset: 0x1011 +# CHECK: Size: 8