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
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)
34 return ((char*) module) + rva;
39 WORD e_magic; /* 00: MZ Header signature */
41 DWORD e_lfanew; /* 3c: Offset to extended header */
44 IMAGE_DOS_SIGNATURE, { 0 }, sizeof(dos_header)
47 static IMAGE_NT_HEADERS nt_header =
49 IMAGE_NT_SIGNATURE, /* Signature */
52 IMAGE_FILE_MACHINE_I386, /* Machine */
53 #elif defined __x86_64__
54 IMAGE_FILE_MACHINE_AMD64, /* Machine */
55 #elif defined __powerpc__
56 IMAGE_FILE_MACHINE_POWERPC, /* Machine */
58 # error You must specify the machine type
60 1, /* NumberOfSections */
61 0, /* TimeDateStamp */
62 0, /* PointerToSymbolTable */
63 0, /* NumberOfSymbols */
64 sizeof(IMAGE_OPTIONAL_HEADER), /* SizeOfOptionalHeader */
65 IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL /* Characteristics */
67 { IMAGE_NT_OPTIONAL_HDR_MAGIC, /* Magic */
68 1, /* MajorLinkerVersion */
69 0, /* MinorLinkerVersion */
71 0, /* SizeOfInitializedData */
72 0, /* SizeOfUninitializedData */
73 0, /* AddressOfEntryPoint */
74 0x10, /* BaseOfCode, also serves as e_lfanew in the truncated MZ header */
76 0x10000000, /* ImageBase */
77 0, /* SectionAlignment */
78 0, /* FileAlignment */
79 4, /* MajorOperatingSystemVersion */
80 0, /* MinorOperatingSystemVersion */
81 1, /* MajorImageVersion */
82 0, /* MinorImageVersion */
83 4, /* MajorSubsystemVersion */
84 0, /* MinorSubsystemVersion */
85 0, /* Win32VersionValue */
86 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000, /* SizeOfImage */
87 sizeof(dos_header) + sizeof(nt_header), /* SizeOfHeaders */
89 IMAGE_SUBSYSTEM_WINDOWS_CUI, /* Subsystem */
90 0, /* DllCharacteristics */
91 0, /* SizeOfStackReserve */
92 0, /* SizeOfStackCommit */
93 0, /* SizeOfHeapReserve */
94 0, /* SizeOfHeapCommit */
96 0, /* NumberOfRvaAndSizes */
97 { { 0 } } /* DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] */
101 static IMAGE_SECTION_HEADER section =
103 ".rodata", /* Name */
105 0, /* VirtualAddress */
106 0x0a, /* SizeOfRawData */
107 0, /* PointerToRawData */
108 0, /* PointerToRelocations */
109 0, /* PointerToLinenumbers */
110 0, /* NumberOfRelocations */
111 0, /* NumberOfLinenumbers */
112 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ, /* Characteristics */
115 static void test_Loader(void)
117 static const struct test_data
119 const void *dos_header;
120 DWORD size_of_dos_header;
121 WORD number_of_sections, size_of_optional_header;
122 DWORD section_alignment, file_alignment;
123 DWORD size_of_image, size_of_headers;
124 DWORD error; /* 0 means LoadLibrary should succeed */
125 DWORD alt_error; /* alternate error */
128 { &dos_header, sizeof(dos_header),
132 { &dos_header, sizeof(dos_header),
133 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
134 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0xe00,
135 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
136 ERROR_BAD_EXE_FORMAT /* XP doesn't like too small image size */
138 { &dos_header, sizeof(dos_header),
139 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
140 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
141 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
144 { &dos_header, sizeof(dos_header),
145 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
150 { &dos_header, sizeof(dos_header),
151 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x200, 0x200,
152 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x200,
153 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
154 ERROR_SUCCESS, ERROR_INVALID_ADDRESS /* vista is more strict */
156 { &dos_header, sizeof(dos_header),
157 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x200, 0x1000,
158 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
159 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
160 ERROR_BAD_EXE_FORMAT /* XP doesn't like alignments */
162 { &dos_header, sizeof(dos_header),
163 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x200,
164 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
165 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
168 { &dos_header, sizeof(dos_header),
169 1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x200,
170 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
174 /* Mandatory are all fields up to SizeOfHeaders, everything else
175 * is really optional (at least that's true for XP).
177 { &dos_header, sizeof(dos_header),
178 1, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
179 sizeof(dos_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum) + sizeof(IMAGE_SECTION_HEADER) + 0x10,
180 sizeof(dos_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum) + sizeof(IMAGE_SECTION_HEADER),
181 ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT /* vista is more strict */
183 { &dos_header, sizeof(dos_header),
184 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
185 0xd0, /* beyond of the end of file */
186 0xc0, /* beyond of the end of file */
187 ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT /* vista is more strict */
189 { &dos_header, sizeof(dos_header),
190 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
193 ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT /* vista is more strict */
195 { &dos_header, sizeof(dos_header),
196 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
199 ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT /* vista is more strict */
201 #if 0 /* not power of 2 alignments need more test cases */
202 { &dos_header, sizeof(dos_header),
203 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x300, 0x300,
206 ERROR_BAD_EXE_FORMAT /* alignment is not power of 2 */
209 { &dos_header, sizeof(dos_header),
210 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 4, 4,
213 ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT /* vista is more strict */
215 { &dos_header, sizeof(dos_header),
216 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 1, 1,
219 ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT /* vista is more strict */
221 { &dos_header, sizeof(dos_header),
222 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
225 ERROR_BAD_EXE_FORMAT /* image size == 0 -> failure */
227 /* the following data mimics the PE image which upack creates */
229 1, 0x148, 0x1000, 0x200,
230 sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
234 /* Minimal PE image that XP is able to load: 92 bytes */
236 0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum),
237 0x04 /* also serves as e_lfanew in the truncated MZ header */, 0x04,
240 ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT /* vista is more strict */
243 static const char filler[0x1000];
244 static const char section_data[0x10] = "section data";
246 DWORD dummy, file_size, file_align;
248 HMODULE hlib, hlib_as_data_file;
250 char temp_path[MAX_PATH];
251 char dll_name[MAX_PATH];
254 trace("system page size 0x%04x\n", si.dwPageSize);
256 /* prevent displaying of the "Unable to load this DLL" message box */
257 SetErrorMode(SEM_FAILCRITICALERRORS);
259 GetTempPath(MAX_PATH, temp_path);
261 for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
263 GetTempFileName(temp_path, "ldr", 0, dll_name);
265 /*trace("creating %s\n", dll_name);*/
266 hfile = CreateFileA(dll_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
267 if (hfile == INVALID_HANDLE_VALUE)
269 ok(0, "could not create %s\n", dll_name);
273 SetLastError(0xdeadbeef);
274 ok(WriteFile(hfile, td[i].dos_header, td[i].size_of_dos_header, &dummy, NULL),
275 "WriteFile error %d\n", GetLastError());
277 nt_header.FileHeader.NumberOfSections = td[i].number_of_sections;
278 nt_header.FileHeader.SizeOfOptionalHeader = td[i].size_of_optional_header;
280 nt_header.OptionalHeader.SectionAlignment = td[i].section_alignment;
281 nt_header.OptionalHeader.FileAlignment = td[i].file_alignment;
282 nt_header.OptionalHeader.SizeOfImage = td[i].size_of_image;
283 nt_header.OptionalHeader.SizeOfHeaders = td[i].size_of_headers;
284 SetLastError(0xdeadbeef);
285 ok(WriteFile(hfile, &nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL),
286 "WriteFile error %d\n", GetLastError());
288 if (nt_header.FileHeader.SizeOfOptionalHeader)
290 SetLastError(0xdeadbeef);
291 ok(WriteFile(hfile, &nt_header.OptionalHeader,
292 min(nt_header.FileHeader.SizeOfOptionalHeader, sizeof(IMAGE_OPTIONAL_HEADER)),
294 "WriteFile error %d\n", GetLastError());
295 if (nt_header.FileHeader.SizeOfOptionalHeader > sizeof(IMAGE_OPTIONAL_HEADER))
297 file_align = nt_header.FileHeader.SizeOfOptionalHeader - sizeof(IMAGE_OPTIONAL_HEADER);
298 assert(file_align < sizeof(filler));
299 SetLastError(0xdeadbeef);
300 ok(WriteFile(hfile, filler, file_align, &dummy, NULL),
301 "WriteFile error %d\n", GetLastError());
305 assert(nt_header.FileHeader.NumberOfSections <= 1);
306 if (nt_header.FileHeader.NumberOfSections)
308 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
310 section.PointerToRawData = td[i].size_of_dos_header;
311 section.VirtualAddress = nt_header.OptionalHeader.SectionAlignment;
312 section.Misc.VirtualSize = section.SizeOfRawData * 10;
316 section.PointerToRawData = nt_header.OptionalHeader.SizeOfHeaders;
317 section.VirtualAddress = nt_header.OptionalHeader.SizeOfHeaders;
318 section.Misc.VirtualSize = 5;
321 SetLastError(0xdeadbeef);
322 ok(WriteFile(hfile, §ion, sizeof(section), &dummy, NULL),
323 "WriteFile error %d\n", GetLastError());
326 SetLastError(0xdeadbeef);
327 ok(WriteFile(hfile, section_data, sizeof(section_data), &dummy, NULL),
328 "WriteFile error %d\n", GetLastError());
331 file_size = GetFileSize(hfile, NULL);
334 SetLastError(0xdeadbeef);
335 hlib = LoadLibrary(dll_name);
338 MEMORY_BASIC_INFORMATION info;
340 ok( td[i].error == ERROR_SUCCESS, "%d: should have failed\n", i );
342 SetLastError(0xdeadbeef);
343 ok(VirtualQuery(hlib, &info, sizeof(info)) == sizeof(info),
344 "%d: VirtualQuery error %d\n", i, GetLastError());
345 ok(info.BaseAddress == hlib, "%d: %p != %p\n", i, info.BaseAddress, hlib);
346 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
347 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
348 ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %lx != expected %x\n",
349 i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
350 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
351 if (nt_header.OptionalHeader.SectionAlignment < si.dwPageSize)
352 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
354 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
355 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
357 SetLastError(0xdeadbeef);
358 ok(VirtualQuery((char *)hlib + info.RegionSize, &info, sizeof(info)) == sizeof(info),
359 "%d: VirtualQuery error %d\n", i, GetLastError());
360 if (nt_header.OptionalHeader.SectionAlignment == si.dwPageSize ||
361 nt_header.OptionalHeader.SectionAlignment == nt_header.OptionalHeader.FileAlignment)
363 ok(info.BaseAddress == (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %p != expected %p\n",
364 i, info.BaseAddress, (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
365 ok(info.AllocationBase == 0, "%d: %p != 0\n", i, info.AllocationBase);
366 ok(info.AllocationProtect == 0, "%d: %x != 0\n", i, info.AllocationProtect);
367 /*ok(info.RegionSize == not_practical_value, "%d: %lx != not_practical_value\n", i, info.RegionSize);*/
368 ok(info.State == MEM_FREE, "%d: %x != MEM_FREE\n", i, info.State);
369 ok(info.Type == 0, "%d: %x != 0\n", i, info.Type);
370 ok(info.Protect == PAGE_NOACCESS, "%d: %x != PAGE_NOACCESS\n", i, info.Protect);
374 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
375 ok(info.BaseAddress == hlib, "%d: got %p != expected %p\n", i, info.BaseAddress, hlib);
376 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
377 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
378 ok(info.RegionSize == ALIGN_SIZE(file_size, si.dwPageSize), "%d: got %lx != expected %x\n",
379 i, info.RegionSize, ALIGN_SIZE(file_size, si.dwPageSize));
380 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
381 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
382 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
385 /* header: check the zeroing of alignment */
386 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
391 start = (const char *)hlib + nt_header.OptionalHeader.SizeOfHeaders;
392 size = ALIGN_SIZE((ULONG_PTR)start, si.dwPageSize) - (ULONG_PTR)start;
393 ok(!memcmp(start, filler, size), "%d: header alignment is not cleared\n", i);
396 if (nt_header.FileHeader.NumberOfSections)
398 SetLastError(0xdeadbeef);
399 ok(VirtualQuery((char *)hlib + section.VirtualAddress, &info, sizeof(info)) == sizeof(info),
400 "%d: VirtualQuery error %d\n", i, GetLastError());
401 if (nt_header.OptionalHeader.SectionAlignment < si.dwPageSize)
403 ok(info.BaseAddress == (char *)hlib, "%d: got %p != expected %p\n", i, info.BaseAddress, (char *)hlib);
404 ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %lx != expected %x\n",
405 i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
406 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
410 ok(info.BaseAddress == (char *)hlib + section.VirtualAddress, "%d: got %p != expected %p\n", i, info.BaseAddress, (char *)hlib + section.VirtualAddress);
411 ok(info.RegionSize == ALIGN_SIZE(section.Misc.VirtualSize, si.dwPageSize), "%d: got %lx != expected %x\n",
412 i, info.RegionSize, ALIGN_SIZE(section.Misc.VirtualSize, si.dwPageSize));
413 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
415 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
416 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
417 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
418 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
420 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
421 ok(!memcmp((const char *)hlib + section.VirtualAddress + section.PointerToRawData, &nt_header, section.SizeOfRawData), "wrong section data\n");
423 ok(!memcmp((const char *)hlib + section.PointerToRawData, section_data, section.SizeOfRawData), "wrong section data\n");
425 /* check the zeroing of alignment */
426 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
431 start = (const char *)hlib + section.VirtualAddress + section.PointerToRawData + section.SizeOfRawData;
432 size = ALIGN_SIZE((ULONG_PTR)start, si.dwPageSize) - (ULONG_PTR)start;
433 ok(memcmp(start, filler, size), "%d: alignment should not be cleared\n", i);
437 SetLastError(0xdeadbeef);
438 hlib_as_data_file = LoadLibraryEx(dll_name, 0, LOAD_LIBRARY_AS_DATAFILE);
439 ok(hlib_as_data_file != 0, "LoadLibraryEx error %u\n", GetLastError());
440 ok(hlib_as_data_file == hlib, "hlib_as_file and hlib are different\n");
442 SetLastError(0xdeadbeef);
443 ok(FreeLibrary(hlib), "FreeLibrary error %d\n", GetLastError());
445 SetLastError(0xdeadbeef);
446 hlib = GetModuleHandle(dll_name);
447 ok(hlib != 0, "GetModuleHandle error %u\n", GetLastError());
449 SetLastError(0xdeadbeef);
450 ok(FreeLibrary(hlib_as_data_file), "FreeLibrary error %d\n", GetLastError());
452 hlib = GetModuleHandle(dll_name);
453 ok(!hlib, "GetModuleHandle should fail\n");
455 SetLastError(0xdeadbeef);
456 hlib_as_data_file = LoadLibraryEx(dll_name, 0, LOAD_LIBRARY_AS_DATAFILE);
457 ok(hlib_as_data_file != 0, "LoadLibraryEx error %u\n", GetLastError());
458 ok((ULONG_PTR)hlib_as_data_file & 1, "hlib_as_data_file is even\n");
460 hlib = GetModuleHandle(dll_name);
461 ok(!hlib, "GetModuleHandle should fail\n");
463 SetLastError(0xdeadbeef);
464 ok(FreeLibrary(hlib_as_data_file), "FreeLibrary error %d\n", GetLastError());
468 ok(td[i].error || td[i].alt_error, "%d: LoadLibrary should succeed\n", i);
470 if (GetLastError() == ERROR_GEN_FAILURE) /* Win9x, broken behaviour */
472 trace("skipping the loader test on Win9x\n");
473 DeleteFile(dll_name);
477 ok(td[i].error == GetLastError() || td[i].alt_error == GetLastError(),
478 "%d: expected error %d or %d, got %d\n",
479 i, td[i].error, td[i].alt_error, GetLastError());
482 SetLastError(0xdeadbeef);
483 ok(DeleteFile(dll_name), "DeleteFile error %d\n", GetLastError());
487 /* Verify linking style of import descriptors */
488 static void test_ImportDescriptors(void)
490 HMODULE kernel32_module = NULL;
491 PIMAGE_DOS_HEADER d_header;
492 PIMAGE_NT_HEADERS nt_headers;
493 DWORD import_dir_size;
494 DWORD_PTR dir_offset;
495 PIMAGE_IMPORT_DESCRIPTOR import_chunk;
497 /* Load kernel32 module */
498 kernel32_module = GetModuleHandleA("kernel32.dll");
499 assert( kernel32_module != NULL );
501 /* Get PE header info from module image */
502 d_header = (PIMAGE_DOS_HEADER) kernel32_module;
503 nt_headers = (PIMAGE_NT_HEADERS) (((char*) d_header) +
506 /* Get size of import entry directory */
507 import_dir_size = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size;
508 if (!import_dir_size)
510 skip("Unable to continue testing due to missing import directory.\n");
514 /* Get address of first import chunk */
515 dir_offset = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
516 import_chunk = RVAToAddr(dir_offset, kernel32_module);
517 ok(import_chunk != 0, "Invalid import_chunk: %p\n", import_chunk);
518 if (!import_chunk) return;
520 /* Iterate through import descriptors and verify set name,
521 * OriginalFirstThunk, and FirstThunk. Core Windows DLLs, such as
522 * kernel32.dll, don't use Borland-style linking, where the table of
523 * imported names is stored directly in FirstThunk and overwritten
524 * by the relocation, instead of being stored in OriginalFirstThunk.
526 for (; import_chunk->FirstThunk; import_chunk++)
528 LPCSTR module_name = RVAToAddr(import_chunk->Name, kernel32_module);
529 PIMAGE_THUNK_DATA name_table = RVAToAddr(
530 U(*import_chunk).OriginalFirstThunk, kernel32_module);
531 PIMAGE_THUNK_DATA iat = RVAToAddr(
532 import_chunk->FirstThunk, kernel32_module);
533 ok(module_name != NULL, "Imported module name should not be NULL\n");
534 ok(name_table != NULL,
535 "Name table for imported module %s should not be NULL\n",
537 ok(iat != NULL, "IAT for imported module %s should not be NULL\n",
545 test_ImportDescriptors();