[PATCH] D34204: [LLD][LinkerScript] Allow non-alloc sections to be assigned to segments.

Andrew Ng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 14 06:41:52 PDT 2017


andrewng created this revision.
Herald added a subscriber: emaste.

This patch makes changes to allow sections without the SHF_ALLOC bit to be
assigned to segments in a linker script.

The assignment of output sections to segments is performed in
LinkerScript::createPhdrs. Previously, this function would bail as soon as it
encountered an output section which did not have the SHF_ALLOC bit set, thus
preventing any output section without SHF_ALLOC from being assigned to a
segment.

This restriction has now been removed from LinkerScript::createPhdrs and instead
a check for SHF_ALLOC has been added to LinkerScript::adjustSectionsAfterSorting
to not propagate program headers to sections without SHF_ALLOC.


https://reviews.llvm.org/D34204

Files:
  ELF/LinkerScript.cpp
  test/ELF/linkerscript/non-alloc-segment.s


Index: test/ELF/linkerscript/non-alloc-segment.s
===================================================================
--- /dev/null
+++ test/ELF/linkerscript/non-alloc-segment.s
@@ -0,0 +1,28 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+
+## Test that non-alloc section .foo can be assigned to a segment.
+# RUN: echo "PHDRS {text PT_LOAD; foo 0x12345678;} \
+# RUN:       SECTIONS { \
+# RUN:           .text : {*(.text .text*)} :text \
+# RUN:           .foo : {*(.foo)} :foo \
+# RUN:       }" > %t.script
+# RUN: ld.lld -o %t --script %t.script %t.o
+# RUN: llvm-readobj -elf-output-style=GNU -s -l %t | FileCheck %s
+
+# CHECK: Program Headers:
+# CHECK-NEXT:  Type
+# CHECK-NEXT:  LOAD
+# CHECK-NEXT:  <unknown>: 0x12345678
+
+# CHECK:      Section to Segment mapping:
+# CHECK-NEXT:  Segment Sections...
+# CHECK-NEXT:   00     .text
+# CHECK-NEXT:   01     .foo
+
+.global _start
+_start:
+ nop
+
+.section .foo
+ .long 0
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -753,10 +753,15 @@
     if (!Cmd)
       continue;
 
-    if (Cmd->Phdrs.empty())
-      Cmd->Phdrs = DefPhdrs;
-    else
+    if (Cmd->Phdrs.empty()) {
+      OutputSection *Sec = Cmd->Sec;
+      // Only propagate program headers to sections that are allocated.
+      if (Sec && (Sec->Flags & SHF_ALLOC))
+        Cmd->Phdrs = DefPhdrs;
+    }
+    else {
       DefPhdrs = Cmd->Phdrs;
+    }
   }
 
   removeEmptyCommands();
@@ -975,8 +980,6 @@
   // Add output sections to program headers.
   for (OutputSectionCommand *Cmd : OutputSectionCommands) {
     OutputSection *Sec = Cmd->Sec;
-    if (!(Sec->Flags & SHF_ALLOC))
-      break;
 
     // Assign headers specified by linker script
     for (size_t Id : getPhdrIndices(Sec)) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D34204.102539.patch
Type: text/x-patch
Size: 1863 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170614/a207abd1/attachment.bin>


More information about the llvm-commits mailing list