+2005-04-01 Danny Smith <dannysmith@users.sourceforge.net>
+
+ * config/i386/cygming.h (SUBTARGET_ATTRIBUTE_TABLE): Define,
+ with entry for selectany attribute.
+ * config/i386/i386-protos.h (ix86_handle_selectany_attribute):
+ Declare.
+ * config/i386/winnt.c (ix86_handle_selectany_attribute): Define.
+ (i386_pe_asm_named_section): Handle sections generated by
+ selectany attribute.
+ * doc/extend.texi (selectany): Document attribute.
+
2005-04-01 Paolo Bonzini <bonzini@gnu.org>
Jan Hubicka <jh@suse.cz>
#undef TARGET_USE_LOCAL_THUNK_ALIAS_P
#define TARGET_USE_LOCAL_THUNK_ALIAS_P(DECL) (!DECL_ONE_ONLY (DECL))
+#define SUBTARGET_ATTRIBUTE_TABLE \
+ /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */ \
+ { "selectany", 0, 0, true, false, false, ix86_handle_selectany_attribute }
+
#undef TREE
#ifndef BUFSIZ
extern int ix86_local_alignment (tree, int);
extern int ix86_constant_alignment (tree, int);
extern tree ix86_handle_shared_attribute (tree *, tree, tree, int, bool *);
+extern tree ix86_handle_selectany_attribute (tree *, tree, tree, int, bool *);
extern unsigned int i386_pe_section_type_flags (tree, const char *, int);
extern void i386_pe_asm_named_section (const char *, unsigned int, tree);
return NULL_TREE;
}
+
+/* Handle a "selectany" attribute;
+ arguments as in struct attribute_spec.handler. */
+tree
+ix86_handle_selectany_attribute (tree *node, tree name,
+ tree args ATTRIBUTE_UNUSED,
+ int flags ATTRIBUTE_UNUSED,
+ bool *no_add_attrs)
+{
+ /* The attribute applies only to objects that are initialized and have
+ external linkage, */
+ if (TREE_CODE (*node) == VAR_DECL && TREE_PUBLIC (*node)
+ && (DECL_INITIAL (*node)
+ /* If an object is initialized with a ctor, the static
+ initialization and destruction code for it is present in
+ each unit defining the object. The code that calls the
+ ctor is protected by a link-once guard variable, so that
+ the object still has link-once semantics, */
+ || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (*node))))
+ make_decl_one_only (*node);
+ else
+ {
+ error ("%qs attribute applies only to initialized variables"
+ " with external linkage", IDENTIFIER_POINTER (name));
+ *no_add_attrs = true;
+ }
+
+ return NULL_TREE;
+}
+
\f
/* Return the type that we should use to determine if DECL is
imported or exported. */
void
i386_pe_asm_named_section (const char *name, unsigned int flags,
- tree decl ATTRIBUTE_UNUSED)
+ tree decl)
{
char flagchars[8], *f = flagchars;
if (flags & SECTION_LINKONCE)
{
/* Functions may have been compiled at various levels of
- optimization so we can't use `same_size' here.
- Instead, have the linker pick one. */
+ optimization so we can't use `same_size' here.
+ Instead, have the linker pick one, without warning.
+ If 'selectany' attibute has been specified, MS compiler
+ sets 'discard' characteristic, rather than telling linker
+ to warn of size or content mismatch, so do the same. */
+ bool discard = (flags & SECTION_CODE)
+ || lookup_attribute ("selectany",
+ DECL_ATTRIBUTES (decl));
fprintf (asm_out_file, "\t.linkonce %s\n",
- (flags & SECTION_CODE ? "discard" : "same_size"));
+ (discard ? "discard" : "same_size"));
}
}
\f
is invalid even if the size of the structure is the same as the size of
the @code{int}.
+@item selectany
+The @code{selectany} attribute causes an initialized global variable to
+have link-once semantics. When multiple definitions of the variable are
+encountered by the linker, the first is selected and the remainder are
+discarded. Following usage by the Microsoft compiler, the linker is told
+@emph{not} to warn about size or content differences of the multiple
+definitions.
+
+Although the primary usage of this attribute is for POD types, the
+attribute can also be applied to global C++ objects that are initialized
+by a constructor. In this case, the static initialization and destruction
+code for the object is emitted in each translation defining the object,
+but the calls to the constructor and destructor are protected by a
+link-once guard variable.
+
+The @code{selectany} attribute is only available on Microsoft Windows
+targets. You can use @code{__declspec (selectany)} as a synonym for
+@code{__attribute__ ((selectany))} for compatibility with other
+compilers.
+
@item weak
The @code{weak} attribute is described in @xref{Function Attributes}.