On 3.09.2024 23:15, Pali Rohár wrote:
On Wednesday 28 August 2024 15:51:46 Jacek Caban wrote:
On 27.08.2024 23:35, Pali Rohár wrote:
On Tuesday 27 August 2024 23:20:23 Jacek Caban wrote:
On 27.08.2024 21:16, Pali Rohár wrote:
+/* define just import symbol, function itself is in crtexe.c and crtdll.c 
files */
+int (__cdecl *__MINGW_IMP_SYMBOL(atexit))(void (__cdecl *)(void)) = atexit;
The patch looks mostly good to me, but do we need this __imp_ symbol at all?
We already declare it without dllimport in the header.


Thanks,

Jacek
I thought that it is a good idea to provide __imp_ symbol for every
function which is originally available in DLL library. Reason is that it
is not required to use declarations from mingw-w64 header files and it is
possible to define own function declarations (with dllimport attribute)
or use object files compiled by msvc compiler.

When writing own declaration of function which is in external DLL
library, it is common to mark function as dllimport. And "atexit"
function looks like it can be in external DLL library.

In any case, this __imp_ symbol is in separate object file in .a
library, so should not cause any issues if the __imp_ symbol is not used
at all. Linker should not include it into final binary if is unused.

I think this is a step backwards from UCRT point of view. Right now if user
makes such a mistake, it gets diagnostics from the linker (an error with
binutils or a warning with LLD), while with this patch it will be silently
accepted.
Hm... it is correct that binutils linker in this throws a fatal error?

Anyway, I have tried to compile this simple program with msvc

   __declspec(dllimport) int atexit(void (*function)(void));
   static void func(void) {}
   int main() { atexit(func); return 0; }

and cl/link successfully generated exe file. After inspecting import
table, I see that "_crt_atexit" is used. Which means that dllimported
atexit function was transformed into _crt_atexit symbol.

So if we want for gcc/ld to allow compiling code which also msvc accepts
then we need this atexit imp symbol for compatibility.


It works because when linker finds an unresolved __imp_ symbol and has a defined regular symbol, it synthesizes __imp_ symbol on the fly. Your test case generates a warning on MSVC:

LINK : warning LNK4217: symbol 'atexit' defined in 'MSVCRT.lib(utility.obj)' is imported by 'test.obj' in function 'main'

and on LLD:

ld.lld: warning: /tmp/test-6a16cd.o: locally defined symbol imported: atexit (defined in /opt/llvm-mingw/x86_64-w64-mingw32/lib/crt2.o) [LNK4217]

If we'd provide the symbol then there would be no warning.


It's an error only with binutils, which lacks that feature.


Jacek



_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to