[PATCH] D74736: [ELF] Use ALIGN to decide output section alignment

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 17 11:35:26 PST 2020


MaskRay created this revision.
MaskRay added reviewers: grimar, psmith, ruiu.
Herald added subscribers: llvm-commits, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.

Follow-up for D74286 <https://reviews.llvm.org/D74286>.

When ALIGN is present, GNU ld uses it to decide the output section alignment and ignores the maximum of input section alignments.

New test linkerscript/section-align2.s checks we don't overalign sh_addr.
lma-align.test is enhanced a bit to check this case.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D74736

Files:
  lld/ELF/LinkerScript.cpp
  lld/test/ELF/linkerscript/lma-align.test
  lld/test/ELF/linkerscript/section-align2.s


Index: lld/test/ELF/linkerscript/section-align2.s
===================================================================
--- /dev/null
+++ 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) {}
+}
Index: lld/test/ELF/linkerscript/lma-align.test
===================================================================
--- lld/test/ELF/linkerscript/lma-align.test
+++ 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 {
Index: lld/ELF/LinkerScript.cpp
===================================================================
--- lld/ELF/LinkerScript.cpp
+++ 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);
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D74736.245017.patch
Type: text/x-patch
Size: 3445 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200217/bcde0c56/attachment.bin>


More information about the llvm-commits mailing list