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
32 #include "mscoree_private.h"
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
55 typedef struct tagCLRTABLE
69 IMAGE_NT_HEADERS
*nthdr
;
70 IMAGE_COR20_HEADER
*corhdr
;
72 METADATAHDR
*metadatahdr
;
75 static inline LPWSTR
strdupW(LPCWSTR src
)
82 dest
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(src
) + 1) * sizeof(WCHAR
));
89 static HRESULT
parse_metadata_header(ASSEMBLY
*assembly
, DWORD
*hdrsz
)
91 METADATAHDR
*metadatahdr
;
96 rva
= assembly
->corhdr
->MetaData
.VirtualAddress
;
97 ptr
= ImageRvaToVa(assembly
->nthdr
, assembly
->data
, rva
, NULL
);
101 metadatahdr
= (METADATAHDR
*)ptr
;
103 assembly
->metadatahdr
= HeapAlloc(GetProcessHeap(), 0, sizeof(METADATAHDR
));
104 if (!assembly
->metadatahdr
)
105 return E_OUTOFMEMORY
;
107 size
= FIELD_OFFSET(METADATAHDR
, Version
);
108 memcpy(assembly
->metadatahdr
, metadatahdr
, size
);
110 assembly
->metadatahdr
->Version
= (LPSTR
)&metadatahdr
->Version
;
112 ofs
= FIELD_OFFSET(METADATAHDR
, Flags
);
113 ptr
+= FIELD_OFFSET(METADATAHDR
, Version
) + metadatahdr
->VersionLength
+ 1;
114 dest
= (BYTE
*)assembly
->metadatahdr
+ ofs
;
115 memcpy(dest
, ptr
, sizeof(METADATAHDR
) - ofs
);
117 *hdrsz
= sizeof(METADATAHDR
) - sizeof(LPSTR
) + metadatahdr
->VersionLength
+ 1;
122 static HRESULT
parse_clr_metadata(ASSEMBLY
*assembly
)
127 hr
= parse_metadata_header(assembly
, &hdrsz
);
134 static HRESULT
parse_pe_header(ASSEMBLY
*assembly
)
136 IMAGE_DATA_DIRECTORY
*datadirs
;
138 assembly
->nthdr
= ImageNtHeader(assembly
->data
);
139 if (!assembly
->nthdr
)
142 if (assembly
->nthdr
->OptionalHeader
.Magic
== IMAGE_NT_OPTIONAL_HDR64_MAGIC
)
144 IMAGE_OPTIONAL_HEADER64
*opthdr
=
145 (IMAGE_OPTIONAL_HEADER64
*)&assembly
->nthdr
->OptionalHeader
;
146 datadirs
= opthdr
->DataDirectory
;
150 IMAGE_OPTIONAL_HEADER32
*opthdr
=
151 (IMAGE_OPTIONAL_HEADER32
*)&assembly
->nthdr
->OptionalHeader
;
152 datadirs
= opthdr
->DataDirectory
;
158 if (!datadirs
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
].VirtualAddress
||
159 !datadirs
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
].Size
)
164 assembly
->corhdr
= ImageRvaToVa(assembly
->nthdr
, assembly
->data
,
165 datadirs
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
].VirtualAddress
, NULL
);
166 if (!assembly
->corhdr
)
172 HRESULT
assembly_create(ASSEMBLY
**out
, LPCWSTR file
)
179 assembly
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(ASSEMBLY
));
181 return E_OUTOFMEMORY
;
183 assembly
->path
= strdupW(file
);
190 assembly
->hfile
= CreateFileW(file
, GENERIC_READ
, FILE_SHARE_READ
,
191 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
192 if (assembly
->hfile
== INVALID_HANDLE_VALUE
)
194 hr
= HRESULT_FROM_WIN32(GetLastError());
198 assembly
->hmap
= CreateFileMappingW(assembly
->hfile
, NULL
, PAGE_READONLY
,
202 hr
= HRESULT_FROM_WIN32(GetLastError());
206 assembly
->data
= MapViewOfFile(assembly
->hmap
, FILE_MAP_READ
, 0, 0, 0);
209 hr
= HRESULT_FROM_WIN32(GetLastError());
213 hr
= parse_pe_header(assembly
);
214 if (FAILED(hr
)) goto failed
;
216 hr
= parse_clr_metadata(assembly
);
217 if (FAILED(hr
)) goto failed
;
223 assembly_release(assembly
);
227 HRESULT
assembly_release(ASSEMBLY
*assembly
)
232 HeapFree(GetProcessHeap(), 0, assembly
->metadatahdr
);
233 HeapFree(GetProcessHeap(), 0, assembly
->path
);
234 UnmapViewOfFile(assembly
->data
);
235 CloseHandle(assembly
->hmap
);
236 CloseHandle(assembly
->hfile
);
237 HeapFree(GetProcessHeap(), 0, assembly
);
242 HRESULT
assembly_get_runtime_version(ASSEMBLY
*assembly
, LPSTR
*version
)
244 *version
= assembly
->metadatahdr
->Version
;