[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
Mon Jun 19 06:57:16 PDT 2017


grimar updated this revision to Diff 103038.
grimar edited the summary of this revision.
grimar added a comment.

- Changed implementation, simplified testcase.

Looks I found finally sorting rule used by gnu linkers.
They do not .[cd]tors simply like stings or like values, 
at least for SORT_BY_INIT_PRIORITY.

With this change bfd and LLD produces similar result for testcase.


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,26 @@
+# 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:  030201
+# CHECK:      Contents of section .fini_array:
+# CHECK-NEXT:  131211
+
+.section .ctors.5, "aw", @progbits
+  .byte 0x1
+.section .ctors.005, "aw", @progbits
+  .byte 0x2
+.section .ctors.100, "aw", @progbits
+  .byte 0x3
+
+.section .dtors.5, "aw", @progbits
+  .byte 0x11
+.section .dtors.005, "aw", @progbits
+  .byte 0x12
+.section .dtors.100, "aw", @progbits
+  .byte 0x13
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -233,6 +233,16 @@
   return false;
 }
 
+static bool compCtorsPriority(StringRef X, StringRef Y) {
+  if (X.empty() && Y.empty())
+    return false;
+  uint64_t ValX, ValY;
+  if (to_integer(X.trim('0'), ValX, 10) && to_integer(Y.trim('0'), ValY, 10))
+    if (ValX != ValY)
+      return ValX < ValY;
+  return X < Y;
+}
+
 // A helper function for the SORT() command.
 static std::function<bool(InputSectionBase *, InputSectionBase *)>
 getComparator(SortSectionPolicy K) {
@@ -250,6 +260,13 @@
     };
   case SortSectionPolicy::Priority:
     return [](InputSectionBase *A, InputSectionBase *B) {
+      // This is used for SORT_BY_INIT_PRIORITY.
+      // Which sorts section by GCC priority attribute.
+      // Manual says order is ascending, but in real life gnu
+      // linkers sort .[cd]tors differently from {.init,.fini}_array's.
+      if ((A->Name.startswith(".ctors.") || A->Name.startswith(".dtors.")) &&
+          (B->Name.startswith(".ctors.") || B->Name.startswith(".dtors.")))
+        return compCtorsPriority(A->Name.substr(7), B->Name.substr(7));
       return getPriority(A->Name) < getPriority(B->Name);
     };
   default:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D34326.103038.patch
Type: text/x-patch
Size: 2317 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170619/8798740f/attachment.bin>


More information about the llvm-commits mailing list