[PATCH] D74286: [ELF] Respect output section alignment for AT> (non-null lmaRegion)

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 8 12:16:15 PST 2020


MaskRay created this revision.
MaskRay added reviewers: grimar, nickdesaulniers, peter.smith, srhines.
Herald added subscribers: llvm-commits, JDevlieghere, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.

When lmaRegion is non-null, respect the output section alignment.
This rule is analogous to `switchTo(sec)` which advances sh_addr (VMA).

This fixes the p_paddr misalignment issue as reported by
https://android-review.googlesource.com/c/trusty/external/trusted-firmware-a/+/1230058

linkerscript/align-lma.s has a FIXME that demonstrates another bug:
`.bss ... >RAM` should be placed in a different PT_LOAD (GNU ld
behavior) because its lmaRegion (nullptr) is different from the previous
section's lmaRegion (ROM).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D74286

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


Index: lld/test/ELF/linkerscript/align-lma.test
===================================================================
--- /dev/null
+++ lld/test/ELF/linkerscript/align-lma.test
@@ -0,0 +1,26 @@
+# REQUIRES: x86
+# RUN: echo 'ret; .data; .byte 0; .bss; .space 1' | 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 PROGBITS 0000000000011000 002000 000001 00  WA  0   0 32
+# CHECK-NEXT: .bss  NOBITS   0000000000011040 002001 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
+## FIXME .data and .bss should be placed in different PT_LOAD segments
+## because their LMAs are different.
+# CHECK-NEXT: LOAD  0x002000 0x0000000000011000 0x0000000000001020 0x000001 0x000041 RW  0x1000
+
+MEMORY {
+  ROM : ORIGIN = 0x1000, LENGTH = 1K
+  RAM : ORIGIN = 0x11000, LENGTH = 1K
+}
+SECTIONS {
+  .text 0x1000 : { *(.text*) } >ROM
+  .data 0x11000 : ALIGN(32) { *(.data*) } >RAM AT>ROM
+  .bss . : ALIGN(64) { *(.bss*) } >RAM
+}
Index: lld/ELF/LinkerScript.cpp
===================================================================
--- lld/ELF/LinkerScript.cpp
+++ 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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D74286.243397.patch
Type: text/x-patch
Size: 1959 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200208/59e0478d/attachment.bin>


More information about the llvm-commits mailing list