git: 87841ead405f - stable/13 - kernel: Fix defining of .init_array and .fini_array sections

From: Zhenlei Huang <zlei_at_FreeBSD.org>
Date: Tue, 24 Sep 2024 04:15:59 UTC
The branch stable/13 has been updated by zlei:

URL: https://cgit.FreeBSD.org/src/commit/?id=87841ead405f7b380cbe18963d02c28ff0163ba2

commit 87841ead405f7b380cbe18963d02c28ff0163ba2
Author:     Zhenlei Huang <zlei@FreeBSD.org>
AuthorDate: 2024-09-02 04:26:47 +0000
Commit:     Zhenlei Huang <zlei@FreeBSD.org>
CommitDate: 2024-09-24 04:15:25 +0000

    kernel: Fix defining of .init_array and .fini_array sections
    
    These input sections can have decimal numbers as the priority suffix.
    Clang emits the '%u' form, while SORT is an alias for SORT_BY_NAME,
    hence will result in wrong order of constructors / destructors in
    output sections. Fix by using the correct sorting command
    SORT_BY_INIT_PRIORITY instead [1].
    
    The functions referenced by section .fini_array is in the normal order,
    but been executed in the reverse order. The order is same with
    .init_array section.
    
    Currently these sections are not used, there should be no functional
    change.
    
    Note: As for the .ctors and .dtors sections, both Clang and GCC emit
    the priority suffix in the form of '%05u', so there is no semantic
    difference between SORT_BY_NAME and SORT_BY_INIT_PRIORITY for those
    sections [2].
    
    This fix is extracted from a bigger patch [3] of hselasky, with
    additional fix for .fini_array section.
    
    1. https://sourceware.org/binutils/docs/ld/Input-Section-Wildcards.html
    2. https://reviews.llvm.org/D91187
    3. https://reviews.freebsd.org/D40467
    
    Reviewed by:    imp (previous version)
    Obtained from:  hselasky
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D45194
    
    (cherry picked from commit e15b5ba77d693609c9a452d1b0a1cdd5eb29350d)
    (cherry picked from commit 1d94490aee5c8353ed08909bbd00af7cfb48a8dc)
---
 sys/conf/ldscript.amd64 | 4 ++--
 sys/conf/ldscript.i386  | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/sys/conf/ldscript.amd64 b/sys/conf/ldscript.amd64
index 9c6e603a5753..72dc573f113e 100644
--- a/sys/conf/ldscript.amd64
+++ b/sys/conf/ldscript.amd64
@@ -93,15 +93,15 @@ SECTIONS
   .init_array     :
   {
      PROVIDE_HIDDEN (__init_array_start = .);
-     KEEP (*(SORT(.init_array.*)))
+     KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*)))
      KEEP (*(.init_array))
      PROVIDE_HIDDEN (__init_array_end = .);
   }
   .fini_array     :
   {
     PROVIDE_HIDDEN (__fini_array_start = .);
+    KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*)))
     KEEP (*(.fini_array))
-    KEEP (*(SORT(.fini_array.*)))
     PROVIDE_HIDDEN (__fini_array_end = .);
   }
   _start_ctors = .;
diff --git a/sys/conf/ldscript.i386 b/sys/conf/ldscript.i386
index 6563ed2cd623..c46a9910eb3b 100644
--- a/sys/conf/ldscript.i386
+++ b/sys/conf/ldscript.i386
@@ -87,15 +87,15 @@ SECTIONS
   .init_array     :
   {
      PROVIDE_HIDDEN (__init_array_start = .);
-     KEEP (*(SORT(.init_array.*)))
+     KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*)))
      KEEP (*(.init_array))
      PROVIDE_HIDDEN (__init_array_end = .);
   }
   .fini_array     :
   {
     PROVIDE_HIDDEN (__fini_array_start = .);
+    KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*)))
     KEEP (*(.fini_array))
-    KEEP (*(SORT(.fini_array.*)))
     PROVIDE_HIDDEN (__fini_array_end = .);
   }
   _start_ctors = .;