2 * Copyright 2002 Dmitry Timoshkov for Codeweavers
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.
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.
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
24 #include "wine/exception.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
30 /* filter for page-fault exceptions */
31 static WINE_EXCEPTION_FILTER(page_fault)
33 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
34 return EXCEPTION_EXECUTE_HANDLER;
35 return EXCEPTION_CONTINUE_SEARCH;
39 NTSTATUS WINAPI LdrDisableThreadCalloutsForDll(HANDLE hModule)
41 if (DisableThreadLibraryCalls(hModule))
42 return STATUS_SUCCESS;
44 return STATUS_DLL_NOT_FOUND;
47 /* FIXME : MODULE_FindModule should depend on LdrGetDllHandle, not vice-versa */
49 NTSTATUS WINAPI LdrGetDllHandle(ULONG x, LONG y, PUNICODE_STRING name, PVOID *base)
54 FIXME("%08lx %08lx %s %p : partial stub\n",x,y,debugstr_wn(name->Buffer,name->Length),base);
58 RtlUnicodeStringToAnsiString(&str, name, TRUE);
59 wm = MODULE_FindModule(str.Buffer);
61 return STATUS_DLL_NOT_FOUND;
62 *base = (PVOID) wm->module;
64 return STATUS_SUCCESS;
67 /* FIXME : MODULE_GetProcAddress should depend on LdrGetProcedureAddress, not vice-versa */
69 NTSTATUS WINAPI LdrGetProcedureAddress(PVOID base, PANSI_STRING name, ULONG ord, PVOID *address)
71 WARN("%p %s %ld %p\n",base, debugstr_an(name->Buffer,name->Length), ord, address);
74 *address = MODULE_GetProcAddress( (HMODULE) base, name->Buffer, -1, FALSE);
76 *address = MODULE_GetProcAddress( (HMODULE) base, (LPSTR) ord, -1, FALSE);
78 return (*address) ? STATUS_SUCCESS : STATUS_DLL_NOT_FOUND;
82 /***********************************************************************
83 * RtlImageNtHeader (NTDLL.@)
85 PIMAGE_NT_HEADERS WINAPI RtlImageNtHeader(HMODULE hModule)
87 IMAGE_NT_HEADERS *ret;
91 IMAGE_DOS_HEADER *dos = (IMAGE_DOS_HEADER *)hModule;
94 if (dos->e_magic == IMAGE_DOS_SIGNATURE)
96 ret = (IMAGE_NT_HEADERS *)((char *)dos + dos->e_lfanew);
97 if (ret->Signature != IMAGE_NT_SIGNATURE) ret = NULL;
109 /***********************************************************************
110 * RtlImageDirectoryEntryToData (NTDLL.@)
112 PVOID WINAPI RtlImageDirectoryEntryToData( HMODULE module, BOOL image, WORD dir, ULONG *size )
114 const IMAGE_NT_HEADERS *nt;
117 if ((ULONG_PTR)module & 1) /* mapped as data file */
119 module = (HMODULE)((ULONG_PTR)module & ~1);
122 if (!(nt = RtlImageNtHeader( module ))) return NULL;
123 if (dir >= nt->OptionalHeader.NumberOfRvaAndSizes) return NULL;
124 if (!(addr = nt->OptionalHeader.DataDirectory[dir].VirtualAddress)) return NULL;
125 *size = nt->OptionalHeader.DataDirectory[dir].Size;
126 if (image || addr < nt->OptionalHeader.SizeOfHeaders) return (char *)module + addr;
128 /* not mapped as image, need to find the section containing the virtual address */
129 return RtlImageRvaToVa( nt, module, addr, NULL );
133 /***********************************************************************
134 * RtlImageRvaToSection (NTDLL.@)
136 PIMAGE_SECTION_HEADER WINAPI RtlImageRvaToSection( const IMAGE_NT_HEADERS *nt,
137 HMODULE module, DWORD rva )
140 IMAGE_SECTION_HEADER *sec = (IMAGE_SECTION_HEADER*)((char*)&nt->OptionalHeader +
141 nt->FileHeader.SizeOfOptionalHeader);
142 for (i = 0; i < nt->FileHeader.NumberOfSections; i++, sec++)
144 if ((sec->VirtualAddress <= rva) && (sec->VirtualAddress + sec->SizeOfRawData > rva))
151 /***********************************************************************
152 * RtlImageRvaToVa (NTDLL.@)
154 PVOID WINAPI RtlImageRvaToVa( const IMAGE_NT_HEADERS *nt, HMODULE module,
155 DWORD rva, IMAGE_SECTION_HEADER **section )
157 IMAGE_SECTION_HEADER *sec;
159 if (section && *section) /* try this section first */
162 if ((sec->VirtualAddress <= rva) && (sec->VirtualAddress + sec->SizeOfRawData > rva))
165 if (!(sec = RtlImageRvaToSection( nt, module, rva ))) return NULL;
167 if (section) *section = sec;
168 return (char *)module + sec->PointerToRawData + (rva - sec->VirtualAddress);