Fixed "conditional expr is always true due to being unsigned < 0"
[wine/dcerpc.git] / dlls / ntdll / loader.c
blob5d04cda7e5036717fe86c2d26c71eeed1c86535f
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 "winnt.h"
21 #include "winternl.h"
23 #include "module.h"
24 #include "wine/exception.h"
25 #include "msvcrt/excpt.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;
43 else
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)
51 STRING str;
52 WINE_MODREF *wm;
54 FIXME("%08lx %08lx %s %p : partial stub\n",x,y,debugstr_wn(name->Buffer,name->Length),base);
56 *base = 0;
58 RtlUnicodeStringToAnsiString(&str, name, TRUE);
59 wm = MODULE_FindModule(str.Buffer);
60 if(!wm)
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);
73 if(name)
74 *address = MODULE_GetProcAddress( (HMODULE) base, name->Buffer, -1, FALSE);
75 else
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;
89 __TRY
91 IMAGE_DOS_HEADER *dos = (IMAGE_DOS_HEADER *)hModule;
93 ret = NULL;
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;
100 __EXCEPT(page_fault)
102 return NULL;
104 __ENDTRY
105 return ret;
109 /***********************************************************************
110 * RtlImageDirectoryEntryToData (NTDLL.@)
112 PVOID WINAPI RtlImageDirectoryEntryToData( HMODULE module, BOOL image, WORD dir, ULONG *size )
114 const IMAGE_NT_HEADERS *nt;
115 DWORD addr;
117 if ((ULONG_PTR)module & 1) /* mapped as data file */
119 module = (HMODULE)((ULONG_PTR)module & ~1);
120 image = FALSE;
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 )
139 int i;
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))
145 return sec;
147 return NULL;
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 */
161 sec = *section;
162 if ((sec->VirtualAddress <= rva) && (sec->VirtualAddress + sec->SizeOfRawData > rva))
163 goto found;
165 if (!(sec = RtlImageRvaToSection( nt, module, rva ))) return NULL;
166 found:
167 if (section) *section = sec;
168 return (char *)module + sec->PointerToRawData + (rva - sec->VirtualAddress);