[PATCH] D34326: [ELF] - Support SORT_BY_INIT_PRIORITY for .ctors.*/.dtors sections in linkerscript.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 20 01:29:40 PDT 2017


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

- Updated according to my last comment.


https://reviews.llvm.org/D34326

Files:
  ELF/LinkerScript.cpp
  test/ELF/linkerscript/sort-init2.s


Index: test/ELF/linkerscript/sort-init2.s
===================================================================
--- test/ELF/linkerscript/sort-init2.s
+++ test/ELF/linkerscript/sort-init2.s
@@ -0,0 +1,22 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o
+# RUN: echo "SECTIONS { \
+# RUN:       .init_array : { *(SORT_BY_INIT_PRIORITY(.ctors.*)) } \
+# RUN:       .fini_array : { *(SORT_BY_INIT_PRIORITY(.dtors.*)) } }" > %t1.script
+# RUN: ld.lld --script %t1.script %t1.o -o %t2
+# RUN: llvm-objdump -s %t2 | FileCheck %s
+
+# CHECK:      Contents of section .init_array:
+# CHECK-NEXT:  0301
+# CHECK:      Contents of section .fini_array:
+# CHECK-NEXT:  1311
+
+.section .ctors.5, "aw", @progbits
+  .byte 0x1
+.section .ctors.100, "aw", @progbits
+  .byte 0x3
+
+.section .dtors.5, "aw", @progbits
+  .byte 0x11
+.section .dtors.100, "aw", @progbits
+  .byte 0x13
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -250,6 +250,14 @@
     };
   case SortSectionPolicy::Priority:
     return [](InputSectionBase *A, InputSectionBase *B) {
+      // This is used for SORT_BY_INIT_PRIORITY.
+      // Which sorts sections by GCC priority attribute in ascending order.
+      // Compiler encodes priority value in section name. For .[cd]tors
+      // it is encoded as (65535 - Priority), and for .{fini,init}array it is
+      // Priority itself. So comparsion predicate is different.
+      if ((A->Name.startswith(".ctors.") || A->Name.startswith(".dtors.")) &&
+          (B->Name.startswith(".ctors.") || B->Name.startswith(".dtors.")))
+        return getPriority(A->Name) > getPriority(B->Name);
       return getPriority(A->Name) < getPriority(B->Name);
     };
   default:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D34326.103172.patch
Type: text/x-patch
Size: 1820 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170620/5a380a12/attachment.bin>


More information about the llvm-commits mailing list