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
34 #include "wine/list.h"
35 #include "mscoree_private.h"
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
58 typedef struct tagCLRTABLE
72 IMAGE_NT_HEADERS *nthdr;
73 IMAGE_COR20_HEADER *corhdr;
75 METADATAHDR *metadatahdr;
78 static inline LPWSTR strdupW(LPCWSTR src)
85 dest = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(src) + 1) * sizeof(WCHAR));
92 static HRESULT parse_metadata_header(ASSEMBLY *assembly, DWORD *hdrsz)
94 METADATAHDR *metadatahdr;
99 rva = assembly->corhdr->MetaData.VirtualAddress;
100 ptr = ImageRvaToVa(assembly->nthdr, assembly->data, rva, NULL);
104 metadatahdr = (METADATAHDR *)ptr;
106 assembly->metadatahdr = HeapAlloc(GetProcessHeap(), 0, sizeof(METADATAHDR));
107 if (!assembly->metadatahdr)
108 return E_OUTOFMEMORY;
110 size = FIELD_OFFSET(METADATAHDR, Version);
111 memcpy(assembly->metadatahdr, metadatahdr, size);
113 assembly->metadatahdr->Version = (LPSTR)&metadatahdr->Version;
115 ofs = FIELD_OFFSET(METADATAHDR, Flags);
116 ptr += FIELD_OFFSET(METADATAHDR, Version) + metadatahdr->VersionLength + 1;
117 dest = (BYTE *)assembly->metadatahdr + ofs;
118 memcpy(dest, ptr, sizeof(METADATAHDR) - ofs);
120 *hdrsz = sizeof(METADATAHDR) - sizeof(LPSTR) + metadatahdr->VersionLength + 1;
125 static HRESULT parse_clr_metadata(ASSEMBLY *assembly)
130 hr = parse_metadata_header(assembly, &hdrsz);
137 static HRESULT parse_pe_header(ASSEMBLY *assembly)
139 IMAGE_DATA_DIRECTORY *datadirs;
141 assembly->nthdr = ImageNtHeader(assembly->data);
142 if (!assembly->nthdr)
145 if (assembly->nthdr->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
147 IMAGE_OPTIONAL_HEADER64 *opthdr =
148 (IMAGE_OPTIONAL_HEADER64 *)&assembly->nthdr->OptionalHeader;
149 datadirs = opthdr->DataDirectory;
153 IMAGE_OPTIONAL_HEADER32 *opthdr =
154 (IMAGE_OPTIONAL_HEADER32 *)&assembly->nthdr->OptionalHeader;
155 datadirs = opthdr->DataDirectory;
161 if (!datadirs[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress ||
162 !datadirs[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size)
167 assembly->corhdr = ImageRvaToVa(assembly->nthdr, assembly->data,
168 datadirs[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress, NULL);
169 if (!assembly->corhdr)
175 HRESULT assembly_create(ASSEMBLY **out, LPCWSTR file)
182 assembly = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ASSEMBLY));
184 return E_OUTOFMEMORY;
186 assembly->path = strdupW(file);
193 assembly->hfile = CreateFileW(file, GENERIC_READ, FILE_SHARE_READ,
194 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
195 if (assembly->hfile == INVALID_HANDLE_VALUE)
197 hr = HRESULT_FROM_WIN32(GetLastError());
201 assembly->hmap = CreateFileMappingW(assembly->hfile, NULL, PAGE_READONLY,
205 hr = HRESULT_FROM_WIN32(GetLastError());
209 assembly->data = MapViewOfFile(assembly->hmap, FILE_MAP_READ, 0, 0, 0);
212 hr = HRESULT_FROM_WIN32(GetLastError());
216 hr = parse_pe_header(assembly);
217 if (FAILED(hr)) goto failed;
219 hr = parse_clr_metadata(assembly);
220 if (FAILED(hr)) goto failed;
226 assembly_release(assembly);
230 HRESULT assembly_release(ASSEMBLY *assembly)
235 HeapFree(GetProcessHeap(), 0, assembly->metadatahdr);
236 HeapFree(GetProcessHeap(), 0, assembly->path);
237 UnmapViewOfFile(assembly->data);
238 CloseHandle(assembly->hmap);
239 CloseHandle(assembly->hfile);
240 HeapFree(GetProcessHeap(), 0, assembly);
245 HRESULT assembly_get_runtime_version(ASSEMBLY *assembly, LPSTR *version)
247 *version = assembly->metadatahdr->Version;