diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -762,7 +762,9 @@ ctx->outSec = sec; uint64_t before = advance(0, 1); - ctx->outSec->addr = advance(0, ctx->outSec->alignment); + uint32_t align = + sec->alignExpr ? sec->alignExpr().getValue() : ctx->outSec->alignment; + ctx->outSec->addr = advance(0, align); expandMemoryRegions(ctx->outSec->addr - before); } diff --git a/lld/test/ELF/linkerscript/lma-align.test b/lld/test/ELF/linkerscript/lma-align.test --- a/lld/test/ELF/linkerscript/lma-align.test +++ b/lld/test/ELF/linkerscript/lma-align.test @@ -1,5 +1,6 @@ # REQUIRES: x86 -# RUN: echo 'ret; .data.rel.ro; .balign 16; .byte 0; .data; .byte 0; .bss; .byte 0' | \ +# RUN: echo 'ret; .data.rel.ro; .balign 16; .byte 0; \ +# RUN: .data; .balign 32; .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 @@ -8,13 +9,13 @@ # 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: .data PROGBITS 0000000000011010 002010 000011 00 WA 0 0 32 # CHECK-NEXT: .bss NOBITS 0000000000011040 002040 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 -# CHECK-NEXT: LOAD 0x002010 0x0000000000011010 0x0000000000001020 0x000001 0x000001 RW 0x1000 +# CHECK-NEXT: LOAD 0x002010 0x0000000000011010 0x0000000000001020 0x000011 0x000011 RW 0x1000 # CHECK-NEXT: LOAD 0x002040 0x0000000000011040 0x0000000000011040 0x000000 0x000001 RW 0x1000 MEMORY { diff --git a/lld/test/ELF/linkerscript/section-align2.s b/lld/test/ELF/linkerscript/section-align2.s new file mode 100644 --- /dev/null +++ b/lld/test/ELF/linkerscript/section-align2.s @@ -0,0 +1,24 @@ +# REQUIRES: x86 +# RUN: echo 'ret; .data.rel.ro; .balign 8; .byte 0; \ +# RUN: .data; .byte 0; .bss; .balign 32; .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 0000000000001010 001010 000001 00 WA 0 0 16 +# CHECK-NEXT: .data PROGBITS 0000000000002000 002000 000001 00 WA 0 0 1 +# CHECK-NEXT: .bss NOBITS 0000000000002010 002001 000011 00 WA 0 0 32 + +SECTIONS { + .text 0x1000 : {} + ## The output section is aligned to 16. + .data.rel.ro . : ALIGN(16) {} + + .data 0x2000 : {} + ## The output section is aligned to 16. + ## The input section has a larger alignment and is thus preceded by a gap. + .bss . : ALIGN(16) {} +}