public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] document that alias and target must have the same type
@ 2020-02-05  1:05 Martin Sebor
  2020-02-05 20:13 ` Martin Sebor
  2020-02-13 22:45 ` Sandra Loosemore
  0 siblings, 2 replies; 7+ messages in thread
From: Martin Sebor @ 2020-02-05  1:05 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 440 bytes --]

GCC diagnoses declarations of function aliases whose type doesn't
match that of the target (ditto for attribute weakref).  It doesn't
yet diagnose such incompatbilities for variable aliases but that's
just an oversight that I will try to remember to correct in GCC 11.
The attached patch updates the manual to make it clear that
aliases must have the same type as their targets, or the behavior
is undefined (and may be diagnosed).

Martin

[-- Attachment #2: gcc-doc-attr-alias.diff --]
[-- Type: text/x-patch, Size: 2877 bytes --]

gcc/ChangeLog:

	* doc/extend.texi (attribute alias, weak, weakref): Mention that
	the target of an alias must have the same type as the alias itself.

diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index ec99c38a607..6f602140bfa 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -2558,7 +2558,9 @@ __attribute__ ((access (write_only, 1, 2), access (read_write, 3))) int fgets (c
 @item alias ("@var{target}")
 @cindex @code{alias} function attribute
 The @code{alias} attribute causes the declaration to be emitted as an
-alias for another symbol, which must be specified.  For instance,
+alias for another symbol, which must have been previously declared with
+the same type.  Declaring an alias with a different type than the target
+is undefined and may be diagnosed.  For instance,
 
 @smallexample
 void __f () @{ /* @r{Do something.} */; @}
@@ -3922,9 +3924,10 @@ results in warning on line 5.
 The @code{weak} attribute causes the declaration to be emitted as a weak
 symbol rather than a global.  This is primarily useful in defining
 library functions that can be overridden in user code, though it can
-also be used with non-function declarations.  Weak symbols are supported
-for ELF targets, and also for a.out targets when using the GNU assembler
-and linker.
+also be used with non-function declarations.  The overriding symbol must
+have the same type as the declaration of the weak symbol.  Weak symbols
+are supported for ELF targets, and also for a.out targets when using
+the GNU assembler and linker.
 
 @item weakref
 @itemx weakref ("@var{target}")
@@ -3932,7 +3935,8 @@ and linker.
 The @code{weakref} attribute marks a declaration as a weak reference.
 Without arguments, it should be accompanied by an @code{alias} attribute
 naming the target symbol.  Optionally, the @var{target} may be given as
-an argument to @code{weakref} itself.  In either case, @code{weakref}
+an argument to @code{weakref} itself.  The the @var{target} must have
+the same type as the declaration.  In either case, @code{weakref}
 implicitly marks the declaration as @code{weak}.  Without a
 @var{target}, given as an argument to @code{weakref} or to @code{alias},
 @code{weakref} is equivalent to @code{weak}.
@@ -3951,7 +3955,9 @@ definition to be given for the target symbol.  If the target symbol is
 only referenced through weak references, then it becomes a @code{weak}
 undefined symbol.  If it is directly referenced, however, then such
 strong references prevail, and a definition is required for the
-symbol, not necessarily in the same translation unit.
+symbol, not necessarily in the same translation unit.  The definition
+must have the same type as the alias, otherwise the behavior is
+undefined.
 
 The effect is equivalent to moving all references to the alias to a
 separate translation unit, renaming the alias to the aliased symbol,

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] document that alias and target must have the same type
  2020-02-05  1:05 [PATCH] document that alias and target must have the same type Martin Sebor
@ 2020-02-05 20:13 ` Martin Sebor
  2020-02-12 21:02   ` [PING PATCH] " Martin Sebor
  2020-02-13 22:55   ` [PATCH] " Sandra Loosemore
  2020-02-13 22:45 ` Sandra Loosemore
  1 sibling, 2 replies; 7+ messages in thread
From: Martin Sebor @ 2020-02-05 20:13 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 2479 bytes --]

