Blame SOURCES/gcc32-weakref.patch

4ac4fd
2005-11-16  Alexandre Oliva  <aoliva@redhat.com>
4ac4fd
4ac4fd
	* attribs.c (handle_weakref_attribute): New.
4ac4fd
	(c_common_attribute_table): Add weakref.
4ac4fd
	* defaults.h (ASM_OUTPUT_WEAKREF): Define.
4ac4fd
	* doc/extend.texi: Document weakref attribute.
4ac4fd
	* varasm.c (do_assemble_alias): Handle weakrefs.
4ac4fd
	(finish_aliases_1): Do not reject weakrefs to external symbols.
4ac4fd
	* gthr-posix.h: Define __gthrw.  For all identifiers that
4ac4fd
	might be weak, introduce weakrefs or non-weak aliases with
4ac4fd
	__gthrw, and prefix all uses with __ghtrw.
4ac4fd
	* config/rs6000/rs6000.h (ASM_OUTPUT_WEAKREF): Define.
4ac4fd
4ac4fd
	* g++.old-deja/g++.ext/weakref1.C: New test.
4ac4fd
	* g++.old-deja/g++.ext/weakref1a.cc: New helper file.
4ac4fd
4ac4fd
--- gcc/defaults.h.orig	2005-11-15 19:55:42.000000000 -0200
4ac4fd
+++ gcc/defaults.h	2005-11-16 13:38:28.000000000 -0200
4ac4fd
@@ -156,6 +156,25 @@
4ac4fd
 #endif
4ac4fd
 #endif
4ac4fd
 
4ac4fd
+/* This is how we tell the assembler that a symbol is a weak alias to
4ac4fd
+   another symbol that doesn't require the other symbol to be defined.
4ac4fd
+   Uses of the former will turn into weak uses of the latter, i.e.,
4ac4fd
+   uses that, in case the latter is undefined, will not cause errors,
4ac4fd
+   and will add it to the symbol table as weak undefined.  However, if
4ac4fd
+   the latter is referenced directly, a strong reference prevails.  */
4ac4fd
+#ifndef ASM_OUTPUT_WEAKREF
4ac4fd
+#define ASM_OUTPUT_WEAKREF(FILE, DECL, NAME, VALUE)			\
4ac4fd
+  do									\
4ac4fd
+    {									\
4ac4fd
+      fprintf ((FILE), "\t.weakref\t");					\
4ac4fd
+      assemble_name ((FILE), (NAME));					\
4ac4fd
+      fprintf ((FILE), ",");						\
4ac4fd
+      assemble_name ((FILE), (VALUE));					\
4ac4fd
+      fprintf ((FILE), "\n");						\
4ac4fd
+    }									\
4ac4fd
+  while (0)
4ac4fd
+#endif
4ac4fd
+
4ac4fd
 /* This determines whether or not we support weak symbols.  */
4ac4fd
 #ifndef SUPPORTS_WEAK
4ac4fd
 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
4ac4fd
--- gcc/doc/extend.texi.orig	2005-11-15 19:55:42.000000000 -0200
4ac4fd
+++ gcc/doc/extend.texi	2005-11-15 19:55:48.000000000 -0200
4ac4fd
@@ -2217,6 +2217,38 @@
4ac4fd
 for ELF targets, and also for a.out targets when using the GNU assembler
4ac4fd
 and linker.
4ac4fd
 
4ac4fd
+@item weakref
4ac4fd
+@itemx weakref ("@var{target}")
4ac4fd
+@cindex @code{weakref} attribute
4ac4fd
+The @code{weakref} attribute marks a declaration as a weak reference.
4ac4fd
+Without arguments, it should be accompanied by an @code{alias} attribute
4ac4fd
+naming the target symbol.  Optionally, the @var{target} may be given as
4ac4fd
+an argument to @code{weakref} itself.  In either case, @code{weakref}
4ac4fd
+implicitly marks the declaration as @code{weak}.  Without a
4ac4fd
+@var{target}, given as an argument to @code{weakref} or to @code{alias},
4ac4fd
+@code{weakref} is equivalent to @code{weak}.
4ac4fd
+
4ac4fd
+@smallexample
4ac4fd
+extern int x() __attribute__ ((weakref ("y")));
4ac4fd
+/* is equivalent to... */
4ac4fd
+extern int x() __attribute__ ((weak, weakref, alias ("y")));
4ac4fd
+/* and to... */
4ac4fd
+extern int x() __attribute__ ((weakref));
4ac4fd
+extern int x() __attribute__ ((alias ("y")));
4ac4fd
+@end smallexample
4ac4fd
+
4ac4fd
+A weak reference is an alias that does not by itself require a
4ac4fd
+definition to be given for the target symbol.  If the target symbol is
4ac4fd
+only referenced through weak references, then the becomes a @code{weak}
4ac4fd
+undefined symbol.  If it is directly referenced, however, then such
4ac4fd
+strong references prevail, and a definition will be required for the
4ac4fd
+symbol, not necessarily in the same translation unit.
4ac4fd
+
4ac4fd
+The effect is equivalent to moving all references to the alias to a
4ac4fd
+separate translation unit, renaming the alias to the aliased symbol,
4ac4fd
+declaring it as weak, compiling the two separate translation units and
4ac4fd
+performing a reloadable link on them.
4ac4fd
+
4ac4fd
 @item malloc
4ac4fd
 @cindex @code{malloc} attribute
4ac4fd
 The @code{malloc} attribute is used to tell the compiler that a function
4ac4fd
--- gcc/gthr-posix.h.orig	2005-11-15 19:55:42.000000000 -0200
4ac4fd
+++ gcc/gthr-posix.h	2005-11-15 19:55:48.000000000 -0200
4ac4fd
@@ -43,6 +43,42 @@
4ac4fd
 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
4ac4fd
 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
4ac4fd
 
