summaryrefslogtreecommitdiff
path: root/src/ildasm/ceeload.cpp
blob: a78ff72c4433b4420f840417150d4f0b3e53583a (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// 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.

//
// CEELOAD reads in the PE file format using LoadLibrary
// ===========================================================================
#include "ildasmpch.h"

#include "ceeload.h"
#include <corhdr.h>
#include <corimage.h>
#include "util.hpp"
#include "pedecoder.h"

/*************************************************************************************/
// Constructor and destructor!
/*************************************************************************************/
PELoader::PELoader()
{
    m_hFile = NULL;
    m_hMod = NULL;
    m_hMapFile = NULL;
    m_pNT64 = NULL;
    m_bIsPE32 = FALSE;
    m_FileSize = m_FileSizeAligned = 0;
}

PELoader::~PELoader()
{

    m_hMod = NULL;
    m_pNT64 = NULL;
    // If we have an hFile then we opened this file ourselves!
    // If we do not then this file was loaded by the OS and the OS will
    // close it for us.
    if (m_hFile)
        this->close();
}

/*************************************************************************************/
/*************************************************************************************/
void PELoader::close()
{

    // _ASSERTE(m_hFile != NULL);
    if (m_hFile)
    {
        if (m_hMod)
            UnmapViewOfFile((void*)m_hMod);
        if (m_hMapFile)
            CloseHandle(m_hMapFile);
        CloseHandle(m_hFile);

        m_hMod = NULL;
        m_hMapFile = NULL;
        m_hFile = NULL;
        m_FileSize = m_FileSizeAligned = 0;
    }
}


BOOL PELoader::open(LPCSTR moduleName)
{
    HMODULE newhMod = NULL;
    DWORD dwFileSizeLow;

    _ASSERTE(moduleName);
    if (!moduleName)
        return FALSE;


    m_hFile = CreateFileA(moduleName, GENERIC_READ, FILE_SHARE_READ,
                         0, OPEN_EXISTING, 0, 0);
    if (m_hFile == INVALID_HANDLE_VALUE)
        return FALSE;

    dwFileSizeLow = GetFileSize( m_hFile, NULL); 
    if (dwFileSizeLow == INVALID_FILE_SIZE)
        return FALSE;
    m_FileSize = dwFileSizeLow;

    m_hMapFile = WszCreateFileMapping(m_hFile, NULL, PAGE_READONLY, 0, 0, NULL);
    if (m_hMapFile == NULL)
        return FALSE;

    newhMod = (HMODULE) MapViewOfFile(m_hMapFile, FILE_MAP_READ, 0, 0, 0);
    if (newhMod == NULL)
        return FALSE;
   return open(newhMod);
}

BOOL PELoader::open(const WCHAR* moduleName)
{
    HMODULE newhMod = NULL;
    DWORD dwFileSizeLow;

    _ASSERTE(moduleName);
    if (!moduleName)
        return FALSE;

    m_hFile = WszCreateFile(moduleName, GENERIC_READ, FILE_SHARE_READ,
                         0, OPEN_EXISTING, 0, 0);
    if (m_hFile == INVALID_HANDLE_VALUE)
        return FALSE;

    dwFileSizeLow = GetFileSize( m_hFile, NULL); 
    if (dwFileSizeLow == INVALID_FILE_SIZE)
        return FALSE;
    m_FileSize = dwFileSizeLow;

    m_hMapFile = WszCreateFileMapping(m_hFile, NULL, PAGE_READONLY, 0, 0, NULL);
    if (m_hMapFile == NULL)
        return FALSE;

    newhMod = (HMODULE) MapViewOfFile(m_hMapFile, FILE_MAP_READ, 0, 0, 0);
    if (newhMod == NULL)
        return FALSE;
   return open(newhMod);
}


/*************************************************************************************/
BOOL PELoader::open(HMODULE hMod)
{
    IMAGE_DOS_HEADER * pdosHeader;
    
    // get the dos header...
    m_hMod = hMod;
    pdosHeader = (IMAGE_DOS_HEADER*) hMod;
    // If this is not a PE32+ image
    if (pdosHeader->e_magic == VAL16(IMAGE_DOS_SIGNATURE) &&
        0 < VAL32(pdosHeader->e_lfanew) && VAL32(pdosHeader->e_lfanew) < 0xFF0)   // has to start on first page
    {
        size_t fileAlignment;

        m_pNT32 = (IMAGE_NT_HEADERS32*) ((BYTE *)m_hMod + VAL32(pdosHeader->e_lfanew));

        m_bIsPE32 = (m_pNT32->OptionalHeader.Magic == VAL16(IMAGE_NT_OPTIONAL_HDR32_MAGIC));

        if (m_bIsPE32)
        {
            if ((m_pNT32->Signature != VAL32(IMAGE_NT_SIGNATURE)) ||
                (m_pNT32->FileHeader.SizeOfOptionalHeader != VAL16(sizeof(IMAGE_OPTIONAL_HEADER32))))
            {
                // Make this appear uninitalized because for some reason this file is toasted.
                m_pNT32 = NULL;
                m_hMod = NULL;
                return FALSE;
            }
            fileAlignment = VAL32(m_pNT32->OptionalHeader.FileAlignment)-1;
        }
        else //For now assume not i386 is IA64
        {
            if ((m_pNT64->Signature != VAL32(IMAGE_NT_SIGNATURE)) ||
                (m_pNT64->FileHeader.SizeOfOptionalHeader != VAL16(sizeof(IMAGE_OPTIONAL_HEADER64))))
            {
                // Make this appear uninitalized because for some reason this file is toasted.
                m_pNT64 = NULL;
                m_hMod = NULL;
                return FALSE;
            }
            fileAlignment = VAL32(m_pNT64->OptionalHeader.FileAlignment)-1;
        }
        m_FileSizeAligned = (m_FileSize + fileAlignment)&(~fileAlignment);
    }
    else
    {
        // Make this appear uninitalized because for some reason this file is toasted.
        m_hMod = NULL;
        return FALSE;
    }
    return TRUE;
}

/*************************************************************************************/
void PELoader::dump()
{
}

/*************************************************************************************/
BOOL PELoader::getCOMHeader(IMAGE_COR20_HEADER **ppCorHeader)
{
    PIMAGE_SECTION_HEADER   pSectionHeader;

    if (m_bIsPE32)
    {
        PIMAGE_NT_HEADERS32     pImageHeader;
        // Get the image header from the image, then get the directory location
        // of the COM+ header which may or may not be filled out.
        pImageHeader = (PIMAGE_NT_HEADERS32)Cor_RtlImageNtHeader(m_hMod, (ULONG) m_FileSize);
        PREFIX_ASSUME(pImageHeader != NULL);
        pSectionHeader = (PIMAGE_SECTION_HEADER) Cor_RtlImageRvaToVa32(pImageHeader, (PBYTE)m_hMod,
            VAL32(pImageHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COMHEADER].VirtualAddress),
            (DWORD)m_FileSizeAligned /* FileLength */);
    }
    else
    {
        PIMAGE_NT_HEADERS64     pImageHeader;

        // Get the image header from the image, then get the directory location
        // of the COM+ header which may or may not be filled out.
        pImageHeader = (PIMAGE_NT_HEADERS64)Cor_RtlImageNtHeader(m_hMod, (ULONG) m_FileSize);
        PREFIX_ASSUME(pImageHeader != NULL);
        pSectionHeader = (PIMAGE_SECTION_HEADER) Cor_RtlImageRvaToVa64(pImageHeader, (PBYTE)m_hMod,
            VAL32(pImageHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COMHEADER].VirtualAddress),
            (DWORD)m_FileSizeAligned /* FileLength */);
    }

    // If the section header exists, then return ok and the address.
    if (pSectionHeader)
    {
        *ppCorHeader = (IMAGE_COR20_HEADER *) pSectionHeader;
        return TRUE;
    }
    // If there is no COM+ Data in this image, return false.
    else
        return FALSE;
}

