+static void apply_pragma_weak (tree, tree);
+static void handle_pragma_weak (cpp_reader *);
+
+static void
+apply_pragma_weak (tree decl, tree value)
+{
+ if (value)
+ {
+ value = build_string (IDENTIFIER_LENGTH (value),
+ IDENTIFIER_POINTER (value));
+ decl_attributes (&decl, build_tree_list (get_identifier ("alias"),
+ build_tree_list (NULL, value)),
+ 0);
+ }
+
+ if (SUPPORTS_WEAK && DECL_EXTERNAL (decl) && TREE_USED (decl)
+ && !DECL_WEAK (decl) /* Don't complain about a redundant #pragma. */
+ && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
+ warning ("%Japplying #pragma weak %qD after first use results "
+ "in unspecified behavior", decl, decl);
+
+ declare_weak (decl);
+}
+
+void
+maybe_apply_pragma_weak (tree decl)
+{
+ tree *p, t, id;
+
+ /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
+
+ /* No weak symbols pending, take the short-cut. */
+ if (!pending_weaks)
+ return;
+ /* If it's not visible outside this file, it doesn't matter whether
+ it's weak. */
+ if (!DECL_EXTERNAL (decl) && !TREE_PUBLIC (decl))
+ return;
+ /* If it's not a function or a variable, it can't be weak.
+ FIXME: what kinds of things are visible outside this file but
+ aren't functions or variables? Should this be an assert instead? */
+ if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
+ return;
+
+ id = DECL_ASSEMBLER_NAME (decl);
+
+ for (p = &pending_weaks; (t = *p) ; p = &TREE_CHAIN (t))
+ if (id == TREE_PURPOSE (t))
+ {
+ apply_pragma_weak (decl, TREE_VALUE (t));
+ *p = TREE_CHAIN (t);
+ break;
+ }
+}
+
+/* #pragma weak name [= value] */
+static void
+handle_pragma_weak (cpp_reader * ARG_UNUSED (dummy))
+{
+ tree name, value, x, decl;
+ enum cpp_ttype t;
+
+ value = 0;
+
+ if (c_lex (&name) != CPP_NAME)
+ GCC_BAD ("malformed #pragma weak, ignored");
+ t = c_lex (&x);
+ if (t == CPP_EQ)
+ {
+ if (c_lex (&value) != CPP_NAME)
+ GCC_BAD ("malformed #pragma weak, ignored");
+ t = c_lex (&x);
+ }
+ if (t != CPP_EOF)
+ warning ("junk at end of #pragma weak");
+
+ decl = identifier_global_value (name);
+ if (decl && DECL_P (decl))
+ {
+ apply_pragma_weak (decl, value);
+ if (value)
+ assemble_alias (decl, value);
+ }
+ else
+ pending_weaks = tree_cons (name, value, pending_weaks);
+}
+#else
+void
+maybe_apply_pragma_weak (tree ARG_UNUSED (decl))
+{
+}