summaryrefslogtreecommitdiff
path: root/src/utilcode/peinformation.cpp
blob: de7bbc61607833616a33bcaaf0974185a8f76ffc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// --------------------------------------------------------------------------------
// PEInformation.cpp
//

// --------------------------------------------------------------------------------

#include "stdafx.h"
#include "utilcode.h"
#include "peinformation.h"


HRESULT TranslatePEToArchitectureType(CorPEKind CLRPeKind, DWORD dwImageType, PEKIND * pPeKind)
{
    return TranslatePEToArchitectureType(CLRPeKind, dwImageType, 0, pPeKind);
}

HRESULT TranslatePEToArchitectureType(CorPEKind CLRPeKind, DWORD dwImageType, DWORD dwAssemblyFlags, PEKIND * pPeKind)
{
    HRESULT hr = S_OK;
    
    _ASSERTE(pPeKind != NULL);
    
    if (CLRPeKind == peNot)
    {   // Not a PE. Shouldn't ever get here.
        *pPeKind = peInvalid;
        fprintf(stderr, "@@[SR] %s:%d\n");
        hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
        goto Exit;
    }
    else if (IsAfPA_NoPlatform(dwAssemblyFlags))
    {
        *pPeKind = peNone;
        goto Exit;
    }
    else
    {
        if ((CLRPeKind & peILonly) && 
            !(CLRPeKind & pe32Plus) && 
            !(CLRPeKind & pe32BitRequired) && 
            (dwImageType == IMAGE_FILE_MACHINE_I386))
        {
            // Processor-agnostic (MSIL)
            *pPeKind = peMSIL;
        }
        else if (CLRPeKind & pe32Plus)
        {
            // 64-bit

            if (CLRPeKind & pe32BitRequired)
            {
                *pPeKind = peInvalid;
                fprintf(stderr, "@@[SR] %s:%d\n");
                hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
                goto Exit;
            }

            // Regardless of whether ILONLY is set or not, the architecture
            // is the machine type.

            if (dwImageType == IMAGE_FILE_MACHINE_IA64)
            {
                *pPeKind = peIA64;
            }
            else if (dwImageType == IMAGE_FILE_MACHINE_AMD64)
            {
                *pPeKind = peAMD64;
            }
            else
            {   // We don't support other architectures
                *pPeKind = peInvalid;
                fprintf(stderr, "@@[SR] %s:%d\n");
                hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
                goto Exit;
            }
        }
        else
        {
            // 32-bit, non-agnostic

            if (dwImageType == IMAGE_FILE_MACHINE_I386)
            {
                *pPeKind = peI386;
            }
            else if (dwImageType == IMAGE_FILE_MACHINE_ARMNT)
            {
                *pPeKind = peARM;
            }
            else
            {   // Not supported
                *pPeKind = peInvalid;
                fprintf(stderr, "@@[SR] %s:%d\n");
                hr = HRESULT_FROM_WIN32(ERROR_BAD_FORMAT);
                goto Exit;
            }
        }
    }

Exit:
    return hr;
}