NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: lib/57756: Incorrect order of .fini_array indirect functions calling



Additionally from the https://docs.oracle.com/cd/E19683-01/817-1983/6mhm6r4es/index.html :

"If an object contains both .init and .init_array sections, the .init section is processed before the functions defined by the .init_array section for that object."

So we also have incorrect order of _init() and _initarray() inside crt0-common.c. _init() has to be called before _initarray(); glibc does the same

Updated patch:
diff -Naur lib/csu/common.orig/crt0-common.c lib/csu/common/crt0-common.c
--- a/lib/csu/common/crt0-common.c      2023-12-05 23:04:36.330759078 +0300
+++ b/lib/csu/common/crt0-common.c      2023-12-06 17:08:43.037463722 +0300
@@ -121,8 +121,9 @@
 static void
 _finiarray(void)
 {
-       for (const fptr_t *f = __fini_array_start; f < __fini_array_end; f++) {
-               (*f)();
+       size_t i = __fini_array_end - __fini_array_start;
+       while (i-- > 0) {
+               (*__fini_array_start[i])();
        }
 }
 
@@ -340,11 +341,13 @@
 #endif
 
        atexit(_finiarray);
+#ifndef HAVE_INITFINI_ARRAY
+       _init();
+#endif
        _initarray();
 
 #ifndef HAVE_INITFINI_ARRAY
        atexit(_fini);
-       _init();
 #endif
 
        exit(main(ps_strings->ps_nargvstr, ps_strings->ps_argvstr, environ));



From: Valery Ushakov <uwe%stderr.spb.ru@localhost>
Sent: Wednesday, December 6, 2023 4:35:01 PM
To: lib-bug-people%netbsd.org@localhost; gnats-admin%netbsd.org@localhost; netbsd-bugs%netbsd.org@localhost; Dmitry Chestnykh
Subject: Re: lib/57756: Incorrect order of .fini_array indirect functions calling
 
Caution: This is an external email. Be cautious while opening links or attachments.



The following reply was made to PR lib/57756; it has been noted by GNATS.

From: Valery Ushakov <uwe%stderr.spb.ru@localhost>
To: gnats-bugs%netbsd.org@localhost
Cc:
Subject: Re: lib/57756: Incorrect order of .fini_array indirect functions
 calling
Date: Wed, 6 Dec 2023 16:30:13 +0300

 To provide chapter and verse:

 https://docs.oracle.com/cd/E19683-01/817-1983/6mhm6r4es/index.html

 The runtime linker executes functions whose addresses are contained in
 the .fini_array section.  These functions are executed in the reverse
 order in which their addresses appear in the array.  The runtime
 linker executes a .fini section as an individual function.  If an
 object contains both .fini and .fini_array sections, the functions
 defined by the .fini_array section are processed before the .fini
 section for that object.



Home | Main Index | Thread Index | Old Index