diff --git a/gcc/ChangeLog b/gcc/ChangeLog index ab5c35f5505..0a78483058d 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,14 @@ +2005-04-01 Danny Smith + + * 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 Jan Hubicka diff --git a/gcc/config/i386/cygming.h b/gcc/config/i386/cygming.h index a3549941797..de731bb7ada 100644 --- a/gcc/config/i386/cygming.h +++ b/gcc/config/i386/cygming.h @@ -418,6 +418,10 @@ extern int i386_pe_dllimport_name_p (const char *); #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 diff --git a/gcc/config/i386/i386-protos.h b/gcc/config/i386/i386-protos.h index bf693860da7..1ecc3aef1c1 100644 --- a/gcc/config/i386/i386-protos.h +++ b/gcc/config/i386/i386-protos.h @@ -216,6 +216,7 @@ extern int ix86_data_alignment (tree, int); 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); diff --git a/gcc/config/i386/winnt.c b/gcc/config/i386/winnt.c index c8a75440f89..ebd8db75252 100644 --- a/gcc/config/i386/winnt.c +++ b/gcc/config/i386/winnt.c @@ -78,6 +78,36 @@ ix86_handle_shared_attribute (tree *node, tree name, 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; +} + /* Return the type that we should use to determine if DECL is imported or exported. */ @@ -622,7 +652,7 @@ i386_pe_section_type_flags (tree decl, const char *name, int reloc) void i386_pe_asm_named_section (const char *name, unsigned int flags, - tree decl ATTRIBUTE_UNUSED) + tree decl) { char flagchars[8], *f = flagchars; @@ -649,10 +679,16 @@ i386_pe_asm_named_section (const char *name, unsigned int flags, 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")); } } diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 02a03ec4eb4..48784af48b7 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -3000,6 +3000,26 @@ struct S __attribute__ ((vector_size (16))) foo; 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}.