On 2/4/20 6:05 PM, Martin Sebor wrote:
> GCC diagnoses declarations of function aliases whose type doesn't
> match that of the target (ditto for attribute weakref).  It doesn't
> yet diagnose such incompatbilities for variable aliases but that's
> just an oversight that I will try to remember to correct in GCC 11.
> The attached patch updates the manual to make it clear that
> aliases must have the same type as their targets, or the behavior
> is undefined (and may be diagnosed).

On further review I noticed a few problems with the documentation
of attribute weakref.  The manual says that

   Without a target, given as an argument to weakref or to alias,
   weakref is equivalent to weak.

and

   At present, a declaration to which weakref is attached can only
   be static.

However, GCC accepts the following declaration:

   extern int x (void) __attribute__ ((weakref));

so the second sentence isn't correct without qualification (unlike
weakref, a weak declaration must be external).

Another documentation problem is with the example in the manual that
says that this declaration:

   static int x() __attribute__ ((weakref ("y")));

   /* is equivalent to... */

   static int x() __attribute__ ((weak, weakref, alias ("y")));

but GCC rejects the latter with

   error: weak declaration of ‘x’ must be public

Changing the latter from static to extern changes the error to

   error: ‘weakref’ symbol ‘x’ must have static linkage

So clearly two declarations with the two sets of attributes are not
equivalent, either extern or static.

I've fixed up these documentation problems in the attached revision
of the original patch.  I also mention that besides their types
having to match, the alias must have the same size and alignment
as the target.

Martin

PS I also noted that when the function is then also used GCC issues:

   warning: ‘weakref’ attribute should be accompanied with an ‘alias’ 
attribute

This matches what the manual says in "Without arguments, it should
be accompanied by an alias attribute naming the target symbol."

But when the weakref function is not used there is no warning.
That seems like an unfortunate side-effect of the choice of issuing
the warning a little too late (waning from the front-end it would
make it consistent regardless of whether the function was used, and
it would highlight the omission even in translation units that
define the weakref without using it).

[-- Attachment #2: gcc-doc-attr-alias.diff --]
[-- Type: text/x-patch, Size: 4347 bytes --]

diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index ec99c38a607..3634ce1c423 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -2557,8 +2557,11 @@ __attribute__ ((access (write_only, 1, 2), access (read_write, 3))) int fgets (c
 
 @item alias ("@var{target}")
 @cindex @code{alias} function attribute
-The @code{alias} attribute causes the declaration to be emitted as an
-alias for another symbol, which must be specified.  For instance,
+The @code{alias} attribute causes the declaration to be emitted as an alias
+for another symbol, which must have been previously declared with the same
+type, and for variables same size and alignment.  Declaring an alias with
+a different type than the target is undefined and may be diagnosed.  For
+instance, an alias for another symbol, which must be specified.  For instance,
 
 @smallexample
 void __f () @{ /* @r{Do something.} */; @}
@@ -3919,31 +3922,41 @@ results in warning on line 5.
 
 @item weak
 @cindex @code{weak} function attribute
-The @code{weak} attribute causes the declaration to be emitted as a weak
-symbol rather than a global.  This is primarily useful in defining
-library functions that can be overridden in user code, though it can
-also be used with non-function declarations.  Weak symbols are supported
-for ELF targets, and also for a.out targets when using the GNU assembler
-and linker.
+The @code{weak} attribute causes a declaration of an external symbol
+to be emitted as a weak symbol rather than a global.  This is primarily
+useful in defining library functions that can be overridden in user code,
+though it can also be used with non-function declarations.  The overriding
+symbol must have the same type, and for variables size, and alignment as
+the weak symbol.  Weak symbols are supported for ELF targets, and also for
+a.out targets when using the GNU assembler and linker.
 
 @item weakref
 @itemx weakref ("@var{target}")
 @cindex @code{weakref} function attribute
 The @code{weakref} attribute marks a declaration as a weak reference.
 Without arguments, it should be accompanied by an @code{alias} attribute
-naming the target symbol.  Optionally, the @var{target} may be given as
-an argument to @code{weakref} itself.  In either case, @code{weakref}
-implicitly marks the declaration as @code{weak}.  Without a
-@var{target}, given as an argument to @code{weakref} or to @code{alias},
-@code{weakref} is equivalent to @code{weak}.
+naming the target symbol.  Alernatively, @var{target} may be given as
+an argument to @code{weakref} itself, naming the target definition of
+the alias.  The the @var{target} must have the same type, and for
+variables also the same size and alignment as the declaration.  In either
+form of the declaration @code{weakref} implicitly marks the declaration
+as @code{weak}.  Without a @var{target} given as an argument to
+@code{weakref} or to @code{alias}, @code{weakref} is equivalent to
+@code{weak} (in that case the declaration may be @code{extern}).
 
 @smallexample
-static int x() __attribute__ ((weakref ("y")));
+/* Given the declaration: */
+extern int y (void);
+
+/* the following... */
+static int x (void) __attribute__ ((weakref ("y")));
+
 /* is equivalent to... */
-static int x() __attribute__ ((weak, weakref, alias ("y")));
-/* and to... */
-static int x() __attribute__ ((weakref));
-static int x() __attribute__ ((alias ("y")));
+static int x (void) __attribute__ ((weakref, alias ("y")));
+
+/* or, alternatively, to... */
+static int x (void) __attribute__ ((weakref));
+static int x (void) __attribute__ ((alias ("y")));
 @end smallexample
 
 A weak reference is an alias that does not by itself require a
@@ -3956,10 +3969,10 @@ symbol, not necessarily in the same translation unit.
 The effect is equivalent to moving all references to the alias to a
 separate translation unit, renaming the alias to the aliased symbol,
 declaring it as weak, compiling the two separate translation units and
-performing a link with relocatable output (ie: @code{ld -r}) on them.
+performing a link with relocatable output (i.e.@: @code{ld -r}) on them.
 
-At present, a declaration to which @code{weakref} is attached can
-only be @code{static}.
+A declaration to which @code{weakref} is attached and that is associated
+with a named @code{target} must be @code{static}.
 
 @end table
 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PING PATCH] document that alias and target must have the same type
  2020-02-05 20:13 ` Martin Sebor
@ 2020-02-12 21:02   ` Martin Sebor
  2020-02-13 22:55   ` [PATCH] " Sandra Loosemore
  1 sibling, 0 replies; 7+ messages in thread
From: Martin Sebor @ 2020-02-12 21:02 UTC (permalink / raw)
  To: gcc-patches

Ping: https://gcc.gnu.org/ml/gcc-patches/2020-02/msg00290.html

On 2/5/20 1:13 PM, Martin Sebor wrote:
> On 2/4/20 6:05 PM, Martin Sebor wrote:
>> GCC diagnoses declarations of function aliases whose type doesn't
>> match that of the target (ditto for attribute weakref).  It doesn't
>> yet diagnose such incompatbilities for variable aliases but that's
>> just an oversight that I will try to remember to correct in GCC 11.
>> The attached patch updates the manual to make it clear that
>> aliases must have the same type as their targets, or the behavior
>> is undefined (and may be diagnosed).
> 
> On further review I noticed a few problems with the documentation
> of attribute weakref.  The manual says that
> 
>    Without a target, given as an argument to weakref or to alias,
>    weakref is equivalent to weak.
> 
> and
> 
>    At present, a declaration to which weakref is attached can only
>    be static.
> 
> However, GCC accepts the following declaration:
> 
>    extern int x (void) __attribute__ ((weakref));
> 
> so the second sentence isn't correct without qualification (unlike
> weakref, a weak declaration must be external).
> 
> Another documentation problem is with the example in the manual that
> says that this declaration:
> 
>    static int x() __attribute__ ((weakref ("y")));
> 
>    /* is equivalent to... */
> 
>    static int x() __attribute__ ((weak, weakref, alias ("y")));
> 
> but GCC rejects the latter with
> 
>    error: weak declaration of ‘x’ must be public
> 
> Changing the latter from static to extern changes the error to
> 
>    error: ‘weakref’ symbol ‘x’ must have static linkage
> 
> So clearly two declarations with the two sets of attributes are not
> equivalent, either extern or static.
> 
> I've fixed up these documentation problems in the attached revision
> of the original patch.  I also mention that besides their types
> having to match, the alias must have the same size and alignment
> as the target.
> 
> Martin
> 
> PS I also noted that when the function is then also used GCC issues:
> 
>    warning: ‘weakref’ attribute should be accompanied with an ‘alias’ 
> attribute
> 
> This matches what the manual says in "Without arguments, it should
> be accompanied by an alias attribute naming the target symbol."
> 
> But when the weakref function is not used there is no warning.
> That seems like an unfortunate side-effect of the choice of issuing
> the warning a little too late (waning from the front-end it would
> make it consistent regardless of whether the function was used, and
> it would highlight the omission even in translation units that
> define the weakref without using it).

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] document that alias and target must have the same type
  2020-02-05  1:05 [PATCH] document that alias and target must have the same type Martin Sebor
  2020-02-05 20:13 ` Martin Sebor
@ 2020-02-13 22:45 ` Sandra Loosemore
  1 sibling, 0 replies; 7+ messages in thread
From: Sandra Loosemore @ 2020-02-13 22:45 UTC (permalink / raw)
  To: Martin Sebor, gcc-patches

On 2/4/20 6:05 PM, Martin Sebor wrote:
> GCC diagnoses declarations of function aliases whose type doesn't
> match that of the target (ditto for attribute weakref).  It doesn't
> yet diagnose such incompatbilities for variable aliases but that's
> just an oversight that I will try to remember to correct in GCC 11.
> The attached patch updates the manual to make it clear that
> aliases must have the same type as their targets, or the behavior
> is undefined (and may be diagnosed).
> 

This is OK with the "The the" typo in this hunk fixed:

> @@ -3932,7 +3935,8 @@ and linker.
>  The @code{weakref} attribute marks a declaration as a weak reference.
>  Without arguments, it should be accompanied by an @code{alias} attribute
>  naming the target symbol.  Optionally, the @var{target} may be given as
> -an argument to @code{weakref} itself.  In either case, @code{weakref}
> +an argument to @code{weakref} itself.  The the @var{target} must have
> +the same type as the declaration.  In either case, @code{weakref}
>  implicitly marks the declaration as @code{weak}.  Without a
>  @var{target}, given as an argument to @code{weakref} or to @code{alias},
>  @code{weakref} is equivalent to @code{weak}.

-Sandra

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] document that alias and target must have the same type
  2020-02-05 20:13 ` Martin Sebor
  2020-02-12 21:02   ` [PING PATCH] " Martin Sebor
@ 2020-02-13 22:55   ` Sandra Loosemore
  2020-02-14 18:30     ` Martin Sebor
  1 sibling, 1 reply; 7+ messages in thread
From: Sandra Loosemore @ 2020-02-13 22:55 UTC (permalink / raw)
  To: Martin Sebor, gcc-patches

On 2/5/20 1:13 PM, Martin Sebor wrote:

> diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
> index ec99c38a607..3634ce1c423 100644
> --- a/gcc/doc/extend.texi
> +++ b/gcc/doc/extend.texi
> @@ -2557,8 +2557,11 @@ __attribute__ ((access (write_only, 1, 2), access (read_write, 3))) int fgets (c
>  
>  @item alias ("@var{target}")
>  @cindex @code{alias} function attribute
> -The @code{alias} attribute causes the declaration to be emitted as an
> -alias for another symbol, which must be specified.  For instance,
> +The @code{alias} attribute causes the declaration to be emitted as an alias
> +for another symbol, which must have been previously declared with the same
> +type, and for variables same size and alignment.  Declaring an alias with
> +a different type than the target is undefined and may be diagnosed.  For
> +instance, an alias for another symbol, which must be specified.  For instance,
>  
>  @smallexample
>  void __f () @{ /* @r{Do something.} */; @}

This is kind of garbled.  Do you have an extraneous sentence beginning 
"For instance" in there?  It doesn't flow from the text you added before 
that.

> @@ -3919,31 +3922,41 @@ results in warning on line 5.
>  
>  @item weak
>  @cindex @code{weak} function attribute
> -The @code{weak} attribute causes the declaration to be emitted as a weak
> -symbol rather than a global.  This is primarily useful in defining
> -library functions that can be overridden in user code, though it can
> -also be used with non-function declarations.  Weak symbols are supported
> -for ELF targets, and also for a.out targets when using the GNU assembler
> -and linker.
> +The @code{weak} attribute causes a declaration of an external symbol
> +to be emitted as a weak symbol rather than a global.  This is primarily
> +useful in defining library functions that can be overridden in user code,
> +though it can also be used with non-function declarations.  The overriding
> +symbol must have the same type, and for variables size, and alignment as
> +the weak symbol.  Weak symbols are supported for ELF targets, and also for
> +a.out targets when using the GNU assembler and linker.

The "for variables" clause is awkward and has a comma in the wrong 
place.  I'd split this into a separate sentence, like

The overriding symbol must have the same type as the weak symbol.  If it 
is a variable it must also have the same size and alignment.

>  @item weakref
>  @itemx weakref ("@var{target}")
>  @cindex @code{weakref} function attribute
>  The @code{weakref} attribute marks a declaration as a weak reference.
>  Without arguments, it should be accompanied by an @code{alias} attribute
> -naming the target symbol.  Optionally, the @var{target} may be given as
> -an argument to @code{weakref} itself.  In either case, @code{weakref}
> -implicitly marks the declaration as @code{weak}.  Without a
> -@var{target}, given as an argument to @code{weakref} or to @code{alias},
> -@code{weakref} is equivalent to @code{weak}.
> +naming the target symbol.  Alernatively, @var{target} may be given as

s/Alernatively/Alternatively/

> +an argument to @code{weakref} itself, naming the target definition of
> +the alias.  The the @var{target} must have the same type, and for

s/The the/The/

> +variables also the same size and alignment as the declaration.  In either

Same comments above re splitting this into two sentences to avoid 
awkward wording.

The rest of this patch looks OK.

-Sandra

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] document that alias and target must have the same type
  2020-02-13 22:55   ` [PATCH] " Sandra Loosemore
@ 2020-02-14 18:30     ` Martin Sebor
  2020-02-15  0:08       ` Sandra Loosemore
  0 siblings, 1 reply; 7+ messages in thread
From: Martin Sebor @ 2020-02-14 18:30 UTC (permalink / raw)
  To: Sandra Loosemore, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 4022 bytes --]

On 2/13/20 3:55 PM, Sandra Loosemore wrote:
> On 2/5/20 1:13 PM, Martin Sebor wrote:
> 
>> diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
>> index ec99c38a607..3634ce1c423 100644
>> --- a/gcc/doc/extend.texi
>> +++ b/gcc/doc/extend.texi
>> @@ -2557,8 +2557,11 @@ __attribute__ ((access (write_only, 1, 2), 
>> access (read_write, 3))) int fgets (c
>>
>>  @item alias ("@var{target}")
>>  @cindex @code{alias} function attribute
>> -The @code{alias} attribute causes the declaration to be emitted as an
>> -alias for another symbol, which must be specified.  For instance,
>> +The @code{alias} attribute causes the declaration to be emitted as an 
>> alias
>> +for another symbol, which must have been previously declared with the 
>> same
>> +type, and for variables same size and alignment.  Declaring an alias 
>> with
>> +a different type than the target is undefined and may be diagnosed.  For
>> +instance, an alias for another symbol, which must be specified.  For 
>> instance,
>>
>>  @smallexample
>>  void __f () @{ /* @r{Do something.} */; @}
> 
> This is kind of garbled.  Do you have an extraneous sentence beginning 
> "For instance" in there?  It doesn't flow from the text you added before 
> that.

That does look garbled.  I've fixed that in the revised patch.

> 
>> @@ -3919,31 +3922,41 @@ results in warning on line 5.
>>
>>  @item weak
>>  @cindex @code{weak} function attribute
>> -The @code{weak} attribute causes the declaration to be emitted as a weak
>> -symbol rather than a global.  This is primarily useful in defining
>> -library functions that can be overridden in user code, though it can
>> -also be used with non-function declarations.  Weak symbols are supported
>> -for ELF targets, and also for a.out targets when using the GNU assembler
>> -and linker.
>> +The @code{weak} attribute causes a declaration of an external symbol
>> +to be emitted as a weak symbol rather than a global.  This is primarily
>> +useful in defining library functions that can be overridden in user 
>> code,
>> +though it can also be used with non-function declarations.  The 
>> overriding
>> +symbol must have the same type, and for variables size, and alignment as
>> +the weak symbol.  Weak symbols are supported for ELF targets, and 
>> also for
>> +a.out targets when using the GNU assembler and linker.
> 
> The "for variables" clause is awkward and has a comma in the wrong 
> place.  I'd split this into a separate sentence, like
> 
> The overriding symbol must have the same type as the weak symbol.  If it 
> is a variable it must also have the same size and alignment.

Agreed, that looks better.

> 
>>  @item weakref
>>  @itemx weakref ("@var{target}")
>>  @cindex @code{weakref} function attribute
>>  The @code{weakref} attribute marks a declaration as a weak reference.
>>  Without arguments, it should be accompanied by an @code{alias} attribute
>> -naming the target symbol.  Optionally, the @var{target} may be given as
>> -an argument to @code{weakref} itself.  In either case, @code{weakref}
>> -implicitly marks the declaration as @code{weak}.  Without a
>> -@var{target}, given as an argument to @code{weakref} or to @code{alias},
>> -@code{weakref} is equivalent to @code{weak}.
>> +naming the target symbol.  Alernatively, @var{target} may be given as
> 
> s/Alernatively/Alternatively/
> 
>> +an argument to @code{weakref} itself, naming the target definition of
>> +the alias.  The the @var{target} must have the same type, and for
> 
> s/The the/The/
> 
>> +variables also the same size and alignment as the declaration.  In 
>> either
> 
> Same comments above re splitting this into two sentences to avoid 
> awkward wording.

I've spit it up into multiple sentences as you suggested.  Attached
is the updated revision that I plan to commit unless you (or anyone
else) have further suggestions.

Thanks for the review!

Martin

> 
> The rest of this patch looks OK.
> 
> -Sandra


[-- Attachment #2: gcc-doc-attr-alias.diff --]
[-- Type: text/x-patch, Size: 5172 bytes --]

gcc/ChangeLog:

	* doc/extend.texi (attribute alias): Mention type requirement
	(attribute weak): Same.
	(attribute weakref): Correct invalid example.

diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 5739063b330..b7f462a76b0 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -2557,8 +2557,11 @@ __attribute__ ((access (write_only, 1, 2), access (read_write, 3))) int fgets (c
 
 @item alias ("@var{target}")
 @cindex @code{alias} function attribute
-The @code{alias} attribute causes the declaration to be emitted as an
-alias for another symbol, which must be specified.  For instance,
+The @code{alias} attribute causes the declaration to be emitted as an alias
+for another symbol, which must have been previously declared with the same
+type, and for variables, also the same size and alignment.  Declaring an alias
+with a different type than the target is undefined and may be diagnosed.  As
+an example, the following declarations:
 
 @smallexample
 void __f () @{ /* @r{Do something.} */; @}
@@ -2566,9 +2569,9 @@ void f () __attribute__ ((weak, alias ("__f")));
 @end smallexample
 
 @noindent
-defines @samp{f} to be a weak alias for @samp{__f}.  In C++, the
-mangled name for the target must be used.  It is an error if @samp{__f}
-is not defined in the same translation unit.
+define @samp{f} to be a weak alias for @samp{__f}.  In C++, the mangled name
+for the target must be used.  It is an error if @samp{__f} is not defined in
+the same translation unit.
 
 This attribute requires assembler and object file support,
 and may not be available on all targets.
@@ -3919,31 +3922,43 @@ results in warning on line 5.
 
 @item weak
 @cindex @code{weak} function attribute
-The @code{weak} attribute causes the declaration to be emitted as a weak
-symbol rather than a global.  This is primarily useful in defining
-library functions that can be overridden in user code, though it can
-also be used with non-function declarations.  Weak symbols are supported
-for ELF targets, and also for a.out targets when using the GNU assembler
-and linker.
+The @code{weak} attribute causes a declaration of an external symbol
+to be emitted as a weak symbol rather than a global.  This is primarily
+useful in defining library functions that can be overridden in user code,
+though it can also be used with non-function declarations.  The overriding
+symbol must have the same type as the weak symbol.  In addition, if it
+designates a variable it must also have the same size and alignment as
+the weak symbol.  Weak symbols are supported for ELF targets, and also
+for a.out targets when using the GNU assembler and linker.
 
 @item weakref
 @itemx weakref ("@var{target}")
 @cindex @code{weakref} function attribute
 The @code{weakref} attribute marks a declaration as a weak reference.
 Without arguments, it should be accompanied by an @code{alias} attribute
-naming the target symbol.  Optionally, the @var{target} may be given as
-an argument to @code{weakref} itself.  In either case, @code{weakref}
-implicitly marks the declaration as @code{weak}.  Without a
-@var{target}, given as an argument to @code{weakref} or to @code{alias},
-@code{weakref} is equivalent to @code{weak}.
+naming the target symbol.  Alternatively, @var{target} may be given as
+an argument to @code{weakref} itself, naming the target definition of
+the alias.  The @var{target} must have the same type as the declaration.
+In addition, if it designates a variable it must also have the same size
+and alignment as the declaration.  In either form of the declaration
+@code{weakref} implicitly marks the declared symbol as @code{weak}.  Without
+a @var{target} given as an argument to @code{weakref} or to @code{alias},
+@code{weakref} is equivalent to @code{weak} (in that case the declaration
+may be @code{extern}).
 
 @smallexample
-static int x() __attribute__ ((weakref ("y")));
+/* Given the declaration: */
+extern int y (void);
+
+/* the following... */
+static int x (void) __attribute__ ((weakref ("y")));
+
 /* is equivalent to... */
-static int x() __attribute__ ((weak, weakref, alias ("y")));
-/* and to... */
-static int x() __attribute__ ((weakref));
-static int x() __attribute__ ((alias ("y")));
+static int x (void) __attribute__ ((weakref, alias ("y")));
+
+/* or, alternatively, to... */
+static int x (void) __attribute__ ((weakref));
+static int x (void) __attribute__ ((alias ("y")));
 @end smallexample
 
 A weak reference is an alias that does not by itself require a
@@ -3956,10 +3971,10 @@ symbol, not necessarily in the same translation unit.
 The effect is equivalent to moving all references to the alias to a
 separate translation unit, renaming the alias to the aliased symbol,
 declaring it as weak, compiling the two separate translation units and
-performing a link with relocatable output (ie: @code{ld -r}) on them.
+performing a link with relocatable output (i.e.@: @code{ld -r}) on them.
 
-At present, a declaration to which @code{weakref} is attached can
-only be @code{static}.
+A declaration to which @code{weakref} is attached and that is associated
+with a named @code{target} must be @code{static}.
 
 @end table
 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] document that alias and target must have the same type
  2020-02-14 18:30     ` Martin Sebor
@ 2020-02-15  0:08       ` Sandra Loosemore
  0 siblings, 0 replies; 7+ messages in thread
From: Sandra Loosemore @ 2020-02-15  0:08 UTC (permalink / raw)
  To: Martin Sebor, gcc-patches

On 2/14/20 11:30 AM, Martin Sebor wrote:
>
> [snip]
> Attached
> is the updated revision that I plan to commit unless you (or anyone
> else) have further suggestions.

This version is OK to commit.

-Sandra

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-02-15  0:08 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-05  1:05 [PATCH] document that alias and target must have the same type Martin Sebor
2020-02-05 20:13 ` Martin Sebor
2020-02-12 21:02   ` [PING PATCH] " Martin Sebor
2020-02-13 22:55   ` [PATCH] " Sandra Loosemore
2020-02-14 18:30     ` Martin Sebor
2020-02-15  0:08       ` Sandra Loosemore
2020-02-13 22:45 ` Sandra Loosemore

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).