2 * coree.c: mscoree.dll functions
5 * Kornel Pal <http://www.kornelpal.hu/>
7 * Copyright (C) 2008 Kornel Pal
8 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
17 #include <mono/io-layer/io-layer.h>
18 #include <mono/utils/mono-path.h>
20 #include "metadata-internals.h"
23 #include "domain-internals.h"
24 #include "appdomain.h"
26 #include "object-internals.h"
29 #include "environment.h"
34 HMODULE coree_module_handle
= NULL
;
36 static gboolean init_from_coree
= FALSE
;
39 mono_get_module_file_name (HMODULE module_handle
)
42 gchar
* file_name_utf8
;
47 file_name
= g_new (gunichar2
, buffer_size
);
50 size
= GetModuleFileName (module_handle
, file_name
, buffer_size
);
56 g_assert (size
<= buffer_size
);
57 if (size
!= buffer_size
)
61 file_name
= g_realloc (file_name
, buffer_size
* sizeof (gunichar2
));
64 file_name_utf8
= g_utf16_to_utf8 (file_name
, size
, NULL
, NULL
, NULL
);
67 return file_name_utf8
;
70 /* Entry point called by LdrLoadDll of ntdll.dll after _CorValidateImage. */
71 BOOL STDMETHODCALLTYPE
_CorDllMain(HINSTANCE hInst
, DWORD dwReason
, LPVOID lpReserved
)
73 MonoAssembly
* assembly
;
80 case DLL_PROCESS_ATTACH
:
81 DisableThreadLibraryCalls (hInst
);
83 file_name
= mono_get_module_file_name (hInst
);
85 if (mono_get_root_domain ()) {
86 image
= mono_image_open_from_module_handle (hInst
, mono_path_resolve_symlinks (file_name
), TRUE
, NULL
);
88 init_from_coree
= TRUE
;
89 mono_runtime_load (file_name
, NULL
);
90 error
= (gchar
*) mono_check_corlib_version ();
98 image
= mono_image_open (file_name
, NULL
);
100 image
->has_entry_point
= TRUE
;
101 mono_close_exe_image ();
102 /* Decrement reference count to zero. (Image will not be closed.) */
103 mono_image_close (image
);
113 * FIXME: Find a better way to call mono_image_fixup_vtable. Only
114 * loader trampolines should be used and assembly loading should
115 * probably be delayed until the first call to an exported function.
117 if (image
->tables
[MONO_TABLE_ASSEMBLY
].rows
&& ((MonoCLIImageInfo
*) image
->image_info
)->cli_cli_header
.ch_vtable_fixups
.rva
)
118 assembly
= mono_assembly_open (file_name
, NULL
);
122 case DLL_PROCESS_DETACH
:
123 if (lpReserved
!= NULL
)
124 /* The process is terminating. */
126 file_name
= mono_get_module_file_name (hInst
);
127 image
= mono_image_loaded (file_name
);
129 mono_image_close (image
);
138 /* Called by ntdll.dll reagardless of entry point after _CorValidateImage. */
139 __int32 STDMETHODCALLTYPE
_CorExeMain(void)
143 MonoAssembly
* assembly
;
148 gchar
* corlib_version_error
;
154 file_name
= mono_get_module_file_name (NULL
);
155 init_from_coree
= TRUE
;
156 domain
= mono_runtime_load (file_name
, NULL
);
158 corlib_version_error
= (gchar
*) mono_check_corlib_version ();
159 if (corlib_version_error
) {
160 g_free (corlib_version_error
);
162 MessageBox (NULL
, L
"Corlib not in sync with this runtime.", NULL
, MB_ICONERROR
);
163 mono_runtime_quit ();
167 assembly
= mono_assembly_open (file_name
, NULL
);
168 mono_close_exe_image ();
171 MessageBox (NULL
, L
"Cannot open assembly.", NULL
, MB_ICONERROR
);
172 mono_runtime_quit ();
176 image
= assembly
->image
;
177 entry
= mono_image_get_entry_point (image
);
180 MessageBox (NULL
, L
"Assembly doesn't have an entry point.", NULL
, MB_ICONERROR
);
181 mono_runtime_quit ();
185 method
= mono_get_method_checked (image
, entry
, NULL
, NULL
, &error
);
186 if (method
== NULL
) {
188 mono_error_cleanup (&error
); /* FIXME don't swallow the error */
189 MessageBox (NULL
, L
"The entry point method could not be loaded.", NULL
, MB_ICONERROR
);
190 mono_runtime_quit ();
194 argvw
= CommandLineToArgvW (GetCommandLine (), &argc
);
195 argv
= g_new0 (gchar
*, argc
);
196 argv
[0] = file_name
;
197 for (i
= 1; i
< argc
; ++i
)
198 argv
[i
] = g_utf16_to_utf8 (argvw
[i
], -1, NULL
, NULL
, NULL
);
201 mono_runtime_run_main_checked (method
, argc
, argv
, &error
);
202 mono_error_raise_exception (&error
); /* OK, triggers unhandled exn handler */
203 mono_thread_manage ();
205 mono_runtime_quit ();
207 /* return does not terminate the process. */
208 ExitProcess (mono_environment_exitcode_get ());
211 /* Called by msvcrt.dll when shutting down. */
212 void STDMETHODCALLTYPE
CorExitProcess(int exitCode
)
214 /* FIXME: This is not currently supported by the runtime. */
216 if (mono_get_root_domain () && !mono_runtime_is_shutting_down ()) {
217 mono_runtime_set_shutting_down ();
218 mono_thread_suspend_all_other_threads ();
219 mono_runtime_quit ();
222 ExitProcess (exitCode
);
225 /* Called by ntdll.dll before _CorDllMain and _CorExeMain. */
226 STDAPI
_CorValidateImage(PVOID
*ImageBase
, LPCWSTR FileName
)
228 IMAGE_DOS_HEADER
* DosHeader
;
229 IMAGE_NT_HEADERS32
* NtHeaders32
;
230 IMAGE_DATA_DIRECTORY
* CliHeaderDir
;
232 IMAGE_NT_HEADERS64
* NtHeaders64
;
233 MonoCLIHeader
* CliHeader
;
239 DosHeader
= (IMAGE_DOS_HEADER
*)*ImageBase
;
240 if (DosHeader
->e_magic
!= IMAGE_DOS_SIGNATURE
)
241 return STATUS_INVALID_IMAGE_FORMAT
;
243 NtHeaders32
= (IMAGE_NT_HEADERS32
*)((DWORD_PTR
)DosHeader
+ DosHeader
->e_lfanew
);
244 if (NtHeaders32
->Signature
!= IMAGE_NT_SIGNATURE
)
245 return STATUS_INVALID_IMAGE_FORMAT
;
248 NtHeaders64
= (IMAGE_NT_HEADERS64
*)NtHeaders32
;
249 if (NtHeaders64
->OptionalHeader
.Magic
== IMAGE_NT_OPTIONAL_HDR64_MAGIC
)
251 if (NtHeaders64
->OptionalHeader
.NumberOfRvaAndSizes
<= IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
)
252 return STATUS_INVALID_IMAGE_FORMAT
;
254 CliHeaderDir
= &NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
];
255 if (!CliHeaderDir
->VirtualAddress
)
256 return STATUS_INVALID_IMAGE_FORMAT
;
258 CliHeader
= (MonoCLIHeader
*)((DWORD_PTR
)DosHeader
+ CliHeaderDir
->VirtualAddress
);
259 if (CliHeader
->ch_flags
& CLI_FLAGS_32BITREQUIRED
)
260 return STATUS_INVALID_IMAGE_FORMAT
;
262 if (CliHeader
->ch_flags
& CLI_FLAGS_ILONLY
)
264 /* Avoid calling _CorDllMain because imports are not resolved for IL only images. */
265 if (NtHeaders64
->OptionalHeader
.AddressOfEntryPoint
!= 0)
267 Address
= &NtHeaders64
->OptionalHeader
.AddressOfEntryPoint
;
268 if (!VirtualProtect(Address
, sizeof(DWORD
), PAGE_READWRITE
, &OldProtect
))
271 if (!VirtualProtect(Address
, sizeof(DWORD
), OldProtect
, &OldProtect
))
276 return STATUS_SUCCESS
;
279 if (NtHeaders32
->OptionalHeader
.Magic
!= IMAGE_NT_OPTIONAL_HDR32_MAGIC
)
280 return STATUS_INVALID_IMAGE_FORMAT
;
282 if (NtHeaders32
->OptionalHeader
.NumberOfRvaAndSizes
<= IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
)
283 return STATUS_INVALID_IMAGE_FORMAT
;
285 CliHeaderDir
= &NtHeaders32
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
];
286 if (!CliHeaderDir
->VirtualAddress
)
287 return STATUS_INVALID_IMAGE_FORMAT
;
289 CliHeader
= (MonoCLIHeader
*)((DWORD_PTR
)DosHeader
+ CliHeaderDir
->VirtualAddress
);
290 if (!(CliHeader
->ch_flags
& CLI_FLAGS_ILONLY
) || (CliHeader
->ch_flags
& CLI_FLAGS_32BITREQUIRED
))
291 return STATUS_INVALID_IMAGE_FORMAT
;
293 /* Fixup IMAGE_NT_HEADERS32 to IMAGE_NT_HEADERS64. */
294 SizeOfHeaders
= NtHeaders32
->OptionalHeader
.SizeOfHeaders
;
295 if (SizeOfHeaders
< DosHeader
->e_lfanew
+ sizeof(IMAGE_NT_HEADERS64
) + (sizeof(IMAGE_SECTION_HEADER
) * NtHeaders32
->FileHeader
.NumberOfSections
))
296 return STATUS_INVALID_IMAGE_FORMAT
;
298 if (!VirtualProtect(DosHeader
, SizeOfHeaders
, PAGE_READWRITE
, &OldProtect
))
301 memmove(NtHeaders64
+ 1, IMAGE_FIRST_SECTION(NtHeaders32
), sizeof(IMAGE_SECTION_HEADER
) * NtHeaders32
->FileHeader
.NumberOfSections
);
303 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_NUMBEROF_DIRECTORY_ENTRIES
- 1].Size
= 0;
304 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_NUMBEROF_DIRECTORY_ENTRIES
- 1].VirtualAddress
= 0;
305 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
].Size
= NtHeaders32
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
].Size
;
306 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
].VirtualAddress
= NtHeaders32
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
].VirtualAddress
;
307 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT
].Size
= 0;
308 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT
].VirtualAddress
= 0;
309 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_IAT
].Size
= 0;
310 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_IAT
].VirtualAddress
= 0;
311 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT
].Size
= 0;
312 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT
].VirtualAddress
= 0;
313 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG
].Size
= 0;
314 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG
].VirtualAddress
= 0;
315 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_TLS
].Size
= 0;
316 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_TLS
].VirtualAddress
= 0;
317 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_GLOBALPTR
].Size
= 0;
318 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_GLOBALPTR
].VirtualAddress
= 0;
319 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_ARCHITECTURE
].Size
= 0;
320 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_ARCHITECTURE
].VirtualAddress
= 0;
321 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_DEBUG
].Size
= NtHeaders32
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_DEBUG
].Size
;
322 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
= NtHeaders32
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
;
323 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
= NtHeaders32
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
;
324 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
= NtHeaders32
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
;
325 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
= NtHeaders32
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
;
326 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_SECURITY
].VirtualAddress
= NtHeaders32
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_SECURITY
].VirtualAddress
;
327 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_EXCEPTION
].Size
= 0;
328 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_EXCEPTION
].VirtualAddress
= 0;
329 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
= NtHeaders32
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
;
330 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
= NtHeaders32
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
;
331 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_IMPORT
].Size
= 0;
332 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_IMPORT
].VirtualAddress
= 0;
333 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_EXPORT
].Size
= 0;
334 NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_EXPORT
].VirtualAddress
= 0;
336 NtHeaders64
->OptionalHeader
.NumberOfRvaAndSizes
= IMAGE_NUMBEROF_DIRECTORY_ENTRIES
;
337 NtHeaders64
->OptionalHeader
.LoaderFlags
= NtHeaders32
->OptionalHeader
.LoaderFlags
;
338 NtHeaders64
->OptionalHeader
.SizeOfHeapCommit
= (ULONGLONG
)NtHeaders32
->OptionalHeader
.SizeOfHeapCommit
;
339 NtHeaders64
->OptionalHeader
.SizeOfHeapReserve
= (ULONGLONG
)NtHeaders32
->OptionalHeader
.SizeOfHeapReserve
;
340 NtHeaders64
->OptionalHeader
.SizeOfStackCommit
= (ULONGLONG
)NtHeaders32
->OptionalHeader
.SizeOfStackCommit
;
341 NtHeaders64
->OptionalHeader
.SizeOfStackReserve
= (ULONGLONG
)NtHeaders32
->OptionalHeader
.SizeOfStackReserve
;
342 NtHeaders64
->OptionalHeader
.DllCharacteristics
= NtHeaders32
->OptionalHeader
.DllCharacteristics
;
343 NtHeaders64
->OptionalHeader
.Subsystem
= NtHeaders32
->OptionalHeader
.Subsystem
;
344 NtHeaders64
->OptionalHeader
.CheckSum
= NtHeaders32
->OptionalHeader
.CheckSum
;
345 NtHeaders64
->OptionalHeader
.SizeOfHeaders
= NtHeaders32
->OptionalHeader
.SizeOfHeaders
;
346 NtHeaders64
->OptionalHeader
.SizeOfImage
= NtHeaders32
->OptionalHeader
.SizeOfImage
;
347 NtHeaders64
->OptionalHeader
.Win32VersionValue
= NtHeaders32
->OptionalHeader
.Win32VersionValue
;
348 NtHeaders64
->OptionalHeader
.MinorSubsystemVersion
= NtHeaders32
->OptionalHeader
.MinorSubsystemVersion
;
349 NtHeaders64
->OptionalHeader
.MajorSubsystemVersion
= NtHeaders32
->OptionalHeader
.MajorSubsystemVersion
;
350 NtHeaders64
->OptionalHeader
.MinorImageVersion
= NtHeaders32
->OptionalHeader
.MinorImageVersion
;
351 NtHeaders64
->OptionalHeader
.MajorImageVersion
= NtHeaders32
->OptionalHeader
.MajorImageVersion
;
352 NtHeaders64
->OptionalHeader
.MinorOperatingSystemVersion
= NtHeaders32
->OptionalHeader
.MinorOperatingSystemVersion
;
353 NtHeaders64
->OptionalHeader
.MajorOperatingSystemVersion
= NtHeaders32
->OptionalHeader
.MajorOperatingSystemVersion
;
354 NtHeaders64
->OptionalHeader
.FileAlignment
= NtHeaders32
->OptionalHeader
.FileAlignment
;
355 NtHeaders64
->OptionalHeader
.SectionAlignment
= NtHeaders32
->OptionalHeader
.SectionAlignment
;
356 NtHeaders64
->OptionalHeader
.ImageBase
= (ULONGLONG
)NtHeaders32
->OptionalHeader
.ImageBase
;
357 /* BaseOfCode is at the same offset. */
358 NtHeaders64
->OptionalHeader
.AddressOfEntryPoint
= 0;
359 NtHeaders64
->OptionalHeader
.Magic
= IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
360 NtHeaders64
->FileHeader
.SizeOfOptionalHeader
= sizeof(IMAGE_OPTIONAL_HEADER64
);
362 if (!VirtualProtect(DosHeader
, SizeOfHeaders
, OldProtect
, &OldProtect
))
365 if (NtHeaders32
->OptionalHeader
.Magic
!= IMAGE_NT_OPTIONAL_HDR32_MAGIC
)
366 return STATUS_INVALID_IMAGE_FORMAT
;
368 if (NtHeaders32
->OptionalHeader
.NumberOfRvaAndSizes
<= IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
)
369 return STATUS_INVALID_IMAGE_FORMAT
;
371 CliHeaderDir
= &NtHeaders32
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
];
372 if (!CliHeaderDir
->VirtualAddress
)
373 return STATUS_INVALID_IMAGE_FORMAT
;
375 Address
= &NtHeaders32
->OptionalHeader
.AddressOfEntryPoint
;
376 if (!VirtualProtect(Address
, sizeof(DWORD
), PAGE_READWRITE
, &OldProtect
))
378 if (NtHeaders32
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
379 *Address
= (DWORD
)((DWORD_PTR
)&_CorDllMain
- (DWORD_PTR
)DosHeader
);
381 *Address
= (DWORD
)((DWORD_PTR
)&_CorExeMain
- (DWORD_PTR
)DosHeader
);
382 if (!VirtualProtect(Address
, sizeof(DWORD
), OldProtect
, &OldProtect
))
386 return STATUS_SUCCESS
;
389 /* Called by ntdll.dll. */
390 STDAPI_(VOID
) _CorImageUnloading(PVOID ImageBase
)
395 STDAPI
CorBindToRuntimeEx(LPCWSTR pwszVersion
, LPCWSTR pwszBuildFlavor
, DWORD startupFlags
, REFCLSID rclsid
, REFIID riid
, LPVOID FAR
*ppv
)
404 STDAPI
CorBindToRuntime(LPCWSTR pwszVersion
, LPCWSTR pwszBuildFlavor
, REFCLSID rclsid
, REFIID riid
, LPVOID FAR
*ppv
)
406 return CorBindToRuntimeEx (pwszVersion
, pwszBuildFlavor
, 0, rclsid
, riid
, ppv
);
409 HMODULE WINAPI
MonoLoadImage(LPCWSTR FileName
)
414 IMAGE_DOS_HEADER
* DosHeader
;
415 IMAGE_NT_HEADERS32
* NtHeaders32
;
417 IMAGE_NT_HEADERS64
* NtHeaders64
;
419 HMODULE ModuleHandle
;
421 FileHandle
= CreateFile(FileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
422 if (FileHandle
== INVALID_HANDLE_VALUE
)
425 FileSize
= GetFileSize(FileHandle
, NULL
);
426 if (FileSize
== INVALID_FILE_SIZE
)
429 MapHandle
= CreateFileMapping(FileHandle
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
430 if (MapHandle
== NULL
)
433 DosHeader
= (IMAGE_DOS_HEADER
*)MapViewOfFile(MapHandle
, FILE_MAP_READ
, 0, 0, 0);
434 if (DosHeader
== NULL
)
437 if (FileSize
< sizeof(IMAGE_DOS_HEADER
) || DosHeader
->e_magic
!= IMAGE_DOS_SIGNATURE
|| FileSize
< DosHeader
->e_lfanew
+ sizeof(IMAGE_NT_HEADERS32
))
438 goto InvalidImageFormat
;
440 NtHeaders32
= (IMAGE_NT_HEADERS32
*)((DWORD_PTR
)DosHeader
+ DosHeader
->e_lfanew
);
441 if (NtHeaders32
->Signature
!= IMAGE_NT_SIGNATURE
)
442 goto InvalidImageFormat
;
445 NtHeaders64
= (IMAGE_NT_HEADERS64
*)NtHeaders32
;
446 if (NtHeaders64
->OptionalHeader
.Magic
== IMAGE_NT_OPTIONAL_HDR64_MAGIC
)
448 if (FileSize
< DosHeader
->e_lfanew
+ sizeof(IMAGE_NT_HEADERS64
) ||
449 NtHeaders64
->OptionalHeader
.NumberOfRvaAndSizes
<= IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
||
450 !NtHeaders64
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
].VirtualAddress
)
451 goto InvalidImageFormat
;
457 if (NtHeaders32
->OptionalHeader
.Magic
!= IMAGE_NT_OPTIONAL_HDR32_MAGIC
||
458 NtHeaders32
->OptionalHeader
.NumberOfRvaAndSizes
<= IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
||
459 !NtHeaders32
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
].VirtualAddress
)
462 SetLastError(STATUS_INVALID_IMAGE_FORMAT
);
469 UnmapViewOfFile(DosHeader
);
470 CloseHandle(MapHandle
);
472 ModuleHandle
= LoadLibrary(FileName
);
474 CloseHandle(FileHandle
);
478 UnmapViewOfFile(DosHeader
);
480 CloseHandle(MapHandle
);
482 CloseHandle(FileHandle
);
486 typedef struct _EXPORT_FIXUP
493 BYTE Bytes
[sizeof(PVOID
)];
495 PLABEL_DESCRIPTOR
* PLabel
;
500 /* Has to be binary ordered. */
501 static const EXPORT_FIXUP ExportFixups
[] = {
502 {"CorBindToRuntime", &CorBindToRuntime
},
503 {"CorBindToRuntimeEx", &CorBindToRuntimeEx
},
504 {"CorExitProcess", &CorExitProcess
},
505 {"_CorDllMain", &_CorDllMain
},
506 {"_CorExeMain", &_CorExeMain
},
507 {"_CorImageUnloading", &_CorImageUnloading
},
508 {"_CorValidateImage", &_CorValidateImage
},
512 #define EXPORT_FIXUP_COUNT (sizeof(ExportFixups) / sizeof(EXPORT_FIXUP) - 1)
514 static HMODULE ExportFixupModuleHandle
= NULL
;
515 static DWORD ExportFixupRvas
[EXPORT_FIXUP_COUNT
];
517 /* Fixup exported functions of mscoree.dll to our implementations. */
518 STDAPI
MonoFixupCorEE(HMODULE ModuleHandle
)
520 IMAGE_DOS_HEADER
* DosHeader
;
521 IMAGE_NT_HEADERS
* NtHeaders
;
522 IMAGE_DATA_DIRECTORY
* ExportDataDir
;
523 IMAGE_EXPORT_DIRECTORY
* ExportDir
;
527 EXPORT_FIXUP
* ExportFixup
;
528 DWORD
* ExportFixupRva
;
535 MEMORY_BASIC_INFORMATION MemoryInfo
;
540 PLABEL_DESCRIPTOR
* PLabel
;
542 #define ELEMENT_SIZE sizeof(PLABEL_DESCRIPTOR)
543 #define REGION_WRITE_PROTECT PAGE_READWRITE
544 #define REGION_PROTECT PAGE_READ
548 #define ELEMENT_SIZE 13
549 #define REGION_WRITE_PROTECT PAGE_EXECUTE_READWRITE
550 #define REGION_PROTECT PAGE_EXECUTE_READ
554 if (ExportFixupModuleHandle
!= NULL
)
555 return ModuleHandle
== ExportFixupModuleHandle
? S_OK
: E_FAIL
;
557 DosHeader
= (IMAGE_DOS_HEADER
*)ModuleHandle
;
558 if (DosHeader
== NULL
)
561 if (DosHeader
->e_magic
!= IMAGE_DOS_SIGNATURE
)
564 NtHeaders
= (IMAGE_NT_HEADERS
*)((DWORD_PTR
)DosHeader
+ DosHeader
->e_lfanew
);
565 if (NtHeaders
->Signature
!= IMAGE_NT_SIGNATURE
)
568 if (NtHeaders
->OptionalHeader
.Magic
!= IMAGE_NT_OPTIONAL_HDR_MAGIC
)
571 if (NtHeaders
->OptionalHeader
.NumberOfRvaAndSizes
<= IMAGE_DIRECTORY_ENTRY_EXPORT
)
574 ExportDataDir
= &NtHeaders
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_EXPORT
];
575 if (!ExportDataDir
->VirtualAddress
)
579 /* Allocate memory after base address because RVAs are 32-bit unsigned integers. */
580 RegionBase
= DosHeader
;
581 MaxRegionBase
= (PVOID
)((DWORD_PTR
)RegionBase
+ (DWORD_PTR
)(0x100000000L
- (ELEMENT_SIZE
* (EXPORT_FIXUP_COUNT
- 1))));
584 if (!VirtualQuery(RegionBase
, &MemoryInfo
, sizeof(MEMORY_BASIC_INFORMATION
)))
586 if (MemoryInfo
.State
== MEM_FREE
&& MemoryInfo
.RegionSize
>= ELEMENT_SIZE
* EXPORT_FIXUP_COUNT
)
588 Region
= VirtualAlloc(RegionBase
, ELEMENT_SIZE
* EXPORT_FIXUP_COUNT
, MEM_COMMIT
| MEM_RESERVE
, REGION_WRITE_PROTECT
);
592 RegionBase
= (PVOID
)((DWORD_PTR
)MemoryInfo
.BaseAddress
+ (DWORD_PTR
)MemoryInfo
.RegionSize
);
593 if (RegionBase
> MaxRegionBase
)
594 return E_OUTOFMEMORY
;
598 PLabel
= (PLABEL_DESCRIPTOR
*)Region
;
600 Trampoline
= (BYTE
*)Region
;
604 ExportDir
= (IMAGE_EXPORT_DIRECTORY
*)((DWORD_PTR
)DosHeader
+ ExportDataDir
->VirtualAddress
);
605 Functions
= (DWORD
*)((DWORD_PTR
)DosHeader
+ ExportDir
->AddressOfFunctions
);
606 Names
= (DWORD
*)((DWORD_PTR
)DosHeader
+ ExportDir
->AddressOfNames
);
607 NameOrdinals
= (WORD
*)((DWORD_PTR
)DosHeader
+ ExportDir
->AddressOfNameOrdinals
);
608 ExportFixup
= (EXPORT_FIXUP
*)&ExportFixups
;
609 ExportFixupRva
= (DWORD
*)&ExportFixupRvas
;
611 for (i
= 0; i
< ExportDir
->NumberOfNames
; i
++)
613 cmp
= strcmp((LPCSTR
)((DWORD_PTR
)DosHeader
+ Names
[i
]), ExportFixup
->Name
);
621 ProcRva
= (DWORD
)((DWORD_PTR
)PLabel
- (DWORD_PTR
)DosHeader
);
622 *(PLabel
)++ = *ExportFixup
->ProcAddress
.PLabel
;
623 #elif defined(_M_AMD64)
624 ProcRva
= (DWORD
)((DWORD_PTR
)Trampoline
- (DWORD_PTR
)DosHeader
);
625 /* mov r11, ExportFixup->ProcAddress */
626 *(Trampoline
)++ = 0x49;
627 *(Trampoline
)++ = 0xBB;
628 *(Trampoline
)++ = ExportFixup
->ProcAddress
.Bytes
[0];
629 *(Trampoline
)++ = ExportFixup
->ProcAddress
.Bytes
[1];
630 *(Trampoline
)++ = ExportFixup
->ProcAddress
.Bytes
[2];
631 *(Trampoline
)++ = ExportFixup
->ProcAddress
.Bytes
[3];
632 *(Trampoline
)++ = ExportFixup
->ProcAddress
.Bytes
[4];
633 *(Trampoline
)++ = ExportFixup
->ProcAddress
.Bytes
[5];
634 *(Trampoline
)++ = ExportFixup
->ProcAddress
.Bytes
[6];
635 *(Trampoline
)++ = ExportFixup
->ProcAddress
.Bytes
[7];
637 *(Trampoline
)++ = 0x41;
638 *(Trampoline
)++ = 0xFF;
639 *(Trampoline
)++ = 0xE3;
641 #error Unsupported architecture.
644 ProcRva
= (DWORD
)(ExportFixup
->ProcAddress
.DWordPtr
- (DWORD_PTR
)DosHeader
);
646 Address
= &Functions
[NameOrdinals
[i
]];
647 if (!VirtualProtect(Address
, sizeof(DWORD
), PAGE_READWRITE
, &OldProtect
))
649 *ExportFixupRva
= *Address
;
651 if (!VirtualProtect(Address
, sizeof(DWORD
), OldProtect
, &OldProtect
))
654 if (ExportFixup
->Name
== NULL
) {
656 if (!VirtualProtect(Region
, ELEMENT_SIZE
* EXPORT_FIXUP_COUNT
, REGION_PROTECT
, &OldProtect
))
660 ExportFixupModuleHandle
= ModuleHandle
;
669 /* Executable images are only mapped by the OS loader. We need to do fixups for native code support. */
670 STDAPI
MonoFixupExe(HMODULE ModuleHandle
)
672 IMAGE_DOS_HEADER
* DosHeader
;
673 IMAGE_NT_HEADERS
* NtHeaders
;
678 DosHeader
= (IMAGE_DOS_HEADER
*)ModuleHandle
;
679 if (DosHeader
== NULL
)
682 if (DosHeader
->e_magic
!= IMAGE_DOS_SIGNATURE
)
685 NtHeaders
= (IMAGE_NT_HEADERS
*)((DWORD_PTR
)DosHeader
+ DosHeader
->e_lfanew
);
686 if (NtHeaders
->Signature
!= IMAGE_NT_SIGNATURE
)
689 if (NtHeaders
->OptionalHeader
.Magic
!= IMAGE_NT_OPTIONAL_HDR_MAGIC
)
692 if (NtHeaders
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
695 if (NtHeaders
->OptionalHeader
.NumberOfRvaAndSizes
> IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
)
697 IMAGE_DATA_DIRECTORY
* CliHeaderDir
;
698 MonoCLIHeader
* CliHeader
;
700 CliHeaderDir
= &NtHeaders
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
];
701 if (CliHeaderDir
->VirtualAddress
)
703 CliHeader
= (MonoCLIHeader
*)((DWORD_PTR
)DosHeader
+ CliHeaderDir
->VirtualAddress
);
704 if (CliHeader
->ch_flags
& CLI_FLAGS_ILONLY
)
709 BaseDiff
= (DWORD_PTR
)DosHeader
- NtHeaders
->OptionalHeader
.ImageBase
;
712 if (NtHeaders
->FileHeader
.Characteristics
& IMAGE_FILE_RELOCS_STRIPPED
)
715 Address
= &NtHeaders
->OptionalHeader
.ImageBase
;
716 if (!VirtualProtect(Address
, sizeof(DWORD_PTR
), PAGE_READWRITE
, &OldProtect
))
718 *Address
= (DWORD_PTR
)DosHeader
;
719 if (!VirtualProtect(Address
, sizeof(DWORD_PTR
), OldProtect
, &OldProtect
))
722 if (NtHeaders
->OptionalHeader
.NumberOfRvaAndSizes
> IMAGE_DIRECTORY_ENTRY_BASERELOC
)
724 IMAGE_DATA_DIRECTORY
* BaseRelocDir
;
725 IMAGE_BASE_RELOCATION
* BaseReloc
;
728 ULONG RelocBlockSize
;
730 DWORD_PTR UNALIGNED
*RelocFixup
;
732 BaseRelocDir
= &NtHeaders
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
733 if (BaseRelocDir
->VirtualAddress
)
735 BaseReloc
= (IMAGE_BASE_RELOCATION
*)((DWORD_PTR
)DosHeader
+ BaseRelocDir
->VirtualAddress
);
736 BaseRelocSize
= BaseRelocDir
->Size
;
738 while (BaseRelocSize
)
740 RelocBlockSize
= BaseReloc
->SizeOfBlock
;
742 if (!RelocBlockSize
|| BaseRelocSize
< RelocBlockSize
)
745 BaseRelocSize
-= RelocBlockSize
;
746 RelocBlock
= (USHORT
*)((DWORD_PTR
)BaseReloc
+ sizeof(IMAGE_BASE_RELOCATION
));
747 RelocBlockSize
-= sizeof(IMAGE_BASE_RELOCATION
);
748 RelocBlockSize
/= sizeof(USHORT
);
750 while (RelocBlockSize
-- != 0)
752 RelocOffset
= *RelocBlock
& (USHORT
)0x0fff;
753 RelocFixup
= (DWORD_PTR
*)((DWORD_PTR
)DosHeader
+ BaseReloc
->VirtualAddress
+ RelocOffset
);
755 switch (*RelocBlock
>> 12)
757 case IMAGE_REL_BASED_ABSOLUTE
:
761 case IMAGE_REL_BASED_DIR64
:
763 case IMAGE_REL_BASED_HIGHLOW
:
765 if (!VirtualProtect(RelocFixup
, sizeof(DWORD_PTR
), PAGE_EXECUTE_READWRITE
, &OldProtect
))
767 *RelocFixup
+= BaseDiff
;
768 if (!VirtualProtect(RelocFixup
, sizeof(DWORD_PTR
), OldProtect
, &OldProtect
))
778 BaseReloc
= (IMAGE_BASE_RELOCATION
*)RelocBlock
;
784 if (NtHeaders
->OptionalHeader
.NumberOfRvaAndSizes
> IMAGE_DIRECTORY_ENTRY_IMPORT
)
786 IMAGE_DATA_DIRECTORY
* ImportDir
;
787 IMAGE_IMPORT_DESCRIPTOR
* ImportDesc
;
788 HMODULE ImportModuleHandle
;
789 IMAGE_THUNK_DATA
* ImportThunkData
;
790 DWORD_PTR ProcAddress
;
792 ImportDir
= &NtHeaders
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_IMPORT
];
793 if (ImportDir
->VirtualAddress
!= 0)
795 ImportDesc
= (IMAGE_IMPORT_DESCRIPTOR
*)((DWORD_PTR
)DosHeader
+ ImportDir
->VirtualAddress
);
796 while (ImportDesc
->Name
&& ImportDesc
->OriginalFirstThunk
)
798 ImportModuleHandle
= LoadLibraryA((PCSTR
)((DWORD_PTR
)DosHeader
+ ImportDesc
->Name
));
799 if (ImportModuleHandle
== NULL
)
802 ImportThunkData
= (IMAGE_THUNK_DATA
*)((DWORD_PTR
)DosHeader
+ ImportDesc
->OriginalFirstThunk
);
803 while (ImportThunkData
->u1
.Ordinal
!= 0)
805 if (IMAGE_SNAP_BY_ORDINAL(ImportThunkData
->u1
.Ordinal
))
806 ProcAddress
= (DWORD_PTR
)GetProcAddress(ImportModuleHandle
, (LPCSTR
)IMAGE_ORDINAL(ImportThunkData
->u1
.Ordinal
));
809 IMAGE_IMPORT_BY_NAME
* ImportByName
= (IMAGE_IMPORT_BY_NAME
*)((DWORD_PTR
)DosHeader
+ ImportThunkData
->u1
.AddressOfData
);
810 ProcAddress
= (DWORD_PTR
)GetProcAddress(ImportModuleHandle
, ImportByName
->Name
);
812 if (ProcAddress
== 0)
814 Address
= (DWORD_PTR
*)((DWORD_PTR
)ImportThunkData
- ImportDesc
->OriginalFirstThunk
+ ImportDesc
->FirstThunk
);
815 if (!VirtualProtect(Address
, sizeof(DWORD_PTR
), PAGE_READWRITE
, &OldProtect
))
817 *Address
= ProcAddress
;
818 if (!VirtualProtect(Address
, sizeof(DWORD_PTR
), OldProtect
, &OldProtect
))
832 mono_set_act_ctx (const char* file_name
)
834 typedef HANDLE (WINAPI
* CREATEACTCTXW_PROC
) (PCACTCTXW pActCtx
);
835 typedef BOOL (WINAPI
* ACTIVATEACTCTX_PROC
) (HANDLE hActCtx
, ULONG_PTR
* lpCookie
);
837 HMODULE kernel32_handle
;
838 CREATEACTCTXW_PROC CreateActCtx_proc
;
839 ACTIVATEACTCTX_PROC ActivateActCtx_proc
;
841 gunichar2
* full_path_utf16
;
843 gunichar2
* dir_name_utf16
;
845 gunichar2
* base_name_utf16
;
850 kernel32_handle
= GetModuleHandle (L
"kernel32.dll");
851 if (!kernel32_handle
)
853 CreateActCtx_proc
= (CREATEACTCTXW_PROC
) GetProcAddress (kernel32_handle
, "CreateActCtxW");
854 if (!CreateActCtx_proc
)
856 ActivateActCtx_proc
= (ACTIVATEACTCTX_PROC
) GetProcAddress (kernel32_handle
, "ActivateActCtx");
857 if (!ActivateActCtx_proc
)
860 full_path
= mono_path_canonicalize (file_name
);
861 full_path_utf16
= g_utf8_to_utf16 (full_path
, -1, NULL
, NULL
, NULL
);
862 dir_name
= g_path_get_dirname (full_path
);
863 dir_name_utf16
= g_utf8_to_utf16 (dir_name
, -1, NULL
, NULL
, NULL
);
864 base_name
= g_path_get_basename (full_path
);
865 base_name_utf16
= g_utf8_to_utf16 (base_name
, -1, NULL
, NULL
, NULL
);
870 memset (&act_ctx
, 0, sizeof (ACTCTX
));
871 act_ctx
.cbSize
= sizeof (ACTCTX
);
872 act_ctx
.dwFlags
= ACTCTX_FLAG_SET_PROCESS_DEFAULT
| ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID
| ACTCTX_FLAG_RESOURCE_NAME_VALID
| ACTCTX_FLAG_APPLICATION_NAME_VALID
;
873 act_ctx
.lpSource
= full_path_utf16
;
874 act_ctx
.lpAssemblyDirectory
= dir_name_utf16
;
875 act_ctx
.lpResourceName
= MAKEINTRESOURCE (CREATEPROCESS_MANIFEST_RESOURCE_ID
);
876 act_ctx
.lpApplicationName
= base_name_utf16
;
878 handle
= CreateActCtx_proc (&act_ctx
);
879 if (handle
== INVALID_HANDLE_VALUE
&& GetLastError () == ERROR_SXS_PROCESS_DEFAULT_ALREADY_SET
) {
880 act_ctx
.dwFlags
&= ~ACTCTX_FLAG_SET_PROCESS_DEFAULT
;
881 handle
= CreateActCtx_proc (&act_ctx
);
884 g_free (base_name_utf16
);
885 g_free (dir_name_utf16
);
886 g_free (full_path_utf16
);
888 if (handle
!= INVALID_HANDLE_VALUE
)
889 ActivateActCtx_proc (handle
, &cookie
);
893 mono_load_coree (const char* exe_file_name
)
895 HMODULE module_handle
;
896 gunichar2
* file_name
;
900 if (coree_module_handle
)
903 if (!init_from_coree
&& exe_file_name
)
904 mono_set_act_ctx (exe_file_name
);
906 /* ntdll.dll loads mscoree.dll from the system32 directory. */
907 required_size
= GetSystemDirectory (NULL
, 0);
908 file_name
= g_new (gunichar2
, required_size
+ 12);
909 size
= GetSystemDirectory (file_name
, required_size
);
910 g_assert (size
< required_size
);
911 if (file_name
[size
- 1] != L
'\\')
912 file_name
[size
++] = L
'\\';
913 memcpy (&file_name
[size
], L
"mscoree.dll", 12 * sizeof (gunichar2
));
915 module_handle
= LoadLibrary (file_name
);
918 if (module_handle
&& !SUCCEEDED (MonoFixupCorEE (module_handle
))) {
919 FreeLibrary (module_handle
);
920 module_handle
= NULL
;
923 coree_module_handle
= module_handle
;
927 mono_fixup_exe_image (MonoImage
* image
)
929 if (!init_from_coree
&& image
&& image
->is_module_handle
)
930 MonoFixupExe ((HMODULE
) image
->raw_data
);
933 #endif /* HOST_WIN32 */