Subject: ctor/dtor bug in crtbegin.c
To: None <tech-toolchain@netbsd.org>
From: Krister Walfridsson <cato@df.lth.se>
List: tech-toolchain
Date: 10/07/2001 17:09:25
  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.
  Send mail to mime@docserver.cac.washington.edu for more info.

---559023410-851401618-1002467365=:2317
Content-Type: TEXT/PLAIN; charset=US-ASCII

I noticed that we run the ctors/dtors in the wrong order for our elf ports.
You can see the problem by running the attached example (taken from the
gcc testsuite.)

I'd like to commit the patch below, but since this represent an abi change
(well, sort of) I thought it best someone else look at it first.

I'll commit this on Wednesday unless someone asks me not to.

   /Krister


Index: crtbegin.c
===================================================================
RCS file: /cvsroot/basesrc/lib/csu/common_elf/crtbegin.c,v
retrieving revision 1.13
diff -u -r1.13 crtbegin.c
--- crtbegin.c	2001/08/03 05:54:44	1.13
+++ crtbegin.c	2001/10/07 14:54:41
@@ -82,26 +82,26 @@
 static void
 __ctors()
 {
-	void (**p)(void) = __CTOR_LIST__ + 1;
-
-	while (*p)
-		(**p++)();
-}
-
-static void
-__dtors()
-{
-	unsigned long i = (unsigned long) __DTOR_LIST__[0];
+	unsigned long i = (unsigned long) __CTOR_LIST__[0];
 	void (**p)(void);

 	if (i == -1)  {
-		for (i = 1; __DTOR_LIST__[i] != NULL; i++)
+		for (i = 1; __CTOR_LIST__[i] != NULL; i++)
 			;
 		i--;
 	}
-	p = __DTOR_LIST__ + i;
+	p = __CTOR_LIST__ + i;
 	while (i--)
 		(**p--)();
+}
+
+static void
+__dtors()
+{
+	void (**p)(void) = __DTOR_LIST__ + 1;
+
+	while (*p)
+		(**p++)();
 }

 void

---559023410-851401618-1002467365=:2317
Content-Type: APPLICATION/octet-stream; name="bug.tgz"
Content-Transfer-Encoding: BASE64
Content-ID: <Pine.SOL.4.32.0110071709250.2317@igloo.df.lth.se>
Content-Description: 
Content-Disposition: attachment; filename="bug.tgz"

H4sIAL1uwDsAA+3UzXKCMBAAYM55iu30Alo1RMAZrb30QRh+omaGEieElo7y
7g0ote209lL00P0OYJLNjyxLXK4nVs/AozPfBwsAGKXtHaC7HxswmwaUBjNm
Ak3DxFvg932wRlnoSAFYSaTlubiXDefZJQ50WbHJfyLzrRqx8WNPe7gmsZ73
c/69wO/y73uMAbiu55r8057O88k/z/9kADtI16NUgipzqGEwIeRW5ElWphzu
C51mIh5vHghJsqgoYCVlqGFHmryJXEO1aH+ah6hF0vYkssz1gmzLOBPJvB1t
J9nPUqSO2a1aDoeHIKjf11nzU4DiulS5WdqM1wtCmvF2ifn8uDjhleYm5HAa
c3Xvmis7Bj9FIu9WI4ctVmCD3QSOm50cuFkCc2C/bzvZqdN1wCHdixnFUpn+
w1/kldA2NY2aXDtpf+hD/Ud9fQB+q/9p4H6tf993sf4v4dv6v16pnwoawjDS
Wom41DwMbVvkQodbJaQS+tU27wp1HFOM7xPY+Ql+N+HaDxwhhBBCCCGEEEII
IYQQQgghhBDqwRvYLe3WACgAAA==
---559023410-851401618-1002467365=:2317--