[Mingw-w64-public] [PATCH v3] crt: Use only __ImageBase symbol in mingw-w64 code

2023-04-22 Thread Pali Rohár
GNU LD provides __ImageBase symbol since 2.19. This is standard windows
symbol and should be used instead of custom gnu _image_base__ symbol.

Detect non-existence of __ImageBase symbol at ./configure time by code done
by Martin Storsjö in commit eeea20fccc42b30988b49a63413b62fa21cb33a2. And
for compatibility with older GNU LD versions, define __ImageBase as
preprocessor macro for crt which expands to _image_base__ symbol name.

This allows to remove all #ifdef hacks for older GNU LD versions as with
this change __ImageBase works correctly with all GNU LD versions.

With ld_provides_imagebase=no is i686-w64-mingw32 object file crt1u.o
referncing symbol __image_base__ instead of ___ImageBase.
---
 mingw-w64-crt/Makefile.am  |  2 +-
 mingw-w64-crt/configure.ac | 49 ++
 mingw-w64-crt/crt/crtexe.c |  5 ---
 mingw-w64-crt/crt/crtexewin.c  |  3 --
 mingw-w64-crt/crt/pesect.c |  9 --
 mingw-w64-crt/crt/pseudo-reloc.c   |  6 +---
 mingw-w64-crt/libsrc/dloadhelper.c |  1 -
 mingw-w64-crt/misc/delayimp.c  |  1 -
 8 files changed, 51 insertions(+), 25 deletions(-)

diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index 3cf7203e9296..2b7cec5855d6 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -20,7 +20,7 @@ else
 endif
 
 AM_CPPFLAGS=$(sysincludes)
-AM_CFLAGS=-pipe -std=gnu99 -D_CRTBLD -D_WIN32_WINNT=0x0f00 
-D__MSVCRT_VERSION__=0x700 -D__USE_MINGW_ANSI_STDIO=0 @CFGUARD_CFLAGS@ 
@ADD_C_CXX_WARNING_FLAGS@ @ADD_C_ONLY_WARNING_FLAGS@
+AM_CFLAGS=-pipe -std=gnu99 -D_CRTBLD -D_WIN32_WINNT=0x0f00 
-D__MSVCRT_VERSION__=0x700 -D__USE_MINGW_ANSI_STDIO=0 @IMAGEBASE_CFLAGS@ 
@CFGUARD_CFLAGS@ @ADD_C_CXX_WARNING_FLAGS@ @ADD_C_ONLY_WARNING_FLAGS@
 AM_CXXFLAGS=@ADD_C_CXX_WARNING_FLAGS@ @ADD_CXX_ONLY_WARNING_FLAGS@
 CPPFLAGSARM32=-mfpu=vfpv3
 CPPFLAGS32=-m32 -masm=att
diff --git a/mingw-w64-crt/configure.ac b/mingw-w64-crt/configure.ac
index 5c68ff967ba7..05f40b261e33 100644
--- a/mingw-w64-crt/configure.ac
+++ b/mingw-w64-crt/configure.ac
@@ -368,6 +368,55 @@ AC_MSG_RESULT([$enable_tests_unicode])
 #AC_FUNC_VPRINTF
 #AC_CHECK_FUNCS([alarm atexit btowc fesetround floor ftruncate gettimeofday 
isascii localeconv mbrlen memmove memset pow rint setlocale sqrt strcasecmp 
strchr strncasecmp strtoull strtoumax])
 
