[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
			
			
This commit is contained in:
		
							parent
							
								
									73d01a80ce
								
							
						
					
					
						commit
						40a42f9f3f
					
				| 
						 | 
					@ -447,19 +447,19 @@ static bool isCrtend(StringRef s) {
 | 
				
			||||||
  return std::regex_match(s.begin(), s.end(), re);
 | 
					  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
 | 
					// 1. .ctors/.dtors in crtbegin (which contains a sentinel value -1).
 | 
				
			||||||
//     some sentinel value in its .ctors and .dtors so that the runtime
 | 
					// 2. The section is named ".ctors" or ".dtors" (priority: 65536).
 | 
				
			||||||
//     can find the beginning of the sections.)
 | 
					// 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"
 | 
					// For 2 and 3, the sections are sorted by priority from high to low, e.g.
 | 
				
			||||||
//     or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
 | 
					// .ctors (65536), .ctors.00100 (65436), .ctors.00200 (65336).  In GNU ld's
 | 
				
			||||||
//     they are compared as string rather than number.
 | 
					// internal linker scripts, the sorting is by string comparison which can
 | 
				
			||||||
//
 | 
					// achieve the same goal given the optional priority values are of the same
 | 
				
			||||||
//  3. The section is just ".ctors" or ".dtors".
 | 
					// length.
 | 
				
			||||||
//
 | 
					 | 
				
			||||||
//  4. The section was contained in crtend, which contains an end marker.
 | 
					 | 
				
			||||||
//
 | 
					//
 | 
				
			||||||
// In an ideal world, we don't need this function because .init_array and
 | 
					// 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
 | 
					// .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());
 | 
					  bool endB = isCrtend(b->file->getName());
 | 
				
			||||||
  if (endA != endB)
 | 
					  if (endA != endB)
 | 
				
			||||||
    return endB;
 | 
					    return endB;
 | 
				
			||||||
  StringRef x = a->name;
 | 
					  return getPriority(a->name) > getPriority(b->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;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Sorts input sections by the special rules for .ctors and .dtors.
 | 
					// Sorts input sections by the special rules for .ctors and .dtors.
 | 
				
			||||||
| 
						 | 
					@ -492,16 +486,17 @@ void OutputSection::sortCtorsDtors() {
 | 
				
			||||||
  llvm::stable_sort(isd->sections, compCtors);
 | 
					  llvm::stable_sort(isd->sections, compCtors);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// If an input string is in the form of "foo.N" where N is a number,
 | 
					// If an input string is in the form of "foo.N" where N is a number, return N
 | 
				
			||||||
// return N. Otherwise, returns 65536, which is one greater than the
 | 
					// (65535-N if .ctors.N or .dtors.N). Otherwise, returns 65536, which is one
 | 
				
			||||||
// lowest priority.
 | 
					// greater than the lowest priority.
 | 
				
			||||||
int elf::getPriority(StringRef s) {
 | 
					int elf::getPriority(StringRef s) {
 | 
				
			||||||
  size_t pos = s.rfind('.');
 | 
					  size_t pos = s.rfind('.');
 | 
				
			||||||
  if (pos == StringRef::npos)
 | 
					  if (pos == StringRef::npos)
 | 
				
			||||||
    return 65536;
 | 
					    return 65536;
 | 
				
			||||||
  int v;
 | 
					  int v = 65536;
 | 
				
			||||||
  if (!to_integer(s.substr(pos + 1), v, 10))
 | 
					  if (to_integer(s.substr(pos + 1), v, 10) &&
 | 
				
			||||||
    return 65536;
 | 
					      (pos == 6 && (s.startswith(".ctors") || s.startswith(".dtors"))))
 | 
				
			||||||
 | 
					    v = 65535 - v;
 | 
				
			||||||
  return v;
 | 
					  return v;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,26 +1,43 @@
 | 
				
			||||||
# REQUIRES: x86
 | 
					# REQUIRES: x86
 | 
				
			||||||
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o
 | 
					## Test SORT_BY_INIT_PRIORITY can be used to convert .ctors into .init_array
 | 
				
			||||||
# 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
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
# CHECK:      Contents of section .init_array:
 | 
					# RUN: split-file %s %t
 | 
				
			||||||
# CHECK-NEXT: 03020000 00060000 010405
 | 
					# 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
 | 
					.globl _start
 | 
				
			||||||
_start:
 | 
					_start:
 | 
				
			||||||
  nop
 | 
					  nop
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.section foo, "aw", @init_array
 | 
					.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 3
 | 
					 | 
				
			||||||
.section .init_array, "aw", @init_array
 | 
					 | 
				
			||||||
  .byte 4
 | 
					 | 
				
			||||||
.section .init_array, "aw", @init_array
 | 
					 | 
				
			||||||
  .byte 5
 | 
					  .byte 5
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.section .ctors.65435, "a"
 | 
				
			||||||
 | 
					  .byte 3
 | 
				
			||||||
 | 
					.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 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)
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue