On Wednesday 29 May 2024 16:00:06 Martin Storsjö wrote:
> On Sun, 26 May 2024, Pali Rohár wrote:
> 
> > GCC documentation for alias attribute for variables says:
> > 
> >  https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
> >  Using both the alias and the alias target to access the same object is
> >  undefined in a translation unit without a declaration of the alias with
> >  the attribute.
> > 
> > So declare also aliases of import symbols via alias attribute to prevent
> > possible undefined behavior.
> 
> I don't agree that what we have is undefined behaviour here. Note that the
> docs say that this is undefined, only if the alias is accessed without a
> declaration of the alias with the attribute.
> 
> So what we have in our code looks like this:
> 
> void func(void) { /* implementation */ }
> void (*__MINGW_IMP_SYMBOL(func))(void) = func;
> void __attribute__((alias("func"))) altname(void);
> void (*__MINGW_IMP_SYMBOL(altname))(void) = altname;
> 
> If I understand you correctly, you mean that since we're accessing both
> "altname", and "func" in the same translation unit, we have undefined
> behaviour?

I meant that accessing __imp__func and __imp__altname can be UB as they
refers to same function but are not defined as aliases.

But what you wrote below makes more sense.

> The way I read the quote, is that the snippet above is ok, but the following
> would be UB:
> 
> void func(void) { /* implementation */ }
> void (*__MINGW_IMP_SYMBOL(func))(void) = func;
> void altname(void); // Regular declaration of altname
> void (*__MINGW_IMP_SYMBOL(altname))(void) = altname; // Use altname, without 
> alias attribute
> void __attribute__((alias("func"))) altname(void); // Add alias attribute
> 
> 
> > This also reduce memory usage as all aliased symbols declared via
> > __MINGW_IMP_SYMBOL will share same global variable.
> 
> This is true. The end result and aim of the patch probably is good.

Ok. Then it means, it is needed to just rewrite commit message.

> > Note that aliased symbol has to be declared with "extern" keyword even it
> > is not extern and gcc will emit this symbol (as described in documentation).
> 
> I found it hard to understand what you're trying to say here. Is this what
> you're trying to say?
> 
> ---
> Note that when declaring an alias, it is done via a declaration, not via a
> definition. For a variable, this means that it has to be declared with
> "extern". Doing that does produce a symbol, contrary to a non-alias variable
> declaration.
> ---
> I.e. if you don't add the "extern", you run into errors like these:
> 
> alias.c:3:44: error: ‘var_alias’ defined both normally and as ‘alias’
> attribute

Yes, this is exactly what I mean.

Note that I'm not sure even if the "not via a definition" is a correct
wording as specifying just "type var;" in C is something like "tentative
definition" which as I figured out is even more complicated due to gcc's
-fcommon/-fno-common flags.


_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to