diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -835,7 +835,7 @@ ctx->lmaOffset = sec->lmaExpr().getValue() - dot; if (MemoryRegion *mr = sec->lmaRegion) - ctx->lmaOffset = mr->curPos - dot; + ctx->lmaOffset = alignTo(mr->curPos, sec->alignment) - 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 diff --git a/lld/test/ELF/linkerscript/lma-align.test b/lld/test/ELF/linkerscript/lma-align.test new file mode 100644 --- /dev/null +++ b/lld/test/ELF/linkerscript/lma-align.test @@ -0,0 +1,32 @@ +# REQUIRES: x86 +# RUN: echo 'ret; .data.rel.ro; .balign 16; .byte 0; .data; .byte 0; .bss; .byte 0' | \ +# RUN: llvm-mc -filetype=obj -triple=x86_64 - -o %t.o +# RUN: ld.lld -T %s %t.o -o %t +# RUN: llvm-readelf -S -l %t | FileCheck %s + +# CHECK: Name Type Address Off Size ES Flg Lk Inf Al +# CHECK-NEXT: NULL 0000000000000000 000000 000000 00 0 0 0 +# CHECK-NEXT: .text PROGBITS 0000000000001000 001000 000001 00 AX 0 0 4 +# CHECK-NEXT: .data.rel.ro PROGBITS 0000000000011000 002000 000001 00 WA 0 0 16 +# CHECK-NEXT: .data PROGBITS 0000000000011010 002010 000001 00 WA 0 0 16 +# CHECK-NEXT: .bss NOBITS 0000000000011040 002011 000001 00 WA 0 0 64 + +# CHECK: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align +# CHECK-NEXT: LOAD 0x001000 0x0000000000001000 0x0000000000001000 0x000001 0x000001 R E 0x1000 +# CHECK-NEXT: LOAD 0x002000 0x0000000000011000 0x0000000000001010 0x000001 0x000001 RW 0x1000 +## FIXME .data and .bss should be placed in different PT_LOAD segments +## because their LMA regions are different. +# CHECK-NEXT: LOAD 0x002010 0x0000000000011010 0x0000000000001020 0x000001 0x000031 RW 0x1000 + +MEMORY { + ROM : ORIGIN = 0x1000, LENGTH = 1K + RAM : ORIGIN = 0x11000, LENGTH = 1K +} +SECTIONS { + .text 0x1000 : { *(.text*) } >ROM + ## Input section alignment decides output section alignment. + .data.rel.ro 0x11000 : { *(.data.rel.ro) } >RAM AT>ROM + ## ALIGN decides output section alignment. + .data . : ALIGN(16) { *(.data*) } >RAM AT>ROM + .bss . : ALIGN(64) { *(.bss*) } >RAM +}