[MSCOREE]
[reactos.git] / reactos / dll / win32 / mscoree / assembly.c
1 /*
2 * assembly parser
3 *
4 * Copyright 2008 James Hawkins
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "winver.h"
28 #include "dbghelp.h"
29 #include "ole2.h"
30 #include "mscoree.h"
31 #include "corhdr.h"
32 #include "metahost.h"
33 #include "cordebug.h"
34 #include "wine/list.h"
35 #include "mscoree_private.h"
36
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
39
40 typedef struct
41 {
42 ULONG Signature;
43 USHORT MajorVersion;
44 USHORT MinorVersion;
45 ULONG Reserved;
46 ULONG VersionLength;
47 LPSTR Version;
48 BYTE Flags;
49 WORD Streams;
50 } METADATAHDR;
51
52 typedef struct
53 {
54 DWORD Offset;
55 DWORD Size;
56 } METADATASTREAMHDR;
57
58 typedef struct tagCLRTABLE
59 {
60 INT rows;
61 DWORD offset;
62 } CLRTABLE;
63
64 struct tagASSEMBLY
65 {
66 int is_mapped_file;
67
68 /* mapped files */
69 LPWSTR path;
70 HANDLE hfile;
71 HANDLE hmap;
72
73 BYTE *data;
74
75 IMAGE_NT_HEADERS *nthdr;
76 IMAGE_COR20_HEADER *corhdr;
77
78 METADATAHDR *metadatahdr;
79 };
80
81 static inline LPWSTR strdupW(LPCWSTR src)
82 {
83 LPWSTR dest;
84
85 if (!src)
86 return NULL;
87
88 dest = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(src) + 1) * sizeof(WCHAR));
89 if (dest)
90 lstrcpyW(dest, src);
91
92 return dest;
93 }
94
95 static void* assembly_rva_to_va(ASSEMBLY *assembly, ULONG rva)
96 {
97 if (assembly->is_mapped_file)
98 return ImageRvaToVa(assembly->nthdr, assembly->data, rva, NULL);
99 else
100 return assembly->data + rva;
101 }
102
103 static ULONG assembly_datadir_get_data(ASSEMBLY *assembly,
104 IMAGE_DATA_DIRECTORY *datadir, void **data)
105 {
106 if (!datadir->VirtualAddress || !datadir->Size)
107 {
108 *data = NULL;
109 return 0;
110 }
111 else
112 {
113 *data = assembly_rva_to_va(assembly, datadir->VirtualAddress);
114 return datadir->Size;
115 }
116 }
117
118 static HRESULT parse_metadata_header(ASSEMBLY *assembly, DWORD *hdrsz)
119 {
120 METADATAHDR *metadatahdr;
121 BYTE *ptr, *dest;
122 DWORD size, ofs;
123 ULONG rva;
124
125 rva = assembly->corhdr->MetaData.VirtualAddress;
126 ptr = assembly_rva_to_va(assembly, rva);
127 if (!ptr)
128 return E_FAIL;
129
130 metadatahdr = (METADATAHDR *)ptr;
131
132 assembly->metadatahdr = HeapAlloc(GetProcessHeap(), 0, sizeof(METADATAHDR));
133 if (!assembly->metadatahdr)
134 return E_OUTOFMEMORY;
135
136 size = FIELD_OFFSET(METADATAHDR, Version);
137 memcpy(assembly->metadatahdr, metadatahdr, size);
138
139 assembly->metadatahdr->Version = (LPSTR)&metadatahdr->Version;
140
141 ofs = FIELD_OFFSET(METADATAHDR, Flags);
142 ptr += FIELD_OFFSET(METADATAHDR, Version) + metadatahdr->VersionLength + 1;
143 dest = (BYTE *)assembly->metadatahdr + ofs;
144 memcpy(dest, ptr, sizeof(METADATAHDR) - ofs);
145
146 *hdrsz = sizeof(METADATAHDR) - sizeof(LPSTR) + metadatahdr->VersionLength + 1;
147
148 return S_OK;
149 }
150
151 static HRESULT parse_clr_metadata(ASSEMBLY *assembly)
152 {
153 HRESULT hr;
154 DWORD hdrsz;
155
156 hr = parse_metadata_header(assembly, &hdrsz);
157 if (FAILED(hr))
158 return hr;
159
160 return S_OK;
161 }
162
163 static HRESULT parse_pe_header(ASSEMBLY *assembly)
164 {
165 IMAGE_DATA_DIRECTORY *datadirs;
166
167 assembly->nthdr = ImageNtHeader(assembly->data);
168 if (!assembly->nthdr)
169 return E_FAIL;
170
171 if (assembly->nthdr->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
172 {
173 IMAGE_OPTIONAL_HEADER64 *opthdr =
174 (IMAGE_OPTIONAL_HEADER64 *)&assembly->nthdr->OptionalHeader;
175 datadirs = opthdr->DataDirectory;
176 }
177 else
178 {
179 IMAGE_OPTIONAL_HEADER32 *opthdr =
180 (IMAGE_OPTIONAL_HEADER32 *)&assembly->nthdr->OptionalHeader;
181 datadirs = opthdr->DataDirectory;
182 }
183
184 if (!datadirs)
185 return E_FAIL;
186
187 if (!assembly_datadir_get_data(assembly, &datadirs[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR], (void**)&assembly->corhdr))
188 return E_FAIL;
189
190 return S_OK;
191 }
192
193 static HRESULT parse_headers(ASSEMBLY *assembly)
194 {
195 HRESULT hr;
196
197 hr = parse_pe_header(assembly);
198
199 if (SUCCEEDED(hr))
200 hr = parse_clr_metadata(assembly);
201
202 return hr;
203 }
204
205 HRESULT assembly_create(ASSEMBLY **out, LPCWSTR file)
206 {
207 ASSEMBLY *assembly;
208 HRESULT hr;
209
210 *out = NULL;
211
212 assembly = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ASSEMBLY));
213 if (!assembly)
214 return E_OUTOFMEMORY;
215
216 assembly->is_mapped_file = 1;
217
218 assembly->path = strdupW(file);
219 if (!assembly->path)
220 {
221 hr = E_OUTOFMEMORY;
222 goto failed;
223 }
224
225 assembly->hfile = CreateFileW(file, GENERIC_READ, FILE_SHARE_READ,
226 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
227 if (assembly->hfile == INVALID_HANDLE_VALUE)
228 {
229 hr = HRESULT_FROM_WIN32(GetLastError());
230 goto failed;
231 }
232
233 assembly->hmap = CreateFileMappingW(assembly->hfile, NULL, PAGE_READONLY,
234 0, 0, NULL);
235 if (!assembly->hmap)
236 {
237 hr = HRESULT_FROM_WIN32(GetLastError());
238 goto failed;
239 }
240
241 assembly->data = MapViewOfFile(assembly->hmap, FILE_MAP_READ, 0, 0, 0);
242 if (!assembly->data)
243 {
244 hr = HRESULT_FROM_WIN32(GetLastError());
245 goto failed;
246 }
247
248 hr = parse_headers(assembly);
249 if (FAILED(hr)) goto failed;
250
251 *out = assembly;
252 return S_OK;
253
254 failed:
255 assembly_release(assembly);
256 return hr;
257 }
258
259 HRESULT assembly_from_hmodule(ASSEMBLY **out, HMODULE hmodule)
260 {
261 ASSEMBLY *assembly;
262 HRESULT hr;
263
264 *out = NULL;
265
266 assembly = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ASSEMBLY));
267 if (!assembly)
268 return E_OUTOFMEMORY;
269
270 assembly->is_mapped_file = 0;
271
272 assembly->data = (BYTE*)hmodule;
273
274 hr = parse_headers(assembly);
275 if (SUCCEEDED(hr))
276 *out = assembly;
277 else
278 assembly_release(assembly);
279
280 return hr;
281 }
282
283 HRESULT assembly_release(ASSEMBLY *assembly)
284 {
285 if (!assembly)
286 return S_OK;
287
288 if (assembly->is_mapped_file)
289 {
290 UnmapViewOfFile(assembly->data);
291 CloseHandle(assembly->hmap);
292 CloseHandle(assembly->hfile);
293 }
294 HeapFree(GetProcessHeap(), 0, assembly->metadatahdr);
295 HeapFree(GetProcessHeap(), 0, assembly->path);
296 HeapFree(GetProcessHeap(), 0, assembly);
297
298 return S_OK;
299 }
300
301 HRESULT assembly_get_runtime_version(ASSEMBLY *assembly, LPSTR *version)
302 {
303 *version = assembly->metadatahdr->Version;
304
305 return S_OK;
306 }
307
308 HRESULT assembly_get_vtable_fixups(ASSEMBLY *assembly, VTableFixup **fixups, DWORD *count)
309 {
310 ULONG size;
311
312 size = assembly_datadir_get_data(assembly, &assembly->corhdr->VTableFixups, (void**)fixups);
313 *count = size / sizeof(VTableFixup);
314
315 return S_OK;
316 }