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"
59 typedef struct tagCLRTABLE
76 IMAGE_NT_HEADERS
*nthdr
;
77 IMAGE_COR20_HEADER
*corhdr
;
79 METADATAHDR
*metadatahdr
;
82 static inline LPWSTR
strdupW(LPCWSTR src
)
89 dest
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(src
) + 1) * sizeof(WCHAR
));
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
);
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
)
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
;
126 rva
= assembly
->corhdr
->MetaData
.VirtualAddress
;
127 ptr
= assembly_rva_to_va(assembly
, rva
);
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;
152 static HRESULT
parse_clr_metadata(ASSEMBLY
*assembly
)
157 hr
= parse_metadata_header(assembly
, &hdrsz
);
164 static HRESULT
parse_pe_header(ASSEMBLY
*assembly
)
166 IMAGE_DATA_DIRECTORY
*datadirs
;
168 assembly
->nthdr
= ImageNtHeader(assembly
->data
);
169 if (!assembly
->nthdr
)
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
;
180 IMAGE_OPTIONAL_HEADER32
*opthdr
=
181 (IMAGE_OPTIONAL_HEADER32
*)&assembly
->nthdr
->OptionalHeader
;
182 datadirs
= opthdr
->DataDirectory
;
188 if (!assembly_datadir_get_data(assembly
, &datadirs
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
], (void**)&assembly
->corhdr
))
194 static HRESULT
parse_headers(ASSEMBLY
*assembly
)
198 hr
= parse_pe_header(assembly
);
201 hr
= parse_clr_metadata(assembly
);
206 HRESULT
assembly_create(ASSEMBLY
**out
, LPCWSTR file
)
213 assembly
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(ASSEMBLY
));
215 return E_OUTOFMEMORY
;
217 assembly
->is_mapped_file
= TRUE
;
219 assembly
->path
= strdupW(file
);
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());
234 assembly
->hmap
= CreateFileMappingW(assembly
->hfile
, NULL
, PAGE_READONLY
,
238 hr
= HRESULT_FROM_WIN32(GetLastError());
242 assembly
->data
= MapViewOfFile(assembly
->hmap
, FILE_MAP_READ
, 0, 0, 0);
245 hr
= HRESULT_FROM_WIN32(GetLastError());
249 hr
= parse_headers(assembly
);
250 if (FAILED(hr
)) goto failed
;
256 assembly_release(assembly
);
260 HRESULT
assembly_from_hmodule(ASSEMBLY
**out
, HMODULE hmodule
)
267 assembly
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(ASSEMBLY
));
269 return E_OUTOFMEMORY
;
271 assembly
->is_mapped_file
= FALSE
;
273 assembly
->data
= (BYTE
*)hmodule
;
275 hr
= parse_headers(assembly
);
279 assembly_release(assembly
);
284 HRESULT
assembly_release(ASSEMBLY
*assembly
)
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
);
302 HRESULT
assembly_get_runtime_version(ASSEMBLY
*assembly
, LPSTR
*version
)
304 *version
= assembly
->metadatahdr
->Version
;
309 HRESULT
assembly_get_vtable_fixups(ASSEMBLY
*assembly
, VTableFixup
**fixups
, DWORD
*count
)
313 size
= assembly_datadir_get_data(assembly
, &assembly
->corhdr
->VTableFixups
, (void**)fixups
);
314 *count
= size
/ sizeof(VTableFixup
);
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
);