[PATCH] D29453: [ELF] - Allow going over alignment commands in algorithm of placing non-alloc orphans.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 7 08:33:02 PST 2017


grimar updated this revision to Diff 87457.
grimar added a comment.

- Simplified testcase.


https://reviews.llvm.org/D29453

Files:
  ELF/LinkerScript.cpp
  test/ELF/linkerscript/symbols-and-orphans.s


Index: test/ELF/linkerscript/symbols-and-orphans.s
===================================================================
--- test/ELF/linkerscript/symbols-and-orphans.s
+++ test/ELF/linkerscript/symbols-and-orphans.s
@@ -0,0 +1,17 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+
+# RUN: echo "SECTIONS { . = SIZEOF_HEADERS; \
+# RUN:  .text : { *(.text) }                \
+# RUN:  . = ALIGN(8);                       \
+# RUN:  Sym = .; }" > %t.script
+# RUN: ld.lld -o %t2 --script %t.script %t
+# RUN: llvm-objdump -section-headers -t %t2 | FileCheck %s
+
+# CHECK: Sections:
+# CHECK:   1 .text         00000000 00000000000000e8 TEXT DATA
+# CHECK: SYMBOL TABLE:
+# CHECK: 00000000000000e8 .text 00000000 Sym
+
+.section .orphan,""
+ .quad 0
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -666,19 +666,27 @@
 // doesn't break the intended meaning of the begin/end symbols.
 // We don't want to go over sections since Writer<ELFT>::sortSections is the
 // one in charge of deciding the order of the sections.
-// We don't want to go over alignments, since doing so in
+// We don't want to go over alignments for SHF_ALLOC sections, since doing so in
 //  rx_sec : { *(rx_sec) }
 //  . = ALIGN(0x1000);
 //  /* The RW PT_LOAD starts here*/
 //  rw_sec : { *(rw_sec) }
 // would mean that the RW PT_LOAD would become unaligned.
-static bool shouldSkip(const BaseCommand &Cmd) {
+// We allow going over alignments for non-allocatable sections, that allows
+// next script to assign _end an expected value. Non-allocatable sections placed
+// last, and can't be a reason of unaligned PT_LOAD.
+// SECTIONS {
+// .foo : { *(.foo*) }
+// . = ALIGN(16);
+// _end = .;
+// /* All non-allocatable orphans placed here */
+static bool shouldSkip(const BaseCommand &Cmd, bool IsAlloc) {
   if (isa<OutputSectionCommand>(Cmd))
     return false;
   const auto *Assign = dyn_cast<SymbolAssignment>(&Cmd);
   if (!Assign)
     return true;
-  return Assign->Name != ".";
+  return !IsAlloc || Assign->Name != ".";
 }
 
 // Orphan sections are sections present in the input files which are
@@ -736,7 +744,7 @@
     // correct result.
     auto CmdIter = Opt.Commands.begin() + CmdIndex;
     auto E = Opt.Commands.end();
-    while (CmdIter != E && shouldSkip(**CmdIter)) {
+    while (CmdIter != E && shouldSkip(**CmdIter, Sec->Flags & SHF_ALLOC)) {
       ++CmdIter;
       ++CmdIndex;
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D29453.87457.patch
Type: text/x-patch
Size: 2524 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170207/8206a444/attachment.bin>


More information about the llvm-commits mailing list