On Saturday 19 November 2022 11:53:28 Pali Rohár wrote:
> On Saturday 19 November 2022 17:12:12 LIU Hao wrote:
> > 在 2022-11-18 23:40, 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.
> > > 
> > > For compatibility with older GNU LD versions, define __ImageBase as weak
> > > alias to _image_base__ symbol.
> > > 
> > > This allows to remove all #ifdef hacks for older GNU LD versions as with
> > > this change __ImageBase works correctly with all GNU LD versions.
> > > ---
> > 
> > Generally speaking, one shouldn't use weak symbols when targeting Windows. I
> > haven't investigated though, but many times weak symbols result in null or
> > faulty addresses.
> 
> Interesting, I did not know that there are issues with weak symbols. All
> my tests with weak symbols passed, I have not spotted any issue.
> 
> > Thus, weak symbols are unacceptable; but removal of references to
> > `__image_base__` seems fine, if dropping support for binutils 2.18 is
> > acceptable. BTW 2.19 was released in 2008 [1].
> > 
> > [1] https://lists.gnu.org/archive/html/info-gnu/2008-10/msg00014.html
> 
> Another option is to detect at ./configure time if linker accepts
> __ImageBase symbol. And if not then define __ImageBase as normal
> (non-weak) symbol. Should be pretty simple.

Here is that variant without weak symbols. I have tested that it
correctly generate helper symbol __ImageBase for older ld version 2.18.
And for new ld version, file __imagebase.c is not compiled at all.

diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index 2056b344dd3e..28d74c66a581 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -129,6 +129,10 @@ src_libmingw32=include/oscalls.h include/internal.h 
include/sect_attribs.h \
   crt/tlsthrd.c       crt/tlsmthread.c     crt/tlsmcrt.c   \
   crt/cxa_atexit.c    crt/cxa_thread_atexit.c crt/tls_atexit.c
 
+if !LD_PROVIDES_IMAGEBASE
+src_libmingw32+=crt/__imagebase.c
+endif
+
 src_libscrnsave=libsrc/scrnsave.c
 src_libscrnsavw=libsrc/scrnsave.c
 src_libstrmiids=libsrc/strmiids.c
diff --git a/mingw-w64-crt/configure.ac b/mingw-w64-crt/configure.ac
index 5c68ff967ba7..a0db980d7e0e 100644
--- a/mingw-w64-crt/configure.ac
+++ b/mingw-w64-crt/configure.ac
@@ -368,6 +368,12 @@ 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 symbols.
+AC_MSG_CHECKING([whether the linker provides __ImageBase symbol])
+AC_LINK_IFELSE([AC_LANG_PROGRAM([[extern unsigned char __ImageBase[];]], 
[[return __ImageBase[0];]])], [ld_provides_imagebase=yes], 
[ld_provides_imagebase=no])
+AC_MSG_RESULT([$ld_provides_imagebase])
+AM_CONDITIONAL([LD_PROVIDES_IMAGEBASE],[test $ld_provides_imagebase = yes])
+
 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/__imagebase.c b/mingw-w64-crt/crt/__imagebase.c
new file mode 100644
index 000000000000..95715f953a61
--- /dev/null
+++ b/mingw-w64-crt/crt/__imagebase.c
@@ -0,0 +1,18 @@
+/**
+ * This file has no copyright assigned and is placed in the Public Domain.
+ * This file is part of the mingw-w64 runtime package.
+ * No warranty is given; refer to the file DISCLAIMER.PD within this package.
+ */
+#include <_mingw_mac.h>
+
+/* This file defines __ImageBase symbol for backward compatibility with older
+ * linker versions which do not provide this symbol. It is used only when 
linker
+ * does not provide this symbol (detected by ./configure). __ImageBase symbol 
is
+ * defined as alias to _image_base__ symbol which is provided by older linker
+ * versions. Note that gcc does not allow declaring global variable as alias to
+ * external symbol, so do it via inline assembly which allows it. */
+#define ASM_SYM(sym) __MINGW64_STRINGIFY(__MINGW_USYMBOL(sym))
+asm (
+".globl\t" ASM_SYM(__ImageBase) "\n\t"
+".set\t"   ASM_SYM(__ImageBase) "," ASM_SYM(_image_base__)
+);


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

Reply via email to