Fixed some issues found by winapi_check.
[wine/dcerpc.git] / dlls / ntdll / loader.c
blob4440426c6e0d58e217132e11f5e9b7a8f9e24cd7
1 /*
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
19 #include "winbase.h"
20 #include "ntdef.h"
21 #include "winnt.h"
22 #include "ntddk.h"
24 #include "module.h"
25 #include "wine/exception.h"
26 #include "msvcrt/excpt.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
31 /* filter for page-fault exceptions */
32 static WINE_EXCEPTION_FILTER(page_fault)
34 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
35 return EXCEPTION_EXECUTE_HANDLER;
36 return EXCEPTION_CONTINUE_SEARCH;
40 NTSTATUS WINAPI LdrDisableThreadCalloutsForDll(HANDLE hModule)
42 if (DisableThreadLibraryCalls(hModule))
43 return STATUS_SUCCESS;
44 else
45 return STATUS_DLL_NOT_FOUND;
48 /* FIXME : MODULE_FindModule should depend on LdrGetDllHandle, not vice-versa */
50 NTSTATUS WINAPI LdrGetDllHandle(ULONG x, LONG y, PUNICODE_STRING name, PVOID *base)
52 STRING str;
53 WINE_MODREF *wm;
55 FIXME("%08lx %08lx %s %p : partial stub\n",x,y,debugstr_wn(name->Buffer,name->Length),base);
57 *base = 0;
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;
65 return STATUS_SUCCESS;
68 /* FIXME : MODULE_GetProcAddress should depend on LdrGetProcedureAddress, not vice-versa */
70 NTSTATUS WINAPI LdrGetProcedureAddress(PVOID base, PANSI_STRING name, ULONG ord, PVOID *address)
72 WARN("%p %s %ld %p\n",base, debugstr_an(name->Buffer,name->Length), ord, address);
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);
79 return (*address) ? STATUS_SUCCESS : STATUS_DLL_NOT_FOUND;
83 /***********************************************************************
84 * RtlImageNtHeader (NTDLL.@)
86 PIMAGE_NT_HEADERS WINAPI RtlImageNtHeader(HMODULE hModule)
88 IMAGE_NT_HEADERS *ret;
90 __TRY
92 IMAGE_DOS_HEADER *dos = (IMAGE_DOS_HEADER *)hModule;
94 ret = NULL;
95 if (dos->e_magic == IMAGE_DOS_SIGNATURE)
97 ret = (IMAGE_NT_HEADERS *)((char *)dos + dos->e_lfanew);
98 if (ret->Signature != IMAGE_NT_SIGNATURE) ret = NULL;
101 __EXCEPT(page_fault)
103 return NULL;
105 __ENDTRY
106 return ret;
110 /***********************************************************************
111 * RtlImageDirectoryEntryToData (NTDLL.@)
113 PVOID WINAPI RtlImageDirectoryEntryToData( HMODULE module, BOOL image, WORD dir, ULONG *size )
115 const IMAGE_NT_HEADERS *nt;
116 DWORD addr;
118 if ((ULONG_PTR)module & 1) /* mapped as data file */
120 module = (HMODULE)((ULONG_PTR)module & ~1);
121 image = FALSE;
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;
129 /* not mapped as image, need to find the section containing the virtual address */
130 return RtlImageRvaToVa( nt, module, addr, NULL );
134 /***********************************************************************
135 * RtlImageRvaToSection (NTDLL.@)
137 PIMAGE_SECTION_HEADER WINAPI RtlImageRvaToSection( const IMAGE_NT_HEADERS *nt,
138 HMODULE module, DWORD rva )
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++)
145 if ((sec->VirtualAddress <= rva) && (sec->VirtualAddress + sec->SizeOfRawData > rva))
146 return sec;
148 return NULL;
152 /***********************************************************************
153 * RtlImageRvaToVa (NTDLL.@)
155 PVOID WINAPI RtlImageRvaToVa( const IMAGE_NT_HEADERS *nt, HMODULE module,
156 DWORD rva, IMAGE_SECTION_HEADER **section )
158 IMAGE_SECTION_HEADER *sec;
160 if (section && *section) /* try this section first */
162 sec = *section;
163 if ((sec->VirtualAddress <= rva) && (sec->VirtualAddress + sec->SizeOfRawData > rva))
164 goto found;
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);