[PATCH] D29689: [ELF] Handle output section alignment in linker scripts

Petr Hosek via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 7 14:54:47 PST 2017


phosek created this revision.
phosek added a project: lld.

LLD already parses ALIGN expression to specify alignment for output  sections in linker scripts but it never applied the alignment to the output section.


Repository:
  rL LLVM

https://reviews.llvm.org/D29689

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


Index: test/ELF/linkerscript/section-align.s
===================================================================
--- /dev/null
+++ test/ELF/linkerscript/section-align.s
@@ -0,0 +1,62 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+
+# RUN: echo "SECTIONS { \
+# RUN:   .aaa : ALIGN(4096) { *(.aaa) } \
+# RUN:   .bbb : ALIGN(4096 * 4) { *(.bbb) } \
+# RUN:   .ccc : ALIGN(4096 * 8) { *(.ccc) } \
+# RUN: }" > %t.script
+# RUN: ld.lld -o %t1 --script %t.script %t
+# RUN: llvm-readobj -sections %t1 | FileCheck %s
+
+.global _start
+_start:
+ nop
+
+// CHECK:      Name: .aaa
+// CHECK-NEXT: Type: SHT_PROGBITS
+// CHECK-NEXT: Flags [
+// CHECK-NEXT:   SHF_ALLOC
+// CHECK-NEXT: ]
+// CHECK-NEXT: Address:
+// CHECK-NEXT: Offset:
+// CHECK-NEXT: Size: 8
+// CHECK-NEXT: Link: 0
+// CHECK-NEXT: Info: 0
+// CHECK-NEXT: AddressAlignment: 4096
+// CHECK-NEXT: EntrySize:
+
+.section .aaa, "a"
+.quad 0
+
+// CHECK:      Name: .bbb
+// CHECK-NEXT: Type: SHT_PROGBITS
+// CHECK-NEXT: Flags [
+// CHECK-NEXT:   SHF_ALLOC
+// CHECK-NEXT: ]
+// CHECK-NEXT: Address:
+// CHECK-NEXT: Offset:
+// CHECK-NEXT: Size: 8
+// CHECK-NEXT: Link: 0
+// CHECK-NEXT: Info: 0
+// CHECK-NEXT: AddressAlignment: 16384
+// CHECK-NEXT: EntrySize:
+
+.section .bbb, "a"
+.quad 0
+
+// CHECK:      Name: .ccc
+// CHECK-NEXT: Type: SHT_PROGBITS
+// CHECK-NEXT: Flags [
+// CHECK-NEXT:   SHF_ALLOC
+// CHECK-NEXT: ]
+// CHECK-NEXT: Address:
+// CHECK-NEXT: Offset:
+// CHECK-NEXT: Size: 8
+// CHECK-NEXT: Link: 0
+// CHECK-NEXT: Info: 0
+// CHECK-NEXT: AddressAlignment: 32768
+// CHECK-NEXT: EntrySize:
+
+.section .ccc, "a"
+.quad 0
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -368,6 +368,13 @@
       // Add input sections to an output section.
       for (InputSectionBase<ELFT> *S : V)
         addSection(Factory, S, Cmd->Name);
+
+      if (Cmd->AlignExpr) {
+        uint32_t Align = Cmd->AlignExpr(0);
+        for (OutputSectionBase *S : *OutputSections)
+          if (S->getName() == Cmd->Name)
+            S->updateAlignment(Align);
+      }
     }
   }
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D29689.87531.patch
Type: text/x-patch
Size: 2186 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170207/4a73fcef/attachment.bin>


More information about the llvm-commits mailing list