| 1 |
/* mingw32 host-specific hook definitions. |
| 2 |
Copyright (C) 2004-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GCC. |
| 5 |
|
| 6 |
GCC is free software; you can redistribute it and/or modify it |
| 7 |
under the terms of the GNU General Public License as published |
| 8 |
by the Free Software Foundation; either version 3, or (at your |
| 9 |
option) any later version. |
| 10 |
|
| 11 |
GCC is distributed in the hope that it will be useful, but WITHOUT |
| 12 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 13 |
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 14 |
License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with GCC; see the file COPYING3. If not see |
| 18 |
<http://www.gnu.org/licenses/>. */ |
| 19 |
|
| 20 |
#define IN_TARGET_CODE 1 |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
#include "system.h" |
| 24 |
#include "coretypes.h" |
| 25 |
#include "diagnostic.h" |
| 26 |
#include "hosthooks.h" |
| 27 |
#include "hosthooks-def.h" |
| 28 |
|
| 29 |
|
| 30 |
#define WIN32_LEAN_AND_MEAN /* Not so important if we have windows.h.gch. */ |
| 31 |
#include <windows.h> |
| 32 |
#include <stdlib.h> |
| 33 |
|
| 34 |
static void * mingw32_gt_pch_get_address (size_t, int); |
| 35 |
static int mingw32_gt_pch_use_address (void *&, size_t, int, size_t); |
| 36 |
static size_t mingw32_gt_pch_alloc_granularity (void); |
| 37 |
|
| 38 |
#undef HOST_HOOKS_GT_PCH_GET_ADDRESS |
| 39 |
#define HOST_HOOKS_GT_PCH_GET_ADDRESS mingw32_gt_pch_get_address |
| 40 |
#undef HOST_HOOKS_GT_PCH_USE_ADDRESS |
| 41 |
#define HOST_HOOKS_GT_PCH_USE_ADDRESS mingw32_gt_pch_use_address |
| 42 |
#undef HOST_HOOKS_GT_PCH_ALLOC_GRANULARITY |
| 43 |
#define HOST_HOOKS_GT_PCH_ALLOC_GRANULARITY mingw32_gt_pch_alloc_granularity |
| 44 |
|
| 45 |
static inline void w32_error(const char*, const char*, int, const char*); |
| 46 |
|
| 47 |
/* Granularity for reserving address space. */ |
| 48 |
static size_t va_granularity = 0x10000; |
| 49 |
|
| 50 |
/* Print out the GetLastError() translation. */ |
| 51 |
static inline void |
| 52 |
w32_error (const char* function, const char* file, int line, |
| 53 |
const char* my_msg) |
| 54 |
{ |
| 55 |
LPSTR w32_msgbuf; |
| 56 |
FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER |
| 57 |
| FORMAT_MESSAGE_FROM_SYSTEM |
| 58 |
| FORMAT_MESSAGE_IGNORE_INSERTS |
| 59 |
| FORMAT_MESSAGE_MAX_WIDTH_MASK, |
| 60 |
NULL, GetLastError(), |
| 61 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 62 |
(LPSTR) &w32_msgbuf, 0, NULL); |
| 63 |
fprintf(stderr, "internal error in %s, at %s:%d: %s: %s\n", |
| 64 |
function, trim_filename (file), line, my_msg, w32_msgbuf); |
| 65 |
LocalFree ((HLOCAL)w32_msgbuf); |
| 66 |
} |
| 67 |
|
| 68 |
/* Granularity for reserving address space. */ |
| 69 |
static size_t |
| 70 |
mingw32_gt_pch_alloc_granularity (void) |
| 71 |
{ |
| 72 |
SYSTEM_INFO si; |
| 73 |
|
| 74 |
GetSystemInfo (&si); |
| 75 |
va_granularity = (size_t) si.dwAllocationGranularity; |
| 76 |
|
| 77 |
return va_granularity; |
| 78 |
} |
| 79 |
|
| 80 |
/* Identify an address that's likely to be free in a subsequent invocation |
| 81 |
of the compiler. The area should be able to hold SIZE bytes. FD is an |
| 82 |
open file descriptor if the host would like to probe with mmap. */ |
| 83 |
|
| 84 |
static void * |
| 85 |
mingw32_gt_pch_get_address (size_t size, int) |
| 86 |
{ |
| 87 |
void* res; |
| 88 |
|
| 89 |
/* FIXME: We let system determine base by setting first arg to NULL. |
| 90 |
Allocating at top of available address space avoids unnecessary |
| 91 |
fragmentation of "ordinary" (malloc's) address space but may not |
| 92 |
be safe with delayed load of system dll's. Preferred addresses |
| 93 |
for NT system dlls is in 0x70000000 to 0x78000000 range. |
| 94 |
If we allocate at bottom we need to reserve the address as early |
| 95 |
as possible and at the same point in each invocation. */ |
| 96 |
|
| 97 |
res = VirtualAlloc (NULL, size, |
| 98 |
MEM_RESERVE | MEM_TOP_DOWN, |
| 99 |
PAGE_NOACCESS); |
| 100 |
if (!res) |
| 101 |
w32_error (__FUNCTION__, __FILE__, __LINE__, "VirtualAlloc"); |
| 102 |
else |
| 103 |
/* We do not need the address space for now, so free it. */ |
| 104 |
VirtualFree (res, 0, MEM_RELEASE); |
| 105 |
|
| 106 |
return res; |
| 107 |
} |
| 108 |
|
| 109 |
/* ADDR is an address returned by gt_pch_get_address. Attempt to allocate |
| 110 |
SIZE bytes at the same address and load it with the data from FD at |
| 111 |
OFFSET. Return -1 if we couldn't allocate memory at ADDR, return 0 |
| 112 |
if the memory is allocated but the data not loaded, return 1 if done. */ |
| 113 |
|
| 114 |
static int |
| 115 |
mingw32_gt_pch_use_address (void *&addr, size_t size, int fd, |
| 116 |
size_t offset) |
| 117 |
{ |
| 118 |
void * mmap_addr; |
| 119 |
HANDLE mmap_handle; |
| 120 |
|
| 121 |
/* Apparently, MS Vista puts unnamed file mapping objects into Global |
| 122 |
namespace when running an application in a Terminal Server |
| 123 |
session. This causes failure since, by default, applications |
| 124 |
don't get SeCreateGlobalPrivilege. We don't need global |
| 125 |
memory sharing so explicitly put object into Local namespace. |
| 126 |
|
| 127 |
If multiple concurrent GCC processes are using PCH functionality, |
| 128 |
MapViewOfFileEx returns "Access Denied" error. So we ensure the |
| 129 |
session-wide mapping name is unique by appending process ID. */ |
| 130 |
|
| 131 |
#define OBJECT_NAME_FMT "Local\\MinGWGCCPCH-" |
| 132 |
|
| 133 |
char* object_name = NULL; |
| 134 |
/* However, the documentation for CreateFileMapping says that on NT4 |
| 135 |
and earlier, backslashes are invalid in object name. So, we need |
| 136 |
to check if we are on Windows2000 or higher. */ |
| 137 |
OSVERSIONINFO version_info; |
| 138 |
|
| 139 |
version_info.dwOSVersionInfoSize = sizeof (version_info); |
| 140 |
|
| 141 |
if (size == 0) |
| 142 |
return 0; |
| 143 |
|
| 144 |
/* Offset must be also be a multiple of allocation granularity for |
| 145 |
this to work. We can't change the offset. */ |
| 146 |
if ((offset & (va_granularity - 1)) != 0) |
| 147 |
return -1; |
| 148 |
|
| 149 |
|
| 150 |
/* Determine the version of Windows we are running on and use a |
| 151 |
uniquely-named local object if running > 4. */ |
| 152 |
GetVersionEx (&version_info); |
| 153 |
|
| 154 |
char local_object_name[sizeof (OBJECT_NAME_FMT) + sizeof (DWORD) * 2]; |
| 155 |
if (version_info.dwMajorVersion > 4) |
| 156 |
{ |
| 157 |
snprintf (local_object_name, sizeof (local_object_name), |
| 158 |
OBJECT_NAME_FMT "%lx", GetCurrentProcessId()); |
| 159 |
object_name = local_object_name; |
| 160 |
} |
| 161 |
mmap_handle = CreateFileMappingA ((HANDLE) _get_osfhandle (fd), NULL, |
| 162 |
PAGE_WRITECOPY | SEC_COMMIT, 0, 0, |
| 163 |
object_name); |
| 164 |
|
| 165 |
if (mmap_handle == NULL) |
| 166 |
{ |
| 167 |
w32_error (__FUNCTION__, __FILE__, __LINE__, "CreateFileMapping"); |
| 168 |
return -1; |
| 169 |
} |
| 170 |
|
| 171 |
/* Try mapping the file at `addr`. */ |
| 172 |
mmap_addr = MapViewOfFileEx (mmap_handle, FILE_MAP_COPY, 0, offset, |
| 173 |
size, addr); |
| 174 |
if (mmap_addr == NULL) |
| 175 |
{ |
| 176 |
/* We could not map the file at its original address, so let the |
| 177 |
system choose a different one. The PCH can be relocated later. */ |
| 178 |
mmap_addr = MapViewOfFileEx (mmap_handle, FILE_MAP_COPY, 0, offset, |
| 179 |
size, NULL); |
| 180 |
if (mmap_addr == NULL) |
| 181 |
{ |
| 182 |
w32_error (__FUNCTION__, __FILE__, __LINE__, "MapViewOfFileEx"); |
| 183 |
CloseHandle(mmap_handle); |
| 184 |
return -1; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
addr = mmap_addr; |
| 189 |
return 1; |
| 190 |
} |
| 191 |
|
| 192 |
const struct host_hooks host_hooks = HOST_HOOKS_INITIALIZER; |