work around constant folding bug 61144 in gcc 4.9.0 and 4.9.1
authorRich Felker <dalias@aerifal.cx>
Thu, 17 Jul 2014 01:32:06 +0000 (21:32 -0400)
committerRich Felker <dalias@aerifal.cx>
Mon, 28 Jul 2014 04:27:59 +0000 (00:27 -0400)
previously we detected this bug in configure and issued advice for a
workaround, but this turned out not to work. since then gcc 4.9.0 has
appeared in several distributions, and now 4.9.1 has been released
without a fix despite this being a wrong code generation bug which is
supposed to be a release-blocker, per gcc policy.

since the scope of the bug seems to affect only data objects (rather
than functions) whose definitions are overridable, and there are only
a very small number of these in musl, I am just changing them from
const to volatile for the time being. simply removing the const would
be sufficient to make gcc 4.9.1 work (the non-const case was
inadvertently fixed as part of another change in gcc), and this would
also be sufficient with 4.9.0 if we forced -O0 on the affected files
or on the whole build. however it's cleaner to just remove all the
broken compiler detection and use volatile, which will ensure that
they are never constant-folded. the quality of a non-broken compiler's
output should not be affected except for the fact that these objects
are no longer const and thus possibly add a few bytes to data/bss.

this change can be reconsidered and possibly reverted at some point in
the future when the broken gcc versions are no longer relevant.

(cherry picked from commit a6adb2bcd8145353943377d6119c1d7a4242bae1)

configure
src/stdio/__stdio_exit.c
src/stdio/fflush.c
src/stdio/stderr.c
src/stdio/stdin.c
src/stdio/stdout.c
src/thread/pthread_create.c
src/thread/pthread_key_create.c

index 03c193d90e7a2195b204ef0fc3f6775919607557..fa5613e616ec96a34f992b8e7459dac501b252c9 100755 (executable)
--- a/configure
+++ b/configure
@@ -495,27 +495,6 @@ printf "no\n"
 fail "$0: error: unsupported long double type"
 fi
 
-#
-# Check for known bug in GCC 4.9.0 that results in a broken libc.
-#
-if test "$cc_is_gcc" = yes ; then
-printf "checking for gcc constant folding bug with weak aliases... "
-echo 'static int x = 0;' > "$tmpc"
-echo 'extern int y __attribute__((__weak__, __alias__("x")));' >> "$tmpc"
-echo 'extern int should_appear;' >> "$tmpc"
-echo 'int foo() { return y ? should_appear : 0; }' >> "$tmpc"
-case "$($CC $CFLAGS_C99FSE -I./arch/$ARCH -I./include \
-  $CPPFLAGS $CFLAGS_AUTO $CFLAGS -S -o - "$tmpc" 2>/dev/null)" in
-*should_appear*)
-printf "no\n"
-;;
-*)
-printf "yes\n"
-fail "$0: error: broken compiler; try CFLAGS=-fno-toplevel-reorder"
-;;
-esac
-fi
-
 printf "creating config.mak... "
 
 cmdline=$(quote "$0")
index e4380aaf80709c04dd7335376ca2b257b8ac3d61..716e5f7366095455791db3616f6f63168298ba27 100644 (file)
@@ -1,6 +1,6 @@
 #include "stdio_impl.h"
 
-static FILE *const dummy_file = 0;
+static FILE *volatile dummy_file = 0;
 weak_alias(dummy_file, __stdin_used);
 weak_alias(dummy_file, __stdout_used);
 weak_alias(dummy_file, __stderr_used);
index af7095033bca73c25a5488ebc2769207e5483666..7bf862a6ae5f57dbd30a530a3902ddb9cda73dd8 100644 (file)
@@ -19,7 +19,7 @@ static int __fflush_unlocked(FILE *f)
 }
 
 /* stdout.c will override this if linked */
-static FILE *const dummy = 0;
+static FILE *volatile dummy = 0;
 weak_alias(dummy, __stdout_used);
 
 int fflush(FILE *f)
index 3fd8f81d76ec4ed4cef8680914b822be55067831..229c8651dd5bd14e31ada5bb542f0c62f19a55bf 100644 (file)
@@ -13,4 +13,4 @@ static FILE f = {
        .lock = -1,
 };
 FILE *const stderr = &f;
-FILE *const __stderr_used = &f;
+FILE *volatile __stderr_used = &f;
index 476dc7088f2ea46af4e95f0607f7b7eb394e3d8a..171ff22a9842dc74db9ccf1be15df17cfc2af2c3 100644 (file)
@@ -12,4 +12,4 @@ static FILE f = {
        .lock = -1,
 };
 FILE *const stdin = &f;
-FILE *const __stdin_used = &f;
+FILE *volatile __stdin_used = &f;
index 3855dd0be2f812906d2493ad0f47b46ee43e8233..6b188942dced8dec953b5f5a7d00a0c78f4a0f0b 100644 (file)
@@ -13,4 +13,4 @@ static FILE f = {
        .lock = -1,
 };
 FILE *const stdout = &f;
-FILE *const __stdout_used = &f;
+FILE *volatile __stdout_used = &f;
index f60cd4a364914d8020755f78304ccf89052f0ba8..02b966abcafac71ee71332d8b7b104592846b60a 100644 (file)
@@ -108,12 +108,12 @@ static int start(void *p)
 #define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
 
 /* pthread_key_create.c overrides this */
-static const size_t dummy = 0;
+static volatile size_t dummy = 0;
 weak_alias(dummy, __pthread_tsd_size);
-static void *const dummy_tsd[1] = { 0 };
+static void *dummy_tsd[1] = { 0 };
 weak_alias(dummy_tsd, __pthread_tsd_main);
 
-static FILE *const dummy_file = 0;
+static FILE *volatile dummy_file = 0;
 weak_alias(dummy_file, __stdin_used);
 weak_alias(dummy_file, __stdout_used);
 weak_alias(dummy_file, __stderr_used);
index c29935c1fb05616d86a722fc7d57c8e3a3099118..d534b1a24d24fb99612ddbcbb8d206449609b403 100644 (file)
@@ -1,6 +1,6 @@
 #include "pthread_impl.h"
 
-const size_t __pthread_tsd_size = sizeof(void *) * PTHREAD_KEYS_MAX;
+volatile size_t __pthread_tsd_size = sizeof(void *) * PTHREAD_KEYS_MAX;
 void *__pthread_tsd_main[PTHREAD_KEYS_MAX] = { 0 };
 
 static void (*keys[PTHREAD_KEYS_MAX])(void *);