+# Check for __ImageBase linker symbol.
+AC_MSG_CHECKING([whether the linker provides __ImageBase symbol])
+# Run this test manually instead of wrapping it in AC_LINK_IFELSE; when
+# bootstrapping an environment, an earlier linking test will have failed,
+# which both causes autoconf to refuse to run any linker test at all,
+# and even if $ac_no_link is overridden, the linker test iteslf will explicitly
+# check that the linker actually produced output in the given output file.
+# If $ac_exeext is empty, as it is when the earlier linker test failed, gcc
+# will behave differently depending on version. If run with "gcc conftest.c
+# -o conftest", old versions will produce explicitly a file named "conftest",
+# while modern GCC versions will produce "conftest.exe". AC_LINK_IFELSE will
+# explicitly look for the output file named "conftest$ac_exeext", which isn't
+# found, and the test fails even though linking succeeded.
+#
+# Therefore, just do a manual test; run the linking command and check the 
return
+# code whether it was successful or not.
+#
+# This test uses both mainCRTStartup and main functions, to let lld deduce
+# entry point and subsystem automatically without having to manually specify,
+# anything. And as long as main() is provided, we need to implicitly provide
+# __main as well, since the compiler injects a call to it.
+
+cat <<_ACEOF >conftest.$ac_ext
+extern unsigned char __ImageBase[[]];
+void __main(void) {
+}
+int main(void) {
+  return __ImageBase[[0]];
+}
+int mainCRTStartup(void) {
+  return main();
+}
+_ACEOF
+
+echo "$as_me:$LINENO: $CC conftest.$ac_ext $LDFLAGS -nostdlib -o 
conftest$ac_exeext" >_MESSAGE_LOG_FD
+# Doing the link test with -nosdlib, to avoid trying to link in any libraries
+# which might not exist yet at this point.
+if $CC conftest.$ac_ext $LDFLAGS -nostdlib -o conftest$ac_exeext 
>_MESSAGE_LOG_FD 2>&1; then
+  ld_provides_imagebase=yes
+else
+  ld_provides_imagebase=no
+fi
+rm -f conftest$ac_exeext conftest.$ac_ext
+AC_MSG_RESULT([$ld_provides_imagebase])
+AS_CASE([$ld_provides_imagebase],
+  
[no],[AS_VAR_SET([IMAGEBASE_CFLAGS],["-D__ImageBase=__MINGW_LSYMBOL\(_image_base__\)"])],
+  [AS_VAR_SET([IMAGEBASE_CFLAGS])])
+AC_SUBST([IMAGEBASE_CFLAGS])
+
 AC_CHECK_HEADER([_mingw_mac.h], [], [AC_MSG_ERROR([Please check if the 
mingw-w64 header set and the build/host option are set properly.])])
 
 #Warnings and errors, default level is 3
diff --git a/mingw-w64-crt/crt/crtexe.c b/mingw-w64-crt/crt/crtexe.c
index e4a180a3afbd..03bda5912025 100644
--- a/mingw-w64-crt/crt/crtexe.c
+++ b/mingw-w64-crt/crt/crtexe.c
@@ -35,11 +35,6 @@ extern char *** __MINGW_IMP_SYMBOL(__initenv);
 

Re: [Mingw-w64-public] [PATCH] crt: Remove conversion functions between UTF-{16, 32} characters from MSVCRT

2023-04-22 Thread Alvin Wong via Mingw-w64-public
Since there had been no other responses, I will add my opinion here: I 
am fine with removing these functions for msvcrt, only if mingw-w64 will 
never reimplement these functions in the future (i.e. will reject 
patches that add them). It seems very tricky to implement them correctly 
with msvcrt's definition of mbstate_t, anyway.


On 22/4/2023 20:46, LIU Hao wrote:

在 2023-03-30 16:38, LIU Hao 写道:
Ping on this patch. A blank line got deleted by accident. I can fix 
that locally.


Removal of stuff could be bad, but given that `mbstoc16()` has never 
been working, it might not be a big loss.




Ping again? It has been almost a month about this patch.




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



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


Re: [Mingw-w64-public] [PATCH] crt: Use only __ImageBase symbol in mingw-w64 code

2023-04-22 Thread Pali Rohár
On Saturday 22 April 2023 20:56:26 LIU Hao wrote:
> 在 2023-04-22 20:01, Pali Rohár 写道:
> > Now I have looked at it again... what about just for mingw-w64-crt build
> > add following CFLAG -D__ImageBase=__MINGW_LSYMBOL(_image_base__)
> > 
> > It would work only for mingw-w64-crt build, needs just configure.ac and
> > Makefile.am adjustment, and does not need any hack in header files or
> > source code files.
> 
> I think it may be a nice solution. Modern binutils has both defined, so
> source code should reference `__ImageBase` and leave the hack in CFLAGS.
> 
> There are actually dedicated CFLAGS for individual targets, so for
> i686-w64-mingw32 this'd be `-D__ImageBase=_image_base__`, and for others
> it'd be `-D__ImageBase=__image_base__`.

I think that there is no need to set individual target CFLAGS.
Macros are expanded at the time of their use so adding just
-D__ImageBase=__MINGW_LSYMBOL(_image_base__) should work
if source file which uses __ImageBase includes appropriate header
file where is __MINGW_LSYMBOL defined (which are already).


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


Re: [Mingw-w64-public] Why does the version in rpcndr.h not match with WinSDK?

2023-04-22 Thread LIU Hao

在 2023-04-22 02:53, Biswapriyo Nath 写道:

There seems to be a version/macro mismatch in rpcndr.h file.
* WinSDK: __RPCNDR_H_VERSION__ = 501
* wine: __RPCNDR_H_VERSION__ = 500
* mingw-w64: __RPCNDR_H_VERSION__ = 475

Could anyone please explain that version mismatch? Is it possible to
make things similar by any means? Is that lower version in mingw-w64
for backward compatibility?

