在 6/14/21 1:49 PM, Martin Storsjö 写道:
FWIW we have __declspec(selectany) in a number of places in our headers already (which expands to the same), but not in the crt/* headers yet. I don't mind either this or plain __declspec(selectany).
The GCC manual says 'you can use __declspec (selectany) as a synonym for __attribute__ ((selectany)) for compatibility with other compilers'.
Another approach would be __attribute__((__used__)) static int* __mingw_reference_charmax = &mingw_initcharmax; which results in an unused variable in every .o file - probably nobody wants that.
I think I'd rather add two double leading underscores to the mingw_initcharmax symbol too, if we're going to be referencing it from public headers.
I thought this would require an additional patch, and not just for `mingw_initcharmax`, as there are a few more in 'crtexe.c'.
Other than that, this looks ok to me. Should the pointer be made const?
Did you mean `const int*` or `int* const`?In C++ the latter would make it internal linkage (as if it was declared `static`), and the compiler would optimize it out if it wasn't otherwise odr-used, e.g. its address was not taken.
FWIW this way of referencing the variable causes one extra base relocation in executables that include it, as the absolute address needs to be written in the data/rdata section. The MSVC equivalent uses an inline function to reference it, which should generate position independent code - but I'm less sure that we can be sure that such code actually gets emitted, so this form probably is more robust against compilers optimizing it out.
MS header uses an `extern inline` function with C++ semantics, which means an out-of-line copy is always emitted but does not cause multiple definition errors, should multiple such copies are found. The closest thing to this in GCC world is weak symbols... but weren't there known issues with some broken linkers?
This kind of `extern inline` does not exist in GNU C. Actually I haven't tested whether `selectany` works on functions. The GCC manual page I referenced in the commit message merely says `selectany` applies to variables.
-- Best regards, Liu Hao
OpenPGP_signature
Description: OpenPGP digital signature
_______________________________________________ Mingw-w64-public mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
