1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4// --------------------------------------------------------------------------------
5// PEInformation.cpp
6//
7
8// --------------------------------------------------------------------------------
9
10#include "stdafx.h"
11#include "utilcode.h"
12#include "peinformation.h"
13
14
15HRESULT TranslatePEToArchitectureType(CorPEKind CLRPeKind, DWORD dwImageType, PEKIND * pPeKind)
16{
17 return TranslatePEToArchitectureType(CLRPeKind, dwImageType, 0, pPeKind);
18}
19
20HRESULT TranslatePEToArchitectureType(CorPEKind CLRPeKind, DWORD dwImageType, DWORD dwAssemblyFlags, PEKIND * pPeKind)
21{
22 HRESULT hr = S_OK;
23
24 _ASSERTE(pPeKind != NULL);
25
26 if (CLRPeKind == peNot)
27 { // Not a PE. Shouldn't ever get here.
28 *pPeKind = peInvalid;
29 hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
30 goto Exit;
31 }
32 else if (IsAfPA_NoPlatform(dwAssemblyFlags))
33 {
34 *pPeKind = peNone;
35 goto Exit;
36 }
37 else
38 {
39 if ((CLRPeKind & peILonly) &&
40 !(CLRPeKind & pe32Plus) &&
41 !(CLRPeKind & pe32BitRequired) &&
42 (dwImageType == IMAGE_FILE_MACHINE_I386))
43 {
44 // Processor-agnostic (MSIL)
45 *pPeKind = peMSIL;
46 }
47 else if (CLRPeKind & pe32Plus)
48 {
49 // 64-bit
50
51 if (CLRPeKind & pe32BitRequired)
52 {
53 *pPeKind = peInvalid;
54 hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
55 goto Exit;
56 }
57
58 // Regardless of whether ILONLY is set or not, the architecture
59 // is the machine type.
60
61 if (dwImageType == IMAGE_FILE_MACHINE_IA64)
62 {
63 *pPeKind = peIA64;
64 }
65 else if (dwImageType == IMAGE_FILE_MACHINE_AMD64)
66 {
67 *pPeKind = peAMD64;
68 }
69 else
70 { // We don't support other architectures
71 *pPeKind = peInvalid;
72 hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
73 goto Exit;
74 }
75 }
76 else
77 {
78 // 32-bit, non-agnostic
79
80 if (dwImageType == IMAGE_FILE_MACHINE_I386)
81 {
82 *pPeKind = peI386;
83 }
84 else if (dwImageType == IMAGE_FILE_MACHINE_ARMNT)
85 {
86 *pPeKind = peARM;
87 }
88 else
89 { // Not supported
90 *pPeKind = peInvalid;
91 hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
92 goto Exit;
93 }
94 }
95 }
96
97Exit:
98 return hr;
99}
100