kernel32: Add ARM support.
[wine] / dlls / kernel32 / tests / loader.c
1 /*
2  * Unit test suite for the PE loader.
3  *
4  * Copyright 2006 Dmitry Timoshkov
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22 #include <assert.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wine/test.h"
27
28 #define ALIGN_SIZE(size, alignment) (((size) + (alignment - 1)) & ~((alignment - 1)))
29
30 static PVOID RVAToAddr(DWORD_PTR rva, HMODULE module)
31 {
32     if (rva == 0)
33         return NULL;
34     return ((char*) module) + rva;
35 }
36
37 static const struct
38 {
39     WORD e_magic;      /* 00: MZ Header signature */
40     WORD unused[29];
41     DWORD e_lfanew;    /* 3c: Offset to extended header */
42 } dos_header =
43 {
44     IMAGE_DOS_SIGNATURE, { 0 }, sizeof(dos_header)
45 };
46
47 static IMAGE_NT_HEADERS nt_header =
48 {
49     IMAGE_NT_SIGNATURE, /* Signature */
50     {
51 #if defined __i386__
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 */
57 #elif defined __sparc__
58       IMAGE_FILE_MACHINE_SPARC, /* Machine */
59 #elif defined __arm__
60       IMAGE_FILE_MACHINE_ARM, /* Machine */
61 #else
62 # error You must specify the machine type
63 #endif
64       1, /* NumberOfSections */
65       0, /* TimeDateStamp */
66       0, /* PointerToSymbolTable */
67       0, /* NumberOfSymbols */
68       sizeof(IMAGE_OPTIONAL_HEADER), /* SizeOfOptionalHeader */
69       IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL /* Characteristics */
70     },
71     { IMAGE_NT_OPTIONAL_HDR_MAGIC, /* Magic */
72       1, /* MajorLinkerVersion */
73       0, /* MinorLinkerVersion */
74       0, /* SizeOfCode */
75       0, /* SizeOfInitializedData */
76       0, /* SizeOfUninitializedData */
77       0, /* AddressOfEntryPoint */
78       0x10, /* BaseOfCode, also serves as e_lfanew in the truncated MZ header */
79 #ifndef _WIN64
80       0, /* BaseOfData */
81 #endif
82       0x10000000, /* ImageBase */
83       0, /* SectionAlignment */
84       0, /* FileAlignment */
85       4, /* MajorOperatingSystemVersion */
86       0, /* MinorOperatingSystemVersion */
87       1, /* MajorImageVersion */
88       0, /* MinorImageVersion */
89       4, /* MajorSubsystemVersion */
90       0, /* MinorSubsystemVersion */
91       0, /* Win32VersionValue */
92       sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000, /* SizeOfImage */
93       sizeof(dos_header) + sizeof(nt_header), /* SizeOfHeaders */
94       0, /* CheckSum */
95       IMAGE_SUBSYSTEM_WINDOWS_CUI, /* Subsystem */
96       0, /* DllCharacteristics */
97       0, /* SizeOfStackReserve */
98       0, /* SizeOfStackCommit */
99       0, /* SizeOfHeapReserve */
100       0, /* SizeOfHeapCommit */
101       0, /* LoaderFlags */
102       0, /* NumberOfRvaAndSizes */
103       { { 0 } } /* DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] */
104     }
105 };
106
107 static IMAGE_SECTION_HEADER section =
108 {
109     ".rodata", /* Name */
110     { 0x10 }, /* Misc */
111     0, /* VirtualAddress */
112     0x0a, /* SizeOfRawData */
113     0, /* PointerToRawData */
114     0, /* PointerToRelocations */
115     0, /* PointerToLinenumbers */
116     0, /* NumberOfRelocations */
117     0, /* NumberOfLinenumbers */
118     IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ, /* Characteristics */
119 };
120
121 static void test_Loader(void)
122 {
123     static const struct test_data
124     {
125         const void *dos_header;
126         DWORD size_of_dos_header;
127         WORD number_of_sections, size_of_optional_header;
128         DWORD section_alignment, file_alignment;
129         DWORD size_of_image, size_of_headers;
130         DWORD error; /* 0 means LoadLibrary should succeed */
131         DWORD alt_error; /* alternate error */
132     } td[] =
133     {
134         { &dos_header, sizeof(dos_header),
135           1, 0, 0, 0, 0, 0,
136           ERROR_BAD_EXE_FORMAT
137         },
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) + 0xe00,
141           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
142           ERROR_BAD_EXE_FORMAT /* XP doesn't like too small image size */
143         },
144         { &dos_header, sizeof(dos_header),
145           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
146           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
147           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
148           ERROR_SUCCESS
149         },
150         { &dos_header, sizeof(dos_header),
151           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x1000,
152           0x1f00,
153           0x1000,
154           ERROR_SUCCESS
155         },
156         { &dos_header, sizeof(dos_header),
157           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x200, 0x200,
158           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x200,
159           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
160           ERROR_SUCCESS, ERROR_INVALID_ADDRESS /* vista is more strict */
161         },
162         { &dos_header, sizeof(dos_header),
163           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x200, 0x1000,
164           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
165           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
166           ERROR_BAD_EXE_FORMAT /* XP doesn't like alignments */
167         },
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,
171           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER),
172           ERROR_SUCCESS
173         },
174         { &dos_header, sizeof(dos_header),
175           1, sizeof(IMAGE_OPTIONAL_HEADER), 0x1000, 0x200,
176           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
177           0x200,
178           ERROR_SUCCESS
179         },
180         /* Mandatory are all fields up to SizeOfHeaders, everything else
181          * is really optional (at least that's true for XP).
182          */
183         { &dos_header, sizeof(dos_header),
184           1, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
185           sizeof(dos_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum) + sizeof(IMAGE_SECTION_HEADER) + 0x10,
186           sizeof(dos_header) + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum) + sizeof(IMAGE_SECTION_HEADER),
187           ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT /* vista is more strict */
188         },
189         { &dos_header, sizeof(dos_header),
190           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
191           0xd0, /* beyond of the end of file */
192           0xc0, /* beyond of the end of file */
193           ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT /* vista is more strict */
194         },
195         { &dos_header, sizeof(dos_header),
196           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
197           0x1000,
198           0,
199           ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT /* vista is more strict */
200         },
201         { &dos_header, sizeof(dos_header),
202           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
203           1,
204           0,
205           ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT /* vista is more strict */
206         },
207 #if 0 /* not power of 2 alignments need more test cases */
208         { &dos_header, sizeof(dos_header),
209           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x300, 0x300,
210           1,
211           0,
212           ERROR_BAD_EXE_FORMAT /* alignment is not power of 2 */
213         },
214 #endif
215         { &dos_header, sizeof(dos_header),
216           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 4, 4,
217           1,
218           0,
219           ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT /* vista is more strict */
220         },
221         { &dos_header, sizeof(dos_header),
222           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 1, 1,
223           1,
224           0,
225           ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT /* vista is more strict */
226         },
227         { &dos_header, sizeof(dos_header),
228           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum), 0x200, 0x200,
229           0,
230           0,
231           ERROR_BAD_EXE_FORMAT /* image size == 0 -> failure */
232         },
233         /* the following data mimics the PE image which upack creates */
234         { &dos_header, 0x10,
235           1, 0x148, 0x1000, 0x200,
236           sizeof(dos_header) + sizeof(nt_header) + sizeof(IMAGE_SECTION_HEADER) + 0x1000,
237           0x200,
238           ERROR_SUCCESS
239         },
240         /* Minimal PE image that XP is able to load: 92 bytes */
241         { &dos_header, 0x04,
242           0, FIELD_OFFSET(IMAGE_OPTIONAL_HEADER, CheckSum),
243           0x04 /* also serves as e_lfanew in the truncated MZ header */, 0x04,
244           1,
245           0,
246           ERROR_SUCCESS, ERROR_BAD_EXE_FORMAT /* vista is more strict */
247         }
248     };
249     static const char filler[0x1000];
250     static const char section_data[0x10] = "section data";
251     int i;
252     DWORD dummy, file_size, file_align;
253     HANDLE hfile;
254     HMODULE hlib, hlib_as_data_file;
255     SYSTEM_INFO si;
256     char temp_path[MAX_PATH];
257     char dll_name[MAX_PATH];
258
259     GetSystemInfo(&si);
260     trace("system page size 0x%04x\n", si.dwPageSize);
261
262     /* prevent displaying of the "Unable to load this DLL" message box */
263     SetErrorMode(SEM_FAILCRITICALERRORS);
264
265     GetTempPath(MAX_PATH, temp_path);
266
267     for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
268     {
269         GetTempFileName(temp_path, "ldr", 0, dll_name);
270
271         /*trace("creating %s\n", dll_name);*/
272         hfile = CreateFileA(dll_name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
273         if (hfile == INVALID_HANDLE_VALUE)
274         {
275             ok(0, "could not create %s\n", dll_name);
276             break;
277         }
278
279         SetLastError(0xdeadbeef);
280         ok(WriteFile(hfile, td[i].dos_header, td[i].size_of_dos_header, &dummy, NULL),
281            "WriteFile error %d\n", GetLastError());
282
283         nt_header.FileHeader.NumberOfSections = td[i].number_of_sections;
284         nt_header.FileHeader.SizeOfOptionalHeader = td[i].size_of_optional_header;
285
286         nt_header.OptionalHeader.SectionAlignment = td[i].section_alignment;
287         nt_header.OptionalHeader.FileAlignment = td[i].file_alignment;
288         nt_header.OptionalHeader.SizeOfImage = td[i].size_of_image;
289         nt_header.OptionalHeader.SizeOfHeaders = td[i].size_of_headers;
290         SetLastError(0xdeadbeef);
291         ok(WriteFile(hfile, &nt_header, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER), &dummy, NULL),
292            "WriteFile error %d\n", GetLastError());
293
294         if (nt_header.FileHeader.SizeOfOptionalHeader)
295         {
296             SetLastError(0xdeadbeef);
297             ok(WriteFile(hfile, &nt_header.OptionalHeader,
298                          min(nt_header.FileHeader.SizeOfOptionalHeader, sizeof(IMAGE_OPTIONAL_HEADER)),
299                          &dummy, NULL),
300                "WriteFile error %d\n", GetLastError());
301             if (nt_header.FileHeader.SizeOfOptionalHeader > sizeof(IMAGE_OPTIONAL_HEADER))
302             {
303                 file_align = nt_header.FileHeader.SizeOfOptionalHeader - sizeof(IMAGE_OPTIONAL_HEADER);
304                 assert(file_align < sizeof(filler));
305                 SetLastError(0xdeadbeef);
306                 ok(WriteFile(hfile, filler, file_align, &dummy, NULL),
307                    "WriteFile error %d\n", GetLastError());
308             }
309         }
310
311         assert(nt_header.FileHeader.NumberOfSections <= 1);
312         if (nt_header.FileHeader.NumberOfSections)
313         {
314             if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
315             {
316                 section.PointerToRawData = td[i].size_of_dos_header;
317                 section.VirtualAddress = nt_header.OptionalHeader.SectionAlignment;
318                 section.Misc.VirtualSize = section.SizeOfRawData * 10;
319             }
320             else
321             {
322                 section.PointerToRawData = nt_header.OptionalHeader.SizeOfHeaders;
323                 section.VirtualAddress = nt_header.OptionalHeader.SizeOfHeaders;
324                 section.Misc.VirtualSize = 5;
325             }
326
327             SetLastError(0xdeadbeef);
328             ok(WriteFile(hfile, &section, sizeof(section), &dummy, NULL),
329                "WriteFile error %d\n", GetLastError());
330
331             /* section data */
332             SetLastError(0xdeadbeef);
333             ok(WriteFile(hfile, section_data, sizeof(section_data), &dummy, NULL),
334                "WriteFile error %d\n", GetLastError());
335         }
336
337         file_size = GetFileSize(hfile, NULL);
338         CloseHandle(hfile);
339
340         SetLastError(0xdeadbeef);
341         hlib = LoadLibrary(dll_name);
342         if (hlib)
343         {
344             MEMORY_BASIC_INFORMATION info;
345
346             ok( td[i].error == ERROR_SUCCESS, "%d: should have failed\n", i );
347
348             SetLastError(0xdeadbeef);
349             ok(VirtualQuery(hlib, &info, sizeof(info)) == sizeof(info),
350                 "%d: VirtualQuery error %d\n", i, GetLastError());
351             ok(info.BaseAddress == hlib, "%d: %p != %p\n", i, info.BaseAddress, hlib);
352             ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
353             ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
354             ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %lx != expected %x\n",
355                i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
356             ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
357             if (nt_header.OptionalHeader.SectionAlignment < si.dwPageSize)
358                 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
359             else
360                 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
361             ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
362
363             SetLastError(0xdeadbeef);
364             ok(VirtualQuery((char *)hlib + info.RegionSize, &info, sizeof(info)) == sizeof(info),
365                 "%d: VirtualQuery error %d\n", i, GetLastError());
366             if (nt_header.OptionalHeader.SectionAlignment == si.dwPageSize ||
367                 nt_header.OptionalHeader.SectionAlignment == nt_header.OptionalHeader.FileAlignment)
368             {
369                 ok(info.BaseAddress == (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %p != expected %p\n",
370                    i, info.BaseAddress, (char *)hlib + ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
371                 ok(info.AllocationBase == 0, "%d: %p != 0\n", i, info.AllocationBase);
372                 ok(info.AllocationProtect == 0, "%d: %x != 0\n", i, info.AllocationProtect);
373                 /*ok(info.RegionSize == not_practical_value, "%d: %lx != not_practical_value\n", i, info.RegionSize);*/
374                 ok(info.State == MEM_FREE, "%d: %x != MEM_FREE\n", i, info.State);
375                 ok(info.Type == 0, "%d: %x != 0\n", i, info.Type);
376                 ok(info.Protect == PAGE_NOACCESS, "%d: %x != PAGE_NOACCESS\n", i, info.Protect);
377             }
378             else
379             {
380                 ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
381                 ok(info.BaseAddress == hlib, "%d: got %p != expected %p\n", i, info.BaseAddress, hlib);
382                 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
383                 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
384                 ok(info.RegionSize == ALIGN_SIZE(file_size, si.dwPageSize), "%d: got %lx != expected %x\n",
385                    i, info.RegionSize, ALIGN_SIZE(file_size, si.dwPageSize));
386                 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
387                 ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
388                 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
389             }
390
391             /* header: check the zeroing of alignment */
392             if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
393             {
394                 const char *start;
395                 int size;
396
397                 start = (const char *)hlib + nt_header.OptionalHeader.SizeOfHeaders;
398                 size = ALIGN_SIZE((ULONG_PTR)start, si.dwPageSize) - (ULONG_PTR)start;
399                 ok(!memcmp(start, filler, size), "%d: header alignment is not cleared\n", i);
400             }
401
402             if (nt_header.FileHeader.NumberOfSections)
403             {
404                 SetLastError(0xdeadbeef);
405                 ok(VirtualQuery((char *)hlib + section.VirtualAddress, &info, sizeof(info)) == sizeof(info),
406                     "%d: VirtualQuery error %d\n", i, GetLastError());
407                 if (nt_header.OptionalHeader.SectionAlignment < si.dwPageSize)
408                 {
409                     ok(info.BaseAddress == hlib, "%d: got %p != expected %p\n", i, info.BaseAddress, hlib);
410                     ok(info.RegionSize == ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize), "%d: got %lx != expected %x\n",
411                        i, info.RegionSize, ALIGN_SIZE(nt_header.OptionalHeader.SizeOfImage, si.dwPageSize));
412                     ok(info.Protect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.Protect);
413                 }
414                 else
415                 {
416                     ok(info.BaseAddress == (char *)hlib + section.VirtualAddress, "%d: got %p != expected %p\n", i, info.BaseAddress, (char *)hlib + section.VirtualAddress);
417                     ok(info.RegionSize == ALIGN_SIZE(section.Misc.VirtualSize, si.dwPageSize), "%d: got %lx != expected %x\n",
418                        i, info.RegionSize, ALIGN_SIZE(section.Misc.VirtualSize, si.dwPageSize));
419                     ok(info.Protect == PAGE_READONLY, "%d: %x != PAGE_READONLY\n", i, info.Protect);
420                 }
421                 ok(info.AllocationBase == hlib, "%d: %p != %p\n", i, info.AllocationBase, hlib);
422                 ok(info.AllocationProtect == PAGE_EXECUTE_WRITECOPY, "%d: %x != PAGE_EXECUTE_WRITECOPY\n", i, info.AllocationProtect);
423                 ok(info.State == MEM_COMMIT, "%d: %x != MEM_COMMIT\n", i, info.State);
424                 ok(info.Type == SEC_IMAGE, "%d: %x != SEC_IMAGE\n", i, info.Type);
425
426                 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
427                     ok(!memcmp((const char *)hlib + section.VirtualAddress + section.PointerToRawData, &nt_header, section.SizeOfRawData), "wrong section data\n");
428                 else
429                     ok(!memcmp((const char *)hlib + section.PointerToRawData, section_data, section.SizeOfRawData), "wrong section data\n");
430
431                 /* check the zeroing of alignment */
432                 if (nt_header.OptionalHeader.SectionAlignment >= si.dwPageSize)
433                 {
434                     const char *start;
435                     int size;
436
437                     start = (const char *)hlib + section.VirtualAddress + section.PointerToRawData + section.SizeOfRawData;
438                     size = ALIGN_SIZE((ULONG_PTR)start, si.dwPageSize) - (ULONG_PTR)start;
439                     ok(memcmp(start, filler, size), "%d: alignment should not be cleared\n", i);
440                 }
441             }
442
443             SetLastError(0xdeadbeef);
444             hlib_as_data_file = LoadLibraryEx(dll_name, 0, LOAD_LIBRARY_AS_DATAFILE);
445             ok(hlib_as_data_file != 0, "LoadLibraryEx error %u\n", GetLastError());
446             ok(hlib_as_data_file == hlib, "hlib_as_file and hlib are different\n");
447
448             SetLastError(0xdeadbeef);
449             ok(FreeLibrary(hlib), "FreeLibrary error %d\n", GetLastError());
450
451             SetLastError(0xdeadbeef);
452             hlib = GetModuleHandle(dll_name);
453             ok(hlib != 0, "GetModuleHandle error %u\n", GetLastError());
454
455             SetLastError(0xdeadbeef);
456             ok(FreeLibrary(hlib_as_data_file), "FreeLibrary error %d\n", GetLastError());
457
458             hlib = GetModuleHandle(dll_name);
459             ok(!hlib, "GetModuleHandle should fail\n");
460
461             SetLastError(0xdeadbeef);
462             hlib_as_data_file = LoadLibraryEx(dll_name, 0, LOAD_LIBRARY_AS_DATAFILE);
463             ok(hlib_as_data_file != 0, "LoadLibraryEx error %u\n", GetLastError());
464             ok((ULONG_PTR)hlib_as_data_file & 1, "hlib_as_data_file is even\n");
465
466             hlib = GetModuleHandle(dll_name);
467             ok(!hlib, "GetModuleHandle should fail\n");
468
469             SetLastError(0xdeadbeef);
470             ok(FreeLibrary(hlib_as_data_file), "FreeLibrary error %d\n", GetLastError());
471         }
472         else
473         {
474             ok(td[i].error || td[i].alt_error, "%d: LoadLibrary should succeed\n", i);
475
476             if (GetLastError() == ERROR_GEN_FAILURE) /* Win9x, broken behaviour */
477             {
478                 trace("skipping the loader test on Win9x\n");
479                 DeleteFile(dll_name);
480                 return;
481             }
482
483             ok(td[i].error == GetLastError() || td[i].alt_error == GetLastError(),
484                "%d: expected error %d or %d, got %d\n",
485                i, td[i].error, td[i].alt_error, GetLastError());
486         }
487
488         SetLastError(0xdeadbeef);
489         ok(DeleteFile(dll_name), "DeleteFile error %d\n", GetLastError());
490     }
491 }
492
493 /* Verify linking style of import descriptors */
494 static void test_ImportDescriptors(void)
495 {
496     HMODULE kernel32_module = NULL;
497     PIMAGE_DOS_HEADER d_header;
498     PIMAGE_NT_HEADERS nt_headers;
499     DWORD import_dir_size;
500     DWORD_PTR dir_offset;
501     PIMAGE_IMPORT_DESCRIPTOR import_chunk;
502
503     /* Load kernel32 module */
504     kernel32_module = GetModuleHandleA("kernel32.dll");
505     assert( kernel32_module != NULL );
506
507     /* Get PE header info from module image */
508     d_header = (PIMAGE_DOS_HEADER) kernel32_module;
509     nt_headers = (PIMAGE_NT_HEADERS) (((char*) d_header) +
510             d_header->e_lfanew);
511
512     /* Get size of import entry directory */
513     import_dir_size = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size;
514     if (!import_dir_size)
515     {
516         skip("Unable to continue testing due to missing import directory.\n");
517         return;
518     }
519
520     /* Get address of first import chunk */
521     dir_offset = nt_headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
522     import_chunk = RVAToAddr(dir_offset, kernel32_module);
523     ok(import_chunk != 0, "Invalid import_chunk: %p\n", import_chunk);
524     if (!import_chunk) return;
525
526     /* Iterate through import descriptors and verify set name,
527      * OriginalFirstThunk, and FirstThunk.  Core Windows DLLs, such as
528      * kernel32.dll, don't use Borland-style linking, where the table of
529      * imported names is stored directly in FirstThunk and overwritten
530      * by the relocation, instead of being stored in OriginalFirstThunk.
531      * */
532     for (; import_chunk->FirstThunk; import_chunk++)
533     {
534         LPCSTR module_name = RVAToAddr(import_chunk->Name, kernel32_module);
535         PIMAGE_THUNK_DATA name_table = RVAToAddr(
536                 U(*import_chunk).OriginalFirstThunk, kernel32_module);
537         PIMAGE_THUNK_DATA iat = RVAToAddr(
538                 import_chunk->FirstThunk, kernel32_module);
539         ok(module_name != NULL, "Imported module name should not be NULL\n");
540         ok(name_table != NULL,
541                 "Name table for imported module %s should not be NULL\n",
542                 module_name);
543         ok(iat != NULL, "IAT for imported module %s should not be NULL\n",
544                 module_name);
545     }
546 }
547
548 START_TEST(loader)
549 {
550     test_Loader();
551     test_ImportDescriptors();
552 }