/
CLIFile.cu
361 lines (313 loc) · 10.7 KB
/
CLIFile.cu
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright (c) 2012 DotNetAnywhere
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include <cuda.h>
#include "Compat.h"
#include "Sys.h"
#include "CLIFile.h"
#include "RVA.h"
#include "MetaData.h"
#include "Thread.h"
#include "MetaDataTables.h"
#include "Type.h"
#include <stdio.h>
#include "System.Array.h"
#include "System.String.h"
#include "Gstring.h"
#include "Gprintf.h"
// Is this exe/dll file for the .NET virtual machine?
#define DOT_NET_MACHINE 0x14c
typedef struct tFilesLoaded_ tFilesLoaded;
struct tFilesLoaded_ {
tCLIFile *pCLIFile;
tFilesLoaded *pNext;
};
// Keep track of all the files currently loaded
__device__ static tFilesLoaded *pFilesLoaded = NULL;
__device__ tMetaData* CLIFile_GetMetaDataForAssembly(char *pAssemblyName) {
tFilesLoaded *pFiles;
// Convert "mscorlib" to "corlib"
if (Gstrcmp(pAssemblyName, "mscorlib") == 0) {
pAssemblyName = "corlib";
}
Gprintf("looking at pfiles.\n");
// Look in already-loaded files first
pFiles = pFilesLoaded;
while (pFiles != NULL) {
Gprintf("pFiles not null.\n");
tCLIFile *pCLIFile;
tMD_Assembly *pThisAssembly;
pCLIFile = pFiles->pCLIFile;
// Get the assembly info - there is only ever one of these in the each file's metadata
pThisAssembly = (tMD_Assembly*)MetaData_GetTableRow(pCLIFile->pMetaData, MAKE_TABLE_INDEX(0x20, 1));
if (Gstrcmp(pAssemblyName, pThisAssembly->name) == 0) {
// Found the correct assembly, so return its meta-data
return pCLIFile->pMetaData;
}
pFiles = pFiles->pNext;
}
Gprintf("Doing load\n");
// Assembly not loaded, so load it if possible
{
tCLIFile *pCLIFile;
char fileName[30];
printf("In CLIFile_GetMetaDataForAssembly0\n");
//Gsprintf(fileName, "%s.dll", pAssemblyName);
pCLIFile = CLIFile_Load("corlib");
printf("In CLIFile_GetMetaDataForAssembly1\n");
pCLIFile = CLIFile_Load(pAssemblyName);
printf("In CLIFile_GetMetaDataForAssembly2\n");
if (pCLIFile == NULL) {
Crash("Cannot load required assembly file: %s", fileName);
return NULL;
}
return pCLIFile->pMetaData;
}
}
__device__ unsigned char Gdata[];
__device__ static void* LoadFileFromDisk(char *pFileName) {
int f;
void *pData = NULL;
char buf[1000];
// Crashes! Gprintf("File name = %s\n", pFileName);
if (Gstrcmp("corlib", pFileName) != 0)
return NULL;
//f = open(pFileName, O_RDONLY|O_BINARY);
//if (f >= 0) {
// int len;
// len = lseek(f, 0, SEEK_END);
// lseek(f, 0, SEEK_SET);
// // TODO: Change to use mmap() or windows equivilent
// pData = mallocForever(len);
// if (pData != NULL) {
// int r = read(f, pData, len);
// if (r != len) {
// free(pData);
// pData = NULL;
// }
// }
// close(f);
//}
pData = Gdata;
return pData;
}
__device__ static tCLIFile* LoadPEFile(void *pData) {
printf("LoadPEFile\n");
tCLIFile *pRet = TMALLOC(tCLIFile);
memset(pRet, 0, sizeof(tCLIFile));
printf("LoadPEFile2\n");
unsigned char *pMSDOSHeader = (unsigned char*)&(((unsigned char*)pData)[0]);
unsigned char *pPEHeader;
unsigned char *pPEOptionalHeader;
unsigned char *pPESectionHeaders;
unsigned char *pCLIHeader;
unsigned char *pRawMetaData;
printf("LoadPEFile2.5\n");
int i;
printf("LoadPEFile2.5a\n");
unsigned int lfanew;
printf("LoadPEFile2.5b\n");
unsigned short machine;
printf("LoadPEFile2.5c\n");
int numSections;
printf("LoadPEFile2.53\n");
unsigned int imageBase;
int fileAlignment;
unsigned int cliHeaderRVA, cliHeaderSize;
unsigned int metaDataRVA, metaDataSize;
tMetaData *pMetaData;
printf("LoadPEFile2.54\n");
printf("In LoadPEFile2.55 %llx\n", pRet);
pRet->pRVA = RVA();
printf("LoadPEFile2.6\n");
pRet->pMetaData = pMetaData = MetaData();
printf("LoadPEFile3\n");
lfanew = GetU32(&(pMSDOSHeader[0x3c]));
pPEHeader = pMSDOSHeader + lfanew + 4;
pPEOptionalHeader = pPEHeader + 20;
pPESectionHeaders = pPEOptionalHeader + 224;
printf("LoadPEFile4\n");
machine = GetU16(&(pPEHeader[0]));
if (machine != DOT_NET_MACHINE) {
Gprintf("Not DOT_NET_MACHINE.\n");
return NULL;
}
printf("LoadPEFile5\n");
numSections = GetU16(&(pPEHeader[2]));
printf("LoadPEFile6\n");
imageBase = GetU32(&(pPEOptionalHeader[28]));
printf("LoadPEFile7\n");
fileAlignment = GetU32(&(pPEOptionalHeader[36]));
printf("LoadPEFile8\n");
for (i=0; i<numSections; i++) {
unsigned char *pSection = pPESectionHeaders + i * 40;
RVA_Create(pRet->pRVA, pData, pSection);
}
Gprintf("C1.\n");
cliHeaderRVA = GetU32(&(pPEOptionalHeader[208]));
cliHeaderSize = GetU32(&(pPEOptionalHeader[212]));
pCLIHeader = (unsigned char *)RVA_FindData(pRet->pRVA, cliHeaderRVA);
Gprintf("C2.\n");
metaDataRVA = GetU32(&(pCLIHeader[8]));
metaDataSize = GetU32(&(pCLIHeader[12]));
pRet->entryPoint = GetU32(&(pCLIHeader[20]));
pRawMetaData = (unsigned char*)RVA_FindData(pRet->pRVA, metaDataRVA);
Gprintf("C3.\n");
// Load all metadata
{
unsigned int versionLen = GetU32(&(pRawMetaData[12]));
unsigned int ofs, numberOfStreams;
unsigned char *pTableStream = NULL;
unsigned int tableStreamSize;
pRet->pVersion = &(pRawMetaData[16]);
log_f(1, "CLI version: %s\n", pRet->pVersion);
ofs = 16 + versionLen;
numberOfStreams = GetU16(&(pRawMetaData[ofs + 2]));
ofs += 4;
int q = ofs;
for (i=0; i<(signed)numberOfStreams; i++) {
// Start at ofs and look for '#Strings', '#US', etc. Backup to get offset and size.
for (; ; ++q)
{
if (pRawMetaData[q] == '#')
{
if (pRawMetaData[q + 1] == 'S' && pRawMetaData[q + 2] == 't')
break;
if (pRawMetaData[q + 1] == 'U' && pRawMetaData[q + 2] == 'S')
break;
if (pRawMetaData[q + 1] == 'B' && pRawMetaData[q + 2] == 'l')
break;
if (pRawMetaData[q + 1] == 'G' && pRawMetaData[q + 2] == 'U')
break;
if (pRawMetaData[q + 1] == '~')
break;
}
}
ofs = q - 8;
unsigned int streamOffset = *(unsigned int*)&pRawMetaData[ofs];
unsigned int streamSize = *(unsigned int*)&pRawMetaData[ofs+4];
unsigned char *pStreamName = &pRawMetaData[ofs+8];
unsigned char *pStream = pRawMetaData + streamOffset;
q = q + 1;
//ofs += (unsigned int)((Gstrlen((const char*)pStreamName)+4) & (~0x3)) + 8;
if (Gstrcasecmp((const char*)pStreamName, "#Strings") == 0) {
MetaData_LoadStrings(pMetaData, pStream, streamSize);
} else if (Gstrcasecmp((const char*)pStreamName, "#US") == 0) {
MetaData_LoadUserStrings(pMetaData, pStream, streamSize);
} else if (Gstrcasecmp((const char*)pStreamName, "#Blob") == 0) {
MetaData_LoadBlobs(pMetaData, pStream, streamSize);
} else if (Gstrcasecmp((const char*)pStreamName, "#GUID") == 0) {
MetaData_LoadGUIDs(pMetaData, pStream, streamSize);
} else if (Gstrcasecmp((const char*)pStreamName, "#~") == 0) {
pTableStream = pStream;
tableStreamSize = streamSize;
}
}
// Must load tables last
if (pTableStream != NULL) {
MetaData_LoadTables(pMetaData, pRet->pRVA, pTableStream, tableStreamSize);
}
}
Gprintf("C4.\n");
// Mark all generic definition types and methods as such
//for (i=pMetaData->tables.numRows[MD_TABLE_GENERICPARAM]; i>0; i--) {
// tMD_GenericParam *pGenericParam;
// IDX_TABLE ownerIdx;
// pGenericParam = (tMD_GenericParam*)MetaData_GetTableRow
// (pMetaData, MAKE_TABLE_INDEX(MD_TABLE_GENERICPARAM, i));
// ownerIdx = pGenericParam->owner;
// switch (TABLE_ID(ownerIdx)) {
// case MD_TABLE_TYPEDEF:
// {
// tMD_TypeDef *pTypeDef = (tMD_TypeDef*)MetaData_GetTableRow(pMetaData, ownerIdx);
// pTypeDef->isGenericDefinition = 1;
// }
// break;
// case MD_TABLE_METHODDEF:
// {
// tMD_MethodDef *pMethodDef = (tMD_MethodDef*)MetaData_GetTableRow(pMetaData, ownerIdx);
// pMethodDef->isGenericDefinition = 1;
// }
// break;
// default:
// Crash("Wrong generic parameter owner: 0x%08x", ownerIdx);
// }
//}
Gprintf("C5.\n");
// Mark all nested classes as such
for (i=pMetaData->tables.numRows[MD_TABLE_NESTEDCLASS]; i>0; i--) {
tMD_NestedClass *pNested;
tMD_TypeDef *pParent, *pChild;
pNested = (tMD_NestedClass*)MetaData_GetTableRow(pMetaData, MAKE_TABLE_INDEX(MD_TABLE_NESTEDCLASS, i));
pParent = (tMD_TypeDef*)MetaData_GetTableRow(pMetaData, pNested->enclosingClass);
pChild = (tMD_TypeDef*)MetaData_GetTableRow(pMetaData, pNested->nestedClass);
pChild->pNestedIn = pParent;
}
Gprintf("normal exit LoadPEFile\n");
return pRet;
}
__device__ tCLIFile* CLIFile_Load(char *pFileName) {
void *pRawFile;
tCLIFile *pRet;
tFilesLoaded *pNewFile;
Gprintf("In CLIFile_Load1\n");
pRawFile = LoadFileFromDisk(pFileName);
if (pRawFile == NULL) {
Crash("Cannot load file: %s", pFileName);
}
log_f(1, "\nLoading file: %s\n", pFileName);
pRet = LoadPEFile(pRawFile);
printf("In CLIFile_Load2\n");
pRet->pFileName = (char*)mallocForever((U32)Gstrlen(pFileName) + 1);
Gstrcpy(pRet->pFileName, pFileName);
// Record that we've loaded this file
pNewFile = TMALLOCFOREVER(tFilesLoaded);
pNewFile->pCLIFile = pRet;
pNewFile->pNext = pFilesLoaded;
pFilesLoaded = pNewFile;
return pRet;
}
__device__ I32 CLIFile_Execute(tCLIFile *pThis, int argc, char **argp) {
//tThread *pThread;
//HEAP_PTR args;
//int i;
//// Create a string array for the program arguments
//// Don't include the argument that is the program name.
//argc--;
//argp++;
//args = SystemArray_NewVector(types[TYPE_SYSTEM_ARRAY_STRING], argc);
//Heap_MakeUndeletable(args);
//for (i = 0; i < argc; i++) {
// HEAP_PTR arg = SystemString_FromCharPtrASCII(argp[i]);
// SystemArray_StoreElement(args, i, (PTR)&arg);
//}
//// Create the main application thread
//pThread = Thread();
//Thread_SetEntryPoint(pThread, pThis->pMetaData, pThis->entryPoint, (PTR)&args, sizeof(void*));
//return Thread_Execute();
return 0;
}
__device__ void CLIFile_GetHeapRoots(tHeapRoots *pHeapRoots) {
//tFilesLoaded *pFile;
//pFile = pFilesLoaded;
//while (pFile != NULL) {
// MetaData_GetHeapRoots(pHeapRoots, pFile->pCLIFile->pMetaData);
// pFile = pFile->pNext;
//}
}