[PATCH] Segfault in INIT_G() due to insufficient 'Magic'?

Joe Krahn krahn at niehs.nih.gov
Wed Mar 5 17:58:33 UTC 2008


The 1.9.1 release exposes a bug that seems to affect a lot of gcc 3.x compilers. I get segfaults
with a large number of applets, due to a compiler bug in handling (*ptr_to_globals). I found this
recent problem report which demonstrates the compiler being at fault:
http://bugs.busybox.net/view.php?id=2344

I also found this report on OpenWRT indicating that busybox 1.7.2 probably exposes the same bug:
https://dev.openwrt.org/ticket/2627

I was just going to post this to help other users be aware of the problem, but I found this in
"include/libbb.h" which makes me wonder if it is not entirely the toolchain's fault:

/* '*const' ptr makes gcc optimize code much better.
 * Magic prevents ptr_to_globals from going into rodata.
 * If you want to assign a value, use PTR_TO_GLOBALS = xxx */
extern struct globals *const ptr_to_globals;
#define PTR_TO_GLOBALS (*(struct globals**)&ptr_to_globals)

Is it possible that GCC 4.x is a prerequisite for this 'Magic', and that it is not really
standard-conforming? I added the following patch which seems to fix the problem, although I have not
done very thorough testing yet. Maybe someone with more knowledge of GCC internals and
ptr_to_globals can do a better job. I am really just guessing.


--- busybox-1.9.1.orig/include/libbb.h  2008-02-12 11:10:25.000000000 -0500
+++ busybox-1.9.1/include/libbb.h       2008-03-05 12:40:57.000000000 -0500
@@ -1137,7 +1137,11 @@
 /* '*const' ptr makes gcc optimize code much better.
  * Magic prevents ptr_to_globals from going into rodata.
  * If you want to assign a value, use PTR_TO_GLOBALS = xxx */
+#if __GNUC_PREREQ (4,0)
 extern struct globals *const ptr_to_globals;
+#else
+extern struct globals *ptr_to_globals;
+#endif
 #define PTR_TO_GLOBALS (*(struct globals**)&ptr_to_globals)


--- busybox-1.9.1.orig/libbb/messages.c 2008-02-12 11:03:08.000000000 -0500
+++ busybox-1.9.1/libbb/messages.c      2008-03-05 12:43:01.000000000 -0500
@@ -74,4 +74,8 @@

 struct globals;
 /* Make it reside in R/W memory: */
+#if __GNUC_PREREQ (4,0)
 struct globals *const ptr_to_globals __attribute__ ((section (".data")));
+#else
+struct globals *ptr_to_globals __attribute__ ((section (".data")));
+#endif



More information about the busybox mailing list