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
75 IMAGE_NT_HEADERS
*nthdr
;
76 IMAGE_COR20_HEADER
*corhdr
;
78 METADATAHDR
*metadatahdr
;
81 static inline LPWSTR
strdupW(LPCWSTR src
)
88 dest
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(src
) + 1) * sizeof(WCHAR
));
95 static void* assembly_rva_to_va(ASSEMBLY
*assembly
, ULONG rva
)
97 if (assembly
->is_mapped_file
)
98 return ImageRvaToVa(assembly
->nthdr
, assembly
->data
, rva
, NULL
);
100 return assembly
->data
+ rva
;
103 static ULONG
assembly_datadir_get_data(ASSEMBLY
*assembly
,
104 IMAGE_DATA_DIRECTORY
*datadir
, void **data
)
106 if (!datadir
->VirtualAddress
|| !datadir
->Size
)
113 *data
= assembly_rva_to_va(assembly
, datadir
->VirtualAddress
);
114 return datadir
->Size
;
118 static HRESULT
parse_metadata_header(ASSEMBLY
*assembly
, DWORD
*hdrsz
)
120 METADATAHDR
*metadatahdr
;
125 rva
= assembly
->corhdr
->MetaData
.VirtualAddress
;
126 ptr
= assembly_rva_to_va(assembly
, rva
);
130 metadatahdr
= (METADATAHDR
*)ptr
;
132 assembly
->metadatahdr
= HeapAlloc(GetProcessHeap(), 0, sizeof(METADATAHDR
));
133 if (!assembly
->metadatahdr
)
134 return E_OUTOFMEMORY
;
136 size
= FIELD_OFFSET(METADATAHDR
, Version
);
137 memcpy(assembly
->metadatahdr
, metadatahdr
, size
);
139 assembly
->metadatahdr
->Version
= (LPSTR
)&metadatahdr
->Version
;
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
);
146 *hdrsz
= sizeof(METADATAHDR
) - sizeof(LPSTR
) + metadatahdr
->VersionLength
+ 1;
151 static HRESULT
parse_clr_metadata(ASSEMBLY
*assembly
)
156 hr
= parse_metadata_header(assembly
, &hdrsz
);
163 static HRESULT
parse_pe_header(ASSEMBLY
*assembly
)
165 IMAGE_DATA_DIRECTORY
*datadirs
;
167 assembly
->nthdr
= ImageNtHeader(assembly
->data
);
168 if (!assembly
->nthdr
)
171 if (assembly
->nthdr
->OptionalHeader
.Magic
== IMAGE_NT_OPTIONAL_HDR64_MAGIC
)
173 IMAGE_OPTIONAL_HEADER64
*opthdr
=
174 (IMAGE_OPTIONAL_HEADER64
*)&assembly
->nthdr
->OptionalHeader
;
175 datadirs
= opthdr
->DataDirectory
;
179 IMAGE_OPTIONAL_HEADER32
*opthdr
=
180 (IMAGE_OPTIONAL_HEADER32
*)&assembly
->nthdr
->OptionalHeader
;
181 datadirs
= opthdr
->DataDirectory
;
187 if (!assembly_datadir_get_data(assembly
, &datadirs
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
], (void**)&assembly
->corhdr
))
193 static HRESULT
parse_headers(ASSEMBLY
*assembly
)
197 hr
= parse_pe_header(assembly
);
200 hr
= parse_clr_metadata(assembly
);
205 HRESULT
assembly_create(ASSEMBLY
**out
, LPCWSTR file
)
212 assembly
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(ASSEMBLY
));
214 return E_OUTOFMEMORY
;
216 assembly
->is_mapped_file
= TRUE
;
218 assembly
->path
= strdupW(file
);
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
)
229 hr
= HRESULT_FROM_WIN32(GetLastError());
233 assembly
->hmap
= CreateFileMappingW(assembly
->hfile
, NULL
, PAGE_READONLY
,
237 hr
= HRESULT_FROM_WIN32(GetLastError());
241 assembly
->data
= MapViewOfFile(assembly
->hmap
, FILE_MAP_READ
, 0, 0, 0);
244 hr
= HRESULT_FROM_WIN32(GetLastError());
248 hr
= parse_headers(assembly
);
249 if (FAILED(hr
)) goto failed
;
255 assembly_release(assembly
);
259 HRESULT
assembly_from_hmodule(ASSEMBLY
**out
, HMODULE hmodule
)
266 assembly
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(ASSEMBLY
));
268 return E_OUTOFMEMORY
;
270 assembly
->is_mapped_file
= FALSE
;
272 assembly
->data
= (BYTE
*)hmodule
;
274 hr
= parse_headers(assembly
);
278 assembly_release(assembly
);
283 HRESULT
assembly_release(ASSEMBLY
*assembly
)
288 if (assembly
->is_mapped_file
)
290 UnmapViewOfFile(assembly
->data
);
291 CloseHandle(assembly
->hmap
);
292 CloseHandle(assembly
->hfile
);
294 HeapFree(GetProcessHeap(), 0, assembly
->metadatahdr
);
295 HeapFree(GetProcessHeap(), 0, assembly
->path
);
296 HeapFree(GetProcessHeap(), 0, assembly
);
301 HRESULT
assembly_get_runtime_version(ASSEMBLY
*assembly
, LPSTR
*version
)
303 *version
= assembly
->metadatahdr
->Version
;
308 HRESULT
assembly_get_vtable_fixups(ASSEMBLY
*assembly
, VTableFixup
**fixups
, DWORD
*count
)
312 size
= assembly_datadir_get_data(assembly
, &assembly
->corhdr
->VTableFixups
, (void**)fixups
);
313 *count
= size
/ sizeof(VTableFixup
);