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
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.
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;
}
# 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)
+ }
+}