This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
weak_import for Darwin
- From: gkeating at apple dot com (Geoffrey Keating)
- To: gcc-patches at gcc dot gnu dot org
- Date: Wed, 27 Oct 2004 15:59:49 -0700 (PDT)
- Subject: weak_import for Darwin
In GCC 3.5, at least when it was called 3.5, a source file like
#define MAC_OS_X_VERSION_MIN_REQUIRED MAC_OS_X_VERSION_10_2
#include <mach-o/dyld.h>
would produce warnings/errors, because '__attribute__((weak_import))'
was not supported. In GCC 4.0, it silently doesn't do weak_import and
doesn't warn, which is even worse, and it happens because of a broken
use of __GNUC__ in a system header file. This patch adds weak_import
to GCC and adds a fixinclude to deal with the header file.
Bootstrapped & tested on powerpc-darwin.
--
- Geoffrey Keating <geoffk@apple.com>
===File ~/patches/gcc-darwin-weak-import.patch==============
Index: fixincludes/ChangeLog
2004-10-27 Geoffrey Keating <geoffk@apple.com>
* inclhack.def (darwin_gcc4_breakage): New.
* fixincl.x: Regenerate.
Index: gcc/ChangeLog
2004-10-27 Geoffrey Keating <geoffk@apple.com>
* config/rs6000/rs6000.c (rs6000_attribute_table): Add
SUBTARGET_ATTRIBUTE_TABLE.
* config/darwin.h (ASM_WEAKEN_DECL): Handle weak_import.
(SUBTARGET_ATTRIBUTE_TABLE): Define.
* config/darwin.c (darwin_handle_weak_import_attribute): New.
(HAVE_DEAD_STRIP): Delete.
(no_dead_strip): Don't test HAVE_DEAD_STRIP.
* config/darwin-protos.h (darwin_handle_weak_import_attribute):
Prototype.
Index: gcc/testsuite/ChangeLog
2004-10-27 Geoffrey Keating <geoffk@apple.com>
* gcc.dg/darwin-weakimport-1.c: New.
Index: fixincludes/inclhack.def
===================================================================
RCS file: /cvs/gcc/gcc/fixincludes/inclhack.def,v
retrieving revision 1.5
diff -u -p -u -p -r1.5 inclhack.def
--- fixincludes/inclhack.def 17 Sep 2004 22:58:40 -0000 1.5
+++ fixincludes/inclhack.def 27 Oct 2004 22:54:29 -0000
@@ -978,6 +978,23 @@ fix = {
/*
+ * AvailabilityMacros.h on Darwin breaks with GCC 4.0, because of
+ * bad __GNUC__ tests.
+ */
+
+fix = {
+ hackname = darwin_gcc4_breakage;
+ mach = "*-*-darwin*";
+ files = AvailabilityMacros.h;
+ select = "\\(__GNUC__ >= 3\\) && \\(__GNUC_MINOR__ >= 1\\)";
+ c_fix = format;
+ c_fix_arg = "((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))";
+ test_text = "#if defined(__GNUC__) && (__GNUC__ >= 3) && "
+ "(__GNUC_MINOR__ >= 1)\n";
+};
+
+
+/*
* __private_extern__ doesn't exist in FSF GCC. Even if it did,
* why would you ever put it in a system header file?
*/
Index: gcc/config/darwin-protos.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/config/darwin-protos.h,v
retrieving revision 1.39
diff -u -p -u -p -r1.39 darwin-protos.h
--- gcc/config/darwin-protos.h 26 Oct 2004 06:08:41 -0000 1.39
+++ gcc/config/darwin-protos.h 27 Oct 2004 22:54:30 -0000
@@ -82,6 +82,10 @@ extern void darwin_file_end (void);
extern void darwin_mark_decl_preserved (const char *);
+extern tree darwin_handle_weak_import_attribute (tree *node, tree name,
+ tree args, int flags,
+ bool * no_add_attrs);
+
/* Expanded by EXTRA_SECTION_FUNCTIONS into varasm.o. */
extern void text_coal_section (void);
extern void text_unlikely_section (void);
Index: gcc/config/darwin.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/config/darwin.c,v
retrieving revision 1.93
diff -u -p -u -p -r1.93 darwin.c
--- gcc/config/darwin.c 26 Oct 2004 06:08:41 -0000 1.93
+++ gcc/config/darwin.c 27 Oct 2004 22:54:30 -0000
@@ -1209,13 +1209,30 @@ darwin_unique_section (tree decl ATTRIBU
/* Darwin does not use unique sections. */
}
-#define HAVE_DEAD_STRIP 0
+/* Handle a "weak_import" attribute; arguments as in
+ struct attribute_spec.handler. */
+
+tree
+darwin_handle_weak_import_attribute (tree *node, tree name,
+ tree ARG_UNUSED (args),
+ int ARG_UNUSED (flags),
+ bool * no_add_attrs)
+{
+ if (TREE_CODE (*node) != FUNCTION_DECL)
+ {
+ warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
+ *no_add_attrs = true;
+ }
+ else
+ declare_weak (*node);
+
+ return NULL_TREE;
+}
static void
no_dead_strip (FILE *file, const char *lab)
{
- if (HAVE_DEAD_STRIP)
- fprintf (file, ".no_dead_strip %s\n", lab);
+ fprintf (file, ".no_dead_strip %s\n", lab);
}
/* Emit a label for an FDE, making it global and/or weak if appropriate.
Index: gcc/config/darwin.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/config/darwin.h,v
retrieving revision 1.101
diff -u -p -u -p -r1.101 darwin.h
--- gcc/config/darwin.h 27 Oct 2004 21:03:43 -0000 1.101
+++ gcc/config/darwin.h 27 Oct 2004 22:54:30 -0000
@@ -385,6 +385,9 @@ extern const char *darwin_fix_and_contin
targetm.asm_out.globalize_label (FILE, NAME); \
if (DECL_EXTERNAL (DECL)) \
fputs ("\t.weak_reference ", FILE); \
+ else if (! lookup_attribute ("weak", DECL_ATTRIBUTES (DECL)) \
+ && lookup_attribute ("weak_import", DECL_ATTRIBUTES (DECL))) \
+ break; \
else if (TREE_PUBLIC (DECL)) \
fputs ("\t.weak_definition ", FILE); \
else \
@@ -862,6 +865,11 @@ objc_section_init (void) \
#undef TARGET_ASM_ASSEMBLE_VISIBILITY
#define TARGET_ASM_ASSEMBLE_VISIBILITY darwin_assemble_visibility
+/* Extra attributes for Darwin. */
+#define SUBTARGET_ATTRIBUTE_TABLE \
+ /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */ \
+ { "weak_import", 0, 0, true, false, false, \
+ darwin_handle_weak_import_attribute }
#undef ASM_GENERATE_INTERNAL_LABEL
#define ASM_GENERATE_INTERNAL_LABEL(LABEL,PREFIX,NUM) \
Index: gcc/config/rs6000/rs6000.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/config/rs6000/rs6000.c,v
retrieving revision 1.730
diff -u -p -u -p -r1.730 rs6000.c
--- gcc/config/rs6000/rs6000.c 27 Oct 2004 13:11:50 -0000 1.730
+++ gcc/config/rs6000/rs6000.c 27 Oct 2004 22:54:31 -0000
@@ -16659,6 +16659,9 @@ const struct attribute_spec rs6000_attri
{ "altivec", 1, 1, false, true, false, rs6000_handle_altivec_attribute },
{ "longcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute },
{ "shortcall", 0, 0, false, true, true, rs6000_handle_longcall_attribute },
+#ifdef SUBTARGET_ATTRIBUTE_TABLE
+ SUBTARGET_ATTRIBUTE_TABLE,
+#endif
{ NULL, 0, 0, false, false, false, NULL }
};
Index: gcc/testsuite/gcc.dg/darwin-weakimport-1.c
===================================================================
RCS file: gcc/testsuite/gcc.dg/darwin-weakimport-1.c
diff -N gcc/testsuite/gcc.dg/darwin-weakimport-1.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ gcc/testsuite/gcc.dg/darwin-weakimport-1.c 27 Oct 2004 22:54:35 -0000
@@ -0,0 +1,16 @@
+/* { dg-do compile { target *-*-darwin* } } */
+/* { dg-require-weak "" } */
+/* { dg-options "-fno-common" } */
+
+/* { dg-final { scan-assembler "weak_reference _a" } } */
+/* { dg-final { scan-assembler-not "weak_\[a-z \t\]*_b" } } */
+
+extern void a (void) __attribute__((weak_import));
+extern void b (void) __attribute__((weak_import));
+
+void b(void)
+{
+ a();
+}
+
+extern int c __attribute__((weak_import)); /* { dg-warning "ignored" } */
============================================================