1  /* Operating system specific defines to be used when targeting GCC for
       2     hosting on Windows32, using GNU tools and the Windows32 API Library.
       3     Copyright (C) 1997-2023 Free Software Foundation, Inc.
       4  
       5  This file is part of GCC.
       6  
       7  GCC is free software; you can redistribute it and/or modify
       8  it under the terms of the GNU General Public License as published by
       9  the Free Software Foundation; either version 3, or (at your option)
      10  any later version.
      11  
      12  GCC is distributed in the hope that it will be useful,
      13  but WITHOUT ANY WARRANTY; without even the implied warranty of
      14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15  GNU General Public License for more details.
      16  
      17  You should have received a copy of the GNU General Public License
      18  along with GCC; see the file COPYING3.  If not see
      19  <http://www.gnu.org/licenses/>.  */
      20  
      21  #undef DEFAULT_ABI
      22  #define DEFAULT_ABI MS_ABI
      23  
      24  /* By default, target has a 80387, uses IEEE compatible arithmetic,
      25     returns float values in the 387 and needs stack probes.
      26     We also align doubles to 64-bits for MSVC default compatibility.
      27     Additionally we enable MS_BITFIELD_LAYOUT by default.  */
      28  
      29  #undef TARGET_SUBTARGET_DEFAULT
      30  #define TARGET_SUBTARGET_DEFAULT \
      31  	(MASK_80387 | MASK_IEEE_FP | MASK_FLOAT_RETURNS \
      32  	 | MASK_STACK_PROBE | MASK_ALIGN_DOUBLE \
      33  	 | MASK_MS_BITFIELD_LAYOUT)
      34  
      35  #ifndef TARGET_USING_MCFGTHREAD
      36  #define TARGET_USING_MCFGTHREAD  0
      37  #endif
      38  
      39  /* See i386/crtdll.h for an alternative definition. _INTEGRAL_MAX_BITS
      40     is for compatibility with native compiler.  */
      41  #define EXTRA_OS_CPP_BUILTINS()					\
      42    do								\
      43      {								\
      44        builtin_define ("__MSVCRT__");				\
      45        builtin_define ("__MINGW32__");			   	\
      46        builtin_define ("_WIN32");				\
      47        builtin_define_std ("WIN32");				\
      48        builtin_define_std ("WINNT");				\
      49        builtin_define_with_int_value ("_INTEGRAL_MAX_BITS",	\
      50  				     TYPE_PRECISION (intmax_type_node));\
      51        if (TARGET_64BIT && ix86_abi == MS_ABI)			\
      52  	{							\
      53  	  builtin_define ("__MINGW64__");			\
      54  	  builtin_define_std ("WIN64");				\
      55  	  builtin_define ("_WIN64");				\
      56  	}							\
      57        if (TARGET_USING_MCFGTHREAD)				\
      58  	builtin_define ("__USING_MCFGTHREAD__");		\
      59      }								\
      60    while (0)
      61  
      62  #ifndef TARGET_USE_PTHREAD_BY_DEFAULT
      63  #define SPEC_PTHREAD1 "pthread"
      64  #define SPEC_PTHREAD2 "!no-pthread"
      65  #else
      66  #define SPEC_PTHREAD1 "!no-pthread"
      67  #define SPEC_PTHREAD2 "pthread"
      68  #endif
      69  
      70  #undef SUB_LINK_ENTRY32
      71  #undef SUB_LINK_ENTRY64
      72  #define SUB_LINK_ENTRY32 "-e _DllMainCRTStartup@12"
      73  #if defined(USE_MINGW64_LEADING_UNDERSCORES)
      74  #define SUB_LINK_ENTRY64 "-e _DllMainCRTStartup"
      75  #else
      76  #define SUB_LINK_ENTRY64 "-e DllMainCRTStartup"
      77  #endif
      78  
      79  #undef SUB_LINK_ENTRY
      80  #if TARGET_64BIT_DEFAULT
      81  #define SUB_LINK_ENTRY SUB_LINK_ENTRY64
      82  #else
      83  #define SUB_LINK_ENTRY SUB_LINK_ENTRY32
      84  #endif
      85  
      86  #undef NATIVE_SYSTEM_HEADER_COMPONENT
      87  #define NATIVE_SYSTEM_HEADER_COMPONENT "MINGW"
      88  
      89  #undef CPP_SPEC
      90  #define CPP_SPEC "%{posix:-D_POSIX_SOURCE} %{mthreads:-D_MT} " \
      91  		 "%{" SPEC_PTHREAD1 ":-D_REENTRANT} " \
      92  		 "%{" SPEC_PTHREAD2 ": } "
      93  
      94  /* For Windows applications, include more libraries, but always include
      95     kernel32.  */
      96  #undef LIB_SPEC
      97  #define LIB_SPEC "%{pg:-lgmon} %{" SPEC_PTHREAD1 ":-lpthread} " \
      98  		 "%{" SPEC_PTHREAD2 ": } " \
      99  		 "%{mwindows:-lgdi32 -lcomdlg32} " \
     100       "%{fvtable-verify=preinit:-lvtv -lpsapi; \
     101          fvtable-verify=std:-lvtv -lpsapi} " \
     102                   "-ladvapi32 -lshell32 -luser32 -lkernel32"
     103  
     104  /* Weak symbols do not get resolved if using a Windows dll import lib.
     105     Make the unwind registration references strong undefs.  */
     106  #if DWARF2_UNWIND_INFO
     107  /* DW2-unwind is just available for 32-bit mode.  */
     108  #if TARGET_64BIT_DEFAULT
     109  #define SHARED_LIBGCC_UNDEFS_SPEC \
     110    "%{m32: %{shared-libgcc: -u ___register_frame_info -u ___deregister_frame_info}}"
     111  #else
     112  #define SHARED_LIBGCC_UNDEFS_SPEC \
     113   "%{shared-libgcc: -u ___register_frame_info -u ___deregister_frame_info}"
     114  #endif
     115  #else
     116  #define SHARED_LIBGCC_UNDEFS_SPEC ""
     117  #endif
     118  
     119  #undef  SUBTARGET_EXTRA_SPECS
     120  #define SUBTARGET_EXTRA_SPECS						\
     121    { "shared_libgcc_undefs", SHARED_LIBGCC_UNDEFS_SPEC }
     122  
     123  #if ! MINGW_DEFAULT_LARGE_ADDR_AWARE
     124  /* This is used without --enable-large-address-aware.  */
     125  # define LINK_SPEC_LARGE_ADDR_AWARE ""
     126  #elif ! TARGET_BI_ARCH
     127  /* This is used on i686-pc-mingw32 with --enable-large-address-aware.  */
     128  # define LINK_SPEC_LARGE_ADDR_AWARE \
     129    "%{!shared:%{!mdll:--large-address-aware}}"
     130  #elif TARGET_64BIT_DEFAULT
     131  /* This is used on x86_64-pc-mingw32 with --enable-large-address-aware.
     132     ??? It probably doesn't work, because the linker emulation defaults
     133     to i386pep, the 64-bit mode that does not support
     134     --large-address-aware, and x86_64-pc-mingw32 does not override the
     135     emulation to i386pe for -m32, unlike x86_64-w64-mingw32.  */
     136  # define LINK_SPEC_LARGE_ADDR_AWARE \
     137    "%{!shared:%{!mdll:%{m32:--large-address-aware}}}"
     138  #else
     139  /* This would only be used if someone introduced a biarch
     140     configuration that defaulted to 32-bit.  */
     141  # define LINK_SPEC_LARGE_ADDR_AWARE \
     142    "%{!shared:%{!mdll:%{!m64:--large-address-aware}}}"
     143  #endif
     144  
     145  #if HAVE_LD_PE_DISABLE_DYNAMICBASE
     146  # define LINK_SPEC_DISABLE_DYNAMICBASE \
     147    "%{!shared:%{!mdll:%{no-pie:--disable-dynamicbase}}}"
     148  #else
     149  # define LINK_SPEC_DISABLE_DYNAMICBASE ""
     150  #endif
     151  
     152  #define LINK_SPEC "%{mwindows:--subsystem windows} \
     153    %{mconsole:--subsystem console} \
     154    %{shared: %{mdll: %eshared and mdll are not compatible}} \
     155    %{shared: --shared} %{mdll:--dll} \
     156    %{static:-Bstatic} %{!static:-Bdynamic} \
     157    %{shared|mdll: " SUB_LINK_ENTRY " --enable-auto-image-base} \
     158    " LINK_SPEC_LARGE_ADDR_AWARE "\
     159    " LINK_SPEC_DISABLE_DYNAMICBASE "\
     160    %(shared_libgcc_undefs)"
     161  
     162  /* Include in the mingw32 libraries with libgcc */
     163  #ifdef ENABLE_SHARED_LIBGCC
     164  #define SHARED_LIBGCC_SPEC " \
     165   %{static|static-libgcc:-lgcc -lgcc_eh} \
     166   %{!static: \
     167     %{!static-libgcc: \
     168       %{!shared: \
     169         %{!shared-libgcc:-lgcc -lgcc_eh} \
     170         %{shared-libgcc:-lgcc_s -lgcc} \
     171        } \
     172       %{shared:-lgcc_s -lgcc} \
     173      } \
     174    } "
     175  #else
     176  #define SHARED_LIBGCC_SPEC " -lgcc "
     177  #endif
     178  #if TARGET_USING_MCFGTHREAD
     179  #define MCFGTHREAD_SPEC  " -lmcfgthread -lkernel32 -lntdll "
     180  #else
     181  #define MCFGTHREAD_SPEC  ""
     182  #endif
     183  #undef REAL_LIBGCC_SPEC
     184  #define REAL_LIBGCC_SPEC \
     185    "%{mthreads:-lmingwthrd} -lmingw32 \
     186     " SHARED_LIBGCC_SPEC " \
     187     -lmoldname -lmingwex -lmsvcrt -lkernel32 " MCFGTHREAD_SPEC
     188  
     189  #undef STARTFILE_SPEC
     190  #define STARTFILE_SPEC "%{shared|mdll:dllcrt2%O%s} \
     191    %{!shared:%{!mdll:crt2%O%s}} %{pg:gcrt2%O%s} \
     192    crtbegin.o%s \
     193    %{fvtable-verify=none:%s; \
     194      fvtable-verify=preinit:vtv_start.o%s; \
     195      fvtable-verify=std:vtv_start.o%s}"
     196  
     197  #undef ENDFILE_SPEC
     198  #define ENDFILE_SPEC \
     199    "%{mdaz-ftz:crtfastmath.o%s;Ofast|ffast-math|funsafe-math-optimizations:%{!shared:%{!mno-daz-ftz:crtfastmath.o%s}}} \
     200     %{!shared:%:if-exists(default-manifest.o%s)}\
     201     %{fvtable-verify=none:%s; \
     202      fvtable-verify=preinit:vtv_end.o%s; \
     203      fvtable-verify=std:vtv_end.o%s} \
     204    crtend.o%s"
     205  
     206  /* Override startfile prefix defaults.  */
     207  #ifndef STANDARD_STARTFILE_PREFIX_1
     208  #define STANDARD_STARTFILE_PREFIX_1 "/mingw/lib/"
     209  #endif
     210  #ifndef STANDARD_STARTFILE_PREFIX_2
     211  #define STANDARD_STARTFILE_PREFIX_2 ""
     212  #endif
     213  
     214  /* For native mingw-version we need to take care that NATIVE_SYSTEM_HEADER_DIR
     215     macro contains POSIX-style path.  See bug 52947.  */
     216  #undef NATIVE_SYSTEM_HEADER_DIR
     217  #define NATIVE_SYSTEM_HEADER_DIR "/mingw/include"
     218  
     219  /* Output STRING, a string representing a filename, to FILE.
     220     We canonicalize it to be in Unix format (backslashes are replaced
     221     forward slashes.  */
     222  #undef OUTPUT_QUOTED_STRING
     223  #define OUTPUT_QUOTED_STRING(FILE, STRING)               \
     224  do {						         \
     225    const char *_string = (const char *) (STRING);	 \
     226    char c;					         \
     227  						         \
     228    putc ('\"', (FILE));				         \
     229  						         \
     230    while ((c = *_string++) != 0)			         \
     231      {						         \
     232        if (c == '\\')				         \
     233  	c = '/';				         \
     234  						         \
     235        if (ISPRINT (c))                                   \
     236          {                                                \
     237            if (c == '\"')			         \
     238  	    putc ('\\', (FILE));		         \
     239            putc (c, (FILE));			         \
     240          }                                                \
     241        else                                               \
     242          fprintf ((FILE), "\\%03o", (unsigned char) c);	 \
     243      }						         \
     244  						         \
     245    putc ('\"', (FILE));					 \
     246  } while (0)
     247  
     248  /* Define as short unsigned for compatibility with MS runtime.  */
     249  #undef WINT_TYPE
     250  #define WINT_TYPE "short unsigned int"
     251  
     252  /* mingw32 uses the  -mthreads option to enable thread support.  */
     253  #undef GOMP_SELF_SPECS
     254  #define GOMP_SELF_SPECS "%{fopenacc|fopenmp|%:gt(%{ftree-parallelize-loops=*:%*} 1): " \
     255  			"-mthreads -pthread}"
     256  #undef GTM_SELF_SPECS
     257  #define GTM_SELF_SPECS "%{fgnu-tm:-mthreads -pthread}"
     258  
     259  /* mingw32 atexit function is safe to use in shared libraries.  Use it
     260     to register C++ static destructors.  */
     261  #define TARGET_CXX_USE_ATEXIT_FOR_CXA_ATEXIT hook_bool_void_true
     262  
     263  /* Contains a pointer to type target_ovr_attr defining the target specific
     264     overrides of format attributes.  See c-format.h for structure
     265     definition.  */
     266  #undef TARGET_OVERRIDES_FORMAT_ATTRIBUTES
     267  #define TARGET_OVERRIDES_FORMAT_ATTRIBUTES mingw_format_attribute_overrides
     268  
     269  /* Specify the count of elements in TARGET_OVERRIDES_ATTRIBUTE.  */
     270  #undef TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT
     271  #define TARGET_OVERRIDES_FORMAT_ATTRIBUTES_COUNT 3
     272  
     273  /* Custom initialization for warning -Wpedantic-ms-format for c-format.  */
     274  #undef TARGET_OVERRIDES_FORMAT_INIT
     275  #define TARGET_OVERRIDES_FORMAT_INIT msformat_init
     276  
     277  /* MS specific format attributes for ms_printf, ms_scanf, ms_strftime.  */
     278  #undef TARGET_FORMAT_TYPES
     279  #define TARGET_FORMAT_TYPES mingw_format_attributes
     280  
     281  #undef TARGET_N_FORMAT_TYPES
     282  #define TARGET_N_FORMAT_TYPES 3
     283  
     284  #define HAVE_ENABLE_EXECUTE_STACK
     285  #undef  CHECK_EXECUTE_STACK_ENABLED
     286  #define CHECK_EXECUTE_STACK_ENABLED flag_setstackexecutable
     287  
     288  /* This matches SHLIB_SONAME and SHLIB_SOVERSION in t-cygming. */
     289  /* This matches SHLIB_SONAME and SHLIB_SOVERSION in t-cygwin. */
     290  #if DWARF2_UNWIND_INFO
     291  #define LIBGCC_EH_EXTN "_dw2"
     292  #else
     293  #define LIBGCC_EH_EXTN "_sjlj"
     294  #endif
     295  #define LIBGCC_SONAME "libgcc_s" LIBGCC_EH_EXTN "-1.dll"
     296