mf/session: Forward more events to the application.
[wine/zf.git] / dlls / mscoree / assembly.c
blob240225a14da208c400af5e43015fe8e7dc043097
1 /*
2 * assembly parser
4 * Copyright 2008 James Hawkins
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.
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.
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
21 #define NONAMELESSUNION
23 #include <stdarg.h>
24 #include <stdio.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winver.h"
30 #include "dbghelp.h"
31 #include "ole2.h"
32 #include "mscoree.h"
33 #include "corhdr.h"
34 #include "metahost.h"
35 #include "cordebug.h"
36 #include "wine/list.h"
37 #include "mscoree_private.h"
39 #include "wine/debug.h"
41 typedef struct
43 ULONG Signature;
44 USHORT MajorVersion;
45 USHORT MinorVersion;
46 ULONG Reserved;
47 ULONG VersionLength;
48 LPSTR Version;
49 BYTE Flags;
50 WORD Streams;
51 } METADATAHDR;
53 typedef struct
55 DWORD Offset;
56 DWORD Size;
57 } METADATASTREAMHDR;
59 typedef struct tagCLRTABLE
61 INT rows;
62 DWORD offset;
63 } CLRTABLE;
65 struct tagASSEMBLY
67 BOOL is_mapped_file;
69 /* mapped files */
70 LPWSTR path;
71 HANDLE hfile;
72 HANDLE hmap;
74 BYTE *data;
76 IMAGE_NT_HEADERS *nthdr;
77 IMAGE_COR20_HEADER *corhdr;
79 METADATAHDR *metadatahdr;
82 static inline LPWSTR strdupW(LPCWSTR src)
84 LPWSTR dest;
86 if (!src)
87 return NULL;
89 dest = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(src) + 1) * sizeof(WCHAR));
90 if (dest)
91 lstrcpyW(dest, src);
93 return dest;
96 static void* assembly_rva_to_va(ASSEMBLY *assembly, ULONG rva)
98 if (assembly->is_mapped_file)
99 return ImageRvaToVa(assembly->nthdr, assembly->data, rva, NULL);
100 else
101 return assembly->data + rva;
104 static ULONG assembly_datadir_get_data(ASSEMBLY *assembly,
105 IMAGE_DATA_DIRECTORY *datadir, void **data)
107 if (!datadir->VirtualAddress || !datadir->Size)
109 *data = NULL;
110 return 0;
112 else
114 *data = assembly_rva_to_va(assembly, datadir->VirtualAddress);
115 return datadir->Size;
119 static HRESULT parse_metadata_header(ASSEMBLY *assembly, DWORD *hdrsz)
121 METADATAHDR *metadatahdr;
122 BYTE *ptr, *dest;
123 DWORD size, ofs;
124 ULONG rva;
126 rva = assembly->corhdr->MetaData.VirtualAddress;
127 ptr = assembly_rva_to_va(assembly, rva);
128 if (!ptr)
129 return E_FAIL;
131 metadatahdr = (METADATAHDR *)ptr;
133 assembly->metadatahdr = HeapAlloc(GetProcessHeap(), 0, sizeof(METADATAHDR));
134 if (!assembly->metadatahdr)
135 return E_OUTOFMEMORY;
137 size = FIELD_OFFSET(METADATAHDR, Version);
138 memcpy(assembly->metadatahdr, metadatahdr, size);
140 assembly->metadatahdr->Version = (LPSTR)&metadatahdr->Version;
142 ofs = FIELD_OFFSET(METADATAHDR, Flags);
143 ptr += FIELD_OFFSET(METADATAHDR, Version) + metadatahdr->VersionLength + 1;
144 dest = (BYTE *)assembly->metadatahdr + ofs;
145 memcpy(dest, ptr, sizeof(METADATAHDR) - ofs);
147 *hdrsz = sizeof(METADATAHDR) - sizeof(LPSTR) + metadatahdr->VersionLength + 1;
149 return S_OK;
152 static HRESULT parse_clr_metadata(ASSEMBLY *assembly)
154 HRESULT hr;
155 DWORD hdrsz;
157 hr = parse_metadata_header(assembly, &hdrsz);
158 if (FAILED(hr))
159 return hr;
161 return S_OK;
164 static HRESULT parse_pe_header(ASSEMBLY *assembly)
166 IMAGE_DATA_DIRECTORY *datadirs;
168 assembly->nthdr = ImageNtHeader(assembly->data);
169 if (!assembly->nthdr)
170 return E_FAIL;
172 if (assembly->nthdr->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
174 IMAGE_OPTIONAL_HEADER64 *opthdr =
175 (IMAGE_OPTIONAL_HEADER64 *)&assembly->nthdr->OptionalHeader;
176 datadirs = opthdr->DataDirectory;
178 else
180 IMAGE_OPTIONAL_HEADER32 *opthdr =
181 (IMAGE_OPTIONAL_HEADER32 *)&assembly->nthdr->OptionalHeader;
182 datadirs = opthdr->DataDirectory;
185 if (!datadirs)
186 return E_FAIL;
188 if (!assembly_datadir_get_data(assembly, &datadirs[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR], (void**)&assembly->corhdr))
189 return E_FAIL;
191 return S_OK;
194 static HRESULT parse_headers(ASSEMBLY *assembly)
196 HRESULT hr;
198 hr = parse_pe_header(assembly);
200 if (SUCCEEDED(hr))
201 hr = parse_clr_metadata(assembly);
203 return hr;
206 HRESULT assembly_create(ASSEMBLY **out, LPCWSTR file)
208 ASSEMBLY *assembly;
209 HRESULT hr;
211 *out = NULL;
213 assembly = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ASSEMBLY));
214 if (!assembly)
215 return E_OUTOFMEMORY;
217 assembly->is_mapped_file = TRUE;
219 assembly->path = strdupW(file);
220 if (!assembly->path)
222 hr = E_OUTOFMEMORY;
223 goto failed;
226 assembly->hfile = CreateFileW(file, GENERIC_READ, FILE_SHARE_READ,
227 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
228 if (assembly->hfile == INVALID_HANDLE_VALUE)
230 hr = HRESULT_FROM_WIN32(GetLastError());
231 goto failed;
234 assembly->hmap = CreateFileMappingW(assembly->hfile, NULL, PAGE_READONLY,
235 0, 0, NULL);
236 if (!assembly->hmap)
238 hr = HRESULT_FROM_WIN32(GetLastError());
239 goto failed;
242 assembly->data = MapViewOfFile(assembly->hmap, FILE_MAP_READ, 0, 0, 0);
243 if (!assembly->data)
245 hr = HRESULT_FROM_WIN32(GetLastError());
246 goto failed;
249 hr = parse_headers(assembly);
250 if (FAILED(hr)) goto failed;
252 *out = assembly;
253 return S_OK;
255 failed:
256 assembly_release(assembly);
257 return hr;
260 HRESULT assembly_from_hmodule(ASSEMBLY **out, HMODULE hmodule)
262 ASSEMBLY *assembly;
263 HRESULT hr;
265 *out = NULL;
267 assembly = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ASSEMBLY));
268 if (!assembly)
269 return E_OUTOFMEMORY;
271 assembly->is_mapped_file = FALSE;
273 assembly->data = (BYTE*)hmodule;
275 hr = parse_headers(assembly);
276 if (SUCCEEDED(hr))
277 *out = assembly;
278 else
279 assembly_release(assembly);
281 return hr;
284 HRESULT assembly_release(ASSEMBLY *assembly)
286 if (!assembly)
287 return S_OK;
289 if (assembly->is_mapped_file)
291 UnmapViewOfFile(assembly->data);
292 CloseHandle(assembly->hmap);
293 CloseHandle(assembly->hfile);
295 HeapFree(GetProcessHeap(), 0, assembly->metadatahdr);
296 HeapFree(GetProcessHeap(), 0, assembly->path);
297 HeapFree(GetProcessHeap(), 0, assembly);
299 return S_OK;
302 HRESULT assembly_get_runtime_version(ASSEMBLY *assembly, LPSTR *version)
304 *version = assembly->metadatahdr->Version;
306 return S_OK;
309 HRESULT assembly_get_vtable_fixups(ASSEMBLY *assembly, VTableFixup **fixups, DWORD *count)
311 ULONG size;
313 size = assembly_datadir_get_data(assembly, &assembly->corhdr->VTableFixups, (void**)fixups);
314 *count = size / sizeof(VTableFixup);
316 return S_OK;
319 HRESULT assembly_get_native_entrypoint(ASSEMBLY *assembly, NativeEntryPointFunc *func)
321 if (assembly->corhdr->Flags & COMIMAGE_FLAGS_NATIVE_ENTRYPOINT)
323 *func = assembly_rva_to_va(assembly, assembly->corhdr->u.EntryPointRVA);
324 return S_OK;
326 else
328 *func = NULL;
329 return S_FALSE;