4ac4fd
+#if SUPPORTS_WEAK && GTHREAD_USE_WEAK && defined __GNUC_RH_RELEASE__ && __GNUC__ == 3 && __GNUC_MINOR__ == 2 && __GNUC_PATCHLEVEL__ == 3 && __GNUC_RH_RELEASE__ > 53 && !defined __attribute__
4ac4fd
+# define __gthrw(name) \
4ac4fd
+  extern __typeof(name) __gthrw_ ## name __attribute__ ((__weakref__(#name)))
4ac4fd
+
4ac4fd
+__gthrw(pthread_once);
4ac4fd
+__gthrw(pthread_key_create);
4ac4fd
+__gthrw(pthread_key_delete);
4ac4fd
+__gthrw(pthread_getspecific);
4ac4fd
+__gthrw(pthread_setspecific);
4ac4fd
+__gthrw(pthread_create);
4ac4fd
+
4ac4fd
+__gthrw(pthread_mutex_lock );
4ac4fd
+__gthrw(pthread_mutex_trylock );
4ac4fd
+__gthrw(pthread_mutex_unlock );
4ac4fd
+
4ac4fd
+#ifdef _LIBOBJC
4ac4fd
+/* Objective C.  */
4ac4fd
+__gthrw(pthread_cond_broadcast);
4ac4fd
+__gthrw(pthread_cond_destroy);
4ac4fd
+__gthrw(pthread_cond_init);
4ac4fd
+__gthrw(pthread_cond_signal);
4ac4fd
+__gthrw(pthread_cond_wait);
4ac4fd
+__gthrw(pthread_exit);
4ac4fd
+__gthrw(pthread_mutex_init);
4ac4fd
+__gthrw(pthread_mutex_destroy);
4ac4fd
+__gthrw(pthread_self);
4ac4fd
+__gthrw(sched_get_priority_max);
4ac4fd
+__gthrw(sched_get_priority_min);
4ac4fd
+__gthrw(sched_yield);
4ac4fd
+__gthrw(pthread_attr_destroy);
4ac4fd
+__gthrw(pthread_attr_init);
4ac4fd
+__gthrw(pthread_attr_setdetachstate);
4ac4fd
+__gthrw(pthread_getschedparam);
4ac4fd
+__gthrw(pthread_setschedparam);
4ac4fd
+#endif
4ac4fd
+#else
4ac4fd
 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
4ac4fd
 
4ac4fd
 #pragma weak pthread_once
4ac4fd
@@ -76,11 +112,47 @@
4ac4fd
 #pragma weak pthread_getschedparam
4ac4fd
 #pragma weak pthread_setschedparam
4ac4fd
 #endif
4ac4fd
+#endif
4ac4fd
+
4ac4fd
+#define __gthrw_pthread_once pthread_once
4ac4fd
+#define __gthrw_pthread_key_create pthread_key_create
4ac4fd
+#define __gthrw_pthread_key_delete pthread_key_delete
4ac4fd
+#define __gthrw_pthread_getspecific pthread_getspecific
4ac4fd
+#define __gthrw_pthread_setspecific pthread_setspecific
4ac4fd
+#define __gthrw_pthread_create pthread_create
4ac4fd
+
4ac4fd
+#define __gthrw_pthread_mutex_lock  pthread_mutex_lock
4ac4fd
+#define __gthrw_pthread_mutex_trylock  pthread_mutex_trylock
4ac4fd
+#define __gthrw_pthread_mutex_unlock  pthread_mutex_unlock
4ac4fd
+
4ac4fd
+#ifdef _LIBOBJC
4ac4fd
+/* Objective C.  */
4ac4fd
+#define __gthrw_pthread_cond_broadcast pthread_cond_broadcast
4ac4fd
+#define __gthrw_pthread_cond_destroy pthread_cond_destroy
4ac4fd
+#define __gthrw_pthread_cond_init pthread_cond_init
4ac4fd
+#define __gthrw_pthread_cond_signal pthread_cond_signal
4ac4fd
+#define __gthrw_pthread_cond_wait pthread_cond_wait
4ac4fd
+#define __gthrw_pthread_exit pthread_exit
4ac4fd
+#define __gthrw_pthread_mutex_init pthread_mutex_init
4ac4fd
+#define __gthrw_pthread_mutex_destroy pthread_mutex_destroy
4ac4fd
+#define __gthrw_pthread_self pthread_self
4ac4fd
+#define __gthrw_sched_get_priority_max sched_get_priority_max
4ac4fd
+#define __gthrw_sched_get_priority_min sched_get_priority_min
4ac4fd
+#define __gthrw_sched_yield sched_yield
4ac4fd
+#define __gthrw_pthread_attr_destroy pthread_attr_destroy
4ac4fd
+#define __gthrw_pthread_attr_init pthread_attr_init
4ac4fd
+#define __gthrw_pthread_attr_setdetachstate pthread_attr_setdetachstate
4ac4fd
+#define __gthrw_pthread_getschedparam pthread_getschedparam
4ac4fd
+#define __gthrw_pthread_setschedparam pthread_setschedparam
4ac4fd
+#endif
4ac4fd
+#endif
4ac4fd
+
4ac4fd
+#if SUPPORTS_WEAK && GTHREAD_USE_WEAK
4ac4fd
 
4ac4fd
 static inline int
4ac4fd
 __gthread_active_p (void)
4ac4fd
 {
4ac4fd
-  static void *const __gthread_active_ptr = (void *) &pthread_create;
4ac4fd
+  static void *const __gthread_active_ptr = (void *) &__gthrw_pthread_create;
4ac4fd
   return __gthread_active_ptr != 0;
4ac4fd
 }
4ac4fd
 
4ac4fd
@@ -119,13 +191,13 @@
4ac4fd
   if (__gthread_active_p ())
