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
36 #include "wine/list.h"
37 #include "mscoree_private.h"
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
60 typedef struct tagCLRTABLE
77 IMAGE_NT_HEADERS
*nthdr
;
78 IMAGE_COR20_HEADER
*corhdr
;
80 METADATAHDR
*metadatahdr
;
83 static inline LPWSTR
strdupW(LPCWSTR src
)
90 dest
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(src
) + 1) * sizeof(WCHAR
));
97 static void* assembly_rva_to_va(ASSEMBLY
*assembly
, ULONG rva
)
99 if (assembly
->is_mapped_file
)
100 return ImageRvaToVa(assembly
->nthdr
, assembly
->data
, rva
, NULL
);
102 return assembly
->data
+ rva
;
105 static ULONG
assembly_datadir_get_data(ASSEMBLY
*assembly
,
106 IMAGE_DATA_DIRECTORY
*datadir
, void **data
)
108 if (!datadir
->VirtualAddress
|| !datadir
->Size
)
115 *data
= assembly_rva_to_va(assembly
, datadir
->VirtualAddress
);
116 return datadir
->Size
;
120 static HRESULT
parse_metadata_header(ASSEMBLY
*assembly
, DWORD
*hdrsz
)
122 METADATAHDR
*metadatahdr
;
127 rva
= assembly
->corhdr
->MetaData
.VirtualAddress
;
128 ptr
= assembly_rva_to_va(assembly
, rva
);
132 metadatahdr
= (METADATAHDR
*)ptr
;
134 assembly
->metadatahdr
= HeapAlloc(GetProcessHeap(), 0, sizeof(METADATAHDR
));
135 if (!assembly
->metadatahdr
)
136 return E_OUTOFMEMORY
;
138 size
= FIELD_OFFSET(METADATAHDR
, Version
);
139 memcpy(assembly
->metadatahdr
, metadatahdr
, size
);
141 assembly
->metadatahdr
->Version
= (LPSTR
)&metadatahdr
->Version
;
143 ofs
= FIELD_OFFSET(METADATAHDR
, Flags
);
144 ptr
+= FIELD_OFFSET(METADATAHDR
, Version
) + metadatahdr
->VersionLength
+ 1;
145 dest
= (BYTE
*)assembly
->metadatahdr
+ ofs
;
146 memcpy(dest
, ptr
, sizeof(METADATAHDR
) - ofs
);
148 *hdrsz
= sizeof(METADATAHDR
) - sizeof(LPSTR
) + metadatahdr
->VersionLength
+ 1;
153 static HRESULT
parse_clr_metadata(ASSEMBLY
*assembly
)
158 hr
= parse_metadata_header(assembly
, &hdrsz
);
165 static HRESULT
parse_pe_header(ASSEMBLY
*assembly
)
167 IMAGE_DATA_DIRECTORY
*datadirs
;
169 assembly
->nthdr
= ImageNtHeader(assembly
->data
);
170 if (!assembly
->nthdr
)
173 if (assembly
->nthdr
->OptionalHeader
.Magic
== IMAGE_NT_OPTIONAL_HDR64_MAGIC
)
175 IMAGE_OPTIONAL_HEADER64
*opthdr
=
176 (IMAGE_OPTIONAL_HEADER64
*)&assembly
->nthdr
->OptionalHeader
;
177 datadirs
= opthdr
->DataDirectory
;
181 IMAGE_OPTIONAL_HEADER32
*opthdr
=
182 (IMAGE_OPTIONAL_HEADER32
*)&assembly
->nthdr
->OptionalHeader
;
183 datadirs
= opthdr
->DataDirectory
;
189 if (!assembly_datadir_get_data(assembly
, &datadirs
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
], (void**)&assembly
->corhdr
))
195 static HRESULT
parse_headers(ASSEMBLY
*assembly
)
199 hr
= parse_pe_header(assembly
);
202 hr
= parse_clr_metadata(assembly
);
207 HRESULT
assembly_create(ASSEMBLY
**out
, LPCWSTR file
)
214 assembly
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(ASSEMBLY
));
216 return E_OUTOFMEMORY
;
218 assembly
->is_mapped_file
= TRUE
;
220 assembly
->path
= strdupW(file
);
227 assembly
->hfile
= CreateFileW(file
, GENERIC_READ
, FILE_SHARE_READ
,
228 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
229 if (assembly
->hfile
== INVALID_HANDLE_VALUE
)
231 hr
= HRESULT_FROM_WIN32(GetLastError());
235 assembly
->hmap
= CreateFileMappingW(assembly
->hfile
, NULL
, PAGE_READONLY
,
239 hr
= HRESULT_FROM_WIN32(GetLastError());
243 assembly
->data
= MapViewOfFile(assembly
->hmap
, FILE_MAP_READ
, 0, 0, 0);
246 hr
= HRESULT_FROM_WIN32(GetLastError());
250 hr
= parse_headers(assembly
);
251 if (FAILED(hr
)) goto failed
;
257 assembly_release(assembly
);
261 HRESULT
assembly_from_hmodule(ASSEMBLY
**out
, HMODULE hmodule
)
268 assembly
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(ASSEMBLY
));
270 return E_OUTOFMEMORY
;
272 assembly
->is_mapped_file
= FALSE
;
274 assembly
->data
= (BYTE
*)hmodule
;
276 hr
= parse_headers(assembly
);
280 assembly_release(assembly
);
285 HRESULT
assembly_release(ASSEMBLY
*assembly
)
290 if (assembly
->is_mapped_file
)
292 UnmapViewOfFile(assembly
->data
);
293 CloseHandle(assembly
->hmap
);
294 CloseHandle(assembly
->hfile
);
296 HeapFree(GetProcessHeap(), 0, assembly
->metadatahdr
);
297 HeapFree(GetProcessHeap(), 0, assembly
->path
);
298 HeapFree(GetProcessHeap(), 0, assembly
);
303 HRESULT
assembly_get_runtime_version(ASSEMBLY
*assembly
, LPSTR
*version
)
305 *version
= assembly
->metadatahdr
->Version
;
310 HRESULT
assembly_get_vtable_fixups(ASSEMBLY
*assembly
, VTableFixup
**fixups
, DWORD
*count
)
314 size
= assembly_datadir_get_data(assembly
, &assembly
->corhdr
->VTableFixups
, (void**)fixups
);
315 *count
= size
/ sizeof(VTableFixup
);
320 HRESULT
assembly_get_native_entrypoint(ASSEMBLY
*assembly
, NativeEntryPointFunc
*func
)
322 if (assembly
->corhdr
->Flags
& COMIMAGE_FLAGS_NATIVE_ENTRYPOINT
)
324 *func
= assembly_rva_to_va(assembly
, assembly
->corhdr
->u
.EntryPointRVA
);