[lld] 40a42f9 - [ELF] Make SORT_INIT_PRIORITY support .ctors.N

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 12 08:56:18 PST 2020


Author: Fangrui Song
Date: 2020-11-12T08:56:12-08:00
New Revision: 40a42f9f3fe9bf34f2d365705e72f12960cbb16c

URL: https://github.com/llvm/llvm-project/commit/40a42f9f3fe9bf34f2d365705e72f12960cbb16c
DIFF: https://github.com/llvm/llvm-project/commit/40a42f9f3fe9bf34f2d365705e72f12960cbb16c.diff

LOG: [ELF] Make SORT_INIT_PRIORITY support .ctors.N

Input sections `.ctors/.ctors.N` may go to either the output section `.init_array` or the output section `.ctors`:

* output `.ctors`: currently we sort them by name. This patch changes to sort by priority from high to low. If N in `.ctors.N` is in the form of %05u, there is no semantic difference. Actually GCC and Clang do use %05u. (In the test `ctors_dtors_priority.s` and Gold's test `gold/testsuite/script_test_14.s`, we can see %03u, but they are not really produced by compilers.)
* output `.init_array`: users can provide an input section description `SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*)` to mix `.init_array.*` and `.ctors.*`. This can make .init_array.N and .ctors.(65535-N) interchangeable.

With this change, users can mix `.ctors.N` and `.init_array.N` in `.init_array` (PR44698 and PR48096) with linker scripts. As an example:

```
SECTIONS {
  .init_array : {
    *(SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*))
    *(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)
  }
} INSERT AFTER .fini_array;
SECTIONS {
  .fini_array : {
    *(SORT_BY_INIT_PRIORITY(.fini_array.* .dtors.*))
    *(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)
  }
} INSERT BEFORE .init_array;
```

Reviewed By: psmith

Differential Revision: https://reviews.llvm.org/D91187

Added: 
    

Modified: 
    lld/ELF/OutputSections.cpp
    lld/test/ELF/linkerscript/sort-init.s

Removed: 
    


################################################################################
diff  --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp
index 518af1ee0548..5eea3bc8f6a5 100644
--- a/lld/ELF/OutputSections.cpp
+++ b/lld/ELF/OutputSections.cpp
@@ -447,19 +447,19 @@ static bool isCrtend(StringRef s) {
   return std::regex_match(s.begin(), s.end(), re);
 }
 
-// .ctors and .dtors are sorted by this priority from highest to lowest.
+// .ctors and .dtors are sorted by this order:
 //
-//  1. The section was contained in crtbegin (crtbegin contains
-//     some sentinel value in its .ctors and .dtors so that the runtime
-//     can find the beginning of the sections.)
+// 1. .ctors/.dtors in crtbegin (which contains a sentinel value -1).
+// 2. The section is named ".ctors" or ".dtors" (priority: 65536).
+// 3. The section has an optional priority value in the form of ".ctors.N" or
+//    ".dtors.N" where N is a number in the form of %05u (priority: 65535-N).
+// 4. .ctors/.dtors in crtend (which contains a sentinel value 0).
 //
-//  2. The section has an optional priority value in the form of ".ctors.N"
-//     or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
-//     they are compared as string rather than number.
-//
-//  3. The section is just ".ctors" or ".dtors".
-//
-//  4. The section was contained in crtend, which contains an end marker.
+// For 2 and 3, the sections are sorted by priority from high to low, e.g.
+// .ctors (65536), .ctors.00100 (65436), .ctors.00200 (65336).  In GNU ld's
+// internal linker scripts, the sorting is by string comparison which can
+// achieve the same goal given the optional priority values are of the same
+// length.
 //
 // In an ideal world, we don't need this function because .init_array and
 // .ctors are duplicate features (and .init_array is newer.) However, there
@@ -474,13 +474,7 @@ static bool compCtors(const InputSection *a, const InputSection *b) {
   bool endB = isCrtend(b->file->getName());
   if (endA != endB)
     return endB;
-  StringRef x = a->name;
-  StringRef y = b->name;
-  assert(x.startswith(".ctors") || x.startswith(".dtors"));
-  assert(y.startswith(".ctors") || y.startswith(".dtors"));
-  x = x.substr(6);
-  y = y.substr(6);
-  return x < y;
+  return getPriority(a->name) > getPriority(b->name);
 }
 
 // Sorts input sections by the special rules for .ctors and .dtors.
@@ -492,16 +486,17 @@ void OutputSection::sortCtorsDtors() {
   llvm::stable_sort(isd->sections, compCtors);
 }
 
-// If an input string is in the form of "foo.N" where N is a number,
-// return N. Otherwise, returns 65536, which is one greater than the
-// lowest priority.
+// If an input string is in the form of "foo.N" where N is a number, return N
+// (65535-N if .ctors.N or .dtors.N). Otherwise, returns 65536, which is one
+// greater than the lowest priority.
 int elf::getPriority(StringRef s) {
   size_t pos = s.rfind('.');
   if (pos == StringRef::npos)
     return 65536;
-  int v;
-  if (!to_integer(s.substr(pos + 1), v, 10))
-    return 65536;
+  int v = 65536;
+  if (to_integer(s.substr(pos + 1), v, 10) &&
+      (pos == 6 && (s.startswith(".ctors") || s.startswith(".dtors"))))
+    v = 65535 - v;
   return v;
 }
 

diff  --git a/lld/test/ELF/linkerscript/sort-init.s b/lld/test/ELF/linkerscript/sort-init.s
index dd030ace2efb..0a349fd9e57f 100644
--- a/lld/test/ELF/linkerscript/sort-init.s
+++ b/lld/test/ELF/linkerscript/sort-init.s
@@ -1,26 +1,43 @@
 # REQUIRES: x86
-# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o
-# RUN: echo "SECTIONS { .init_array : { *(SORT_BY_INIT_PRIORITY(.init_array.* foo*)) } }" > %t1.script
-# RUN: ld.lld --script %t1.script %t1.o -o %t2
-# RUN: llvm-objdump -s %t2 | FileCheck %s
+## Test SORT_BY_INIT_PRIORITY can be used to convert .ctors into .init_array
 
-# CHECK:      Contents of section .init_array:
-# CHECK-NEXT: 03020000 00060000 010405
+# RUN: split-file %s %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/asm -o %t.o
+# RUN: ld.lld -T %t/lds %t.o -o %t.out
+# RUN: llvm-readelf -x .init_array %t.out | FileCheck %s
 
+# CHECK:      Hex dump of section '.init_array':
+# CHECK-NEXT: 0x00000001 00010203 04050607
+
+#--- asm
 .globl _start
 _start:
   nop
 
 .section foo, "aw", @init_array
-  .byte 6
-.section .init_array, "aw", @init_array
-  .align 8
-  .byte 1
-.section .init_array.100, "aw", @init_array
-  .long 2
-.section .init_array.5, "aw", @init_array
+  .byte 5
+
+.section .ctors.65435, "a"
   .byte 3
-.section .init_array, "aw", @init_array
+.section .init_array.100, "aw", @init_array
   .byte 4
+
+.section .init_array.7, "aw", @init_array
+  .byte 2
+.section .ctors.65529,"a"
+  .byte 1
+.section .init_array.5, "aw", @init_array
+  .byte 0
+
 .section .init_array, "aw", @init_array
-  .byte 5
+  .byte 6
+.section .ctors, "a"
+  .byte 7
+
+#--- lds
+SECTIONS {
+  .init_array : {
+    *(SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*) SORT_BY_INIT_PRIORITY(foo*))
+    *(.init_array .ctors)
+  }
+}


        


More information about the llvm-commits mailing list