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?

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.

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


// Martin

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

Reply via email to