Trap exceptions in RtlImageNtHeader.
[wine] / dlls / ntdll / loader.c
1 /*
2  * Copyright 2002 Dmitry Timoshkov for Codeweavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include "winbase.h"
20 #include "ntdef.h"
21 #include "winnt.h"
22 #include "ntddk.h"
23
24 #include "module.h"
25 #include "wine/exception.h"
26 #include "msvcrt/excpt.h"
27 #include "wine/debug.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
30
31 /* filter for page-fault exceptions */
32 static WINE_EXCEPTION_FILTER(page_fault)
33 {
34     if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
35         return EXCEPTION_EXECUTE_HANDLER;
36     return EXCEPTION_CONTINUE_SEARCH;
37 }
38
39
40 NTSTATUS WINAPI LdrDisableThreadCalloutsForDll(HANDLE hModule)
41 {
42     if (DisableThreadLibraryCalls(hModule))
43         return STATUS_SUCCESS;
44     else
45         return STATUS_DLL_NOT_FOUND;
46 }
47
48 /* FIXME : MODULE_FindModule should depend on LdrGetDllHandle, not vice-versa */
49
50 NTSTATUS WINAPI LdrGetDllHandle(ULONG x, LONG y, PUNICODE_STRING name, PVOID *base)
51 {
52     STRING str;
53     WINE_MODREF *wm;
54
55     FIXME("%08lx %08lx %s %p : partial stub\n",x,y,debugstr_wn(name->Buffer,name->Length),base);
56
57     *base = 0;
58
59     RtlUnicodeStringToAnsiString(&str, name, TRUE);
60     wm = MODULE_FindModule(str.Buffer);
61     if(!wm)
62         return STATUS_DLL_NOT_FOUND;
63     *base = (PVOID) wm->module;
64
65     return STATUS_SUCCESS;
66 }
67
68 /* FIXME : MODULE_GetProcAddress should depend on LdrGetProcedureAddress, not vice-versa */
69
70 NTSTATUS WINAPI LdrGetProcedureAddress(PVOID base, PANSI_STRING name, ULONG ord, PVOID *address)
71 {
72     WARN("%p %s %ld %p\n",base, debugstr_an(name->Buffer,name->Length), ord, address);
73
74     if(name)
75         *address = MODULE_GetProcAddress( (HMODULE) base, name->Buffer, -1, FALSE);
76     else
77         *address = MODULE_GetProcAddress( (HMODULE) base, (LPSTR) ord, -1, FALSE);
78
79     return (*address) ? STATUS_SUCCESS : STATUS_DLL_NOT_FOUND;
80 }
81
82
83 /***********************************************************************
84  *           RtlImageNtHeader   (NTDLL.@)
85  */
86 PIMAGE_NT_HEADERS WINAPI RtlImageNtHeader(HMODULE hModule)
87 {
88     IMAGE_NT_HEADERS *ret;
89
90     __TRY
91     {
92         IMAGE_DOS_HEADER *dos = (IMAGE_DOS_HEADER *)hModule;
93
94         ret = NULL;
95         if (dos->e_magic == IMAGE_DOS_SIGNATURE)
96         {
97             ret = (IMAGE_NT_HEADERS *)((char *)dos + dos->e_lfanew);
98             if (ret->Signature != IMAGE_NT_SIGNATURE) ret = NULL;
99         }
100     }
101     __EXCEPT(page_fault)
102     {
103         return NULL;
104     }
105     __ENDTRY
106     return ret;
107 }
108
109
110 /***********************************************************************
111  *           RtlImageDirectoryEntryToData   (NTDLL.@)
112  */
113 PVOID WINAPI RtlImageDirectoryEntryToData( HMODULE module, BOOL image, WORD dir, ULONG *size )
114 {
115     const IMAGE_NT_HEADERS *nt;
116     DWORD addr;
117
118     if ((ULONG_PTR)module & 1)  /* mapped as data file */
119     {
120         module = (HMODULE)((ULONG_PTR)module & ~1);
121         image = FALSE;
122     }
123     if (!(nt = RtlImageNtHeader( module ))) return NULL;
124     if (dir >= nt->OptionalHeader.NumberOfRvaAndSizes) return NULL;
125     if (!(addr = nt->OptionalHeader.DataDirectory[dir].VirtualAddress)) return NULL;
126     *size = nt->OptionalHeader.DataDirectory[dir].Size;
127     if (image || addr < nt->OptionalHeader.SizeOfHeaders) return (char *)module + addr;
128
129     /* not mapped as image, need to find the section containing the virtual address */
130     return RtlImageRvaToVa( nt, module, addr, NULL );
131 }
132
133
134 /***********************************************************************
135  *           RtlImageRvaToSection   (NTDLL.@)
136  */
137 PIMAGE_SECTION_HEADER WINAPI RtlImageRvaToSection( const IMAGE_NT_HEADERS *nt,
138                                                    HMODULE module, DWORD rva )
139 {
140     int i;
141     IMAGE_SECTION_HEADER *sec = (IMAGE_SECTION_HEADER*)((char*)&nt->OptionalHeader +
142                                                         nt->FileHeader.SizeOfOptionalHeader);
143     for (i = 0; i < nt->FileHeader.NumberOfSections; i++, sec++)
144     {
145         if ((sec->VirtualAddress <= rva) && (sec->VirtualAddress + sec->SizeOfRawData > rva))
146             return sec;
147     }
148     return NULL;
149 }
150
151
152 /***********************************************************************
153  *           RtlImageRvaToVa   (NTDLL.@)
154  */
155 PVOID WINAPI RtlImageRvaToVa( const IMAGE_NT_HEADERS *nt, HMODULE module,
156                               DWORD rva, IMAGE_SECTION_HEADER **section )
157 {
158     IMAGE_SECTION_HEADER *sec;
159
160     if (section && *section)  /* try this section first */
161     {
162         sec = *section;
163         if ((sec->VirtualAddress <= rva) && (sec->VirtualAddress + sec->SizeOfRawData > rva))
164             goto found;
165     }
166     if (!(sec = RtlImageRvaToSection( nt, module, rva ))) return NULL;
167  found:
168     if (section) *section = sec;
169     return (char *)module + sec->PointerToRawData + (rva - sec->VirtualAddress);
170 }