Asking for this https://github.com/microsoft/DirectX-Headers/pull/93


475 was what got checked in since the very first commit. I don't know how this value is supposed to 
be useful; maybe it should be bumped. Windows SDK 7.1, 8.0 and 8.1 all have this as 500. I don't 
have any older versions at hand.



--
Best regards,
LIU Hao



OpenPGP_signature
Description: OpenPGP digital signature
___
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public


Re: [Mingw-w64-public] [PATCH] crt: Use only __ImageBase symbol in mingw-w64 code

2023-04-22 Thread LIU Hao

在 2023-04-22 20:01, Pali Rohár 写道:

Now I have looked at it again... what about just for mingw-w64-crt build
add following CFLAG -D__ImageBase=__MINGW_LSYMBOL(_image_base__)

It would work only for mingw-w64-crt build, needs just configure.ac and
Makefile.am adjustment, and does not need any hack in header files or
source code files.


I think it may be a nice solution. Modern binutils has both defined, so source code should reference 
`__ImageBase` and leave the hack in CFLAGS.


There are actually dedicated CFLAGS for individual targets, so for i686-w64-mingw32 this'd be 
`-D__ImageBase=_image_base__`, and for others it'd be `-D__ImageBase=__image_base__`.




--
Best regards,
LIU Hao



OpenPGP_signature
Description: OpenPGP digital signature
___
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public


Re: [Mingw-w64-public] [PATCH] crt: Remove conversion functions between UTF-{16, 32} characters from MSVCRT

2023-04-22 Thread LIU Hao

在 2023-03-30 16:38, LIU Hao 写道:

Ping on this patch. A blank line got deleted by accident. I can fix that 
locally.

Removal of stuff could be bad, but given that `mbstoc16()` has never been working, it might not be a 
big loss.




Ping again? It has been almost a month about this patch.


--
Best regards,
LIU Hao



OpenPGP_signature
Description: OpenPGP digital signature
___
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public


Re: [Mingw-w64-public] [PATCH] crt: Use only __ImageBase symbol in mingw-w64 code

2023-04-22 Thread Pali Rohár
On Thursday 29 December 2022 15:14:53 Martin Storsjö wrote:
> On Thu, 29 Dec 2022, Pali Rohár wrote:
> 
> > Putting extern IMAGE_DOS_HEADER __ImageBase __asm__("__image_base__");
> > into header file can cause issues for applications which define in their
> > own code extern char __ImageBase[]; as types do not match.
> 
> I didn't mean putting it in a public header, only within some header
> internal to mingw-w64-crt. And I didn't mean putting the whole thing in a
> header, only something like this:
> 
> #ifdef IMAGE_BASE_MISSING
> #define IMAGE_BASE_ALIAS __asm__("__image_base__")
> #else
> #define IMAGE_BASE_ALIAS
> #endif
> 
> Then everywhere within mingw-w64-crt, where we declare __ImageBase, we'd
> change it into this:
> 
> extern IMAGE_DOS_HEADER __ImageBase IMAGE_BASE_ALIAS;
> 
> 
> > Anyway, is there any issue if following code is put into some header file?
> > 
> >  #define __MINGW64_ASM_SYM(sym) __MINGW64_STRINGIFY(__MINGW_USYMBOL(sym))
> >  #ifdef IMAGE_BASE_MISSING
> >  asm (".set\t" __MINGW64_ASM_SYM(__ImageBase) "," 
> > __MINGW64_ASM_SYM(_image_base__));
> >  #endif
> 
> Only within mingw-w64-crt's own internal headers - not within public
> installed headers. The whole configure check shouldn't be something that
> needs to be handled outside of the build of mingw-w64-crt itself.
> 
> > It should make just local symbol alias visible only in translation unit
> > and does not export any symbol. And also does not define any extern C
> > variable.
> 
> I guess such a workaround would be fine too.
> 
> However I'm a bit hesitant to have this header always inject this extra
> asm() bit (into essentially all mingw-w64-crt sources, or at least all ones
> that include that particular internal header).
> 
> Then again, the alternative is to have a macro that expands to the asm()
> bit, and that litters the source files even more (needing to remember to add
> IMAGE_BASE_ALIAS in all the files that need it), littering the source files
> with something that is a workaround for an old ld version.
> 
> So maybe that solution is the cleanest in the end...
> 
> // Martin

Now I have looked at it again... what about just for mingw-w64-crt build
add following CFLAG -D__ImageBase=__MINGW_LSYMBOL(_image_base__)

It would work only for mingw-w64-crt build, needs just configure.ac and
Makefile.am adjustment, and does not need any hack in header files or
source code files.


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