push 8edcbf8579c1d48dd3fb4c679acf4b3012a9efac
[wine/hacks.git] / dlls / kernel32 / tests / loader.c
bloba08cfdba16c20e74a0f56295c30a83fcd7d8ece8
1 /*
2 * Unit test suite for the PE loader.
4 * Copyright 2006 Dmitry Timoshkov
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <assert.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wine/test.h"
28 #define ALIGN_SIZE(size, alignment) (((size) + (alignment - 1)) & ~((alignment - 1)))
30 static PVOID RVAToAddr(DWORD_PTR rva, HMODULE module)
32 if (rva == 0)
33 return NULL;
34 return ((char*) module) + rva;
37 static const struct
39 WORD e_magic; /* 00: MZ Header signature */
40 WORD unused[29];
41 DWORD e_lfanew; /* 3c: Offset to extended header */
42 } dos_header =
44 IMAGE_DOS_SIGNATURE, { 0 }, sizeof(dos_header)
47 static IMAGE_NT_HEADERS nt_header =
49 IMAGE_NT_SIGNATURE, /* Signature */
51 #if defined __i386__
52 IMAGE_FILE_MACHINE_I386, /* Machine */
53 #elif defined __x86_64__
54 IMAGE_FILE_MACHINE_AMD64, /* Machine */
55 #else
56 # error You must specify the machine type
57 #endif
58 1, /* NumberOfSections */
59 0, /* TimeDateStamp */
60 0, /* PointerToSymbolTable */
61 0, /* NumberOfSymbols */
62 sizeof(IMAGE_OPTIONAL_HEADER), /* SizeOfOptionalHeader */
63 IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL /* Characteristics */
65 { IMAGE_NT_OPTIONAL_HDR_MAGIC, /* Magic */
66 1, /* MajorLinkerVersion */
67 0, /* MinorLinkerVersion */
68 0, /* SizeOfCode */
69 0, /* SizeOfInitializedData */
70 0, /* SizeOfUninitializedData */
71 0, /* AddressOfEntryPoint */
72 0x10, /* BaseOfCode, also serves as e_lfanew in the truncated MZ header */
73 0, /* BaseOfData */
74 0x10000000, /* ImageBase */
75 0, /* SectionAlignment */
76 0, /* FileAlignment */
77 4, /* MajorOperatingSystemVersion */
78 0, /* MinorOperatingSystemVersion */
79 1, /* MajorImageVersion */
80 0, /* MinorImageVersion */
81 4, /* MajorSubsystemVersion */
82 0, /* MinorSubsystemVersion */
83 0, /* Win32VersionValue */
84 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000, /* SizeOfImage */
85 sizeof(dos_header) + sizeof(nt_header), /* SizeOfHeaders */
86 0, /* CheckSum */
87 IMAGE_SUBSYSTEM_WINDOWS_CUI, /* Subsystem */
88 0, /* DllCharacteristics */
89 0, /* SizeOfStackReserve */
90 0, /* SizeOfStackCommit */
91 0, /* SizeOfHeapReserve */
92 0, /* SizeOfHeapCommit */
93 0, /* LoaderFlags */
94 0, /* NumberOfRvaAndSizes */
95 { { 0 } } /* DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] */
99 static IMAGE_SECTION_HEADER section =
101 ".rodata", /* Name */
102 { 0x10 }, /* Misc */
103 0, /* VirtualAddress */
104 0x0a, /* SizeOfRawData */
105 0, /* PointerToRawData */
106 0, /* PointerToRelocations */
107 0, /* PointerToLinenumbers */
108 0, /* NumberOfRelocations */
109 0, /* NumberOfLinenumbers */
110 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ, /* Characteristics */
113 static void test_Loader(void)
115 static const struct test_data
117 const void *dos_header;
118 DWORD size_of_dos_header;
119 WORD number_of_sections, size_of_optional_header;
120 DWORD section_alignment, file_alignment;
121 DWORD size_of_image, size_of_headers;
122 DWORD error; /* 0 means LoadLibrary should succeed */
123 } td[] =
125 { &dos_header, sizeof(dos_header),
126 1, 0, 0, 0, 0, 0,
127 ERROR_BAD_EXE_FORMAT
129 { &dos_header, sizeof(dos_header),
130 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
131 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0xe00,
132 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
133 ERROR_BAD_EXE_FORMAT /* XP doesn't like too small image size */
135 { &dos_header, sizeof(dos_header),
136 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
137 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
138 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
139 ERROR_SUCCESS
141 { &dos_header, sizeof(dos_header),
142 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
143 0x1f00,
144 0x1000,
145 ERROR_SUCCESS
147 { &dos_header, sizeof(dos_header),
148 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x200, 0x200,
149 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x200,
150 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
151 ERROR_SUCCESS
153 { &dos_header, sizeof(dos_header),
154 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x200, 0x1000,
155 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
156 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
157 ERROR_BAD_EXE_FORMAT /* XP doesn't like alignments */
159 { &dos_header, sizeof(dos_header),
160 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x200,
161 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
162 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
163 ERROR_SUCCESS
165 { &dos_header, sizeof(dos_header),
166 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x200,
167 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
168 0x200,
169 ERROR_SUCCESS
171 /* Mandatory are all fields up to SizeOfHeaders, everything else
172 * is really optional (at least that's true for XP).
174 { &dos_header, sizeof(dos_header),
175 1, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
176 sizeof(dos_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum) + sizeof(IMAGE_SECTION_HEADER) + 0x10,
177 sizeof(dos_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum) + sizeof(IMAGE_SECTION_HEADER),
178 ERROR_SUCCESS
180 { &dos_header, sizeof(dos_header),
181 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
182 0xd0, /* beyond of the end of file */
183 0xc0, /* beyond of the end of file */
184 ERROR_SUCCESS
186 { &dos_header, sizeof(dos_header),
187 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
188 0x1000,
190 ERROR_SUCCESS
192 { &dos_header, sizeof(dos_header),
193 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
196 ERROR_SUCCESS
198 #if 0 /* not power of 2 alignments need more test cases */
199 { &dos_header, sizeof(dos_header),
200 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x300, 0x300,
203 ERROR_BAD_EXE_FORMAT /* alignment is not power of 2 */
205 #endif
206 { &dos_header, sizeof(dos_header),
207 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 4, 4,
210 ERROR_SUCCESS
212 { &dos_header, sizeof(dos_header),
213 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 1, 1,
216 ERROR_SUCCESS
218 { &dos_header, sizeof(dos_header),
219 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
222 ERROR_BAD_EXE_FORMAT /* image size == 0 -> failure */
224 /* the following data mimics the PE image which upack creates */
225 { &dos_header, 0x10,
226 1, 0x148, 0x1000, 0x200,
227 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
228 0x200,
229 ERROR_SUCCESS
231 /* Minimal PE image that XP is able to load: 92 bytes */
232 { &dos_header, 0x04,
233 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum),
234 0x04 /* also serves as e_lfanew in the truncated MZ header */, 0x04,
237 ERROR_SUCCESS
240 static const char filler[0x1000];
241 static const char section_data[0x10] = "section data";
242 int i;
243 DWORD dummy, file_size, file_align;
244 HANDLE hfile;
245 HMODULE hlib, hlib_as_data_file;
246 SYSTEM_INFO si;
247 char temp_path[MAX_PATH];
248 char dll_name[MAX_PATH];
250 GetSystemInfo(&si);
251 trace("system page size 0x%04x\n", si.dwPageSize);
253 /* prevent displaying of the "Unable to load this DLL" message box */
254 SetErrorMode(SEM_FAILCRITICALERRORS);
256 GetTempPath(MAX_PATH, temp_path);
258 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
260 GetTempFileName(temp_path, "ldr", 0, dll_name);
262 /*trace("creating %s\n", dll_name);*/
263 hfile = CreateFileA(dll_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
264 if (hfile == INVALID_HANDLE_VALUE)
266 ok(0, "could not create %s\n", dll_name);
267 break;
270 SetLastError(0xdeadbeef);
271 ok(WriteFile(hfile, td[i].dos_header, td[i].size_of_dos_header, &dummy, NULL),
272 "WriteFile error %d\n", GetLastError());
274 nt_header.FileHeader.NumberOfSections = td[i].number_of_sections;
275 nt_header.FileHeader.SizeOfOptionalHeader = td[i].size_of_optional_header;
277 nt_header.OptionalHeader.SectionAlignment = td[i].section_alignment;
278 nt_header.OptionalHeader.FileAlignment = td[i].file_alignment;
279 nt_header.OptionalHeader.SizeOfImage = td[i].size_of_image;
280 nt_header.OptionalHeader.SizeOfHeaders = td[i].size_of_headers;
281 SetLastError(0xdeadbeef);
282 ok(WriteFile(hfile, &nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL),
283 "WriteFile error %d\n", GetLastError());
285 if (nt_header.FileHeader.SizeOfOptionalHeader)
287 SetLastError(0xdeadbeef);
288 ok(WriteFile(hfile, &nt_header.OptionalHeader,
289 min(nt_header.FileHeader.SizeOfOptionalHeader, sizeof(IMAGE_OPTIONAL_HEADER)),
290 &dummy, NULL),
291 "WriteFile error %d\n", GetLastError());
292 if (nt_header.FileHeader.SizeOfOptionalHeader > sizeof(IMAGE_OPTIONAL_HEADER))
294 file_align = nt_header.FileHeader.SizeOfOptionalHeader - sizeof(IMAGE_OPTIONAL_HEADER);
295 assert(file_align < sizeof(filler));
296 SetLastError(0xdeadbeef);
297 ok(WriteFile(hfile, filler, file_align, &dummy, NULL),
298 "WriteFile error %d\n", GetLastError());
302 assert(nt_header.FileHeader.NumberOfSections <= 1);
303 if (nt_header.FileHeader.NumberOfSections)
305 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
307 section.PointerToRawData = td[i].size_of_dos_header;
308 section.VirtualAddress = nt_header.OptionalHeader.SectionAlignment;
309 section.Misc.VirtualSize = section.SizeOfRawData * 10;
311 else
313 section.PointerToRawData = nt_header.OptionalHeader.SizeOfHeaders;
314 section.VirtualAddress = nt_header.OptionalHeader.SizeOfHeaders;
315 section.Misc.VirtualSize = 5;
318 SetLastError(0xdeadbeef);
319 ok(WriteFile(hfile, &section, sizeof(section), &dummy, NULL),
320 "WriteFile error %d\n", GetLastError());
322 /* section data */
323 SetLastError(0xdeadbeef);
324 ok(WriteFile(hfile, section_data, sizeof(section_data), &dummy, NULL),
325 "WriteFile error %d\n", GetLastError());
328 file_size = GetFileSize(hfile, NULL);
329 CloseHandle(hfile);
331 SetLastError(0xdeadbeef);
332 hlib = LoadLibrary(dll_name);
333 if (td[i].error == ERROR_SUCCESS)
335 MEMORY_BASIC_INFORMATION info;
337 ok(hlib != 0, "%d: LoadLibrary error %d\n", i, GetLastError());
339 /* No point in crashing. Test crashes on Vista with some of the given files */
340 if (hlib == 0)
342 skip("Failed to load dll number %d\n", i);
343 goto endloop;
346 SetLastError(0xdeadbeef);
347 ok(VirtualQuery(hlib, &info, sizeof(info)) == sizeof(info),
348 "%d: VirtualQuery error %d\n", i, GetLastError());
349 ok(info.BaseAddress == hlib, "%d: %p != %p\n", i, info.BaseAddress, hlib);
350 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
351 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
352 ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %lx != expected %x\n",
353 i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
354 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
355 if (nt_header.OptionalHeader.SectionAlignment < si.dwPageSize)
356 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
357 else
358 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
359 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
361 SetLastError(0xdeadbeef);
362 ok(VirtualQuery((char *)hlib + info.RegionSize, &info, sizeof(info)) == sizeof(info),
363 "%d: VirtualQuery error %d\n", i, GetLastError());
364 if (nt_header.OptionalHeader.SectionAlignment == si.dwPageSize ||
365 nt_header.OptionalHeader.SectionAlignment == nt_header.OptionalHeader.FileAlignment)
367 ok(info.BaseAddress == (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %p != expected %p\n",
368 i, info.BaseAddress, (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
369 ok(info.AllocationBase == 0, "%d: %p != 0\n", i, info.AllocationBase);
370 ok(info.AllocationProtect == 0, "%d: %x != 0\n", i, info.AllocationProtect);
371 /*ok(info.RegionSize == not_practical_value, "%d: %lx != not_practical_value\n", i, info.RegionSize);*/
372 ok(info.State == MEM_FREE, "%d: %x != MEM_FREE\n", i, info.State);
373 ok(info.Type == 0, "%d: %x != 0\n", i, info.Type);
374 ok(info.Protect == PAGE_NOACCESS, "%d: %x != PAGE_NOACCESS\n", i, info.Protect);
376 else
378 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
379 ok(info.BaseAddress == hlib, "%d: got %p != expected %p\n", i, info.BaseAddress, hlib);
380 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
381 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
382 ok(info.RegionSize == ALIGN_SIZE(file_size, si.dwPageSize), "%d: got %lx != expected %x\n",
383 i, info.RegionSize, ALIGN_SIZE(file_size, si.dwPageSize));
384 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
385 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
386 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
389 /* header: check the zeroing of alignment */
390 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
392 const char *start;
393 int size;
395 start = (const char *)hlib + nt_header.OptionalHeader.SizeOfHeaders;
396 size = ALIGN_SIZE((ULONG_PTR)start, si.dwPageSize) - (ULONG_PTR)start;
397 ok(!memcmp(start, filler, size), "%d: header alignment is not cleared\n", i);
400 if (nt_header.FileHeader.NumberOfSections)
402 SetLastError(0xdeadbeef);
403 ok(VirtualQuery((char *)hlib + section.VirtualAddress, &info, sizeof(info)) == sizeof(info),
404 "%d: VirtualQuery error %d\n", i, GetLastError());
405 if (nt_header.OptionalHeader.SectionAlignment < si.dwPageSize)
407 ok(info.BaseAddress == (char *)hlib, "%d: got %p != expected %p\n", i, info.BaseAddress, (char *)hlib);
408 ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %lx != expected %x\n",
409 i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
410 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
412 else
414 ok(info.BaseAddress == (char *)hlib + section.VirtualAddress, "%d: got %p != expected %p\n", i, info.BaseAddress, (char *)hlib + section.VirtualAddress);
415 ok(info.RegionSize == ALIGN_SIZE(section.Misc.VirtualSize, si.dwPageSize), "%d: got %lx != expected %x\n",
416 i, info.RegionSize, ALIGN_SIZE(section.Misc.VirtualSize, si.dwPageSize));
417 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
419 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
420 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
421 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
422 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
424 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
425 ok(!memcmp((const char *)hlib + section.VirtualAddress + section.PointerToRawData, &nt_header, section.SizeOfRawData), "wrong section data\n");
426 else
427 ok(!memcmp((const char *)hlib + section.PointerToRawData, section_data, section.SizeOfRawData), "wrong section data\n");
429 /* check the zeroing of alignment */
430 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
432 const char *start;
433 int size;
435 start = (const char *)hlib + section.VirtualAddress + section.PointerToRawData + section.SizeOfRawData;
436 size = ALIGN_SIZE((ULONG_PTR)start, si.dwPageSize) - (ULONG_PTR)start;
437 ok(memcmp(start, filler, size), "%d: alignment should not be cleared\n", i);
441 SetLastError(0xdeadbeef);
442 hlib_as_data_file = LoadLibraryEx(dll_name, 0, LOAD_LIBRARY_AS_DATAFILE);
443 ok(hlib_as_data_file != 0, "LoadLibraryEx error %u\n", GetLastError());
444 ok(hlib_as_data_file == hlib, "hlib_as_file and hlib are different\n");
446 SetLastError(0xdeadbeef);
447 ok(FreeLibrary(hlib), "FreeLibrary error %d\n", GetLastError());
449 SetLastError(0xdeadbeef);
450 hlib = GetModuleHandle(dll_name);
451 ok(hlib != 0, "GetModuleHandle error %u\n", GetLastError());
453 SetLastError(0xdeadbeef);
454 ok(FreeLibrary(hlib_as_data_file), "FreeLibrary error %d\n", GetLastError());
456 hlib = GetModuleHandle(dll_name);
457 ok(!hlib, "GetModuleHandle should fail\n");
459 SetLastError(0xdeadbeef);
460 hlib_as_data_file = LoadLibraryEx(dll_name, 0, LOAD_LIBRARY_AS_DATAFILE);
461 ok(hlib_as_data_file != 0, "LoadLibraryEx error %u\n", GetLastError());
462 ok((ULONG_PTR)hlib_as_data_file & 1, "hlib_as_data_file is even\n");
464 hlib = GetModuleHandle(dll_name);
465 ok(!hlib, "GetModuleHandle should fail\n");
467 SetLastError(0xdeadbeef);
468 ok(FreeLibrary(hlib_as_data_file), "FreeLibrary error %d\n", GetLastError());
470 else
471 { /* LoadLibrary is expected to fail */
472 ok(!hlib, "%d: LoadLibrary should fail\n", i);
474 if (GetLastError() == ERROR_GEN_FAILURE) /* Win9x, broken behaviour */
476 trace("skipping the loader test on Win9x\n");
477 DeleteFile(dll_name);
478 return;
481 ok(td[i].error == GetLastError(), "%d: expected error %d, got %d\n",
482 i, td[i].error, GetLastError());
485 endloop:
486 SetLastError(0xdeadbeef);
487 ok(DeleteFile(dll_name), "DeleteFile error %d\n", GetLastError());
491 /* Verify linking style of import descriptors */
492 static void test_ImportDescriptors(void)
494 HMODULE kernel32_module = NULL;
495 PIMAGE_DOS_HEADER d_header;
496 PIMAGE_NT_HEADERS nt_headers;
497 DWORD import_dir_size;
498 DWORD_PTR dir_offset;
499 PIMAGE_IMPORT_DESCRIPTOR import_chunk;
501 /* Load kernel32 module */
502 kernel32_module = GetModuleHandleA("kernel32.dll");
503 assert( kernel32_module != NULL );
505 /* Get PE header info from module image */
506 d_header = (PIMAGE_DOS_HEADER) kernel32_module;
507 nt_headers = (PIMAGE_NT_HEADERS) (((char*) d_header) +
508 d_header->e_lfanew);
510 /* Get size of import entry directory */
511 import_dir_size = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size;
512 if (!import_dir_size)
514 skip("Unable to continue testing due to missing import directory.\n");
515 return;
518 /* Get address of first import chunk */
519 dir_offset = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
520 import_chunk = RVAToAddr(dir_offset, kernel32_module);
521 ok(import_chunk != 0, "Invalid import_chunk: %p\n", import_chunk);
522 if (!import_chunk) return;
524 /* Iterate through import descriptors and verify set name,
525 * OriginalFirstThunk, and FirstThunk. Core Windows DLLs, such as
526 * kernel32.dll, don't use Borland-style linking, where the table of
527 * imported names is stored directly in FirstThunk and overwritten
528 * by the relocation, instead of being stored in OriginalFirstThunk.
529 * */
530 for (; import_chunk->FirstThunk; import_chunk++)
532 LPCSTR module_name = (LPCSTR) RVAToAddr(
533 import_chunk->Name, kernel32_module);
534 PIMAGE_THUNK_DATA name_table = (PIMAGE_THUNK_DATA) RVAToAddr(
535 import_chunk->OriginalFirstThunk, kernel32_module);
536 PIMAGE_THUNK_DATA iat = (PIMAGE_THUNK_DATA) RVAToAddr(
537 import_chunk->FirstThunk, kernel32_module);
538 ok(module_name != NULL, "Imported module name should not be NULL\n");
539 ok(name_table != NULL,
540 "Name table for imported module %s should not be NULL\n",
541 module_name);
542 ok(iat != NULL, "IAT for imported module %s should not be NULL\n",
543 module_name);
547 START_TEST(loader)
549 test_Loader();
550 test_ImportDescriptors();