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" >&AS_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 
>&AS_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);
 #define __initenv (* __MINGW_IMP_SYMBOL(__initenv))
 #endif
 
-/* Hack, for bug in ld.  Will be removed soon.  */
-#if defined(__GNUC__)
-#define __ImageBase __MINGW_LSYMBOL(_image_base__)
-#endif
-/* This symbol is defined by ld.  */
 extern IMAGE_DOS_HEADER __ImageBase;
 
 extern void _fpreset (void);
diff --git a/mingw-w64-crt/crt/crtexewin.c b/mingw-w64-crt/crt/crtexewin.c
index 33ac1979e331..26179fa905bb 100644
--- a/mingw-w64-crt/crt/crtexewin.c
+++ b/mingw-w64-crt/crt/crtexewin.c
@@ -14,9 +14,6 @@
 #define SPACECHAR _T(' ')
 #define DQUOTECHAR _T('\"')
 
-#if defined(__GNUC__)
-#define __ImageBase __MINGW_LSYMBOL(_image_base__)
-#endif
 extern IMAGE_DOS_HEADER __ImageBase;
 
 int _tmain (int, _TCHAR **, _TCHAR **);
diff --git a/mingw-w64-crt/crt/pesect.c b/mingw-w64-crt/crt/pesect.c
index 28c34f8caddb..b7f0b181097e 100644
--- a/mingw-w64-crt/crt/pesect.c
+++ b/mingw-w64-crt/crt/pesect.c
@@ -7,16 +7,7 @@
 #include <windows.h>
 #include <string.h>
 
-#if defined (_WIN64) && defined (__ia64__)
-#error FIXME: Unsupported __ImageBase implementation.
-#else
-#ifdef __GNUC__
-/* Hack, for bug in ld.  Will be removed soon.  */
-#define __ImageBase __MINGW_LSYMBOL(_image_base__)
-#endif
-/* This symbol is defined by the linker.  */
 extern IMAGE_DOS_HEADER __ImageBase;
-#endif
 
 WINBOOL _ValidateImageBase (PBYTE);
 
diff --git a/mingw-w64-crt/crt/pseudo-reloc.c b/mingw-w64-crt/crt/pseudo-reloc.c
index a0e667b50463..d6eb089d461e 100644
--- a/mingw-w64-crt/crt/pseudo-reloc.c
+++ b/mingw-w64-crt/crt/pseudo-reloc.c
@@ -48,7 +48,7 @@
 
 extern char __RUNTIME_PSEUDO_RELOC_LIST__;
 extern char __RUNTIME_PSEUDO_RELOC_LIST_END__;
-extern IMAGE_DOS_HEADER __MINGW_LSYMBOL(_image_base__);
+extern IMAGE_DOS_HEADER __ImageBase;
 
 void _pei386_runtime_relocator (void);
 
@@ -500,11 +500,7 @@ _pei386_runtime_relocator (void)
 
   do_pseudo_reloc (&__RUNTIME_PSEUDO_RELOC_LIST__,
                   &__RUNTIME_PSEUDO_RELOC_LIST_END__,
-#ifdef __GNUC__
-                  &__MINGW_LSYMBOL(_image_base__)
-#else
                   &__ImageBase
-#endif
                   );
 #ifdef __MINGW64_VERSION_MAJOR
   restore_modified_sections ();
diff --git a/mingw-w64-crt/libsrc/dloadhelper.c 
b/mingw-w64-crt/libsrc/dloadhelper.c
index ce3a57914b92..85c1a1130157 100644
--- a/mingw-w64-crt/libsrc/dloadhelper.c
+++ b/mingw-w64-crt/libsrc/dloadhelper.c
@@ -15,7 +15,6 @@
  */
 typedef DWORD NTSTATUS;
 
-#define __ImageBase __MINGW_LSYMBOL(_image_base__)
 extern IMAGE_DOS_HEADER __ImageBase;
 
 /* this typedef is missing from the Windows SDK header, but is present in
diff --git a/mingw-w64-crt/misc/delayimp.c b/mingw-w64-crt/misc/delayimp.c
index 26afde8079ae..ca4b51ad161d 100644
--- a/mingw-w64-crt/misc/delayimp.c
+++ b/mingw-w64-crt/misc/delayimp.c
@@ -47,7 +47,6 @@ static unsigned IndexFromPImgThunkData(PCImgThunkData 
pitdCur,PCImgThunkData pit
   return (unsigned) (pitdCur - pitdBase);
 }
 
-#define __ImageBase __MINGW_LSYMBOL(_image_base__)
 extern IMAGE_DOS_HEADER __ImageBase;
 
 #define PtrFromRVA(RVA)   (((PBYTE)&__ImageBase) + (RVA))
-- 
2.20.1



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

Reply via email to