/*************************************************************************************/
BOOL PELoader::getVAforRVA(DWORD rva,void **ppva)
{
    PIMAGE_SECTION_HEADER   pSectionHeader;

    if (m_bIsPE32)
    {
        // Get the image header from the image, then get the directory location
        // of the COM+ header which may or may not be filled out.
        PIMAGE_NT_HEADERS32     pImageHeader;
        pImageHeader = (PIMAGE_NT_HEADERS32) Cor_RtlImageNtHeader(m_hMod, (ULONG) m_FileSize);
        PREFIX_ASSUME(pImageHeader != NULL);
        pSectionHeader = (PIMAGE_SECTION_HEADER) Cor_RtlImageRvaToVa32(pImageHeader, (PBYTE)m_hMod,
            rva, (DWORD)m_FileSizeAligned /* FileLength */);
    }
    else
    {
        PIMAGE_NT_HEADERS64     pImageHeader;
        pImageHeader = (PIMAGE_NT_HEADERS64) Cor_RtlImageNtHeader(m_hMod, (ULONG) m_FileSize);
        PREFIX_ASSUME(pImageHeader != NULL);
        pSectionHeader = (PIMAGE_SECTION_HEADER) Cor_RtlImageRvaToVa64(pImageHeader, (PBYTE)m_hMod,
            rva, (DWORD)m_FileSizeAligned /* FileLength */);
    }

    // If the section header exists, then return ok and the address.
    if (pSectionHeader)
    {
        *ppva = pSectionHeader;
        return TRUE;
    }
    // If there is no COM+ Data in this image, return false.
    else
        return FALSE;
}

void SectionInfo::Init(PELoader *pPELoader, IMAGE_DATA_DIRECTORY *dir)
{
    _ASSERTE(dir);
    m_dwSectionOffset = VAL32(dir->VirtualAddress);
    if (m_dwSectionOffset != 0)
        m_pSection = pPELoader->base() + m_dwSectionOffset;
    else
        m_pSection = 0;
    m_dwSectionSize = VAL32(dir->Size);
}