2009-07-30 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / metadata / coree.c
index b7af801ac43b69e6752b55621671c003bbdd8754..ab879b9a6a883cabf91fe541452d42c3a933a64e 100644 (file)
@@ -252,6 +252,24 @@ STDAPI _CorValidateImage(PVOID *ImageBase, LPCWSTR FileName)
                if (!CliHeaderDir->VirtualAddress)
                        return STATUS_INVALID_IMAGE_FORMAT;
 
+               CliHeader = (MonoCLIHeader*)((DWORD_PTR)DosHeader + CliHeaderDir->VirtualAddress);
+               if (CliHeader->ch_flags & CLI_FLAGS_32BITREQUIRED)
+                       return STATUS_INVALID_IMAGE_FORMAT;
+
+               if (CliHeader->ch_flags & CLI_FLAGS_ILONLY)
+               {
+                       /* Avoid calling _CorDllMain because imports are not resolved for IL only images. */
+                       if (NtHeaders64->OptionalHeader.AddressOfEntryPoint != 0)
+                       {
+                               Address = &NtHeaders64->OptionalHeader.AddressOfEntryPoint;
+                               if (!VirtualProtect(Address, sizeof(DWORD), PAGE_READWRITE, &OldProtect))
+                                       return E_UNEXPECTED;
+                               *Address = (DWORD)0;
+                               if (!VirtualProtect(Address, sizeof(DWORD), OldProtect, &OldProtect))
+                                       return E_UNEXPECTED;
+                       }
+               }
+
                return STATUS_SUCCESS;
        }
 
@@ -718,8 +736,8 @@ STDAPI MonoFixupExe(HMODULE ModuleHandle)
                                                return E_FAIL;
 
                                        BaseRelocSize -= RelocBlockSize;
-                                       RelocBlock = (USHORT*)((DWORD_PTR)BaseReloc + IMAGE_SIZEOF_BASE_RELOCATION);
-                                       RelocBlockSize -= IMAGE_SIZEOF_BASE_RELOCATION;
+                                       RelocBlock = (USHORT*)((DWORD_PTR)BaseReloc + sizeof(IMAGE_BASE_RELOCATION));
+                                       RelocBlockSize -= sizeof(IMAGE_BASE_RELOCATION);
                                        RelocBlockSize /= sizeof(USHORT);
 
                                        while (RelocBlockSize-- != 0)
@@ -867,6 +885,7 @@ mono_set_act_ctx (const char* file_name)
 void
 mono_load_coree (const char* exe_file_name)
 {
+       HMODULE module_handle;
        gunichar2* file_name;
        UINT required_size;
        UINT size;
@@ -886,13 +905,15 @@ mono_load_coree (const char* exe_file_name)
                file_name [size++] = L'\\';
        memcpy (&file_name [size], L"mscoree.dll", 12 * sizeof (gunichar2));
 
-       coree_module_handle = LoadLibrary (file_name);
+       module_handle = LoadLibrary (file_name);
        g_free (file_name);
 
-       if (coree_module_handle && !SUCCEEDED (MonoFixupCorEE (coree_module_handle))) {
-               FreeLibrary (coree_module_handle);
-               coree_module_handle = NULL;
+       if (module_handle && !SUCCEEDED (MonoFixupCorEE (module_handle))) {
+               FreeLibrary (module_handle);
+               module_handle = NULL;
        }
+
+       coree_module_handle = module_handle;
 }
 
 void