4ac4fd
     {
4ac4fd
       /* Initialize the thread storage key */
4ac4fd
-      if (pthread_key_create(&_objc_thread_storage, NULL) == 0)
4ac4fd
+      if (__gthrw_pthread_key_create(&_objc_thread_storage, NULL) == 0)
4ac4fd
         {
4ac4fd
           /* The normal default detach state for threads is
4ac4fd
            * PTHREAD_CREATE_JOINABLE which causes threads to not die
4ac4fd
            * when you think they should.  */
4ac4fd
-          if (pthread_attr_init(&_objc_thread_attribs) == 0
4ac4fd
-              && pthread_attr_setdetachstate(&_objc_thread_attribs, 
4ac4fd
+          if (__gthrw_pthread_attr_init(&_objc_thread_attribs) == 0
4ac4fd
+              && __gthrw_pthread_attr_setdetachstate(&_objc_thread_attribs,
4ac4fd
                                              PTHREAD_CREATE_DETACHED) == 0)
4ac4fd
             return 0;
4ac4fd
         }
4ac4fd
@@ -139,8 +211,8 @@
4ac4fd
 __gthread_objc_close_thread_system(void)
4ac4fd
 {
4ac4fd
   if (__gthread_active_p ()
4ac4fd
-      && pthread_key_delete(_objc_thread_storage) == 0
4ac4fd
-      && pthread_attr_destroy(&_objc_thread_attribs) == 0)
4ac4fd
+      && __gthrw_pthread_key_delete(_objc_thread_storage) == 0
4ac4fd
+      && __gthrw_pthread_attr_destroy(&_objc_thread_attribs) == 0)
4ac4fd
     return 0;
4ac4fd
 
4ac4fd
   return -1;
4ac4fd
@@ -158,7 +230,7 @@
4ac4fd
   if (!__gthread_active_p ())
4ac4fd
     return NULL;
4ac4fd
  
4ac4fd
-  if ( !(pthread_create(&new_thread_handle, NULL, (void *)func, arg)) )
4ac4fd
+  if ( !(__gthrw_pthread_create(&new_thread_handle, NULL, (void *)func, arg)) )
4ac4fd
     thread_id = (objc_thread_t) new_thread_handle;
4ac4fd
   else
4ac4fd
     thread_id = NULL;
4ac4fd
@@ -173,17 +245,17 @@
4ac4fd
   if (!__gthread_active_p())
4ac4fd
     return -1;
4ac4fd
   else {
4ac4fd
-    pthread_t thread_id = pthread_self();
4ac4fd
+    pthread_t thread_id = __gthrw_pthread_self();
4ac4fd
     int policy;
4ac4fd
     struct sched_param params;
4ac4fd
     int priority_min, priority_max;
4ac4fd
 
4ac4fd
-    if (pthread_getschedparam(thread_id, &policy, &params) == 0)
4ac4fd
+    if (__gthrw_pthread_getschedparam(thread_id, &policy, &params) == 0)
4ac4fd
       {
4ac4fd
-        if ((priority_max = sched_get_priority_max(policy)) != 0)
4ac4fd
+        if ((priority_max = __gthrw_sched_get_priority_max(policy)) != 0)
4ac4fd
           return -1;
4ac4fd
 
4ac4fd
-        if ((priority_min = sched_get_priority_min(policy)) != 0)
4ac4fd
+        if ((priority_min = __gthrw_sched_get_priority_min(policy)) != 0)
4ac4fd
           return -1;
4ac4fd
 
4ac4fd
         if (priority > priority_max)
4ac4fd
@@ -197,7 +269,7 @@
4ac4fd
          * this should be a pointer to policy but pthread.h is universally
4ac4fd
          * at odds with this.
4ac4fd
          */
4ac4fd
-        if (pthread_setschedparam(thread_id, policy, &params) == 0)
4ac4fd
+        if (__gthrw_pthread_setschedparam(thread_id, policy, &params) == 0)
4ac4fd
           return 0;
4ac4fd
       }
4ac4fd
     return -1;
4ac4fd
@@ -213,7 +285,7 @@
4ac4fd
       int policy;
4ac4fd
       struct sched_param params;
4ac4fd
 
4ac4fd
-      if (pthread_getschedparam(pthread_self(), &policy, &params) == 0)
4ac4fd
+      if (__gthrw_pthread_getschedparam(__gthrw_pthread_self(), &policy, &params) == 0)
4ac4fd
         return params.sched_priority;
4ac4fd
       else
4ac4fd
         return -1;
4ac4fd
@@ -227,7 +299,7 @@
4ac4fd
 __gthread_objc_thread_yield(void)
4ac4fd
 {
4ac4fd
   if (__gthread_active_p ())
4ac4fd
-    sched_yield();
4ac4fd
+    __gthrw_sched_yield();
4ac4fd
 }
4ac4fd
 
4ac4fd
 /* Terminate the current thread.  */
4ac4fd
@@ -236,7 +308,7 @@
4ac4fd
 {
4ac4fd
   if (__gthread_active_p ())
4ac4fd
     /* exit the thread */
4ac4fd
-    pthread_exit(&__objc_thread_exit_status);
4ac4fd
+    __gthrw_pthread_exit(&__objc_thread_exit_status);
4ac4fd
 
4ac4fd
   /* Failed if we reached here */
4ac4fd
   return -1;
4ac4fd
@@ -247,7 +319,7 @@
4ac4fd
 __gthread_objc_thread_id(void)
4ac4fd
 {
4ac4fd
   if (__gthread_active_p ())
4ac4fd
-    return (objc_thread_t) pthread_self();
4ac4fd
+    return (objc_thread_t) __gthrw_pthread_self();
4ac4fd
   else
4ac4fd
     return (objc_thread_t) 1;
4ac4fd
 }
4ac4fd
@@ -257,7 +329,7 @@
4ac4fd
 __gthread_objc_thread_set_data(void *value)
4ac4fd
 {
4ac4fd
   if (__gthread_active_p ())
4ac4fd
-    return pthread_setspecific(_objc_thread_storage, value);
4ac4fd
+    return __gthrw_pthread_setspecific(_objc_thread_storage, value);
4ac4fd
   else
4ac4fd
     {
4ac4fd
       thread_local_storage = value;
4ac4fd
@@ -270,7 +342,7 @@
4ac4fd
 __gthread_objc_thread_get_data(void)
4ac4fd
 {
4ac4fd
   if (__gthread_active_p ())
4ac4fd
-    return pthread_getspecific(_objc_thread_storage);
4ac4fd
+    return __gthrw_pthread_getspecific(_objc_thread_storage);
4ac4fd
   else
4ac4fd
     return thread_local_storage;
4ac4fd
 }
4ac4fd
@@ -285,7 +357,7 @@
4ac4fd
     {
4ac4fd
       mutex->backend = objc_malloc(sizeof(pthread_mutex_t));
4ac4fd
 
4ac4fd
-      if (pthread_mutex_init((pthread_mutex_t *)mutex->backend, NULL))
4ac4fd
+      if (__gthrw_pthread_mutex_init((pthread_mutex_t *)mutex->backend, NULL))
4ac4fd
 	{
4ac4fd
 	  objc_free(mutex->backend);
4ac4fd
 	  mutex->backend = NULL;
4ac4fd
@@ -306,18 +378,18 @@
4ac4fd
 
4ac4fd
       /*
4ac4fd
        * Posix Threads specifically require that the thread be unlocked
4ac4fd
-       * for pthread_mutex_destroy to work.
4ac4fd
+       * for __gthrw_pthread_mutex_destroy to work.
4ac4fd
        */
4ac4fd
 
4ac4fd
       do
4ac4fd
 	{
4ac4fd
-	  count = pthread_mutex_unlock((pthread_mutex_t *)mutex->backend);
4ac4fd
+	  count = __gthrw_pthread_mutex_unlock((pthread_mutex_t *)mutex->backend);
4ac4fd
 	  if (count < 0)
4ac4fd
 	    return -1;
4ac4fd
 	}
4ac4fd
       while (count);
4ac4fd
 
4ac4fd
-      if (pthread_mutex_destroy((pthread_mutex_t *)mutex->backend))
4ac4fd
+      if (__gthrw_pthread_mutex_destroy((pthread_mutex_t *)mutex->backend))
4ac4fd
 	return -1;
4ac4fd
 
4ac4fd
       objc_free(mutex->backend);
4ac4fd
@@ -331,7 +403,7 @@
4ac4fd
 __gthread_objc_mutex_lock(objc_mutex_t mutex)
4ac4fd
 {
4ac4fd
   if (__gthread_active_p () 
4ac4fd
-      && pthread_mutex_lock((pthread_mutex_t *)mutex->backend) != 0)
4ac4fd
+      && __gthrw_pthread_mutex_lock((pthread_mutex_t *)mutex->backend) != 0)
4ac4fd
     {
4ac4fd
       return -1;
4ac4fd
     }
4ac4fd
@@ -344,7 +416,7 @@
4ac4fd
 __gthread_objc_mutex_trylock(objc_mutex_t mutex)
4ac4fd
 {
4ac4fd
   if (__gthread_active_p () 
4ac4fd
-      && pthread_mutex_trylock((pthread_mutex_t *)mutex->backend) != 0)
4ac4fd
+      && __gthrw_pthread_mutex_trylock((pthread_mutex_t *)mutex->backend) != 0)
4ac4fd
     {
4ac4fd
       return -1;
4ac4fd
     }
4ac4fd
@@ -357,7 +429,7 @@
4ac4fd
 __gthread_objc_mutex_unlock(objc_mutex_t mutex)
4ac4fd
 {
4ac4fd
   if (__gthread_active_p () 
4ac4fd
-      && pthread_mutex_unlock((pthread_mutex_t *)mutex->backend) != 0)
4ac4fd
+      && __gthrw_pthread_mutex_unlock((pthread_mutex_t *)mutex->backend) != 0)
4ac4fd
     {
4ac4fd
       return -1;
4ac4fd
     }
4ac4fd
@@ -375,7 +447,7 @@
4ac4fd
     {
4ac4fd
       condition->backend = objc_malloc(sizeof(pthread_cond_t));
4ac4fd
 
4ac4fd
-      if (pthread_cond_init((pthread_cond_t *)condition->backend, NULL))
4ac4fd
+      if (__gthrw_pthread_cond_init((pthread_cond_t *)condition->backend, NULL))
4ac4fd
 	{
4ac4fd
 	  objc_free(condition->backend);
4ac4fd
 	  condition->backend = NULL;
4ac4fd
@@ -392,7 +464,7 @@
4ac4fd
 {
4ac4fd
   if (__gthread_active_p ())
4ac4fd
     {
4ac4fd
-      if (pthread_cond_destroy((pthread_cond_t *)condition->backend))
4ac4fd
+      if (__gthrw_pthread_cond_destroy((pthread_cond_t *)condition->backend))
4ac4fd
 	return -1;
4ac4fd
 
4ac4fd
       objc_free(condition->backend);
4ac4fd
@@ -406,7 +478,7 @@
4ac4fd
 __gthread_objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
4ac4fd
 {
4ac4fd
   if (__gthread_active_p ())
4ac4fd
-    return pthread_cond_wait((pthread_cond_t *)condition->backend,
4ac4fd
+    return __gthrw_pthread_cond_wait((pthread_cond_t *)condition->backend,
4ac4fd
 			   (pthread_mutex_t *)mutex->backend);
4ac4fd
   else
4ac4fd
     return 0;
4ac4fd
@@ -417,7 +489,7 @@
4ac4fd
 __gthread_objc_condition_broadcast(objc_condition_t condition)
4ac4fd
 {
4ac4fd
   if (__gthread_active_p ())
4ac4fd
-    return pthread_cond_broadcast((pthread_cond_t *)condition->backend);
4ac4fd
+    return __gthrw_pthread_cond_broadcast((pthread_cond_t *)condition->backend);
4ac4fd
   else
4ac4fd
     return 0;
4ac4fd
 }
4ac4fd
@@ -427,7 +499,7 @@
4ac4fd
 __gthread_objc_condition_signal(objc_condition_t condition)
4ac4fd
 {
4ac4fd
   if (__gthread_active_p ())
4ac4fd
-    return pthread_cond_signal((pthread_cond_t *)condition->backend);
4ac4fd
+    return __gthrw_pthread_cond_signal((pthread_cond_t *)condition->backend);
4ac4fd
   else
4ac4fd
     return 0;
4ac4fd
 }
4ac4fd
@@ -438,7 +510,7 @@
4ac4fd
 __gthread_once (__gthread_once_t *once, void (*func) (void))
4ac4fd
 {
4ac4fd
   if (__gthread_active_p ())
4ac4fd
-    return pthread_once (once, func);
4ac4fd
+    return __gthrw_pthread_once (once, func);
4ac4fd
   else
4ac4fd
     return -1;
4ac4fd
 }
4ac4fd
@@ -446,7 +518,7 @@
4ac4fd
 static inline int
4ac4fd
 __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
4ac4fd
 {
4ac4fd
-  return pthread_key_create (key, dtor);
4ac4fd
+  return __gthrw_pthread_key_create (key, dtor);
4ac4fd
 }
4ac4fd
 
4ac4fd
 static inline int
4ac4fd
@@ -454,7 +526,7 @@
4ac4fd
 {
4ac4fd
   /* Just reset the key value to zero.  */
4ac4fd
   if (ptr)
4ac4fd
-    return pthread_setspecific (key, 0);
4ac4fd
+    return __gthrw_pthread_setspecific (key, 0);
4ac4fd
   else
4ac4fd
     return 0;
4ac4fd
 }
4ac4fd
@@ -462,26 +534,26 @@
4ac4fd
 static inline int
4ac4fd
 __gthread_key_delete (__gthread_key_t key)
4ac4fd
 {
4ac4fd
-  return pthread_key_delete (key);
4ac4fd
+  return __gthrw_pthread_key_delete (key);
4ac4fd
 }
4ac4fd
 
4ac4fd
 static inline void *
4ac4fd
 __gthread_getspecific (__gthread_key_t key)
4ac4fd
 {
4ac4fd
-  return pthread_getspecific (key);
4ac4fd
+  return __gthrw_pthread_getspecific (key);
4ac4fd
 }
4ac4fd
 
4ac4fd
 static inline int
4ac4fd
 __gthread_setspecific (__gthread_key_t key, const void *ptr)
4ac4fd
 {
4ac4fd
-  return pthread_setspecific (key, ptr);
4ac4fd
+  return __gthrw_pthread_setspecific (key, ptr);
4ac4fd
 }
4ac4fd
 
4ac4fd
 static inline int
4ac4fd
 __gthread_mutex_lock (__gthread_mutex_t *mutex)
4ac4fd
 {
4ac4fd
   if (__gthread_active_p ())
4ac4fd
-    return pthread_mutex_lock (mutex);
4ac4fd
+    return __gthrw_pthread_mutex_lock (mutex);
4ac4fd
   else
4ac4fd
     return 0;
4ac4fd
 }
4ac4fd
@@ -490,7 +562,7 @@
4ac4fd
 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
4ac4fd
 {
4ac4fd
   if (__gthread_active_p ())
4ac4fd
-    return pthread_mutex_trylock (mutex);
4ac4fd
+    return __gthrw_pthread_mutex_trylock (mutex);
4ac4fd
   else
4ac4fd
     return 0;
4ac4fd
 }
4ac4fd
@@ -499,7 +571,7 @@
4ac4fd
 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
4ac4fd
 {
4ac4fd
   if (__gthread_active_p ())
4ac4fd
-    return pthread_mutex_unlock (mutex);
4ac4fd
+    return __gthrw_pthread_mutex_unlock (mutex);
4ac4fd
   else
4ac4fd
     return 0;
4ac4fd
 }
4ac4fd
--- gcc/varasm.c.orig	2005-11-15 19:55:42.000000000 -0200
4ac4fd
+++ gcc/varasm.c	2005-11-16 13:38:44.000000000 -0200
4ac4fd
@@ -5130,6 +5130,9 @@
4ac4fd
       if (! TREE_USED (decl))
4ac4fd
 	continue;
4ac4fd
 
4ac4fd
+      if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
4ac4fd
+	continue;
4ac4fd
+
4ac4fd
 #ifdef ASM_WEAKEN_DECL
4ac4fd
       ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
4ac4fd
 #else
4ac4fd
@@ -5195,6 +5198,18 @@
4ac4fd
 
4ac4fd
   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4ac4fd
 
4ac4fd
+  if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
4ac4fd
+    {
4ac4fd
+#ifdef ASM_OUTPUT_WEAKREF
4ac4fd
+      ASM_OUTPUT_WEAKREF (asm_out_file, decl,
4ac4fd
+			  IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
4ac4fd
+			  IDENTIFIER_POINTER (target));
4ac4fd
+#else
4ac4fd
+      error ("%Jweakref is not supported in this configuration", decl);
4ac4fd
+#endif
4ac4fd
+      return;
4ac4fd
+    }
4ac4fd
+
4ac4fd
 #ifdef ASM_OUTPUT_DEF
4ac4fd
   /* Make name accessible from other files, if appropriate.  */
4ac4fd
   if (TREE_PUBLIC (decl))
4ac4fd
--- gcc/attribs.c.orig	2005-11-15 19:55:42.000000000 -0200
4ac4fd
+++ gcc/attribs.c	2005-11-15 19:55:48.000000000 -0200
4ac4fd
@@ -75,6 +75,8 @@
4ac4fd
 						 bool *));
4ac4fd
 static tree handle_alias_attribute	PARAMS ((tree *, tree, tree, int,
4ac4fd
 						 bool *));
4ac4fd
+static tree handle_weakref_attribute	PARAMS ((tree *, tree, tree, int,
4ac4fd
+						 bool *));
4ac4fd
 static tree handle_visibility_attribute	PARAMS ((tree *, tree, tree, int,
4ac4fd
 						 bool *));
4ac4fd
 static tree handle_tls_model_attribute	PARAMS ((tree *, tree, tree, int,
4ac4fd
@@ -140,6 +142,8 @@
4ac4fd
 			      handle_weak_attribute },
4ac4fd
   { "alias",                  1, 1, true,  false, false,
4ac4fd
 			      handle_alias_attribute },
4ac4fd
+  { "weakref",                0, 1, true,  false, false,
4ac4fd
+			      handle_weakref_attribute },
4ac4fd
   { "no_instrument_function", 0, 0, true,  false, false,
4ac4fd
 			      handle_no_instrument_function_attribute },
4ac4fd
   { "malloc",                 0, 0, true,  false, false,
4ac4fd
@@ -1060,7 +1064,12 @@
4ac4fd
       if (TREE_CODE (decl) == FUNCTION_DECL)
4ac4fd
 	DECL_INITIAL (decl) = error_mark_node;
4ac4fd
       else
4ac4fd
-	DECL_EXTERNAL (decl) = 0;
4ac4fd
+	{
4ac4fd
+	  if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
4ac4fd
+	    DECL_EXTERNAL (decl) = 1;
4ac4fd
+	  else
4ac4fd
+	    DECL_EXTERNAL (decl) = 0;
4ac4fd
+	}
4ac4fd
     }
4ac4fd
   else
4ac4fd
     {
4ac4fd
@@ -1071,6 +1080,44 @@
4ac4fd
   return NULL_TREE;
4ac4fd
 }
4ac4fd
 
4ac4fd
+/* Handle a "weakref" attribute; arguments as in struct
4ac4fd
+   attribute_spec.handler.  */
4ac4fd
+
4ac4fd
+static tree
4ac4fd
+handle_weakref_attribute (node, name, args, flags, no_add_attrs)
4ac4fd
+     tree *node;
4ac4fd
+     tree name ATTRIBUTE_UNUSED;
4ac4fd
+     tree args;
4ac4fd
+     int flags;
4ac4fd
+     bool *no_add_attrs;
4ac4fd
+{
4ac4fd
+  tree attr = NULL_TREE;
4ac4fd
+
4ac4fd
+  /* The idea here is that `weakref("name")' mutates into `weakref,
4ac4fd
+     alias("name")', and weakref without arguments, in turn,
4ac4fd
+     implicitly adds weak. */
4ac4fd
+
4ac4fd
+  if (args)
4ac4fd
+    {
4ac4fd
+      attr = tree_cons (get_identifier ("alias"), args, attr);
4ac4fd
+      attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
4ac4fd
+
4ac4fd
+      *no_add_attrs = true;
4ac4fd
+    }
4ac4fd
+  else
4ac4fd
+    {
4ac4fd
+      if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
4ac4fd
+	error ("%Jweakref attribute must appear before alias attribute",
4ac4fd
+	       *node);
4ac4fd
+
4ac4fd
+      attr = tree_cons (get_identifier ("weak"), NULL_TREE, attr);
4ac4fd
+    }
4ac4fd
+
4ac4fd
+  decl_attributes (node, attr, flags);
4ac4fd
+
4ac4fd
+  return NULL_TREE;
4ac4fd
+}
4ac4fd
+
4ac4fd
 /* Handle an "visibility" attribute; arguments as in
4ac4fd
    struct attribute_spec.handler.  */
4ac4fd
 
4ac4fd
--- gcc/testsuite/g++.old-deja/g++.ext/weakref1.C	1970-01-01 00:00:00.000000000 +0000
4ac4fd
+++ gcc/testsuite/g++.old-deja/g++.ext/weakref1.C	2005-11-15 19:55:48.000000000 -0200
4ac4fd
@@ -0,0 +1,226 @@
4ac4fd
+// execution test
4ac4fd
+// Additional sources: weakref1a.cc
4ac4fd
+
4ac4fd
+// Copyright 2005 Free Software Foundation, Inc.
4ac4fd
+// Contributed by Alexandre Oliva <aoliva@redhat.com>
4ac4fd
+
4ac4fd
+// Torture test for weakrefs.  The first letter of an identifier
4ac4fd
+// indicates whether/how it is defined; the second letter indicates
4ac4fd
+// whether it is part of a variable or function test; the number that
4ac4fd
+// follows is a test counter, and a letter that may follow enables
4ac4fd
+// multiple identifiers within the same test (e.g., multiple weakrefs
4ac4fd
+// or pointers to the same identifier).
4ac4fd
+
4ac4fd
+// Identifiers starting with W are weakrefs; those with p are
4ac4fd
+// pointers; those with g are global definitions; those with l are
4ac4fd
+// local definitions; those with w are expected to be weak undefined
4ac4fd
+// in the symbol table; those with u are expected to be marked as
4ac4fd
+// non-weak undefined in the symbol table.
4ac4fd
+
4ac4fd
+#include <stdlib.h>
4ac4fd
+
4ac4fd
+#define USED __attribute__((used))
4ac4fd
+
4ac4fd
+extern "C" {
4ac4fd
+typedef int vtype;
4ac4fd
+
4ac4fd
+extern vtype wv1;
4ac4fd
+extern vtype Wv1a __attribute__((weakref ("wv1")));
4ac4fd
+static vtype *pv1a USED = &Wv1a;
4ac4fd
+extern vtype Wv1b __attribute__((weak, weakref, alias ("wv1")));
4ac4fd
+static vtype *pv1b USED = &Wv1b;
4ac4fd
+extern vtype Wv1c __attribute__((weakref));
4ac4fd
+extern vtype Wv1c __attribute__((alias ("wv1")));
4ac4fd
+static vtype *pv1c USED = &Wv1c;
4ac4fd
+
4ac4fd
+vtype gv2;
4ac4fd
+extern vtype Wv2a __attribute__((weakref ("gv2")));
4ac4fd
+static vtype *pv2a USED = &Wv2a;
4ac4fd
+
4ac4fd
+static vtype lv3;
4ac4fd
+extern vtype Wv3a __attribute__((weakref ("lv3")));
4ac4fd
+static vtype *pv3a USED = &Wv3a;
4ac4fd
+
4ac4fd
+extern vtype uv4;
4ac4fd
+extern vtype Wv4a __attribute__((weakref ("uv4")));
4ac4fd
+static vtype *pv4a USED = &Wv4a;
4ac4fd
+static vtype *pv4 USED = &uv;;
4ac4fd
+
4ac4fd
+extern vtype Wv5a __attribute__((weakref ("uv5")));
4ac4fd
+static vtype *pv5a USED = &Wv5a;
4ac4fd
+extern vtype uv5;
4ac4fd
+static vtype *pv5 USED = &uv;;
4ac4fd
+
4ac4fd
+extern vtype Wv6a __attribute__((weakref ("wv6")));
4ac4fd
+static vtype *pv6a USED = &Wv6a;
4ac4fd
+extern vtype wv6;
4ac4fd
+
4ac4fd
+extern vtype Wv7a __attribute__((weakref ("uv7")));
4ac4fd
+static vtype* USED fv7 (void) {
4ac4fd
+  return &Wv7a;
4ac4fd
+}
4ac4fd
+extern vtype uv7;
4ac4fd
+static vtype* USED fv7a (void) {
4ac4fd
+  return &uv;;
4ac4fd
+}
4ac4fd
+
4ac4fd
+extern vtype uv8;
4ac4fd
+static vtype* USED fv8a (void) {
4ac4fd
+  return &uv;;
4ac4fd
+}
4ac4fd
+extern vtype Wv8a __attribute__((weakref ("uv8")));
4ac4fd
+static vtype* USED fv8 (void) {
4ac4fd
+  return &Wv8a;
4ac4fd
+}
4ac4fd
+
4ac4fd
+extern vtype wv9 __attribute__((weak));
4ac4fd
+extern vtype Wv9a __attribute__((weakref ("wv9")));
4ac4fd
+static vtype *pv9a USED = &Wv9a;
4ac4fd
+
4ac4fd
+extern vtype Wv10a __attribute__((weakref ("Wv10b")));
4ac4fd
+extern vtype Wv10b __attribute__((weakref ("Wv10c")));
4ac4fd
+extern vtype Wv10c __attribute__((weakref ("Wv10d")));
4ac4fd
+extern vtype Wv10d __attribute__((weakref ("wv10")));
4ac4fd
+extern vtype wv10;
4ac4fd
+
4ac4fd
+extern vtype wv11;
4ac4fd
+extern vtype Wv11d __attribute__((weakref ("wv11")));
4ac4fd
+extern vtype Wv11c __attribute__((weakref ("Wv11d")));
4ac4fd
+extern vtype Wv11b __attribute__((weakref ("Wv11c")));
4ac4fd
+extern vtype Wv11a __attribute__((weakref ("Wv11b")));
4ac4fd
+
4ac4fd
+extern vtype Wv12 __attribute__((weakref ("wv12")));
4ac4fd
+extern vtype wv12 __attribute__((weak));
4ac4fd
+
4ac4fd
+extern vtype Wv13 __attribute__((weakref ("wv13")));
4ac4fd
+extern vtype wv13 __attribute__((weak));
4ac4fd
+
4ac4fd
+extern vtype Wv14a __attribute__((weakref ("wv14")));
4ac4fd
+extern vtype Wv14b __attribute__((weakref ("wv14")));
4ac4fd
+extern vtype wv14 __attribute__((weak));
4ac4fd
+
4ac4fd
+typedef void ftype(void);
4ac4fd
+
4ac4fd
+extern ftype wf1;
4ac4fd
+extern ftype Wf1a __attribute__((weakref ("wf1")));
4ac4fd
+static ftype *pf1a USED = &Wf1;;
4ac4fd
+extern ftype Wf1b __attribute__((weak, weakref, alias ("wf1")));
4ac4fd
+static ftype *pf1b USED = &Wf1;;
4ac4fd
+extern ftype Wf1c __attribute__((weakref));
4ac4fd
+extern ftype Wf1c __attribute__((alias ("wf1")));
4ac4fd
+static ftype *pf1c USED = &Wf1;;
4ac4fd
+
4ac4fd
+void gf2(void) {}
4ac4fd
+extern ftype Wf2a __attribute__((weakref ("gf2")));
4ac4fd
+static ftype *pf2a USED = &Wf2;;
4ac4fd
+
4ac4fd
+static void lf3(void) {}
4ac4fd
+extern ftype Wf3a __attribute__((weakref ("lf3")));
4ac4fd
+static ftype *pf3a USED = &Wf3;;
4ac4fd
+
4ac4fd
+extern ftype uf4;
4ac4fd
+extern ftype Wf4a __attribute__((weakref ("uf4")));
4ac4fd
+static ftype *pf4a USED = &Wf4;;
4ac4fd
+static ftype *pf4 USED = &uf4;
4ac4fd
+
4ac4fd
+extern ftype Wf5a __attribute__((weakref ("uf5")));
4ac4fd
+static ftype *pf5a USED = &Wf5;;
4ac4fd
+extern ftype uf5;
4ac4fd
+static ftype *pf5 USED = &uf5;
4ac4fd
+
4ac4fd
+extern ftype Wf6a __attribute__((weakref ("wf6")));
4ac4fd
+static ftype *pf6a USED = &Wf6;;
4ac4fd
+extern ftype wf6;
4ac4fd
+
4ac4fd
+extern ftype Wf7a __attribute__((weakref ("uf7")));
4ac4fd
+static ftype* USED ff7 (void) {
4ac4fd
+  return &Wf7;;
4ac4fd
+}
4ac4fd
+extern ftype uf7;
4ac4fd
+static ftype* USED ff7a (void) {
4ac4fd
+  return &uf7;
4ac4fd
+}
4ac4fd
+
4ac4fd
+extern ftype uf8;
4ac4fd
+static ftype* USED ff8a (void) {
4ac4fd
+  return &uf8;
4ac4fd
+}
4ac4fd
+extern ftype Wf8a __attribute__((weakref ("uf8")));
4ac4fd
+static ftype* USED ff8 (void) {
4ac4fd
+  return &Wf8;;
4ac4fd
+}
4ac4fd
+
4ac4fd
+extern ftype wf9 __attribute__((weak));
4ac4fd
+extern ftype Wf9a __attribute__((weakref ("wf9")));
4ac4fd
+static ftype *pf9a USED = &Wf9;;
4ac4fd
+
4ac4fd
+extern ftype Wf10a __attribute__((weakref ("Wf10b")));
4ac4fd
+extern ftype Wf10b __attribute__((weakref ("Wf10c")));
4ac4fd
+extern ftype Wf10c __attribute__((weakref ("Wf10d")));
4ac4fd
+extern ftype Wf10d __attribute__((weakref ("wf10")));
4ac4fd
+extern ftype wf10;
4ac4fd
+
4ac4fd
+extern ftype wf11;
4ac4fd
+extern ftype Wf11d __attribute__((weakref ("wf11")));
4ac4fd
+extern ftype Wf11c __attribute__((weakref ("Wf11d")));
4ac4fd
+extern ftype Wf11b __attribute__((weakref ("Wf11c")));
4ac4fd
+extern ftype Wf11a __attribute__((weakref ("Wf11b")));
4ac4fd
+
4ac4fd
+extern ftype Wf12 __attribute__((weakref ("wf12")));
4ac4fd
+extern ftype wf12 __attribute__((weak));
4ac4fd
+
4ac4fd
+extern ftype Wf13 __attribute__((weakref ("wf13")));
4ac4fd
+extern ftype wf13 __attribute__((weak));
4ac4fd
+
4ac4fd
+extern ftype Wf14a __attribute__((weakref ("wf14")));
4ac4fd
+extern ftype Wf14b __attribute__((weakref ("wf14")));
4ac4fd
+extern ftype wf14 __attribute__((weak));
4ac4fd
+}
4ac4fd
+
4ac4fd
+#define chk(p) do { if (!p) abort (); } while (0)
4ac4fd
+
4ac4fd
+int main () {
4ac4fd
+  chk (!pv1a);
4ac4fd
+  chk (!pv1b);
4ac4fd
+  chk (!pv1c);
4ac4fd
+  chk (pv2a);
4ac4fd
+  chk (pv3a);
4ac4fd
+  chk (pv4a);
4ac4fd
+  chk (pv4);
4ac4fd
+  chk (pv5a);
4ac4fd
+  chk (pv5);
4ac4fd
+  chk (!pv6a);
4ac4fd
+  chk (fv7 ());
4ac4fd
+  chk (fv7a ());
4ac4fd
+  chk (fv8 ());
4ac4fd
+  chk (fv8a ());
4ac4fd
+  chk (!pv9a);
4ac4fd
+  chk (!&Wv10a);
4ac4fd
+  chk (!&Wv11a);
4ac4fd
+  chk (!&Wv12);
4ac4fd
+  chk (!&wv12);
4ac4fd
+  chk (!&wv13);
4ac4fd
+  chk (!&Wv14a);
4ac4fd
+
4ac4fd
+  chk (!pf1a);
4ac4fd
+  chk (!pf1b);
4ac4fd
+  chk (!pf1c);
4ac4fd
+  chk (pf2a);
4ac4fd
+  chk (pf3a);
4ac4fd
+  chk (pf4a);
4ac4fd
+  chk (pf4);
4ac4fd
+  chk (pf5a);
4ac4fd
+  chk (pf5);
4ac4fd
+  chk (!pf6a);
4ac4fd
+  chk (ff7 ());
4ac4fd
+  chk (ff7a ());
4ac4fd
+  chk (ff8 ());
4ac4fd
+  chk (ff8a ());
4ac4fd
+  chk (!pf9a);
4ac4fd
+  chk (!&Wf10a);
4ac4fd
+  chk (!&Wf11a);
4ac4fd
+  chk (!&Wf12);
4ac4fd
+  chk (!&wf12);
4ac4fd
+  chk (!&wf13);
4ac4fd
+  chk (!&Wf14a);
4ac4fd
+}
4ac4fd
--- gcc/testsuite/g++.old-deja/g++.ext/weakref1a.cc	1970-01-01 00:00:00.000000000 +0000
4ac4fd
+++ gcc/testsuite/g++.old-deja/g++.ext/weakref1a.cc	2005-11-15 19:55:48.000000000 -0200
4ac4fd
@@ -0,0 +1,10 @@
4ac4fd
+extern "C" {
4ac4fd
+int uv4;
4ac4fd
+int uv5;
4ac4fd
+int uv7;
4ac4fd
+int uv8;
4ac4fd
+void uf4 (void) {}
4ac4fd
+void uf5 (void) {}
4ac4fd
+void uf7 (void) {}
4ac4fd
+void uf8 (void) {}
4ac4fd
+}
4ac4fd
--- gcc/config/rs6000/rs6000.h.orig	2005-11-16 13:37:46.000000000 -0200
4ac4fd
+++ gcc/config/rs6000/rs6000.h	2005-11-16 14:10:01.000000000 -0200
4ac4fd
@@ -2487,6 +2487,24 @@
4ac4fd
   while (0)
4ac4fd
 #endif
4ac4fd
 
4ac4fd
+#define ASM_OUTPUT_WEAKREF(FILE, DECL, NAME, VALUE)			\
4ac4fd
+  do									\
4ac4fd
+    {									\
4ac4fd
+      fputs ("\t.weakref\t", (FILE));					\
4ac4fd
+      RS6000_OUTPUT_BASENAME ((FILE), (NAME)); 				\
4ac4fd
+      fputs (", ", (FILE));						\
4ac4fd
+      RS6000_OUTPUT_BASENAME ((FILE), (VALUE));				\
4ac4fd
+      if ((DECL) && TREE_CODE (DECL) == FUNCTION_DECL			\
4ac4fd
+	  && DEFAULT_ABI == ABI_AIX)					\
4ac4fd
+	{								\
4ac4fd
+	  fputs ("\n\t.weakref\t.", (FILE));				\
4ac4fd
+	  RS6000_OUTPUT_BASENAME ((FILE), (NAME)); 			\
4ac4fd
+	  fputs (", .", (FILE));					\
4ac4fd
+	  RS6000_OUTPUT_BASENAME ((FILE), (VALUE));			\
4ac4fd
+	}								\
4ac4fd
+      fputc ('\n', (FILE));						\
4ac4fd
+    } while (0)
4ac4fd
+
4ac4fd
 /* This implements the `alias' attribute.  */
4ac4fd
 #undef	ASM_OUTPUT_DEF_FROM_DECLS
4ac4fd
 #define	ASM_OUTPUT_DEF_FROM_DECLS(FILE, DECL, TARGET)			\